gcf-common-lib 0.33.0 → 0.35.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 -0
- package/eslint.config.mjs +7 -9
- package/package.json +3 -3
- package/src/amqp-helper.js +4 -4
- package/src/amqp-helper.ts +6 -6
- package/src/index.js +55 -44
- package/src/index.ts +45 -56
- package/src/mongo-lock.js +1 -1
- package/src/utils.js +9 -10
- package/tsconfig.json +3 -1
|
@@ -17,6 +17,7 @@ jobs:
|
|
|
17
17
|
with:
|
|
18
18
|
node-version: 20
|
|
19
19
|
- run: npm ci
|
|
20
|
+
# - run: npm run build
|
|
20
21
|
- run: npm test
|
|
21
22
|
|
|
22
23
|
publish-npm:
|
|
@@ -29,6 +30,7 @@ jobs:
|
|
|
29
30
|
node-version: 20
|
|
30
31
|
registry-url: https://registry.npmjs.org/
|
|
31
32
|
- run: npm ci
|
|
33
|
+
# - run: npm run build
|
|
32
34
|
- run: npm publish
|
|
33
35
|
env:
|
|
34
36
|
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
package/eslint.config.mjs
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
|
+
import { defineConfig } from 'eslint/config';
|
|
1
2
|
import globals from 'globals';
|
|
2
|
-
import
|
|
3
|
+
import js from '@eslint/js';
|
|
3
4
|
import tseslint from 'typescript-eslint';
|
|
4
5
|
import eslintPluginUnicorn from 'eslint-plugin-unicorn';
|
|
5
6
|
import pluginPromise from 'eslint-plugin-promise';
|
|
6
|
-
// import pluginLodash from 'eslint-plugin-lodash';
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
export default [
|
|
8
|
+
export default defineConfig([
|
|
10
9
|
{ files: ['**/*.{js,mjs,cjs,ts}'] },
|
|
11
10
|
{ files: ['**/*.js'], languageOptions: { sourceType: 'commonjs' } },
|
|
12
|
-
{ languageOptions: { globals: globals.node } },
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
{ files: ['**/*.{js,mjs,cjs,ts}'], languageOptions: { globals: globals.node } },
|
|
12
|
+
{ files: ['**/*.{js,mjs,cjs,ts}'], plugins: { js }, extends: ['js/recommended'] },
|
|
13
|
+
tseslint.configs.recommended,
|
|
15
14
|
eslintPluginUnicorn.configs.recommended,
|
|
16
15
|
pluginPromise.configs['flat/recommended'],
|
|
17
|
-
// pluginLodash.configs.recommended,
|
|
18
16
|
{
|
|
19
17
|
rules: {
|
|
20
18
|
'block-scoped-var': 'error',
|
|
@@ -30,4 +28,4 @@ export default [
|
|
|
30
28
|
// 'rxjs/no-implicit-any-catch': 'off',
|
|
31
29
|
},
|
|
32
30
|
},
|
|
33
|
-
];
|
|
31
|
+
]);
|
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.35.0",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
7
7
|
"branches": [
|
|
@@ -23,12 +23,10 @@
|
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@google-cloud/pubsub": "^4.10.0",
|
|
25
25
|
"@google-cloud/storage": "^7.15.2",
|
|
26
|
-
"@tsconfig/node20": "^20.1.4",
|
|
27
26
|
"@types/amqplib": "^0.10.7",
|
|
28
27
|
"@types/bluebird": "^3.5.42",
|
|
29
28
|
"@types/express": "^4.17.21",
|
|
30
29
|
"@types/lodash": "^4.17.16",
|
|
31
|
-
"@types/node": "^20.17.23",
|
|
32
30
|
"amqplib": "^0.10.5",
|
|
33
31
|
"bluebird": "^3.7.2",
|
|
34
32
|
"lodash": "^4.17.21",
|
|
@@ -37,6 +35,8 @@
|
|
|
37
35
|
"rxjs": "^7.8.2"
|
|
38
36
|
},
|
|
39
37
|
"devDependencies": {
|
|
38
|
+
"@tsconfig/node20": "^20.1.4",
|
|
39
|
+
"@types/node": "^20.17.23",
|
|
40
40
|
"@eslint/js": "^9.21.0",
|
|
41
41
|
"eslint": "^9.21.0",
|
|
42
42
|
"eslint-plugin-lodash": "^8.0.0",
|
package/src/amqp-helper.js
CHANGED
|
@@ -13,15 +13,15 @@ exports.AmqpHelper = {
|
|
|
13
13
|
const amqpConn = await (0, amqplib_1.connect)(url);
|
|
14
14
|
amqpConn.on('close', () => console.info('Amqp connection closed!'));
|
|
15
15
|
return amqpConn;
|
|
16
|
-
})().disposer((
|
|
16
|
+
})().disposer((channelModel, promise) => channelModel.close());
|
|
17
17
|
}
|
|
18
|
-
return bluebird_1.default.using(withDisposer(),
|
|
18
|
+
return bluebird_1.default.using(withDisposer(), channelModel => fn(channelModel));
|
|
19
19
|
},
|
|
20
20
|
async withAmqpCh(fn, url, useConfirmChannel = false, prefetch = 1) {
|
|
21
|
-
return exports.AmqpHelper.withAmqpConn(async (
|
|
21
|
+
return exports.AmqpHelper.withAmqpConn(async (channelModel) => {
|
|
22
22
|
function withDisposer() {
|
|
23
23
|
return bluebird_1.default.method(async () => {
|
|
24
|
-
const ch = useConfirmChannel ? await
|
|
24
|
+
const ch = useConfirmChannel ? await channelModel.createConfirmChannel() : await channelModel.createChannel();
|
|
25
25
|
await ch.prefetch(prefetch);
|
|
26
26
|
return ch;
|
|
27
27
|
})().disposer((ch, promise) => ch.close());
|
package/src/amqp-helper.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { Channel, ConfirmChannel, connect,
|
|
1
|
+
import { Channel, ChannelModel, ConfirmChannel, connect, Options } from 'amqplib';
|
|
2
2
|
import Bluebird from 'bluebird';
|
|
3
3
|
import Dict = NodeJS.Dict;
|
|
4
4
|
|
|
5
5
|
export const AmqpHelper = {
|
|
6
|
-
async withAmqpConn(fn: (
|
|
6
|
+
async withAmqpConn(fn: (channelModel: ChannelModel) => Promise<any>, url: string) {
|
|
7
7
|
function withDisposer() {
|
|
8
8
|
return Bluebird.method(async () => {
|
|
9
9
|
const amqpConn = await connect(url);
|
|
10
10
|
amqpConn.on('close', () => console.info('Amqp connection closed!'));
|
|
11
11
|
return amqpConn;
|
|
12
|
-
})().disposer((
|
|
12
|
+
})().disposer((channelModel, promise) => channelModel.close());
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
return Bluebird.using(withDisposer(),
|
|
15
|
+
return Bluebird.using(withDisposer(), channelModel => fn(channelModel));
|
|
16
16
|
},
|
|
17
17
|
|
|
18
18
|
async withAmqpCh(fn: (ch: Channel) => Promise<any>, url: string, useConfirmChannel = false, prefetch = 1) {
|
|
19
|
-
return AmqpHelper.withAmqpConn(async
|
|
19
|
+
return AmqpHelper.withAmqpConn(async channelModel => {
|
|
20
20
|
function withDisposer() {
|
|
21
21
|
return Bluebird.method(async () => {
|
|
22
|
-
const ch = useConfirmChannel ? await
|
|
22
|
+
const ch = useConfirmChannel ? await channelModel.createConfirmChannel() : await channelModel.createChannel();
|
|
23
23
|
await ch.prefetch(prefetch);
|
|
24
24
|
return ch;
|
|
25
25
|
})().disposer((ch, promise) => ch.close());
|
package/src/index.js
CHANGED
|
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
25
35
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
26
36
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
27
37
|
};
|
|
@@ -35,7 +45,6 @@ const storage_1 = require("@google-cloud/storage");
|
|
|
35
45
|
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
|
|
36
46
|
const mapValues_1 = __importDefault(require("lodash/mapValues"));
|
|
37
47
|
const noop_1 = __importDefault(require("lodash/noop"));
|
|
38
|
-
const rxjs_1 = require("rxjs");
|
|
39
48
|
const amqp_helper_1 = require("./amqp-helper");
|
|
40
49
|
const utils_1 = require("./utils");
|
|
41
50
|
exports.Storage = __importStar(require("@google-cloud/storage"));
|
|
@@ -44,30 +53,33 @@ exports.RxJs = __importStar(require("rxjs"));
|
|
|
44
53
|
exports.MongoDb = __importStar(require("mongodb"));
|
|
45
54
|
__exportStar(require("./types"), exports);
|
|
46
55
|
__exportStar(require("./utils"), exports);
|
|
56
|
+
__exportStar(require("./mongo-helper"), exports);
|
|
57
|
+
__exportStar(require("./amqp-helper"), exports);
|
|
47
58
|
exports.pubSub = new pubsub_1.PubSub();
|
|
48
59
|
exports.storage = new storage_1.Storage();
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
constructor() { }
|
|
52
|
-
static amqpOptions = {
|
|
60
|
+
exports.GcfCommon = {
|
|
61
|
+
amqpOptions: {
|
|
53
62
|
assertExchange: { durable: true, autoDelete: false },
|
|
54
63
|
assertOptions: { durable: true, autoDelete: true, expires: (0, utils_1.ms)({ d: 1 }) },
|
|
55
64
|
publishOptions: { persistent: true },
|
|
56
|
-
}
|
|
57
|
-
|
|
65
|
+
},
|
|
66
|
+
async process(payload, handler) {
|
|
58
67
|
const asyncHandler = async (p) => await handler(p);
|
|
59
|
-
return
|
|
68
|
+
// return firstValueFrom(
|
|
69
|
+
// defer(() => from(asyncHandler({ ...payload }))).pipe(first(), timeout({ first: ms({ s: timeoutSec }) })),
|
|
70
|
+
// )
|
|
71
|
+
return asyncHandler({ ...payload })
|
|
60
72
|
.then(async (res) => {
|
|
61
73
|
// console.log('res:', res);
|
|
62
|
-
await
|
|
74
|
+
await exports.GcfCommon.response({ ...payload }, res).catch(noop_1.default);
|
|
63
75
|
return res;
|
|
64
76
|
})
|
|
65
77
|
.catch(async (error) => {
|
|
66
|
-
await
|
|
78
|
+
await exports.GcfCommon.response({ ...payload }, exports.GcfCommon.buildResponse(error), { error: '1' }).catch(noop_1.default);
|
|
67
79
|
throw error;
|
|
68
80
|
});
|
|
69
|
-
}
|
|
70
|
-
|
|
81
|
+
},
|
|
82
|
+
buildResponse(error) {
|
|
71
83
|
return {
|
|
72
84
|
error: {
|
|
73
85
|
name: error.name,
|
|
@@ -75,16 +87,16 @@ class GcfCommon {
|
|
|
75
87
|
stack: error.stack,
|
|
76
88
|
},
|
|
77
89
|
};
|
|
78
|
-
}
|
|
79
|
-
|
|
90
|
+
},
|
|
91
|
+
async response(payload, json, attributes) {
|
|
80
92
|
// console.time('safeGetAttributes');
|
|
81
|
-
// const { topic, exchange, queue, consumer_id, request_id, app_id, env } = await
|
|
93
|
+
// const { topic, exchange, queue, consumer_id, request_id, app_id, env } = await GcfCommon.safeGetAttributes(
|
|
82
94
|
// event,
|
|
83
95
|
// context,
|
|
84
96
|
// ['consumer_id', 'topic', 'exchange', 'queue'],
|
|
85
97
|
// );
|
|
86
98
|
// console.timeEnd('safeGetAttributes');
|
|
87
|
-
const { topic, exchange, queue, consumer_id, request_id, app_id, env } =
|
|
99
|
+
const { topic, exchange, queue, consumer_id, request_id, app_id, env } = exports.GcfCommon.getMetadataOrAttribute(payload);
|
|
88
100
|
//
|
|
89
101
|
console.time('publish');
|
|
90
102
|
if (topic && !(0, isEmpty_1.default)(topic)) {
|
|
@@ -103,27 +115,27 @@ class GcfCommon {
|
|
|
103
115
|
if (exchange && !(0, isEmpty_1.default)(exchange)) {
|
|
104
116
|
console.log('send:', exchange, queue, app_id, env, json, attributes);
|
|
105
117
|
await amqp_helper_1.AmqpHelper.withAmqpCh(async (ch) => {
|
|
106
|
-
await ch.assertExchange(exchange, 'direct',
|
|
118
|
+
await ch.assertExchange(exchange, 'direct', exports.GcfCommon.amqpOptions.assertExchange);
|
|
107
119
|
await amqp_helper_1.AmqpHelper.publishAmqp(ch, exchange, queue ?? '', json ?? {}, {
|
|
108
|
-
...
|
|
120
|
+
...exports.GcfCommon.amqpOptions.publishOptions,
|
|
109
121
|
correlationId: request_id,
|
|
110
122
|
});
|
|
111
|
-
},
|
|
123
|
+
}, exports.GcfCommon.amqpOptions.url);
|
|
112
124
|
}
|
|
113
125
|
else if (queue && !(0, isEmpty_1.default)(queue)) {
|
|
114
126
|
console.log('send:', queue, app_id, env, json, attributes);
|
|
115
127
|
await amqp_helper_1.AmqpHelper.withAmqpCh(async (ch) => {
|
|
116
|
-
// await ch.assertQueue(queue,
|
|
128
|
+
// await ch.assertQueue(queue, GcfCommon.amqpOptions.assertOptions);
|
|
117
129
|
await amqp_helper_1.AmqpHelper.publishAmqp(ch, undefined, queue, json ?? {}, {
|
|
118
|
-
...
|
|
130
|
+
...exports.GcfCommon.amqpOptions.publishOptions,
|
|
119
131
|
correlationId: request_id,
|
|
120
132
|
});
|
|
121
|
-
},
|
|
133
|
+
}, exports.GcfCommon.amqpOptions.url);
|
|
122
134
|
}
|
|
123
135
|
console.timeEnd('publish');
|
|
124
|
-
}
|
|
125
|
-
//
|
|
126
|
-
// let metaOrAttr =
|
|
136
|
+
},
|
|
137
|
+
// async safeGetAttributes<E = TEvent>(event: E, context: TContext, props: string[]) {
|
|
138
|
+
// let metaOrAttr = GcfCommon.getMetadataOrAttribute(event, context);
|
|
127
139
|
// // const everyPropIsNil = props.map(prop => get(metaOrAttr, prop)).every(v => isNil(v));
|
|
128
140
|
//
|
|
129
141
|
// const someProp = props.map(prop => get(metaOrAttr, prop)).some(v => !isNil(v));
|
|
@@ -142,13 +154,13 @@ class GcfCommon {
|
|
|
142
154
|
// app_id: metaOrAttr.app_id,
|
|
143
155
|
// request_id: metaOrAttr.request_id,
|
|
144
156
|
// } as TMetadataOrAttributes;
|
|
145
|
-
// }
|
|
146
|
-
|
|
147
|
-
// const { options } = await
|
|
148
|
-
const { options } =
|
|
157
|
+
// },
|
|
158
|
+
async getOptions(payload) {
|
|
159
|
+
// const { options } = await GcfCommon.safeGetAttributes(event, context, ['options']);
|
|
160
|
+
const { options } = exports.GcfCommon.getMetadataOrAttribute(payload);
|
|
149
161
|
return (0, utils_1.safeJsonParse)(options, {});
|
|
150
|
-
}
|
|
151
|
-
|
|
162
|
+
},
|
|
163
|
+
getMetadataOrAttribute(payload) {
|
|
152
164
|
let metadataOrAttribute;
|
|
153
165
|
if (payload?.context) {
|
|
154
166
|
switch (payload?.context?.eventType) {
|
|
@@ -165,13 +177,12 @@ class GcfCommon {
|
|
|
165
177
|
}
|
|
166
178
|
}
|
|
167
179
|
else if (payload?.request) {
|
|
168
|
-
metadataOrAttribute =
|
|
180
|
+
metadataOrAttribute = exports.GcfCommon.getRequestMessage(payload.request)?.attributes;
|
|
169
181
|
}
|
|
170
182
|
// console.log('metadataOrAttribute:', metadataOrAttribute);
|
|
171
183
|
return metadataOrAttribute ?? {};
|
|
172
|
-
}
|
|
173
|
-
|
|
184
|
+
},
|
|
185
|
+
getRequestMessage(request) {
|
|
174
186
|
return request.body.message;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
exports.GcfCommon = GcfCommon;
|
|
187
|
+
},
|
|
188
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -5,7 +5,6 @@ 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, timeout } from 'rxjs';
|
|
9
8
|
import { AmqpHelper } from './amqp-helper';
|
|
10
9
|
import { TEvent, TGSEvent, TMetadataOrAttributes, TPayload, TPSEvent, TResponse } from './types';
|
|
11
10
|
import { ms, safeJsonParse } from './utils';
|
|
@@ -17,46 +16,48 @@ export * as RxJs from 'rxjs';
|
|
|
17
16
|
export * as MongoDb from 'mongodb';
|
|
18
17
|
export * from './types';
|
|
19
18
|
export * from './utils';
|
|
19
|
+
export * from './mongo-helper';
|
|
20
|
+
export * from './amqp-helper';
|
|
20
21
|
|
|
21
22
|
export const pubSub = new PubSub();
|
|
22
23
|
export const storage = new Storage();
|
|
23
|
-
// export const secretClient = new SecretManagerServiceClient();
|
|
24
24
|
|
|
25
|
-
export
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
export const GcfCommon = {
|
|
26
|
+
amqpOptions: {
|
|
27
|
+
assertExchange: { durable: true, autoDelete: false },
|
|
28
|
+
assertOptions: { durable: true, autoDelete: true, expires: ms({ d: 1 }) },
|
|
29
|
+
publishOptions: { persistent: true },
|
|
30
|
+
} as {
|
|
29
31
|
url?: string;
|
|
30
32
|
assertExchange?: Options.AssertExchange;
|
|
31
33
|
assertOptions?: Options.AssertQueue;
|
|
32
34
|
publishOptions?: Options.Publish;
|
|
33
|
-
}
|
|
34
|
-
assertExchange: { durable: true, autoDelete: false },
|
|
35
|
-
assertOptions: { durable: true, autoDelete: true, expires: ms({ d: 1 }) },
|
|
36
|
-
publishOptions: { persistent: true },
|
|
37
|
-
};
|
|
35
|
+
},
|
|
38
36
|
|
|
39
|
-
|
|
37
|
+
async process<T extends TResponse, E = TEvent>(
|
|
40
38
|
payload: TPayload<E>,
|
|
41
39
|
handler: (p: TPayload<E>) => Promise<any> | any,
|
|
42
|
-
timeoutSec = 535,
|
|
40
|
+
// timeoutSec = 535,
|
|
43
41
|
) {
|
|
44
42
|
const asyncHandler = async (p: TPayload<E>) => await handler(p);
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
)
|
|
43
|
+
|
|
44
|
+
// return firstValueFrom(
|
|
45
|
+
// defer(() => from(asyncHandler({ ...payload }))).pipe(first(), timeout({ first: ms({ s: timeoutSec }) })),
|
|
46
|
+
// )
|
|
47
|
+
|
|
48
|
+
return asyncHandler({ ...payload })
|
|
48
49
|
.then(async res => {
|
|
49
50
|
// console.log('res:', res);
|
|
50
|
-
await
|
|
51
|
+
await GcfCommon.response({ ...payload }, res as T).catch(noop);
|
|
51
52
|
return res;
|
|
52
53
|
})
|
|
53
54
|
.catch(async (error: Error) => {
|
|
54
|
-
await
|
|
55
|
+
await GcfCommon.response({ ...payload }, GcfCommon.buildResponse(error), { error: '1' }).catch(noop);
|
|
55
56
|
throw error;
|
|
56
57
|
});
|
|
57
|
-
}
|
|
58
|
+
},
|
|
58
59
|
|
|
59
|
-
|
|
60
|
+
buildResponse(error: Error) {
|
|
60
61
|
return {
|
|
61
62
|
error: {
|
|
62
63
|
name: error.name,
|
|
@@ -64,18 +65,18 @@ export class GcfCommon {
|
|
|
64
65
|
stack: error.stack,
|
|
65
66
|
},
|
|
66
67
|
} as TResponse;
|
|
67
|
-
}
|
|
68
|
+
},
|
|
68
69
|
|
|
69
|
-
|
|
70
|
+
async response<E = TEvent>(payload: TPayload<E>, json?: TResponse, attributes?: Dict<any>) {
|
|
70
71
|
// console.time('safeGetAttributes');
|
|
71
|
-
// const { topic, exchange, queue, consumer_id, request_id, app_id, env } = await
|
|
72
|
+
// const { topic, exchange, queue, consumer_id, request_id, app_id, env } = await GcfCommon.safeGetAttributes(
|
|
72
73
|
// event,
|
|
73
74
|
// context,
|
|
74
75
|
// ['consumer_id', 'topic', 'exchange', 'queue'],
|
|
75
76
|
// );
|
|
76
77
|
// console.timeEnd('safeGetAttributes');
|
|
77
78
|
|
|
78
|
-
const { topic, exchange, queue, consumer_id, request_id, app_id, env } =
|
|
79
|
+
const { topic, exchange, queue, consumer_id, request_id, app_id, env } = GcfCommon.getMetadataOrAttribute(payload);
|
|
79
80
|
|
|
80
81
|
//
|
|
81
82
|
|
|
@@ -98,28 +99,28 @@ export class GcfCommon {
|
|
|
98
99
|
if (exchange && !isEmpty(exchange)) {
|
|
99
100
|
console.log('send:', exchange, queue, app_id, env, json, attributes);
|
|
100
101
|
await AmqpHelper.withAmqpCh(async ch => {
|
|
101
|
-
await ch.assertExchange(exchange, 'direct',
|
|
102
|
+
await ch.assertExchange(exchange, 'direct', GcfCommon.amqpOptions.assertExchange);
|
|
102
103
|
await AmqpHelper.publishAmqp(ch, exchange, queue ?? '', json ?? {}, {
|
|
103
|
-
...
|
|
104
|
+
...GcfCommon.amqpOptions.publishOptions,
|
|
104
105
|
correlationId: request_id,
|
|
105
106
|
});
|
|
106
|
-
},
|
|
107
|
+
}, GcfCommon.amqpOptions.url as string);
|
|
107
108
|
} else if (queue && !isEmpty(queue)) {
|
|
108
109
|
console.log('send:', queue, app_id, env, json, attributes);
|
|
109
110
|
await AmqpHelper.withAmqpCh(async ch => {
|
|
110
|
-
// await ch.assertQueue(queue,
|
|
111
|
+
// await ch.assertQueue(queue, GcfCommon.amqpOptions.assertOptions);
|
|
111
112
|
await AmqpHelper.publishAmqp(ch, undefined, queue, json ?? {}, {
|
|
112
|
-
...
|
|
113
|
+
...GcfCommon.amqpOptions.publishOptions,
|
|
113
114
|
correlationId: request_id,
|
|
114
115
|
});
|
|
115
|
-
},
|
|
116
|
+
}, GcfCommon.amqpOptions.url as string);
|
|
116
117
|
}
|
|
117
118
|
|
|
118
119
|
console.timeEnd('publish');
|
|
119
|
-
}
|
|
120
|
+
},
|
|
120
121
|
|
|
121
|
-
//
|
|
122
|
-
// let metaOrAttr =
|
|
122
|
+
// async safeGetAttributes<E = TEvent>(event: E, context: TContext, props: string[]) {
|
|
123
|
+
// let metaOrAttr = GcfCommon.getMetadataOrAttribute(event, context);
|
|
123
124
|
// // const everyPropIsNil = props.map(prop => get(metaOrAttr, prop)).every(v => isNil(v));
|
|
124
125
|
//
|
|
125
126
|
// const someProp = props.map(prop => get(metaOrAttr, prop)).some(v => !isNil(v));
|
|
@@ -138,15 +139,15 @@ export class GcfCommon {
|
|
|
138
139
|
// app_id: metaOrAttr.app_id,
|
|
139
140
|
// request_id: metaOrAttr.request_id,
|
|
140
141
|
// } as TMetadataOrAttributes;
|
|
141
|
-
// }
|
|
142
|
+
// },
|
|
142
143
|
|
|
143
|
-
|
|
144
|
-
// const { options } = await
|
|
145
|
-
const { options } =
|
|
144
|
+
async getOptions(payload: TPayload): Promise<Dict<any>> {
|
|
145
|
+
// const { options } = await GcfCommon.safeGetAttributes(event, context, ['options']);
|
|
146
|
+
const { options } = GcfCommon.getMetadataOrAttribute(payload);
|
|
146
147
|
return safeJsonParse(options as any, {} as any);
|
|
147
|
-
}
|
|
148
|
+
},
|
|
148
149
|
|
|
149
|
-
|
|
150
|
+
getMetadataOrAttribute<E = TEvent>(payload: TPayload<E>) {
|
|
150
151
|
let metadataOrAttribute: TMetadataOrAttributes | undefined;
|
|
151
152
|
|
|
152
153
|
if (payload?.context) {
|
|
@@ -163,27 +164,15 @@ export class GcfCommon {
|
|
|
163
164
|
}
|
|
164
165
|
}
|
|
165
166
|
} else if (payload?.request) {
|
|
166
|
-
metadataOrAttribute =
|
|
167
|
+
metadataOrAttribute = GcfCommon.getRequestMessage(payload.request)?.attributes;
|
|
167
168
|
}
|
|
168
169
|
|
|
169
170
|
// console.log('metadataOrAttribute:', metadataOrAttribute);
|
|
170
171
|
|
|
171
172
|
return metadataOrAttribute ?? {};
|
|
172
|
-
}
|
|
173
|
+
},
|
|
173
174
|
|
|
174
|
-
|
|
175
|
+
getRequestMessage(request: Request) {
|
|
175
176
|
return request.body.message as { attributes: TMetadataOrAttributes; data: string; json?: Dict<any> };
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// static async getSecret(name: string, version?: string) {
|
|
179
|
-
// const projectId = await secretClient.getProjectId();
|
|
180
|
-
// const secretName = `projects/${projectId}/secrets/${name}`;
|
|
181
|
-
// const secretVersion = `${secretName}/versions/${version ?? 'latest'}`;
|
|
182
|
-
// const [response] = await secretClient.accessSecretVersion({ name: secretVersion });
|
|
183
|
-
// return response?.payload?.data?.toString();
|
|
184
|
-
// }
|
|
185
|
-
//
|
|
186
|
-
// static async getSecrets(names: string[], versions?: string[]) {
|
|
187
|
-
// return Promise.all(names.map(async (name, idx) => this.getSecret(name, versions?.[idx]).catch(noop)));
|
|
188
|
-
// }
|
|
189
|
-
}
|
|
177
|
+
},
|
|
178
|
+
};
|
package/src/mongo-lock.js
CHANGED
package/src/utils.js
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.timeoutAfter = timeoutAfter;
|
|
4
|
+
exports.delay = delay;
|
|
5
|
+
exports.ms = ms;
|
|
6
|
+
exports.sec = sec;
|
|
7
|
+
exports.indexToA1 = indexToA1;
|
|
8
|
+
exports.A1ToIndex = A1ToIndex;
|
|
9
|
+
exports.colNumToA1 = colNumToA1;
|
|
10
|
+
exports.A1ToColNum = A1ToColNum;
|
|
11
|
+
exports.safeJsonParse = safeJsonParse;
|
|
4
12
|
/**
|
|
5
13
|
*
|
|
6
14
|
* @param seconds Google function v1 timeout limit (max: 9 min)
|
|
@@ -8,15 +16,12 @@ exports.safeJsonParse = exports.A1ToColNum = exports.colNumToA1 = exports.A1ToIn
|
|
|
8
16
|
async function timeoutAfter(seconds = 540) {
|
|
9
17
|
return new Promise((resolve, reject) => setTimeout(() => reject(new Error(`${seconds} seconds timeout exceeded`)), seconds * 1000));
|
|
10
18
|
}
|
|
11
|
-
exports.timeoutAfter = timeoutAfter;
|
|
12
19
|
async function delay(seconds) {
|
|
13
20
|
return new Promise(resolve => setTimeout(() => resolve(), seconds * 1000));
|
|
14
21
|
}
|
|
15
|
-
exports.delay = delay;
|
|
16
22
|
function ms(o) {
|
|
17
23
|
return sec(o) * 1000;
|
|
18
24
|
}
|
|
19
|
-
exports.ms = ms;
|
|
20
25
|
function sec(o) {
|
|
21
26
|
const multiMap = {};
|
|
22
27
|
multiMap.s = 1;
|
|
@@ -28,15 +33,12 @@ function sec(o) {
|
|
|
28
33
|
.map(([k, v]) => (multiMap[k] ?? 0) * (v ?? 0))
|
|
29
34
|
.reduce((sum, v) => sum + v, 0);
|
|
30
35
|
}
|
|
31
|
-
exports.sec = sec;
|
|
32
36
|
function indexToA1(idx) {
|
|
33
37
|
return colNumToA1(idx + 1);
|
|
34
38
|
}
|
|
35
|
-
exports.indexToA1 = indexToA1;
|
|
36
39
|
function A1ToIndex(value) {
|
|
37
40
|
return A1ToColNum(value) - 1;
|
|
38
41
|
}
|
|
39
|
-
exports.A1ToIndex = A1ToIndex;
|
|
40
42
|
function colNumToA1(columnNumber) {
|
|
41
43
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
42
44
|
// To store result (Excel column name)
|
|
@@ -62,7 +64,6 @@ function colNumToA1(columnNumber) {
|
|
|
62
64
|
.map(n => chars[n])
|
|
63
65
|
.join('');
|
|
64
66
|
}
|
|
65
|
-
exports.colNumToA1 = colNumToA1;
|
|
66
67
|
function A1ToColNum(value) {
|
|
67
68
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
68
69
|
let result = 0;
|
|
@@ -73,7 +74,6 @@ function A1ToColNum(value) {
|
|
|
73
74
|
}
|
|
74
75
|
return result;
|
|
75
76
|
}
|
|
76
|
-
exports.A1ToColNum = A1ToColNum;
|
|
77
77
|
function safeJsonParse(value, fallbackValue) {
|
|
78
78
|
try {
|
|
79
79
|
return JSON.parse(value);
|
|
@@ -82,4 +82,3 @@ function safeJsonParse(value, fallbackValue) {
|
|
|
82
82
|
return fallbackValue;
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
|
-
exports.safeJsonParse = safeJsonParse;
|