gcf-common-lib 0.13.2 → 0.15.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 +27 -30
- package/index.ts +32 -35
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -50,54 +50,51 @@ class GcfCommon {
|
|
|
50
50
|
stack: err.stack,
|
|
51
51
|
},
|
|
52
52
|
};
|
|
53
|
-
|
|
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);
|
|
53
|
+
yield this.publish(event, context, response, { error: '1' }).catch(noop_1.default);
|
|
60
54
|
throw err;
|
|
61
55
|
}));
|
|
62
56
|
});
|
|
63
57
|
}
|
|
64
58
|
static publish(event, context, json, attributes) {
|
|
65
|
-
var _a, _b, _c;
|
|
66
59
|
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
-
const topic = yield this.getTopic(event, context);
|
|
68
|
-
console.log('publish:', topic === null || topic === void 0 ? void 0 : topic.name, json);
|
|
60
|
+
const { topic, requestId } = yield this.getTopic(event, context);
|
|
61
|
+
console.log('publish:', topic === null || topic === void 0 ? void 0 : topic.name, json, attributes);
|
|
69
62
|
if (topic) {
|
|
70
|
-
return
|
|
63
|
+
return topic.publishMessage({
|
|
71
64
|
json,
|
|
72
|
-
attributes: Object.assign(Object.assign({}, attributes), { requestId:
|
|
65
|
+
attributes: Object.assign(Object.assign({}, attributes), { requestId: requestId !== null && requestId !== void 0 ? requestId : '', type: 'response', response: '1' }),
|
|
73
66
|
});
|
|
74
67
|
}
|
|
75
68
|
});
|
|
76
69
|
}
|
|
77
70
|
static getTopic(event, context) {
|
|
78
|
-
var _a, _b, _c, _d, _e, _f;
|
|
71
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
79
72
|
return __awaiter(this, void 0, void 0, function* () {
|
|
80
73
|
/** t_{GUID}__{YYYY-MM-DD} */
|
|
81
74
|
let topicName;
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
topicName
|
|
89
|
-
|
|
75
|
+
let requestId;
|
|
76
|
+
switch (context === null || context === void 0 ? void 0 : context.eventType) {
|
|
77
|
+
case 'google.storage.object.finalize': {
|
|
78
|
+
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;
|
|
86
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
case 'google.pubsub.topic.publish': {
|
|
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;
|
|
93
|
+
break;
|
|
90
94
|
}
|
|
91
95
|
}
|
|
92
|
-
|
|
93
|
-
|
|
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;
|
|
95
|
-
}
|
|
96
|
-
if (topicName && !(0, isEmpty_1.default)(topicName)) {
|
|
97
|
-
const topic = exports.pubSub.topic(topicName);
|
|
98
|
-
yield topic.setMetadata({ labels: { date: topicName.split('__')[1] } });
|
|
99
|
-
return topic;
|
|
100
|
-
}
|
|
96
|
+
console.log('topic:', topicName);
|
|
97
|
+
return !(0, isEmpty_1.default)(topicName) ? { topic: exports.pubSub.topic(topicName), requestId } : {};
|
|
101
98
|
});
|
|
102
99
|
}
|
|
103
100
|
static getOptions(event, context) {
|
package/index.ts
CHANGED
|
@@ -40,8 +40,8 @@ export type TContext = {
|
|
|
40
40
|
eventType: 'google.storage.object.finalize' | 'google.pubsub.topic.publish';
|
|
41
41
|
resource: {
|
|
42
42
|
service: 'storage.googleapis.com' | 'pubsub.googleapis.com';
|
|
43
|
+
type: 'storage#object' | 'type.googleapis.com/google.pubsub.v1.PubsubMessage';
|
|
43
44
|
name: string;
|
|
44
|
-
type: string; // 'storage#object' | 'type.googleapis.com/google.pubsub.v1.PubsubMessage'
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
|
|
@@ -76,7 +76,7 @@ export class GcfCommon {
|
|
|
76
76
|
timeoutAfter(timeout),
|
|
77
77
|
handler(event, context),
|
|
78
78
|
])
|
|
79
|
-
.then(async (res:
|
|
79
|
+
.then(async (res: T | undefined) => {
|
|
80
80
|
// console.log('res:', res);
|
|
81
81
|
await this.publish(event, context, res ?? {});
|
|
82
82
|
})
|
|
@@ -89,28 +89,21 @@ export class GcfCommon {
|
|
|
89
89
|
stack: err.stack,
|
|
90
90
|
},
|
|
91
91
|
};
|
|
92
|
-
|
|
93
|
-
error: {
|
|
94
|
-
name: err.name,
|
|
95
|
-
message: `GCF [${fname}]: ${err.message}`,
|
|
96
|
-
},
|
|
97
|
-
};
|
|
98
|
-
await this.publish(event, context, response, attr).catch(noop);
|
|
92
|
+
await this.publish(event, context, response, {error: '1'}).catch(noop);
|
|
99
93
|
throw err;
|
|
100
|
-
})
|
|
101
|
-
;
|
|
94
|
+
});
|
|
102
95
|
}
|
|
103
96
|
|
|
104
97
|
static async publish(event: TEvent, context: TContext, json: TResponse, attributes?: any) {
|
|
105
|
-
const topic = await this.getTopic(event, context);
|
|
106
|
-
console.log('publish:', topic?.name, json);
|
|
98
|
+
const {topic, requestId} = await this.getTopic(event, context);
|
|
99
|
+
console.log('publish:', topic?.name, json, attributes);
|
|
107
100
|
|
|
108
101
|
if (topic) {
|
|
109
|
-
return
|
|
102
|
+
return topic.publishMessage({
|
|
110
103
|
json,
|
|
111
104
|
attributes: {
|
|
112
105
|
...attributes,
|
|
113
|
-
requestId:
|
|
106
|
+
requestId: requestId ?? '',
|
|
114
107
|
type: 'response',
|
|
115
108
|
response: '1',
|
|
116
109
|
},
|
|
@@ -121,29 +114,33 @@ export class GcfCommon {
|
|
|
121
114
|
static async getTopic(event: TEvent, context: TContext) {
|
|
122
115
|
|
|
123
116
|
/** t_{GUID}__{YYYY-MM-DD} */
|
|
124
|
-
let topicName: string
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
117
|
+
let topicName: string;
|
|
118
|
+
let requestId: string;
|
|
119
|
+
|
|
120
|
+
switch (context?.eventType) {
|
|
121
|
+
case 'google.storage.object.finalize': {
|
|
122
|
+
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;
|
|
130
|
+
}
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
case 'google.pubsub.topic.publish' : {
|
|
134
|
+
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;
|
|
137
|
+
break;
|
|
136
138
|
}
|
|
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;
|
|
140
139
|
}
|
|
141
140
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
return topic;
|
|
146
|
-
}
|
|
141
|
+
console.log('topic:', topicName);
|
|
142
|
+
|
|
143
|
+
return !isEmpty(topicName) ? {topic: pubSub.topic(topicName), requestId} : {};
|
|
147
144
|
}
|
|
148
145
|
|
|
149
146
|
static async getOptions(event: TGSEvent, context: TContext) {
|