gcf-common-lib 0.11.1 → 0.13.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 +30 -11
  2. package/index.ts +48 -14
  3. package/package.json +5 -5
package/index.js CHANGED
@@ -50,29 +50,48 @@ class GcfCommon {
50
50
  stack: err.stack,
51
51
  },
52
52
  };
53
- yield this.publish(event, context, response).catch(noop_1.default);
53
+ const attr = {
54
+ error: {
55
+ name: err.name,
56
+ message: `GCF [${fname}]: ${err.message}`,
57
+ },
58
+ };
59
+ yield this.publish(event, context, response, attr).catch(noop_1.default);
54
60
  throw err;
55
61
  }));
56
62
  });
57
63
  }
58
- static publish(event, context, json) {
64
+ static publish(event, context, json, attributes) {
65
+ var _a, _b, _c;
59
66
  return __awaiter(this, void 0, void 0, function* () {
60
67
  const topic = yield this.getTopic(event, context);
61
68
  console.log('publish:', topic === null || topic === void 0 ? void 0 : topic.name, json);
62
- if (topic)
63
- return yield topic.publishMessage({ json });
69
+ if (topic) {
70
+ return yield topic.publishMessage({
71
+ json,
72
+ attributes: Object.assign(Object.assign({}, attributes), { requestId: (_c = (_b = (_a = event) === null || _a === void 0 ? void 0 : _a.attributes) === null || _b === void 0 ? void 0 : _b.requestId) !== null && _c !== void 0 ? _c : undefined, type: 'response', response: '1' }),
73
+ });
74
+ }
64
75
  });
65
76
  }
66
77
  static getTopic(event, context) {
67
- var _a, _b, _c;
78
+ var _a, _b, _c, _d, _e, _f;
68
79
  return __awaiter(this, void 0, void 0, function* () {
69
80
  /** 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);
81
+ let topicName;
82
+ if ((context === null || context === void 0 ? void 0 : context.eventType) === 'google.storage.object.finalize') {
83
+ const gsEvent = event;
84
+ topicName = (_a = gsEvent === null || gsEvent === void 0 ? void 0 : gsEvent.metadata) === null || _a === void 0 ? void 0 : _a.topic;
85
+ if (!topicName && ((_b = context === null || context === void 0 ? void 0 : context.resource) === null || _b === void 0 ? void 0 : _b.type) === 'storage#object') {
86
+ const file = exports.storage.bucket(gsEvent.bucket).file(gsEvent.name);
87
+ const [meta] = yield file.getMetadata();
88
+ topicName = (_c = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _c === void 0 ? void 0 : _c.topic;
89
+ console.log('topic:', topicName);
90
+ }
91
+ }
92
+ else if ((context === null || context === void 0 ? void 0 : context.eventType) === 'google.pubsub.topic.publish') {
93
+ const psEvent = event;
94
+ topicName = (_e = (_d = psEvent.attributes) === null || _d === void 0 ? void 0 : _d.topic) !== null && _e !== void 0 ? _e : (_f = psEvent.data) === null || _f === void 0 ? void 0 : _f.topic;
76
95
  }
77
96
  if (topicName && !(0, isEmpty_1.default)(topicName)) {
78
97
  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?: { [k: string]: any };
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
 
@@ -81,28 +89,54 @@ export class GcfCommon {
81
89
  stack: err.stack,
82
90
  },
83
91
  };
84
- await this.publish(event, context, response).catch(noop);
92
+ const attr = {
93
+ error: {
94
+ name: err.name,
95
+ message: `GCF [${fname}]: ${err.message}`,
96
+ },
97
+ };
98
+ await this.publish(event, context, response, attr).catch(noop);
85
99
  throw err;
86
100
  })
87
101
  ;
88
102
  }
89
103
 
90
- static async publish(event: TEvent, context: TContext, json: TResponse) {
104
+ static async publish(event: TEvent, context: TContext, json: TResponse, attributes?: any) {
91
105
  const topic = await this.getTopic(event, context);
92
106
  console.log('publish:', topic?.name, json);
93
- if (topic) return await topic.publishMessage({json});
107
+
108
+ if (topic) {
109
+ return await topic.publishMessage({
110
+ json,
111
+ attributes: {
112
+ ...attributes,
113
+ requestId: (event as TPSEvent)?.attributes?.requestId ?? undefined,
114
+ type: 'response',
115
+ response: '1',
116
+ },
117
+ });
118
+ }
94
119
  }
95
120
 
96
121
  static async getTopic(event: TEvent, context: TContext) {
97
122
 
98
123
  /** t_{GUID}__{YYYY-MM-DD} */
99
- let topicName: string | undefined = event?.metadata?.topic;
124
+ let topicName: string | undefined;
100
125
 
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);
126
+ if (context?.eventType === 'google.storage.object.finalize') {
127
+ const gsEvent = event as TGSEvent;
128
+
129
+ topicName = gsEvent?.metadata?.topic;
130
+
131
+ if (!topicName && context?.resource?.type === 'storage#object') {
132
+ const file: File = storage.bucket(gsEvent.bucket).file(gsEvent.name);
133
+ const [meta] = await file.getMetadata();
134
+ topicName = meta?.metadata?.topic;
135
+ console.log('topic:', topicName);
136
+ }
137
+ } else if (context?.eventType === 'google.pubsub.topic.publish') {
138
+ const psEvent = event as TPSEvent;
139
+ topicName = psEvent.attributes?.topic ?? (psEvent.data as any)?.topic;
106
140
  }
107
141
 
108
142
  if (topicName && !isEmpty(topicName)) {
@@ -112,7 +146,7 @@ export class GcfCommon {
112
146
  }
113
147
  }
114
148
 
115
- static async getOptions(event: TEvent, context: TContext) {
149
+ static async getOptions(event: TGSEvent, context: TContext) {
116
150
  if (event?.metadata?.options) {
117
151
  return JSON.parse(event?.metadata?.options ?? '{}');
118
152
  } 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.13.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"