gcf-common-lib 0.25.43 → 0.25.45
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.ts +6 -6
- package/src/utils.js +18 -7
- package/src/utils.ts +34 -11
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -40,10 +40,10 @@ export class GcfCommon {
|
|
|
40
40
|
* @param handler
|
|
41
41
|
* @param timeout Seconds
|
|
42
42
|
*/
|
|
43
|
-
static async process<T extends TResponse>(
|
|
44
|
-
event:
|
|
43
|
+
static async process<T extends TResponse, E = TEvent>(
|
|
44
|
+
event: E,
|
|
45
45
|
context: TContext,
|
|
46
|
-
handler: (event:
|
|
46
|
+
handler: (event: E, context: TContext) => Promise<T> = async (e, c) => e as any,
|
|
47
47
|
timeout = 535,
|
|
48
48
|
) {
|
|
49
49
|
return Promise.race([timeoutAfter(timeout), handler(event, context)])
|
|
@@ -67,7 +67,7 @@ export class GcfCommon {
|
|
|
67
67
|
} as TResponse;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
static async publish(event:
|
|
70
|
+
static async publish<E = TEvent>(event: E, context: TContext, json?: TResponse, attributes?: Dict<any>) {
|
|
71
71
|
console.time('safeGetAttributes');
|
|
72
72
|
|
|
73
73
|
const { topic, exchange, queue, appId, env, requestId } = await this.safeGetAttributes(event, context, [
|
|
@@ -115,7 +115,7 @@ export class GcfCommon {
|
|
|
115
115
|
console.timeEnd('publish');
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
-
static async safeGetAttributes(event:
|
|
118
|
+
static async safeGetAttributes<E = TEvent>(event: E, context: TContext, props: string[]) {
|
|
119
119
|
let metaOrAttr = this.getMetadataOrAttribute(event, context);
|
|
120
120
|
// const everyPropIsNil = props.map(prop => get(metaOrAttr, prop)).every(v => isNil(v));
|
|
121
121
|
const someProp = props.map(prop => get(metaOrAttr, prop)).some(v => !isNil(v));
|
|
@@ -142,7 +142,7 @@ export class GcfCommon {
|
|
|
142
142
|
return JSON.parse(options ?? '{}');
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
-
static getMetadataOrAttribute(event:
|
|
145
|
+
static getMetadataOrAttribute<E = TEvent>(event: E, context: TContext) {
|
|
146
146
|
let metadataOrAttribute: TMetadataOrAttributes | undefined;
|
|
147
147
|
|
|
148
148
|
switch (context?.eventType) {
|
package/src/utils.js
CHANGED
|
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.sendToQueueConfAmqp = exports.publishAmqp = exports.withAmqpCh = exports.withAmqpConn = exports.A1ToColNum = exports.colNumToA1 = exports.A1ToIndex = exports.indexToA1 = exports.sec = exports.ms = exports.delay = exports.timeoutAfter = void 0;
|
|
15
|
+
exports.sendToQueueConfAmqp = exports.publishAmqp = exports.withAmqpCh = exports.withAmqpConn = exports.safeJsonParse = exports.A1ToColNum = exports.colNumToA1 = exports.A1ToIndex = exports.indexToA1 = exports.sec = exports.ms = exports.delay = exports.timeoutAfter = void 0;
|
|
16
16
|
const amqplib_1 = require("amqplib");
|
|
17
17
|
const bluebird_1 = __importDefault(require("bluebird"));
|
|
18
18
|
const lodash_1 = require("lodash");
|
|
@@ -28,7 +28,7 @@ function timeoutAfter(seconds = 540) {
|
|
|
28
28
|
exports.timeoutAfter = timeoutAfter;
|
|
29
29
|
function delay(seconds) {
|
|
30
30
|
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
-
return new Promise(
|
|
31
|
+
return new Promise(resolve => setTimeout(() => resolve(undefined), seconds * 1000));
|
|
32
32
|
});
|
|
33
33
|
}
|
|
34
34
|
exports.delay = delay;
|
|
@@ -95,6 +95,15 @@ function A1ToColNum(value) {
|
|
|
95
95
|
return result;
|
|
96
96
|
}
|
|
97
97
|
exports.A1ToColNum = A1ToColNum;
|
|
98
|
+
function safeJsonParse(value, fallbackValue) {
|
|
99
|
+
try {
|
|
100
|
+
return JSON.parse(value);
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
return fallbackValue;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.safeJsonParse = safeJsonParse;
|
|
98
107
|
//
|
|
99
108
|
function withAmqpConn(fn, url) {
|
|
100
109
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -105,7 +114,7 @@ function withAmqpConn(fn, url) {
|
|
|
105
114
|
return amqpConn;
|
|
106
115
|
}))().disposer((conn, promise) => conn.close());
|
|
107
116
|
}
|
|
108
|
-
return bluebird_1.default.using(withDisposer(),
|
|
117
|
+
return bluebird_1.default.using(withDisposer(), conn => fn(conn));
|
|
109
118
|
});
|
|
110
119
|
}
|
|
111
120
|
exports.withAmqpConn = withAmqpConn;
|
|
@@ -114,10 +123,10 @@ function withAmqpCh(fn, url, useConfirmChannel = false) {
|
|
|
114
123
|
return withAmqpConn((conn) => __awaiter(this, void 0, void 0, function* () {
|
|
115
124
|
function withDisposer() {
|
|
116
125
|
return bluebird_1.default.method(() => __awaiter(this, void 0, void 0, function* () {
|
|
117
|
-
return
|
|
126
|
+
return useConfirmChannel ? conn.createConfirmChannel() : conn.createChannel();
|
|
118
127
|
}))().disposer((ch, promise) => ch.close());
|
|
119
128
|
}
|
|
120
|
-
return bluebird_1.default.using(withDisposer(),
|
|
129
|
+
return bluebird_1.default.using(withDisposer(), ch => fn(ch));
|
|
121
130
|
}), url);
|
|
122
131
|
});
|
|
123
132
|
}
|
|
@@ -125,7 +134,9 @@ exports.withAmqpCh = withAmqpCh;
|
|
|
125
134
|
function publishAmqp(ch, exchange, routingKey, json, options) {
|
|
126
135
|
return __awaiter(this, void 0, void 0, function* () {
|
|
127
136
|
const payload = Buffer.from(JSON.stringify(json));
|
|
128
|
-
const keepSending = exchange
|
|
137
|
+
const keepSending = exchange
|
|
138
|
+
? ch.publish(exchange, routingKey, payload, options)
|
|
139
|
+
: ch.sendToQueue(routingKey, payload, options);
|
|
129
140
|
if (!keepSending)
|
|
130
141
|
yield new Promise(resolve => ch.once('drain', () => resolve(undefined)));
|
|
131
142
|
});
|
|
@@ -134,7 +145,7 @@ exports.publishAmqp = publishAmqp;
|
|
|
134
145
|
function sendToQueueConfAmqp(ch, queue, json, options) {
|
|
135
146
|
return __awaiter(this, void 0, void 0, function* () {
|
|
136
147
|
const payload = Buffer.from(JSON.stringify(json));
|
|
137
|
-
yield new Promise((resolve, reject) => ch.sendToQueue(queue, payload, options, (err, ok) => err ? reject(err) : resolve(ok)));
|
|
148
|
+
yield new Promise((resolve, reject) => ch.sendToQueue(queue, payload, options, (err, ok) => (err ? reject(err) : resolve(ok))));
|
|
138
149
|
});
|
|
139
150
|
}
|
|
140
151
|
exports.sendToQueueConfAmqp = sendToQueueConfAmqp;
|
package/src/utils.ts
CHANGED
|
@@ -8,11 +8,12 @@ import { chain, Dictionary } from 'lodash';
|
|
|
8
8
|
*/
|
|
9
9
|
export async function timeoutAfter(seconds: number = 540) {
|
|
10
10
|
return new Promise<undefined>((resolve, reject) =>
|
|
11
|
-
setTimeout(() => reject(new Error(`${seconds} seconds timeout exceeded`)), seconds * 1000)
|
|
11
|
+
setTimeout(() => reject(new Error(`${seconds} seconds timeout exceeded`)), seconds * 1000),
|
|
12
|
+
);
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
export async function delay(seconds: number) {
|
|
15
|
-
return new Promise(
|
|
16
|
+
return new Promise(resolve => setTimeout(() => resolve(undefined), seconds * 1000));
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
//
|
|
@@ -83,6 +84,14 @@ export function A1ToColNum(value: string) {
|
|
|
83
84
|
return result;
|
|
84
85
|
}
|
|
85
86
|
|
|
87
|
+
export function safeJsonParse(value: string, fallbackValue?: any) {
|
|
88
|
+
try {
|
|
89
|
+
return JSON.parse(value);
|
|
90
|
+
} catch (err) {
|
|
91
|
+
return fallbackValue;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
86
95
|
//
|
|
87
96
|
|
|
88
97
|
export async function withAmqpConn(fn: (conn: Connection) => Promise<any>, url: string) {
|
|
@@ -94,29 +103,43 @@ export async function withAmqpConn(fn: (conn: Connection) => Promise<any>, url:
|
|
|
94
103
|
})().disposer((conn, promise) => conn.close());
|
|
95
104
|
}
|
|
96
105
|
|
|
97
|
-
return Bluebird.using(withDisposer(),
|
|
106
|
+
return Bluebird.using(withDisposer(), conn => fn(conn));
|
|
98
107
|
}
|
|
99
108
|
|
|
100
109
|
export async function withAmqpCh(fn: (ch: Channel) => Promise<any>, url: string, useConfirmChannel = false) {
|
|
101
110
|
return withAmqpConn(async conn => {
|
|
102
111
|
function withDisposer() {
|
|
103
112
|
return Bluebird.method(async () => {
|
|
104
|
-
return
|
|
113
|
+
return useConfirmChannel ? conn.createConfirmChannel() : conn.createChannel();
|
|
105
114
|
})().disposer((ch, promise) => ch.close());
|
|
106
115
|
}
|
|
107
116
|
|
|
108
|
-
return Bluebird.using(withDisposer(),
|
|
109
|
-
|
|
117
|
+
return Bluebird.using(withDisposer(), ch => fn(ch));
|
|
110
118
|
}, url);
|
|
111
119
|
}
|
|
112
120
|
|
|
113
|
-
export async function publishAmqp(
|
|
121
|
+
export async function publishAmqp(
|
|
122
|
+
ch: Channel,
|
|
123
|
+
exchange: string | undefined,
|
|
124
|
+
routingKey: string,
|
|
125
|
+
json: Dictionary<any>,
|
|
126
|
+
options?: Options.Publish,
|
|
127
|
+
) {
|
|
114
128
|
const payload = Buffer.from(JSON.stringify(json));
|
|
115
|
-
const keepSending = exchange
|
|
129
|
+
const keepSending = exchange
|
|
130
|
+
? ch.publish(exchange, routingKey, payload, options)
|
|
131
|
+
: ch.sendToQueue(routingKey, payload, options);
|
|
116
132
|
if (!keepSending) await new Promise(resolve => ch.once('drain', () => resolve(undefined)));
|
|
117
133
|
}
|
|
118
134
|
|
|
119
|
-
export async function sendToQueueConfAmqp(
|
|
135
|
+
export async function sendToQueueConfAmqp(
|
|
136
|
+
ch: ConfirmChannel,
|
|
137
|
+
queue: string,
|
|
138
|
+
json: Dictionary<any>,
|
|
139
|
+
options?: Options.Publish,
|
|
140
|
+
) {
|
|
120
141
|
const payload = Buffer.from(JSON.stringify(json));
|
|
121
|
-
await new Promise((resolve, reject) =>
|
|
122
|
-
|
|
142
|
+
await new Promise((resolve, reject) =>
|
|
143
|
+
ch.sendToQueue(queue, payload, options, (err, ok) => (err ? reject(err) : resolve(ok))),
|
|
144
|
+
);
|
|
145
|
+
}
|