gcf-common-lib 0.32.65 → 0.32.67
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 +4 -2
- package/src/index.js +29 -44
- package/src/index.ts +3 -3
- package/src/utils.js +0 -2
- package/test/test.js +25 -0
- package/test/test.ts +26 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gcf-common-lib",
|
|
3
3
|
"description": "",
|
|
4
|
-
"version": "0.32.
|
|
4
|
+
"version": "0.32.67",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
7
7
|
"branches": [
|
|
@@ -13,7 +13,9 @@
|
|
|
13
13
|
},
|
|
14
14
|
"main": "src/index",
|
|
15
15
|
"scripts": {
|
|
16
|
-
"
|
|
16
|
+
"postinstall": "npm run build && npm run test",
|
|
17
|
+
"build": "tsc",
|
|
18
|
+
"test": "node --test ./test/"
|
|
17
19
|
},
|
|
18
20
|
"repository": {
|
|
19
21
|
"type": "git",
|
package/src/index.js
CHANGED
|
@@ -54,43 +54,19 @@ class GcfCommon {
|
|
|
54
54
|
assertOptions: { durable: true, autoDelete: true, expires: (0, utils_1.ms)({ d: 1 }) },
|
|
55
55
|
publishOptions: { persistent: true },
|
|
56
56
|
};
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
* @param {!TContext} context Metadata for the event.
|
|
61
|
-
* @param handler
|
|
62
|
-
* @param timeoutSec Seconds
|
|
63
|
-
*/
|
|
64
|
-
static async process(event, context, handler = rxjs_1.identity, timeoutSec = 535) {
|
|
65
|
-
const asyncHandler = async (_event, _context) => await handler(_event, _context);
|
|
66
|
-
return (0, rxjs_1.firstValueFrom)((0, rxjs_1.defer)(() => (0, rxjs_1.from)(asyncHandler(event, context))).pipe((0, rxjs_1.first)(), (0, rxjs_1.timeout)({ first: (0, utils_1.ms)({ s: timeoutSec }) })))
|
|
57
|
+
static async process(payload, handler, timeoutSec = 535) {
|
|
58
|
+
const asyncHandler = async (p) => await handler(p);
|
|
59
|
+
return (0, rxjs_1.firstValueFrom)((0, rxjs_1.defer)(() => (0, rxjs_1.from)(asyncHandler({ ...payload }))).pipe((0, rxjs_1.first)(), (0, rxjs_1.timeout)({ first: (0, utils_1.ms)({ s: timeoutSec }) })))
|
|
67
60
|
.then(async (res) => {
|
|
68
61
|
// console.log('res:', res);
|
|
69
|
-
await this.response(
|
|
62
|
+
await this.response({ ...payload }, res).catch(noop_1.default);
|
|
70
63
|
return res;
|
|
71
64
|
})
|
|
72
65
|
.catch(async (error) => {
|
|
73
|
-
await this.response(
|
|
66
|
+
await this.response({ ...payload }, GcfCommon.buildResponse(error), { error: '1' }).catch(noop_1.default);
|
|
74
67
|
throw error;
|
|
75
68
|
});
|
|
76
69
|
}
|
|
77
|
-
// static async processOLd<T extends TResponse, E = TEvent>(
|
|
78
|
-
// event: E,
|
|
79
|
-
// context: TContext,
|
|
80
|
-
// handler: (event: E, context: TContext) => Promise<T | E> | T | E = identity,
|
|
81
|
-
// timeoutSec = 535,
|
|
82
|
-
// ) {
|
|
83
|
-
// return Promise.race([timeoutAfter(timeoutSec), handler(event, context)])
|
|
84
|
-
// .then(async res => {
|
|
85
|
-
// // console.log('res:', res);
|
|
86
|
-
// await this.response(event, context, res as T);
|
|
87
|
-
// return res;
|
|
88
|
-
// })
|
|
89
|
-
// .catch(async (error: Error) => {
|
|
90
|
-
// await this.response(event, context, GcfCommon.buildResponse(error), { error: '1' }).catch(noop);
|
|
91
|
-
// throw error;
|
|
92
|
-
// });
|
|
93
|
-
// }
|
|
94
70
|
static buildResponse(error) {
|
|
95
71
|
return {
|
|
96
72
|
error: {
|
|
@@ -100,7 +76,7 @@ class GcfCommon {
|
|
|
100
76
|
},
|
|
101
77
|
};
|
|
102
78
|
}
|
|
103
|
-
static async response(
|
|
79
|
+
static async response(payload, json, attributes) {
|
|
104
80
|
// console.time('safeGetAttributes');
|
|
105
81
|
// const { topic, exchange, queue, consumer_id, request_id, app_id, env } = await this.safeGetAttributes(
|
|
106
82
|
// event,
|
|
@@ -108,7 +84,7 @@ class GcfCommon {
|
|
|
108
84
|
// ['consumer_id', 'topic', 'exchange', 'queue'],
|
|
109
85
|
// );
|
|
110
86
|
// console.timeEnd('safeGetAttributes');
|
|
111
|
-
const { topic, exchange, queue, consumer_id, request_id, app_id, env } = this.getMetadataOrAttribute(
|
|
87
|
+
const { topic, exchange, queue, consumer_id, request_id, app_id, env } = this.getMetadataOrAttribute(payload);
|
|
112
88
|
//
|
|
113
89
|
console.time('publish');
|
|
114
90
|
if (topic && !(0, isEmpty_1.default)(topic)) {
|
|
@@ -167,26 +143,35 @@ class GcfCommon {
|
|
|
167
143
|
// request_id: metaOrAttr.request_id,
|
|
168
144
|
// } as TMetadataOrAttributes;
|
|
169
145
|
// }
|
|
170
|
-
static async getOptions(
|
|
146
|
+
static async getOptions(payload) {
|
|
171
147
|
// const { options } = await this.safeGetAttributes(event, context, ['options']);
|
|
172
|
-
const { options } = this.getMetadataOrAttribute(
|
|
148
|
+
const { options } = this.getMetadataOrAttribute(payload);
|
|
173
149
|
return (0, utils_1.safeJsonParse)(options, {});
|
|
174
150
|
}
|
|
175
|
-
static getMetadataOrAttribute(
|
|
151
|
+
static getMetadataOrAttribute(payload) {
|
|
176
152
|
let metadataOrAttribute;
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
153
|
+
if (payload?.context) {
|
|
154
|
+
switch (payload?.context?.eventType) {
|
|
155
|
+
case 'google.storage.object.finalize': {
|
|
156
|
+
const gsEvent = payload?.event;
|
|
157
|
+
metadataOrAttribute = gsEvent?.metadata;
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
case 'google.pubsub.topic.publish': {
|
|
161
|
+
const psEvent = payload?.event;
|
|
162
|
+
metadataOrAttribute = psEvent?.attributes;
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
187
165
|
}
|
|
188
166
|
}
|
|
167
|
+
else if (payload?.request) {
|
|
168
|
+
metadataOrAttribute = this.getRequestMessage(payload.request)?.attributes;
|
|
169
|
+
}
|
|
170
|
+
// console.log('metadataOrAttribute:', metadataOrAttribute);
|
|
189
171
|
return metadataOrAttribute ?? {};
|
|
190
172
|
}
|
|
173
|
+
static getRequestMessage(request) {
|
|
174
|
+
return request.body.message;
|
|
175
|
+
}
|
|
191
176
|
}
|
|
192
177
|
exports.GcfCommon = GcfCommon;
|
package/src/index.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { Request } from 'express';
|
|
|
5
5
|
import isEmpty from 'lodash/isEmpty';
|
|
6
6
|
import mapValues from 'lodash/mapValues';
|
|
7
7
|
import noop from 'lodash/noop';
|
|
8
|
-
import { defer, first, firstValueFrom, from,
|
|
8
|
+
import { defer, first, firstValueFrom, from, timeout } from 'rxjs';
|
|
9
9
|
import { AmqpHelper } from './amqp-helper';
|
|
10
10
|
import { TEvent, TGSEvent, TMetadataOrAttributes, TPayload, TPSEvent, TResponse } from './types';
|
|
11
11
|
import { ms, safeJsonParse } from './utils';
|
|
@@ -38,7 +38,7 @@ export class GcfCommon {
|
|
|
38
38
|
|
|
39
39
|
static async process<T extends TResponse, E = TEvent>(
|
|
40
40
|
payload: TPayload<E>,
|
|
41
|
-
handler: (
|
|
41
|
+
handler: (p: TPayload<E>) => Promise<any> | any,
|
|
42
42
|
timeoutSec = 535,
|
|
43
43
|
) {
|
|
44
44
|
const asyncHandler = async (p: TPayload<E>) => await handler(p);
|
|
@@ -166,7 +166,7 @@ export class GcfCommon {
|
|
|
166
166
|
metadataOrAttribute = this.getRequestMessage(payload.request)?.attributes;
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
-
console.log('metadataOrAttribute:', metadataOrAttribute);
|
|
169
|
+
// console.log('metadataOrAttribute:', metadataOrAttribute);
|
|
170
170
|
|
|
171
171
|
return metadataOrAttribute ?? {};
|
|
172
172
|
}
|
package/src/utils.js
CHANGED
|
@@ -13,7 +13,6 @@ async function delay(seconds) {
|
|
|
13
13
|
return new Promise(resolve => setTimeout(() => resolve(), seconds * 1000));
|
|
14
14
|
}
|
|
15
15
|
exports.delay = delay;
|
|
16
|
-
//
|
|
17
16
|
function ms(o) {
|
|
18
17
|
return sec(o) * 1000;
|
|
19
18
|
}
|
|
@@ -84,4 +83,3 @@ function safeJsonParse(value, fallbackValue) {
|
|
|
84
83
|
}
|
|
85
84
|
}
|
|
86
85
|
exports.safeJsonParse = safeJsonParse;
|
|
87
|
-
//
|
package/test/test.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const node_test_1 = require("node:test");
|
|
4
|
+
const src_1 = require("../src");
|
|
5
|
+
(0, node_test_1.describe)('test', () => {
|
|
6
|
+
const event = {
|
|
7
|
+
'@type': 'type.googleapis.com/google.pubsub.v1.PubsubMessage',
|
|
8
|
+
json: { foo: 'bar' },
|
|
9
|
+
attributes: { topic: 'test', env: 'test', options: '{}' },
|
|
10
|
+
};
|
|
11
|
+
const context = {
|
|
12
|
+
eventId: '1',
|
|
13
|
+
timestamp: `${Date.now()}`,
|
|
14
|
+
eventType: 'google.pubsub.topic.publish',
|
|
15
|
+
resource: {
|
|
16
|
+
name: 'test',
|
|
17
|
+
service: 'pubsub.googleapis.com',
|
|
18
|
+
type: 'type.googleapis.com/google.pubsub.v1.PubsubMessage',
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
(0, node_test_1.it)('test', async () => {
|
|
22
|
+
const res = await src_1.GcfCommon.process({ event, context }, async (p) => p.event?.data);
|
|
23
|
+
console.log(res);
|
|
24
|
+
});
|
|
25
|
+
});
|
package/test/test.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import { GcfCommon, TContext, TEvent } from '../src';
|
|
3
|
+
|
|
4
|
+
describe('test', () => {
|
|
5
|
+
const event: TEvent = {
|
|
6
|
+
'@type': 'type.googleapis.com/google.pubsub.v1.PubsubMessage',
|
|
7
|
+
json: { foo: 'bar' },
|
|
8
|
+
attributes: { topic: 'test', env: 'test', options: '{}' },
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const context: TContext = {
|
|
12
|
+
eventId: '1',
|
|
13
|
+
timestamp: `${Date.now()}`,
|
|
14
|
+
eventType: 'google.pubsub.topic.publish',
|
|
15
|
+
resource: {
|
|
16
|
+
name: 'test',
|
|
17
|
+
service: 'pubsub.googleapis.com',
|
|
18
|
+
type: 'type.googleapis.com/google.pubsub.v1.PubsubMessage',
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
it('test', async () => {
|
|
23
|
+
const res = await GcfCommon.process({ event, context }, async p => p.event?.data);
|
|
24
|
+
console.log(res);
|
|
25
|
+
});
|
|
26
|
+
});
|