Skip to content

Commit ffa3a57

Browse files
authored
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
1 parent b096107 commit ffa3a57

File tree

176 files changed

+13348
-12229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

176 files changed

+13348
-12229
lines changed

.eslintignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
lib
2+
dev
3+
node_modules
4+
/coverage/
5+
/docgen/
6+
/v1/
7+
/v2/
8+
/logger/
9+
/dist/
10+
/spec/fixtures
11+
/scripts/**/*.js

.eslintrc.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
module.exports = {
2+
env: {
3+
es6: true,
4+
node: true,
5+
},
6+
extends: [
7+
"eslint:recommended",
8+
"plugin:@typescript-eslint/recommended",
9+
"plugin:@typescript-eslint/recommended-requiring-type-checking",
10+
"plugin:jsdoc/recommended",
11+
"google",
12+
"prettier",
13+
],
14+
rules: {
15+
"jsdoc/newline-after-description": "off",
16+
"jsdoc/require-jsdoc": ["warn", { publicOnly: true }],
17+
"no-restricted-globals": ["error", "name", "length"],
18+
"prefer-arrow-callback": "error",
19+
"prettier/prettier": "error",
20+
"require-atomic-updates": "off", // This rule is so noisy and isn't useful: https://github.com/eslint/eslint/issues/11899
21+
"require-jsdoc": "off", // This rule is deprecated and superseded by jsdoc/require-jsdoc.
22+
"valid-jsdoc": "off", // This is deprecated but included in recommended configs.
23+
24+
"no-prototype-builtins": "warn",
25+
"no-useless-escape": "warn",
26+
"prefer-promise-reject-errors": "warn",
27+
},
28+
overrides: [
29+
{
30+
files: ["*.ts"],
31+
rules: {
32+
"jsdoc/require-param-type": "off",
33+
"jsdoc/require-returns-type": "off",
34+
35+
// Google style guide allows us to omit trivial parameters and returns
36+
"jsdoc/require-param": "off",
37+
"jsdoc/require-returns": "off",
38+
39+
"@typescript-eslint/no-invalid-this": "error",
40+
"@typescript-eslint/no-unused-vars": "error", // Unused vars should not exist.
41+
"@typescript-eslint/no-misused-promises": "warn", // rule does not work with async handlers for express.
42+
"no-invalid-this": "off", // Turned off in favor of @typescript-eslint/no-invalid-this.
43+
"no-unused-vars": "off", // Off in favor of @typescript-eslint/no-unused-vars.
44+
eqeqeq: ["error", "always", { null: "ignore" }],
45+
camelcase: ["error", { properties: "never" }], // snake_case allowed in properties iif to satisfy an external contract / style
46+
47+
// Ideally, all these warning should be error - let's fix them in the future.
48+
"@typescript-eslint/no-unsafe-argument": "warn",
49+
"@typescript-eslint/no-unsafe-assignment": "warn",
50+
"@typescript-eslint/no-unsafe-call": "warn",
51+
"@typescript-eslint/no-unsafe-member-access": "warn",
52+
"@typescript-eslint/no-unsafe-return": "warn",
53+
"@typescript-eslint/restrict-template-expressions": "warn",
54+
},
55+
},
56+
{
57+
files: ["*.spec.*"],
58+
env: {
59+
mocha: true,
60+
},
61+
rules: {},
62+
},
63+
],
64+
globals: {},
65+
parserOptions: {
66+
project: "tsconfig.json",
67+
},
68+
plugins: ["prettier", "@typescript-eslint", "jsdoc"],
69+
parser: "@typescript-eslint/parser",
70+
};

.github/ISSUE_TEMPLATE/---report-a-bug.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
2-
name: '⚠️ Report a Bug'
2+
name: "⚠️ Report a Bug"
33
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.
4-
title: ''
5-
labels: ''
6-
assignees: ''
4+
title: ""
5+
labels: ""
6+
assignees: ""
77
---
88

99
<!-- DO NOT DELETE

.github/workflows/postmerge.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ name: Post-merge tests
1616
on:
1717
workflow_dispatch:
1818
workflow_run:
19-
workflows: ['CI Tests']
19+
workflows: ["CI Tests"]
2020
types: [completed]
2121
branches: [master]
2222

@@ -38,16 +38,16 @@ jobs:
3838

3939
- uses: google-github-actions/auth@v0
4040
with:
41-
credentials_json: '${{ secrets.CF3_INTEGRATION_TEST_GOOGLE_CREDENTIALS }}'
41+
credentials_json: "${{ secrets.CF3_INTEGRATION_TEST_GOOGLE_CREDENTIALS }}"
4242
create_credentials_file: true
4343

44-
- name: 'Set up Cloud SDK'
44+
- name: "Set up Cloud SDK"
4545
uses: google-github-actions/setup-gcloud@v0
4646

47-
- name: 'Setup Firebase CLI'
47+
- name: "Setup Firebase CLI"
4848
run: npm i -g firebase-tools
4949

50-
- name: 'Run integration test'
50+
- name: "Run integration test"
5151
run: npm run test:postmerge
5252

5353
- name: Print debug logs

.github/workflows/test.yaml

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,59 @@ env:
88
CI: true
99

1010
jobs:
11+
lint:
12+
runs-on: ubuntu-latest
13+
if: github.event_name == 'pull_request'
14+
strategy:
15+
matrix:
16+
node-version:
17+
- "16"
18+
steps:
19+
- uses: actions/checkout@v3
20+
with:
21+
fetch-depth: 0
22+
- uses: actions/setup-node@v3
23+
with:
24+
node-version: ${{ matrix.node-version }}
25+
cache: npm
26+
- run: npm ci
27+
- run: npm run lint
1128
unit:
1229
runs-on: ubuntu-latest
1330
strategy:
1431
matrix:
1532
node-version:
16-
- 10.x
17-
- 12.x
1833
- 14.x
1934
- 16.x
2035
steps:
2136
- uses: actions/checkout@v1
2237
- uses: actions/setup-node@v1
2338
with:
2439
node-version: ${{ matrix.node-version }}
25-
2640
- name: Cache npm
2741
uses: actions/cache@v1
2842
with:
2943
path: ~/.npm
3044
key: ${{ runner.os }}-node-${{ matrix.node-version }}-${{ hashFiles('**/package-lock.json') }}
31-
3245
- run: npm ci
33-
- run: npm run lint
34-
- run: npm run format
3546
- run: npm run test
3647
integration:
48+
needs: "unit"
3749
runs-on: ubuntu-latest
3850
strategy:
3951
matrix:
4052
node-version:
41-
- 12.x
4253
- 14.x
4354
- 16.x
4455
steps:
4556
- uses: actions/checkout@v1
4657
- uses: actions/setup-node@v1
4758
with:
4859
node-version: ${{ matrix.node-version }}
49-
5060
- name: Cache npm
5161
uses: actions/cache@v1
5262
with:
5363
path: ~/.npm
5464
key: ${{ runner.os }}-node-${{ matrix.node-version }}-${{ hashFiles('**/package-lock.json') }}
55-
56-
- run: npm install
65+
- run: npm ci
5766
- run: npm run test:bin

.mocharc.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ extension:
44
package: ./package.json
55
reporter: spec
66
require:
7-
- 'ts-node/register'
8-
- 'source-map-support/register'
7+
- "ts-node/register"
8+
- "source-map-support/register"

.prettierignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
lib
2-
package.json
3-
spec/fixtures/credential/unparsable.key.json
1+
/node_modules
2+
/lib/**/*
3+
/CONTRIBUTING.md
4+
/docgen

.prettierrc

Lines changed: 0 additions & 5 deletions
This file was deleted.

.prettierrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
printWidth: 100,
3+
};

CHANGELOG.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
### Breaking Changes
2+
3+
- Deprecated `allowInvalidAppCheckToken` option. Instead use
4+
`enforceAppCheck`.
5+
6+
> App Check enforcement on callable functions is disabled by default in v4.
7+
> Requests containing invalid App Check tokens won't be denied unless you
8+
> explicitly enable App Check enforcement using the new `enforceAppCheck` option.
9+
> Furthermore, when enforcement is enabled, callable functions will deny
10+
> all requests without App Check tokens.
11+
12+
- Dropped support for Node.js versions 8, 10, and 12.
13+
- Dropped support for Admin SDK versions 8 and 9.
14+
- Removed the `functions.handler` namespace.
15+
- `DataSnapshot` passed to the Firebase Realtime Database trigger now
16+
matches the `DataSnapshot` returned by the Admin SDK, with null values
17+
removed.
18+
- Removed `__trigger` object on function handlers.
19+
- Reorganized source code location. This affects only apps that directly import files instead of using the recommend entry points specified in the
20+
- Reworked the `apps` library and removed `lodash` as a runtime dependency.
21+
22+
### Enhancements
23+
24+
- Logs created with the `functions.logger` package in v2 functions
25+
are now annotated with each request's trace ID, making it easy to correlate
26+
log entries with the incoming request. Trace IDs are especially useful for
27+
cases where 2nd gen's concurrency feature permits a function
28+
to handle multiple requests at any given time. See
29+
[Correlate log entries](https://cloud.google.com/logging/docs/view/correlate-logs) to learn more.
30+
- `functions.logger.error` now always outputs an error object and is included in Google Cloud Error Reporting.
31+
- The logging severity of Auth/App Check token validation has changed from `info` to `debug` level.
32+
- Event parameters for 2nd generation functions are now strongly typed, permitting stronger TypeScript types for matched parameters.

README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ _Please avoid double posting across multiple channels!_
2323

2424
```js
2525
// functions/index.js
26-
const functions = require('firebase-functions');
27-
const notifyUsers = require('./notify-users');
28-
29-
exports.newPost = functions.database
30-
.ref('/posts/{postId}')
31-
.onCreate((snapshot, context) => {
32-
functions.logger.info('Received new post with ID:', context.params.postId);
33-
return notifyUsers(snapshot.val());
34-
});
26+
const functions = require("firebase-functions");
27+
const notifyUsers = require("./notify-users");
28+
29+
exports.newPost = functions.database.ref("/posts/{postId}").onCreate((snapshot, context) => {
30+
functions.logger.info("Received new post with ID:", context.params.postId);
31+
return notifyUsers(snapshot.val());
32+
});
3533
```
3634

3735
## Contributing

0 commit comments

Comments
 (0)