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.

triggerHelper.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.functionResourceToEmulatedTriggerDefintion = void 0;
  4. const emulatorLogger_1 = require("../../emulator/emulatorLogger");
  5. const functionsEmulatorShared_1 = require("../../emulator/functionsEmulatorShared");
  6. const types_1 = require("../../emulator/types");
  7. const error_1 = require("../../error");
  8. const types_2 = require("../../extensions/types");
  9. const proto = require("../../gcp/proto");
  10. function functionResourceToEmulatedTriggerDefintion(resource) {
  11. const resourceType = resource.type;
  12. if (resource.type === types_2.FUNCTIONS_RESOURCE_TYPE) {
  13. const etd = {
  14. name: resource.name,
  15. entryPoint: resource.name,
  16. platform: "gcfv1",
  17. };
  18. const properties = resource.properties || {};
  19. proto.convertIfPresent(etd, properties, "timeoutSeconds", "timeout", proto.secondsFromDuration);
  20. proto.convertIfPresent(etd, properties, "regions", "location", (str) => [str]);
  21. proto.copyIfPresent(etd, properties, "availableMemoryMb");
  22. if (properties.httpsTrigger) {
  23. etd.httpsTrigger = properties.httpsTrigger;
  24. }
  25. if (properties.eventTrigger) {
  26. etd.eventTrigger = {
  27. eventType: properties.eventTrigger.eventType,
  28. resource: properties.eventTrigger.resource,
  29. service: (0, functionsEmulatorShared_1.getServiceFromEventType)(properties.eventTrigger.eventType),
  30. };
  31. }
  32. else if (properties.scheduleTrigger) {
  33. const schedule = {
  34. schedule: properties.scheduleTrigger.schedule,
  35. };
  36. etd.schedule = schedule;
  37. etd.eventTrigger = {
  38. eventType: "google.pubsub.topic.publish",
  39. resource: "",
  40. };
  41. }
  42. else {
  43. emulatorLogger_1.EmulatorLogger.forEmulator(types_1.Emulators.FUNCTIONS).log("WARN", `Function '${resource.name} is missing a trigger in extension.yaml. Please add one, as triggers defined in code are ignored.`);
  44. }
  45. return etd;
  46. }
  47. if (resource.type === types_2.FUNCTIONS_V2_RESOURCE_TYPE) {
  48. const etd = {
  49. name: resource.name,
  50. entryPoint: resource.name,
  51. platform: "gcfv2",
  52. };
  53. const properties = resource.properties || {};
  54. proto.convertIfPresent(etd, properties, "regions", "location", (str) => [str]);
  55. if (properties.serviceConfig) {
  56. proto.copyIfPresent(etd, properties.serviceConfig, "timeoutSeconds");
  57. proto.convertIfPresent(etd, properties.serviceConfig, "availableMemoryMb", "availableMemory", (mem) => parseInt(mem));
  58. }
  59. if (properties.eventTrigger) {
  60. etd.eventTrigger = {
  61. eventType: properties.eventTrigger.eventType,
  62. service: (0, functionsEmulatorShared_1.getServiceFromEventType)(properties.eventTrigger.eventType),
  63. };
  64. proto.copyIfPresent(etd.eventTrigger, properties.eventTrigger, "channel");
  65. if (properties.eventTrigger.eventFilters) {
  66. const eventFilters = {};
  67. const eventFilterPathPatterns = {};
  68. for (const filter of properties.eventTrigger.eventFilters) {
  69. if (filter.operator === undefined) {
  70. eventFilters[filter.attribute] = filter.value;
  71. }
  72. else if (filter.operator === "match-path-pattern") {
  73. eventFilterPathPatterns[filter.attribute] = filter.value;
  74. }
  75. }
  76. etd.eventTrigger.eventFilters = eventFilters;
  77. etd.eventTrigger.eventFilterPathPatterns = eventFilterPathPatterns;
  78. }
  79. }
  80. else {
  81. emulatorLogger_1.EmulatorLogger.forEmulator(types_1.Emulators.FUNCTIONS).log("WARN", `Function '${resource.name} is missing a trigger in extension.yaml. Please add one, as triggers defined in code are ignored.`);
  82. }
  83. return etd;
  84. }
  85. throw new error_1.FirebaseError("Unexpected resource type " + resourceType);
  86. }
  87. exports.functionResourceToEmulatedTriggerDefintion = functionResourceToEmulatedTriggerDefintion;