gcf-common-lib 0.15.3 → 0.18.0

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.
package/index.js CHANGED
@@ -16,6 +16,7 @@ exports.GcfCommon = exports.storage = exports.pubSub = void 0;
16
16
  const pubsub_1 = require("@google-cloud/pubsub");
17
17
  const secret_manager_1 = require("@google-cloud/secret-manager");
18
18
  const storage_1 = require("@google-cloud/storage");
19
+ const fromPairs_1 = __importDefault(require("lodash/fromPairs"));
19
20
  const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
20
21
  const noop_1 = __importDefault(require("lodash/noop"));
21
22
  const utils_1 = require("./utils");
@@ -38,7 +39,7 @@ class GcfCommon {
38
39
  ])
39
40
  .then((res) => __awaiter(this, void 0, void 0, function* () {
40
41
  // console.log('res:', res);
41
- yield this.publish(event, context, res !== null && res !== void 0 ? res : {});
42
+ yield this.publish(event, context, res);
42
43
  }))
43
44
  .catch((err) => __awaiter(this, void 0, void 0, function* () {
44
45
  var _a, _b;
@@ -57,44 +58,41 @@ class GcfCommon {
57
58
  }
58
59
  static publish(event, context, json, attributes) {
59
60
  return __awaiter(this, void 0, void 0, function* () {
60
- const { topic, requestId } = yield this.getTopic(event, context);
61
- console.log('publish:', topic === null || topic === void 0 ? void 0 : topic.name, json, attributes);
62
- if (topic) {
63
- return topic.publishMessage({
64
- json,
65
- attributes: Object.assign(Object.assign({}, attributes), { requestId: requestId !== null && requestId !== void 0 ? requestId : '', type: 'response', response: '1' }),
61
+ const { topic, requestId, env } = yield this.getTopic(event, context);
62
+ console.log('publish:', topic, env, json, attributes);
63
+ if (!(0, isEmpty_1.default)(topic)) {
64
+ return exports.pubSub.topic(topic).publishMessage({
65
+ json: json !== null && json !== void 0 ? json : {},
66
+ attributes: Object.assign(Object.assign({}, (0, fromPairs_1.default)(Object.entries(attributes !== null && attributes !== void 0 ? attributes : {})
67
+ .map(([k, v]) => [k, '' + v]))), { env: env !== null && env !== void 0 ? env : '', requestId: requestId !== null && requestId !== void 0 ? requestId : '', type: 'response', response: '1' }),
66
68
  });
67
69
  }
68
70
  });
69
71
  }
70
72
  static getTopic(event, context) {
71
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
73
+ var _a, _b, _c, _d;
72
74
  return __awaiter(this, void 0, void 0, function* () {
73
75
  /** t_{GUID}__{YYYY-MM-DD} */
74
- let topicName;
76
+ let topic;
75
77
  let requestId;
78
+ let env;
76
79
  switch (context === null || context === void 0 ? void 0 : context.eventType) {
77
80
  case 'google.storage.object.finalize': {
78
81
  const gsEvent = event;
79
- topicName = (_a = gsEvent === null || gsEvent === void 0 ? void 0 : gsEvent.metadata) === null || _a === void 0 ? void 0 : _a.topic;
80
- requestId = (_b = gsEvent === null || gsEvent === void 0 ? void 0 : gsEvent.metadata) === null || _b === void 0 ? void 0 : _b.requestId;
81
- if (!topicName && ((_c = context === null || context === void 0 ? void 0 : context.resource) === null || _c === void 0 ? void 0 : _c.type) === 'storage#object') {
82
- const file = exports.storage.bucket(gsEvent.bucket).file(gsEvent.name);
83
- const [meta] = yield file.getMetadata();
84
- topicName = (_d = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _d === void 0 ? void 0 : _d.topic;
85
- requestId = (_e = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _e === void 0 ? void 0 : _e.requestId;
82
+ ({ topic, requestId, env } = (_a = gsEvent === null || gsEvent === void 0 ? void 0 : gsEvent.metadata) !== null && _a !== void 0 ? _a : {});
83
+ if (!topic && ((_b = context === null || context === void 0 ? void 0 : context.resource) === null || _b === void 0 ? void 0 : _b.type) === 'storage#object') {
84
+ const [meta] = yield exports.storage.bucket(gsEvent.bucket).file(gsEvent.name).getMetadata();
85
+ ({ topic, requestId, env } = (_c = meta === null || meta === void 0 ? void 0 : meta.metadata) !== null && _c !== void 0 ? _c : {});
86
86
  }
87
87
  break;
88
88
  }
89
89
  case 'google.pubsub.topic.publish': {
90
90
  const psEvent = event;
91
- topicName = (_g = (_f = psEvent.attributes) === null || _f === void 0 ? void 0 : _f.topic) !== null && _g !== void 0 ? _g : (_h = psEvent.data) === null || _h === void 0 ? void 0 : _h.topic;
92
- requestId = (_k = (_j = psEvent.attributes) === null || _j === void 0 ? void 0 : _j.requestId) !== null && _k !== void 0 ? _k : (_l = psEvent.data) === null || _l === void 0 ? void 0 : _l.requestId;
91
+ ({ topic, requestId, env } = (_d = psEvent === null || psEvent === void 0 ? void 0 : psEvent.attributes) !== null && _d !== void 0 ? _d : {});
93
92
  break;
94
93
  }
95
94
  }
96
- console.log('topic:', topicName);
97
- return !(0, isEmpty_1.default)(topicName) ? { topic: exports.pubSub.topic(topicName), requestId } : {};
95
+ return { topic, requestId, env };
98
96
  });
99
97
  }
100
98
  static getOptions(event, context) {
@@ -122,7 +120,7 @@ class GcfCommon {
122
120
  }
123
121
  static getSecrets(names, versions) {
124
122
  return __awaiter(this, void 0, void 0, function* () {
125
- return Promise.all(names.map((name, idx) => __awaiter(this, void 0, void 0, function* () { return this.getSecret(name, versions === null || versions === void 0 ? void 0 : versions[idx]); })));
123
+ return Promise.all(names.map((name, idx) => __awaiter(this, void 0, void 0, function* () { return this.getSecret(name, versions === null || versions === void 0 ? void 0 : versions[idx]).catch(); })));
126
124
  });
127
125
  }
128
126
  }
package/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import {PubSub} from "@google-cloud/pubsub";
2
2
  import {SecretManagerServiceClient} from "@google-cloud/secret-manager";
3
3
  import {File, Storage} from "@google-cloud/storage";
4
+ import fromPairs from "lodash/fromPairs";
4
5
  import isEmpty from "lodash/isEmpty";
5
6
  import noop from "lodash/noop";
6
7
  import {timeoutAfter} from "./utils";
@@ -15,7 +16,13 @@ export type TGSEvent = {
15
16
  kind: string; // 'storage#object'
16
17
  md5Hash: string;
17
18
  mediaLink: string;
18
- metadata: { [key: string]: string, topic: string, options?: any };
19
+ metadata: {
20
+ // [key: string]: string,
21
+ topic: string,
22
+ requestId: string,
23
+ env: string,
24
+ options?: any,
25
+ };
19
26
  metageneration: string;
20
27
  name: string;
21
28
  selfLink: string;
@@ -28,7 +35,12 @@ export type TGSEvent = {
28
35
 
29
36
  export type TPSEvent = {
30
37
  '@type': string; // 'type.googleapis.com/google.pubsub.v1.PubsubMessage'
31
- attributes?: { [k: string]: any };
38
+ attributes?: {
39
+ // [k: string]: string,
40
+ topic: string,
41
+ requestId: string,
42
+ env: string,
43
+ };
32
44
  data?: string | { [k: string]: any };
33
45
  }
34
46
 
@@ -78,7 +90,7 @@ export class GcfCommon {
78
90
  ])
79
91
  .then(async (res: T | undefined) => {
80
92
  // console.log('res:', res);
81
- await this.publish(event, context, res ?? {});
93
+ await this.publish(event, context, res);
82
94
  })
83
95
  .catch(async (err: Error) => {
84
96
  const fname = process?.env?.K_SERVICE ?? 'UNKNOWN';
@@ -94,15 +106,19 @@ export class GcfCommon {
94
106
  });
95
107
  }
96
108
 
97
- static async publish(event: TEvent, context: TContext, json: TResponse, attributes?: any) {
98
- const {topic, requestId} = await this.getTopic(event, context);
99
- console.log('publish:', topic?.name, json, attributes);
109
+ static async publish(event: TEvent, context: TContext, json?: TResponse, attributes?: { [k: string]: any }) {
110
+ const {topic, requestId, env} = await this.getTopic(event, context);
111
+ console.log('publish:', topic, env, json, attributes);
100
112
 
101
- if (topic) {
102
- return topic.publishMessage({
103
- json,
113
+ if (!isEmpty(topic)) {
114
+ return pubSub.topic(topic as string).publishMessage({
115
+ json: json ?? {},
104
116
  attributes: {
105
- ...attributes,
117
+ ...fromPairs(
118
+ Object.entries(attributes ?? {})
119
+ .map(([k, v]) => [k, '' + v])
120
+ ),
121
+ env: env ?? '',
106
122
  requestId: requestId ?? '',
107
123
  type: 'response',
108
124
  response: '1',
@@ -114,33 +130,28 @@ export class GcfCommon {
114
130
  static async getTopic(event: TEvent, context: TContext) {
115
131
 
116
132
  /** t_{GUID}__{YYYY-MM-DD} */
117
- let topicName: string;
118
- let requestId: string;
133
+ let topic: string | undefined;
134
+ let requestId: string | undefined;
135
+ let env: string | undefined;
119
136
 
120
137
  switch (context?.eventType) {
121
138
  case 'google.storage.object.finalize': {
122
139
  const gsEvent = event as TGSEvent;
123
- topicName = gsEvent?.metadata?.topic;
124
- requestId = gsEvent?.metadata?.requestId;
125
- if (!topicName && context?.resource?.type === 'storage#object') {
126
- const file: File = storage.bucket(gsEvent.bucket).file(gsEvent.name);
127
- const [meta] = await file.getMetadata();
128
- topicName = meta?.metadata?.topic;
129
- requestId = meta?.metadata?.requestId;
140
+ ({topic, requestId, env} = gsEvent?.metadata ?? {});
141
+ if (!topic && context?.resource?.type === 'storage#object') {
142
+ const [meta] = await storage.bucket(gsEvent.bucket).file(gsEvent.name).getMetadata();
143
+ ({topic, requestId, env} = meta?.metadata ?? {});
130
144
  }
131
145
  break;
132
146
  }
133
147
  case 'google.pubsub.topic.publish' : {
134
148
  const psEvent = event as TPSEvent;
135
- topicName = psEvent.attributes?.topic ?? (psEvent.data as any)?.topic;
136
- requestId = psEvent.attributes?.requestId ?? (psEvent.data as any)?.requestId;
149
+ ({topic, requestId, env} = psEvent?.attributes ?? {} as any);
137
150
  break;
138
151
  }
139
152
  }
140
153
 
141
- console.log('topic:', topicName);
142
-
143
- return !isEmpty(topicName) ? {topic: pubSub.topic(topicName), requestId} : {};
154
+ return {topic, requestId, env};
144
155
  }
145
156
 
146
157
  static async getOptions(event: TGSEvent, context: TContext) {
@@ -162,6 +173,9 @@ export class GcfCommon {
162
173
  }
163
174
 
164
175
  static async getSecrets(names: string[], versions?: string[]) {
165
- return Promise.all(names.map(async (name, idx) => this.getSecret(name, versions?.[idx])));
176
+ return Promise.all(
177
+ names.map(async (name, idx) =>
178
+ this.getSecret(name, versions?.[idx]).catch())
179
+ );
166
180
  }
167
181
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gcf-common-lib",
3
3
  "description": "",
4
- "version": "0.15.3",
4
+ "version": "0.18.0",
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.19.0",
24
- "@google-cloud/secret-manager": "^3.11.0",
25
- "@google-cloud/storage": "^5.18.2",
23
+ "@google-cloud/pubsub": "^3.1.0",
24
+ "@google-cloud/secret-manager": "^4.0.0",
25
+ "@google-cloud/storage": "^6.3.0",
26
26
  "lodash": "^4.17.21"
27
27
  },
28
28
  "devDependencies": {
29
- "@tsconfig/node14": "^1.0.1",
30
- "@types/lodash": "^4.14.179"
29
+ "@tsconfig/node14": "^1.0.3",
30
+ "@types/lodash": "^4.14.182"
31
31
  },
32
32
  "author": "alert83@gmail.com",
33
33
  "license": "MIT"
package/tsconfig.json CHANGED
@@ -2,7 +2,6 @@
2
2
  "$schema": "http://json.schemastore.org/tsconfig",
3
3
  "extends": "@tsconfig/node14/tsconfig.json",
4
4
  "compilerOptions": {
5
- "moduleResolution": "node",
6
5
  "target": "ES6",
7
6
  "lib": [
8
7
  "ES2018",