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.

cloudFunctions.js 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.StorageCloudFunctions = void 0;
  4. const uuid = require("uuid");
  5. const registry_1 = require("../registry");
  6. const types_1 = require("../types");
  7. const emulatorLogger_1 = require("../emulatorLogger");
  8. const metadata_1 = require("./metadata");
  9. const STORAGE_V2_ACTION_MAP = {
  10. finalize: "finalized",
  11. metadataUpdate: "metadataUpdated",
  12. delete: "deleted",
  13. archive: "archived",
  14. };
  15. class StorageCloudFunctions {
  16. constructor(projectId) {
  17. this.projectId = projectId;
  18. this.logger = emulatorLogger_1.EmulatorLogger.forEmulator(types_1.Emulators.STORAGE);
  19. this.multicastPath = "";
  20. this.enabled = false;
  21. if (registry_1.EmulatorRegistry.isRunning(types_1.Emulators.FUNCTIONS)) {
  22. this.enabled = true;
  23. this.multicastPath = `/functions/projects/${projectId}/trigger_multicast`;
  24. this.client = registry_1.EmulatorRegistry.client(types_1.Emulators.FUNCTIONS);
  25. }
  26. }
  27. async dispatch(action, object) {
  28. if (!this.enabled) {
  29. return;
  30. }
  31. const errStatus = [];
  32. let err;
  33. try {
  34. const eventBody = this.createLegacyEventRequestBody(action, object);
  35. const eventRes = await this.client.post(this.multicastPath, eventBody);
  36. if (eventRes.status !== 200) {
  37. errStatus.push(eventRes.status);
  38. }
  39. const cloudEventBody = this.createCloudEventRequestBody(action, object);
  40. const cloudEventRes = await this.client.post(this.multicastPath, cloudEventBody, {
  41. headers: { "Content-Type": "application/cloudevents+json; charset=UTF-8" },
  42. });
  43. if (cloudEventRes.status !== 200) {
  44. errStatus.push(cloudEventRes.status);
  45. }
  46. }
  47. catch (e) {
  48. err = e;
  49. }
  50. if (err || errStatus.length > 0) {
  51. this.logger.logLabeled("WARN", "functions", `Firebase Storage function was not triggered due to emulation error. Please file a bug.`);
  52. }
  53. }
  54. createLegacyEventRequestBody(action, objectMetadataPayload) {
  55. const timestamp = new Date();
  56. return {
  57. eventId: `${timestamp.getTime()}`,
  58. timestamp: (0, metadata_1.toSerializedDate)(timestamp),
  59. eventType: `google.storage.object.${action}`,
  60. resource: {
  61. service: "storage.googleapis.com",
  62. name: `projects/_/buckets/${objectMetadataPayload.bucket}/objects/${objectMetadataPayload.name}`,
  63. type: "storage#object",
  64. },
  65. data: objectMetadataPayload,
  66. };
  67. }
  68. createCloudEventRequestBody(action, objectMetadataPayload) {
  69. const ceAction = STORAGE_V2_ACTION_MAP[action];
  70. if (!ceAction) {
  71. throw new Error("Action is not defined as a CloudEvents action");
  72. }
  73. const data = objectMetadataPayload;
  74. let time = new Date().toISOString();
  75. if (data.updated) {
  76. time = typeof data.updated === "string" ? data.updated : data.updated.toISOString();
  77. }
  78. return {
  79. specversion: "1",
  80. id: uuid.v4(),
  81. type: `google.cloud.storage.object.v1.${ceAction}`,
  82. source: `//storage.googleapis.com/projects/_/buckets/${objectMetadataPayload.bucket}/objects/${objectMetadataPayload.name}`,
  83. time,
  84. data,
  85. };
  86. }
  87. }
  88. exports.StorageCloudFunctions = StorageCloudFunctions;