gcf-common-lib 0.14.3 → 0.17.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 +20 -21
- package/index.ts +40 -24
- package/package.json +5 -5
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
|
|
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;
|
|
@@ -56,44 +57,42 @@ class GcfCommon {
|
|
|
56
57
|
});
|
|
57
58
|
}
|
|
58
59
|
static publish(event, context, json, attributes) {
|
|
59
|
-
var _a, _b, _c;
|
|
60
60
|
return __awaiter(this, void 0, void 0, function* () {
|
|
61
|
-
const topic = yield this.getTopic(event, context);
|
|
62
|
-
console.log('publish:', topic
|
|
63
|
-
if (topic) {
|
|
64
|
-
return topic.publishMessage({
|
|
65
|
-
json,
|
|
66
|
-
attributes: Object.assign(Object.assign({},
|
|
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' }),
|
|
67
68
|
});
|
|
68
69
|
}
|
|
69
70
|
});
|
|
70
71
|
}
|
|
71
72
|
static getTopic(event, context) {
|
|
72
|
-
var _a, _b, _c, _d
|
|
73
|
+
var _a, _b, _c, _d;
|
|
73
74
|
return __awaiter(this, void 0, void 0, function* () {
|
|
74
75
|
/** t_{GUID}__{YYYY-MM-DD} */
|
|
75
|
-
let
|
|
76
|
+
let topic;
|
|
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
|
-
|
|
80
|
-
if (!
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
topicName = (_c = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _c === void 0 ? void 0 : _c.topic;
|
|
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 : {});
|
|
84
86
|
}
|
|
85
87
|
break;
|
|
86
88
|
}
|
|
87
89
|
case 'google.pubsub.topic.publish': {
|
|
88
90
|
const psEvent = event;
|
|
89
|
-
|
|
91
|
+
({ topic, requestId, env } = (_d = psEvent === null || psEvent === void 0 ? void 0 : psEvent.attributes) !== null && _d !== void 0 ? _d : {});
|
|
90
92
|
break;
|
|
91
93
|
}
|
|
92
94
|
}
|
|
93
|
-
|
|
94
|
-
if (!(0, isEmpty_1.default)(topicName)) {
|
|
95
|
-
return exports.pubSub.topic(topicName);
|
|
96
|
-
}
|
|
95
|
+
return { topic, requestId, env };
|
|
97
96
|
});
|
|
98
97
|
}
|
|
99
98
|
static getOptions(event, context) {
|
|
@@ -121,7 +120,7 @@ class GcfCommon {
|
|
|
121
120
|
}
|
|
122
121
|
static getSecrets(names, versions) {
|
|
123
122
|
return __awaiter(this, void 0, void 0, function* () {
|
|
124
|
-
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(); })));
|
|
125
124
|
});
|
|
126
125
|
}
|
|
127
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: {
|
|
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?: {
|
|
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,16 +106,20 @@ export class GcfCommon {
|
|
|
94
106
|
});
|
|
95
107
|
}
|
|
96
108
|
|
|
97
|
-
static async publish(event: TEvent, context: TContext, json
|
|
98
|
-
const topic = await this.getTopic(event, context);
|
|
99
|
-
console.log('publish:', topic
|
|
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
|
-
...
|
|
106
|
-
|
|
117
|
+
...fromPairs(
|
|
118
|
+
Object.entries(attributes ?? {})
|
|
119
|
+
.map(([k, v]) => [k, '' + v])
|
|
120
|
+
),
|
|
121
|
+
env: env ?? '',
|
|
122
|
+
requestId: requestId ?? '',
|
|
107
123
|
type: 'response',
|
|
108
124
|
response: '1',
|
|
109
125
|
},
|
|
@@ -114,31 +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
|
|
133
|
+
let topic: string | undefined;
|
|
134
|
+
let requestId: string | undefined;
|
|
135
|
+
let env: string | undefined;
|
|
118
136
|
|
|
119
137
|
switch (context?.eventType) {
|
|
120
138
|
case 'google.storage.object.finalize': {
|
|
121
139
|
const gsEvent = event as TGSEvent;
|
|
122
|
-
|
|
123
|
-
if (!
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
topicName = meta?.metadata?.topic;
|
|
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 ?? {});
|
|
127
144
|
}
|
|
128
145
|
break;
|
|
129
146
|
}
|
|
130
147
|
case 'google.pubsub.topic.publish' : {
|
|
131
148
|
const psEvent = event as TPSEvent;
|
|
132
|
-
|
|
149
|
+
({topic, requestId, env} = psEvent?.attributes ?? {} as any);
|
|
133
150
|
break;
|
|
134
151
|
}
|
|
135
152
|
}
|
|
136
153
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (!isEmpty(topicName)) {
|
|
140
|
-
return pubSub.topic(topicName)
|
|
141
|
-
}
|
|
154
|
+
return {topic, requestId, env};
|
|
142
155
|
}
|
|
143
156
|
|
|
144
157
|
static async getOptions(event: TGSEvent, context: TContext) {
|
|
@@ -160,6 +173,9 @@ export class GcfCommon {
|
|
|
160
173
|
}
|
|
161
174
|
|
|
162
175
|
static async getSecrets(names: string[], versions?: string[]) {
|
|
163
|
-
return Promise.all(
|
|
176
|
+
return Promise.all(
|
|
177
|
+
names.map(async (name, idx) =>
|
|
178
|
+
this.getSecret(name, versions?.[idx]).catch())
|
|
179
|
+
);
|
|
164
180
|
}
|
|
165
181
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gcf-common-lib",
|
|
3
3
|
"description": "",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.17.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.
|
|
24
|
-
"@google-cloud/secret-manager": "^3.
|
|
25
|
-
"@google-cloud/storage": "^5.
|
|
23
|
+
"@google-cloud/pubsub": "^2.19.4",
|
|
24
|
+
"@google-cloud/secret-manager": "^3.12.0",
|
|
25
|
+
"@google-cloud/storage": "^5.19.4",
|
|
26
26
|
"lodash": "^4.17.21"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@tsconfig/node14": "^1.0.1",
|
|
30
|
-
"@types/lodash": "^4.14.
|
|
30
|
+
"@types/lodash": "^4.14.182"
|
|
31
31
|
},
|
|
32
32
|
"author": "alert83@gmail.com",
|
|
33
33
|
"license": "MIT"
|