forked from firebase/firebase-admin-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
303 lines (280 loc) · 9.9 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*!
* @license
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { App } from '../app/index';
import {
ServiceAccountCredential, ComputeEngineCredential
} from '../app/credential-internal';
import * as validator from './validator';
let sdkVersion: string;
// TODO: Move to firebase-admin/app as an internal member.
export function getSdkVersion(): string {
if (!sdkVersion) {
const { version } = require('../../package.json'); // eslint-disable-line @typescript-eslint/no-var-requires
sdkVersion = version;
}
return sdkVersion;
}
/**
* Renames properties on an object given a mapping from old to new property names.
*
* For example, this can be used to map underscore_cased properties to camelCase.
*
* @param obj - The object whose properties to rename.
* @param keyMap - The mapping from old to new property names.
*/
export function renameProperties(obj: {[key: string]: any}, keyMap: { [key: string]: string }): void {
Object.keys(keyMap).forEach((oldKey) => {
if (oldKey in obj) {
const newKey = keyMap[oldKey];
// The old key's value takes precedence over the new key's value.
obj[newKey] = obj[oldKey];
delete obj[oldKey];
}
});
}
/**
* Defines a new read-only property directly on an object and returns the object.
*
* @param obj - The object on which to define the property.
* @param prop - The name of the property to be defined or modified.
* @param value - The value associated with the property.
*/
export function addReadonlyGetter(obj: object, prop: string, value: any): void {
Object.defineProperty(obj, prop, {
value,
// Make this property read-only.
writable: false,
// Include this property during enumeration of obj's properties.
enumerable: true,
});
}
/**
* Returns the Google Cloud project ID associated with a Firebase app, if it's explicitly
* specified in either the Firebase app options, credentials or the local environment.
* Otherwise returns null.
*
* @param app - A Firebase app to get the project ID from.
*
* @returns A project ID string or null.
*/
export function getExplicitProjectId(app: App): string | null {
const options = app.options;
if (validator.isNonEmptyString(options.projectId)) {
return options.projectId;
}
const credential = app.options.credential;
if (credential instanceof ServiceAccountCredential) {
return credential.projectId;
}
const projectId = process.env.GOOGLE_CLOUD_PROJECT || process.env.GCLOUD_PROJECT;
if (validator.isNonEmptyString(projectId)) {
return projectId;
}
return null;
}
/**
* Determines the Google Cloud project ID associated with a Firebase app. This method
* first checks if a project ID is explicitly specified in either the Firebase app options,
* credentials or the local environment in that order. If no explicit project ID is
* configured, but the SDK has been initialized with ComputeEngineCredentials, this
* method attempts to discover the project ID from the local metadata service.
*
* @param app - A Firebase app to get the project ID from.
*
* @returns A project ID string or null.
*/
export function findProjectId(app: App): Promise<string | null> {
const projectId = getExplicitProjectId(app);
if (projectId) {
return Promise.resolve(projectId);
}
const credential = app.options.credential;
if (credential instanceof ComputeEngineCredential) {
return credential.getProjectId();
}
return Promise.resolve(null);
}
/**
* Returns the service account email associated with a Firebase app, if it's explicitly
* specified in either the Firebase app options, credentials or the local environment.
* Otherwise returns null.
*
* @param app - A Firebase app to get the service account email from.
*
* @returns A service account email string or null.
*/
export function getExplicitServiceAccountEmail(app: App): string | null {
const options = app.options;
if (validator.isNonEmptyString(options.serviceAccountId)) {
return options.serviceAccountId;
}
const credential = app.options.credential;
if (credential instanceof ServiceAccountCredential) {
return credential.clientEmail;
}
return null;
}
/**
* Determines the service account email associated with a Firebase app. This method first
* checks if a service account email is explicitly specified in either the Firebase app options,
* credentials or the local environment in that order. If no explicit service account email is
* configured, but the SDK has been initialized with ComputeEngineCredentials, this
* method attempts to discover the service account email from the local metadata service.
*
* @param app - A Firebase app to get the service account email from.
*
* @returns A service account email ID string or null.
*/
export function findServiceAccountEmail(app: App): Promise<string | null> {
const accountId = getExplicitServiceAccountEmail(app);
if (accountId) {
return Promise.resolve(accountId);
}
const credential = app.options.credential;
if (credential instanceof ComputeEngineCredential) {
return credential.getServiceAccountEmail();
}
return Promise.resolve(null);
}
/**
* Encodes data using web-safe-base64.
*
* @param data - The raw data byte input.
* @returns The base64-encoded result.
*/
export function toWebSafeBase64(data: Buffer): string {
return data.toString('base64').replace(/\//g, '_').replace(/\+/g, '-');
}
/**
* Formats a string of form 'project/{projectId}/{api}' and replaces
* with corresponding arguments {projectId: '1234', api: 'resource'}
* and returns output: 'project/1234/resource'.
*
* @param str - The original string where the param need to be
* replaced.
* @param params - The optional parameters to replace in the
* string.
* @returns The resulting formatted string.
*/
export function formatString(str: string, params?: object): string {
let formatted = str;
Object.keys(params || {}).forEach((key) => {
formatted = formatted.replace(
new RegExp('{' + key + '}', 'g'),
(params as {[key: string]: string})[key]);
});
return formatted;
}
/**
* Generates the update mask for the provided object.
* Note this will ignore the last key with value undefined.
*
* @param obj - The object to generate the update mask for.
* @param terminalPaths - The optional map of keys for maximum paths to traverse.
* Nested objects beyond that path will be ignored. This is useful for
* keys with variable object values.
* @param root - The path so far.
* @returns The computed update mask list.
*/
export function generateUpdateMask(
obj: any, terminalPaths: string[] = [], root = ''
): string[] {
const updateMask: string[] = [];
if (!validator.isNonNullObject(obj)) {
return updateMask;
}
for (const key in obj) {
if (typeof obj[key] !== 'undefined') {
const nextPath = root ? `${root}.${key}` : key;
// We hit maximum path.
// Consider switching to Set<string> if the list grows too large.
if (terminalPaths.indexOf(nextPath) !== -1) {
// Add key and stop traversing this branch.
updateMask.push(key);
} else {
const maskList = generateUpdateMask(obj[key], terminalPaths, nextPath);
if (maskList.length > 0) {
maskList.forEach((mask) => {
updateMask.push(`${key}.${mask}`);
});
} else {
updateMask.push(key);
}
}
}
}
return updateMask;
}
/**
* Transforms milliseconds to a protobuf Duration type string.
* Returns the duration in seconds with up to nine fractional
* digits, terminated by 's'. Example: "3 seconds 0 nano seconds as 3s,
* 3 seconds 1 nano seconds as 3.000000001s".
*
* @param milliseconds - The duration in milliseconds.
* @returns The resulting formatted string in seconds with up to nine fractional
* digits, terminated by 's'.
*/
export function transformMillisecondsToSecondsString(milliseconds: number): string {
let duration: string;
const seconds = Math.floor(milliseconds / 1000);
const nanos = Math.floor((milliseconds - seconds * 1000) * 1000000);
if (nanos > 0) {
let nanoString = nanos.toString();
while (nanoString.length < 9) {
nanoString = '0' + nanoString;
}
duration = `${seconds}.${nanoString}s`;
} else {
duration = `${seconds}s`;
}
return duration;
}
/**
* Internal type to represent a resource name
*/
export type ParsedResource = {
projectId?: string;
locationId?: string;
resourceId: string;
}
/**
* Parses the top level resources of a given resource name.
* Supports both full and partial resources names, example:
* `locations/{location}/functions/{functionName}`,
* `projects/{project}/locations/{location}/functions/{functionName}`, or {functionName}
* Does not support deeply nested resource names.
*
* @param resourceName - The resource name string.
* @param resourceIdKey - The key of the resource name to be parsed.
* @returns A parsed resource name object.
*/
export function parseResourceName(resourceName: string, resourceIdKey: string): ParsedResource {
if (!resourceName.includes('/')) {
return { resourceId: resourceName };
}
const CHANNEL_NAME_REGEX =
new RegExp(`^(projects/([^/]+)/)?locations/([^/]+)/${resourceIdKey}/([^/]+)$`);
const match = CHANNEL_NAME_REGEX.exec(resourceName);
if (match === null) {
throw new Error('Invalid resource name format.');
}
const projectId = match[2];
const locationId = match[3];
const resourceId = match[4];
return { projectId, locationId, resourceId };
}