firebase-functions 4.6.0 → 4.8.0

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 (36) hide show
  1. package/lib/common/onInit.d.ts +7 -0
  2. package/lib/common/onInit.js +36 -0
  3. package/lib/common/providers/database.d.ts +9 -1
  4. package/lib/common/providers/database.js +3 -0
  5. package/lib/params/index.d.ts +25 -24
  6. package/lib/params/index.js +34 -30
  7. package/lib/params/types.d.ts +60 -49
  8. package/lib/params/types.js +56 -15
  9. package/lib/v1/cloud-functions.js +2 -0
  10. package/lib/v1/index.d.ts +1 -0
  11. package/lib/v1/index.js +3 -1
  12. package/lib/v1/providers/https.js +9 -5
  13. package/lib/v2/core.d.ts +1 -0
  14. package/lib/v2/core.js +3 -1
  15. package/lib/v2/index.d.ts +1 -1
  16. package/lib/v2/index.js +3 -1
  17. package/lib/v2/providers/alerts/alerts.js +2 -1
  18. package/lib/v2/providers/alerts/appDistribution.js +3 -2
  19. package/lib/v2/providers/alerts/billing.js +2 -1
  20. package/lib/v2/providers/alerts/crashlytics.js +2 -1
  21. package/lib/v2/providers/alerts/performance.d.ts +0 -4
  22. package/lib/v2/providers/alerts/performance.js +7 -1
  23. package/lib/v2/providers/database.js +5 -2
  24. package/lib/v2/providers/eventarc.js +2 -1
  25. package/lib/v2/providers/firestore.js +3 -2
  26. package/lib/v2/providers/https.d.ts +2 -2
  27. package/lib/v2/providers/https.js +4 -3
  28. package/lib/v2/providers/identity.js +2 -1
  29. package/lib/v2/providers/pubsub.js +2 -1
  30. package/lib/v2/providers/remoteConfig.js +4 -2
  31. package/lib/v2/providers/scheduler.js +2 -1
  32. package/lib/v2/providers/storage.d.ts +5 -5
  33. package/lib/v2/providers/storage.js +5 -3
  34. package/lib/v2/providers/tasks.js +2 -1
  35. package/lib/v2/providers/testLab.js +2 -1
  36. package/package.json +4 -1
@@ -21,7 +21,7 @@
21
21
  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
22
  // SOFTWARE.
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
- exports.ListParam = exports.BooleanParam = exports.FloatParam = exports.IntParam = exports.InternalExpression = exports.StringParam = exports.SecretParam = exports.Param = exports.CompareExpression = exports.TernaryExpression = exports.Expression = void 0;
24
+ exports.ListParam = exports.BooleanParam = exports.FloatParam = exports.IntParam = exports.InternalExpression = exports.StringParam = exports.SecretParam = exports.Param = exports.BUCKET_PICKER = exports.multiSelect = exports.select = exports.CompareExpression = exports.TernaryExpression = exports.Expression = void 0;
25
25
  const logger = require("../logger");
26
26
  /*
27
27
  * A CEL expression which can be evaluated during function deployment, and
@@ -29,7 +29,7 @@ const logger = require("../logger");
29
29
  * an Expression<number> as the value of an option that normally accepts numbers.
30
30
  */
31
31
  class Expression {
32
- /** Returns the Expression's runtime value, based on the CLI's resolution of params. */
32
+ /** Returns the expression's runtime value, based on the CLI's resolution of parameters. */
33
33
  value() {
34
34
  if (process.env.FUNCTIONS_CONTROL_API === "true") {
35
35
  logger.warn(`${this.toString()}.value() invoked during function deployment, instead of during runtime.`);
@@ -42,10 +42,11 @@ class Expression {
42
42
  runtimeValue() {
43
43
  throw new Error("Not implemented");
44
44
  }
45
- /** Returns the Expression's representation as a braced CEL expression. */
45
+ /** Returns the expression's representation as a braced CEL expression. */
46
46
  toCEL() {
47
47
  return `{{ ${this.toString()} }}`;
48
48
  }
49
+ /** Returns the expression's representation as JSON. */
49
50
  toJSON() {
50
51
  return this.toString();
51
52
  }
@@ -55,8 +56,8 @@ function valueOf(arg) {
55
56
  return arg instanceof Expression ? arg.runtimeValue() : arg;
56
57
  }
57
58
  /**
58
- * Returns how an entity (either an Expression or a literal value) should be represented in CEL.
59
- * - Expressions delegate to the .toString() method, which is used by the WireManifest
59
+ * Returns how an entity (either an `Expression` or a literal value) should be represented in CEL.
60
+ * - Expressions delegate to the `.toString()` method, which is used by the WireManifest
60
61
  * - Strings have to be quoted explicitly
61
62
  * - Arrays are represented as []-delimited, parsable JSON
62
63
  * - Numbers and booleans are not quoted explicitly
@@ -136,12 +137,52 @@ class CompareExpression extends Expression {
136
137
  const rhsStr = refOf(this.rhs);
137
138
  return `${this.lhs} ${this.cmp} ${rhsStr}`;
138
139
  }
139
- /** Returns a TernaryExpression which can resolve to one of two values, based on the resolution of this comparison. */
140
+ /** Returns a `TernaryExpression` which can resolve to one of two values, based on the resolution of this comparison. */
140
141
  thenElse(ifTrue, ifFalse) {
141
142
  return new TernaryExpression(this, ifTrue, ifFalse);
142
143
  }
143
144
  }
144
145
  exports.CompareExpression = CompareExpression;
146
+ /** Create a select input from a series of values or a map of labels to values */
147
+ function select(options) {
148
+ let wireOpts;
149
+ if (Array.isArray(options)) {
150
+ wireOpts = options.map((opt) => ({ value: opt }));
151
+ }
152
+ else {
153
+ wireOpts = Object.entries(options).map(([label, value]) => ({ label, value }));
154
+ }
155
+ return {
156
+ select: {
157
+ options: wireOpts,
158
+ },
159
+ };
160
+ }
161
+ exports.select = select;
162
+ /** Create a multi-select input from a series of values or map of labels to values. */
163
+ function multiSelect(options) {
164
+ let wireOpts;
165
+ if (Array.isArray(options)) {
166
+ wireOpts = options.map((opt) => ({ value: opt }));
167
+ }
168
+ else {
169
+ wireOpts = Object.entries(options).map(([label, value]) => ({ label, value }));
170
+ }
171
+ return {
172
+ multiSelect: {
173
+ options: wireOpts,
174
+ },
175
+ };
176
+ }
177
+ exports.multiSelect = multiSelect;
178
+ /**
179
+ * Autogenerate a list of buckets in a project that a user can select from.
180
+ */
181
+ exports.BUCKET_PICKER = {
182
+ resource: {
183
+ type: "storage.googleapis.com/Bucket",
184
+ },
185
+ };
145
186
  /**
146
187
  * Represents a parametrized value that will be read from .env files if present,
147
188
  * or prompted for by the CLI if missing. Instantiate these with the defineX
@@ -157,36 +198,36 @@ class Param extends Expression {
157
198
  runtimeValue() {
158
199
  throw new Error("Not implemented");
159
200
  }
160
- /** Returns a parametrized expression of Boolean type, based on comparing the value of this param to a literal or a different expression. */
201
+ /** Returns a parametrized expression of Boolean type, based on comparing the value of this parameter to a literal or a different expression. */
161
202
  cmp(cmp, rhs) {
162
203
  return new CompareExpression(cmp, this, rhs);
163
204
  }
164
- /** Returns a parametrized expression of Boolean type, based on comparing the value of this param to a literal or a different expression. */
205
+ /** Returns a parametrized expression of Boolean type, based on comparing the value of this parameter to a literal or a different expression. */
165
206
  equals(rhs) {
166
207
  return this.cmp("==", rhs);
167
208
  }
168
- /** Returns a parametrized expression of Boolean type, based on comparing the value of this param to a literal or a different expression. */
209
+ /** Returns a parametrized expression of Boolean type, based on comparing the value of this parameter to a literal or a different expression. */
169
210
  notEquals(rhs) {
170
211
  return this.cmp("!=", rhs);
171
212
  }
172
- /** Returns a parametrized expression of Boolean type, based on comparing the value of this param to a literal or a different expression. */
213
+ /** Returns a parametrized expression of Boolean type, based on comparing the value of this parameter to a literal or a different expression. */
173
214
  greaterThan(rhs) {
174
215
  return this.cmp(">", rhs);
175
216
  }
176
- /** Returns a parametrized expression of Boolean type, based on comparing the value of this param to a literal or a different expression. */
217
+ /** Returns a parametrized expression of Boolean type, based on comparing the value of this parameter to a literal or a different expression. */
177
218
  greaterThanOrEqualTo(rhs) {
178
219
  return this.cmp(">=", rhs);
179
220
  }
180
- /** Returns a parametrized expression of Boolean type, based on comparing the value of this param to a literal or a different expression. */
221
+ /** Returns a parametrized expression of Boolean type, based on comparing the value of this parameter to a literal or a different expression. */
181
222
  lessThan(rhs) {
182
223
  return this.cmp("<", rhs);
183
224
  }
184
- /** Returns a parametrized expression of Boolean type, based on comparing the value of this param to a literal or a different expression. */
225
+ /** Returns a parametrized expression of Boolean type, based on comparing the value of this parameter to a literal or a different expression. */
185
226
  lessThanOrEqualTo(rhs) {
186
227
  return this.cmp("<=", rhs);
187
228
  }
188
229
  /**
189
- * Returns a parametrized expression of Boolean type, based on comparing the value of this param to a literal or a different expression.
230
+ * Returns a parametrized expression of Boolean type, based on comparing the value of this parameter to a literal or a different expression.
190
231
  * @deprecated A typo. Use lessThanOrEqualTo instead.
191
232
  */
192
233
  lessThanorEqualTo(rhs) {
@@ -266,7 +307,7 @@ exports.StringParam = StringParam;
266
307
  /**
267
308
  * A CEL expression which represents an internal Firebase variable. This class
268
309
  * cannot be instantiated by developers, but we provide several canned instances
269
- * of it to make available params that will never have to be defined at
310
+ * of it to make available parameters that will never have to be defined at
270
311
  * deployment time, and can always be read from process.env.
271
312
  * @internal
272
313
  */
@@ -28,6 +28,7 @@ const encoding_1 = require("../common/encoding");
28
28
  const manifest_1 = require("../runtime/manifest");
29
29
  const options_1 = require("../common/options");
30
30
  const types_1 = require("../params/types");
31
+ const onInit_1 = require("../common/onInit");
31
32
  var change_1 = require("../common/change");
32
33
  Object.defineProperty(exports, "Change", { enumerable: true, get: function () { return change_1.Change; } });
33
34
  /** @internal */
@@ -69,6 +70,7 @@ function makeCloudFunction({ contextOnlyHandler, dataConstructor = (raw) => raw.
69
70
  else {
70
71
  context.params = context.params || _makeParams(context, triggerResource);
71
72
  }
73
+ handler = (0, onInit_1.withInit)(handler);
72
74
  let promise;
73
75
  if (labels && labels["deployment-scheduled"]) {
74
76
  // Scheduled function do not have meaningful data, so exclude it
package/lib/v1/index.d.ts CHANGED
@@ -20,3 +20,4 @@ export * from "./function-builder";
20
20
  export * from "./function-configuration";
21
21
  import * as params from "../params";
22
22
  export { params };
23
+ export { onInit } from "../common/onInit";
package/lib/v1/index.js CHANGED
@@ -35,7 +35,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
35
35
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
36
36
  };
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.params = exports.app = exports.logger = exports.testLab = exports.tasks = exports.storage = exports.remoteConfig = exports.pubsub = exports.https = exports.firestore = exports.database = exports.auth = exports.analytics = void 0;
38
+ exports.onInit = exports.params = exports.app = exports.logger = exports.testLab = exports.tasks = exports.storage = exports.remoteConfig = exports.pubsub = exports.https = exports.firestore = exports.database = exports.auth = exports.analytics = void 0;
39
39
  // Providers:
40
40
  const logger = require("../logger");
41
41
  exports.logger = logger;
@@ -69,3 +69,5 @@ __exportStar(require("./function-configuration"), exports);
69
69
  // NOTE: Equivalent to `export * as params from "../params"` but api-extractor doesn't support that syntax.
70
70
  const params = require("../params");
71
71
  exports.params = params;
72
+ var onInit_1 = require("../common/onInit");
73
+ Object.defineProperty(exports, "onInit", { enumerable: true, get: function () { return onInit_1.onInit; } });
@@ -27,6 +27,8 @@ const https_1 = require("../../common/providers/https");
27
27
  Object.defineProperty(exports, "HttpsError", { enumerable: true, get: function () { return https_1.HttpsError; } });
28
28
  const cloud_functions_1 = require("../cloud-functions");
29
29
  const manifest_1 = require("../../runtime/manifest");
30
+ const onInit_1 = require("../../common/onInit");
31
+ const trace_1 = require("../../v2/trace");
30
32
  /**
31
33
  * Handle HTTP requests.
32
34
  * @param handler A function that takes a request and response object,
@@ -48,7 +50,7 @@ exports.onCall = onCall;
48
50
  function _onRequestWithOptions(handler, options) {
49
51
  // lets us add __endpoint without altering handler:
50
52
  const cloudFunction = (req, res) => {
51
- return handler(req, res);
53
+ return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))(req, res);
52
54
  };
53
55
  cloudFunction.__trigger = {
54
56
  ...(0, cloud_functions_1.optionsToTrigger)(options),
@@ -71,12 +73,14 @@ function _onCallWithOptions(handler, options) {
71
73
  // onCallHandler sniffs the function length of the passed-in callback
72
74
  // and the user could have only tried to listen to data. Wrap their handler
73
75
  // in another handler to avoid accidentally triggering the v2 API
74
- const fixedLen = (data, context) => handler(data, context);
75
- const func = (0, https_1.onCallHandler)({
76
+ const fixedLen = (data, context) => {
77
+ return (0, onInit_1.withInit)(handler)(data, context);
78
+ };
79
+ const func = (0, trace_1.wrapTraceContext)((0, https_1.onCallHandler)({
76
80
  enforceAppCheck: options.enforceAppCheck,
77
81
  consumeAppCheckToken: options.consumeAppCheckToken,
78
82
  cors: { origin: true, methods: "POST" },
79
- }, fixedLen);
83
+ }, fixedLen));
80
84
  func.__trigger = {
81
85
  labels: {},
82
86
  ...(0, cloud_functions_1.optionsToTrigger)(options),
@@ -90,7 +94,7 @@ function _onCallWithOptions(handler, options) {
90
94
  ...(0, cloud_functions_1.optionsToEndpoint)(options),
91
95
  callableTrigger: {},
92
96
  };
93
- func.run = handler;
97
+ func.run = fixedLen;
94
98
  return func;
95
99
  }
96
100
  exports._onCallWithOptions = _onCallWithOptions;
package/lib/v2/core.d.ts CHANGED
@@ -6,6 +6,7 @@ import { Change } from "../common/change";
6
6
  import { ManifestEndpoint } from "../runtime/manifest";
7
7
  export { Change };
8
8
  export { ParamsOf } from "../common/params";
9
+ export { onInit } from "../common/onInit";
9
10
  /**
10
11
  * A `CloudEventBase` is the base of a cross-platform format for encoding a serverless event.
11
12
  * For more information, see https://github.com/cloudevents/spec.
package/lib/v2/core.js CHANGED
@@ -21,10 +21,12 @@
21
21
  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
22
  // SOFTWARE.
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
- exports.Change = void 0;
24
+ exports.onInit = exports.Change = void 0;
25
25
  /**
26
26
  * Core functionality of the Cloud Functions for Firebase 2nd gen SDK.
27
27
  * @packageDocumentation
28
28
  */
29
29
  const change_1 = require("../common/change");
30
30
  Object.defineProperty(exports, "Change", { enumerable: true, get: function () { return change_1.Change; } });
31
+ var onInit_1 = require("../common/onInit");
32
+ Object.defineProperty(exports, "onInit", { enumerable: true, get: function () { return onInit_1.onInit; } });
package/lib/v2/index.d.ts CHANGED
@@ -20,7 +20,7 @@ import * as testLab from "./providers/testLab";
20
20
  import * as firestore from "./providers/firestore";
21
21
  export { alerts, database, storage, https, identity, pubsub, logger, tasks, eventarc, scheduler, remoteConfig, testLab, firestore, };
22
22
  export { setGlobalOptions, GlobalOptions, SupportedRegion, MemoryOption, VpcEgressSetting, IngressSetting, EventHandlerOptions, } from "./options";
23
- export { CloudFunction, CloudEvent, ParamsOf } from "./core";
23
+ export { CloudFunction, CloudEvent, ParamsOf, onInit } from "./core";
24
24
  export { Change } from "../common/change";
25
25
  import * as params from "../params";
26
26
  export { params };
package/lib/v2/index.js CHANGED
@@ -21,7 +21,7 @@
21
21
  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
22
  // SOFTWARE.
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
- exports.params = exports.Change = exports.setGlobalOptions = exports.firestore = exports.testLab = exports.remoteConfig = exports.scheduler = exports.eventarc = exports.tasks = exports.logger = exports.pubsub = exports.identity = exports.https = exports.storage = exports.database = exports.alerts = void 0;
24
+ exports.params = exports.Change = exports.onInit = exports.setGlobalOptions = exports.firestore = exports.testLab = exports.remoteConfig = exports.scheduler = exports.eventarc = exports.tasks = exports.logger = exports.pubsub = exports.identity = exports.https = exports.storage = exports.database = exports.alerts = void 0;
25
25
  /**
26
26
  * The 2nd gen API for Cloud Functions for Firebase.
27
27
  * This SDK supports deep imports. For example, the namespace
@@ -57,6 +57,8 @@ const firestore = require("./providers/firestore");
57
57
  exports.firestore = firestore;
58
58
  var options_1 = require("./options");
59
59
  Object.defineProperty(exports, "setGlobalOptions", { enumerable: true, get: function () { return options_1.setGlobalOptions; } });
60
+ var core_1 = require("./core");
61
+ Object.defineProperty(exports, "onInit", { enumerable: true, get: function () { return core_1.onInit; } });
60
62
  var change_1 = require("../common/change");
61
63
  Object.defineProperty(exports, "Change", { enumerable: true, get: function () { return change_1.Change; } });
62
64
  // NOTE: Equivalent to `export * as params from "../params"` but api-extractor doesn't support that syntax.
@@ -25,12 +25,13 @@ exports.convertAlertAndApp = exports.getOptsAndAlertTypeAndApp = exports.getEndp
25
25
  const manifest_1 = require("../../../runtime/manifest");
26
26
  const trace_1 = require("../../trace");
27
27
  const options = require("../../options");
28
+ const onInit_1 = require("../../../common/onInit");
28
29
  /** @internal */
29
30
  exports.eventType = "google.firebase.firebasealerts.alerts.v1.published";
30
31
  function onAlertPublished(alertTypeOrOpts, handler) {
31
32
  const [opts, alertType, appId] = getOptsAndAlertTypeAndApp(alertTypeOrOpts);
32
33
  const func = (raw) => {
33
- return (0, trace_1.wrapTraceContext)(handler)(convertAlertAndApp(raw));
34
+ return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))(convertAlertAndApp(raw));
34
35
  };
35
36
  func.run = handler;
36
37
  func.__endpoint = getEndpointAnnotation(opts, alertType, appId);
@@ -24,6 +24,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.getOptsAndApp = exports.onInAppFeedbackPublished = exports.onNewTesterIosDevicePublished = exports.inAppFeedbackAlert = exports.newTesterIosDeviceAlert = void 0;
25
25
  const trace_1 = require("../../trace");
26
26
  const alerts_1 = require("./alerts");
27
+ const onInit_1 = require("../../../common/onInit");
27
28
  /** @internal */
28
29
  exports.newTesterIosDeviceAlert = "appDistribution.newTesterIosDevice";
29
30
  /** @internal */
@@ -41,7 +42,7 @@ function onNewTesterIosDevicePublished(appIdOrOptsOrHandler, handler) {
41
42
  }
42
43
  const [opts, appId] = getOptsAndApp(appIdOrOptsOrHandler);
43
44
  const func = (raw) => {
44
- return (0, trace_1.wrapTraceContext)(handler)((0, alerts_1.convertAlertAndApp)(raw));
45
+ return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))((0, alerts_1.convertAlertAndApp)(raw));
45
46
  };
46
47
  func.run = handler;
47
48
  func.__endpoint = (0, alerts_1.getEndpointAnnotation)(opts, exports.newTesterIosDeviceAlert, appId);
@@ -61,7 +62,7 @@ function onInAppFeedbackPublished(appIdOrOptsOrHandler, handler) {
61
62
  }
62
63
  const [opts, appId] = getOptsAndApp(appIdOrOptsOrHandler);
63
64
  const func = (raw) => {
64
- return (0, trace_1.wrapTraceContext)(handler)((0, alerts_1.convertAlertAndApp)(raw));
65
+ return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))((0, alerts_1.convertAlertAndApp)(raw));
65
66
  };
66
67
  func.run = handler;
67
68
  func.__endpoint = (0, alerts_1.getEndpointAnnotation)(opts, exports.inAppFeedbackAlert, appId);
@@ -24,6 +24,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.onOperation = exports.onPlanAutomatedUpdatePublished = exports.onPlanUpdatePublished = exports.planAutomatedUpdateAlert = exports.planUpdateAlert = void 0;
25
25
  const trace_1 = require("../../trace");
26
26
  const alerts_1 = require("./alerts");
27
+ const onInit_1 = require("../../../common/onInit");
27
28
  /** @internal */
28
29
  exports.planUpdateAlert = "billing.planUpdate";
29
30
  /** @internal */
@@ -55,7 +56,7 @@ function onOperation(alertType, optsOrHandler, handler) {
55
56
  optsOrHandler = {};
56
57
  }
57
58
  const func = (raw) => {
58
- return (0, trace_1.wrapTraceContext)(handler)((0, alerts_1.convertAlertAndApp)(raw));
59
+ return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))((0, alerts_1.convertAlertAndApp)(raw));
59
60
  };
60
61
  func.run = handler;
61
62
  func.__endpoint = (0, alerts_1.getEndpointAnnotation)(optsOrHandler, alertType);
@@ -24,6 +24,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.getOptsAndApp = exports.onOperation = exports.onNewAnrIssuePublished = exports.onVelocityAlertPublished = exports.onStabilityDigestPublished = exports.onRegressionAlertPublished = exports.onNewNonfatalIssuePublished = exports.onNewFatalIssuePublished = exports.newAnrIssueAlert = exports.velocityAlert = exports.stabilityDigestAlert = exports.regressionAlert = exports.newNonfatalIssueAlert = exports.newFatalIssueAlert = void 0;
25
25
  const trace_1 = require("../../trace");
26
26
  const alerts_1 = require("./alerts");
27
+ const onInit_1 = require("../../../common/onInit");
27
28
  /** @internal */
28
29
  exports.newFatalIssueAlert = "crashlytics.newFatalIssue";
29
30
  /** @internal */
@@ -104,7 +105,7 @@ function onOperation(alertType, appIdOrOptsOrHandler, handler) {
104
105
  }
105
106
  const [opts, appId] = getOptsAndApp(appIdOrOptsOrHandler);
106
107
  const func = (raw) => {
107
- return (0, trace_1.wrapTraceContext)(handler((0, alerts_1.convertAlertAndApp)(raw)));
108
+ return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))((0, alerts_1.convertAlertAndApp)(raw));
108
109
  };
109
110
  func.run = handler;
110
111
  func.__endpoint = (0, alerts_1.getEndpointAnnotation)(opts, alertType, appId);
@@ -1,7 +1,3 @@
1
- /**
2
- * Cloud functions to handle Firebase Performance Monitoring events from Firebase Alerts.
3
- * @packageDocumentation
4
- */
5
1
  import { CloudEvent, CloudFunction } from "../../core";
6
2
  import { EventHandlerOptions } from "../../options";
7
3
  import { FirebaseAlertData } from "./alerts";
@@ -22,6 +22,12 @@
22
22
  // SOFTWARE.
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.convertPayload = exports.getOptsAndApp = exports.onThresholdAlertPublished = exports.thresholdAlert = void 0;
25
+ /**
26
+ * Cloud functions to handle Firebase Performance Monitoring events from Firebase Alerts.
27
+ * @packageDocumentation
28
+ */
29
+ const onInit_1 = require("../../../common/onInit");
30
+ const trace_1 = require("../../trace");
25
31
  const alerts_1 = require("./alerts");
26
32
  /** @internal */
27
33
  exports.thresholdAlert = "performance.threshold";
@@ -41,7 +47,7 @@ function onThresholdAlertPublished(appIdOrOptsOrHandler, handler) {
41
47
  const event = (0, alerts_1.convertAlertAndApp)(raw);
42
48
  const convertedPayload = convertPayload(event.data.payload);
43
49
  event.data.payload = convertedPayload;
44
- return handler(event);
50
+ return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler(event)));
45
51
  };
46
52
  func.run = handler;
47
53
  func.__endpoint = (0, alerts_1.getEndpointAnnotation)(opts, exports.thresholdAlert, appId);
@@ -31,6 +31,7 @@ const utils_1 = require("../../common/utilities/utils");
31
31
  const manifest_1 = require("../../runtime/manifest");
32
32
  const trace_1 = require("../trace");
33
33
  const options = require("../options");
34
+ const onInit_1 = require("../../common/onInit");
34
35
  /** @internal */
35
36
  exports.writtenEventType = "google.firebase.database.ref.v1.written";
36
37
  /** @internal */
@@ -181,7 +182,9 @@ function onChangedOperation(eventType, referenceOrOpts, handler) {
181
182
  const instanceUrl = getInstance(event);
182
183
  const params = makeParams(event, pathPattern, instancePattern);
183
184
  const databaseEvent = makeChangedDatabaseEvent(event, instanceUrl, params);
184
- return (0, trace_1.wrapTraceContext)(handler)(databaseEvent);
185
+ // Intentionally put init in the context of traces in case there is something
186
+ // expensive to observe.
187
+ return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))(databaseEvent);
185
188
  };
186
189
  func.run = handler;
187
190
  func.__endpoint = makeEndpoint(eventType, opts, pathPattern, instancePattern);
@@ -200,7 +203,7 @@ function onOperation(eventType, referenceOrOpts, handler) {
200
203
  const params = makeParams(event, pathPattern, instancePattern);
201
204
  const data = eventType === exports.deletedEventType ? event.data.data : event.data.delta;
202
205
  const databaseEvent = makeDatabaseEvent(event, data, instanceUrl, params);
203
- return handler(databaseEvent);
206
+ return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))(databaseEvent);
204
207
  };
205
208
  func.run = handler;
206
209
  func.__endpoint = makeEndpoint(eventType, opts, pathPattern, instancePattern);
@@ -30,6 +30,7 @@ const encoding_1 = require("../../common/encoding");
30
30
  const manifest_1 = require("../../runtime/manifest");
31
31
  const trace_1 = require("../trace");
32
32
  const options = require("../options");
33
+ const onInit_1 = require("../../common/onInit");
33
34
  function onCustomEventPublished(eventTypeOrOpts, handler) {
34
35
  var _a;
35
36
  let opts;
@@ -42,7 +43,7 @@ function onCustomEventPublished(eventTypeOrOpts, handler) {
42
43
  opts = eventTypeOrOpts;
43
44
  }
44
45
  const func = (raw) => {
45
- return (0, trace_1.wrapTraceContext)(handler)(raw);
46
+ return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))(raw);
46
47
  };
47
48
  func.run = handler;
48
49
  const channel = (_a = opts.channel) !== null && _a !== void 0 ? _a : "locations/us-central1/channels/firebase";
@@ -31,6 +31,7 @@ Object.defineProperty(exports, "Change", { enumerable: true, get: function () {
31
31
  const options_1 = require("../options");
32
32
  const firestore_1 = require("../../common/providers/firestore");
33
33
  const trace_1 = require("../trace");
34
+ const onInit_1 = require("../../common/onInit");
34
35
  /** @internal */
35
36
  exports.writtenEventType = "google.cloud.firestore.document.v1.written";
36
37
  /** @internal */
@@ -220,7 +221,7 @@ function onOperation(eventType, documentOrOpts, handler) {
220
221
  const event = raw;
221
222
  const params = makeParams(event.document, documentPattern);
222
223
  const firestoreEvent = makeFirestoreEvent(eventType, event, params);
223
- return (0, trace_1.wrapTraceContext)(handler)(firestoreEvent);
224
+ return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))(firestoreEvent);
224
225
  };
225
226
  func.run = handler;
226
227
  func.__endpoint = makeEndpoint(eventType, opts, documentPattern, database, namespace);
@@ -236,7 +237,7 @@ function onChangedOperation(eventType, documentOrOpts, handler) {
236
237
  const event = raw;
237
238
  const params = makeParams(event.document, documentPattern);
238
239
  const firestoreEvent = makeChangedFirestoreEvent(event, params);
239
- return handler(firestoreEvent);
240
+ return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))(firestoreEvent);
240
241
  };
241
242
  func.run = handler;
242
243
  func.__endpoint = makeEndpoint(eventType, opts, documentPattern, database, namespace);
@@ -175,10 +175,10 @@ export declare function onRequest(handler: (request: Request, response: express.
175
175
  * @param handler - A function that takes a {@link https.CallableRequest}.
176
176
  * @returns A function that you can export and deploy.
177
177
  */
178
- export declare function onCall<T = any, Return = any | Promise<any>>(opts: CallableOptions, handler: (request: CallableRequest<T>) => Return): CallableFunction<T, Return>;
178
+ export declare function onCall<T = any, Return = any | Promise<any>>(opts: CallableOptions, handler: (request: CallableRequest<T>) => Return): CallableFunction<T, Return extends Promise<unknown> ? Return : Promise<Return>>;
179
179
  /**
180
180
  * Declares a callable method for clients to call using a Firebase SDK.
181
181
  * @param handler - A function that takes a {@link https.CallableRequest}.
182
182
  * @returns A function that you can export and deploy.
183
183
  */
184
- export declare function onCall<T = any, Return = any | Promise<any>>(handler: (request: CallableRequest<T>) => Return): CallableFunction<T, Return>;
184
+ export declare function onCall<T = any, Return = any | Promise<any>>(handler: (request: CallableRequest<T>) => Return): CallableFunction<T, Return extends Promise<unknown> ? Return : Promise<Return>>;
@@ -34,6 +34,7 @@ const https_1 = require("../../common/providers/https");
34
34
  Object.defineProperty(exports, "HttpsError", { enumerable: true, get: function () { return https_1.HttpsError; } });
35
35
  const manifest_1 = require("../../runtime/manifest");
36
36
  const options = require("../options");
37
+ const onInit_1 = require("../../common/onInit");
37
38
  function onRequest(optsOrHandler, handler) {
38
39
  let opts;
39
40
  if (arguments.length === 1) {
@@ -59,7 +60,7 @@ function onRequest(optsOrHandler, handler) {
59
60
  });
60
61
  };
61
62
  }
62
- handler = (0, trace_1.wrapTraceContext)(handler);
63
+ handler = (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler));
63
64
  Object.defineProperty(handler, "__trigger", {
64
65
  get: () => {
65
66
  const baseOpts = options.optionsToTriggerAnnotations(options.getGlobalOptions());
@@ -118,7 +119,7 @@ function onCall(optsOrHandler, handler) {
118
119
  const origin = (0, debug_1.isDebugFeatureEnabled)("enableCors") ? true : "cors" in opts ? opts.cors : true;
119
120
  // onCallHandler sniffs the function length to determine which API to present.
120
121
  // fix the length to prevent api versions from being mismatched.
121
- const fixedLen = (req) => handler(req);
122
+ const fixedLen = (req) => (0, onInit_1.withInit)(handler)(req);
122
123
  let func = (0, https_1.onCallHandler)({
123
124
  cors: { origin, methods: "POST" },
124
125
  enforceAppCheck: (_a = opts.enforceAppCheck) !== null && _a !== void 0 ? _a : options.getGlobalOptions().enforceAppCheck,
@@ -161,7 +162,7 @@ function onCall(optsOrHandler, handler) {
161
162
  },
162
163
  callableTrigger: {},
163
164
  };
164
- func.run = handler;
165
+ func.run = (0, onInit_1.withInit)(handler);
165
166
  return func;
166
167
  }
167
168
  exports.onCall = onCall;
@@ -27,6 +27,7 @@ Object.defineProperty(exports, "HttpsError", { enumerable: true, get: function (
27
27
  const trace_1 = require("../trace");
28
28
  const manifest_1 = require("../../runtime/manifest");
29
29
  const options = require("../options");
30
+ const onInit_1 = require("../../common/onInit");
30
31
  /**
31
32
  * Handles an event that is triggered before a user is created
32
33
  * @param optsOrHandler - Either an object containing function options, or an event handler (run before user creation)
@@ -55,7 +56,7 @@ function beforeOperation(eventType, optsOrHandler, handler) {
55
56
  // Create our own function that just calls the provided function so we know for sure that
56
57
  // handler takes one argument. This is something common/providers/identity depends on.
57
58
  const wrappedHandler = (event) => handler(event);
58
- const func = (0, trace_1.wrapTraceContext)((0, identity_1.wrapHandler)(eventType, wrappedHandler));
59
+ const func = (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)((0, identity_1.wrapHandler)(eventType, wrappedHandler)));
59
60
  const legacyEventType = `providers/cloud.auth/eventTypes/user.${eventType}`;
60
61
  /** Endpoint */
61
62
  const baseOptsEndpoint = options.optionsToEndpoint(options.getGlobalOptions());
@@ -30,6 +30,7 @@ const encoding_1 = require("../../common/encoding");
30
30
  const manifest_1 = require("../../runtime/manifest");
31
31
  const trace_1 = require("../trace");
32
32
  const options = require("../options");
33
+ const onInit_1 = require("../../common/onInit");
33
34
  /**
34
35
  * Google Cloud Pub/Sub is a globally distributed message bus that automatically scales as you need it.
35
36
  * You can create a function ({@link onMessagePublished}) that handles pub/sub events by using functions.pubsub.
@@ -125,7 +126,7 @@ function onMessagePublished(topicOrOptions, handler) {
125
126
  const func = (raw) => {
126
127
  const messagePublishedData = raw.data;
127
128
  messagePublishedData.message = new Message(messagePublishedData.message);
128
- return (0, trace_1.wrapTraceContext)(handler)(raw);
129
+ return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))(raw);
129
130
  };
130
131
  func.run = handler;
131
132
  Object.defineProperty(func, "__trigger", {
@@ -22,8 +22,10 @@
22
22
  // SOFTWARE.
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.onConfigUpdated = exports.eventType = void 0;
25
+ const onInit_1 = require("../../common/onInit");
25
26
  const manifest_1 = require("../../runtime/manifest");
26
27
  const options_1 = require("../options");
28
+ const trace_1 = require("../trace");
27
29
  /** @internal */
28
30
  exports.eventType = "google.firebase.remoteconfig.remoteConfig.v1.updated";
29
31
  /**
@@ -40,9 +42,9 @@ function onConfigUpdated(optsOrHandler, handler) {
40
42
  }
41
43
  const baseOpts = (0, options_1.optionsToEndpoint)((0, options_1.getGlobalOptions)());
42
44
  const specificOpts = (0, options_1.optionsToEndpoint)(optsOrHandler);
43
- const func = (raw) => {
45
+ const func = (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)((raw) => {
44
46
  return handler(raw);
45
- };
47
+ }));
46
48
  func.run = handler;
47
49
  const ep = {
48
50
  ...(0, manifest_1.initV2Endpoint)((0, options_1.getGlobalOptions)(), optsOrHandler),
@@ -27,6 +27,7 @@ const manifest_1 = require("../../runtime/manifest");
27
27
  const trace_1 = require("../trace");
28
28
  const logger = require("../../logger");
29
29
  const options = require("../options");
30
+ const onInit_1 = require("../../common/onInit");
30
31
  /** @internal */
31
32
  function getOpts(args) {
32
33
  if (typeof args === "string") {
@@ -72,7 +73,7 @@ function onSchedule(args, handler) {
72
73
  res.status(500).send();
73
74
  }
74
75
  };
75
- const func = (0, trace_1.wrapTraceContext)(httpFunc);
76
+ const func = (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(httpFunc));
76
77
  func.run = handler;
77
78
  const globalOpts = options.getGlobalOptions();
78
79
  const baseOptsEndpoint = options.optionsToEndpoint(globalOpts);