No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.detectFromPort = exports.detectFromYaml = exports.yamlToBuild = exports.readFileAsync = void 0;
  4. const node_fetch_1 = require("node-fetch");
  5. const fs = require("fs");
  6. const path = require("path");
  7. const yaml = require("js-yaml");
  8. const util_1 = require("util");
  9. const logger_1 = require("../../../../logger");
  10. const api = require("../../.../../../../api");
  11. const v1alpha1 = require("./v1alpha1");
  12. const error_1 = require("../../../../error");
  13. exports.readFileAsync = (0, util_1.promisify)(fs.readFile);
  14. function yamlToBuild(yaml, project, region, runtime) {
  15. try {
  16. if (!yaml.specVersion) {
  17. throw new error_1.FirebaseError("Expect manifest yaml to specify a version number");
  18. }
  19. if (yaml.specVersion === "v1alpha1") {
  20. return v1alpha1.buildFromV1Alpha1(yaml, project, region, runtime);
  21. }
  22. throw new error_1.FirebaseError("It seems you are using a newer SDK than this version of the CLI can handle. Please update your CLI with `npm install -g firebase-tools`");
  23. }
  24. catch (err) {
  25. throw new error_1.FirebaseError("Failed to parse build specification", { children: [err] });
  26. }
  27. }
  28. exports.yamlToBuild = yamlToBuild;
  29. async function detectFromYaml(directory, project, runtime) {
  30. let text;
  31. try {
  32. text = await exports.readFileAsync(path.join(directory, "functions.yaml"), "utf8");
  33. }
  34. catch (err) {
  35. if (err.code === "ENOENT") {
  36. logger_1.logger.debug("Could not find functions.yaml. Must use http discovery");
  37. }
  38. else {
  39. logger_1.logger.debug("Unexpected error looking for functions.yaml file:", err);
  40. }
  41. return;
  42. }
  43. logger_1.logger.debug("Found functions.yaml. Got spec:", text);
  44. const parsed = yaml.load(text);
  45. return yamlToBuild(parsed, project, api.functionsDefaultRegion, runtime);
  46. }
  47. exports.detectFromYaml = detectFromYaml;
  48. async function detectFromPort(port, project, runtime, timeout = 30000) {
  49. let res;
  50. const timedOut = new Promise((resolve, reject) => {
  51. setTimeout(() => {
  52. reject(new error_1.FirebaseError("User code failed to load. Cannot determine backend specification"));
  53. }, timeout);
  54. });
  55. while (true) {
  56. try {
  57. res = await Promise.race([(0, node_fetch_1.default)(`http://localhost:${port}/__/functions.yaml`), timedOut]);
  58. break;
  59. }
  60. catch (err) {
  61. if ((err === null || err === void 0 ? void 0 : err.code) === "ECONNREFUSED") {
  62. continue;
  63. }
  64. throw err;
  65. }
  66. }
  67. const text = await res.text();
  68. logger_1.logger.debug("Got response from /__/functions.yaml", text);
  69. let parsed;
  70. try {
  71. parsed = yaml.load(text);
  72. }
  73. catch (err) {
  74. logger_1.logger.debug("Failed to parse functions.yaml", err);
  75. throw new error_1.FirebaseError(`Failed to load function definition from source: ${text}`);
  76. }
  77. return yamlToBuild(parsed, project, api.functionsDefaultRegion, runtime);
  78. }
  79. exports.detectFromPort = detectFromPort;