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.

ExpressBasedEmulator.js 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ExpressBasedEmulator = void 0;
  4. const cors = require("cors");
  5. const express = require("express");
  6. const bodyParser = require("body-parser");
  7. const utils = require("../utils");
  8. const node_http_1 = require("node:http");
  9. const dns_1 = require("./dns");
  10. class ExpressBasedEmulator {
  11. constructor(options) {
  12. this.options = options;
  13. this.destroyers = new Set();
  14. }
  15. createExpressApp() {
  16. const app = express();
  17. if (!this.options.noCors) {
  18. app.use(cors({ origin: true }));
  19. app.use((req, res, next) => {
  20. if (req.headers["access-control-request-private-network"]) {
  21. res.setHeader("access-control-allow-private-network", "true");
  22. }
  23. next();
  24. });
  25. }
  26. if (!this.options.noBodyParser) {
  27. app.use(bodyParser.json());
  28. }
  29. app.set("json spaces", 2);
  30. return Promise.resolve(app);
  31. }
  32. async start() {
  33. const app = await this.createExpressApp();
  34. const promises = [];
  35. const specs = this.options.listen;
  36. for (const opt of ExpressBasedEmulator.listenOptionsFromSpecs(specs)) {
  37. promises.push(new Promise((resolve, reject) => {
  38. const server = (0, node_http_1.createServer)(app).listen(opt);
  39. server.once("listening", resolve);
  40. server.once("error", reject);
  41. this.destroyers.add(utils.createDestroyer(server));
  42. }));
  43. }
  44. }
  45. static listenOptionsFromSpecs(specs) {
  46. const listenOptions = [];
  47. const dualStackPorts = new Set();
  48. for (const spec of specs) {
  49. if (spec.address === dns_1.IPV6_UNSPECIFIED.address) {
  50. if (specs.some((s) => s.port === spec.port && s.address === dns_1.IPV4_UNSPECIFIED.address)) {
  51. listenOptions.push({
  52. port: spec.port,
  53. ipv6Only: false,
  54. });
  55. dualStackPorts.add(spec.port);
  56. }
  57. }
  58. }
  59. for (const spec of specs) {
  60. if (!dualStackPorts.has(spec.port)) {
  61. listenOptions.push({
  62. host: spec.address,
  63. port: spec.port,
  64. ipv6Only: spec.family === "IPv6",
  65. });
  66. }
  67. }
  68. return listenOptions;
  69. }
  70. async connect() {
  71. }
  72. async stop() {
  73. const promises = [];
  74. for (const destroyer of this.destroyers) {
  75. promises.push(destroyer().then(() => this.destroyers.delete(destroyer)));
  76. }
  77. await Promise.all(promises);
  78. }
  79. getInfo() {
  80. return {
  81. name: this.getName(),
  82. listen: this.options.listen,
  83. host: this.options.listen[0].address,
  84. port: this.options.listen[0].port,
  85. };
  86. }
  87. }
  88. exports.ExpressBasedEmulator = ExpressBasedEmulator;
  89. ExpressBasedEmulator.PATH_EXPORT = "/_admin/export";
  90. ExpressBasedEmulator.PATH_DISABLE_FUNCTIONS = "/functions/disableBackgroundTriggers";
  91. ExpressBasedEmulator.PATH_ENABLE_FUNCTIONS = "/functions/enableBackgroundTriggers";
  92. ExpressBasedEmulator.PATH_EMULATORS = "/emulators";