1 | ;
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var app = require('@firebase/app');
|
6 | var tslib = require('tslib');
|
7 | var util = require('@firebase/util');
|
8 | var component = require('@firebase/component');
|
9 | var nodeFetch = require('node-fetch');
|
10 |
|
11 | function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
12 |
|
13 | var nodeFetch__default = /*#__PURE__*/_interopDefaultLegacy(nodeFetch);
|
14 |
|
15 | /**
|
16 | * @license
|
17 | * Copyright 2017 Google LLC
|
18 | *
|
19 | * Licensed under the Apache License, Version 2.0 (the "License");
|
20 | * you may not use this file except in compliance with the License.
|
21 | * You may obtain a copy of the License at
|
22 | *
|
23 | * http://www.apache.org/licenses/LICENSE-2.0
|
24 | *
|
25 | * Unless required by applicable law or agreed to in writing, software
|
26 | * distributed under the License is distributed on an "AS IS" BASIS,
|
27 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
28 | * See the License for the specific language governing permissions and
|
29 | * limitations under the License.
|
30 | */
|
31 | var LONG_TYPE = 'type.googleapis.com/google.protobuf.Int64Value';
|
32 | var UNSIGNED_LONG_TYPE = 'type.googleapis.com/google.protobuf.UInt64Value';
|
33 | function mapValues(
|
34 | // { [k: string]: unknown } is no longer a wildcard assignment target after typescript 3.5
|
35 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
36 | o, f) {
|
37 | var result = {};
|
38 | for (var key in o) {
|
39 | if (o.hasOwnProperty(key)) {
|
40 | result[key] = f(o[key]);
|
41 | }
|
42 | }
|
43 | return result;
|
44 | }
|
45 | /**
|
46 | * Takes data and encodes it in a JSON-friendly way, such that types such as
|
47 | * Date are preserved.
|
48 | * @internal
|
49 | * @param data - Data to encode.
|
50 | */
|
51 | function encode(data) {
|
52 | if (data == null) {
|
53 | return null;
|
54 | }
|
55 | if (data instanceof Number) {
|
56 | data = data.valueOf();
|
57 | }
|
58 | if (typeof data === 'number' && isFinite(data)) {
|
59 | // Any number in JS is safe to put directly in JSON and parse as a double
|
60 | // without any loss of precision.
|
61 | return data;
|
62 | }
|
63 | if (data === true || data === false) {
|
64 | return data;
|
65 | }
|
66 | if (Object.prototype.toString.call(data) === '[object String]') {
|
67 | return data;
|
68 | }
|
69 | if (data instanceof Date) {
|
70 | return data.toISOString();
|
71 | }
|
72 | if (Array.isArray(data)) {
|
73 | return data.map(function (x) { return encode(x); });
|
74 | }
|
75 | if (typeof data === 'function' || typeof data === 'object') {
|
76 | return mapValues(data, function (x) { return encode(x); });
|
77 | }
|
78 | // If we got this far, the data is not encodable.
|
79 | throw new Error('Data cannot be encoded in JSON: ' + data);
|
80 | }
|
81 | /**
|
82 | * Takes data that's been encoded in a JSON-friendly form and returns a form
|
83 | * with richer datatypes, such as Dates, etc.
|
84 | * @internal
|
85 | * @param json - JSON to convert.
|
86 | */
|
87 | function decode(json) {
|
88 | if (json == null) {
|
89 | return json;
|
90 | }
|
91 | if (json['@type']) {
|
92 | switch (json['@type']) {
|
93 | case LONG_TYPE:
|
94 | // Fall through and handle this the same as unsigned.
|
95 | case UNSIGNED_LONG_TYPE: {
|
96 | // Technically, this could work return a valid number for malformed
|
97 | // data if there was a number followed by garbage. But it's just not
|
98 | // worth all the extra code to detect that case.
|
99 | var value = Number(json['value']);
|
100 | if (isNaN(value)) {
|
101 | throw new Error('Data cannot be decoded from JSON: ' + json);
|
102 | }
|
103 | return value;
|
104 | }
|
105 | default: {
|
106 | throw new Error('Data cannot be decoded from JSON: ' + json);
|
107 | }
|
108 | }
|
109 | }
|
110 | if (Array.isArray(json)) {
|
111 | return json.map(function (x) { return decode(x); });
|
112 | }
|
113 | if (typeof json === 'function' || typeof json === 'object') {
|
114 | return mapValues(json, function (x) { return decode(x); });
|
115 | }
|
116 | // Anything else is safe to return.
|
117 | return json;
|
118 | }
|
119 |
|
120 | /**
|
121 | * @license
|
122 | * Copyright 2020 Google LLC
|
123 | *
|
124 | * Licensed under the Apache License, Version 2.0 (the "License");
|
125 | * you may not use this file except in compliance with the License.
|
126 | * You may obtain a copy of the License at
|
127 | *
|
128 | * http://www.apache.org/licenses/LICENSE-2.0
|
129 | *
|
130 | * Unless required by applicable law or agreed to in writing, software
|
131 | * distributed under the License is distributed on an "AS IS" BASIS,
|
132 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
133 | * See the License for the specific language governing permissions and
|
134 | * limitations under the License.
|
135 | */
|
136 | /**
|
137 | * Type constant for Firebase Functions.
|
138 | */
|
139 | var FUNCTIONS_TYPE = 'functions';
|
140 |
|
141 | /**
|
142 | * @license
|
143 | * Copyright 2017 Google LLC
|
144 | *
|
145 | * Licensed under the Apache License, Version 2.0 (the "License");
|
146 | * you may not use this file except in compliance with the License.
|
147 | * You may obtain a copy of the License at
|
148 | *
|
149 | * http://www.apache.org/licenses/LICENSE-2.0
|
150 | *
|
151 | * Unless required by applicable law or agreed to in writing, software
|
152 | * distributed under the License is distributed on an "AS IS" BASIS,
|
153 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
154 | * See the License for the specific language governing permissions and
|
155 | * limitations under the License.
|
156 | */
|
157 | /**
|
158 | * Standard error codes for different ways a request can fail, as defined by:
|
159 | * https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto
|
160 | *
|
161 | * This map is used primarily to convert from a backend error code string to
|
162 | * a client SDK error code string, and make sure it's in the supported set.
|
163 | */
|
164 | var errorCodeMap = {
|
165 | OK: 'ok',
|
166 | CANCELLED: 'cancelled',
|
167 | UNKNOWN: 'unknown',
|
168 | INVALID_ARGUMENT: 'invalid-argument',
|
169 | DEADLINE_EXCEEDED: 'deadline-exceeded',
|
170 | NOT_FOUND: 'not-found',
|
171 | ALREADY_EXISTS: 'already-exists',
|
172 | PERMISSION_DENIED: 'permission-denied',
|
173 | UNAUTHENTICATED: 'unauthenticated',
|
174 | RESOURCE_EXHAUSTED: 'resource-exhausted',
|
175 | FAILED_PRECONDITION: 'failed-precondition',
|
176 | ABORTED: 'aborted',
|
177 | OUT_OF_RANGE: 'out-of-range',
|
178 | UNIMPLEMENTED: 'unimplemented',
|
179 | INTERNAL: 'internal',
|
180 | UNAVAILABLE: 'unavailable',
|
181 | DATA_LOSS: 'data-loss'
|
182 | };
|
183 | /**
|
184 | * An explicit error that can be thrown from a handler to send an error to the
|
185 | * client that called the function.
|
186 | */
|
187 | var FunctionsError = /** @class */ (function (_super) {
|
188 | tslib.__extends(FunctionsError, _super);
|
189 | function FunctionsError(
|
190 | /**
|
191 | * A standard error code that will be returned to the client. This also
|
192 | * determines the HTTP status code of the response, as defined in code.proto.
|
193 | */
|
194 | code, message,
|
195 | /**
|
196 | * Extra data to be converted to JSON and included in the error response.
|
197 | */
|
198 | details) {
|
199 | var _this = _super.call(this, FUNCTIONS_TYPE + "/" + code, message || '') || this;
|
200 | _this.details = details;
|
201 | return _this;
|
202 | }
|
203 | return FunctionsError;
|
204 | }(util.FirebaseError));
|
205 | /**
|
206 | * Takes an HTTP status code and returns the corresponding ErrorCode.
|
207 | * This is the standard HTTP status code -> error mapping defined in:
|
208 | * https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto
|
209 | *
|
210 | * @param status An HTTP status code.
|
211 | * @return The corresponding ErrorCode, or ErrorCode.UNKNOWN if none.
|
212 | */
|
213 | function codeForHTTPStatus(status) {
|
214 | // Make sure any successful status is OK.
|
215 | if (status >= 200 && status < 300) {
|
216 | return 'ok';
|
217 | }
|
218 | switch (status) {
|
219 | case 0:
|
220 | // This can happen if the server returns 500.
|
221 | return 'internal';
|
222 | case 400:
|
223 | return 'invalid-argument';
|
224 | case 401:
|
225 | return 'unauthenticated';
|
226 | case 403:
|
227 | return 'permission-denied';
|
228 | case 404:
|
229 | return 'not-found';
|
230 | case 409:
|
231 | return 'aborted';
|
232 | case 429:
|
233 | return 'resource-exhausted';
|
234 | case 499:
|
235 | return 'cancelled';
|
236 | case 500:
|
237 | return 'internal';
|
238 | case 501:
|
239 | return 'unimplemented';
|
240 | case 503:
|
241 | return 'unavailable';
|
242 | case 504:
|
243 | return 'deadline-exceeded';
|
244 | }
|
245 | return 'unknown';
|
246 | }
|
247 | /**
|
248 | * Takes an HTTP response and returns the corresponding Error, if any.
|
249 | */
|
250 | function _errorForResponse(status, bodyJSON) {
|
251 | var code = codeForHTTPStatus(status);
|
252 | // Start with reasonable defaults from the status code.
|
253 | var description = code;
|
254 | var details = undefined;
|
255 | // Then look through the body for explicit details.
|
256 | try {
|
257 | var errorJSON = bodyJSON && bodyJSON.error;
|
258 | if (errorJSON) {
|
259 | var status_1 = errorJSON.status;
|
260 | if (typeof status_1 === 'string') {
|
261 | if (!errorCodeMap[status_1]) {
|
262 | // They must've included an unknown error code in the body.
|
263 | return new FunctionsError('internal', 'internal');
|
264 | }
|
265 | code = errorCodeMap[status_1];
|
266 | // TODO(klimt): Add better default descriptions for error enums.
|
267 | // The default description needs to be updated for the new code.
|
268 | description = status_1;
|
269 | }
|
270 | var message = errorJSON.message;
|
271 | if (typeof message === 'string') {
|
272 | description = message;
|
273 | }
|
274 | details = errorJSON.details;
|
275 | if (details !== undefined) {
|
276 | details = decode(details);
|
277 | }
|
278 | }
|
279 | }
|
280 | catch (e) {
|
281 | // If we couldn't parse explicit error data, that's fine.
|
282 | }
|
283 | if (code === 'ok') {
|
284 | // Technically, there's an edge case where a developer could explicitly
|
285 | // return an error code of OK, and we will treat it as success, but that
|
286 | // seems reasonable.
|
287 | return null;
|
288 | }
|
289 | return new FunctionsError(code, description, details);
|
290 | }
|
291 |
|
292 | /**
|
293 | * @license
|
294 | * Copyright 2017 Google LLC
|
295 | *
|
296 | * Licensed under the Apache License, Version 2.0 (the "License");
|
297 | * you may not use this file except in compliance with the License.
|
298 | * You may obtain a copy of the License at
|
299 | *
|
300 | * http://www.apache.org/licenses/LICENSE-2.0
|
301 | *
|
302 | * Unless required by applicable law or agreed to in writing, software
|
303 | * distributed under the License is distributed on an "AS IS" BASIS,
|
304 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
305 | * See the License for the specific language governing permissions and
|
306 | * limitations under the License.
|
307 | */
|
308 | /**
|
309 | * Helper class to get metadata that should be included with a function call.
|
310 | * @internal
|
311 | */
|
312 | var ContextProvider = /** @class */ (function () {
|
313 | function ContextProvider(authProvider, messagingProvider, appCheckProvider) {
|
314 | var _this = this;
|
315 | this.auth = null;
|
316 | this.messaging = null;
|
317 | this.appCheck = null;
|
318 | this.auth = authProvider.getImmediate({ optional: true });
|
319 | this.messaging = messagingProvider.getImmediate({
|
320 | optional: true
|
321 | });
|
322 | if (!this.auth) {
|
323 | authProvider.get().then(function (auth) { return (_this.auth = auth); }, function () {
|
324 | /* get() never rejects */
|
325 | });
|
326 | }
|
327 | if (!this.messaging) {
|
328 | messagingProvider.get().then(function (messaging) { return (_this.messaging = messaging); }, function () {
|
329 | /* get() never rejects */
|
330 | });
|
331 | }
|
332 | if (!this.appCheck) {
|
333 | appCheckProvider.get().then(function (appCheck) { return (_this.appCheck = appCheck); }, function () {
|
334 | /* get() never rejects */
|
335 | });
|
336 | }
|
337 | }
|
338 | ContextProvider.prototype.getAuthToken = function () {
|
339 | return tslib.__awaiter(this, void 0, void 0, function () {
|
340 | var token;
|
341 | return tslib.__generator(this, function (_a) {
|
342 | switch (_a.label) {
|
343 | case 0:
|
344 | if (!this.auth) {
|
345 | return [2 /*return*/, undefined];
|
346 | }
|
347 | _a.label = 1;
|
348 | case 1:
|
349 | _a.trys.push([1, 3, , 4]);
|
350 | return [4 /*yield*/, this.auth.getToken()];
|
351 | case 2:
|
352 | token = _a.sent();
|
353 | return [2 /*return*/, token === null || token === void 0 ? void 0 : token.accessToken];
|
354 | case 3:
|
355 | _a.sent();
|
356 | // If there's any error when trying to get the auth token, leave it off.
|
357 | return [2 /*return*/, undefined];
|
358 | case 4: return [2 /*return*/];
|
359 | }
|
360 | });
|
361 | });
|
362 | };
|
363 | ContextProvider.prototype.getMessagingToken = function () {
|
364 | return tslib.__awaiter(this, void 0, void 0, function () {
|
365 | return tslib.__generator(this, function (_a) {
|
366 | switch (_a.label) {
|
367 | case 0:
|
368 | if (!this.messaging ||
|
369 | !('Notification' in self) ||
|
370 | Notification.permission !== 'granted') {
|
371 | return [2 /*return*/, undefined];
|
372 | }
|
373 | _a.label = 1;
|
374 | case 1:
|
375 | _a.trys.push([1, 3, , 4]);
|
376 | return [4 /*yield*/, this.messaging.getToken()];
|
377 | case 2: return [2 /*return*/, _a.sent()];
|
378 | case 3:
|
379 | _a.sent();
|
380 | // We don't warn on this, because it usually means messaging isn't set up.
|
381 | // console.warn('Failed to retrieve instance id token.', e);
|
382 | // If there's any error when trying to get the token, leave it off.
|
383 | return [2 /*return*/, undefined];
|
384 | case 4: return [2 /*return*/];
|
385 | }
|
386 | });
|
387 | });
|
388 | };
|
389 | ContextProvider.prototype.getAppCheckToken = function () {
|
390 | return tslib.__awaiter(this, void 0, void 0, function () {
|
391 | var result;
|
392 | return tslib.__generator(this, function (_a) {
|
393 | switch (_a.label) {
|
394 | case 0:
|
395 | if (!this.appCheck) return [3 /*break*/, 2];
|
396 | return [4 /*yield*/, this.appCheck.getToken()];
|
397 | case 1:
|
398 | result = _a.sent();
|
399 | if (result.error) {
|
400 | // Do not send the App Check header to the functions endpoint if
|
401 | // there was an error from the App Check exchange endpoint. The App
|
402 | // Check SDK will already have logged the error to console.
|
403 | return [2 /*return*/, null];
|
404 | }
|
405 | return [2 /*return*/, result.token];
|
406 | case 2: return [2 /*return*/, null];
|
407 | }
|
408 | });
|
409 | });
|
410 | };
|
411 | ContextProvider.prototype.getContext = function () {
|
412 | return tslib.__awaiter(this, void 0, void 0, function () {
|
413 | var authToken, messagingToken, appCheckToken;
|
414 | return tslib.__generator(this, function (_a) {
|
415 | switch (_a.label) {
|
416 | case 0: return [4 /*yield*/, this.getAuthToken()];
|
417 | case 1:
|
418 | authToken = _a.sent();
|
419 | return [4 /*yield*/, this.getMessagingToken()];
|
420 | case 2:
|
421 | messagingToken = _a.sent();
|
422 | return [4 /*yield*/, this.getAppCheckToken()];
|
423 | case 3:
|
424 | appCheckToken = _a.sent();
|
425 | return [2 /*return*/, { authToken: authToken, messagingToken: messagingToken, appCheckToken: appCheckToken }];
|
426 | }
|
427 | });
|
428 | });
|
429 | };
|
430 | return ContextProvider;
|
431 | }());
|
432 |
|
433 | /**
|
434 | * @license
|
435 | * Copyright 2017 Google LLC
|
436 | *
|
437 | * Licensed under the Apache License, Version 2.0 (the "License");
|
438 | * you may not use this file except in compliance with the License.
|
439 | * You may obtain a copy of the License at
|
440 | *
|
441 | * http://www.apache.org/licenses/LICENSE-2.0
|
442 | *
|
443 | * Unless required by applicable law or agreed to in writing, software
|
444 | * distributed under the License is distributed on an "AS IS" BASIS,
|
445 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
446 | * See the License for the specific language governing permissions and
|
447 | * limitations under the License.
|
448 | */
|
449 | var DEFAULT_REGION = 'us-central1';
|
450 | /**
|
451 | * Returns a Promise that will be rejected after the given duration.
|
452 | * The error will be of type FunctionsError.
|
453 | *
|
454 | * @param millis Number of milliseconds to wait before rejecting.
|
455 | */
|
456 | function failAfter(millis) {
|
457 | // Node timers and browser timers are fundamentally incompatible, but we
|
458 | // don't care about the value here
|
459 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
460 | var timer = null;
|
461 | return {
|
462 | promise: new Promise(function (_, reject) {
|
463 | timer = setTimeout(function () {
|
464 | reject(new FunctionsError('deadline-exceeded', 'deadline-exceeded'));
|
465 | }, millis);
|
466 | }),
|
467 | cancel: function () {
|
468 | if (timer) {
|
469 | clearTimeout(timer);
|
470 | }
|
471 | }
|
472 | };
|
473 | }
|
474 | /**
|
475 | * The main class for the Firebase Functions SDK.
|
476 | * @internal
|
477 | */
|
478 | var FunctionsService = /** @class */ (function () {
|
479 | /**
|
480 | * Creates a new Functions service for the given app.
|
481 | * @param app - The FirebaseApp to use.
|
482 | */
|
483 | function FunctionsService(app, authProvider, messagingProvider, appCheckProvider, regionOrCustomDomain, fetchImpl) {
|
484 | var _this = this;
|
485 | if (regionOrCustomDomain === void 0) { regionOrCustomDomain = DEFAULT_REGION; }
|
486 | this.app = app;
|
487 | this.fetchImpl = fetchImpl;
|
488 | this.emulatorOrigin = null;
|
489 | this.contextProvider = new ContextProvider(authProvider, messagingProvider, appCheckProvider);
|
490 | // Cancels all ongoing requests when resolved.
|
491 | this.cancelAllRequests = new Promise(function (resolve) {
|
492 | _this.deleteService = function () {
|
493 | return Promise.resolve(resolve());
|
494 | };
|
495 | });
|
496 | // Resolve the region or custom domain overload by attempting to parse it.
|
497 | try {
|
498 | var url = new URL(regionOrCustomDomain);
|
499 | this.customDomain = url.origin;
|
500 | this.region = DEFAULT_REGION;
|
501 | }
|
502 | catch (e) {
|
503 | this.customDomain = null;
|
504 | this.region = regionOrCustomDomain;
|
505 | }
|
506 | }
|
507 | FunctionsService.prototype._delete = function () {
|
508 | return this.deleteService();
|
509 | };
|
510 | /**
|
511 | * Returns the URL for a callable with the given name.
|
512 | * @param name - The name of the callable.
|
513 | * @internal
|
514 | */
|
515 | FunctionsService.prototype._url = function (name) {
|
516 | var projectId = this.app.options.projectId;
|
517 | if (this.emulatorOrigin !== null) {
|
518 | var origin_1 = this.emulatorOrigin;
|
519 | return origin_1 + "/" + projectId + "/" + this.region + "/" + name;
|
520 | }
|
521 | if (this.customDomain !== null) {
|
522 | return this.customDomain + "/" + name;
|
523 | }
|
524 | return "https://" + this.region + "-" + projectId + ".cloudfunctions.net/" + name;
|
525 | };
|
526 | return FunctionsService;
|
527 | }());
|
528 | /**
|
529 | * Modify this instance to communicate with the Cloud Functions emulator.
|
530 | *
|
531 | * Note: this must be called before this instance has been used to do any operations.
|
532 | *
|
533 | * @param host The emulator host (ex: localhost)
|
534 | * @param port The emulator port (ex: 5001)
|
535 | * @public
|
536 | */
|
537 | function connectFunctionsEmulator$1(functionsInstance, host, port) {
|
538 | functionsInstance.emulatorOrigin = "http://" + host + ":" + port;
|
539 | }
|
540 | /**
|
541 | * Returns a reference to the callable https trigger with the given name.
|
542 | * @param name - The name of the trigger.
|
543 | * @public
|
544 | */
|
545 | function httpsCallable$1(functionsInstance, name, options) {
|
546 | return (function (data) {
|
547 | return call(functionsInstance, name, data, options || {});
|
548 | });
|
549 | }
|
550 | /**
|
551 | * Returns a reference to the callable https trigger with the given url.
|
552 | * @param url - The url of the trigger.
|
553 | * @public
|
554 | */
|
555 | function httpsCallableFromURL$1(functionsInstance, url, options) {
|
556 | return (function (data) {
|
557 | return callAtURL(functionsInstance, url, data, options || {});
|
558 | });
|
559 | }
|
560 | /**
|
561 | * Does an HTTP POST and returns the completed response.
|
562 | * @param url The url to post to.
|
563 | * @param body The JSON body of the post.
|
564 | * @param headers The HTTP headers to include in the request.
|
565 | * @return A Promise that will succeed when the request finishes.
|
566 | */
|
567 | function postJSON(url, body, headers, fetchImpl) {
|
568 | return tslib.__awaiter(this, void 0, void 0, function () {
|
569 | var response, json;
|
570 | return tslib.__generator(this, function (_a) {
|
571 | switch (_a.label) {
|
572 | case 0:
|
573 | headers['Content-Type'] = 'application/json';
|
574 | _a.label = 1;
|
575 | case 1:
|
576 | _a.trys.push([1, 3, , 4]);
|
577 | return [4 /*yield*/, fetchImpl(url, {
|
578 | method: 'POST',
|
579 | body: JSON.stringify(body),
|
580 | headers: headers
|
581 | })];
|
582 | case 2:
|
583 | response = _a.sent();
|
584 | return [3 /*break*/, 4];
|
585 | case 3:
|
586 | _a.sent();
|
587 | // This could be an unhandled error on the backend, or it could be a
|
588 | // network error. There's no way to know, since an unhandled error on the
|
589 | // backend will fail to set the proper CORS header, and thus will be
|
590 | // treated as a network error by fetch.
|
591 | return [2 /*return*/, {
|
592 | status: 0,
|
593 | json: null
|
594 | }];
|
595 | case 4:
|
596 | json = null;
|
597 | _a.label = 5;
|
598 | case 5:
|
599 | _a.trys.push([5, 7, , 8]);
|
600 | return [4 /*yield*/, response.json()];
|
601 | case 6:
|
602 | json = _a.sent();
|
603 | return [3 /*break*/, 8];
|
604 | case 7:
|
605 | _a.sent();
|
606 | return [3 /*break*/, 8];
|
607 | case 8: return [2 /*return*/, {
|
608 | status: response.status,
|
609 | json: json
|
610 | }];
|
611 | }
|
612 | });
|
613 | });
|
614 | }
|
615 | /**
|
616 | * Calls a callable function asynchronously and returns the result.
|
617 | * @param name The name of the callable trigger.
|
618 | * @param data The data to pass as params to the function.s
|
619 | */
|
620 | function call(functionsInstance, name, data, options) {
|
621 | var url = functionsInstance._url(name);
|
622 | return callAtURL(functionsInstance, url, data, options);
|
623 | }
|
624 | /**
|
625 | * Calls a callable function asynchronously and returns the result.
|
626 | * @param url The url of the callable trigger.
|
627 | * @param data The data to pass as params to the function.s
|
628 | */
|
629 | function callAtURL(functionsInstance, url, data, options) {
|
630 | return tslib.__awaiter(this, void 0, void 0, function () {
|
631 | var body, headers, context, timeout, failAfterHandle, response, error, responseData, decodedData;
|
632 | return tslib.__generator(this, function (_a) {
|
633 | switch (_a.label) {
|
634 | case 0:
|
635 | // Encode any special types, such as dates, in the input data.
|
636 | data = encode(data);
|
637 | body = { data: data };
|
638 | headers = {};
|
639 | return [4 /*yield*/, functionsInstance.contextProvider.getContext()];
|
640 | case 1:
|
641 | context = _a.sent();
|
642 | if (context.authToken) {
|
643 | headers['Authorization'] = 'Bearer ' + context.authToken;
|
644 | }
|
645 | if (context.messagingToken) {
|
646 | headers['Firebase-Instance-ID-Token'] = context.messagingToken;
|
647 | }
|
648 | if (context.appCheckToken !== null) {
|
649 | headers['X-Firebase-AppCheck'] = context.appCheckToken;
|
650 | }
|
651 | timeout = options.timeout || 70000;
|
652 | failAfterHandle = failAfter(timeout);
|
653 | return [4 /*yield*/, Promise.race([
|
654 | postJSON(url, body, headers, functionsInstance.fetchImpl),
|
655 | failAfterHandle.promise,
|
656 | functionsInstance.cancelAllRequests
|
657 | ])];
|
658 | case 2:
|
659 | response = _a.sent();
|
660 | // Always clear the failAfter timeout
|
661 | failAfterHandle.cancel();
|
662 | // If service was deleted, interrupted response throws an error.
|
663 | if (!response) {
|
664 | throw new FunctionsError('cancelled', 'Firebase Functions instance was deleted.');
|
665 | }
|
666 | error = _errorForResponse(response.status, response.json);
|
667 | if (error) {
|
668 | throw error;
|
669 | }
|
670 | if (!response.json) {
|
671 | throw new FunctionsError('internal', 'Response is not valid JSON object.');
|
672 | }
|
673 | responseData = response.json.data;
|
674 | // TODO(klimt): For right now, allow "result" instead of "data", for
|
675 | // backwards compatibility.
|
676 | if (typeof responseData === 'undefined') {
|
677 | responseData = response.json.result;
|
678 | }
|
679 | if (typeof responseData === 'undefined') {
|
680 | // Consider the response malformed.
|
681 | throw new FunctionsError('internal', 'Response is missing data field.');
|
682 | }
|
683 | decodedData = decode(responseData);
|
684 | return [2 /*return*/, { data: decodedData }];
|
685 | }
|
686 | });
|
687 | });
|
688 | }
|
689 |
|
690 | var name = "@firebase/functions";
|
691 | var version = "0.8.8";
|
692 |
|
693 | /**
|
694 | * @license
|
695 | * Copyright 2019 Google LLC
|
696 | *
|
697 | * Licensed under the Apache License, Version 2.0 (the "License");
|
698 | * you may not use this file except in compliance with the License.
|
699 | * You may obtain a copy of the License at
|
700 | *
|
701 | * http://www.apache.org/licenses/LICENSE-2.0
|
702 | *
|
703 | * Unless required by applicable law or agreed to in writing, software
|
704 | * distributed under the License is distributed on an "AS IS" BASIS,
|
705 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
706 | * See the License for the specific language governing permissions and
|
707 | * limitations under the License.
|
708 | */
|
709 | var AUTH_INTERNAL_NAME = 'auth-internal';
|
710 | var APP_CHECK_INTERNAL_NAME = 'app-check-internal';
|
711 | var MESSAGING_INTERNAL_NAME = 'messaging-internal';
|
712 | function registerFunctions(fetchImpl, variant) {
|
713 | var factory = function (container, _a) {
|
714 | var regionOrCustomDomain = _a.instanceIdentifier;
|
715 | // Dependencies
|
716 | var app = container.getProvider('app').getImmediate();
|
717 | var authProvider = container.getProvider(AUTH_INTERNAL_NAME);
|
718 | var messagingProvider = container.getProvider(MESSAGING_INTERNAL_NAME);
|
719 | var appCheckProvider = container.getProvider(APP_CHECK_INTERNAL_NAME);
|
720 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
721 | return new FunctionsService(app, authProvider, messagingProvider, appCheckProvider, regionOrCustomDomain, fetchImpl);
|
722 | };
|
723 | app._registerComponent(new component.Component(FUNCTIONS_TYPE, factory, "PUBLIC" /* PUBLIC */).setMultipleInstances(true));
|
724 | app.registerVersion(name, version, variant);
|
725 | // BUILD_TARGET will be replaced by values like esm5, esm2017, cjs5, etc during the compilation
|
726 | app.registerVersion(name, version, 'cjs5');
|
727 | }
|
728 |
|
729 | /**
|
730 | * @license
|
731 | * Copyright 2020 Google LLC
|
732 | *
|
733 | * Licensed under the Apache License, Version 2.0 (the "License");
|
734 | * you may not use this file except in compliance with the License.
|
735 | * You may obtain a copy of the License at
|
736 | *
|
737 | * http://www.apache.org/licenses/LICENSE-2.0
|
738 | *
|
739 | * Unless required by applicable law or agreed to in writing, software
|
740 | * distributed under the License is distributed on an "AS IS" BASIS,
|
741 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
742 | * See the License for the specific language governing permissions and
|
743 | * limitations under the License.
|
744 | */
|
745 | /**
|
746 | * Returns a {@link Functions} instance for the given app.
|
747 | * @param app - The {@link @firebase/app#FirebaseApp} to use.
|
748 | * @param regionOrCustomDomain - one of:
|
749 | * a) The region the callable functions are located in (ex: us-central1)
|
750 | * b) A custom domain hosting the callable functions (ex: https://mydomain.com)
|
751 | * @public
|
752 | */
|
753 | function getFunctions(app$1, regionOrCustomDomain) {
|
754 | if (app$1 === void 0) { app$1 = app.getApp(); }
|
755 | if (regionOrCustomDomain === void 0) { regionOrCustomDomain = DEFAULT_REGION; }
|
756 | // Dependencies
|
757 | var functionsProvider = app._getProvider(util.getModularInstance(app$1), FUNCTIONS_TYPE);
|
758 | var functionsInstance = functionsProvider.getImmediate({
|
759 | identifier: regionOrCustomDomain
|
760 | });
|
761 | var emulator = util.getDefaultEmulatorHostnameAndPort('functions');
|
762 | if (emulator) {
|
763 | connectFunctionsEmulator.apply(void 0, tslib.__spreadArray([functionsInstance], emulator));
|
764 | }
|
765 | return functionsInstance;
|
766 | }
|
767 | /**
|
768 | * Modify this instance to communicate with the Cloud Functions emulator.
|
769 | *
|
770 | * Note: this must be called before this instance has been used to do any operations.
|
771 | *
|
772 | * @param host - The emulator host (ex: localhost)
|
773 | * @param port - The emulator port (ex: 5001)
|
774 | * @public
|
775 | */
|
776 | function connectFunctionsEmulator(functionsInstance, host, port) {
|
777 | connectFunctionsEmulator$1(util.getModularInstance(functionsInstance), host, port);
|
778 | }
|
779 | /**
|
780 | * Returns a reference to the callable HTTPS trigger with the given name.
|
781 | * @param name - The name of the trigger.
|
782 | * @public
|
783 | */
|
784 | function httpsCallable(functionsInstance, name, options) {
|
785 | return httpsCallable$1(util.getModularInstance(functionsInstance), name, options);
|
786 | }
|
787 | /**
|
788 | * Returns a reference to the callable HTTPS trigger with the specified url.
|
789 | * @param url - The url of the trigger.
|
790 | * @public
|
791 | */
|
792 | function httpsCallableFromURL(functionsInstance, url, options) {
|
793 | return httpsCallableFromURL$1(util.getModularInstance(functionsInstance), url, options);
|
794 | }
|
795 |
|
796 | /**
|
797 | * @license
|
798 | * Copyright 2017 Google LLC
|
799 | *
|
800 | * Licensed under the Apache License, Version 2.0 (the "License");
|
801 | * you may not use this file except in compliance with the License.
|
802 | * You may obtain a copy of the License at
|
803 | *
|
804 | * http://www.apache.org/licenses/LICENSE-2.0
|
805 | *
|
806 | * Unless required by applicable law or agreed to in writing, software
|
807 | * distributed under the License is distributed on an "AS IS" BASIS,
|
808 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
809 | * See the License for the specific language governing permissions and
|
810 | * limitations under the License.
|
811 | */
|
812 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
813 | registerFunctions(nodeFetch__default["default"], 'node');
|
814 |
|
815 | exports.connectFunctionsEmulator = connectFunctionsEmulator;
|
816 | exports.getFunctions = getFunctions;
|
817 | exports.httpsCallable = httpsCallable;
|
818 | exports.httpsCallableFromURL = httpsCallableFromURL;
|
819 | //# sourceMappingURL=index.node.cjs.js.map
|