1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
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.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 | 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 | Object.defineProperty(exports, "Change", { enumerable: true, get: function () { return core_1.Change; } });
|
31 | const options_1 = require("../options");
|
32 | const firestore_1 = require("../../common/providers/firestore");
|
33 | const trace_1 = require("../trace");
|
34 | const onInit_1 = require("../../common/onInit");
|
35 |
|
36 | exports.writtenEventType = "google.cloud.firestore.document.v1.written";
|
37 |
|
38 | exports.createdEventType = "google.cloud.firestore.document.v1.created";
|
39 |
|
40 | exports.updatedEventType = "google.cloud.firestore.document.v1.updated";
|
41 |
|
42 | exports.deletedEventType = "google.cloud.firestore.document.v1.deleted";
|
43 |
|
44 | exports.writtenEventWithAuthContextType = "google.cloud.firestore.document.v1.written.withAuthContext";
|
45 |
|
46 | exports.createdEventWithAuthContextType = "google.cloud.firestore.document.v1.created.withAuthContext";
|
47 |
|
48 | exports.updatedEventWithAuthContextType = "google.cloud.firestore.document.v1.updated.withAuthContext";
|
49 |
|
50 | exports.deletedEventWithAuthContextType = "google.cloud.firestore.document.v1.deleted.withAuthContext";
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | function onDocumentWritten(documentOrOpts, handler) {
|
58 | return onChangedOperation(exports.writtenEventType, documentOrOpts, handler);
|
59 | }
|
60 | exports.onDocumentWritten = onDocumentWritten;
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | function onDocumentWrittenWithAuthContext(documentOrOpts, handler) {
|
69 | return onChangedOperation(exports.writtenEventWithAuthContextType, documentOrOpts, handler);
|
70 | }
|
71 | exports.onDocumentWrittenWithAuthContext = onDocumentWrittenWithAuthContext;
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | function onDocumentCreated(documentOrOpts, handler) {
|
79 | return onOperation(exports.createdEventType, documentOrOpts, handler);
|
80 | }
|
81 | exports.onDocumentCreated = onDocumentCreated;
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | function onDocumentCreatedWithAuthContext(documentOrOpts, handler) {
|
89 | return onOperation(exports.createdEventWithAuthContextType, documentOrOpts, handler);
|
90 | }
|
91 | exports.onDocumentCreatedWithAuthContext = onDocumentCreatedWithAuthContext;
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | function onDocumentUpdated(documentOrOpts, handler) {
|
99 | return onChangedOperation(exports.updatedEventType, documentOrOpts, handler);
|
100 | }
|
101 | exports.onDocumentUpdated = onDocumentUpdated;
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | function onDocumentUpdatedWithAuthContext(documentOrOpts, handler) {
|
109 | return onChangedOperation(exports.updatedEventWithAuthContextType, documentOrOpts, handler);
|
110 | }
|
111 | exports.onDocumentUpdatedWithAuthContext = onDocumentUpdatedWithAuthContext;
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | function onDocumentDeleted(documentOrOpts, handler) {
|
119 | return onOperation(exports.deletedEventType, documentOrOpts, handler);
|
120 | }
|
121 | exports.onDocumentDeleted = onDocumentDeleted;
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | function onDocumentDeletedWithAuthContext(documentOrOpts, handler) {
|
129 | return onOperation(exports.deletedEventWithAuthContextType, documentOrOpts, handler);
|
130 | }
|
131 | exports.onDocumentDeletedWithAuthContext = onDocumentDeletedWithAuthContext;
|
132 |
|
133 | function getOpts(documentOrOpts) {
|
134 | let document;
|
135 | let database;
|
136 | let namespace;
|
137 | let opts;
|
138 | if (typeof documentOrOpts === "string") {
|
139 | document = (0, path_1.normalizePath)(documentOrOpts);
|
140 | database = "(default)";
|
141 | namespace = "(default)";
|
142 | opts = {};
|
143 | }
|
144 | else {
|
145 | document =
|
146 | typeof documentOrOpts.document === "string"
|
147 | ? (0, path_1.normalizePath)(documentOrOpts.document)
|
148 | : documentOrOpts.document;
|
149 | database = documentOrOpts.database || "(default)";
|
150 | namespace = documentOrOpts.namespace || "(default)";
|
151 | opts = { ...documentOrOpts };
|
152 | delete opts.document;
|
153 | delete opts.database;
|
154 | delete opts.namespace;
|
155 | }
|
156 | return {
|
157 | document,
|
158 | database,
|
159 | namespace,
|
160 | opts,
|
161 | };
|
162 | }
|
163 | exports.getOpts = getOpts;
|
164 |
|
165 | function getPath(event) {
|
166 | return `projects/${event.project}/databases/${event.database}/documents/${event.document}`;
|
167 | }
|
168 |
|
169 | function createSnapshot(event) {
|
170 | var _a, _b, _c, _d;
|
171 | if (((_a = event.datacontenttype) === null || _a === void 0 ? void 0 : _a.includes("application/protobuf")) || Buffer.isBuffer(event.data)) {
|
172 | return (0, firestore_1.createSnapshotFromProtobuf)(event.data, getPath(event), event.database);
|
173 | }
|
174 | else if ((_b = event.datacontenttype) === null || _b === void 0 ? void 0 : _b.includes("application/json")) {
|
175 | 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, event.database);
|
176 | }
|
177 | else {
|
178 | logger.error(`Cannot determine payload type, datacontenttype is ${event.datacontenttype}, failing out.`);
|
179 | throw Error("Error: Cannot parse event payload.");
|
180 | }
|
181 | }
|
182 | exports.createSnapshot = createSnapshot;
|
183 |
|
184 | function createBeforeSnapshot(event) {
|
185 | var _a, _b, _c, _d;
|
186 | if (((_a = event.datacontenttype) === null || _a === void 0 ? void 0 : _a.includes("application/protobuf")) || Buffer.isBuffer(event.data)) {
|
187 | return (0, firestore_1.createBeforeSnapshotFromProtobuf)(event.data, getPath(event), event.database);
|
188 | }
|
189 | else if ((_b = event.datacontenttype) === null || _b === void 0 ? void 0 : _b.includes("application/json")) {
|
190 | 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, event.database);
|
191 | }
|
192 | else {
|
193 | logger.error(`Cannot determine payload type, datacontenttype is ${event.datacontenttype}, failing out.`);
|
194 | throw Error("Error: Cannot parse event payload.");
|
195 | }
|
196 | }
|
197 | exports.createBeforeSnapshot = createBeforeSnapshot;
|
198 |
|
199 | function makeParams(document, documentPattern) {
|
200 | return {
|
201 | ...documentPattern.extractMatches(document),
|
202 | };
|
203 | }
|
204 | exports.makeParams = makeParams;
|
205 |
|
206 | function makeFirestoreEvent(eventType, event, params) {
|
207 | const data = event.data
|
208 | ? eventType === exports.createdEventType || eventType === exports.createdEventWithAuthContextType
|
209 | ? createSnapshot(event)
|
210 | : createBeforeSnapshot(event)
|
211 | : undefined;
|
212 | const firestoreEvent = {
|
213 | ...event,
|
214 | params,
|
215 | data,
|
216 | };
|
217 | delete firestoreEvent.datacontenttype;
|
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 | }
|
229 | return firestoreEvent;
|
230 | }
|
231 | exports.makeFirestoreEvent = makeFirestoreEvent;
|
232 |
|
233 | function makeChangedFirestoreEvent(event, params) {
|
234 | const data = event.data
|
235 | ? core_1.Change.fromObjects(createBeforeSnapshot(event), createSnapshot(event))
|
236 | : undefined;
|
237 | const firestoreEvent = {
|
238 | ...event,
|
239 | params,
|
240 | data,
|
241 | };
|
242 | delete firestoreEvent.datacontenttype;
|
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 | }
|
254 | return firestoreEvent;
|
255 | }
|
256 | exports.makeChangedFirestoreEvent = makeChangedFirestoreEvent;
|
257 |
|
258 | function makeEndpoint(eventType, opts, document, database, namespace) {
|
259 | var _a;
|
260 | const baseOpts = (0, options_1.optionsToEndpoint)((0, options_1.getGlobalOptions)());
|
261 | const specificOpts = (0, options_1.optionsToEndpoint)(opts);
|
262 | const eventFilters = {
|
263 | database,
|
264 | namespace,
|
265 | };
|
266 | const eventFilterPathPatterns = {};
|
267 | const maybePattern = typeof document === "string" ? new path_pattern_1.PathPattern(document).hasWildcards() : true;
|
268 | if (maybePattern) {
|
269 | eventFilterPathPatterns.document = document;
|
270 | }
|
271 | else {
|
272 | eventFilters.document = document;
|
273 | }
|
274 | return {
|
275 | ...(0, manifest_1.initV2Endpoint)((0, options_1.getGlobalOptions)(), opts),
|
276 | platform: "gcfv2",
|
277 | ...baseOpts,
|
278 | ...specificOpts,
|
279 | labels: {
|
280 | ...baseOpts === null || baseOpts === void 0 ? void 0 : baseOpts.labels,
|
281 | ...specificOpts === null || specificOpts === void 0 ? void 0 : specificOpts.labels,
|
282 | },
|
283 | eventTrigger: {
|
284 | eventType,
|
285 | eventFilters,
|
286 | eventFilterPathPatterns,
|
287 | retry: (_a = opts.retry) !== null && _a !== void 0 ? _a : false,
|
288 | },
|
289 | };
|
290 | }
|
291 | exports.makeEndpoint = makeEndpoint;
|
292 |
|
293 | function onOperation(eventType, documentOrOpts, handler) {
|
294 | const { document, database, namespace, opts } = getOpts(documentOrOpts);
|
295 |
|
296 | const func = (raw) => {
|
297 | const event = raw;
|
298 | const documentPattern = new path_pattern_1.PathPattern(typeof document === "string" ? document : document.value());
|
299 | const params = makeParams(event.document, documentPattern);
|
300 | const firestoreEvent = makeFirestoreEvent(eventType, event, params);
|
301 | return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))(firestoreEvent);
|
302 | };
|
303 | func.run = handler;
|
304 | func.__endpoint = makeEndpoint(eventType, opts, document, database, namespace);
|
305 | return func;
|
306 | }
|
307 | exports.onOperation = onOperation;
|
308 |
|
309 | function onChangedOperation(eventType, documentOrOpts, handler) {
|
310 | const { document, database, namespace, opts } = getOpts(documentOrOpts);
|
311 |
|
312 | const func = (raw) => {
|
313 | const event = raw;
|
314 | const documentPattern = new path_pattern_1.PathPattern(typeof document === "string" ? document : document.value());
|
315 | const params = makeParams(event.document, documentPattern);
|
316 | const firestoreEvent = makeChangedFirestoreEvent(event, params);
|
317 | return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))(firestoreEvent);
|
318 | };
|
319 | func.run = handler;
|
320 | func.__endpoint = makeEndpoint(eventType, opts, document, database, namespace);
|
321 | return func;
|
322 | }
|
323 | exports.onChangedOperation = onChangedOperation;
|