UNPKG

5.3 kBJavaScriptView Raw
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.
23Object.defineProperty(exports, "__esModule", { value: true });
24exports.createBeforeSnapshotFromJson = exports.createSnapshotFromJson = exports.createBeforeSnapshotFromProtobuf = exports.createSnapshotFromProtobuf = void 0;
25const firestore = require("firebase-admin/firestore");
26const logger = require("../../logger");
27const app_1 = require("../../common/app");
28const compiledFirestore_1 = require("../../../protos/compiledFirestore");
29const encoder_1 = require("../../common/utilities/encoder");
30/** static-complied protobufs */
31const DocumentEventData = compiledFirestore_1.google.events.cloud.firestore.v1.DocumentEventData;
32let firestoreInstance;
33/** @hidden */
34function _getValueProto(data, resource, valueFieldName) {
35 const value = data === null || data === void 0 ? void 0 : data[valueFieldName];
36 if (typeof value === "undefined" ||
37 value === null ||
38 (typeof value === "object" && !Object.keys(value).length)) {
39 // Firestore#snapshot_ takes resource string instead of proto for a non-existent snapshot
40 return resource;
41 }
42 const proto = {
43 fields: (value === null || value === void 0 ? void 0 : value.fields) || {},
44 createTime: (0, encoder_1.dateToTimestampProto)(value === null || value === void 0 ? void 0 : value.createTime),
45 updateTime: (0, encoder_1.dateToTimestampProto)(value === null || value === void 0 ? void 0 : value.updateTime),
46 name: (value === null || value === void 0 ? void 0 : value.name) || resource,
47 };
48 return proto;
49}
50/** @internal */
51function createSnapshotFromProtobuf(data, path, databaseId) {
52 if (!firestoreInstance) {
53 firestoreInstance = firestore.getFirestore((0, app_1.getApp)(), databaseId);
54 }
55 try {
56 const dataBuffer = Buffer.from(data);
57 const firestoreDecoded = DocumentEventData.decode(dataBuffer);
58 return firestoreInstance.snapshot_(firestoreDecoded.value || path, null, "protobufJS");
59 }
60 catch (err) {
61 logger.error("Failed to decode protobuf and create a snapshot.");
62 throw err;
63 }
64}
65exports.createSnapshotFromProtobuf = createSnapshotFromProtobuf;
66/** @internal */
67function createBeforeSnapshotFromProtobuf(data, path, databaseId) {
68 if (!firestoreInstance) {
69 firestoreInstance = firestore.getFirestore((0, app_1.getApp)(), databaseId);
70 }
71 try {
72 const dataBuffer = Buffer.from(data);
73 const firestoreDecoded = DocumentEventData.decode(dataBuffer);
74 return firestoreInstance.snapshot_(firestoreDecoded.oldValue || path, null, "protobufJS");
75 }
76 catch (err) {
77 logger.error("Failed to decode protobuf and create a before snapshot.");
78 throw err;
79 }
80}
81exports.createBeforeSnapshotFromProtobuf = createBeforeSnapshotFromProtobuf;
82/** @internal */
83function createSnapshotFromJson(data, source, createTime, updateTime, databaseId) {
84 if (!firestoreInstance) {
85 firestoreInstance = databaseId
86 ? firestore.getFirestore((0, app_1.getApp)(), databaseId)
87 : firestore.getFirestore((0, app_1.getApp)());
88 }
89 const valueProto = _getValueProto(data, source, "value");
90 let timeString = createTime || updateTime;
91 if (!timeString) {
92 logger.warn("Snapshot has no readTime. Using now()");
93 timeString = new Date().toISOString();
94 }
95 const readTime = (0, encoder_1.dateToTimestampProto)(timeString);
96 return firestoreInstance.snapshot_(valueProto, readTime, "json");
97}
98exports.createSnapshotFromJson = createSnapshotFromJson;
99/** @internal */
100function createBeforeSnapshotFromJson(data, source, createTime, updateTime, databaseId) {
101 if (!firestoreInstance) {
102 firestoreInstance = databaseId
103 ? firestore.getFirestore((0, app_1.getApp)(), databaseId)
104 : firestore.getFirestore((0, app_1.getApp)());
105 }
106 const oldValueProto = _getValueProto(data, source, "oldValue");
107 const oldReadTime = (0, encoder_1.dateToTimestampProto)(createTime || updateTime);
108 return firestoreInstance.snapshot_(oldValueProto, oldReadTime, "json");
109}
110exports.createBeforeSnapshotFromJson = createBeforeSnapshotFromJson;