firebase-admin 9.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +90 -0
  3. package/lib/app-check/app-check-api-client-internal.js +197 -0
  4. package/lib/app-check/app-check.js +79 -0
  5. package/lib/app-check/index.d.ts +160 -0
  6. package/lib/app-check/index.js +19 -0
  7. package/lib/app-check/token-generator.js +161 -0
  8. package/lib/app-check/token-verifier.js +152 -0
  9. package/lib/auth/action-code-settings-builder.js +118 -0
  10. package/lib/auth/auth-api-request.js +1856 -0
  11. package/lib/auth/auth-config.js +636 -0
  12. package/lib/auth/auth.js +836 -0
  13. package/lib/auth/identifier.js +40 -0
  14. package/lib/auth/index.d.ts +1927 -0
  15. package/lib/auth/index.js +18 -0
  16. package/lib/auth/tenant-manager.js +140 -0
  17. package/lib/auth/tenant.js +171 -0
  18. package/lib/auth/token-generator.js +200 -0
  19. package/lib/auth/token-verifier.js +259 -0
  20. package/lib/auth/user-import-builder.js +387 -0
  21. package/lib/auth/user-record.js +346 -0
  22. package/lib/credential/credential-internal.js +391 -0
  23. package/lib/credential/credential.js +44 -0
  24. package/lib/credential/index.d.ts +169 -0
  25. package/lib/credential/index.js +23 -0
  26. package/lib/database/database-internal.js +266 -0
  27. package/lib/database/index.d.ts +89 -0
  28. package/lib/database/index.js +31 -0
  29. package/lib/default-namespace.js +31 -0
  30. package/lib/firebase-app.js +349 -0
  31. package/lib/firebase-namespace-api.d.ts +243 -0
  32. package/lib/firebase-namespace-api.js +18 -0
  33. package/lib/firebase-namespace.d.ts +31 -0
  34. package/lib/firebase-namespace.js +417 -0
  35. package/lib/firestore/firestore-internal.js +105 -0
  36. package/lib/firestore/index.d.ts +50 -0
  37. package/lib/firestore/index.js +47 -0
  38. package/lib/index.d.ts +24 -0
  39. package/lib/index.js +27 -0
  40. package/lib/installations/index.d.ts +81 -0
  41. package/lib/installations/index.js +18 -0
  42. package/lib/installations/installations-request-handler.js +117 -0
  43. package/lib/installations/installations.js +62 -0
  44. package/lib/instance-id/index.d.ts +83 -0
  45. package/lib/instance-id/index.js +18 -0
  46. package/lib/instance-id/instance-id.js +87 -0
  47. package/lib/machine-learning/index.d.ts +249 -0
  48. package/lib/machine-learning/index.js +18 -0
  49. package/lib/machine-learning/machine-learning-api-client.js +304 -0
  50. package/lib/machine-learning/machine-learning-utils.js +62 -0
  51. package/lib/machine-learning/machine-learning.js +364 -0
  52. package/lib/messaging/batch-request-internal.js +129 -0
  53. package/lib/messaging/index.d.ts +1174 -0
  54. package/lib/messaging/index.js +18 -0
  55. package/lib/messaging/messaging-api-request-internal.js +128 -0
  56. package/lib/messaging/messaging-errors-internal.js +106 -0
  57. package/lib/messaging/messaging-internal.js +484 -0
  58. package/lib/messaging/messaging.js +846 -0
  59. package/lib/project-management/android-app.js +176 -0
  60. package/lib/project-management/index.d.ts +363 -0
  61. package/lib/project-management/index.js +41 -0
  62. package/lib/project-management/ios-app.js +88 -0
  63. package/lib/project-management/project-management-api-request-internal.js +273 -0
  64. package/lib/project-management/project-management.js +254 -0
  65. package/lib/remote-config/index.d.ts +369 -0
  66. package/lib/remote-config/index.js +18 -0
  67. package/lib/remote-config/remote-config-api-client-internal.js +407 -0
  68. package/lib/remote-config/remote-config.js +304 -0
  69. package/lib/security-rules/index.d.ts +216 -0
  70. package/lib/security-rules/index.js +18 -0
  71. package/lib/security-rules/security-rules-api-client-internal.js +237 -0
  72. package/lib/security-rules/security-rules-internal.js +41 -0
  73. package/lib/security-rules/security-rules.js +310 -0
  74. package/lib/storage/index.d.ts +60 -0
  75. package/lib/storage/index.js +18 -0
  76. package/lib/storage/storage.js +123 -0
  77. package/lib/utils/api-request.js +845 -0
  78. package/lib/utils/crypto-signer.js +237 -0
  79. package/lib/utils/deep-copy.js +78 -0
  80. package/lib/utils/error.js +1063 -0
  81. package/lib/utils/index.js +217 -0
  82. package/lib/utils/jwt.js +355 -0
  83. package/lib/utils/validator.js +271 -0
  84. package/package.json +122 -0
@@ -0,0 +1,266 @@
1
+ /*! firebase-admin v9.12.0 */
2
+ "use strict";
3
+ /*!
4
+ * Copyright 2020 Google Inc.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.DatabaseService = void 0;
20
+ var url_1 = require("url");
21
+ var path = require("path");
22
+ var error_1 = require("../utils/error");
23
+ var validator = require("../utils/validator");
24
+ var api_request_1 = require("../utils/api-request");
25
+ var index_1 = require("../utils/index");
26
+ var TOKEN_REFRESH_THRESHOLD_MILLIS = 5 * 60 * 1000;
27
+ var DatabaseService = /** @class */ (function () {
28
+ function DatabaseService(app) {
29
+ this.databases = {};
30
+ if (!validator.isNonNullObject(app) || !('options' in app)) {
31
+ throw new error_1.FirebaseDatabaseError({
32
+ code: 'invalid-argument',
33
+ message: 'First argument passed to admin.database() must be a valid Firebase app instance.',
34
+ });
35
+ }
36
+ this.appInternal = app;
37
+ }
38
+ /**
39
+ * @internal
40
+ */
41
+ DatabaseService.prototype.delete = function () {
42
+ var _this = this;
43
+ if (this.tokenListener) {
44
+ this.appInternal.INTERNAL.removeAuthTokenListener(this.tokenListener);
45
+ clearTimeout(this.tokenRefreshTimeout);
46
+ }
47
+ var promises = [];
48
+ for (var _i = 0, _a = Object.keys(this.databases); _i < _a.length; _i++) {
49
+ var dbUrl = _a[_i];
50
+ var db = this.databases[dbUrl];
51
+ promises.push(db.INTERNAL.delete());
52
+ }
53
+ return Promise.all(promises).then(function () {
54
+ _this.databases = {};
55
+ });
56
+ };
57
+ Object.defineProperty(DatabaseService.prototype, "app", {
58
+ /**
59
+ * Returns the app associated with this DatabaseService instance.
60
+ *
61
+ * @return The app associated with this DatabaseService instance.
62
+ */
63
+ get: function () {
64
+ return this.appInternal;
65
+ },
66
+ enumerable: false,
67
+ configurable: true
68
+ });
69
+ DatabaseService.prototype.getDatabase = function (url) {
70
+ var dbUrl = this.ensureUrl(url);
71
+ if (!validator.isNonEmptyString(dbUrl)) {
72
+ throw new error_1.FirebaseDatabaseError({
73
+ code: 'invalid-argument',
74
+ message: 'Database URL must be a valid, non-empty URL string.',
75
+ });
76
+ }
77
+ var db = this.databases[dbUrl];
78
+ if (typeof db === 'undefined') {
79
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
80
+ var rtdb = require('@firebase/database-compat/standalone');
81
+ db = rtdb.initStandalone(this.appInternal, dbUrl, index_1.getSdkVersion()).instance;
82
+ var rulesClient_1 = new DatabaseRulesClient(this.app, dbUrl);
83
+ db.getRules = function () {
84
+ return rulesClient_1.getRules();
85
+ };
86
+ db.getRulesJSON = function () {
87
+ return rulesClient_1.getRulesJSON();
88
+ };
89
+ db.setRules = function (source) {
90
+ return rulesClient_1.setRules(source);
91
+ };
92
+ this.databases[dbUrl] = db;
93
+ }
94
+ if (!this.tokenListener) {
95
+ this.tokenListener = this.onTokenChange.bind(this);
96
+ this.appInternal.INTERNAL.addAuthTokenListener(this.tokenListener);
97
+ }
98
+ return db;
99
+ };
100
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
101
+ DatabaseService.prototype.onTokenChange = function (_) {
102
+ var token = this.appInternal.INTERNAL.getCachedToken();
103
+ if (token) {
104
+ var delayMillis = token.expirationTime - TOKEN_REFRESH_THRESHOLD_MILLIS - Date.now();
105
+ // If the new token is set to expire soon (unlikely), do nothing. Somebody will eventually
106
+ // notice and refresh the token, at which point this callback will fire again.
107
+ if (delayMillis > 0) {
108
+ this.scheduleTokenRefresh(delayMillis);
109
+ }
110
+ }
111
+ };
112
+ DatabaseService.prototype.scheduleTokenRefresh = function (delayMillis) {
113
+ var _this = this;
114
+ clearTimeout(this.tokenRefreshTimeout);
115
+ this.tokenRefreshTimeout = setTimeout(function () {
116
+ _this.appInternal.INTERNAL.getToken(/*forceRefresh=*/ true)
117
+ .catch(function () {
118
+ // Ignore the error since this might just be an intermittent failure. If we really cannot
119
+ // refresh the token, an error will be logged once the existing token expires and we try
120
+ // to fetch a fresh one.
121
+ });
122
+ }, delayMillis);
123
+ };
124
+ DatabaseService.prototype.ensureUrl = function (url) {
125
+ if (typeof url !== 'undefined') {
126
+ return url;
127
+ }
128
+ else if (typeof this.appInternal.options.databaseURL !== 'undefined') {
129
+ return this.appInternal.options.databaseURL;
130
+ }
131
+ throw new error_1.FirebaseDatabaseError({
132
+ code: 'invalid-argument',
133
+ message: 'Can\'t determine Firebase Database URL.',
134
+ });
135
+ };
136
+ return DatabaseService;
137
+ }());
138
+ exports.DatabaseService = DatabaseService;
139
+ var RULES_URL_PATH = '.settings/rules.json';
140
+ /**
141
+ * A helper client for managing RTDB security rules.
142
+ */
143
+ var DatabaseRulesClient = /** @class */ (function () {
144
+ function DatabaseRulesClient(app, dbUrl) {
145
+ var parsedUrl = new url_1.URL(dbUrl);
146
+ var emulatorHost = process.env.FIREBASE_DATABASE_EMULATOR_HOST;
147
+ if (emulatorHost) {
148
+ var namespace = extractNamespace(parsedUrl);
149
+ parsedUrl = new url_1.URL("http://" + emulatorHost + "?ns=" + namespace);
150
+ }
151
+ parsedUrl.pathname = path.join(parsedUrl.pathname, RULES_URL_PATH);
152
+ this.dbUrl = parsedUrl.toString();
153
+ this.httpClient = new api_request_1.AuthorizedHttpClient(app);
154
+ }
155
+ /**
156
+ * Gets the currently applied security rules as a string. The return value consists of
157
+ * the rules source including comments.
158
+ *
159
+ * @return A promise fulfilled with the rules as a raw string.
160
+ */
161
+ DatabaseRulesClient.prototype.getRules = function () {
162
+ var _this = this;
163
+ var req = {
164
+ method: 'GET',
165
+ url: this.dbUrl,
166
+ };
167
+ return this.httpClient.send(req)
168
+ .then(function (resp) {
169
+ if (!resp.text) {
170
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INTERNAL_ERROR, 'HTTP response missing data.');
171
+ }
172
+ return resp.text;
173
+ })
174
+ .catch(function (err) {
175
+ throw _this.handleError(err);
176
+ });
177
+ };
178
+ /**
179
+ * Gets the currently applied security rules as a parsed JSON object. Any comments in
180
+ * the original source are stripped away.
181
+ *
182
+ * @return {Promise<object>} A promise fulfilled with the parsed rules source.
183
+ */
184
+ DatabaseRulesClient.prototype.getRulesJSON = function () {
185
+ var _this = this;
186
+ var req = {
187
+ method: 'GET',
188
+ url: this.dbUrl,
189
+ data: { format: 'strict' },
190
+ };
191
+ return this.httpClient.send(req)
192
+ .then(function (resp) {
193
+ return resp.data;
194
+ })
195
+ .catch(function (err) {
196
+ throw _this.handleError(err);
197
+ });
198
+ };
199
+ /**
200
+ * Sets the specified rules on the Firebase Database instance. If the rules source is
201
+ * specified as a string or a Buffer, it may include comments.
202
+ *
203
+ * @param {string|Buffer|object} source Source of the rules to apply. Must not be `null`
204
+ * or empty.
205
+ * @return {Promise<void>} Resolves when the rules are set on the Database.
206
+ */
207
+ DatabaseRulesClient.prototype.setRules = function (source) {
208
+ var _this = this;
209
+ if (!validator.isNonEmptyString(source) &&
210
+ !validator.isBuffer(source) &&
211
+ !validator.isNonNullObject(source)) {
212
+ var error = new error_1.FirebaseDatabaseError({
213
+ code: 'invalid-argument',
214
+ message: 'Source must be a non-empty string, Buffer or an object.',
215
+ });
216
+ return Promise.reject(error);
217
+ }
218
+ var req = {
219
+ method: 'PUT',
220
+ url: this.dbUrl,
221
+ data: source,
222
+ headers: {
223
+ 'content-type': 'application/json; charset=utf-8',
224
+ },
225
+ };
226
+ return this.httpClient.send(req)
227
+ .then(function () {
228
+ return;
229
+ })
230
+ .catch(function (err) {
231
+ throw _this.handleError(err);
232
+ });
233
+ };
234
+ DatabaseRulesClient.prototype.handleError = function (err) {
235
+ if (err instanceof api_request_1.HttpError) {
236
+ return new error_1.FirebaseDatabaseError({
237
+ code: error_1.AppErrorCodes.INTERNAL_ERROR,
238
+ message: this.getErrorMessage(err),
239
+ });
240
+ }
241
+ return err;
242
+ };
243
+ DatabaseRulesClient.prototype.getErrorMessage = function (err) {
244
+ var intro = 'Error while accessing security rules';
245
+ try {
246
+ var body = err.response.data;
247
+ if (body && body.error) {
248
+ return intro + ": " + body.error.trim();
249
+ }
250
+ }
251
+ catch (_a) {
252
+ // Ignore parsing errors
253
+ }
254
+ return intro + ": " + err.response.text;
255
+ };
256
+ return DatabaseRulesClient;
257
+ }());
258
+ function extractNamespace(parsedUrl) {
259
+ var ns = parsedUrl.searchParams.get('ns');
260
+ if (ns) {
261
+ return ns;
262
+ }
263
+ var hostname = parsedUrl.hostname;
264
+ var dotIndex = hostname.indexOf('.');
265
+ return hostname.substring(0, dotIndex).toLowerCase();
266
+ }
@@ -0,0 +1,89 @@
1
+ /*! firebase-admin v9.12.0 */
2
+ /*!
3
+ * Copyright 2020 Google Inc.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ /// <reference types="node" />
18
+ import { app } from '../firebase-namespace-api';
19
+ import * as rtdb from '@firebase/database-types';
20
+ /**
21
+ * Gets the {@link database.Database `Database`} service for the default
22
+ * app or a given app.
23
+ *
24
+ * `admin.database()` can be called with no arguments to access the default
25
+ * app's {@link database.Database `Database`} service or as
26
+ * `admin.database(app)` to access the
27
+ * {@link database.Database `Database`} service associated with a specific
28
+ * app.
29
+ *
30
+ * `admin.database` is also a namespace that can be used to access global
31
+ * constants and methods associated with the `Database` service.
32
+ *
33
+ * @example
34
+ * ```javascript
35
+ * // Get the Database service for the default app
36
+ * var defaultDatabase = admin.database();
37
+ * ```
38
+ *
39
+ * @example
40
+ * ```javascript
41
+ * // Get the Database service for a specific app
42
+ * var otherDatabase = admin.database(app);
43
+ * ```
44
+ *
45
+ * @param App whose `Database` service to
46
+ * return. If not provided, the default `Database` service will be returned.
47
+ *
48
+ * @return The default `Database` service if no app
49
+ * is provided or the `Database` service associated with the provided app.
50
+ */
51
+ export declare function database(app?: app.App): database.Database;
52
+ export declare namespace database {
53
+ interface Database extends rtdb.FirebaseDatabase {
54
+ /**
55
+ * Gets the currently applied security rules as a string. The return value consists of
56
+ * the rules source including comments.
57
+ *
58
+ * @return A promise fulfilled with the rules as a raw string.
59
+ */
60
+ getRules(): Promise<string>;
61
+ /**
62
+ * Gets the currently applied security rules as a parsed JSON object. Any comments in
63
+ * the original source are stripped away.
64
+ *
65
+ * @return A promise fulfilled with the parsed rules object.
66
+ */
67
+ getRulesJSON(): Promise<object>;
68
+ /**
69
+ * Sets the specified rules on the Firebase Realtime Database instance. If the rules source is
70
+ * specified as a string or a Buffer, it may include comments.
71
+ *
72
+ * @param source Source of the rules to apply. Must not be `null` or empty.
73
+ * @return Resolves when the rules are set on the Realtime Database.
74
+ */
75
+ setRules(source: string | Buffer | object): Promise<void>;
76
+ }
77
+ export import DataSnapshot = rtdb.DataSnapshot;
78
+ export import EventType = rtdb.EventType;
79
+ export import OnDisconnect = rtdb.OnDisconnect;
80
+ export import Query = rtdb.Query;
81
+ export import Reference = rtdb.Reference;
82
+ export import ThenableReference = rtdb.ThenableReference;
83
+ export import enableLogging = rtdb.enableLogging;
84
+ /**
85
+ * [`ServerValue`](https://firebase.google.com/docs/reference/js/firebase.database.ServerValue)
86
+ * module from the `@firebase/database` package.
87
+ */
88
+ const ServerValue: rtdb.ServerValue;
89
+ }
@@ -0,0 +1,31 @@
1
+ /*! firebase-admin v9.12.0 */
2
+ "use strict";
3
+ /*!
4
+ * Copyright 2020 Google Inc.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.database = void 0;
20
+ var standalone_1 = require("@firebase/database-compat/standalone");
21
+ var rtdb = require("@firebase/database-types");
22
+ /* eslint-disable @typescript-eslint/no-namespace */
23
+ var database;
24
+ (function (database) {
25
+ database.enableLogging = rtdb.enableLogging;
26
+ /**
27
+ * [`ServerValue`](https://firebase.google.com/docs/reference/js/firebase.database.ServerValue)
28
+ * module from the `@firebase/database` package.
29
+ */
30
+ database.ServerValue = standalone_1.ServerValue;
31
+ })(database = exports.database || (exports.database = {}));
@@ -0,0 +1,31 @@
1
+ /*! firebase-admin v9.12.0 */
2
+ "use strict";
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
+ var firebase_namespace_1 = require("./firebase-namespace");
20
+ var firebaseAdmin = new firebase_namespace_1.FirebaseNamespace();
21
+ // Inject a circular default export to allow users to use both:
22
+ //
23
+ // import firebaseAdmin from 'firebase-admin';
24
+ // which becomes: var firebaseAdmin = require('firebase-admin').default;
25
+ //
26
+ // as well as the more correct:
27
+ //
28
+ // import * as firebaseAdmin from 'firebase-admin';
29
+ // which becomes: var firebaseAdmin = require('firebase-admin');
30
+ firebaseAdmin.default = firebaseAdmin;
31
+ module.exports = firebaseAdmin;