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.

hub.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.EmulatorHub = void 0;
  4. const os = require("os");
  5. const fs = require("fs");
  6. const path = require("path");
  7. const utils = require("../utils");
  8. const logger_1 = require("../logger");
  9. const types_1 = require("./types");
  10. const hubExport_1 = require("./hubExport");
  11. const registry_1 = require("./registry");
  12. const ExpressBasedEmulator_1 = require("./ExpressBasedEmulator");
  13. const pkg = require("../../package.json");
  14. class EmulatorHub extends ExpressBasedEmulator_1.ExpressBasedEmulator {
  15. constructor(args) {
  16. super({
  17. listen: args.listen,
  18. });
  19. this.args = args;
  20. }
  21. static readLocatorFile(projectId) {
  22. const locatorPath = this.getLocatorFilePath(projectId);
  23. if (!fs.existsSync(locatorPath)) {
  24. return undefined;
  25. }
  26. const data = fs.readFileSync(locatorPath, "utf8").toString();
  27. const locator = JSON.parse(data);
  28. if (locator.version !== this.CLI_VERSION) {
  29. logger_1.logger.debug(`Found locator with mismatched version, ignoring: ${JSON.stringify(locator)}`);
  30. return undefined;
  31. }
  32. return locator;
  33. }
  34. static getLocatorFilePath(projectId) {
  35. const dir = os.tmpdir();
  36. const filename = `hub-${projectId}.json`;
  37. return path.join(dir, filename);
  38. }
  39. async start() {
  40. await super.start();
  41. await this.writeLocatorFile();
  42. }
  43. async createExpressApp() {
  44. const app = await super.createExpressApp();
  45. app.get("/", (req, res) => {
  46. res.json(Object.assign(Object.assign({}, this.getLocator()), { host: utils.connectableHostname(this.args.listen[0].address), port: this.args.listen[0].port }));
  47. });
  48. app.get(EmulatorHub.PATH_EMULATORS, (req, res) => {
  49. const body = {};
  50. for (const info of registry_1.EmulatorRegistry.listRunningWithInfo()) {
  51. body[info.name] = Object.assign({ listen: this.args.listenForEmulator[info.name] }, info);
  52. }
  53. res.json(body);
  54. });
  55. app.post(EmulatorHub.PATH_EXPORT, async (req, res) => {
  56. const path = req.body.path;
  57. const initiatedBy = req.body.initiatedBy || "unknown";
  58. utils.logLabeledBullet("emulators", `Received export request. Exporting data to ${path}.`);
  59. try {
  60. await new hubExport_1.HubExport(this.args.projectId, {
  61. path,
  62. initiatedBy,
  63. }).exportAll();
  64. utils.logLabeledSuccess("emulators", "Export complete.");
  65. res.status(200).send({
  66. message: "OK",
  67. });
  68. }
  69. catch (e) {
  70. const errorString = e.message || JSON.stringify(e);
  71. utils.logLabeledWarning("emulators", `Export failed: ${errorString}`);
  72. res.status(500).json({
  73. message: errorString,
  74. });
  75. }
  76. });
  77. app.put(EmulatorHub.PATH_DISABLE_FUNCTIONS, async (req, res) => {
  78. utils.logLabeledBullet("emulators", `Disabling Cloud Functions triggers, non-HTTP functions will not execute.`);
  79. const instance = registry_1.EmulatorRegistry.get(types_1.Emulators.FUNCTIONS);
  80. if (!instance) {
  81. res.status(400).json({ error: "The Cloud Functions emulator is not running." });
  82. return;
  83. }
  84. const emu = instance;
  85. await emu.disableBackgroundTriggers();
  86. res.status(200).json({ enabled: false });
  87. });
  88. app.put(EmulatorHub.PATH_ENABLE_FUNCTIONS, async (req, res) => {
  89. utils.logLabeledBullet("emulators", `Enabling Cloud Functions triggers, non-HTTP functions will execute.`);
  90. const instance = registry_1.EmulatorRegistry.get(types_1.Emulators.FUNCTIONS);
  91. if (!instance) {
  92. res.status(400).send("The Cloud Functions emulator is not running.");
  93. return;
  94. }
  95. const emu = instance;
  96. await emu.reloadTriggers();
  97. res.status(200).json({ enabled: true });
  98. });
  99. return app;
  100. }
  101. async stop() {
  102. await super.stop();
  103. await this.deleteLocatorFile();
  104. }
  105. getName() {
  106. return types_1.Emulators.HUB;
  107. }
  108. getLocator() {
  109. const version = pkg.version;
  110. const origins = [];
  111. for (const spec of this.args.listen) {
  112. if (spec.family === "IPv6") {
  113. origins.push(`http://[${utils.connectableHostname(spec.address)}]:${spec.port}`);
  114. }
  115. else {
  116. origins.push(`http://${utils.connectableHostname(spec.address)}:${spec.port}`);
  117. }
  118. }
  119. return {
  120. version,
  121. origins,
  122. };
  123. }
  124. async writeLocatorFile() {
  125. const projectId = this.args.projectId;
  126. const locatorPath = EmulatorHub.getLocatorFilePath(projectId);
  127. const locator = this.getLocator();
  128. if (fs.existsSync(locatorPath)) {
  129. utils.logLabeledWarning("emulators", `It seems that you are running multiple instances of the emulator suite for project ${projectId}. This may result in unexpected behavior.`);
  130. }
  131. logger_1.logger.debug(`[hub] writing locator at ${locatorPath}`);
  132. return new Promise((resolve, reject) => {
  133. fs.writeFile(locatorPath, JSON.stringify(locator), (e) => {
  134. if (e) {
  135. reject(e);
  136. }
  137. else {
  138. resolve();
  139. }
  140. });
  141. });
  142. }
  143. async deleteLocatorFile() {
  144. const locatorPath = EmulatorHub.getLocatorFilePath(this.args.projectId);
  145. return new Promise((resolve, reject) => {
  146. fs.unlink(locatorPath, (e) => {
  147. if (e) {
  148. reject(e);
  149. }
  150. else {
  151. resolve();
  152. }
  153. });
  154. });
  155. }
  156. }
  157. exports.EmulatorHub = EmulatorHub;
  158. EmulatorHub.CLI_VERSION = pkg.version;
  159. EmulatorHub.PATH_EXPORT = "/_admin/export";
  160. EmulatorHub.PATH_DISABLE_FUNCTIONS = "/functions/disableBackgroundTriggers";
  161. EmulatorHub.PATH_ENABLE_FUNCTIONS = "/functions/enableBackgroundTriggers";
  162. EmulatorHub.PATH_EMULATORS = "/emulators";