firebase-functions 6.5.0 → 7.0.0-rc.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 (157) hide show
  1. package/lib/_virtual/rolldown_runtime.js +34 -0
  2. package/lib/bin/firebase-functions.js +78 -103
  3. package/lib/common/app.js +35 -55
  4. package/lib/common/change.js +54 -75
  5. package/lib/common/config.js +41 -41
  6. package/lib/common/debug.js +23 -47
  7. package/lib/common/encoding.js +59 -82
  8. package/lib/common/onInit.js +26 -28
  9. package/lib/common/options.js +22 -42
  10. package/lib/common/params.d.ts +6 -6
  11. package/lib/common/params.js +0 -23
  12. package/lib/common/providers/database.js +270 -300
  13. package/lib/common/providers/firestore.js +66 -92
  14. package/lib/common/providers/https.d.ts +0 -1
  15. package/lib/common/providers/https.js +537 -539
  16. package/lib/common/providers/identity.js +393 -444
  17. package/lib/common/providers/tasks.js +64 -98
  18. package/lib/common/timezone.js +544 -542
  19. package/lib/common/trace.d.ts +0 -1
  20. package/lib/common/trace.js +63 -55
  21. package/lib/common/utilities/assertions.d.ts +11 -0
  22. package/lib/common/utilities/assertions.js +18 -0
  23. package/lib/common/utilities/encoder.js +20 -37
  24. package/lib/common/utilities/path-pattern.js +106 -132
  25. package/lib/common/utilities/path.js +28 -27
  26. package/lib/common/utilities/utils.js +23 -45
  27. package/lib/esm/_virtual/rolldown_runtime.mjs +16 -0
  28. package/lib/esm/bin/firebase-functions.mjs +91 -0
  29. package/lib/esm/common/app.mjs +39 -0
  30. package/lib/esm/common/change.mjs +57 -0
  31. package/lib/esm/common/config.mjs +45 -0
  32. package/lib/esm/common/debug.mjs +28 -0
  33. package/lib/esm/common/encoding.mjs +69 -0
  34. package/lib/esm/common/onInit.mjs +33 -0
  35. package/lib/esm/common/options.mjs +22 -0
  36. package/lib/esm/common/params.mjs +1 -0
  37. package/lib/esm/common/providers/database.mjs +269 -0
  38. package/lib/esm/common/providers/firestore.mjs +78 -0
  39. package/lib/esm/common/providers/https.mjs +573 -0
  40. package/lib/esm/common/providers/identity.mjs +428 -0
  41. package/lib/esm/common/providers/tasks.mjs +67 -0
  42. package/lib/esm/common/timezone.mjs +544 -0
  43. package/lib/esm/common/trace.mjs +73 -0
  44. package/lib/esm/common/utilities/assertions.mjs +17 -0
  45. package/lib/esm/common/utilities/encoder.mjs +21 -0
  46. package/lib/esm/common/utilities/path-pattern.mjs +116 -0
  47. package/lib/esm/common/utilities/path.mjs +35 -0
  48. package/lib/esm/common/utilities/utils.mjs +29 -0
  49. package/lib/esm/function-configuration.mjs +1 -0
  50. package/lib/esm/logger/common.mjs +23 -0
  51. package/lib/esm/logger/compat.mjs +25 -0
  52. package/lib/esm/logger/index.mjs +131 -0
  53. package/lib/esm/params/index.mjs +160 -0
  54. package/lib/esm/params/types.mjs +400 -0
  55. package/lib/esm/runtime/loader.mjs +132 -0
  56. package/lib/esm/runtime/manifest.mjs +134 -0
  57. package/lib/esm/types/global.d.mjs +1 -0
  58. package/lib/esm/v1/cloud-functions.mjs +206 -0
  59. package/lib/esm/v1/config.mjs +14 -0
  60. package/lib/esm/v1/function-builder.mjs +252 -0
  61. package/lib/esm/v1/function-configuration.mjs +72 -0
  62. package/lib/esm/v1/index.mjs +27 -0
  63. package/lib/esm/v1/providers/analytics.mjs +212 -0
  64. package/lib/esm/v1/providers/auth.mjs +156 -0
  65. package/lib/esm/v1/providers/database.mjs +243 -0
  66. package/lib/esm/v1/providers/firestore.mjs +131 -0
  67. package/lib/esm/v1/providers/https.mjs +82 -0
  68. package/lib/esm/v1/providers/pubsub.mjs +175 -0
  69. package/lib/esm/v1/providers/remoteConfig.mjs +64 -0
  70. package/lib/esm/v1/providers/storage.mjs +163 -0
  71. package/lib/esm/v1/providers/tasks.mjs +63 -0
  72. package/lib/esm/v1/providers/testLab.mjs +94 -0
  73. package/lib/esm/v2/core.mjs +4 -0
  74. package/lib/esm/v2/index.mjs +28 -0
  75. package/lib/esm/v2/options.mjs +102 -0
  76. package/lib/esm/v2/providers/alerts/alerts.mjs +85 -0
  77. package/lib/esm/v2/providers/alerts/appDistribution.mjs +75 -0
  78. package/lib/esm/v2/providers/alerts/billing.mjs +51 -0
  79. package/lib/esm/v2/providers/alerts/crashlytics.mjs +122 -0
  80. package/lib/esm/v2/providers/alerts/index.mjs +22 -0
  81. package/lib/esm/v2/providers/alerts/performance.mjs +66 -0
  82. package/lib/esm/v2/providers/database.mjs +197 -0
  83. package/lib/esm/v2/providers/dataconnect.mjs +130 -0
  84. package/lib/esm/v2/providers/eventarc.mjs +51 -0
  85. package/lib/esm/v2/providers/firestore.mjs +294 -0
  86. package/lib/esm/v2/providers/https.mjs +210 -0
  87. package/lib/esm/v2/providers/identity.mjs +103 -0
  88. package/lib/esm/v2/providers/pubsub.mjs +148 -0
  89. package/lib/esm/v2/providers/remoteConfig.mjs +52 -0
  90. package/lib/esm/v2/providers/scheduler.mjs +84 -0
  91. package/lib/esm/v2/providers/storage.mjs +155 -0
  92. package/lib/esm/v2/providers/tasks.mjs +65 -0
  93. package/lib/esm/v2/providers/testLab.mjs +53 -0
  94. package/lib/esm/v2/trace.mjs +20 -0
  95. package/lib/function-configuration.d.ts +0 -0
  96. package/lib/function-configuration.js +0 -0
  97. package/lib/logger/common.js +21 -41
  98. package/lib/logger/compat.js +18 -33
  99. package/lib/logger/index.js +119 -130
  100. package/lib/params/index.d.ts +19 -4
  101. package/lib/params/index.js +153 -129
  102. package/lib/params/types.d.ts +17 -0
  103. package/lib/params/types.js +390 -382
  104. package/lib/runtime/loader.js +114 -148
  105. package/lib/runtime/manifest.js +106 -126
  106. package/lib/types/global.d.js +0 -0
  107. package/lib/v1/cloud-functions.d.ts +2 -2
  108. package/lib/v1/cloud-functions.js +193 -241
  109. package/lib/v1/config.d.ts +4 -7
  110. package/lib/v1/config.js +13 -75
  111. package/lib/v1/function-builder.js +239 -368
  112. package/lib/v1/function-configuration.js +70 -63
  113. package/lib/v1/index.js +118 -73
  114. package/lib/v1/providers/analytics.js +189 -210
  115. package/lib/v1/providers/auth.d.ts +2 -1
  116. package/lib/v1/providers/auth.js +159 -164
  117. package/lib/v1/providers/database.js +237 -242
  118. package/lib/v1/providers/firestore.js +131 -130
  119. package/lib/v1/providers/https.d.ts +2 -1
  120. package/lib/v1/providers/https.js +79 -86
  121. package/lib/v1/providers/pubsub.js +175 -172
  122. package/lib/v1/providers/remoteConfig.js +64 -68
  123. package/lib/v1/providers/storage.js +161 -163
  124. package/lib/v1/providers/tasks.d.ts +1 -1
  125. package/lib/v1/providers/tasks.js +65 -80
  126. package/lib/v1/providers/testLab.js +94 -94
  127. package/lib/v2/core.d.ts +1 -1
  128. package/lib/v2/core.js +5 -32
  129. package/lib/v2/index.d.ts +6 -3
  130. package/lib/v2/index.js +123 -75
  131. package/lib/v2/options.js +88 -114
  132. package/lib/v2/providers/alerts/alerts.js +76 -95
  133. package/lib/v2/providers/alerts/appDistribution.js +73 -78
  134. package/lib/v2/providers/alerts/billing.js +49 -53
  135. package/lib/v2/providers/alerts/crashlytics.js +110 -102
  136. package/lib/v2/providers/alerts/index.js +56 -53
  137. package/lib/v2/providers/alerts/performance.js +64 -74
  138. package/lib/v2/providers/database.js +177 -180
  139. package/lib/v2/providers/dataconnect.d.ts +95 -0
  140. package/lib/v2/providers/dataconnect.js +137 -0
  141. package/lib/v2/providers/eventarc.js +55 -77
  142. package/lib/v2/providers/firestore.js +262 -260
  143. package/lib/v2/providers/https.d.ts +3 -2
  144. package/lib/v2/providers/https.js +210 -247
  145. package/lib/v2/providers/identity.d.ts +2 -1
  146. package/lib/v2/providers/identity.js +96 -105
  147. package/lib/v2/providers/pubsub.js +149 -167
  148. package/lib/v2/providers/remoteConfig.js +54 -63
  149. package/lib/v2/providers/scheduler.js +84 -96
  150. package/lib/v2/providers/storage.js +147 -162
  151. package/lib/v2/providers/tasks.d.ts +1 -1
  152. package/lib/v2/providers/tasks.js +68 -95
  153. package/lib/v2/providers/testLab.js +55 -64
  154. package/lib/v2/trace.js +18 -19
  155. package/package.json +290 -226
  156. package/protos/compiledFirestore.mjs +3512 -0
  157. package/protos/update.sh +28 -7
@@ -0,0 +1,210 @@
1
+ import { __export } from "../../_virtual/rolldown_runtime.mjs";
2
+ import { debug } from "../../logger/index.mjs";
3
+ import { Expression } from "../../params/types.mjs";
4
+ import "../../params/index.mjs";
5
+ import { initV2Endpoint } from "../../runtime/manifest.mjs";
6
+ import { isDebugFeatureEnabled } from "../../common/debug.mjs";
7
+ import { convertIfPresent, convertInvoker, copyIfPresent } from "../../common/encoding.mjs";
8
+ import { withInit } from "../../common/onInit.mjs";
9
+ import { HttpsError, onCallHandler, withErrorHandler } from "../../common/providers/https.mjs";
10
+ import { wrapTraceContext } from "../trace.mjs";
11
+ import { getGlobalOptions, optionsToEndpoint, optionsToTriggerAnnotations } from "../options.mjs";
12
+ import cors from "cors";
13
+
14
+ //#region src/v2/providers/https.ts
15
+ var https_exports = /* @__PURE__ */ __export({
16
+ HttpsError: () => HttpsError,
17
+ hasClaim: () => hasClaim,
18
+ isSignedIn: () => isSignedIn,
19
+ onCall: () => onCall,
20
+ onCallGenkit: () => onCallGenkit,
21
+ onRequest: () => onRequest
22
+ });
23
+ /**
24
+ * @deprecated
25
+ *
26
+ * An auth policy that requires a user to be signed in.
27
+ */
28
+ const isSignedIn = () => (auth) => !!auth;
29
+ /**
30
+ * @deprecated
31
+ *
32
+ * An auth policy that requires a user to be both signed in and have a specific claim (optionally with a specific value)
33
+ */
34
+ const hasClaim = (claim, value) => (auth) => {
35
+ if (!auth) {
36
+ return false;
37
+ }
38
+ if (!(claim in auth.token)) {
39
+ return false;
40
+ }
41
+ return !value || auth.token[claim] === value;
42
+ };
43
+ function onRequest(optsOrHandler, handler) {
44
+ let opts;
45
+ if (arguments.length === 1) {
46
+ opts = {};
47
+ handler = optsOrHandler;
48
+ } else {
49
+ opts = optsOrHandler;
50
+ }
51
+ handler = withErrorHandler(handler);
52
+ if (isDebugFeatureEnabled("enableCors") || "cors" in opts) {
53
+ let origin = opts.cors instanceof Expression ? opts.cors.value() : opts.cors;
54
+ if (isDebugFeatureEnabled("enableCors")) {
55
+ origin = opts.cors === false ? false : true;
56
+ }
57
+ if (Array.isArray(origin) && origin.length === 1) {
58
+ origin = origin[0];
59
+ }
60
+ const middleware = cors({ origin });
61
+ const userProvidedHandler = handler;
62
+ handler = (req, res) => {
63
+ return new Promise((resolve) => {
64
+ res.on("finish", resolve);
65
+ middleware(req, res, () => {
66
+ resolve(userProvidedHandler(req, res));
67
+ });
68
+ });
69
+ };
70
+ }
71
+ handler = wrapTraceContext(withInit(handler));
72
+ Object.defineProperty(handler, "__trigger", { get: () => {
73
+ const baseOpts$1 = optionsToTriggerAnnotations(getGlobalOptions());
74
+ const specificOpts$1 = optionsToTriggerAnnotations(opts);
75
+ const trigger = {
76
+ platform: "gcfv2",
77
+ ...baseOpts$1,
78
+ ...specificOpts$1,
79
+ labels: {
80
+ ...baseOpts$1?.labels,
81
+ ...specificOpts$1?.labels
82
+ },
83
+ httpsTrigger: { allowInsecure: false }
84
+ };
85
+ convertIfPresent(trigger.httpsTrigger, getGlobalOptions(), "invoker", "invoker", convertInvoker);
86
+ convertIfPresent(trigger.httpsTrigger, opts, "invoker", "invoker", convertInvoker);
87
+ return trigger;
88
+ } });
89
+ const globalOpts = getGlobalOptions();
90
+ const baseOpts = optionsToEndpoint(globalOpts);
91
+ const specificOpts = optionsToEndpoint(opts);
92
+ const endpoint = {
93
+ ...initV2Endpoint(globalOpts, opts),
94
+ platform: "gcfv2",
95
+ ...baseOpts,
96
+ ...specificOpts,
97
+ labels: {
98
+ ...baseOpts?.labels,
99
+ ...specificOpts?.labels
100
+ },
101
+ httpsTrigger: {}
102
+ };
103
+ convertIfPresent(endpoint.httpsTrigger, globalOpts, "invoker", "invoker", convertInvoker);
104
+ convertIfPresent(endpoint.httpsTrigger, opts, "invoker", "invoker", convertInvoker);
105
+ handler.__endpoint = endpoint;
106
+ return handler;
107
+ }
108
+ function onCall(optsOrHandler, handler) {
109
+ let opts;
110
+ if (arguments.length === 1) {
111
+ opts = {};
112
+ handler = optsOrHandler;
113
+ } else {
114
+ opts = optsOrHandler;
115
+ }
116
+ let cors$1;
117
+ if ("cors" in opts) {
118
+ if (opts.cors instanceof Expression) {
119
+ cors$1 = opts.cors.value();
120
+ } else {
121
+ cors$1 = opts.cors;
122
+ }
123
+ } else {
124
+ cors$1 = true;
125
+ }
126
+ let origin = isDebugFeatureEnabled("enableCors") ? true : cors$1;
127
+ if (Array.isArray(origin) && origin.length === 1) {
128
+ origin = origin[0];
129
+ }
130
+ const fixedLen = (req, resp) => handler(req, resp);
131
+ let func = onCallHandler({
132
+ cors: {
133
+ origin,
134
+ methods: "POST"
135
+ },
136
+ enforceAppCheck: opts.enforceAppCheck ?? getGlobalOptions().enforceAppCheck,
137
+ consumeAppCheckToken: opts.consumeAppCheckToken,
138
+ heartbeatSeconds: opts.heartbeatSeconds,
139
+ authPolicy: opts.authPolicy
140
+ }, fixedLen, "gcfv2");
141
+ func = wrapTraceContext(withInit(func));
142
+ Object.defineProperty(func, "__trigger", { get: () => {
143
+ const baseOpts$1 = optionsToTriggerAnnotations(getGlobalOptions());
144
+ const specificOpts$1 = optionsToTriggerAnnotations(opts);
145
+ return {
146
+ platform: "gcfv2",
147
+ ...baseOpts$1,
148
+ ...specificOpts$1,
149
+ labels: {
150
+ ...baseOpts$1?.labels,
151
+ ...specificOpts$1?.labels,
152
+ "deployment-callable": "true"
153
+ },
154
+ httpsTrigger: { allowInsecure: false }
155
+ };
156
+ } });
157
+ const baseOpts = optionsToEndpoint(getGlobalOptions());
158
+ const specificOpts = optionsToEndpoint(opts);
159
+ func.__endpoint = {
160
+ ...initV2Endpoint(getGlobalOptions(), opts),
161
+ platform: "gcfv2",
162
+ ...baseOpts,
163
+ ...specificOpts,
164
+ labels: {
165
+ ...baseOpts?.labels,
166
+ ...specificOpts?.labels
167
+ },
168
+ callableTrigger: {}
169
+ };
170
+ func.run = withInit(handler);
171
+ func.stream = () => {
172
+ return {
173
+ stream: { next() {
174
+ return Promise.reject("Coming soon");
175
+ } },
176
+ output: Promise.reject("Coming soon")
177
+ };
178
+ };
179
+ return func;
180
+ }
181
+ function onCallGenkit(optsOrAction, action) {
182
+ let opts;
183
+ if (arguments.length === 2) {
184
+ opts = optsOrAction;
185
+ } else {
186
+ opts = {};
187
+ action = optsOrAction;
188
+ }
189
+ if (!opts.secrets?.length) {
190
+ debug(`Genkit function for ${action.__action.name} is not bound to any secret. This may mean that you are not storing API keys as a secret or that you are not binding your secret to this function. See https://firebase.google.com/docs/functions/config-env?gen=2nd#secret_parameters for more information.`);
191
+ }
192
+ const cloudFunction = onCall(opts, async (req, res) => {
193
+ const context = {};
194
+ copyIfPresent(context, req, "auth", "app", "instanceIdToken");
195
+ if (!req.acceptsStreaming) {
196
+ const { result } = await action.run(req.data, { context });
197
+ return result;
198
+ }
199
+ const { stream, output } = action.stream(req.data, { context });
200
+ for await (const chunk of stream) {
201
+ await res.sendChunk(chunk);
202
+ }
203
+ return output;
204
+ });
205
+ cloudFunction.__endpoint.callableTrigger.genkitAction = action.__action.name;
206
+ return cloudFunction;
207
+ }
208
+
209
+ //#endregion
210
+ export { HttpsError, hasClaim, https_exports, isSignedIn, onCall, onCallGenkit, onRequest };
@@ -0,0 +1,103 @@
1
+ import { __export } from "../../_virtual/rolldown_runtime.mjs";
2
+ import { initV2Endpoint } from "../../runtime/manifest.mjs";
3
+ import { withInit } from "../../common/onInit.mjs";
4
+ import { HttpsError } from "../../common/providers/https.mjs";
5
+ import { wrapHandler } from "../../common/providers/identity.mjs";
6
+ import { wrapTraceContext } from "../trace.mjs";
7
+ import { getGlobalOptions, optionsToEndpoint } from "../options.mjs";
8
+
9
+ //#region src/v2/providers/identity.ts
10
+ var identity_exports = /* @__PURE__ */ __export({
11
+ HttpsError: () => HttpsError,
12
+ beforeEmailSent: () => beforeEmailSent,
13
+ beforeOperation: () => beforeOperation,
14
+ beforeSmsSent: () => beforeSmsSent,
15
+ beforeUserCreated: () => beforeUserCreated,
16
+ beforeUserSignedIn: () => beforeUserSignedIn,
17
+ getOpts: () => getOpts
18
+ });
19
+ /**
20
+ * Handles an event that is triggered before a user is created.
21
+ * @param optsOrHandler - Either an object containing function options, or an event handler (run before user creation).
22
+ * @param handler? - If defined, an event handler which is run every time before a user is created.
23
+ */
24
+ function beforeUserCreated(optsOrHandler, handler) {
25
+ return beforeOperation("beforeCreate", optsOrHandler, handler);
26
+ }
27
+ /**
28
+ * Handles an event that is triggered before a user is signed in.
29
+ * @param optsOrHandler - Either an object containing function options, or an event handler (run before user signin).
30
+ * @param handler - Event handler which is run every time before a user is signed in.
31
+ */
32
+ function beforeUserSignedIn(optsOrHandler, handler) {
33
+ return beforeOperation("beforeSignIn", optsOrHandler, handler);
34
+ }
35
+ /**
36
+ * Handles an event that is triggered before an email is sent to a user.
37
+ * @param optsOrHandler- Either an object containing function options, or an event handler that is run before an email is sent to a user.
38
+ * @param handler - Event handler that is run before an email is sent to a user.
39
+ */
40
+ function beforeEmailSent(optsOrHandler, handler) {
41
+ return beforeOperation("beforeSendEmail", optsOrHandler, handler);
42
+ }
43
+ /**
44
+ * Handles an event that is triggered before an SMS is sent to a user.
45
+ * @param optsOrHandler - Either an object containing function options, or an event handler that is run before an SMS is sent to a user.
46
+ * @param handler - Event handler that is run before an SMS is sent to a user.
47
+ */
48
+ function beforeSmsSent(optsOrHandler, handler) {
49
+ return beforeOperation("beforeSendSms", optsOrHandler, handler);
50
+ }
51
+ /** @hidden */
52
+ function beforeOperation(eventType, optsOrHandler, handler) {
53
+ if (!handler || typeof optsOrHandler === "function") {
54
+ handler = optsOrHandler;
55
+ optsOrHandler = {};
56
+ }
57
+ const { opts,...blockingOptions } = getOpts(optsOrHandler);
58
+ const annotatedHandler = Object.assign(handler, { platform: "gcfv2" });
59
+ const func = wrapTraceContext(withInit(wrapHandler(eventType, annotatedHandler)));
60
+ const legacyEventType = `providers/cloud.auth/eventTypes/user.${eventType}`;
61
+ /** Endpoint */
62
+ const baseOptsEndpoint = optionsToEndpoint(getGlobalOptions());
63
+ const specificOptsEndpoint = optionsToEndpoint(opts);
64
+ func.__endpoint = {
65
+ ...initV2Endpoint(getGlobalOptions(), opts),
66
+ platform: "gcfv2",
67
+ ...baseOptsEndpoint,
68
+ ...specificOptsEndpoint,
69
+ labels: {
70
+ ...baseOptsEndpoint?.labels,
71
+ ...specificOptsEndpoint?.labels
72
+ },
73
+ blockingTrigger: {
74
+ eventType: legacyEventType,
75
+ options: { ...(eventType === "beforeCreate" || eventType === "beforeSignIn") && blockingOptions }
76
+ }
77
+ };
78
+ func.__requiredAPIs = [{
79
+ api: "identitytoolkit.googleapis.com",
80
+ reason: "Needed for auth blocking functions"
81
+ }];
82
+ func.run = handler;
83
+ return func;
84
+ }
85
+ /** @hidden */
86
+ function getOpts(blockingOptions) {
87
+ const accessToken = blockingOptions.accessToken || false;
88
+ const idToken = blockingOptions.idToken || false;
89
+ const refreshToken = blockingOptions.refreshToken || false;
90
+ const opts = { ...blockingOptions };
91
+ delete opts.accessToken;
92
+ delete opts.idToken;
93
+ delete opts.refreshToken;
94
+ return {
95
+ opts,
96
+ accessToken,
97
+ idToken,
98
+ refreshToken
99
+ };
100
+ }
101
+
102
+ //#endregion
103
+ export { HttpsError, beforeEmailSent, beforeOperation, beforeSmsSent, beforeUserCreated, beforeUserSignedIn, getOpts, identity_exports };
@@ -0,0 +1,148 @@
1
+ import { __export } from "../../_virtual/rolldown_runtime.mjs";
2
+ import { initV2Endpoint } from "../../runtime/manifest.mjs";
3
+ import { copyIfPresent } from "../../common/encoding.mjs";
4
+ import { withInit } from "../../common/onInit.mjs";
5
+ import { wrapTraceContext } from "../trace.mjs";
6
+ import { getGlobalOptions, optionsToEndpoint, optionsToTriggerAnnotations } from "../options.mjs";
7
+
8
+ //#region src/v2/providers/pubsub.ts
9
+ var pubsub_exports = /* @__PURE__ */ __export({
10
+ Message: () => Message,
11
+ onMessagePublished: () => onMessagePublished
12
+ });
13
+ /**
14
+ * Google Cloud Pub/Sub is a globally distributed message bus that automatically scales as you need it.
15
+ * You can create a function ({@link onMessagePublished}) that handles pub/sub events by using functions.pubsub.
16
+ *
17
+ * This function triggers whenever a new pub/sub message is sent to a specific topic.
18
+ * You must specify the Pub/Sub topic name that you want to trigger your function, and set the event within the
19
+ * onPublish() event handler.
20
+ *
21
+ * PubSub Topic:
22
+ * <ul>
23
+ * <li>A resource that you can publish messages to and then consume those messages via subscriptions.
24
+ * <li>An isolated data stream for pub/sub messages.
25
+ * <li>Messages are published to a topic.
26
+ * <li>Messages are listened to via a subscription.
27
+ * <li>Each subscription listens to the messages published to exactly one topic.
28
+ *
29
+ * Subscriptions - Resource that listens to the messages published by exactly one topic.
30
+ *
31
+ * [More info here](https://firebase.google.com/docs/functions/pubsub-events)
32
+ */
33
+ /**
34
+ * Interface representing a Google Cloud Pub/Sub message.
35
+ *
36
+ * @param data - Payload of a Pub/Sub message.
37
+ * @typeParam T - Type representing `Message.data`'s JSON format
38
+ */
39
+ var Message = class {
40
+ /**
41
+ * @hidden
42
+ * @alpha
43
+ */
44
+ constructor(data) {
45
+ this.messageId = data.messageId;
46
+ this.data = data.data;
47
+ this.attributes = data.attributes || {};
48
+ this.orderingKey = data.orderingKey || "";
49
+ this.publishTime = data.publishTime || new Date().toISOString();
50
+ this._json = data.json;
51
+ }
52
+ /**
53
+ * The JSON data payload of this message object, if any.
54
+ */
55
+ get json() {
56
+ if (typeof this._json === "undefined") {
57
+ try {
58
+ this._json = JSON.parse(Buffer.from(this.data, "base64").toString("utf8"));
59
+ } catch (err) {
60
+ throw new Error(`Unable to parse Pub/Sub message data as JSON: ${err.message}`);
61
+ }
62
+ }
63
+ return this._json;
64
+ }
65
+ /**
66
+ * Returns a JSON-serializable representation of this object.
67
+ *
68
+ * @returns A JSON-serializable representation of this object.
69
+ */
70
+ toJSON() {
71
+ const json = {
72
+ messageId: this.messageId,
73
+ data: this.data,
74
+ publishTime: this.publishTime
75
+ };
76
+ if (Object.keys(this.attributes).length) {
77
+ json.attributes = this.attributes;
78
+ }
79
+ if (this.orderingKey) {
80
+ json.orderingKey = this.orderingKey;
81
+ }
82
+ return json;
83
+ }
84
+ };
85
+ /**
86
+ * Handle a message being published to a Pub/Sub topic.
87
+ * @param topicOrOptions - A string representing the PubSub topic or an option (which contains the topic)
88
+ * @param handler - runs every time a Cloud Pub/Sub message is published
89
+ * @typeParam T - Type representing `Message.data`'s JSON format
90
+ */
91
+ function onMessagePublished(topicOrOptions, handler) {
92
+ let topic;
93
+ let opts;
94
+ if (typeof topicOrOptions === "string") {
95
+ topic = topicOrOptions;
96
+ opts = {};
97
+ } else {
98
+ topic = topicOrOptions.topic;
99
+ opts = { ...topicOrOptions };
100
+ delete opts.topic;
101
+ }
102
+ const func = (raw) => {
103
+ const messagePublishedData = raw.data;
104
+ messagePublishedData.message = new Message(messagePublishedData.message);
105
+ return wrapTraceContext(withInit(handler))(raw);
106
+ };
107
+ func.run = handler;
108
+ Object.defineProperty(func, "__trigger", { get: () => {
109
+ const baseOpts$1 = optionsToTriggerAnnotations(getGlobalOptions());
110
+ const specificOpts$1 = optionsToTriggerAnnotations(opts);
111
+ return {
112
+ platform: "gcfv2",
113
+ ...baseOpts$1,
114
+ ...specificOpts$1,
115
+ labels: {
116
+ ...baseOpts$1?.labels,
117
+ ...specificOpts$1?.labels
118
+ },
119
+ eventTrigger: {
120
+ eventType: "google.cloud.pubsub.topic.v1.messagePublished",
121
+ resource: `projects/${process.env.GCLOUD_PROJECT}/topics/${topic}`
122
+ }
123
+ };
124
+ } });
125
+ const baseOpts = optionsToEndpoint(getGlobalOptions());
126
+ const specificOpts = optionsToEndpoint(opts);
127
+ const endpoint = {
128
+ ...initV2Endpoint(getGlobalOptions(), opts),
129
+ platform: "gcfv2",
130
+ ...baseOpts,
131
+ ...specificOpts,
132
+ labels: {
133
+ ...baseOpts?.labels,
134
+ ...specificOpts?.labels
135
+ },
136
+ eventTrigger: {
137
+ eventType: "google.cloud.pubsub.topic.v1.messagePublished",
138
+ eventFilters: { topic },
139
+ retry: opts.retry ?? false
140
+ }
141
+ };
142
+ copyIfPresent(endpoint.eventTrigger, opts, "retry", "retry");
143
+ func.__endpoint = endpoint;
144
+ return func;
145
+ }
146
+
147
+ //#endregion
148
+ export { Message, onMessagePublished, pubsub_exports };
@@ -0,0 +1,52 @@
1
+ import { __export } from "../../_virtual/rolldown_runtime.mjs";
2
+ import { initV2Endpoint } from "../../runtime/manifest.mjs";
3
+ import { withInit } from "../../common/onInit.mjs";
4
+ import { wrapTraceContext } from "../trace.mjs";
5
+ import { getGlobalOptions, optionsToEndpoint } from "../options.mjs";
6
+
7
+ //#region src/v2/providers/remoteConfig.ts
8
+ var remoteConfig_exports = /* @__PURE__ */ __export({
9
+ eventType: () => eventType,
10
+ onConfigUpdated: () => onConfigUpdated
11
+ });
12
+ /** @internal */
13
+ const eventType = "google.firebase.remoteconfig.remoteConfig.v1.updated";
14
+ /**
15
+ * Event handler which triggers when data is updated in a Remote Config.
16
+ *
17
+ * @param optsOrHandler - Options or an event handler.
18
+ * @param handler - Event handler which is run every time a Remote Config update occurs.
19
+ * @returns A function that you can export and deploy.
20
+ */
21
+ function onConfigUpdated(optsOrHandler, handler) {
22
+ if (typeof optsOrHandler === "function") {
23
+ handler = optsOrHandler;
24
+ optsOrHandler = {};
25
+ }
26
+ const baseOpts = optionsToEndpoint(getGlobalOptions());
27
+ const specificOpts = optionsToEndpoint(optsOrHandler);
28
+ const func = wrapTraceContext(withInit((raw) => {
29
+ return handler(raw);
30
+ }));
31
+ func.run = handler;
32
+ const ep = {
33
+ ...initV2Endpoint(getGlobalOptions(), optsOrHandler),
34
+ platform: "gcfv2",
35
+ ...baseOpts,
36
+ ...specificOpts,
37
+ labels: {
38
+ ...baseOpts?.labels,
39
+ ...specificOpts?.labels
40
+ },
41
+ eventTrigger: {
42
+ eventType,
43
+ eventFilters: {},
44
+ retry: optsOrHandler.retry ?? false
45
+ }
46
+ };
47
+ func.__endpoint = ep;
48
+ return func;
49
+ }
50
+
51
+ //#endregion
52
+ export { eventType, onConfigUpdated, remoteConfig_exports };
@@ -0,0 +1,84 @@
1
+ import { __export } from "../../_virtual/rolldown_runtime.mjs";
2
+ import { error } from "../../logger/index.mjs";
3
+ import { initV2Endpoint, initV2ScheduleTrigger } from "../../runtime/manifest.mjs";
4
+ import { copyIfPresent } from "../../common/encoding.mjs";
5
+ import { withInit } from "../../common/onInit.mjs";
6
+ import { wrapTraceContext } from "../trace.mjs";
7
+ import { getGlobalOptions, optionsToEndpoint } from "../options.mjs";
8
+
9
+ //#region src/v2/providers/scheduler.ts
10
+ var scheduler_exports = /* @__PURE__ */ __export({
11
+ getOpts: () => getOpts,
12
+ onSchedule: () => onSchedule
13
+ });
14
+ /** @internal */
15
+ function getOpts(args) {
16
+ if (typeof args === "string") {
17
+ return {
18
+ schedule: args,
19
+ opts: {}
20
+ };
21
+ }
22
+ return {
23
+ schedule: args.schedule,
24
+ timeZone: args.timeZone,
25
+ retryConfig: {
26
+ retryCount: args.retryCount,
27
+ maxRetrySeconds: args.maxRetrySeconds,
28
+ minBackoffSeconds: args.minBackoffSeconds,
29
+ maxBackoffSeconds: args.maxBackoffSeconds,
30
+ maxDoublings: args.maxDoublings
31
+ },
32
+ opts: args
33
+ };
34
+ }
35
+ /**
36
+ * Handler for scheduled functions. Triggered whenever the associated
37
+ * scheduler job sends a http request.
38
+ * @param args - Either a schedule or an object containing function options.
39
+ * @param handler - A function to execute when triggered.
40
+ * @returns A function that you can export and deploy.
41
+ */
42
+ function onSchedule(args, handler) {
43
+ const separatedOpts = getOpts(args);
44
+ const httpFunc = async (req, res) => {
45
+ const event = {
46
+ jobName: req.header("X-CloudScheduler-JobName") || undefined,
47
+ scheduleTime: req.header("X-CloudScheduler-ScheduleTime") || new Date().toISOString()
48
+ };
49
+ try {
50
+ await handler(event);
51
+ res.status(200).send();
52
+ } catch (err) {
53
+ error(err.message);
54
+ res.status(500).send();
55
+ }
56
+ };
57
+ const func = wrapTraceContext(withInit(httpFunc));
58
+ func.run = handler;
59
+ const globalOpts = getGlobalOptions();
60
+ const baseOptsEndpoint = optionsToEndpoint(globalOpts);
61
+ const specificOptsEndpoint = optionsToEndpoint(separatedOpts.opts);
62
+ const ep = {
63
+ ...initV2Endpoint(globalOpts, separatedOpts.opts),
64
+ platform: "gcfv2",
65
+ ...baseOptsEndpoint,
66
+ ...specificOptsEndpoint,
67
+ labels: {
68
+ ...baseOptsEndpoint?.labels,
69
+ ...specificOptsEndpoint?.labels
70
+ },
71
+ scheduleTrigger: initV2ScheduleTrigger(separatedOpts.schedule, globalOpts, separatedOpts.opts)
72
+ };
73
+ copyIfPresent(ep.scheduleTrigger, separatedOpts, "timeZone");
74
+ copyIfPresent(ep.scheduleTrigger.retryConfig, separatedOpts.retryConfig, "retryCount", "maxRetrySeconds", "minBackoffSeconds", "maxBackoffSeconds", "maxDoublings");
75
+ func.__endpoint = ep;
76
+ func.__requiredAPIs = [{
77
+ api: "cloudscheduler.googleapis.com",
78
+ reason: "Needed for scheduled functions."
79
+ }];
80
+ return func;
81
+ }
82
+
83
+ //#endregion
84
+ export { getOpts, onSchedule, scheduler_exports };