index.node.esm.js 26 KB

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