From ffa3a574087e12e0fd866d504328b6a6e3d7f344 Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Thu, 13 Oct 2022 12:03:42 -0700 Subject: [PATCH] Functions SDK v4 (#1161) ### 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. - Dropped support for Node.js versions 8, 10, and 12. - Dropped support for Admin SDK versions 8 and 9. - Removed the `functions.handler` namespace. - `DataSnapshot` passed to the Firebase Realtime Database trigger now matches the `DataSnapshot` returned by the Admin SDK, with null values 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. - Unspecified function configuration value will be reset to platform default. Use `preserveExternalChanges` to prevent this behavior. ### 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 cases where 2nd gen's concurrency feature permits a function to handle multiple requests at any given time. See [Correlate log entries](https://cloud.google.com/logging/docs/view/correlate-logs) to learn more. - `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. - Add new params package to support parameterized environment configuration. See https://firebase.google.com/docs/functions/config-env for more information. - Add new `functions.RESET_VALUE` and `functions.v2.options.RESET_VALUE` sentinel value for explicitly resetting function configuration to platform default. - Add new `preserveExternalChanges` option to prevent Firebase CLI from resetting unspecified configuration option to platform default --- .eslintignore | 11 + .eslintrc.js | 70 + .github/ISSUE_TEMPLATE/---report-a-bug.md | 8 +- .github/workflows/postmerge.yaml | 10 +- .github/workflows/test.yaml | 29 +- .mocharc.yaml | 4 +- .prettierignore | 7 +- .prettierrc | 5 - .prettierrc.js | 3 + CHANGELOG.md | 32 + README.md | 16 +- docgen/api-extractor.base.json | 728 ++-- docgen/api-extractor.v1.json | 2 +- docgen/generate-docs.js | 383 -- docgen/toc.ts | 216 +- docgen/type-aliases.json | 5 - docgen/typedoc.js | 26 - integration_test/firebase.json | 2 + integration_test/functions/src/index.ts | 173 +- integration_test/functions/src/region.ts | 2 +- integration_test/functions/src/testing.ts | 34 +- .../functions/src/v1/auth-tests.ts | 68 +- .../functions/src/v1/database-tests.ts | 47 +- .../functions/src/v1/firestore-tests.ts | 30 +- .../functions/src/v1/https-tests.ts | 14 +- integration_test/functions/src/v1/index.ts | 17 +- .../functions/src/v1/pubsub-tests.ts | 62 +- .../functions/src/v1/remoteConfig-tests.ts | 39 +- .../functions/src/v1/storage-tests.ts | 20 +- .../functions/src/v1/testLab-tests.ts | 18 +- .../functions/src/v1/testLab-utils.ts | 38 +- .../functions/src/v2/https-tests.ts | 12 +- integration_test/functions/src/v2/index.ts | 9 +- .../functions/src/v2/scheduled-tests.ts | 22 +- mocha/setup.ts | 6 +- package-lock.json | 3410 ++++++++++++++--- package.json | 125 +- scripts/bin-test/mocha-setup.ts | 4 +- .../sources/commonjs-preserve/index.js | 18 + .../sources/commonjs-preserve/package.json | 3 + scripts/bin-test/test.ts | 167 +- scripts/publish-container/cloudbuild.yaml | 6 +- scripts/publish/cloudbuild.yaml | 130 +- spec/common/change.spec.ts | 72 +- spec/common/config.spec.ts | 72 + spec/common/encoding.spec.ts | 48 +- spec/common/metaprogramming.ts | 25 + spec/common/options.ts | 36 + spec/common/params.spec.ts | 109 + spec/common/providers/https.spec.ts | 735 ++-- spec/common/providers/identity.spec.ts | 676 ++-- spec/common/providers/tasks.spec.ts | 130 +- spec/common/trace.spec.ts | 103 + spec/common/utilities/path-pattern.spec.ts | 129 + spec/common/utilities/path.spec.ts | 24 + spec/fixtures.ts | 64 + spec/fixtures/mockrequest.ts | 3 +- spec/fixtures/sources/commonjs-grouped/g1.js | 2 +- .../sources/commonjs-grouped/index.js | 4 +- .../sources/commonjs-main/functions.js | 4 +- .../fixtures/sources/commonjs-params/index.js | 35 +- spec/fixtures/sources/commonjs/index.js | 4 +- spec/helper.ts | 21 +- spec/logger.spec.ts | 120 +- spec/params/params.spec.ts | 292 ++ spec/runtime/loader.spec.ts | 251 +- spec/runtime/manifest.spec.ts | 139 +- spec/utilities/path-pattern.spec.ts | 145 - spec/utilities/path.spec.ts | 24 - spec/v1/apps.spec.ts | 143 - spec/v1/cloud-functions.spec.ts | 279 +- spec/v1/config.spec.ts | 110 +- spec/v1/function-builder.spec.ts | 356 +- spec/v1/providers/analytics.spec.input.ts | 64 +- spec/v1/providers/analytics.spec.ts | 272 +- spec/v1/providers/auth.spec.ts | 285 +- spec/v1/providers/database.spec.ts | 890 ++--- spec/v1/providers/firestore.spec.ts | 485 +-- spec/v1/providers/fixtures.ts | 50 + spec/v1/providers/https.spec.ts | 147 +- spec/v1/providers/pubsub.spec.ts | 408 +- spec/v1/providers/remoteConfig.spec.ts | 127 +- spec/v1/providers/storage.spec.ts | 531 +-- spec/v1/providers/tasks.spec.ts | 93 +- spec/v1/providers/testLab.spec.ts | 206 +- spec/v1/utils.spec.ts | 22 +- spec/v2/params.spec.ts | 0 spec/v2/providers/alerts/alerts.spec.ts | 121 +- .../providers/alerts/appDistribution.spec.ts | 102 +- spec/v2/providers/alerts/billing.spec.ts | 65 +- spec/v2/providers/alerts/crashlytics.spec.ts | 106 +- spec/v2/providers/alerts/performance.spec.ts | 85 +- spec/v2/providers/database.spec.ts | 426 +- spec/v2/providers/eventarc.spec.ts | 78 +- spec/v2/providers/fixtures.ts | 61 +- spec/v2/providers/https.spec.ts | 307 +- spec/v2/providers/identity.spec.ts | 145 +- spec/v2/providers/pubsub.spec.ts | 143 +- spec/v2/providers/remoteConfig.spec.ts | 77 + spec/v2/providers/scheduler.spec.ts | 103 +- spec/v2/providers/storage.spec.ts | 522 +-- spec/v2/providers/tasks.spec.ts | 121 +- spec/v2/providers/testLab.spec.ts | 74 + src/apps.ts | 134 - src/bin/firebase-functions.ts | 35 +- src/common/app.ts | 69 + src/common/change.ts | 29 +- src/common/config.ts | 57 + src/common/debug.ts | 4 +- src/common/encoding.ts | 43 +- src/common/options.ts | 48 + src/common/params.ts | 87 + src/common/providers/database.ts | 133 +- src/common/providers/https.ts | 396 +- src/common/providers/identity.ts | 218 +- src/common/providers/tasks.ts | 48 +- src/common/timezone.ts | 1076 +++--- src/common/trace.ts | 82 + src/{ => common}/utilities/assertions.ts | 4 +- src/{ => common/utilities}/encoder.ts | 2 +- src/{ => common}/utilities/path-pattern.ts | 41 +- src/{ => common}/utilities/path.ts | 12 +- .../common/utilities/utils.ts | 52 +- src/config.ts | 138 - src/function-configuration.ts | 180 - src/handler-builder.ts | 379 -- src/logger/common.ts | 24 +- src/logger/compat.ts | 30 +- src/logger/index.ts | 86 +- src/{v2 => }/params/index.ts | 74 +- src/params/types.ts | 434 +++ src/runtime/loader.ts | 47 +- src/runtime/manifest.ts | 233 +- src/setup.ts | 67 - src/types/global.d.ts | 1 + src/{ => v1}/cloud-functions.ts | 471 +-- src/v1/config.ts | 80 + src/{ => v1}/function-builder.ts | 214 +- src/v1/function-configuration.ts | 269 ++ src/v1/handler-builder.ts | 0 src/{ => v1}/index.ts | 42 +- src/{ => v1}/providers/analytics.ts | 175 +- src/{ => v1}/providers/auth.ts | 125 +- src/{ => v1}/providers/database.ts | 145 +- src/{ => v1}/providers/firestore.ts | 168 +- src/{ => v1}/providers/https.ts | 57 +- src/{ => v1}/providers/pubsub.ts | 78 +- src/{ => v1}/providers/remoteConfig.ts | 48 +- src/{ => v1}/providers/storage.ts | 108 +- src/{ => v1}/providers/tasks.ts | 64 +- src/{ => v1}/providers/testLab.ts | 245 +- src/v2/core.ts | 39 +- src/v2/index.ts | 31 +- src/v2/options.ts | 269 +- src/v2/params/types.ts | 294 -- src/v2/providers/alerts/alerts.ts | 80 +- src/v2/providers/alerts/appDistribution.ts | 120 +- src/v2/providers/alerts/billing.ts | 51 +- src/v2/providers/alerts/crashlytics.ts | 189 +- src/v2/providers/alerts/index.ts | 10 +- src/v2/providers/alerts/performance.ts | 50 +- src/v2/providers/database.ts | 249 +- src/v2/providers/eventarc.ts | 72 +- src/v2/providers/https.ts | 202 +- src/v2/providers/identity.ts | 122 +- src/v2/providers/pubsub.ts | 99 +- src/v2/providers/remoteConfig.ts | 154 + src/v2/providers/scheduler.ts | 98 +- src/v2/providers/storage.ts | 151 +- src/v2/providers/tasks.ts | 130 +- src/v2/providers/testLab.ts | 214 ++ src/v2/trace.ts | 33 + tsconfig.json | 6 +- tsconfig.release.json | 6 +- src/utils.ts => v2/remoteConfig.js | 28 +- v2/testLab.js | 26 + 176 files changed, 13348 insertions(+), 12229 deletions(-) create mode 100644 .eslintignore create mode 100644 .eslintrc.js delete mode 100644 .prettierrc create mode 100644 .prettierrc.js delete mode 100644 docgen/generate-docs.js delete mode 100644 docgen/type-aliases.json delete mode 100644 docgen/typedoc.js create mode 100644 scripts/bin-test/sources/commonjs-preserve/index.js create mode 100644 scripts/bin-test/sources/commonjs-preserve/package.json create mode 100644 spec/common/config.spec.ts create mode 100644 spec/common/metaprogramming.ts create mode 100644 spec/common/options.ts create mode 100644 spec/common/params.spec.ts create mode 100644 spec/common/trace.spec.ts create mode 100644 spec/common/utilities/path-pattern.spec.ts create mode 100644 spec/common/utilities/path.spec.ts create mode 100644 spec/fixtures.ts create mode 100644 spec/params/params.spec.ts delete mode 100644 spec/utilities/path-pattern.spec.ts delete mode 100644 spec/utilities/path.spec.ts delete mode 100644 spec/v1/apps.spec.ts create mode 100644 spec/v1/providers/fixtures.ts create mode 100644 spec/v2/params.spec.ts create mode 100644 spec/v2/providers/remoteConfig.spec.ts create mode 100644 spec/v2/providers/testLab.spec.ts delete mode 100644 src/apps.ts create mode 100644 src/common/app.ts create mode 100644 src/common/config.ts create mode 100644 src/common/options.ts create mode 100644 src/common/params.ts create mode 100644 src/common/trace.ts rename src/{ => common}/utilities/assertions.ts (79%) rename src/{ => common/utilities}/encoder.ts (97%) rename src/{ => common}/utilities/path-pattern.ts (80%) rename src/{ => common}/utilities/path.ts (74%) rename spec/v1/setup.spec.ts => src/common/utilities/utils.ts (58%) delete mode 100644 src/config.ts delete mode 100644 src/handler-builder.ts rename src/{v2 => }/params/index.ts (69%) create mode 100644 src/params/types.ts delete mode 100644 src/setup.ts rename src/{ => v1}/cloud-functions.ts (50%) create mode 100644 src/v1/config.ts rename src/{ => v1}/function-builder.ts (74%) create mode 100644 src/v1/function-configuration.ts create mode 100644 src/v1/handler-builder.ts rename src/{ => v1}/index.ts (61%) rename src/{ => v1}/providers/analytics.ts (76%) rename src/{ => v1}/providers/auth.ts (69%) rename src/{ => v1}/providers/database.ts (73%) rename src/{ => v1}/providers/firestore.ts (60%) rename src/{ => v1}/providers/https.ts (74%) rename src/{ => v1}/providers/pubsub.ts (75%) rename src/{ => v1}/providers/remoteConfig.ts (81%) rename src/{ => v1}/providers/storage.ts (77%) rename src/{ => v1}/providers/tasks.ts (75%) rename src/{ => v1}/providers/testLab.ts (55%) delete mode 100644 src/v2/params/types.ts create mode 100644 src/v2/providers/remoteConfig.ts create mode 100644 src/v2/providers/testLab.ts create mode 100644 src/v2/trace.ts rename src/utils.ts => v2/remoteConfig.js (68%) create mode 100644 v2/testLab.js diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 000000000..72d1288b0 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,11 @@ +lib +dev +node_modules +/coverage/ +/docgen/ +/v1/ +/v2/ +/logger/ +/dist/ +/spec/fixtures +/scripts/**/*.js diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 000000000..f225e5960 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,70 @@ +module.exports = { + env: { + es6: true, + node: true, + }, + extends: [ + "eslint:recommended", + "plugin:@typescript-eslint/recommended", + "plugin:@typescript-eslint/recommended-requiring-type-checking", + "plugin:jsdoc/recommended", + "google", + "prettier", + ], + rules: { + "jsdoc/newline-after-description": "off", + "jsdoc/require-jsdoc": ["warn", { publicOnly: true }], + "no-restricted-globals": ["error", "name", "length"], + "prefer-arrow-callback": "error", + "prettier/prettier": "error", + "require-atomic-updates": "off", // This rule is so noisy and isn't useful: https://github.com/eslint/eslint/issues/11899 + "require-jsdoc": "off", // This rule is deprecated and superseded by jsdoc/require-jsdoc. + "valid-jsdoc": "off", // This is deprecated but included in recommended configs. + + "no-prototype-builtins": "warn", + "no-useless-escape": "warn", + "prefer-promise-reject-errors": "warn", + }, + overrides: [ + { + files: ["*.ts"], + rules: { + "jsdoc/require-param-type": "off", + "jsdoc/require-returns-type": "off", + + // Google style guide allows us to omit trivial parameters and returns + "jsdoc/require-param": "off", + "jsdoc/require-returns": "off", + + "@typescript-eslint/no-invalid-this": "error", + "@typescript-eslint/no-unused-vars": "error", // Unused vars should not exist. + "@typescript-eslint/no-misused-promises": "warn", // rule does not work with async handlers for express. + "no-invalid-this": "off", // Turned off in favor of @typescript-eslint/no-invalid-this. + "no-unused-vars": "off", // Off in favor of @typescript-eslint/no-unused-vars. + eqeqeq: ["error", "always", { null: "ignore" }], + camelcase: ["error", { properties: "never" }], // snake_case allowed in properties iif to satisfy an external contract / style + + // Ideally, all these warning should be error - let's fix them in the future. + "@typescript-eslint/no-unsafe-argument": "warn", + "@typescript-eslint/no-unsafe-assignment": "warn", + "@typescript-eslint/no-unsafe-call": "warn", + "@typescript-eslint/no-unsafe-member-access": "warn", + "@typescript-eslint/no-unsafe-return": "warn", + "@typescript-eslint/restrict-template-expressions": "warn", + }, + }, + { + files: ["*.spec.*"], + env: { + mocha: true, + }, + rules: {}, + }, + ], + globals: {}, + parserOptions: { + project: "tsconfig.json", + }, + plugins: ["prettier", "@typescript-eslint", "jsdoc"], + parser: "@typescript-eslint/parser", +}; diff --git a/.github/ISSUE_TEMPLATE/---report-a-bug.md b/.github/ISSUE_TEMPLATE/---report-a-bug.md index 3ad82bf60..abffad1b7 100644 --- a/.github/ISSUE_TEMPLATE/---report-a-bug.md +++ b/.github/ISSUE_TEMPLATE/---report-a-bug.md @@ -1,9 +1,9 @@ --- -name: '⚠️ Report a Bug' +name: "⚠️ Report a Bug" about: Think you found a bug in the firebase-functions SDK? Report it here. Please do not use this form if your function is deployed successfully but not working as you expected. -title: '' -labels: '' -assignees: '' +title: "" +labels: "" +assignees: "" ---