index.cjs.js 26 KB

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