Skip to content

Add v2 Remote Config Triggers #1216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
### Breaking Changes

- Deprecated `allowInvalidAppCheckToken` option. Instead use
`enforceAppCheck`.

> App Check enforcement on callable functions is disabled by default in v4.
Requests containing invalid App Check tokens won't be denied unless you
explicitly enable App Check enforcement using the new `enforceAppCheck` option.
Furthermore, when enforcement is enabled, callable functions will deny
all requests without App Check tokens.
> Requests containing invalid App Check tokens won't be denied unless you
> explicitly enable App Check enforcement using the new `enforceAppCheck` option.
> Furthermore, when enforcement is enabled, callable functions will deny
> all requests without App Check tokens.

- Dropped support for Node.js versions 8, 10, and 12.
- Dropped support for Admin SDK versions 8 and 9.
Expand All @@ -16,9 +17,10 @@
removed.
- Removed `__trigger` object on function handlers.
- Reorganized source code location. This affects only apps that directly import files instead of using the recommend entry points specified in the
- Reworked the `apps` library and removed `lodash` as a runtime dependency.
- Reworked the `apps` library and removed `lodash` as a runtime dependency.

### Enhancements

- Logs created with the `functions.logger` package in v2 functions
are now annotated with each request's trace ID, making it easy to correlate
log entries with the incoming request. Trace IDs are especially useful for
Expand All @@ -28,5 +30,3 @@
- `functions.logger.error` now always outputs an error object and is included in Google Cloud Error Reporting.
- The logging severity of Auth/App Check token validation has changed from `info` to `debug` level.
- Event parameters for 2nd generation functions are now strongly typed, permitting stronger TypeScript types for matched parameters.


6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"./v2/eventarc": "./lib/v2/providers/eventarc.js",
"./v2/identity": "./lib/v2/providers/identity.js",
"./v2/database": "./lib/v2/providers/database.js",
"./v2/scheduler": "./lib/v2/providers/scheduler.js"
"./v2/scheduler": "./lib/v2/providers/scheduler.js",
"./v2/remoteConfig": "./lib/v2/providers/remoteConfig.js"
},
"typesVersions": {
"*": {
Expand Down Expand Up @@ -142,6 +143,9 @@
],
"v2/scheduler": [
"lib/v2/providers/scheduler"
],
"v2/remoteConfig": [
"lib/v2/providers/remoteConfig"
]
}
},
Expand Down
74 changes: 74 additions & 0 deletions spec/v2/providers/remoteConfig.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// The MIT License (MIT)
//
// Copyright (c) 2022 Firebase
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import { expect } from "chai";
import * as remoteConfig from "../../../src/v2/providers/remoteConfig";
import * as options from "../../../src/v2/options";

describe("onConfigUpdated", () => {
afterEach(() => {
options.setGlobalOptions({});
});

it("should create a function with a handler", () => {
const fn = remoteConfig.onConfigUpdated(() => 2);

expect(fn.__endpoint).to.deep.eq({
platform: "gcfv2",
labels: {},
eventTrigger: {
eventType: remoteConfig.eventType,
eventFilters: {},
retry: false,
},
});
expect(fn.run(1 as any)).to.eq(2);
});

it("should create a function with opts and a handler", () => {
options.setGlobalOptions({
memory: "512MiB",
region: "us-west1",
});

const fn = remoteConfig.onConfigUpdated(
{
region: "us-central1",
retry: true,
},
() => 2
);

expect(fn.__endpoint).to.deep.eq({
platform: "gcfv2",
availableMemoryMb: 512,
region: ["us-central1"],
labels: {},
eventTrigger: {
eventType: remoteConfig.eventType,
eventFilters: {},
retry: true,
},
});
expect(fn.run(1 as any)).to.eq(2);
});
});
15 changes: 14 additions & 1 deletion src/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,21 @@ import * as pubsub from "./providers/pubsub";
import * as scheduler from "./providers/scheduler";
import * as storage from "./providers/storage";
import * as tasks from "./providers/tasks";
import * as remoteConfig from "./providers/remoteConfig";

export { alerts, database, storage, https, identity, pubsub, logger, tasks, eventarc, scheduler };
export {
alerts,
database,
storage,
https,
identity,
pubsub,
logger,
tasks,
eventarc,
scheduler,
remoteConfig,
};

export {
setGlobalOptions,
Expand Down
156 changes: 156 additions & 0 deletions src/v2/providers/remoteConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// The MIT License (MIT)
//
// Copyright (c) 2022 Firebase
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import { ManifestEndpoint } from "../../runtime/manifest";
import { CloudEvent, CloudFunction } from "../core";
import { EventHandlerOptions, getGlobalOptions, optionsToEndpoint } from "../options";

/** @internal */
export const eventType = "google.firebase.remoteconfig.remoteConfig.v1.updated";

/* All the fields associated with the person/service account that wrote a Remote Config template. */
export interface ConfigUser {
/* Display name. */
name: string;

/* Email address. */
email: string;

/* Image URL. */
imageUrl: string;
}

/* What type of update was associated with the Remote Config template version. */
export type ConfigUpdateOrigin =
/* Catch-all for unrecognized values. */
| "REMOTE_CONFIG_UPDATE_ORIGIN_UNSPECIFIED"
/* The update came from the Firebase UI. */
| "CONSOLE"
/* The update came from the Remote Config REST API. */
| "REST_API"
/* The update came from the Firebase Admin Node SDK. */
| "ADMIN_SDK_NODE";

/* Where the Remote Config update action originated. */
export type ConfigUpdateType =
/* Catch-all for unrecognized enum values */
| "REMOTE_CONFIG_UPDATE_TYPE_UNSPECIFIED"
/* A regular incremental update */
| "INCREMENTAL_UPDATE"
/* A forced update. The ETag was specified as "*" in an UpdateRemoteConfigRequest request or the "Force Update" button was pressed on the console */
| "FORCED_UPDATE"
/* A rollback to a previous Remote Config template */
| "ROLLBACK";

/* The data within Firebase Remote Config update events. */
export interface ConfigUpdateData {
/* The version number of the version's corresponding Remote Config template. */
versionNumber: number;

/* When the Remote Config template was written to the Remote Config server. */
updateTime: string;

/* Aggregation of all metadata fields about the account that performed the update. */
updateUser: ConfigUser;

/* The user-provided description of the corresponding Remote Config template. */
description: string;

/* Where the update action originated. */
updateOrigin: ConfigUpdateOrigin;

/* What type of update was made. */
updateType: ConfigUpdateType;

/* Only present if this version is the result of a rollback, and will be the version number of the Remote Config template that was rolled-back to. */
rollbackSource: number;
}

/**
* Event handler which triggers when data is updated in a Remote Config.
*
* @param handler - Event handler which is run every time a Remote Config update occurs.
* @returns A Cloud Function that you can export and deploy.
* @alpha
*/
export function onConfigUpdated(
handler: (event: CloudEvent<ConfigUpdateData>) => any | Promise<any>
): CloudFunction<CloudEvent<ConfigUpdateData>>;

/**
* Event handler which triggers when data is updated in a Remote Config.
*
* @param opts - Options that can be set on an individual event-handling function.
* @param handler - Event handler which is run every time a Remote Config update occurs.
* @returns A Cloud Function that you can export and deploy.
* @alpha
*/
export function onConfigUpdated(
opts: EventHandlerOptions,
handler: (event: CloudEvent<ConfigUpdateData>) => any | Promise<any>
): CloudFunction<CloudEvent<ConfigUpdateData>>;

/**
* Event handler which triggers when data is updated in a Remote Config.
*
* @param optsOrHandler - Options or an event handler.
* @param handler - Event handler which is run every time a Remote Config update occurs.
* @returns A Cloud Function that you can export and deploy.
* @alpha
*/
export function onConfigUpdated(
optsOrHandler:
| EventHandlerOptions
| ((event: CloudEvent<ConfigUpdateData>) => any | Promise<any>),
handler?: (event: CloudEvent<ConfigUpdateData>) => any | Promise<any>
): CloudFunction<CloudEvent<ConfigUpdateData>> {
if (typeof optsOrHandler === "function") {
handler = optsOrHandler as (event: CloudEvent<ConfigUpdateData>) => any | Promise<any>;
optsOrHandler = {};
}

const baseOpts = optionsToEndpoint(getGlobalOptions());
const specificOpts = optionsToEndpoint(optsOrHandler);

const func: any = (raw: CloudEvent<unknown>) => {
return handler(raw as CloudEvent<ConfigUpdateData>);
};
func.run = handler;

const ep: ManifestEndpoint = {
platform: "gcfv2",
...baseOpts,
...specificOpts,
labels: {
...baseOpts?.labels,
...specificOpts?.labels,
},
eventTrigger: {
eventType,
eventFilters: {},
retry: !!optsOrHandler.retry,
},
};
func.__endpoint = ep;

return func;
}
26 changes: 26 additions & 0 deletions v2/remoteConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// The MIT License (MIT)
//
// Copyright (c) 2022 Firebase
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// This file is not part of the firebase-functions SDK. It is used to silence the
// imports eslint plugin until it can understand import paths defined by node
// package exports.
// For more information, see github.com/import-js/eslint-plugin-import/issues/1810