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.

functionsEmulatorShell.js 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.FunctionsEmulatorShell = void 0;
  4. const uuid = require("uuid");
  5. const functionsEmulator_1 = require("./functionsEmulator");
  6. const utils = require("../utils");
  7. const logger_1 = require("../logger");
  8. const error_1 = require("../error");
  9. class FunctionsEmulatorShell {
  10. constructor(emu) {
  11. this.emu = emu;
  12. this.urls = {};
  13. this.triggers = emu.getTriggerDefinitions();
  14. this.emulatedFunctions = this.triggers.map((t) => t.id);
  15. const entryPoints = this.triggers.map((t) => t.entryPoint);
  16. utils.logLabeledBullet("functions", `Loaded functions: ${entryPoints.join(", ")}`);
  17. for (const trigger of this.triggers) {
  18. if (trigger.httpsTrigger) {
  19. this.urls[trigger.id] = functionsEmulator_1.FunctionsEmulator.getHttpFunctionUrl(this.emu.getProjectId(), trigger.name, trigger.region, this.emu.getInfo());
  20. }
  21. }
  22. }
  23. call(name, data, opts) {
  24. const trigger = this.getTrigger(name);
  25. logger_1.logger.debug(`shell:${name}: trigger=${JSON.stringify(trigger)}`);
  26. logger_1.logger.debug(`shell:${name}: opts=${JSON.stringify(opts)}, data=${JSON.stringify(data)}`);
  27. if (!trigger.eventTrigger) {
  28. throw new error_1.FirebaseError(`Function ${name} is not a background function`);
  29. }
  30. const eventType = trigger.eventTrigger.eventType;
  31. let resource = opts.resource;
  32. if (typeof resource === "object" && resource.name) {
  33. resource = resource.name;
  34. }
  35. const proto = {
  36. eventId: uuid.v4(),
  37. timestamp: new Date().toISOString(),
  38. eventType,
  39. resource,
  40. params: opts.params,
  41. auth: opts.auth,
  42. data,
  43. };
  44. this.emu.sendRequest(trigger, proto);
  45. }
  46. getTrigger(name) {
  47. const result = this.triggers.find((trigger) => {
  48. return trigger.name === name;
  49. });
  50. if (!result) {
  51. throw new error_1.FirebaseError(`Could not find trigger ${name}`);
  52. }
  53. return result;
  54. }
  55. }
  56. exports.FunctionsEmulatorShell = FunctionsEmulatorShell;