gcf-common-lib 0.25.40 → 0.25.42
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/package.json +1 -1
- package/src/index.js +13 -12
- package/src/index.ts +15 -13
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -64,9 +64,9 @@ class GcfCommon {
|
|
|
64
64
|
* @param handler
|
|
65
65
|
* @param timeout Seconds
|
|
66
66
|
*/
|
|
67
|
-
static process(event, context, handler, timeout = 535) {
|
|
67
|
+
static process(event, context, handler = (e, c) => __awaiter(this, void 0, void 0, function* () { return e; }), timeout = 535) {
|
|
68
68
|
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
-
return Promise.race([(0, utils_1.timeoutAfter)(timeout),
|
|
69
|
+
return Promise.race([(0, utils_1.timeoutAfter)(timeout), handler(event, context)])
|
|
70
70
|
.then((res) => __awaiter(this, void 0, void 0, function* () {
|
|
71
71
|
// console.log('res:', res);
|
|
72
72
|
yield this.publish(event, context, res);
|
|
@@ -96,30 +96,31 @@ class GcfCommon {
|
|
|
96
96
|
'queue',
|
|
97
97
|
]);
|
|
98
98
|
console.timeEnd('safeGetAttributes');
|
|
99
|
+
//
|
|
99
100
|
console.time('publish');
|
|
100
101
|
if (topic && !(0, isEmpty_1.default)(topic)) {
|
|
101
|
-
console.log('
|
|
102
|
-
|
|
102
|
+
console.log('send:', topic, appId, env, json, attributes);
|
|
103
|
+
yield exports.pubSub.topic(topic).publishMessage({
|
|
103
104
|
json: json !== null && json !== void 0 ? json : {},
|
|
104
105
|
attributes: Object.assign(Object.assign({}, (0, lodash_1.mapValues)(attributes !== null && attributes !== void 0 ? attributes : {}, v => '' + v)), { appId: appId !== null && appId !== void 0 ? appId : '', env: env !== null && env !== void 0 ? env : '', requestId: requestId !== null && requestId !== void 0 ? requestId : '',
|
|
105
106
|
//
|
|
106
107
|
type: 'response', response: '1' }),
|
|
107
108
|
});
|
|
108
109
|
}
|
|
109
|
-
if (queue && !(0, isEmpty_1.default)(queue)) {
|
|
110
|
-
console.log('send:', queue, appId, env, json, attributes);
|
|
111
|
-
return (0, utils_1.withAmqpCh)((ch) => __awaiter(this, void 0, void 0, function* () {
|
|
112
|
-
yield ch.assertQueue(queue, this.amqpOptions.assertOptions);
|
|
113
|
-
yield (0, utils_1.publishAmqp)(ch, undefined, queue, json !== null && json !== void 0 ? json : {}, this.amqpOptions.publishOptions);
|
|
114
|
-
}), this.amqpOptions.url);
|
|
115
|
-
}
|
|
116
110
|
if (exchange && !(0, isEmpty_1.default)(exchange)) {
|
|
117
111
|
console.log('send:', exchange, queue, appId, env, json, attributes);
|
|
118
|
-
|
|
112
|
+
yield (0, utils_1.withAmqpCh)((ch) => __awaiter(this, void 0, void 0, function* () {
|
|
119
113
|
yield ch.assertExchange(exchange, 'direct', this.amqpOptions.assertExchange);
|
|
120
114
|
yield (0, utils_1.publishAmqp)(ch, exchange, queue !== null && queue !== void 0 ? queue : '', json !== null && json !== void 0 ? json : {}, this.amqpOptions.publishOptions);
|
|
121
115
|
}), this.amqpOptions.url);
|
|
122
116
|
}
|
|
117
|
+
else if (queue && !(0, isEmpty_1.default)(queue)) {
|
|
118
|
+
console.log('send:', queue, appId, env, json, attributes);
|
|
119
|
+
yield (0, utils_1.withAmqpCh)((ch) => __awaiter(this, void 0, void 0, function* () {
|
|
120
|
+
yield ch.assertQueue(queue, this.amqpOptions.assertOptions);
|
|
121
|
+
yield (0, utils_1.publishAmqp)(ch, undefined, queue, json !== null && json !== void 0 ? json : {}, this.amqpOptions.publishOptions);
|
|
122
|
+
}), this.amqpOptions.url);
|
|
123
|
+
}
|
|
123
124
|
console.timeEnd('publish');
|
|
124
125
|
});
|
|
125
126
|
}
|
package/src/index.ts
CHANGED
|
@@ -43,10 +43,10 @@ export class GcfCommon {
|
|
|
43
43
|
static async process<T extends TResponse>(
|
|
44
44
|
event: TEvent,
|
|
45
45
|
context: TContext,
|
|
46
|
-
handler
|
|
46
|
+
handler: (event: TEvent, context: TContext) => Promise<T> = async (e, c) => e as any,
|
|
47
47
|
timeout = 535,
|
|
48
48
|
) {
|
|
49
|
-
return Promise.race([timeoutAfter(timeout),
|
|
49
|
+
return Promise.race([timeoutAfter(timeout), handler(event, context)])
|
|
50
50
|
.then(async (res: T | undefined) => {
|
|
51
51
|
// console.log('res:', res);
|
|
52
52
|
await this.publish(event, context, res);
|
|
@@ -69,18 +69,22 @@ export class GcfCommon {
|
|
|
69
69
|
|
|
70
70
|
static async publish(event: TEvent, context: TContext, json?: TResponse, attributes?: Dict<any>) {
|
|
71
71
|
console.time('safeGetAttributes');
|
|
72
|
+
|
|
72
73
|
const { topic, exchange, queue, appId, env, requestId } = await this.safeGetAttributes(event, context, [
|
|
73
74
|
'topic',
|
|
74
75
|
'exchange',
|
|
75
76
|
'queue',
|
|
76
77
|
]);
|
|
78
|
+
|
|
77
79
|
console.timeEnd('safeGetAttributes');
|
|
78
80
|
|
|
81
|
+
//
|
|
82
|
+
|
|
79
83
|
console.time('publish');
|
|
80
84
|
|
|
81
85
|
if (topic && !isEmpty(topic)) {
|
|
82
|
-
console.log('
|
|
83
|
-
|
|
86
|
+
console.log('send:', topic, appId, env, json, attributes);
|
|
87
|
+
await pubSub.topic(topic).publishMessage({
|
|
84
88
|
json: json ?? {},
|
|
85
89
|
attributes: {
|
|
86
90
|
...mapValues(attributes ?? {}, v => '' + v),
|
|
@@ -94,20 +98,18 @@ export class GcfCommon {
|
|
|
94
98
|
});
|
|
95
99
|
}
|
|
96
100
|
|
|
97
|
-
if (queue && !isEmpty(queue)) {
|
|
98
|
-
console.log('send:', queue, appId, env, json, attributes);
|
|
99
|
-
return withAmqpCh(async ch => {
|
|
100
|
-
await ch.assertQueue(queue, this.amqpOptions.assertOptions);
|
|
101
|
-
await publishAmqp(ch, undefined, queue, json ?? {}, this.amqpOptions.publishOptions);
|
|
102
|
-
}, this.amqpOptions.url as string);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
101
|
if (exchange && !isEmpty(exchange)) {
|
|
106
102
|
console.log('send:', exchange, queue, appId, env, json, attributes);
|
|
107
|
-
|
|
103
|
+
await withAmqpCh(async ch => {
|
|
108
104
|
await ch.assertExchange(exchange, 'direct', this.amqpOptions.assertExchange);
|
|
109
105
|
await publishAmqp(ch, exchange, queue ?? '', json ?? {}, this.amqpOptions.publishOptions);
|
|
110
106
|
}, this.amqpOptions.url as string);
|
|
107
|
+
} else if (queue && !isEmpty(queue)) {
|
|
108
|
+
console.log('send:', queue, appId, env, json, attributes);
|
|
109
|
+
await withAmqpCh(async ch => {
|
|
110
|
+
await ch.assertQueue(queue, this.amqpOptions.assertOptions);
|
|
111
|
+
await publishAmqp(ch, undefined, queue, json ?? {}, this.amqpOptions.publishOptions);
|
|
112
|
+
}, this.amqpOptions.url as string);
|
|
111
113
|
}
|
|
112
114
|
|
|
113
115
|
console.timeEnd('publish');
|