UNPKG

9.29 kBJavaScriptView Raw
1"use strict";
2// The MIT License (MIT)
3//
4// Copyright (c) 2022 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.
23Object.defineProperty(exports, "__esModule", { value: true });
24exports.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;
25const app_1 = require("../../common/app");
26const database_1 = require("../../common/providers/database");
27Object.defineProperty(exports, "DataSnapshot", { enumerable: true, get: function () { return database_1.DataSnapshot; } });
28const path_1 = require("../../common/utilities/path");
29const path_pattern_1 = require("../../common/utilities/path-pattern");
30const utils_1 = require("../../common/utilities/utils");
31const manifest_1 = require("../../runtime/manifest");
32const trace_1 = require("../trace");
33const options = require("../options");
34const onInit_1 = require("../../common/onInit");
35/** @internal */
36exports.writtenEventType = "google.firebase.database.ref.v1.written";
37/** @internal */
38exports.createdEventType = "google.firebase.database.ref.v1.created";
39/** @internal */
40exports.updatedEventType = "google.firebase.database.ref.v1.updated";
41/** @internal */
42exports.deletedEventType = "google.firebase.database.ref.v1.deleted";
43/**
44 * Event handler which triggers when data is created, updated, or deleted in Realtime Database.
45 *
46 * @param referenceOrOpts - Options or a string reference.
47 * @param handler - Event handler which is run every time a Realtime Database create, update, or delete occurs.
48 */
49function onValueWritten(referenceOrOpts, handler) {
50 return onChangedOperation(exports.writtenEventType, referenceOrOpts, handler);
51}
52exports.onValueWritten = onValueWritten;
53/**
54 * Event handler which triggers when data is created in Realtime Database.
55 *
56 * @param referenceOrOpts - Options or a string reference.
57 * @param handler - Event handler which is run every time a Realtime Database create occurs.
58 */
59function onValueCreated(referenceOrOpts, handler) {
60 return onOperation(exports.createdEventType, referenceOrOpts, handler);
61}
62exports.onValueCreated = onValueCreated;
63/**
64 * Event handler which triggers when data is updated in Realtime Database.
65 *
66 * @param referenceOrOpts - Options or a string reference.
67 * @param handler - Event handler which is run every time a Realtime Database update occurs.
68 */
69function onValueUpdated(referenceOrOpts, handler) {
70 return onChangedOperation(exports.updatedEventType, referenceOrOpts, handler);
71}
72exports.onValueUpdated = onValueUpdated;
73/**
74 * Event handler which triggers when data is deleted in Realtime Database.
75 *
76 * @param referenceOrOpts - Options or a string reference.
77 * @param handler - Event handler which is run every time a Realtime Database deletion occurs.
78 */
79function onValueDeleted(referenceOrOpts, handler) {
80 // TODO - need to use event.data.delta
81 return onOperation(exports.deletedEventType, referenceOrOpts, handler);
82}
83exports.onValueDeleted = onValueDeleted;
84/** @internal */
85function 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}
107exports.getOpts = getOpts;
108/** @internal */
109function makeParams(event, path, instance) {
110 return {
111 ...path.extractMatches(event.ref),
112 ...instance.extractMatches(event.instance),
113 };
114}
115exports.makeParams = makeParams;
116/** @hidden */
117function 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/** @hidden */
129function 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/** @internal */
145function 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 // Note: Eventarc always treats ref as a path pattern
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}
174exports.makeEndpoint = makeEndpoint;
175/** @internal */
176function 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 // wrap the handler
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 // Intentionally put init in the context of traces in case there is something
187 // expensive to observe.
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}
194exports.onChangedOperation = onChangedOperation;
195/** @internal */
196function 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 // wrap the handler
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}
213exports.onOperation = onOperation;
214function 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}