firebase-functions 4.8.1 → 4.9.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.
@@ -35,7 +35,7 @@ Object.defineProperty(exports, "Change", { enumerable: true, get: function () {
35
35
  const WILDCARD_REGEX = new RegExp("{[^/{}]*}", "g");
36
36
  /** @internal */
37
37
  function makeCloudFunction({ contextOnlyHandler, dataConstructor = (raw) => raw.data, eventType, handler, labels = {}, legacyEventType, options = {}, provider, service, triggerResource, }) {
38
- handler = (0, onInit_1.withInit)(handler);
38
+ handler = (0, onInit_1.withInit)(handler !== null && handler !== void 0 ? handler : contextOnlyHandler);
39
39
  const cloudFunction = (data, context) => {
40
40
  if (legacyEventType && context.eventType === legacyEventType) {
41
41
  /*
@@ -8,6 +8,15 @@ export { Change };
8
8
  export type DocumentSnapshot = firestore.DocumentSnapshot;
9
9
  /** A Firestore QueryDocumentSnapshot */
10
10
  export type QueryDocumentSnapshot = firestore.QueryDocumentSnapshot;
11
+ /**
12
+ * AuthType defines the possible values for the authType field in a Firestore event with auth context.
13
+ * - service_account: a non-user principal used to identify a workload or machine user.
14
+ * - api_key: a non-user client API key.
15
+ * - system: an obscured identity used when Cloud Platform or another system triggered the event. Examples include a database record which was deleted based on a TTL.
16
+ * - unauthenticated: an unauthenticated action.
17
+ * - unknown: a general type to capture all other principals not captured in the other auth types.
18
+ */
19
+ export type AuthType = "service_account" | "api_key" | "system" | "unauthenticated" | "unknown";
11
20
  /** A CloudEvent that contains a DocumentSnapshot or a Change<DocumentSnapshot> */
12
21
  export interface FirestoreEvent<T, Params = Record<string, string>> extends CloudEvent<T> {
13
22
  /** The location of the Firestore instance */
@@ -26,6 +35,12 @@ export interface FirestoreEvent<T, Params = Record<string, string>> extends Clou
26
35
  */
27
36
  params: Params;
28
37
  }
38
+ export interface FirestoreAuthEvent<T, Params = Record<string, string>> extends FirestoreEvent<T, Params> {
39
+ /** The type of principal that triggered the event */
40
+ authType: AuthType;
41
+ /** The unique identifier for the principal */
42
+ authId?: string;
43
+ }
29
44
  /** DocumentOptions extend EventHandlerOptions with provided document and optional database and namespace. */
30
45
  export interface DocumentOptions<Document extends string = string> extends EventHandlerOptions {
31
46
  /** The document path */
@@ -36,58 +51,122 @@ export interface DocumentOptions<Document extends string = string> extends Event
36
51
  namespace?: string | Expression<string>;
37
52
  }
38
53
  /**
39
- * Event handler which triggers when a document is created, updated, or deleted in Firestore.
54
+ * Event handler that triggers when a document is created, updated, or deleted in Firestore.
40
55
  *
41
56
  * @param document - The Firestore document path to trigger on.
42
57
  * @param handler - Event handler which is run every time a Firestore create, update, or delete occurs.
43
58
  */
44
59
  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>>>;
45
60
  /**
46
- * Event handler which triggers when a document is created, updated, or deleted in Firestore.
61
+ * Event handler that triggers when a document is created, updated, or deleted in Firestore.
47
62
  *
48
63
  * @param opts - Options that can be set on an individual event-handling function.
49
64
  * @param handler - Event handler which is run every time a Firestore create, update, or delete occurs.
50
65
  */
51
66
  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>>>;
52
67
  /**
53
- * Event handler which triggers when a document is created in Firestore.
68
+ * Event handler that triggers when a document is created, updated, or deleted in Firestore.
69
+ * This trigger also provides the authentication context of the principal who triggered the event.
70
+ *
71
+ * @param document - The Firestore document path to trigger on.
72
+ * @param handler - Event handler which is run every time a Firestore create, update, or delete occurs.
73
+ */
74
+ export declare function onDocumentWrittenWithAuthContext<Document extends string>(document: Document, handler: (event: FirestoreAuthEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreAuthEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>>;
75
+ /**
76
+ * Event handler that triggers when a document is created, updated, or deleted in Firestore.
77
+ * This trigger also provides the authentication context of the principal who triggered the event.
78
+ *
79
+ * @param opts - Options that can be set on an individual event-handling function.
80
+ * @param handler - Event handler which is run every time a Firestore create, update, or delete occurs.
81
+ */
82
+ export declare function onDocumentWrittenWithAuthContext<Document extends string>(opts: DocumentOptions<Document>, handler: (event: FirestoreAuthEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreAuthEvent<Change<DocumentSnapshot> | undefined, ParamsOf<Document>>>;
83
+ /**
84
+ * Event handler that triggers when a document is created in Firestore.
54
85
  *
55
86
  * @param document - The Firestore document path to trigger on.
56
87
  * @param handler - Event handler which is run every time a Firestore create occurs.
57
88
  */
58
89
  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>>>;
59
90
  /**
60
- * Event handler which triggers when a document is created in Firestore.
91
+ * Event handler that triggers when a document is created in Firestore.
61
92
  *
62
93
  * @param opts - Options that can be set on an individual event-handling function.
63
94
  * @param handler - Event handler which is run every time a Firestore create occurs.
64
95
  */
65
96
  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>>>;
66
97
  /**
67
- * Event handler which triggers when a document is updated in Firestore.
98
+ * Event handler that triggers when a document is created in Firestore.
99
+ * This trigger also provides the authentication context of the principal who triggered the event.
100
+ *
101
+ * @param document - The Firestore document path to trigger on.
102
+ * @param handler - Event handler which is run every time a Firestore create occurs.
103
+ */
104
+ export declare function onDocumentCreatedWithAuthContext<Document extends string>(document: Document, handler: (event: FirestoreAuthEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreAuthEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>>;
105
+ /**
106
+ * Event handler that triggers when a document is created in Firestore.
107
+ * This trigger also provides the authentication context of the principal who triggered the event.
108
+ *
109
+ * @param opts - Options that can be set on an individual event-handling function.
110
+ * @param handler - Event handler which is run every time a Firestore create occurs.
111
+ */
112
+ export declare function onDocumentCreatedWithAuthContext<Document extends string>(opts: DocumentOptions<Document>, handler: (event: FirestoreAuthEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreAuthEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>>;
113
+ /**
114
+ * Event handler that triggers when a document is updated in Firestore.
68
115
  *
69
116
  * @param document - The Firestore document path to trigger on.
70
117
  * @param handler - Event handler which is run every time a Firestore update occurs.
71
118
  */
72
119
  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>>>;
73
120
  /**
74
- * Event handler which triggers when a document is updated in Firestore.
121
+ * Event handler that triggers when a document is updated in Firestore.
75
122
  *
76
123
  * @param opts - Options that can be set on an individual event-handling function.
77
124
  * @param handler - Event handler which is run every time a Firestore update occurs.
78
125
  */
79
126
  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>>>;
80
127
  /**
81
- * Event handler which triggers when a document is deleted in Firestore.
128
+ * Event handler that triggers when a document is updated in Firestore.
129
+ * This trigger also provides the authentication context of the principal who triggered the event.
130
+ *
131
+ * @param document - The Firestore document path to trigger on.
132
+ * @param handler - Event handler which is run every time a Firestore update occurs.
133
+ */
134
+ export declare function onDocumentUpdatedWithAuthContext<Document extends string>(document: Document, handler: (event: FirestoreAuthEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreAuthEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>>>;
135
+ /**
136
+ * Event handler that triggers when a document is updated in Firestore.
137
+ * This trigger also provides the authentication context of the principal who triggered the event.
138
+ *
139
+ * @param opts - Options that can be set on an individual event-handling function.
140
+ * @param handler - Event handler which is run every time a Firestore update occurs.
141
+ */
142
+ export declare function onDocumentUpdatedWithAuthContext<Document extends string>(opts: DocumentOptions<Document>, handler: (event: FirestoreAuthEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreAuthEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>>>;
143
+ /**
144
+ * Event handler that triggers when a document is deleted in Firestore.
82
145
  *
83
146
  * @param document - The Firestore document path to trigger on.
84
147
  * @param handler - Event handler which is run every time a Firestore delete occurs.
85
148
  */
86
149
  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>>>;
87
150
  /**
88
- * Event handler which triggers when a document is deleted in Firestore.
151
+ * Event handler that triggers when a document is deleted in Firestore.
89
152
  *
90
153
  * @param opts - Options that can be set on an individual event-handling function.
91
154
  * @param handler - Event handler which is run every time a Firestore delete occurs.
92
155
  */
93
156
  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>>>;
157
+ /**
158
+ * Event handler that triggers when a document is deleted in Firestore.
159
+ * This trigger also provides the authentication context of the principal who triggered the event.
160
+ *
161
+ * @param document - The Firestore document path to trigger on.
162
+ * @param handler - Event handler which is run every time a Firestore delete occurs.
163
+ */
164
+ export declare function onDocumentDeletedWithAuthContext<Document extends string>(document: Document, handler: (event: FirestoreAuthEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreAuthEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>>;
165
+ /**
166
+ * Event handler that triggers when a document is deleted in Firestore.
167
+ * This trigger also provides the authentication context of the principal who triggered the event.
168
+ *
169
+ * @param opts - Options that can be set on an individual event-handling function.
170
+ * @param handler - Event handler which is run every time a Firestore delete occurs.
171
+ */
172
+ export declare function onDocumentDeletedWithAuthContext<Document extends string>(opts: DocumentOptions<Document>, handler: (event: FirestoreAuthEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>) => any | Promise<any>): CloudFunction<FirestoreAuthEvent<QueryDocumentSnapshot | undefined, ParamsOf<Document>>>;
@@ -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.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 = exports.Change = void 0;
24
+ exports.onChangedOperation = exports.onOperation = exports.makeEndpoint = exports.makeChangedFirestoreEvent = exports.makeFirestoreEvent = exports.makeParams = exports.createBeforeSnapshot = exports.createSnapshot = exports.getOpts = exports.onDocumentDeletedWithAuthContext = exports.onDocumentDeleted = exports.onDocumentUpdatedWithAuthContext = exports.onDocumentUpdated = exports.onDocumentCreatedWithAuthContext = exports.onDocumentCreated = exports.onDocumentWrittenWithAuthContext = exports.onDocumentWritten = exports.deletedEventWithAuthContextType = exports.updatedEventWithAuthContextType = exports.createdEventWithAuthContextType = exports.writtenEventWithAuthContextType = exports.deletedEventType = exports.updatedEventType = exports.createdEventType = exports.writtenEventType = exports.Change = void 0;
25
25
  const logger = require("../../logger");
26
26
  const path_1 = require("../../common/utilities/path");
27
27
  const path_pattern_1 = require("../../common/utilities/path-pattern");
@@ -40,8 +40,16 @@ exports.createdEventType = "google.cloud.firestore.document.v1.created";
40
40
  exports.updatedEventType = "google.cloud.firestore.document.v1.updated";
41
41
  /** @internal */
42
42
  exports.deletedEventType = "google.cloud.firestore.document.v1.deleted";
43
+ /** @internal */
44
+ exports.writtenEventWithAuthContextType = "google.cloud.firestore.document.v1.written.withAuthContext";
45
+ /** @internal */
46
+ exports.createdEventWithAuthContextType = "google.cloud.firestore.document.v1.created.withAuthContext";
47
+ /** @internal */
48
+ exports.updatedEventWithAuthContextType = "google.cloud.firestore.document.v1.updated.withAuthContext";
49
+ /** @internal */
50
+ exports.deletedEventWithAuthContextType = "google.cloud.firestore.document.v1.deleted.withAuthContext";
43
51
  /**
44
- * Event handler which triggers when a document is created, updated, or deleted in Firestore.
52
+ * Event handler that triggers when a document is created, updated, or deleted in Firestore.
45
53
  *
46
54
  * @param documentOrOpts - Options or a string document path.
47
55
  * @param handler - Event handler which is run every time a Firestore create, update, or delete occurs.
@@ -51,7 +59,18 @@ function onDocumentWritten(documentOrOpts, handler) {
51
59
  }
52
60
  exports.onDocumentWritten = onDocumentWritten;
53
61
  /**
54
- * Event handler which triggers when a document is created in Firestore.
62
+ * Event handler that triggers when a document is created, updated, or deleted in Firestore.
63
+ * This trigger also provides the authentication context of the principal who triggered the event.
64
+ *
65
+ * @param opts - Options or a string document path.
66
+ * @param handler - Event handler which is run every time a Firestore create, update, or delete occurs.
67
+ */
68
+ function onDocumentWrittenWithAuthContext(documentOrOpts, handler) {
69
+ return onChangedOperation(exports.writtenEventWithAuthContextType, documentOrOpts, handler);
70
+ }
71
+ exports.onDocumentWrittenWithAuthContext = onDocumentWrittenWithAuthContext;
72
+ /**
73
+ * Event handler that triggers when a document is created in Firestore.
55
74
  *
56
75
  * @param documentOrOpts - Options or a string document path.
57
76
  * @param handler - Event handler which is run every time a Firestore create occurs.
@@ -61,7 +80,17 @@ function onDocumentCreated(documentOrOpts, handler) {
61
80
  }
62
81
  exports.onDocumentCreated = onDocumentCreated;
63
82
  /**
64
- * Event handler which triggers when a document is updated in Firestore.
83
+ * Event handler that triggers when a document is created in Firestore.
84
+ *
85
+ * @param documentOrOpts - Options or a string document path.
86
+ * @param handler - Event handler which is run every time a Firestore create occurs.
87
+ */
88
+ function onDocumentCreatedWithAuthContext(documentOrOpts, handler) {
89
+ return onOperation(exports.createdEventWithAuthContextType, documentOrOpts, handler);
90
+ }
91
+ exports.onDocumentCreatedWithAuthContext = onDocumentCreatedWithAuthContext;
92
+ /**
93
+ * Event handler that triggers when a document is updated in Firestore.
65
94
  *
66
95
  * @param documentOrOpts - Options or a string document path.
67
96
  * @param handler - Event handler which is run every time a Firestore update occurs.
@@ -71,7 +100,17 @@ function onDocumentUpdated(documentOrOpts, handler) {
71
100
  }
72
101
  exports.onDocumentUpdated = onDocumentUpdated;
73
102
  /**
74
- * Event handler which triggers when a document is deleted in Firestore.
103
+ * Event handler that triggers when a document is updated in Firestore.
104
+ *
105
+ * @param documentOrOpts - Options or a string document path.
106
+ * @param handler - Event handler which is run every time a Firestore update occurs.
107
+ */
108
+ function onDocumentUpdatedWithAuthContext(documentOrOpts, handler) {
109
+ return onChangedOperation(exports.updatedEventWithAuthContextType, documentOrOpts, handler);
110
+ }
111
+ exports.onDocumentUpdatedWithAuthContext = onDocumentUpdatedWithAuthContext;
112
+ /**
113
+ * Event handler that triggers when a document is deleted in Firestore.
75
114
  *
76
115
  * @param documentOrOpts - Options or a string document path.
77
116
  * @param handler - Event handler which is run every time a Firestore delete occurs.
@@ -80,6 +119,16 @@ function onDocumentDeleted(documentOrOpts, handler) {
80
119
  return onOperation(exports.deletedEventType, documentOrOpts, handler);
81
120
  }
82
121
  exports.onDocumentDeleted = onDocumentDeleted;
122
+ /**
123
+ * Event handler that triggers when a document is deleted in Firestore.
124
+ *
125
+ * @param documentOrOpts - Options or a string document path.
126
+ * @param handler - Event handler which is run every time a Firestore delete occurs.
127
+ */
128
+ function onDocumentDeletedWithAuthContext(documentOrOpts, handler) {
129
+ return onOperation(exports.deletedEventWithAuthContextType, documentOrOpts, handler);
130
+ }
131
+ exports.onDocumentDeletedWithAuthContext = onDocumentDeletedWithAuthContext;
83
132
  /** @internal */
84
133
  function getOpts(documentOrOpts) {
85
134
  let document;
@@ -156,7 +205,7 @@ exports.makeParams = makeParams;
156
205
  /** @internal */
157
206
  function makeFirestoreEvent(eventType, event, params) {
158
207
  const data = event.data
159
- ? eventType === exports.createdEventType
208
+ ? eventType === exports.createdEventType || eventType === exports.createdEventWithAuthContextType
160
209
  ? createSnapshot(event)
161
210
  : createBeforeSnapshot(event)
162
211
  : undefined;
@@ -167,6 +216,16 @@ function makeFirestoreEvent(eventType, event, params) {
167
216
  };
168
217
  delete firestoreEvent.datacontenttype;
169
218
  delete firestoreEvent.dataschema;
219
+ if ("authtype" in event) {
220
+ const eventWithAuth = {
221
+ ...firestoreEvent,
222
+ authType: event.authtype,
223
+ authId: event.authid,
224
+ };
225
+ delete eventWithAuth.authtype;
226
+ delete eventWithAuth.authid;
227
+ return eventWithAuth;
228
+ }
170
229
  return firestoreEvent;
171
230
  }
172
231
  exports.makeFirestoreEvent = makeFirestoreEvent;
@@ -182,6 +241,16 @@ function makeChangedFirestoreEvent(event, params) {
182
241
  };
183
242
  delete firestoreEvent.datacontenttype;
184
243
  delete firestoreEvent.dataschema;
244
+ if ("authtype" in event) {
245
+ const eventWithAuth = {
246
+ ...firestoreEvent,
247
+ authType: event.authtype,
248
+ authId: event.authid,
249
+ };
250
+ delete eventWithAuth.authtype;
251
+ delete eventWithAuth.authid;
252
+ return eventWithAuth;
253
+ }
185
254
  return firestoreEvent;
186
255
  }
187
256
  exports.makeChangedFirestoreEvent = makeChangedFirestoreEvent;
@@ -54,7 +54,7 @@ function onRequest(optsOrHandler, handler) {
54
54
  // on the origin header of the request. If there is only one element in the
55
55
  // array, this is unnecessary.
56
56
  if (Array.isArray(origin) && origin.length === 1) {
57
- origin = origin[1];
57
+ origin = origin[0];
58
58
  }
59
59
  const middleware = cors({ origin });
60
60
  const userProvidedHandler = handler;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "firebase-functions",
3
- "version": "4.8.1",
3
+ "version": "4.9.0",
4
4
  "description": "Firebase SDK for Cloud Functions",
5
5
  "keywords": [
6
6
  "firebase",
@@ -201,7 +201,6 @@
201
201
  "@types/express": "4.17.3",
202
202
  "cors": "^2.8.5",
203
203
  "express": "^4.17.1",
204
- "node-fetch": "^2.6.7",
205
204
  "protobufjs": "^7.2.2"
206
205
  },
207
206
  "devDependencies": {