GSI - Employe Self Service Mobile
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.

122 lines
4.3 KiB

2 months ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.EmulatorRegistry = void 0;
  4. const types_1 = require("./types");
  5. const error_1 = require("../error");
  6. const portUtils = require("./portUtils");
  7. const constants_1 = require("./constants");
  8. const emulatorLogger_1 = require("./emulatorLogger");
  9. const utils_1 = require("../utils");
  10. const apiv2_1 = require("../apiv2");
  11. class EmulatorRegistry {
  12. static async start(instance) {
  13. const description = constants_1.Constants.description(instance.getName());
  14. if (this.isRunning(instance.getName())) {
  15. throw new error_1.FirebaseError(`${description} is already running!`, {});
  16. }
  17. this.set(instance.getName(), instance);
  18. await instance.start();
  19. if (instance.getName() !== types_1.Emulators.EXTENSIONS) {
  20. const info = instance.getInfo();
  21. await portUtils.waitForPortClosed(info.port, (0, utils_1.connectableHostname)(info.host));
  22. }
  23. }
  24. static async stop(name) {
  25. emulatorLogger_1.EmulatorLogger.forEmulator(name).logLabeled("BULLET", name, `Stopping ${constants_1.Constants.description(name)}`);
  26. const instance = this.get(name);
  27. if (!instance) {
  28. return;
  29. }
  30. await instance.stop();
  31. this.clear(instance.getName());
  32. }
  33. static async stopAll() {
  34. const stopPriority = {
  35. ui: 0,
  36. extensions: 1,
  37. functions: 1.1,
  38. hosting: 2,
  39. database: 3.0,
  40. firestore: 3.1,
  41. pubsub: 3.2,
  42. auth: 3.3,
  43. storage: 3.5,
  44. eventarc: 3.6,
  45. hub: 4,
  46. logging: 5,
  47. };
  48. const emulatorsToStop = this.listRunning().sort((a, b) => {
  49. return stopPriority[a] - stopPriority[b];
  50. });
  51. for (const name of emulatorsToStop) {
  52. try {
  53. await this.stop(name);
  54. }
  55. catch (e) {
  56. emulatorLogger_1.EmulatorLogger.forEmulator(name).logLabeled("WARN", name, `Error stopping ${constants_1.Constants.description(name)}`);
  57. }
  58. }
  59. }
  60. static isRunning(emulator) {
  61. if (emulator === types_1.Emulators.EXTENSIONS) {
  62. return this.INSTANCES.get(emulator) !== undefined && this.isRunning(types_1.Emulators.FUNCTIONS);
  63. }
  64. const instance = this.INSTANCES.get(emulator);
  65. return instance !== undefined;
  66. }
  67. static listRunning() {
  68. return types_1.ALL_EMULATORS.filter((name) => this.isRunning(name));
  69. }
  70. static listRunningWithInfo() {
  71. return this.listRunning()
  72. .map((emulator) => this.getInfo(emulator))
  73. .filter((info) => typeof info !== "undefined");
  74. }
  75. static get(emulator) {
  76. return this.INSTANCES.get(emulator);
  77. }
  78. static getInfo(emulator) {
  79. var _a;
  80. const info = (_a = EmulatorRegistry.get(emulator)) === null || _a === void 0 ? void 0 : _a.getInfo();
  81. if (!info) {
  82. return undefined;
  83. }
  84. return Object.assign(Object.assign({}, info), { host: (0, utils_1.connectableHostname)(info.host) });
  85. }
  86. static url(emulator, req) {
  87. const url = new URL("http://unknown/");
  88. if (req) {
  89. url.protocol = req.protocol;
  90. const host = req.headers.host;
  91. if (host) {
  92. url.host = host;
  93. return url;
  94. }
  95. }
  96. const info = EmulatorRegistry.getInfo(emulator);
  97. if (info) {
  98. if (info.host.includes(":")) {
  99. url.hostname = `[${info.host}]`;
  100. }
  101. else {
  102. url.hostname = info.host;
  103. }
  104. url.port = info.port.toString();
  105. }
  106. else {
  107. console.warn(`Cannot determine host and port of ${emulator}`);
  108. }
  109. return url;
  110. }
  111. static client(emulator, options = {}) {
  112. return new apiv2_1.Client(Object.assign({ urlPrefix: EmulatorRegistry.url(emulator).toString(), auth: false }, options));
  113. }
  114. static set(emulator, instance) {
  115. this.INSTANCES.set(emulator, instance);
  116. }
  117. static clear(emulator) {
  118. this.INSTANCES.delete(emulator);
  119. }
  120. }
  121. exports.EmulatorRegistry = EmulatorRegistry;
  122. EmulatorRegistry.INSTANCES = new Map();