gcf-common-lib 0.32.66 → 0.33.0
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/.github/workflows/npm-publish.yml +2 -2
- package/package.json +13 -11
- package/src/index.js +29 -44
- package/src/index.ts +1 -3
- package/src/utils.js +0 -2
- package/test/test.js +25 -0
- package/test/test.ts +26 -0
- package/.eslintrc.json +0 -49
|
@@ -15,7 +15,7 @@ jobs:
|
|
|
15
15
|
- uses: actions/checkout@v4
|
|
16
16
|
- uses: actions/setup-node@v4
|
|
17
17
|
with:
|
|
18
|
-
node-version:
|
|
18
|
+
node-version: 20
|
|
19
19
|
- run: npm ci
|
|
20
20
|
- run: npm test
|
|
21
21
|
|
|
@@ -26,7 +26,7 @@ jobs:
|
|
|
26
26
|
- uses: actions/checkout@v4
|
|
27
27
|
- uses: actions/setup-node@v4
|
|
28
28
|
with:
|
|
29
|
-
node-version:
|
|
29
|
+
node-version: 20
|
|
30
30
|
registry-url: https://registry.npmjs.org/
|
|
31
31
|
- run: npm ci
|
|
32
32
|
- run: npm publish
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gcf-common-lib",
|
|
3
3
|
"description": "",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.33.0",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
7
7
|
"branches": [
|
|
@@ -13,27 +13,28 @@
|
|
|
13
13
|
},
|
|
14
14
|
"main": "src/index",
|
|
15
15
|
"scripts": {
|
|
16
|
-
"
|
|
16
|
+
"build": "tsc",
|
|
17
|
+
"test": "node --test ./test/"
|
|
17
18
|
},
|
|
18
19
|
"repository": {
|
|
19
20
|
"type": "git",
|
|
20
21
|
"url": "https://github.com/TopTechnologies/gcf-common.git"
|
|
21
22
|
},
|
|
22
23
|
"dependencies": {
|
|
23
|
-
"@google-cloud/pubsub": "^4.
|
|
24
|
-
"@google-cloud/storage": "^7.
|
|
24
|
+
"@google-cloud/pubsub": "^4.10.0",
|
|
25
|
+
"@google-cloud/storage": "^7.15.2",
|
|
25
26
|
"@tsconfig/node20": "^20.1.4",
|
|
26
|
-
"@types/amqplib": "^0.10.
|
|
27
|
+
"@types/amqplib": "^0.10.7",
|
|
27
28
|
"@types/bluebird": "^3.5.42",
|
|
28
29
|
"@types/express": "^4.17.21",
|
|
29
|
-
"@types/lodash": "^4.17.
|
|
30
|
-
"@types/node": "^20.17.
|
|
31
|
-
"amqplib": "^0.10.
|
|
30
|
+
"@types/lodash": "^4.17.16",
|
|
31
|
+
"@types/node": "^20.17.23",
|
|
32
|
+
"amqplib": "^0.10.5",
|
|
32
33
|
"bluebird": "^3.7.2",
|
|
33
34
|
"lodash": "^4.17.21",
|
|
34
35
|
"moment": "^2.30.1",
|
|
35
|
-
"mongodb": "^6.
|
|
36
|
-
"rxjs": "^7.8.
|
|
36
|
+
"mongodb": "^6.14.1",
|
|
37
|
+
"rxjs": "^7.8.2"
|
|
37
38
|
},
|
|
38
39
|
"devDependencies": {
|
|
39
40
|
"@eslint/js": "^9.21.0",
|
|
@@ -43,7 +44,8 @@
|
|
|
43
44
|
"eslint-plugin-unicorn": "^57.0.0",
|
|
44
45
|
"globals": "^15.15.0",
|
|
45
46
|
"prettier": "^3.5.2",
|
|
46
|
-
"typescript-eslint": "^8.25.0"
|
|
47
|
+
"typescript-eslint": "^8.25.0",
|
|
48
|
+
"typescript": "^5.3.3"
|
|
47
49
|
},
|
|
48
50
|
"author": "alert83@gmail.com",
|
|
49
51
|
"license": ""
|
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
|
@@ -75,8 +75,6 @@ export class GcfCommon {
|
|
|
75
75
|
// );
|
|
76
76
|
// console.timeEnd('safeGetAttributes');
|
|
77
77
|
|
|
78
|
-
console.log('response:', payload);
|
|
79
|
-
|
|
80
78
|
const { topic, exchange, queue, consumer_id, request_id, app_id, env } = this.getMetadataOrAttribute(payload);
|
|
81
79
|
|
|
82
80
|
//
|
|
@@ -168,7 +166,7 @@ export class GcfCommon {
|
|
|
168
166
|
metadataOrAttribute = this.getRequestMessage(payload.request)?.attributes;
|
|
169
167
|
}
|
|
170
168
|
|
|
171
|
-
console.log('metadataOrAttribute:', metadataOrAttribute);
|
|
169
|
+
// console.log('metadataOrAttribute:', metadataOrAttribute);
|
|
172
170
|
|
|
173
171
|
return metadataOrAttribute ?? {};
|
|
174
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
|
+
});
|
package/.eslintrc.json
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"root": true,
|
|
3
|
-
"env": {
|
|
4
|
-
"es2021": true,
|
|
5
|
-
"commonjs": true
|
|
6
|
-
},
|
|
7
|
-
"parser": "@typescript-eslint/parser",
|
|
8
|
-
"parserOptions": {
|
|
9
|
-
"ecmaVersion": "latest",
|
|
10
|
-
"sourceType": "module",
|
|
11
|
-
"tsconfigRootDir": ".",
|
|
12
|
-
"project": [
|
|
13
|
-
"./tsconfig.json"
|
|
14
|
-
]
|
|
15
|
-
},
|
|
16
|
-
"plugins": [
|
|
17
|
-
"@typescript-eslint",
|
|
18
|
-
"import",
|
|
19
|
-
"promise",
|
|
20
|
-
"rxjs",
|
|
21
|
-
"lodash",
|
|
22
|
-
"unicorn"
|
|
23
|
-
],
|
|
24
|
-
"extends": [
|
|
25
|
-
"eslint:recommended",
|
|
26
|
-
"plugin:@typescript-eslint/recommended",
|
|
27
|
-
"plugin:import/recommended",
|
|
28
|
-
"plugin:import/typescript",
|
|
29
|
-
"plugin:promise/recommended",
|
|
30
|
-
"plugin:rxjs/recommended",
|
|
31
|
-
"plugin:lodash/recommended",
|
|
32
|
-
"plugin:unicorn/recommended"
|
|
33
|
-
],
|
|
34
|
-
"settings": {
|
|
35
|
-
"import/resolver": {
|
|
36
|
-
"typescript": true,
|
|
37
|
-
"node": true
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
"rules": {
|
|
41
|
-
"@typescript-eslint/no-explicit-any": "off",
|
|
42
|
-
"@typescript-eslint/no-unused-vars": "off",
|
|
43
|
-
"@typescript-eslint/no-empty-function": "off",
|
|
44
|
-
"@typescript-eslint/no-empty-interface": "off",
|
|
45
|
-
"lodash/prefer-lodash-method": "off",
|
|
46
|
-
"rxjs/no-implicit-any-catch": "off",
|
|
47
|
-
"unicorn/prevent-abbreviations": "off"
|
|
48
|
-
}
|
|
49
|
-
}
|