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