firebase-functions 3.20.0 → 3.21.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/lib/apps.js +1 -1
  2. package/lib/bin/firebase-functions.js +22 -1
  3. package/lib/cloud-functions.d.ts +56 -35
  4. package/lib/cloud-functions.js +12 -12
  5. package/lib/common/encoding.js +21 -1
  6. package/lib/common/providers/https.d.ts +37 -17
  7. package/lib/common/providers/https.js +11 -6
  8. package/lib/common/providers/identity.d.ts +11 -1
  9. package/lib/common/providers/identity.js +55 -207
  10. package/lib/common/providers/tasks.d.ts +9 -0
  11. package/lib/common/providers/tasks.js +12 -3
  12. package/lib/function-builder.d.ts +2 -2
  13. package/lib/function-builder.js +1 -1
  14. package/lib/handler-builder.js +3 -3
  15. package/lib/index.js +6 -2
  16. package/lib/logger/common.js +21 -0
  17. package/lib/logger/compat.js +22 -1
  18. package/lib/logger/index.d.ts +14 -6
  19. package/lib/logger/index.js +33 -6
  20. package/lib/providers/analytics.js +1 -1
  21. package/lib/providers/auth.d.ts +44 -10
  22. package/lib/providers/auth.js +80 -14
  23. package/lib/providers/database.js +11 -11
  24. package/lib/providers/firestore.js +7 -7
  25. package/lib/providers/https.js +7 -7
  26. package/lib/providers/pubsub.d.ts +6 -6
  27. package/lib/providers/pubsub.js +8 -8
  28. package/lib/providers/remoteConfig.js +1 -1
  29. package/lib/providers/storage.js +2 -2
  30. package/lib/providers/tasks.d.ts +30 -15
  31. package/lib/providers/tasks.js +19 -12
  32. package/lib/providers/testLab.js +1 -1
  33. package/lib/runtime/loader.js +9 -7
  34. package/lib/runtime/manifest.d.ts +5 -0
  35. package/lib/setup.js +3 -3
  36. package/lib/v2/core.d.ts +24 -20
  37. package/lib/v2/index.d.ts +11 -3
  38. package/lib/v2/index.js +12 -3
  39. package/lib/v2/options.d.ts +25 -35
  40. package/lib/v2/options.js +28 -88
  41. package/lib/v2/params/index.d.ts +4 -1
  42. package/lib/v2/params/index.js +25 -1
  43. package/lib/v2/params/types.js +21 -0
  44. package/lib/v2/providers/alerts/alerts.d.ts +107 -8
  45. package/lib/v2/providers/alerts/alerts.js +23 -7
  46. package/lib/v2/providers/alerts/appDistribution.d.ts +112 -12
  47. package/lib/v2/providers/alerts/appDistribution.js +29 -2
  48. package/lib/v2/providers/alerts/billing.d.ts +39 -12
  49. package/lib/v2/providers/alerts/billing.js +38 -1
  50. package/lib/v2/providers/alerts/crashlytics.d.ts +255 -47
  51. package/lib/v2/providers/alerts/crashlytics.js +63 -2
  52. package/lib/v2/providers/alerts/index.d.ts +6 -0
  53. package/lib/v2/providers/alerts/index.js +32 -1
  54. package/lib/v2/providers/eventarc.d.ts +116 -0
  55. package/lib/v2/providers/eventarc.js +69 -0
  56. package/lib/v2/providers/https.d.ts +128 -4
  57. package/lib/v2/providers/https.js +18 -14
  58. package/lib/v2/providers/identity.d.ts +126 -0
  59. package/lib/v2/providers/identity.js +104 -0
  60. package/lib/v2/providers/pubsub.d.ts +125 -8
  61. package/lib/v2/providers/pubsub.js +60 -7
  62. package/lib/v2/providers/storage.d.ts +209 -17
  63. package/lib/v2/providers/storage.js +57 -13
  64. package/lib/v2/providers/tasks.d.ts +107 -7
  65. package/lib/v2/providers/tasks.js +11 -8
  66. package/package.json +22 -3
@@ -0,0 +1,116 @@
1
+ import { CloudEvent, CloudFunction } from '../core';
2
+ import * as options from '../options';
3
+ /** Options that can be set on an Eventarc trigger. */
4
+ export interface EventarcTriggerOptions extends options.EventHandlerOptions {
5
+ /**
6
+ * Type of the event to trigger on.
7
+ */
8
+ eventType: string;
9
+ /**
10
+ * ID of the channel. Can be either:
11
+ * * fully qualified channel resource name:
12
+ * `projects/{project}/locations/{location}/channels/{channel-id}`
13
+ * * partial resource name with location and channel ID, in which case
14
+ * the runtime project ID of the function will be used:
15
+ * `locations/{location}/channels/{channel-id}`
16
+ * * partial channel ID, in which case the runtime project ID of the
17
+ * function and `us-central1` as location will be used:
18
+ * `{channel-id}`
19
+ *
20
+ * If not specified, the default Firebase channel will be used:
21
+ * `projects/{project}/locations/us-central1/channels/firebase`
22
+ */
23
+ channel?: string;
24
+ /**
25
+ * Eventarc event exact match filter.
26
+ */
27
+ filters?: Record<string, string>;
28
+ /**
29
+ * Region where functions should be deployed.
30
+ */
31
+ region?: options.SupportedRegion | string;
32
+ /**
33
+ * Amount of memory to allocate to a function.
34
+ * A value of null restores the defaults of 256MB.
35
+ */
36
+ memory?: options.MemoryOption | null;
37
+ /**
38
+ * Timeout for the function in sections, possible values are 0 to 540.
39
+ * HTTPS functions can specify a higher timeout.
40
+ * A value of null restores the default of 60s
41
+ * The minimum timeout for a gen 2 function is 1s. The maximum timeout for a
42
+ * function depends on the type of function: Event handling functions have a
43
+ * maximum timeout of 540s (9 minutes). HTTPS and callable functions have a
44
+ * maximum timeout of 36,00s (1 hour). Task queue functions have a maximum
45
+ * timeout of 1,800s (30 minutes)
46
+ */
47
+ timeoutSeconds?: number | null;
48
+ /**
49
+ * Min number of actual instances to be running at a given time.
50
+ * Instances will be billed for memory allocation and 10% of CPU allocation
51
+ * while idle.
52
+ * A value of null restores the default min instances.
53
+ */
54
+ minInstances?: number | null;
55
+ /**
56
+ * Max number of instances to be running in parallel.
57
+ * A value of null restores the default max instances.
58
+ */
59
+ maxInstances?: number | null;
60
+ /**
61
+ * Number of requests a function can serve at once.
62
+ * Can only be applied to functions running on Cloud Functions v2.
63
+ * A value of null restores the default concurrency (80 when CPU >= 1, 1 otherwise).
64
+ * Concurrency cannot be set to any value other than 1 if `cpu` is less than 1.
65
+ * The maximum value for concurrency is 1,000.
66
+ */
67
+ concurrency?: number | null;
68
+ /**
69
+ * Fractional number of CPUs to allocate to a function.
70
+ * Defaults to 1 for functions with <= 2GB RAM and increases for larger memory sizes.
71
+ * This is different from the defaults when using the gcloud utility and is different from
72
+ * the fixed amount assigned in Google Cloud Functions generation 1.
73
+ * To revert to the CPU amounts used in gcloud or in Cloud Functions generation 1, set this
74
+ * to the value "gcf_gen1"
75
+ */
76
+ cpu?: number | 'gcf_gen1';
77
+ /**
78
+ * Connect cloud function to specified VPC connector.
79
+ * A value of null removes the VPC connector
80
+ */
81
+ vpcConnector?: string | null;
82
+ /**
83
+ * Egress settings for VPC connector.
84
+ * A value of null turns off VPC connector egress settings
85
+ */
86
+ vpcConnectorEgressSettings?: options.VpcEgressSetting | null;
87
+ /**
88
+ * Specific service account for the function to run as.
89
+ * A value of null restores the default service account.
90
+ */
91
+ serviceAccount?: string | null;
92
+ /**
93
+ * Ingress settings which control where this function can be called from.
94
+ * A value of null turns off ingress settings.
95
+ */
96
+ ingressSettings?: options.IngressSetting | null;
97
+ /**
98
+ * User labels to set on the function.
99
+ */
100
+ labels?: Record<string, string>;
101
+ secrets?: string[];
102
+ /** Whether failed executions should be delivered again. */
103
+ retry?: boolean;
104
+ }
105
+ /** Handles an Eventarc event published on the default channel.
106
+ * @param eventType - Type of the event to trigger on.
107
+ * @param handler - A function to execute when triggered.
108
+ * @returns A function that you can export and deploy.
109
+ */
110
+ export declare function onCustomEventPublished<T = any>(eventType: string, handler: (event: CloudEvent<T>) => any | Promise<any>): CloudFunction<CloudEvent<T>>;
111
+ /** Handles an Eventarc event.
112
+ * @param opts - Options to set on this function
113
+ * @param handler - A function to execute when triggered.
114
+ * @returns A function that you can export and deploy.
115
+ */
116
+ export declare function onCustomEventPublished<T = any>(opts: EventarcTriggerOptions, handler: (event: CloudEvent<T>) => any | Promise<any>): CloudFunction<CloudEvent<T>>;
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ // The MIT License (MIT)
3
+ //
4
+ // Copyright (c) 2022 Firebase
5
+ //
6
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ // of this software and associated documentation files (the "Software"), to deal
8
+ // in the Software without restriction, including without limitation the rights
9
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ // copies of the Software, and to permit persons to whom the Software is
11
+ // furnished to do so, subject to the following conditions:
12
+ //
13
+ // The above copyright notice and this permission notice shall be included in all
14
+ // copies or substantial portions of the Software.
15
+ //
16
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ // SOFTWARE.
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.onCustomEventPublished = void 0;
25
+ /**
26
+ * Cloud functions to integrate directly with Eventarc.
27
+ * @packageDocumentation
28
+ */
29
+ const encoding_1 = require("../../common/encoding");
30
+ const options = require("../options");
31
+ function onCustomEventPublished(eventTypeOrOpts, handler) {
32
+ var _a;
33
+ let opts;
34
+ if (typeof eventTypeOrOpts === 'string') {
35
+ opts = {
36
+ eventType: eventTypeOrOpts,
37
+ };
38
+ }
39
+ else if (typeof eventTypeOrOpts === 'object') {
40
+ opts = eventTypeOrOpts;
41
+ }
42
+ const func = (raw) => {
43
+ return handler(raw);
44
+ };
45
+ func.run = handler;
46
+ const channel = (_a = opts.channel) !== null && _a !== void 0 ? _a : 'locations/us-central1/channels/firebase';
47
+ const baseOpts = options.optionsToEndpoint(options.getGlobalOptions());
48
+ const specificOpts = options.optionsToEndpoint(opts);
49
+ const endpoint = {
50
+ platform: 'gcfv2',
51
+ ...baseOpts,
52
+ ...specificOpts,
53
+ labels: {
54
+ ...baseOpts === null || baseOpts === void 0 ? void 0 : baseOpts.labels,
55
+ ...specificOpts === null || specificOpts === void 0 ? void 0 : specificOpts.labels,
56
+ },
57
+ eventTrigger: {
58
+ eventType: opts.eventType,
59
+ eventFilters: {},
60
+ retry: false,
61
+ channel,
62
+ },
63
+ };
64
+ (0, encoding_1.convertIfPresent)(endpoint.eventTrigger, opts, 'eventFilters', 'filters');
65
+ (0, encoding_1.copyIfPresent)(endpoint.eventTrigger, opts, 'retry');
66
+ func.__endpoint = endpoint;
67
+ return func;
68
+ }
69
+ exports.onCustomEventPublished = onCustomEventPublished;
@@ -1,20 +1,144 @@
1
1
  import * as express from 'express';
2
- import * as options from '../options';
3
2
  import { CallableRequest, FunctionsErrorCode, HttpsError, Request } from '../../common/providers/https';
4
3
  import { ManifestEndpoint } from '../../runtime/manifest';
4
+ import * as options from '../options';
5
+ import { GlobalOptions, SupportedRegion } from '../options';
5
6
  export { Request, CallableRequest, FunctionsErrorCode, HttpsError };
6
- export interface HttpsOptions extends Omit<options.GlobalOptions, 'region'> {
7
- region?: options.SupportedRegion | string | Array<options.SupportedRegion | string>;
7
+ /**
8
+ * Options that can be set on an individual HTTPS function.
9
+ */
10
+ export interface HttpsOptions extends Omit<GlobalOptions, 'region'> {
11
+ /** HTTP functions can override global options and can specify multiple regions to deploy to. */
12
+ region?: SupportedRegion | string | Array<SupportedRegion | string>;
13
+ /** If true, allows CORS on requests to this function.
14
+ * If this is a `string` or `RegExp`, allows requests from domains that match the provided value.
15
+ * If this is an `Array`, allows requests from domains matching at least one entry of the array.
16
+ * Defaults to true for {@link https.CallableFunction} and false otherwise.
17
+ */
8
18
  cors?: string | boolean | RegExp | Array<string | RegExp>;
19
+ /**
20
+ * Amount of memory to allocate to a function.
21
+ * A value of null restores the defaults of 256MB.
22
+ */
23
+ memory?: options.MemoryOption | null;
24
+ /**
25
+ * Timeout for the function in sections, possible values are 0 to 540.
26
+ * HTTPS functions can specify a higher timeout.
27
+ * A value of null restores the default of 60s
28
+ * The minimum timeout for a gen 2 function is 1s. The maximum timeout for a
29
+ * function depends on the type of function: Event handling functions have a
30
+ * maximum timeout of 540s (9 minutes). HTTPS and callable functions have a
31
+ * maximum timeout of 36,00s (1 hour). Task queue functions have a maximum
32
+ * timeout of 1,800s (30 minutes)
33
+ */
34
+ timeoutSeconds?: number | null;
35
+ /**
36
+ * Min number of actual instances to be running at a given time.
37
+ * Instances will be billed for memory allocation and 10% of CPU allocation
38
+ * while idle.
39
+ * A value of null restores the default min instances.
40
+ */
41
+ minInstances?: number | null;
42
+ /**
43
+ * Max number of instances to be running in parallel.
44
+ * A value of null restores the default max instances.
45
+ */
46
+ maxInstances?: number | null;
47
+ /**
48
+ * Number of requests a function can serve at once.
49
+ * Can only be applied to functions running on Cloud Functions v2.
50
+ * A value of null restores the default concurrency (80 when CPU >= 1, 1 otherwise).
51
+ * Concurrency cannot be set to any value other than 1 if `cpu` is less than 1.
52
+ * The maximum value for concurrency is 1,000.
53
+ */
54
+ concurrency?: number | null;
55
+ /**
56
+ * Fractional number of CPUs to allocate to a function.
57
+ * Defaults to 1 for functions with <= 2GB RAM and increases for larger memory sizes.
58
+ * This is different from the defaults when using the gcloud utility and is different from
59
+ * the fixed amount assigned in Google Cloud Functions generation 1.
60
+ * To revert to the CPU amounts used in gcloud or in Cloud Functions generation 1, set this
61
+ * to the value "gcf_gen1"
62
+ */
63
+ cpu?: number | 'gcf_gen1';
64
+ /**
65
+ * Connect cloud function to specified VPC connector.
66
+ * A value of null removes the VPC connector
67
+ */
68
+ vpcConnector?: string | null;
69
+ /**
70
+ * Egress settings for VPC connector.
71
+ * A value of null turns off VPC connector egress settings
72
+ */
73
+ vpcConnectorEgressSettings?: options.VpcEgressSetting | null;
74
+ /**
75
+ * Specific service account for the function to run as.
76
+ * A value of null restores the default service account.
77
+ */
78
+ serviceAccount?: string | null;
79
+ /**
80
+ * Ingress settings which control where this function can be called from.
81
+ * A value of null turns off ingress settings.
82
+ */
83
+ ingressSettings?: options.IngressSetting | null;
84
+ /**
85
+ * User labels to set on the function.
86
+ */
87
+ labels?: Record<string, string>;
88
+ /**
89
+ * Invoker to set access control on https functions.
90
+ */
91
+ invoker?: 'public' | 'private' | string | string[];
92
+ secrets?: string[];
93
+ /** Whether failed executions should be delivered again. */
94
+ retry?: boolean;
9
95
  }
10
- export declare type HttpsFunction = ((req: Request, res: express.Response) => void | Promise<void>) & {
96
+ /**
97
+ * Handles HTTPS requests.
98
+ */
99
+ export declare type HttpsFunction = ((
100
+ /** An Express request object representing the HTTPS call to the function. */
101
+ req: Request,
102
+ /** An Express response object, for this function to respond to callers. */
103
+ res: express.Response) => void | Promise<void>) & {
104
+ /** @alpha */
11
105
  __trigger?: unknown;
106
+ /** @alpha */
12
107
  __endpoint: ManifestEndpoint;
13
108
  };
109
+ /**
110
+ * Creates a callable method for clients to call using a Firebase SDK.
111
+ */
14
112
  export interface CallableFunction<T, Return> extends HttpsFunction {
113
+ /** Executes the handler function with the provided data as input. Used for unit testing.
114
+ * @param data - An input for the handler function.
115
+ * @returns The output of the handler function.
116
+ */
15
117
  run(data: CallableRequest<T>): Return;
16
118
  }
119
+ /**
120
+ * Handles HTTPS requests.
121
+ * @param opts - Options to set on this function
122
+ * @param handler - A function that takes a {@link https.Request} and response object, same signature as an Express app.
123
+ * @returns A function that you can export and deploy.
124
+ */
17
125
  export declare function onRequest(opts: HttpsOptions, handler: (request: Request, response: express.Response) => void | Promise<void>): HttpsFunction;
126
+ /**
127
+ * Handles HTTPS requests.
128
+ * @param handler - A function that takes a {@link https.Request} and response object, same signature as an Express app.
129
+ * @returns A function that you can export and deploy.
130
+ */
18
131
  export declare function onRequest(handler: (request: Request, response: express.Response) => void | Promise<void>): HttpsFunction;
132
+ /**
133
+ * Declares a callable method for clients to call using a Firebase SDK.
134
+ * @param opts - Options to set on this function.
135
+ * @param handler - A function that takes a {@link https.CallableRequest}.
136
+ * @returns A function that you can export and deploy.
137
+ */
19
138
  export declare function onCall<T = any, Return = any | Promise<any>>(opts: HttpsOptions, handler: (request: CallableRequest<T>) => Return): CallableFunction<T, Return>;
139
+ /**
140
+ * Declares a callable method for clients to call using a Firebase SDK.
141
+ * @param handler - A function that takes a {@link https.CallableRequest}.
142
+ * @returns A function that you can export and deploy.
143
+ */
20
144
  export declare function onCall<T = any, Return = any | Promise<any>>(handler: (request: CallableRequest<T>) => Return): CallableFunction<T, Return>;
@@ -22,11 +22,16 @@
22
22
  // SOFTWARE.
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.onCall = exports.onRequest = exports.HttpsError = void 0;
25
+ /**
26
+ * Cloud functions to handle HTTPS request or callable RPCs.
27
+ * @packageDocumentation
28
+ */
25
29
  const cors = require("cors");
26
30
  const encoding_1 = require("../../common/encoding");
27
- const options = require("../options");
31
+ const debug_1 = require("../../common/debug");
28
32
  const https_1 = require("../../common/providers/https");
29
33
  Object.defineProperty(exports, "HttpsError", { enumerable: true, get: function () { return https_1.HttpsError; } });
34
+ const options = require("../options");
30
35
  function onRequest(optsOrHandler, handler) {
31
36
  let opts;
32
37
  if (arguments.length === 1) {
@@ -36,12 +41,13 @@ function onRequest(optsOrHandler, handler) {
36
41
  else {
37
42
  opts = optsOrHandler;
38
43
  }
39
- if ('cors' in opts) {
44
+ if ((0, debug_1.isDebugFeatureEnabled)('enableCors') || 'cors' in opts) {
45
+ const origin = (0, debug_1.isDebugFeatureEnabled)('enableCors') ? true : opts.cors;
40
46
  const userProvidedHandler = handler;
41
47
  handler = (req, res) => {
42
48
  return new Promise((resolve) => {
43
49
  res.on('finish', resolve);
44
- cors({ origin: opts.cors })(req, res, () => {
50
+ cors({ origin })(req, res, () => {
45
51
  resolve(userProvidedHandler(req, res));
46
52
  });
47
53
  });
@@ -54,9 +60,6 @@ function onRequest(optsOrHandler, handler) {
54
60
  // but optionsToTriggerAnnotations handles both cases.
55
61
  const specificOpts = options.optionsToTriggerAnnotations(opts);
56
62
  const trigger = {
57
- // TODO(inlined): Remove "apiVersion" once the latest version of the CLI
58
- // has migrated to "platform".
59
- apiVersion: 2,
60
63
  platform: 'gcfv2',
61
64
  ...baseOpts,
62
65
  ...specificOpts,
@@ -68,7 +71,7 @@ function onRequest(optsOrHandler, handler) {
68
71
  allowInsecure: false,
69
72
  },
70
73
  };
71
- encoding_1.convertIfPresent(trigger.httpsTrigger, opts, 'invoker', 'invoker', encoding_1.convertInvoker);
74
+ (0, encoding_1.convertIfPresent)(trigger.httpsTrigger, opts, 'invoker', 'invoker', encoding_1.convertInvoker);
72
75
  return trigger;
73
76
  },
74
77
  });
@@ -86,7 +89,7 @@ function onRequest(optsOrHandler, handler) {
86
89
  },
87
90
  httpsTrigger: {},
88
91
  };
89
- encoding_1.convertIfPresent(endpoint.httpsTrigger, opts, 'invoker', 'invoker', encoding_1.convertInvoker);
92
+ (0, encoding_1.convertIfPresent)(endpoint.httpsTrigger, opts, 'invoker', 'invoker', encoding_1.convertInvoker);
90
93
  handler.__endpoint = endpoint;
91
94
  return handler;
92
95
  }
@@ -100,11 +103,15 @@ function onCall(optsOrHandler, handler) {
100
103
  else {
101
104
  opts = optsOrHandler;
102
105
  }
103
- const origin = 'cors' in opts ? opts.cors : true;
106
+ const origin = (0, debug_1.isDebugFeatureEnabled)('enableCors')
107
+ ? true
108
+ : 'cors' in opts
109
+ ? opts.cors
110
+ : true;
104
111
  // onCallHandler sniffs the function length to determine which API to present.
105
112
  // fix the length to prevent api versions from being mismatched.
106
113
  const fixedLen = (req) => handler(req);
107
- const func = https_1.onCallHandler({ cors: { origin, methods: 'POST' } }, fixedLen);
114
+ const func = (0, https_1.onCallHandler)({ cors: { origin, methods: 'POST' } }, fixedLen);
108
115
  Object.defineProperty(func, '__trigger', {
109
116
  get: () => {
110
117
  const baseOpts = options.optionsToTriggerAnnotations(options.getGlobalOptions());
@@ -112,9 +119,6 @@ function onCall(optsOrHandler, handler) {
112
119
  // but optionsToTriggerAnnotations handles both cases.
113
120
  const specificOpts = options.optionsToTriggerAnnotations(opts);
114
121
  return {
115
- // TODO(inlined): Remove "apiVersion" once the latest version of the CLI
116
- // has migrated to "platform".
117
- apiVersion: 2,
118
122
  platform: 'gcfv2',
119
123
  ...baseOpts,
120
124
  ...specificOpts,
@@ -131,7 +135,7 @@ function onCall(optsOrHandler, handler) {
131
135
  });
132
136
  const baseOpts = options.optionsToEndpoint(options.getGlobalOptions());
133
137
  // global options calls region a scalar and https allows it to be an array,
134
- // but optionsToManifestEndpoint handles both cases.
138
+ // but optionsToEndpoint handles both cases.
135
139
  const specificOpts = options.optionsToEndpoint(opts);
136
140
  func.__endpoint = {
137
141
  platform: 'gcfv2',
@@ -0,0 +1,126 @@
1
+ /**
2
+ * Cloud functions to handle events from Google Cloud Identity Platform.
3
+ * @packageDocumentation
4
+ */
5
+ import { BlockingFunction } from '../../cloud-functions';
6
+ import { AuthBlockingEvent, AuthBlockingEventType, BeforeCreateResponse, BeforeSignInResponse, HttpsError } from '../../common/providers/identity';
7
+ import * as options from '../options';
8
+ export { HttpsError };
9
+ /** @hidden Internally used when parsing the options. */
10
+ interface InternalOptions {
11
+ opts: options.GlobalOptions;
12
+ idToken: boolean;
13
+ accessToken: boolean;
14
+ refreshToken: boolean;
15
+ }
16
+ /**
17
+ * All function options plus idToken, accessToken, and refreshToken.
18
+ */
19
+ export interface BlockingOptions {
20
+ /** Pass the ID Token credential to the function. */
21
+ idToken?: boolean;
22
+ /** Pass the Access Token credential to the function. */
23
+ accessToken?: boolean;
24
+ /** Pass the Refresh Token credential to the function. */
25
+ refreshToken?: boolean;
26
+ /**
27
+ * Region where functions should be deployed.
28
+ */
29
+ region?: options.SupportedRegion | string;
30
+ /**
31
+ * Amount of memory to allocate to a function.
32
+ * A value of null restores the defaults of 256MB.
33
+ */
34
+ memory?: options.MemoryOption | null;
35
+ /**
36
+ * Timeout for the function in sections, possible values are 0 to 540.
37
+ * HTTPS functions can specify a higher timeout.
38
+ * A value of null restores the default of 60s
39
+ * The minimum timeout for a gen 2 function is 1s. The maximum timeout for a
40
+ * function depends on the type of function: Event handling functions have a
41
+ * maximum timeout of 540s (9 minutes). HTTPS and callable functions have a
42
+ * maximum timeout of 36,00s (1 hour). Task queue functions have a maximum
43
+ * timeout of 1,800s (30 minutes)
44
+ */
45
+ timeoutSeconds?: number | null;
46
+ /**
47
+ * Min number of actual instances to be running at a given time.
48
+ * Instances will be billed for memory allocation and 10% of CPU allocation
49
+ * while idle.
50
+ * A value of null restores the default min instances.
51
+ */
52
+ minInstances?: number | null;
53
+ /**
54
+ * Max number of instances to be running in parallel.
55
+ * A value of null restores the default max instances.
56
+ */
57
+ maxInstances?: number | null;
58
+ /**
59
+ * Number of requests a function can serve at once.
60
+ * Can only be applied to functions running on Cloud Functions v2.
61
+ * A value of null restores the default concurrency (80 when CPU >= 1, 1 otherwise).
62
+ * Concurrency cannot be set to any value other than 1 if `cpu` is less than 1.
63
+ * The maximum value for concurrency is 1,000.
64
+ */
65
+ concurrency?: number | null;
66
+ /**
67
+ * Fractional number of CPUs to allocate to a function.
68
+ * Defaults to 1 for functions with <= 2GB RAM and increases for larger memory sizes.
69
+ * This is different from the defaults when using the gcloud utility and is different from
70
+ * the fixed amount assigned in Google Cloud Functions generation 1.
71
+ * To revert to the CPU amounts used in gcloud or in Cloud Functions generation 1, set this
72
+ * to the value "gcf_gen1"
73
+ */
74
+ cpu?: number | 'gcf_gen1';
75
+ /**
76
+ * Connect cloud function to specified VPC connector.
77
+ * A value of null removes the VPC connector
78
+ */
79
+ vpcConnector?: string | null;
80
+ /**
81
+ * Egress settings for VPC connector.
82
+ * A value of null turns off VPC connector egress settings
83
+ */
84
+ vpcConnectorEgressSettings?: options.VpcEgressSetting | null;
85
+ /**
86
+ * Specific service account for the function to run as.
87
+ * A value of null restores the default service account.
88
+ */
89
+ serviceAccount?: string | null;
90
+ /**
91
+ * Ingress settings which control where this function can be called from.
92
+ * A value of null turns off ingress settings.
93
+ */
94
+ ingressSettings?: options.IngressSetting | null;
95
+ /**
96
+ * User labels to set on the function.
97
+ */
98
+ labels?: Record<string, string>;
99
+ secrets?: string[];
100
+ }
101
+ /**
102
+ * Handles an event that is triggered before a user is created.
103
+ * @param handler - Event handler which is run every time before a user is created
104
+ */
105
+ export declare function beforeUserCreated(handler: (event: AuthBlockingEvent) => BeforeCreateResponse | Promise<BeforeCreateResponse> | void | Promise<void>): BlockingFunction;
106
+ /**
107
+ * Handles an event that is triggered before a user is created.
108
+ * @param opts - Object containing function options
109
+ * @param handler - Event handler which is run every time before a user is created
110
+ */
111
+ export declare function beforeUserCreated(opts: BlockingOptions, handler: (event: AuthBlockingEvent) => BeforeCreateResponse | Promise<BeforeCreateResponse> | void | Promise<void>): BlockingFunction;
112
+ /**
113
+ * Handles an event that is triggered before a user is signed in.
114
+ * @param handler - Event handler which is run every time before a user is signed in
115
+ */
116
+ export declare function beforeUserSignedIn(handler: (event: AuthBlockingEvent) => BeforeSignInResponse | Promise<BeforeSignInResponse> | void | Promise<void>): BlockingFunction;
117
+ /**
118
+ * Handles an event that is triggered before a user is signed in.
119
+ * @param opts - Object containing function options
120
+ * @param handler - Event handler which is run every time before a user is signed in
121
+ */
122
+ export declare function beforeUserSignedIn(opts: BlockingOptions, handler: (event: AuthBlockingEvent) => BeforeSignInResponse | Promise<BeforeSignInResponse> | void | Promise<void>): BlockingFunction;
123
+ /** @hidden */
124
+ export declare function beforeOperation(eventType: AuthBlockingEventType, optsOrHandler: BlockingOptions | ((event: AuthBlockingEvent) => BeforeCreateResponse | BeforeSignInResponse | void | Promise<BeforeCreateResponse> | Promise<BeforeSignInResponse> | Promise<void>), handler: (event: AuthBlockingEvent) => BeforeCreateResponse | BeforeSignInResponse | void | Promise<BeforeCreateResponse> | Promise<BeforeSignInResponse> | Promise<void>): BlockingFunction;
125
+ /** @hidden */
126
+ export declare function getOpts(blockingOptions: BlockingOptions): InternalOptions;
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ // The MIT License (MIT)
3
+ //
4
+ // Copyright (c) 2022 Firebase
5
+ //
6
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ // of this software and associated documentation files (the "Software"), to deal
8
+ // in the Software without restriction, including without limitation the rights
9
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ // copies of the Software, and to permit persons to whom the Software is
11
+ // furnished to do so, subject to the following conditions:
12
+ //
13
+ // The above copyright notice and this permission notice shall be included in all
14
+ // copies or substantial portions of the Software.
15
+ //
16
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ // SOFTWARE.
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.getOpts = exports.beforeOperation = exports.beforeUserSignedIn = exports.beforeUserCreated = exports.HttpsError = void 0;
25
+ const identity_1 = require("../../common/providers/identity");
26
+ Object.defineProperty(exports, "HttpsError", { enumerable: true, get: function () { return identity_1.HttpsError; } });
27
+ const options = require("../options");
28
+ /**
29
+ * Handles an event that is triggered before a user is created
30
+ * @param optsOrHandler - Either an object containing function options, or an event handler (run before user creation)
31
+ * @param handler? - If defined, an event handler which is run every time before a user is created
32
+ */
33
+ function beforeUserCreated(optsOrHandler, handler) {
34
+ return beforeOperation('beforeCreate', optsOrHandler, handler);
35
+ }
36
+ exports.beforeUserCreated = beforeUserCreated;
37
+ /**
38
+ * Handles an event that is triggered before a user is signed in.
39
+ * @param optsOrHandler - Either an object containing function options, or an event handler (run before user signin)
40
+ * @param handler - Event handler which is run every time before a user is signed in
41
+ */
42
+ function beforeUserSignedIn(optsOrHandler, handler) {
43
+ return beforeOperation('beforeSignIn', optsOrHandler, handler);
44
+ }
45
+ exports.beforeUserSignedIn = beforeUserSignedIn;
46
+ /** @hidden */
47
+ function beforeOperation(eventType, optsOrHandler, handler) {
48
+ if (!handler || typeof optsOrHandler === 'function') {
49
+ handler = optsOrHandler;
50
+ optsOrHandler = {};
51
+ }
52
+ const { opts, accessToken, idToken, refreshToken } = getOpts(optsOrHandler);
53
+ // Create our own function that just calls the provided function so we know for sure that
54
+ // handler takes one argument. This is something common/providers/identity depends on.
55
+ const wrappedHandler = (event) => handler(event);
56
+ const func = (0, identity_1.wrapHandler)(eventType, wrappedHandler);
57
+ const legacyEventType = `providers/cloud.auth/eventTypes/user.${eventType}`;
58
+ /** Endpoint */
59
+ const baseOptsEndpoint = options.optionsToEndpoint(options.getGlobalOptions());
60
+ const specificOptsEndpoint = options.optionsToEndpoint(opts);
61
+ func.__endpoint = {
62
+ platform: 'gcfv2',
63
+ ...baseOptsEndpoint,
64
+ ...specificOptsEndpoint,
65
+ labels: {
66
+ ...baseOptsEndpoint === null || baseOptsEndpoint === void 0 ? void 0 : baseOptsEndpoint.labels,
67
+ ...specificOptsEndpoint === null || specificOptsEndpoint === void 0 ? void 0 : specificOptsEndpoint.labels,
68
+ },
69
+ blockingTrigger: {
70
+ eventType: legacyEventType,
71
+ options: {
72
+ accessToken,
73
+ idToken,
74
+ refreshToken,
75
+ },
76
+ },
77
+ };
78
+ func.__requiredAPIs = [
79
+ {
80
+ api: 'identitytoolkit.googleapis.com',
81
+ reason: 'Needed for auth blocking functions',
82
+ },
83
+ ];
84
+ func.run = handler;
85
+ return func;
86
+ }
87
+ exports.beforeOperation = beforeOperation;
88
+ /** @hidden */
89
+ function getOpts(blockingOptions) {
90
+ const accessToken = blockingOptions.accessToken || false;
91
+ const idToken = blockingOptions.idToken || false;
92
+ const refreshToken = blockingOptions.refreshToken || false;
93
+ const opts = { ...blockingOptions };
94
+ delete opts.accessToken;
95
+ delete opts.idToken;
96
+ delete opts.refreshToken;
97
+ return {
98
+ opts,
99
+ accessToken,
100
+ idToken,
101
+ refreshToken,
102
+ };
103
+ }
104
+ exports.getOpts = getOpts;