gcf-common-lib 0.11.1 → 0.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +15 -7
  2. package/index.ts +28 -11
  3. package/package.json +5 -5
package/index.js CHANGED
@@ -64,15 +64,23 @@ class GcfCommon {
64
64
  });
65
65
  }
66
66
  static getTopic(event, context) {
67
- var _a, _b, _c;
67
+ var _a, _b, _c, _d;
68
68
  return __awaiter(this, void 0, void 0, function* () {
69
69
  /** t_{GUID}__{YYYY-MM-DD} */
70
- let topicName = (_a = event === null || event === void 0 ? void 0 : event.metadata) === null || _a === void 0 ? void 0 : _a.topic;
71
- if (!topicName && ((_b = context === null || context === void 0 ? void 0 : context.resource) === null || _b === void 0 ? void 0 : _b.type) === 'storage#object') {
72
- const file = exports.storage.bucket(event.bucket).file(event.name);
73
- const [meta] = yield file.getMetadata();
74
- topicName = (_c = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _c === void 0 ? void 0 : _c.topic;
75
- console.log('topic:', topicName);
70
+ let topicName;
71
+ if ((context === null || context === void 0 ? void 0 : context.eventType) === 'google.storage.object.finalize') {
72
+ const gsEvent = event;
73
+ topicName = (_a = gsEvent === null || gsEvent === void 0 ? void 0 : gsEvent.metadata) === null || _a === void 0 ? void 0 : _a.topic;
74
+ if (!topicName && ((_b = context === null || context === void 0 ? void 0 : context.resource) === null || _b === void 0 ? void 0 : _b.type) === 'storage#object') {
75
+ const file = exports.storage.bucket(gsEvent.bucket).file(gsEvent.name);
76
+ const [meta] = yield file.getMetadata();
77
+ topicName = (_c = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _c === void 0 ? void 0 : _c.topic;
78
+ console.log('topic:', topicName);
79
+ }
80
+ }
81
+ else if ((context === null || context === void 0 ? void 0 : context.eventType) === 'google.pubsub.topic.publish') {
82
+ const psEvent = event;
83
+ topicName = (_d = psEvent.data) === null || _d === void 0 ? void 0 : _d.topic;
76
84
  }
77
85
  if (topicName && !(0, isEmpty_1.default)(topicName)) {
78
86
  const topic = exports.pubSub.topic(topicName);
package/index.ts CHANGED
@@ -5,7 +5,7 @@ import isEmpty from "lodash/isEmpty";
5
5
  import noop from "lodash/noop";
6
6
  import {timeoutAfter} from "./utils";
7
7
 
8
- export type TEvent = {
8
+ export type TGSEvent = {
9
9
  bucket: string;
10
10
  contentType: string;
11
11
  crc32c: string;
@@ -26,14 +26,22 @@ export type TEvent = {
26
26
  updated: string;
27
27
  }
28
28
 
29
+ export type TPSEvent = {
30
+ '@type': string; // 'type.googleapis.com/google.pubsub.v1.PubsubMessage'
31
+ attributes?: string | null;
32
+ data: string | { [k: string]: any };
33
+ }
34
+
35
+ export type TEvent = TGSEvent | TPSEvent;
36
+
29
37
  export type TContext = {
30
38
  eventId: string;
31
39
  timestamp: string;
32
- eventType: string; // 'google.storage.object.finalize'
40
+ eventType: 'google.storage.object.finalize' | 'google.pubsub.topic.publish';
33
41
  resource: {
34
- service: string; // 'storage.googleapis.com'
42
+ service: 'storage.googleapis.com' | 'pubsub.googleapis.com';
35
43
  name: string;
36
- type: string; // 'storage#object'
44
+ type: string; // 'storage#object' | 'type.googleapis.com/google.pubsub.v1.PubsubMessage'
37
45
  }
38
46
  }
39
47
 
@@ -96,13 +104,22 @@ export class GcfCommon {
96
104
  static async getTopic(event: TEvent, context: TContext) {
97
105
 
98
106
  /** t_{GUID}__{YYYY-MM-DD} */
99
- let topicName: string | undefined = event?.metadata?.topic;
107
+ let topicName: string | undefined;
100
108
 
101
- if (!topicName && context?.resource?.type === 'storage#object') {
102
- const file: File = storage.bucket(event.bucket).file(event.name);
103
- const [meta] = await file.getMetadata();
104
- topicName = meta?.metadata?.topic;
105
- console.log('topic:', topicName);
109
+ if (context?.eventType === 'google.storage.object.finalize') {
110
+ const gsEvent = event as TGSEvent;
111
+
112
+ topicName = gsEvent?.metadata?.topic;
113
+
114
+ if (!topicName && context?.resource?.type === 'storage#object') {
115
+ const file: File = storage.bucket(gsEvent.bucket).file(gsEvent.name);
116
+ const [meta] = await file.getMetadata();
117
+ topicName = meta?.metadata?.topic;
118
+ console.log('topic:', topicName);
119
+ }
120
+ } else if (context?.eventType === 'google.pubsub.topic.publish') {
121
+ const psEvent = event as TPSEvent;
122
+ topicName = (psEvent.data as any)?.topic;
106
123
  }
107
124
 
108
125
  if (topicName && !isEmpty(topicName)) {
@@ -112,7 +129,7 @@ export class GcfCommon {
112
129
  }
113
130
  }
114
131
 
115
- static async getOptions(event: TEvent, context: TContext) {
132
+ static async getOptions(event: TGSEvent, context: TContext) {
116
133
  if (event?.metadata?.options) {
117
134
  return JSON.parse(event?.metadata?.options ?? '{}');
118
135
  } else {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gcf-common-lib",
3
3
  "description": "",
4
- "version": "0.11.1",
4
+ "version": "0.12.1",
5
5
  "publishConfig": {
6
6
  "access": "public",
7
7
  "branches": [
@@ -20,14 +20,14 @@
20
20
  "url": "https://github.com/TopTechnologies/gcf-common.git"
21
21
  },
22
22
  "dependencies": {
23
- "@google-cloud/pubsub": "^2.18.5",
24
- "@google-cloud/secret-manager": "^3.10.1",
25
- "@google-cloud/storage": "^5.18.1",
23
+ "@google-cloud/pubsub": "^2.19.0",
24
+ "@google-cloud/secret-manager": "^3.11.0",
25
+ "@google-cloud/storage": "^5.18.2",
26
26
  "lodash": "^4.17.21"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@tsconfig/node14": "^1.0.1",
30
- "@types/lodash": "^4.14.178"
30
+ "@types/lodash": "^4.14.179"
31
31
  },
32
32
  "author": "alert83@gmail.com",
33
33
  "license": "MIT"