firebase-functions 4.2.0 → 4.3.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 (47) hide show
  1. package/lib/common/change.js +4 -4
  2. package/lib/common/encoding.d.ts +3 -2
  3. package/lib/common/encoding.js +4 -0
  4. package/lib/common/params.d.ts +4 -4
  5. package/lib/common/providers/database.js +1 -6
  6. package/lib/common/providers/firestore.d.ts +1 -0
  7. package/lib/common/providers/firestore.js +106 -0
  8. package/lib/common/providers/https.d.ts +3 -3
  9. package/lib/common/providers/https.js +28 -1
  10. package/lib/common/providers/identity.d.ts +3 -3
  11. package/lib/common/providers/tasks.d.ts +1 -1
  12. package/lib/common/timezone.d.ts +1 -1
  13. package/lib/common/utilities/path-pattern.js +5 -5
  14. package/lib/logger/index.d.ts +1 -1
  15. package/lib/params/index.d.ts +1 -1
  16. package/lib/params/types.d.ts +5 -5
  17. package/lib/v1/function-builder.d.ts +4 -2
  18. package/lib/v1/function-builder.js +7 -5
  19. package/lib/v1/function-configuration.d.ts +5 -5
  20. package/lib/v1/providers/auth.js +3 -3
  21. package/lib/v1/providers/firestore.d.ts +2 -2
  22. package/lib/v1/providers/firestore.js +4 -39
  23. package/lib/v1/providers/testLab.d.ts +4 -4
  24. package/lib/v2/index.d.ts +2 -1
  25. package/lib/v2/index.js +3 -1
  26. package/lib/v2/options.d.ts +8 -8
  27. package/lib/v2/providers/alerts/alerts.d.ts +3 -3
  28. package/lib/v2/providers/alerts/appDistribution.d.ts +2 -2
  29. package/lib/v2/providers/alerts/crashlytics.d.ts +2 -2
  30. package/lib/v2/providers/database.d.ts +3 -3
  31. package/lib/v2/providers/database.js +8 -2
  32. package/lib/v2/providers/eventarc.d.ts +2 -2
  33. package/lib/v2/providers/firestore.d.ts +91 -0
  34. package/lib/v2/providers/firestore.js +243 -0
  35. package/lib/v2/providers/https.d.ts +3 -3
  36. package/lib/v2/providers/identity.d.ts +2 -2
  37. package/lib/v2/providers/pubsub.d.ts +2 -2
  38. package/lib/v2/providers/remoteConfig.d.ts +2 -2
  39. package/lib/v2/providers/storage.d.ts +2 -2
  40. package/lib/v2/providers/tasks.d.ts +3 -3
  41. package/lib/v2/providers/testLab.d.ts +2 -2
  42. package/lib/v2/trace.d.ts +1 -1
  43. package/package.json +13 -6
  44. package/protos/README.md +15 -0
  45. package/protos/compiledFirestore.d.ts +1342 -0
  46. package/protos/compiledFirestore.js +3514 -0
  47. package/protos/update.sh +75 -0
@@ -121,7 +121,7 @@ export declare class ResultStorage {
121
121
  * - `USE_DESTINATION_ARTIFACTS`: One or more of the test targets defined in the
122
122
  * .xctestrun file specifies "UseDestinationArtifacts", which is disallowed.
123
123
  *
124
- * - `TEST_NON_APP_HOSTED`: XC tests which run on physical devices must have
124
+ * - `TEST_NOT_APP_HOSTED`: XC tests which run on physical devices must have
125
125
  * "IsAppHostedTestBundle" == "true" in the xctestrun file.
126
126
  *
127
127
  * - `PLIST_CANNOT_BE_PARSED`: An Info.plist file in the XCTest zip could not be
@@ -136,7 +136,7 @@ export declare class ResultStorage {
136
136
  * - `INVALID_APK_PREVIEW_SDK`: APK is built for a preview SDK which is
137
137
  * unsupported.
138
138
  */
139
- export declare type InvalidMatrixDetails = "DETAILS_UNAVAILABLE" | "MALFORMED_APK" | "MALFORMED_TEST_APK" | "NO_MANIFEST" | "NO_PACKAGE_NAME" | "INVALID_PACKAGE_NAME" | "TEST_SAME_AS_APP" | "NO_INSTRUMENTATION" | "NO_SIGNATURE" | "INSTRUMENTATION_ORCHESTRATOR_INCOMPATIBLE" | "NO_TEST_RUNNER_CLASS" | "NO_LAUNCHER_ACTIVITY" | "FORBIDDEN_PERMISSIONS" | "INVALID_ROBO_DIRECTIVES" | "INVALID_RESOURCE_NAME" | "INVALID_DIRECTIVE_ACTION" | "TEST_LOOP_INTENT_FILTER_NOT_FOUND" | "SCENARIO_LABEL_NOT_DECLARED" | "SCENARIO_LABEL_MALFORMED" | "SCENARIO_NOT_DECLARED" | "DEVICE_ADMIN_RECEIVER" | "MALFORMED_XC_TEST_ZIP" | "BUILT_FOR_IOS_SIMULATOR" | "NO_TESTS_IN_XC_TEST_ZIP" | "USE_DESTINATION_ARTIFACTS" | "TEST_NOT_APP_HOSTED" | "PLIST_CANNOT_BE_PARSED" | "NO_CODE_APK" | "INVALID_INPUT_APK" | "INVALID_APK_PREVIEW_SDK";
139
+ export type InvalidMatrixDetails = "DETAILS_UNAVAILABLE" | "MALFORMED_APK" | "MALFORMED_TEST_APK" | "NO_MANIFEST" | "NO_PACKAGE_NAME" | "INVALID_PACKAGE_NAME" | "TEST_SAME_AS_APP" | "NO_INSTRUMENTATION" | "NO_SIGNATURE" | "INSTRUMENTATION_ORCHESTRATOR_INCOMPATIBLE" | "NO_TEST_RUNNER_CLASS" | "NO_LAUNCHER_ACTIVITY" | "FORBIDDEN_PERMISSIONS" | "INVALID_ROBO_DIRECTIVES" | "INVALID_RESOURCE_NAME" | "INVALID_DIRECTIVE_ACTION" | "TEST_LOOP_INTENT_FILTER_NOT_FOUND" | "SCENARIO_LABEL_NOT_DECLARED" | "SCENARIO_LABEL_MALFORMED" | "SCENARIO_NOT_DECLARED" | "DEVICE_ADMIN_RECEIVER" | "MALFORMED_XC_TEST_ZIP" | "BUILT_FOR_IOS_SIMULATOR" | "NO_TESTS_IN_XC_TEST_ZIP" | "USE_DESTINATION_ARTIFACTS" | "TEST_NOT_APP_HOSTED" | "PLIST_CANNOT_BE_PARSED" | "NO_CODE_APK" | "INVALID_INPUT_APK" | "INVALID_APK_PREVIEW_SDK";
140
140
  /**
141
141
  * The state (i.e. progress) of a TestMatrix.
142
142
  *
@@ -158,7 +158,7 @@ export declare type InvalidMatrixDetails = "DETAILS_UNAVAILABLE" | "MALFORMED_AP
158
158
  * valid. E.g. the input file is not of the expected type, or is
159
159
  * malformed/corrupt.
160
160
  */
161
- export declare type TestState = "VALIDATING" | "PENDING" | "FINISHED" | "ERROR" | "INVALID";
161
+ export type TestState = "VALIDATING" | "PENDING" | "FINISHED" | "ERROR" | "INVALID";
162
162
  /**
163
163
  * Outcome summary for a finished TestMatrix.
164
164
  *
@@ -181,4 +181,4 @@ export declare type TestState = "VALIDATING" | "PENDING" | "FINISHED" | "ERROR"
181
181
  * - `SKIPPED`: All tests were skipped, for instance:
182
182
  * - All device configurations were incompatible.
183
183
  */
184
- export declare type OutcomeSummary = "SUCCESS" | "FAILURE" | "INCONCLUSIVE" | "SKIPPED";
184
+ export type OutcomeSummary = "SUCCESS" | "FAILURE" | "INCONCLUSIVE" | "SKIPPED";
package/lib/v2/index.d.ts CHANGED
@@ -17,7 +17,8 @@ import * as storage from "./providers/storage";
17
17
  import * as tasks from "./providers/tasks";
18
18
  import * as remoteConfig from "./providers/remoteConfig";
19
19
  import * as testLab from "./providers/testLab";
20
- export { alerts, database, storage, https, identity, pubsub, logger, tasks, eventarc, scheduler, remoteConfig, testLab, };
20
+ import * as firestore from "./providers/firestore";
21
+ export { alerts, database, storage, https, identity, pubsub, logger, tasks, eventarc, scheduler, remoteConfig, testLab, firestore, };
21
22
  export { setGlobalOptions, GlobalOptions, SupportedRegion, MemoryOption, VpcEgressSetting, IngressSetting, EventHandlerOptions, } from "./options";
22
23
  export { CloudFunction, CloudEvent, ParamsOf } from "./core";
23
24
  export { Change } from "../common/change";
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.Change = exports.setGlobalOptions = 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.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;
25
25
  /**
26
26
  * The V2 API for Cloud Functions for Firebase.
27
27
  * This SDK also supports deep imports. For example, the namespace
@@ -53,6 +53,8 @@ const remoteConfig = require("./providers/remoteConfig");
53
53
  exports.remoteConfig = remoteConfig;
54
54
  const testLab = require("./providers/testLab");
55
55
  exports.testLab = testLab;
56
+ const firestore = require("./providers/firestore");
57
+ exports.firestore = firestore;
56
58
  var options_1 = require("./options");
57
59
  Object.defineProperty(exports, "setGlobalOptions", { enumerable: true, get: function () { return options_1.setGlobalOptions; } });
58
60
  var change_1 = require("../common/change");
@@ -5,19 +5,19 @@ export { RESET_VALUE } from "../common/options";
5
5
  /**
6
6
  * List of all regions supported by Cloud Functions v2
7
7
  */
8
- export declare type SupportedRegion = "asia-northeast1" | "europe-north1" | "europe-west1" | "europe-west4" | "us-central1" | "us-east1" | "us-west1";
8
+ export type SupportedRegion = "asia-northeast1" | "europe-north1" | "europe-west1" | "europe-west4" | "us-central1" | "us-east1" | "us-west1";
9
9
  /**
10
10
  * List of available memory options supported by Cloud Functions.
11
11
  */
12
- export declare type MemoryOption = "128MiB" | "256MiB" | "512MiB" | "1GiB" | "2GiB" | "4GiB" | "8GiB" | "16GiB" | "32GiB";
12
+ export type MemoryOption = "128MiB" | "256MiB" | "512MiB" | "1GiB" | "2GiB" | "4GiB" | "8GiB" | "16GiB" | "32GiB";
13
13
  /**
14
14
  * List of available options for VpcConnectorEgressSettings.
15
15
  */
16
- export declare type VpcEgressSetting = "PRIVATE_RANGES_ONLY" | "ALL_TRAFFIC";
16
+ export type VpcEgressSetting = "PRIVATE_RANGES_ONLY" | "ALL_TRAFFIC";
17
17
  /**
18
18
  * List of available options for IngressSettings.
19
19
  */
20
- export declare type IngressSetting = "ALLOW_ALL" | "ALLOW_INTERNAL_ONLY" | "ALLOW_INTERNAL_AND_GCLB";
20
+ export type IngressSetting = "ALLOW_ALL" | "ALLOW_INTERNAL_ONLY" | "ALLOW_INTERNAL_AND_GCLB";
21
21
  /**
22
22
  * GlobalOptions are options that can be set across an entire project.
23
23
  * These options are common to HTTPS and Event handling functions.
@@ -30,7 +30,7 @@ export interface GlobalOptions {
30
30
  /**
31
31
  * Region where functions should be deployed.
32
32
  */
33
- region?: SupportedRegion | string;
33
+ region?: SupportedRegion | string | Expression<string> | ResetValue;
34
34
  /**
35
35
  * Amount of memory to allocate to a function.
36
36
  */
@@ -91,7 +91,7 @@ export interface GlobalOptions {
91
91
  /**
92
92
  * Specific service account for the function to run as.
93
93
  */
94
- serviceAccount?: string | ResetValue;
94
+ serviceAccount?: string | Expression<string> | ResetValue;
95
95
  /**
96
96
  * Ingress settings which control where this function can be called from.
97
97
  */
@@ -140,9 +140,9 @@ export interface EventHandlerOptions extends Omit<GlobalOptions, "enforceAppChec
140
140
  /** Whether failed executions should be delivered again. */
141
141
  retry?: boolean | Expression<boolean> | ResetValue;
142
142
  /** Region of the EventArc trigger. */
143
- region?: string;
143
+ region?: string | Expression<string> | ResetValue;
144
144
  /** The service account that EventArc should use to invoke this function. Requires the P4SA to have ActAs permission on this service account. */
145
- serviceAccount?: string | ResetValue;
145
+ serviceAccount?: string | Expression<string> | ResetValue;
146
146
  /** The name of the channel where the function receives events. */
147
147
  channel?: string;
148
148
  }
@@ -31,7 +31,7 @@ export interface AlertEvent<T> extends CloudEvent<FirebaseAlertData<T>> {
31
31
  data: FirebaseAlertData<T>;
32
32
  }
33
33
  /** The underlying alert type of the Firebase Alerts provider. */
34
- export declare type AlertType = "crashlytics.newFatalIssue" | "crashlytics.newNonfatalIssue" | "crashlytics.regression" | "crashlytics.stabilityDigest" | "crashlytics.velocity" | "crashlytics.newAnrIssue" | "billing.planUpdate" | "billing.automatedPlanUpdate" | "appDistribution.newTesterIosDevice" | "appDistribution.inAppFeedback" | "performance.threshold" | string;
34
+ export type AlertType = "crashlytics.newFatalIssue" | "crashlytics.newNonfatalIssue" | "crashlytics.regression" | "crashlytics.stabilityDigest" | "crashlytics.velocity" | "crashlytics.newAnrIssue" | "billing.planUpdate" | "billing.automatedPlanUpdate" | "appDistribution.newTesterIosDevice" | "appDistribution.inAppFeedback" | "performance.threshold" | string;
35
35
  /**
36
36
  * Configuration for Firebase Alert functions.
37
37
  */
@@ -47,7 +47,7 @@ export interface FirebaseAlertOptions extends options.EventHandlerOptions {
47
47
  /**
48
48
  * Region where functions should be deployed.
49
49
  */
50
- region?: options.SupportedRegion | string;
50
+ region?: options.SupportedRegion | string | Expression<string> | ResetValue;
51
51
  /**
52
52
  * Amount of memory to allocate to a function.
53
53
  * A value of null restores the defaults of 256MB.
@@ -107,7 +107,7 @@ export interface FirebaseAlertOptions extends options.EventHandlerOptions {
107
107
  * Specific service account for the function to run as.
108
108
  * A value of null restores the default service account.
109
109
  */
110
- serviceAccount?: string | ResetValue;
110
+ serviceAccount?: string | Expression<string> | ResetValue;
111
111
  /**
112
112
  * Ingress settings which control where this function can be called from.
113
113
  * A value of null turns off ingress settings.
@@ -70,7 +70,7 @@ export interface AppDistributionOptions extends options.EventHandlerOptions {
70
70
  /**
71
71
  * Region where functions should be deployed.
72
72
  */
73
- region?: options.SupportedRegion | string;
73
+ region?: options.SupportedRegion | string | Expression<string> | ResetValue;
74
74
  /**
75
75
  * Amount of memory to allocate to a function.
76
76
  */
@@ -131,7 +131,7 @@ export interface AppDistributionOptions extends options.EventHandlerOptions {
131
131
  /**
132
132
  * Specific service account for the function to run as.
133
133
  */
134
- serviceAccount?: string | ResetValue;
134
+ serviceAccount?: string | Expression<string> | ResetValue;
135
135
  /**
136
136
  * Ingress settings which control where this function can be called from.
137
137
  */
@@ -136,7 +136,7 @@ export interface CrashlyticsOptions extends options.EventHandlerOptions {
136
136
  /**
137
137
  * Region where functions should be deployed.
138
138
  */
139
- region?: options.SupportedRegion | string;
139
+ region?: options.SupportedRegion | string | Expression<string> | ResetValue;
140
140
  /**
141
141
  * Amount of memory to allocate to a function.
142
142
  */
@@ -197,7 +197,7 @@ export interface CrashlyticsOptions extends options.EventHandlerOptions {
197
197
  /**
198
198
  * Specific service account for the function to run as.
199
199
  */
200
- serviceAccount?: string | ResetValue;
200
+ serviceAccount?: string | Expression<string> | ResetValue;
201
201
  /**
202
202
  * Ingress settings which control where this function can be called from.
203
203
  */
@@ -58,7 +58,7 @@ export interface ReferenceOptions<Ref extends string = string> extends options.E
58
58
  /**
59
59
  * Region where functions should be deployed.
60
60
  */
61
- region?: options.SupportedRegion | string;
61
+ region?: options.SupportedRegion | string | Expression<string> | ResetValue;
62
62
  /**
63
63
  * Amount of memory to allocate to a function.
64
64
  */
@@ -71,7 +71,7 @@ export interface ReferenceOptions<Ref extends string = string> extends options.E
71
71
  * The minimum timeout for a gen 2 function is 1s. The maximum timeout for a
72
72
  * function depends on the type of function: Event handling functions have a
73
73
  * maximum timeout of 540s (9 minutes). HTTPS and callable functions have a
74
- * maximum timeout of 36,00s (1 hour). Task queue functions have a maximum
74
+ * maximum timeout of 3,600s (1 hour). Task queue functions have a maximum
75
75
  * timeout of 1,800s (30 minutes)
76
76
  */
77
77
  timeoutSeconds?: number | Expression<number> | ResetValue;
@@ -119,7 +119,7 @@ export interface ReferenceOptions<Ref extends string = string> extends options.E
119
119
  /**
120
120
  * Specific service account for the function to run as.
121
121
  */
122
- serviceAccount?: string | ResetValue;
122
+ serviceAccount?: string | Expression<string> | ResetValue;
123
123
  /**
124
124
  * Ingress settings which control where this function can be called from.
125
125
  */
@@ -178,7 +178,7 @@ function onChangedOperation(eventType, referenceOrOpts, handler) {
178
178
  // wrap the handler
179
179
  const func = (raw) => {
180
180
  const event = raw;
181
- const instanceUrl = `https://${event.instance}.${event.firebasedatabasehost}`;
181
+ const instanceUrl = getInstance(event);
182
182
  const params = makeParams(event, pathPattern, instancePattern);
183
183
  const databaseEvent = makeChangedDatabaseEvent(event, instanceUrl, params);
184
184
  return (0, trace_1.wrapTraceContext)(handler)(databaseEvent);
@@ -196,7 +196,7 @@ function onOperation(eventType, referenceOrOpts, handler) {
196
196
  // wrap the handler
197
197
  const func = (raw) => {
198
198
  const event = raw;
199
- const instanceUrl = `https://${event.instance}.${event.firebasedatabasehost}`;
199
+ const instanceUrl = getInstance(event);
200
200
  const params = makeParams(event, pathPattern, instancePattern);
201
201
  const data = eventType === exports.deletedEventType ? event.data.data : event.data.delta;
202
202
  const databaseEvent = makeDatabaseEvent(event, data, instanceUrl, params);
@@ -207,3 +207,9 @@ function onOperation(eventType, referenceOrOpts, handler) {
207
207
  return func;
208
208
  }
209
209
  exports.onOperation = onOperation;
210
+ function getInstance(event) {
211
+ const emuHost = process.env.FIREBASE_DATABASE_EMULATOR_HOST;
212
+ return emuHost
213
+ ? `http://${emuHost}/?ns=${event.instance}`
214
+ : `https://${event.instance}.${event.firebasedatabasehost}`;
215
+ }
@@ -35,7 +35,7 @@ export interface EventarcTriggerOptions extends options.EventHandlerOptions {
35
35
  /**
36
36
  * Region where functions should be deployed.
37
37
  */
38
- region?: options.SupportedRegion | string;
38
+ region?: options.SupportedRegion | string | Expression<string> | ResetValue;
39
39
  /**
40
40
  * Amount of memory to allocate to a function.
41
41
  */
@@ -96,7 +96,7 @@ export interface EventarcTriggerOptions extends options.EventHandlerOptions {
96
96
  /**
97
97
  * Specific service account for the function to run as.
98
98
  */
99
- serviceAccount?: string | ResetValue;
99
+ serviceAccount?: string | Expression<string> | ResetValue;
100
100
  /**
101
101
  * Ingress settings which control where this function can be called from.
102
102
  */
@@ -0,0 +1,91 @@
1
+ import * as firestore from "firebase-admin/firestore";
2
+ import { ParamsOf } from "../../common/params";
3
+ import { Change, CloudEvent, CloudFunction } from "../core";
4
+ import { EventHandlerOptions } from "../options";
5
+ /** A Firestore DocumentSnapshot */
6
+ export type DocumentSnapshot = firestore.DocumentSnapshot;
7
+ /** A Firestore QueryDocumentSnapshot */
8
+ export type QueryDocumentSnapshot = firestore.QueryDocumentSnapshot;
9
+ /** A CloudEvent that contains a DocumentSnapshot or a Change<DocumentSnapshot> */
10
+ export interface FirestoreEvent<T, Params = Record<string, string>> extends CloudEvent<T> {
11
+ /** The location of the Firestore instance */
12
+ location: string;
13
+ /** The project identifier */
14
+ project: string;
15
+ /** The Firestore database */
16
+ database: string;
17
+ /** The Firestore namespace */
18
+ namespace: string;
19
+ /** The document path */
20
+ document: string;
21
+ /**
22
+ * An object containing the values of the path patterns.
23
+ * Only named capture groups will be populated - {key}, {key=*}, {key=**}
24
+ */
25
+ params: Params;
26
+ }
27
+ /** DocumentOptions extend EventHandlerOptions with provided document and optional database and namespace. */
28
+ export interface DocumentOptions<Document extends string = string> extends EventHandlerOptions {
29
+ /** The document path */
30
+ document: Document;
31
+ /** The Firestore database */
32
+ database?: string;
33
+ /** The Firestore namespace */
34
+ namespace?: string;
35
+ }
36
+ /**
37
+ * Event handler which triggers when a document is created, updated, or deleted in Firestore.
38
+ *
39
+ * @param document - The Firestore document path to trigger on.
40
+ * @param handler - Event handler which is run every time a Firestore create, update, or delete occurs.
41
+ */
42
+ export declare function onDocumentWritten<Document extends string>(document: Document, handler: (event: FirestoreEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>>;
43
+ /**
44
+ * Event handler which triggers when a document is created, updated, or deleted in Firestore.
45
+ *
46
+ * @param opts - Options that can be set on an individual event-handling function.
47
+ * @param handler - Event handler which is run every time a Firestore create, update, or delete occurs.
48
+ */
49
+ export declare function onDocumentWritten<Document extends string>(opts: DocumentOptions<Document>, handler: (event: FirestoreEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>>;
50
+ /**
51
+ * Event handler which triggers when a document is created in Firestore.
52
+ *
53
+ * @param document - The Firestore document path to trigger on.
54
+ * @param handler - Event handler which is run every time a Firestore create occurs.
55
+ */
56
+ export declare function onDocumentCreated<Document extends string>(document: Document, handler: (event: FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>>;
57
+ /**
58
+ * Event handler which triggers when a document is created in Firestore.
59
+ *
60
+ * @param opts - Options that can be set on an individual event-handling function.
61
+ * @param handler - Event handler which is run every time a Firestore create occurs.
62
+ */
63
+ export declare function onDocumentCreated<Document extends string>(opts: DocumentOptions<Document>, handler: (event: FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>>;
64
+ /**
65
+ * Event handler which triggers when a document is updated in Firestore.
66
+ *
67
+ * @param document - The Firestore document path to trigger on.
68
+ * @param handler - Event handler which is run every time a Firestore update occurs.
69
+ */
70
+ export declare function onDocumentUpdated<Document extends string>(document: Document, handler: (event: FirestoreEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>>>;
71
+ /**
72
+ * Event handler which triggers when a document is updated in Firestore.
73
+ *
74
+ * @param opts - Options that can be set on an individual event-handling function.
75
+ * @param handler - Event handler which is run every time a Firestore update occurs.
76
+ */
77
+ export declare function onDocumentUpdated<Document extends string>(opts: DocumentOptions<Document>, handler: (event: FirestoreEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>>>;
78
+ /**
79
+ * Event handler which triggers when a document is deleted in Firestore.
80
+ *
81
+ * @param document - The Firestore document path to trigger on.
82
+ * @param handler - Event handler which is run every time a Firestore delete occurs.
83
+ */
84
+ export declare function onDocumentDeleted<Document extends string>(document: Document, handler: (event: FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>>;
85
+ /**
86
+ * Event handler which triggers when a document is deleted in Firestore.
87
+ *
88
+ * @param opts - Options that can be set on an individual event-handling function.
89
+ * @param handler - Event handler which is run every time a Firestore delete occurs.
90
+ */
91
+ export declare function onDocumentDeleted<Document extends string>(opts: DocumentOptions<Document>, handler: (event: FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>>;
@@ -0,0 +1,243 @@
1
+ "use strict";
2
+ // The MIT License (MIT)
3
+ //
4
+ // Copyright (c) 2023 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.onChangedOperation = exports.onOperation = exports.makeEndpoint = exports.makeChangedFirestoreEvent = exports.makeFirestoreEvent = exports.makeParams = exports.createBeforeSnapshot = exports.createSnapshot = exports.getOpts = exports.onDocumentDeleted = exports.onDocumentUpdated = exports.onDocumentCreated = exports.onDocumentWritten = exports.deletedEventType = exports.updatedEventType = exports.createdEventType = exports.writtenEventType = void 0;
25
+ const logger = require("../../logger");
26
+ const path_1 = require("../../common/utilities/path");
27
+ const path_pattern_1 = require("../../common/utilities/path-pattern");
28
+ const manifest_1 = require("../../runtime/manifest");
29
+ const core_1 = require("../core");
30
+ const options_1 = require("../options");
31
+ const firestore_1 = require("../../common/providers/firestore");
32
+ /** @internal */
33
+ exports.writtenEventType = "google.cloud.firestore.document.v1.written";
34
+ /** @internal */
35
+ exports.createdEventType = "google.cloud.firestore.document.v1.created";
36
+ /** @internal */
37
+ exports.updatedEventType = "google.cloud.firestore.document.v1.updated";
38
+ /** @internal */
39
+ exports.deletedEventType = "google.cloud.firestore.document.v1.deleted";
40
+ /**
41
+ * Event handler which triggers when a document is created, updated, or deleted in Firestore.
42
+ *
43
+ * @param documentOrOpts - Options or a string document path.
44
+ * @param handler - Event handler which is run every time a Firestore create, update, or delete occurs.
45
+ */
46
+ function onDocumentWritten(documentOrOpts, handler) {
47
+ return onChangedOperation(exports.writtenEventType, documentOrOpts, handler);
48
+ }
49
+ exports.onDocumentWritten = onDocumentWritten;
50
+ /**
51
+ * Event handler which triggers when a document is created in Firestore.
52
+ *
53
+ * @param documentOrOpts - Options or a string document path.
54
+ * @param handler - Event handler which is run every time a Firestore create occurs.
55
+ */
56
+ function onDocumentCreated(documentOrOpts, handler) {
57
+ return onOperation(exports.createdEventType, documentOrOpts, handler);
58
+ }
59
+ exports.onDocumentCreated = onDocumentCreated;
60
+ /**
61
+ * Event handler which triggers when a document is updated in Firestore.
62
+ *
63
+ * @param documentOrOpts - Options or a string document path.
64
+ * @param handler - Event handler which is run every time a Firestore update occurs.
65
+ */
66
+ function onDocumentUpdated(documentOrOpts, handler) {
67
+ return onChangedOperation(exports.updatedEventType, documentOrOpts, handler);
68
+ }
69
+ exports.onDocumentUpdated = onDocumentUpdated;
70
+ /**
71
+ * Event handler which triggers when a document is deleted in Firestore.
72
+ *
73
+ * @param documentOrOpts - Options or a string document path.
74
+ * @param handler - Event handler which is run every time a Firestore delete occurs.
75
+ */
76
+ function onDocumentDeleted(documentOrOpts, handler) {
77
+ return onOperation(exports.deletedEventType, documentOrOpts, handler);
78
+ }
79
+ exports.onDocumentDeleted = onDocumentDeleted;
80
+ /** @internal */
81
+ function getOpts(documentOrOpts) {
82
+ let document;
83
+ let database;
84
+ let namespace;
85
+ let opts;
86
+ if (typeof documentOrOpts === "string") {
87
+ document = (0, path_1.normalizePath)(documentOrOpts);
88
+ database = "(default)";
89
+ namespace = "(default)";
90
+ opts = {};
91
+ }
92
+ else {
93
+ document = (0, path_1.normalizePath)(documentOrOpts.document);
94
+ database = documentOrOpts.database || "(default)";
95
+ namespace = documentOrOpts.namespace || "(default)";
96
+ opts = { ...documentOrOpts };
97
+ delete opts.document;
98
+ delete opts.database;
99
+ delete opts.namespace;
100
+ }
101
+ return {
102
+ document,
103
+ database,
104
+ namespace,
105
+ opts,
106
+ };
107
+ }
108
+ exports.getOpts = getOpts;
109
+ /** @hidden */
110
+ function getPath(event) {
111
+ return `projects/${event.project}/databases/${event.database}/documents/${event.document}`;
112
+ }
113
+ /** @internal */
114
+ function createSnapshot(event) {
115
+ var _a, _b, _c, _d;
116
+ if (((_a = event.datacontenttype) === null || _a === void 0 ? void 0 : _a.includes("application/protobuf")) || Buffer.isBuffer(event.data)) {
117
+ return (0, firestore_1.createSnapshotFromProtobuf)(event.data, getPath(event));
118
+ }
119
+ else if ((_b = event.datacontenttype) === null || _b === void 0 ? void 0 : _b.includes("application/json")) {
120
+ return (0, firestore_1.createSnapshotFromJson)(event.data, event.source, (_c = event.data.value) === null || _c === void 0 ? void 0 : _c.createTime, (_d = event.data.value) === null || _d === void 0 ? void 0 : _d.updateTime);
121
+ }
122
+ else {
123
+ logger.error(`Cannot determine payload type, datacontenttype is ${event.datacontenttype}, failing out.`);
124
+ throw Error("Error: Cannot parse event payload.");
125
+ }
126
+ }
127
+ exports.createSnapshot = createSnapshot;
128
+ /** @internal */
129
+ function createBeforeSnapshot(event) {
130
+ var _a, _b, _c, _d;
131
+ if (((_a = event.datacontenttype) === null || _a === void 0 ? void 0 : _a.includes("application/protobuf")) || Buffer.isBuffer(event.data)) {
132
+ return (0, firestore_1.createBeforeSnapshotFromProtobuf)(event.data, getPath(event));
133
+ }
134
+ else if ((_b = event.datacontenttype) === null || _b === void 0 ? void 0 : _b.includes("application/json")) {
135
+ return (0, firestore_1.createBeforeSnapshotFromJson)(event.data, event.source, (_c = event.data.oldValue) === null || _c === void 0 ? void 0 : _c.createTime, (_d = event.data.oldValue) === null || _d === void 0 ? void 0 : _d.updateTime);
136
+ }
137
+ else {
138
+ logger.error(`Cannot determine payload type, datacontenttype is ${event.datacontenttype}, failing out.`);
139
+ throw Error("Error: Cannot parse event payload.");
140
+ }
141
+ }
142
+ exports.createBeforeSnapshot = createBeforeSnapshot;
143
+ /** @internal */
144
+ function makeParams(document, documentPattern) {
145
+ return {
146
+ ...documentPattern.extractMatches(document),
147
+ };
148
+ }
149
+ exports.makeParams = makeParams;
150
+ /** @internal */
151
+ function makeFirestoreEvent(eventType, event, params) {
152
+ const data = event.data
153
+ ? eventType === exports.createdEventType
154
+ ? createSnapshot(event)
155
+ : createBeforeSnapshot(event)
156
+ : undefined;
157
+ const firestoreEvent = {
158
+ ...event,
159
+ params,
160
+ data,
161
+ };
162
+ delete firestoreEvent.datacontenttype;
163
+ delete firestoreEvent.dataschema;
164
+ return firestoreEvent;
165
+ }
166
+ exports.makeFirestoreEvent = makeFirestoreEvent;
167
+ /** @internal */
168
+ function makeChangedFirestoreEvent(event, params) {
169
+ const data = event.data
170
+ ? core_1.Change.fromObjects(createBeforeSnapshot(event), createSnapshot(event))
171
+ : undefined;
172
+ const firestoreEvent = {
173
+ ...event,
174
+ params,
175
+ data,
176
+ };
177
+ delete firestoreEvent.datacontenttype;
178
+ delete firestoreEvent.dataschema;
179
+ return firestoreEvent;
180
+ }
181
+ exports.makeChangedFirestoreEvent = makeChangedFirestoreEvent;
182
+ /** @internal */
183
+ function makeEndpoint(eventType, opts, document, database, namespace) {
184
+ const baseOpts = (0, options_1.optionsToEndpoint)((0, options_1.getGlobalOptions)());
185
+ const specificOpts = (0, options_1.optionsToEndpoint)(opts);
186
+ const eventFilters = {
187
+ database,
188
+ namespace,
189
+ };
190
+ const eventFilterPathPatterns = {};
191
+ document.hasWildcards()
192
+ ? (eventFilterPathPatterns.document = document.getValue())
193
+ : (eventFilters.document = document.getValue());
194
+ return {
195
+ ...(0, manifest_1.initV2Endpoint)((0, options_1.getGlobalOptions)(), opts),
196
+ platform: "gcfv2",
197
+ ...baseOpts,
198
+ ...specificOpts,
199
+ labels: {
200
+ ...baseOpts === null || baseOpts === void 0 ? void 0 : baseOpts.labels,
201
+ ...specificOpts === null || specificOpts === void 0 ? void 0 : specificOpts.labels,
202
+ },
203
+ eventTrigger: {
204
+ eventType,
205
+ eventFilters,
206
+ eventFilterPathPatterns,
207
+ retry: !!opts.retry,
208
+ },
209
+ };
210
+ }
211
+ exports.makeEndpoint = makeEndpoint;
212
+ /** @internal */
213
+ function onOperation(eventType, documentOrOpts, handler) {
214
+ const { document, database, namespace, opts } = getOpts(documentOrOpts);
215
+ const documentPattern = new path_pattern_1.PathPattern(document);
216
+ // wrap the handler
217
+ const func = (raw) => {
218
+ const event = raw;
219
+ const params = makeParams(event.document, documentPattern);
220
+ const firestoreEvent = makeFirestoreEvent(eventType, event, params);
221
+ return handler(firestoreEvent);
222
+ };
223
+ func.run = handler;
224
+ func.__endpoint = makeEndpoint(eventType, opts, documentPattern, database, namespace);
225
+ return func;
226
+ }
227
+ exports.onOperation = onOperation;
228
+ /** @internal */
229
+ function onChangedOperation(eventType, documentOrOpts, handler) {
230
+ const { document, database, namespace, opts } = getOpts(documentOrOpts);
231
+ const documentPattern = new path_pattern_1.PathPattern(document);
232
+ // wrap the handler
233
+ const func = (raw) => {
234
+ const event = raw;
235
+ const params = makeParams(event.document, documentPattern);
236
+ const firestoreEvent = makeChangedFirestoreEvent(event, params);
237
+ return handler(firestoreEvent);
238
+ };
239
+ func.run = handler;
240
+ func.__endpoint = makeEndpoint(eventType, opts, documentPattern, database, namespace);
241
+ return func;
242
+ }
243
+ exports.onChangedOperation = onChangedOperation;
@@ -16,7 +16,7 @@ export interface HttpsOptions extends Omit<GlobalOptions, "region"> {
16
16
  */
17
17
  omit?: boolean | Expression<boolean>;
18
18
  /** HTTP functions can override global options and can specify multiple regions to deploy to. */
19
- region?: SupportedRegion | string | Array<SupportedRegion | string>;
19
+ region?: SupportedRegion | string | Array<SupportedRegion | string> | Expression<string> | ResetValue;
20
20
  /** If true, allows CORS on requests to this function.
21
21
  * If this is a `string` or `RegExp`, allows requests from domains that match the provided value.
22
22
  * If this is an `Array`, allows requests from domains matching at least one entry of the array.
@@ -83,7 +83,7 @@ export interface HttpsOptions extends Omit<GlobalOptions, "region"> {
83
83
  /**
84
84
  * Specific service account for the function to run as.
85
85
  */
86
- serviceAccount?: string | ResetValue;
86
+ serviceAccount?: string | Expression<string> | ResetValue;
87
87
  /**
88
88
  * Ingress settings which control where this function can be called from.
89
89
  */
@@ -113,7 +113,7 @@ export interface CallableOptions extends HttpsOptions {
113
113
  /**
114
114
  * Handles HTTPS requests.
115
115
  */
116
- export declare type HttpsFunction = ((
116
+ export type HttpsFunction = ((
117
117
  /** An Express request object representing the HTTPS call to the function. */
118
118
  req: Request,
119
119
  /** An Express response object, for this function to respond to callers. */
@@ -33,7 +33,7 @@ export interface BlockingOptions {
33
33
  /**
34
34
  * Region where functions should be deployed.
35
35
  */
36
- region?: options.SupportedRegion | string;
36
+ region?: options.SupportedRegion | string | Expression<string> | ResetValue;
37
37
  /**
38
38
  * Amount of memory to allocate to a function.
39
39
  */
@@ -94,7 +94,7 @@ export interface BlockingOptions {
94
94
  /**
95
95
  * Specific service account for the function to run as.
96
96
  */
97
- serviceAccount?: string | ResetValue;
97
+ serviceAccount?: string | Expression<string> | ResetValue;
98
98
  /**
99
99
  * Ingress settings which control where this function can be called from.
100
100
  */