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.onOperation = exports.onChangedOperation = exports.makeEndpoint = exports.makeParams = exports.getOpts = exports.onValueDeleted = exports.onValueUpdated = exports.onValueCreated = exports.onValueWritten = exports.deletedEventType = exports.updatedEventType = exports.createdEventType = exports.writtenEventType = exports.DataSnapshot = void 0;
|
25 | const app_1 = require("../../common/app");
|
26 | const database_1 = require("../../common/providers/database");
|
27 | Object.defineProperty(exports, "DataSnapshot", { enumerable: true, get: function () { return database_1.DataSnapshot; } });
|
28 | const path_1 = require("../../common/utilities/path");
|
29 | const path_pattern_1 = require("../../common/utilities/path-pattern");
|
30 | const utils_1 = require("../../common/utilities/utils");
|
31 | const manifest_1 = require("../../runtime/manifest");
|
32 | const trace_1 = require("../trace");
|
33 | const options = require("../options");
|
34 | const onInit_1 = require("../../common/onInit");
|
35 |
|
36 | exports.writtenEventType = "google.firebase.database.ref.v1.written";
|
37 |
|
38 | exports.createdEventType = "google.firebase.database.ref.v1.created";
|
39 |
|
40 | exports.updatedEventType = "google.firebase.database.ref.v1.updated";
|
41 |
|
42 | exports.deletedEventType = "google.firebase.database.ref.v1.deleted";
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function onValueWritten(referenceOrOpts, handler) {
|
50 | return onChangedOperation(exports.writtenEventType, referenceOrOpts, handler);
|
51 | }
|
52 | exports.onValueWritten = onValueWritten;
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | function onValueCreated(referenceOrOpts, handler) {
|
60 | return onOperation(exports.createdEventType, referenceOrOpts, handler);
|
61 | }
|
62 | exports.onValueCreated = onValueCreated;
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | function onValueUpdated(referenceOrOpts, handler) {
|
70 | return onChangedOperation(exports.updatedEventType, referenceOrOpts, handler);
|
71 | }
|
72 | exports.onValueUpdated = onValueUpdated;
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | function onValueDeleted(referenceOrOpts, handler) {
|
80 |
|
81 | return onOperation(exports.deletedEventType, referenceOrOpts, handler);
|
82 | }
|
83 | exports.onValueDeleted = onValueDeleted;
|
84 |
|
85 | function getOpts(referenceOrOpts) {
|
86 | let path;
|
87 | let instance;
|
88 | let opts;
|
89 | if (typeof referenceOrOpts === "string") {
|
90 | path = (0, path_1.normalizePath)(referenceOrOpts);
|
91 | instance = "*";
|
92 | opts = {};
|
93 | }
|
94 | else {
|
95 | path = (0, path_1.normalizePath)(referenceOrOpts.ref);
|
96 | instance = referenceOrOpts.instance || "*";
|
97 | opts = { ...referenceOrOpts };
|
98 | delete opts.ref;
|
99 | delete opts.instance;
|
100 | }
|
101 | return {
|
102 | path,
|
103 | instance,
|
104 | opts,
|
105 | };
|
106 | }
|
107 | exports.getOpts = getOpts;
|
108 |
|
109 | function makeParams(event, path, instance) {
|
110 | return {
|
111 | ...path.extractMatches(event.ref),
|
112 | ...instance.extractMatches(event.instance),
|
113 | };
|
114 | }
|
115 | exports.makeParams = makeParams;
|
116 |
|
117 | function makeDatabaseEvent(event, data, instance, params) {
|
118 | const snapshot = new database_1.DataSnapshot(data, event.ref, (0, app_1.getApp)(), instance);
|
119 | const databaseEvent = {
|
120 | ...event,
|
121 | firebaseDatabaseHost: event.firebasedatabasehost,
|
122 | data: snapshot,
|
123 | params,
|
124 | };
|
125 | delete databaseEvent.firebasedatabasehost;
|
126 | return databaseEvent;
|
127 | }
|
128 |
|
129 | function makeChangedDatabaseEvent(event, instance, params) {
|
130 | const before = new database_1.DataSnapshot(event.data.data, event.ref, (0, app_1.getApp)(), instance);
|
131 | const after = new database_1.DataSnapshot((0, utils_1.applyChange)(event.data.data, event.data.delta), event.ref, (0, app_1.getApp)(), instance);
|
132 | const databaseEvent = {
|
133 | ...event,
|
134 | firebaseDatabaseHost: event.firebasedatabasehost,
|
135 | data: {
|
136 | before,
|
137 | after,
|
138 | },
|
139 | params,
|
140 | };
|
141 | delete databaseEvent.firebasedatabasehost;
|
142 | return databaseEvent;
|
143 | }
|
144 |
|
145 | function makeEndpoint(eventType, opts, path, instance) {
|
146 | var _a;
|
147 | const baseOpts = options.optionsToEndpoint(options.getGlobalOptions());
|
148 | const specificOpts = options.optionsToEndpoint(opts);
|
149 | const eventFilters = {};
|
150 | const eventFilterPathPatterns = {
|
151 |
|
152 | ref: path.getValue(),
|
153 | };
|
154 | instance.hasWildcards()
|
155 | ? (eventFilterPathPatterns.instance = instance.getValue())
|
156 | : (eventFilters.instance = instance.getValue());
|
157 | return {
|
158 | ...(0, manifest_1.initV2Endpoint)(options.getGlobalOptions(), opts),
|
159 | platform: "gcfv2",
|
160 | ...baseOpts,
|
161 | ...specificOpts,
|
162 | labels: {
|
163 | ...baseOpts === null || baseOpts === void 0 ? void 0 : baseOpts.labels,
|
164 | ...specificOpts === null || specificOpts === void 0 ? void 0 : specificOpts.labels,
|
165 | },
|
166 | eventTrigger: {
|
167 | eventType,
|
168 | eventFilters,
|
169 | eventFilterPathPatterns,
|
170 | retry: (_a = opts.retry) !== null && _a !== void 0 ? _a : false,
|
171 | },
|
172 | };
|
173 | }
|
174 | exports.makeEndpoint = makeEndpoint;
|
175 |
|
176 | function onChangedOperation(eventType, referenceOrOpts, handler) {
|
177 | const { path, instance, opts } = getOpts(referenceOrOpts);
|
178 | const pathPattern = new path_pattern_1.PathPattern(path);
|
179 | const instancePattern = new path_pattern_1.PathPattern(instance);
|
180 |
|
181 | const func = (raw) => {
|
182 | const event = raw;
|
183 | const instanceUrl = getInstance(event);
|
184 | const params = makeParams(event, pathPattern, instancePattern);
|
185 | const databaseEvent = makeChangedDatabaseEvent(event, instanceUrl, params);
|
186 |
|
187 |
|
188 | return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))(databaseEvent);
|
189 | };
|
190 | func.run = handler;
|
191 | func.__endpoint = makeEndpoint(eventType, opts, pathPattern, instancePattern);
|
192 | return func;
|
193 | }
|
194 | exports.onChangedOperation = onChangedOperation;
|
195 |
|
196 | function onOperation(eventType, referenceOrOpts, handler) {
|
197 | const { path, instance, opts } = getOpts(referenceOrOpts);
|
198 | const pathPattern = new path_pattern_1.PathPattern(path);
|
199 | const instancePattern = new path_pattern_1.PathPattern(instance);
|
200 |
|
201 | const func = (raw) => {
|
202 | const event = raw;
|
203 | const instanceUrl = getInstance(event);
|
204 | const params = makeParams(event, pathPattern, instancePattern);
|
205 | const data = eventType === exports.deletedEventType ? event.data.data : event.data.delta;
|
206 | const databaseEvent = makeDatabaseEvent(event, data, instanceUrl, params);
|
207 | return (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler))(databaseEvent);
|
208 | };
|
209 | func.run = handler;
|
210 | func.__endpoint = makeEndpoint(eventType, opts, pathPattern, instancePattern);
|
211 | return func;
|
212 | }
|
213 | exports.onOperation = onOperation;
|
214 | function getInstance(event) {
|
215 | const emuHost = process.env.FIREBASE_DATABASE_EMULATOR_HOST;
|
216 | return emuHost
|
217 | ? `http://${emuHost}/?ns=${event.instance}`
|
218 | : `https://${event.instance}.${event.firebasedatabasehost}`;
|
219 | }
|