gcf-common-lib 0.13.1 → 0.14.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 +22 -26
- package/index.ts +28 -33
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -50,13 +50,7 @@ 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
|
});
|
|
@@ -65,11 +59,11 @@ class GcfCommon {
|
|
|
65
59
|
var _a, _b, _c;
|
|
66
60
|
return __awaiter(this, void 0, void 0, function* () {
|
|
67
61
|
const topic = yield this.getTopic(event, context);
|
|
68
|
-
console.log('publish:', topic === null || topic === void 0 ? void 0 : topic.name, json);
|
|
62
|
+
console.log('publish:', topic === null || topic === void 0 ? void 0 : topic.name, json, attributes);
|
|
69
63
|
if (topic) {
|
|
70
|
-
return
|
|
64
|
+
return topic.publishMessage({
|
|
71
65
|
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 :
|
|
66
|
+
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 : '', type: 'response', response: '1' }),
|
|
73
67
|
});
|
|
74
68
|
}
|
|
75
69
|
});
|
|
@@ -79,24 +73,26 @@ class GcfCommon {
|
|
|
79
73
|
return __awaiter(this, void 0, void 0, function* () {
|
|
80
74
|
/** t_{GUID}__{YYYY-MM-DD} */
|
|
81
75
|
let topicName;
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
+
if (!topicName && ((_b = context === null || context === void 0 ? void 0 : context.resource) === null || _b === void 0 ? void 0 : _b.type) === 'storage#object') {
|
|
81
|
+
const file = exports.storage.bucket(gsEvent.bucket).file(gsEvent.name);
|
|
82
|
+
const [meta] = yield file.getMetadata();
|
|
83
|
+
topicName = (_c = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _c === void 0 ? void 0 : _c.topic;
|
|
84
|
+
}
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
case 'google.pubsub.topic.publish': {
|
|
88
|
+
const psEvent = event;
|
|
89
|
+
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;
|
|
90
|
+
break;
|
|
90
91
|
}
|
|
91
92
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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;
|
|
93
|
+
console.log('topic:', topicName);
|
|
94
|
+
if (!(0, isEmpty_1.default)(topicName)) {
|
|
95
|
+
return exports.pubSub.topic(topicName);
|
|
100
96
|
}
|
|
101
97
|
});
|
|
102
98
|
}
|
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
98
|
const topic = await this.getTopic(event, context);
|
|
106
|
-
console.log('publish:', topic?.name, json);
|
|
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: (event as TPSEvent)?.attributes?.requestId ??
|
|
106
|
+
requestId: (event as TPSEvent)?.attributes?.requestId ?? '',
|
|
114
107
|
type: 'response',
|
|
115
108
|
response: '1',
|
|
116
109
|
},
|
|
@@ -121,28 +114,30 @@ 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
|
+
|
|
119
|
+
switch (context?.eventType) {
|
|
120
|
+
case 'google.storage.object.finalize': {
|
|
121
|
+
const gsEvent = event as TGSEvent;
|
|
122
|
+
topicName = gsEvent?.metadata?.topic;
|
|
123
|
+
if (!topicName && context?.resource?.type === 'storage#object') {
|
|
124
|
+
const file: File = storage.bucket(gsEvent.bucket).file(gsEvent.name);
|
|
125
|
+
const [meta] = await file.getMetadata();
|
|
126
|
+
topicName = meta?.metadata?.topic;
|
|
127
|
+
}
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
case 'google.pubsub.topic.publish' : {
|
|
131
|
+
const psEvent = event as TPSEvent;
|
|
132
|
+
topicName = psEvent.attributes?.topic ?? (psEvent.data as any)?.topic;
|
|
133
|
+
break;
|
|
136
134
|
}
|
|
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
135
|
}
|
|
141
136
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
return topic
|
|
137
|
+
console.log('topic:', topicName);
|
|
138
|
+
|
|
139
|
+
if (!isEmpty(topicName)) {
|
|
140
|
+
return pubSub.topic(topicName)
|
|
146
141
|
}
|
|
147
142
|
}
|
|
148
143
|
|