gcf-common-lib 0.13.3 → 0.16.3
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 +28 -27
- package/index.ts +49 -35
- package/package.json +1 -1
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,42 +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
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
76
|
+
let topic;
|
|
77
|
+
let requestId;
|
|
78
|
+
let env;
|
|
79
|
+
switch (context === null || context === void 0 ? void 0 : context.eventType) {
|
|
80
|
+
case 'google.storage.object.finalize': {
|
|
81
|
+
const gsEvent = event;
|
|
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
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
case 'google.pubsub.topic.publish': {
|
|
90
|
+
const psEvent = event;
|
|
91
|
+
({ topic, requestId, env } = (_d = psEvent === null || psEvent === void 0 ? void 0 : psEvent.attributes) !== null && _d !== void 0 ? _d : {});
|
|
92
|
+
break;
|
|
84
93
|
}
|
|
85
94
|
}
|
|
86
|
-
|
|
87
|
-
const psEvent = event;
|
|
88
|
-
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;
|
|
89
|
-
}
|
|
90
|
-
if (topicName && !(0, isEmpty_1.default)(topicName)) {
|
|
91
|
-
const topic = exports.pubSub.topic(topicName);
|
|
92
|
-
yield topic.setMetadata({ labels: { date: topicName.split('__')[1] } });
|
|
93
|
-
return topic;
|
|
94
|
-
}
|
|
95
|
+
return { topic, requestId, env };
|
|
95
96
|
});
|
|
96
97
|
}
|
|
97
98
|
static getOptions(event, context) {
|
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
|
|
|
@@ -40,8 +52,8 @@ export type TContext = {
|
|
|
40
52
|
eventType: 'google.storage.object.finalize' | 'google.pubsub.topic.publish';
|
|
41
53
|
resource: {
|
|
42
54
|
service: 'storage.googleapis.com' | 'pubsub.googleapis.com';
|
|
55
|
+
type: 'storage#object' | 'type.googleapis.com/google.pubsub.v1.PubsubMessage';
|
|
43
56
|
name: string;
|
|
44
|
-
type: string; // 'storage#object' | 'type.googleapis.com/google.pubsub.v1.PubsubMessage'
|
|
45
57
|
}
|
|
46
58
|
}
|
|
47
59
|
|
|
@@ -76,9 +88,9 @@ export class GcfCommon {
|
|
|
76
88
|
timeoutAfter(timeout),
|
|
77
89
|
handler(event, context),
|
|
78
90
|
])
|
|
79
|
-
.then(async (res:
|
|
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';
|
|
@@ -91,20 +103,23 @@ export class GcfCommon {
|
|
|
91
103
|
};
|
|
92
104
|
await this.publish(event, context, response, {error: '1'}).catch(noop);
|
|
93
105
|
throw err;
|
|
94
|
-
})
|
|
95
|
-
;
|
|
106
|
+
});
|
|
96
107
|
}
|
|
97
108
|
|
|
98
|
-
static async publish(event: TEvent, context: TContext, json
|
|
99
|
-
const topic = await this.getTopic(event, context);
|
|
100
|
-
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);
|
|
101
112
|
|
|
102
|
-
if (topic) {
|
|
103
|
-
return
|
|
104
|
-
json,
|
|
113
|
+
if (!isEmpty(topic)) {
|
|
114
|
+
return pubSub.topic(topic as string).publishMessage({
|
|
115
|
+
json: json ?? {},
|
|
105
116
|
attributes: {
|
|
106
|
-
...
|
|
107
|
-
|
|
117
|
+
...fromPairs(
|
|
118
|
+
Object.entries(attributes ?? {})
|
|
119
|
+
.map(([k, v]) => [k, '' + v])
|
|
120
|
+
),
|
|
121
|
+
env: env ?? '',
|
|
122
|
+
requestId: requestId ?? '',
|
|
108
123
|
type: 'response',
|
|
109
124
|
response: '1',
|
|
110
125
|
},
|
|
@@ -115,29 +130,28 @@ export class GcfCommon {
|
|
|
115
130
|
static async getTopic(event: TEvent, context: TContext) {
|
|
116
131
|
|
|
117
132
|
/** t_{GUID}__{YYYY-MM-DD} */
|
|
118
|
-
let
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
133
|
+
let topic: string | undefined;
|
|
134
|
+
let requestId: string | undefined;
|
|
135
|
+
let env: string | undefined;
|
|
136
|
+
|
|
137
|
+
switch (context?.eventType) {
|
|
138
|
+
case 'google.storage.object.finalize': {
|
|
139
|
+
const gsEvent = event as TGSEvent;
|
|
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 ?? {});
|
|
144
|
+
}
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
case 'google.pubsub.topic.publish' : {
|
|
148
|
+
const psEvent = event as TPSEvent;
|
|
149
|
+
({topic, requestId, env} = psEvent?.attributes ?? {} as any);
|
|
150
|
+
break;
|
|
130
151
|
}
|
|
131
|
-
} else if (context?.eventType === 'google.pubsub.topic.publish') {
|
|
132
|
-
const psEvent = event as TPSEvent;
|
|
133
|
-
topicName = psEvent.attributes?.topic ?? (psEvent.data as any)?.topic;
|
|
134
152
|
}
|
|
135
153
|
|
|
136
|
-
|
|
137
|
-
const topic = pubSub.topic(topicName);
|
|
138
|
-
await topic.setMetadata({labels: {date: topicName.split('__')[1]}});
|
|
139
|
-
return topic;
|
|
140
|
-
}
|
|
154
|
+
return {topic, requestId, env};
|
|
141
155
|
}
|
|
142
156
|
|
|
143
157
|
static async getOptions(event: TGSEvent, context: TContext) {
|