1 | /*! firebase-admin v10.0.0 */
|
2 | ;
|
3 | /*!
|
4 | * @license
|
5 | * Copyright 2017 Google Inc.
|
6 | *
|
7 | * Licensed under the Apache License, Version 2.0 (the "License");
|
8 | * you may not use this file except in compliance with the License.
|
9 | * You may obtain a copy of the License at
|
10 | *
|
11 | * http://www.apache.org/licenses/LICENSE-2.0
|
12 | *
|
13 | * Unless required by applicable law or agreed to in writing, software
|
14 | * distributed under the License is distributed on an "AS IS" BASIS,
|
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16 | * See the License for the specific language governing permissions and
|
17 | * limitations under the License.
|
18 | */
|
19 | Object.defineProperty(exports, "__esModule", { value: true });
|
20 | exports.transformMillisecondsToSecondsString = exports.generateUpdateMask = exports.formatString = exports.toWebSafeBase64 = exports.findProjectId = exports.getExplicitProjectId = exports.addReadonlyGetter = exports.renameProperties = exports.getSdkVersion = void 0;
|
21 | var credential_internal_1 = require("../app/credential-internal");
|
22 | var validator = require("./validator");
|
23 | var sdkVersion;
|
24 | // TODO: Move to firebase-admin/app as an internal member.
|
25 | function getSdkVersion() {
|
26 | if (!sdkVersion) {
|
27 | var version = require('../../package.json').version; // eslint-disable-line @typescript-eslint/no-var-requires
|
28 | sdkVersion = version;
|
29 | }
|
30 | return sdkVersion;
|
31 | }
|
32 | exports.getSdkVersion = getSdkVersion;
|
33 | /**
|
34 | * Renames properties on an object given a mapping from old to new property names.
|
35 | *
|
36 | * For example, this can be used to map underscore_cased properties to camelCase.
|
37 | *
|
38 | * @param obj - The object whose properties to rename.
|
39 | * @param keyMap - The mapping from old to new property names.
|
40 | */
|
41 | function renameProperties(obj, keyMap) {
|
42 | Object.keys(keyMap).forEach(function (oldKey) {
|
43 | if (oldKey in obj) {
|
44 | var newKey = keyMap[oldKey];
|
45 | // The old key's value takes precedence over the new key's value.
|
46 | obj[newKey] = obj[oldKey];
|
47 | delete obj[oldKey];
|
48 | }
|
49 | });
|
50 | }
|
51 | exports.renameProperties = renameProperties;
|
52 | /**
|
53 | * Defines a new read-only property directly on an object and returns the object.
|
54 | *
|
55 | * @param obj - The object on which to define the property.
|
56 | * @param prop - The name of the property to be defined or modified.
|
57 | * @param value - The value associated with the property.
|
58 | */
|
59 | function addReadonlyGetter(obj, prop, value) {
|
60 | Object.defineProperty(obj, prop, {
|
61 | value: value,
|
62 | // Make this property read-only.
|
63 | writable: false,
|
64 | // Include this property during enumeration of obj's properties.
|
65 | enumerable: true,
|
66 | });
|
67 | }
|
68 | exports.addReadonlyGetter = addReadonlyGetter;
|
69 | /**
|
70 | * Returns the Google Cloud project ID associated with a Firebase app, if it's explicitly
|
71 | * specified in either the Firebase app options, credentials or the local environment.
|
72 | * Otherwise returns null.
|
73 | *
|
74 | * @param app - A Firebase app to get the project ID from.
|
75 | *
|
76 | * @returns A project ID string or null.
|
77 | */
|
78 | function getExplicitProjectId(app) {
|
79 | var options = app.options;
|
80 | if (validator.isNonEmptyString(options.projectId)) {
|
81 | return options.projectId;
|
82 | }
|
83 | var credential = app.options.credential;
|
84 | if (credential instanceof credential_internal_1.ServiceAccountCredential) {
|
85 | return credential.projectId;
|
86 | }
|
87 | var projectId = process.env.GOOGLE_CLOUD_PROJECT || process.env.GCLOUD_PROJECT;
|
88 | if (validator.isNonEmptyString(projectId)) {
|
89 | return projectId;
|
90 | }
|
91 | return null;
|
92 | }
|
93 | exports.getExplicitProjectId = getExplicitProjectId;
|
94 | /**
|
95 | * Determines the Google Cloud project ID associated with a Firebase app. This method
|
96 | * first checks if a project ID is explicitly specified in either the Firebase app options,
|
97 | * credentials or the local environment in that order. If no explicit project ID is
|
98 | * configured, but the SDK has been initialized with ComputeEngineCredentials, this
|
99 | * method attempts to discover the project ID from the local metadata service.
|
100 | *
|
101 | * @param app - A Firebase app to get the project ID from.
|
102 | *
|
103 | * @returns A project ID string or null.
|
104 | */
|
105 | function findProjectId(app) {
|
106 | var projectId = getExplicitProjectId(app);
|
107 | if (projectId) {
|
108 | return Promise.resolve(projectId);
|
109 | }
|
110 | var credential = app.options.credential;
|
111 | if (credential instanceof credential_internal_1.ComputeEngineCredential) {
|
112 | return credential.getProjectId();
|
113 | }
|
114 | return Promise.resolve(null);
|
115 | }
|
116 | exports.findProjectId = findProjectId;
|
117 | /**
|
118 | * Encodes data using web-safe-base64.
|
119 | *
|
120 | * @param data - The raw data byte input.
|
121 | * @returns The base64-encoded result.
|
122 | */
|
123 | function toWebSafeBase64(data) {
|
124 | return data.toString('base64').replace(/\//g, '_').replace(/\+/g, '-');
|
125 | }
|
126 | exports.toWebSafeBase64 = toWebSafeBase64;
|
127 | /**
|
128 | * Formats a string of form 'project/{projectId}/{api}' and replaces
|
129 | * with corresponding arguments {projectId: '1234', api: 'resource'}
|
130 | * and returns output: 'project/1234/resource'.
|
131 | *
|
132 | * @param str - The original string where the param need to be
|
133 | * replaced.
|
134 | * @param params - The optional parameters to replace in the
|
135 | * string.
|
136 | * @returns The resulting formatted string.
|
137 | */
|
138 | function formatString(str, params) {
|
139 | var formatted = str;
|
140 | Object.keys(params || {}).forEach(function (key) {
|
141 | formatted = formatted.replace(new RegExp('{' + key + '}', 'g'), params[key]);
|
142 | });
|
143 | return formatted;
|
144 | }
|
145 | exports.formatString = formatString;
|
146 | /**
|
147 | * Generates the update mask for the provided object.
|
148 | * Note this will ignore the last key with value undefined.
|
149 | *
|
150 | * @param obj - The object to generate the update mask for.
|
151 | * @param terminalPaths - The optional map of keys for maximum paths to traverse.
|
152 | * Nested objects beyond that path will be ignored. This is useful for
|
153 | * keys with variable object values.
|
154 | * @param root - The path so far.
|
155 | * @returns The computed update mask list.
|
156 | */
|
157 | function generateUpdateMask(obj, terminalPaths, root) {
|
158 | if (terminalPaths === void 0) { terminalPaths = []; }
|
159 | if (root === void 0) { root = ''; }
|
160 | var updateMask = [];
|
161 | if (!validator.isNonNullObject(obj)) {
|
162 | return updateMask;
|
163 | }
|
164 | var _loop_1 = function (key) {
|
165 | if (typeof obj[key] !== 'undefined') {
|
166 | var nextPath = root ? root + "." + key : key;
|
167 | // We hit maximum path.
|
168 | // Consider switching to Set<string> if the list grows too large.
|
169 | if (terminalPaths.indexOf(nextPath) !== -1) {
|
170 | // Add key and stop traversing this branch.
|
171 | updateMask.push(key);
|
172 | }
|
173 | else {
|
174 | var maskList = generateUpdateMask(obj[key], terminalPaths, nextPath);
|
175 | if (maskList.length > 0) {
|
176 | maskList.forEach(function (mask) {
|
177 | updateMask.push(key + "." + mask);
|
178 | });
|
179 | }
|
180 | else {
|
181 | updateMask.push(key);
|
182 | }
|
183 | }
|
184 | }
|
185 | };
|
186 | for (var key in obj) {
|
187 | _loop_1(key);
|
188 | }
|
189 | return updateMask;
|
190 | }
|
191 | exports.generateUpdateMask = generateUpdateMask;
|
192 | /**
|
193 | * Transforms milliseconds to a protobuf Duration type string.
|
194 | * Returns the duration in seconds with up to nine fractional
|
195 | * digits, terminated by 's'. Example: "3 seconds 0 nano seconds as 3s,
|
196 | * 3 seconds 1 nano seconds as 3.000000001s".
|
197 | *
|
198 | * @param milliseconds - The duration in milliseconds.
|
199 | * @returns The resulting formatted string in seconds with up to nine fractional
|
200 | * digits, terminated by 's'.
|
201 | */
|
202 | function transformMillisecondsToSecondsString(milliseconds) {
|
203 | var duration;
|
204 | var seconds = Math.floor(milliseconds / 1000);
|
205 | var nanos = Math.floor((milliseconds - seconds * 1000) * 1000000);
|
206 | if (nanos > 0) {
|
207 | var nanoString = nanos.toString();
|
208 | while (nanoString.length < 9) {
|
209 | nanoString = '0' + nanoString;
|
210 | }
|
211 | duration = seconds + "." + nanoString + "s";
|
212 | }
|
213 | else {
|
214 | duration = seconds + "s";
|
215 | }
|
216 | return duration;
|
217 | }
|
218 | exports.transformMillisecondsToSecondsString = transformMillisecondsToSecondsString;
|