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.

hosting.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.connect = exports.start = exports.stop = void 0;
  4. const morgan = require("morgan");
  5. const net_1 = require("net");
  6. const superstatic_1 = require("superstatic");
  7. const clc = require("colorette");
  8. const detectProjectRoot_1 = require("../detectProjectRoot");
  9. const error_1 = require("../error");
  10. const implicitInit_1 = require("../hosting/implicitInit");
  11. const initMiddleware_1 = require("../hosting/initMiddleware");
  12. const config = require("../hosting/config");
  13. const cloudRunProxy_1 = require("../hosting/cloudRunProxy");
  14. const functionsProxy_1 = require("../hosting/functionsProxy");
  15. const stream_1 = require("stream");
  16. const emulatorLogger_1 = require("../emulator/emulatorLogger");
  17. const types_1 = require("../emulator/types");
  18. const utils_1 = require("../utils");
  19. const requireHostingSite_1 = require("../requireHostingSite");
  20. const projectUtils_1 = require("../projectUtils");
  21. const portUtils_1 = require("../emulator/portUtils");
  22. let destroyServer = undefined;
  23. const logger = emulatorLogger_1.EmulatorLogger.forEmulator(types_1.Emulators.HOSTING);
  24. function startServer(options, config, port, init) {
  25. const firebaseMiddleware = (0, initMiddleware_1.initMiddleware)(init);
  26. const morganStream = new stream_1.Writable();
  27. morganStream._write = (chunk, encoding, callback) => {
  28. if (chunk instanceof Buffer) {
  29. logger.logLabeled("BULLET", "hosting", chunk.toString().trim());
  30. }
  31. callback();
  32. };
  33. const morganMiddleware = morgan("combined", {
  34. stream: morganStream,
  35. });
  36. const after = options.frameworksDevModeHandle && {
  37. files: options.frameworksDevModeHandle,
  38. };
  39. const server = (0, superstatic_1.server)({
  40. debug: false,
  41. port: port,
  42. hostname: options.host,
  43. config: config,
  44. compression: true,
  45. cwd: (0, detectProjectRoot_1.detectProjectRoot)(options) || undefined,
  46. stack: "strict",
  47. before: {
  48. files: (req, res, next) => {
  49. morganMiddleware(req, res, () => null);
  50. firebaseMiddleware(req, res, next);
  51. },
  52. },
  53. after,
  54. rewriters: {
  55. function: (0, functionsProxy_1.functionsProxy)(options),
  56. run: (0, cloudRunProxy_1.default)(options),
  57. },
  58. }).listen(() => {
  59. const siteName = config.target || config.site;
  60. const label = siteName ? "hosting[" + siteName + "]" : "hosting";
  61. if (config.public && config.public !== ".") {
  62. logger.logLabeled("BULLET", label, "Serving hosting files from: " + clc.bold(config.public));
  63. }
  64. logger.logLabeled("SUCCESS", label, "Local server: " + clc.underline(clc.bold("http://" + options.host + ":" + port)));
  65. });
  66. destroyServer = (0, utils_1.createDestroyer)(server);
  67. server.on("error", (err) => {
  68. logger.log("DEBUG", `Error from superstatic server: ${err.stack || ""}`);
  69. throw new error_1.FirebaseError(`An error occurred while starting the hosting development server:\n\n${err.message}`);
  70. });
  71. }
  72. function stop() {
  73. return destroyServer ? destroyServer() : Promise.resolve();
  74. }
  75. exports.stop = stop;
  76. async function start(options) {
  77. const init = await (0, implicitInit_1.implicitInit)(options);
  78. if (!options.site) {
  79. try {
  80. await (0, requireHostingSite_1.requireHostingSite)(options);
  81. }
  82. catch (_a) {
  83. if (init.json) {
  84. options.site = JSON.parse(init.json).projectId;
  85. }
  86. else {
  87. options.site = (0, projectUtils_1.getProjectId)(options) || "site";
  88. }
  89. }
  90. }
  91. const configs = config.hostingConfig(options);
  92. const assignedPorts = new Set([5001]);
  93. for (let i = 0; i < configs.length; i++) {
  94. let port = i === 0 ? options.port : options.port + 4 + i;
  95. while (assignedPorts.has(port) || !(await availablePort(options.host, port))) {
  96. port += 1;
  97. }
  98. assignedPorts.add(port);
  99. startServer(options, configs[i], port, init);
  100. }
  101. assignedPorts.delete(5001);
  102. return { ports: Array.from(assignedPorts) };
  103. }
  104. exports.start = start;
  105. async function connect() {
  106. await Promise.resolve();
  107. }
  108. exports.connect = connect;
  109. function availablePort(host, port) {
  110. return (0, portUtils_1.checkListenable)({
  111. address: host,
  112. port,
  113. family: (0, net_1.isIPv4)(host) ? "IPv4" : "IPv6",
  114. });
  115. }