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.

functions.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.FunctionsServer = void 0;
  4. const path = require("path");
  5. const functionsEmulator_1 = require("../emulator/functionsEmulator");
  6. const functionsEmulatorUtils_1 = require("../emulator/functionsEmulatorUtils");
  7. const projectUtils_1 = require("../projectUtils");
  8. const auth_1 = require("../auth");
  9. const projectConfig = require("../functions/projectConfig");
  10. const utils = require("../utils");
  11. const registry_1 = require("../emulator/registry");
  12. class FunctionsServer {
  13. assertServer() {
  14. if (!this.emulator || !this.backends) {
  15. throw new Error("Must call start() before calling any other operation!");
  16. }
  17. return this.emulator;
  18. }
  19. async start(options, partialArgs) {
  20. const projectId = (0, projectUtils_1.needProjectId)(options);
  21. const config = projectConfig.normalizeAndValidate(options.config.src.functions);
  22. const backends = [];
  23. for (const cfg of config) {
  24. const functionsDir = path.join(options.config.projectDir, cfg.source);
  25. const nodeMajorVersion = (0, functionsEmulatorUtils_1.parseRuntimeVersion)(cfg.runtime);
  26. backends.push({
  27. functionsDir,
  28. codebase: cfg.codebase,
  29. nodeMajorVersion,
  30. env: {},
  31. secretEnv: [],
  32. });
  33. }
  34. this.backends = backends;
  35. const account = (0, auth_1.getProjectDefaultAccount)(options.config.projectDir);
  36. const args = Object.assign({ projectId, projectDir: options.config.projectDir, emulatableBackends: this.backends, projectAlias: options.projectAlias, account }, partialArgs);
  37. if (options.host) {
  38. utils.assertIsStringOrUndefined(options.host);
  39. args.host = options.host;
  40. }
  41. if (options.port) {
  42. utils.assertIsNumber(options.port);
  43. const targets = options.targets;
  44. const port = options.port;
  45. const hostingRunning = targets && targets.includes("hosting");
  46. if (hostingRunning) {
  47. args.port = port + 1;
  48. }
  49. else {
  50. args.port = port;
  51. }
  52. }
  53. this.emulator = new functionsEmulator_1.FunctionsEmulator(args);
  54. return registry_1.EmulatorRegistry.start(this.emulator);
  55. }
  56. async connect() {
  57. await this.assertServer().connect();
  58. }
  59. async stop() {
  60. await this.assertServer().stop();
  61. }
  62. get() {
  63. return this.assertServer();
  64. }
  65. }
  66. exports.FunctionsServer = FunctionsServer;