@venizia/ignis-helpers 0.0.1-2 → 0.0.1-4
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/LICENSE.md +102 -0
- package/README.md +43 -0
- package/dist/common/constants/app.d.ts +1 -1
- package/dist/common/constants/app.js +8 -8
- package/dist/common/constants/http.d.ts +1 -1
- package/dist/common/constants/http.js +13 -13
- package/dist/common/constants/index.d.ts +3 -3
- package/dist/common/constants/mime.d.ts +1 -1
- package/dist/common/constants/mime.js +4 -4
- package/dist/common/index.d.ts +2 -2
- package/dist/common/types.d.ts +13 -13
- package/dist/common/types.js +2 -2
- package/dist/helpers/base.d.ts +1 -1
- package/dist/helpers/base.js +3 -3
- package/dist/helpers/cron/cron.helper.d.ts +2 -2
- package/dist/helpers/cron/cron.helper.js +2 -2
- package/dist/helpers/cron/index.d.ts +1 -1
- package/dist/helpers/crypto/algorithms/aes.algorithm.d.ts +3 -3
- package/dist/helpers/crypto/algorithms/aes.algorithm.js +10 -10
- package/dist/helpers/crypto/algorithms/base.algorithm.d.ts +2 -2
- package/dist/helpers/crypto/algorithms/base.algorithm.js +1 -1
- package/dist/helpers/crypto/algorithms/index.d.ts +3 -3
- package/dist/helpers/crypto/algorithms/rsa.algorithm.d.ts +3 -3
- package/dist/helpers/crypto/algorithms/rsa.algorithm.js +10 -10
- package/dist/helpers/crypto/common/index.d.ts +2 -2
- package/dist/helpers/crypto/index.d.ts +2 -2
- package/dist/helpers/env/app-env.d.ts +1 -1
- package/dist/helpers/env/app-env.js +9 -9
- package/dist/helpers/env/index.d.ts +2 -2
- package/dist/helpers/error/app-error.d.ts +1 -1
- package/dist/helpers/error/index.d.ts +2 -2
- package/dist/helpers/error/types.d.ts +1 -1
- package/dist/helpers/error/types.js +5 -5
- package/dist/helpers/index.d.ts +14 -14
- package/dist/helpers/inversion/common/index.d.ts +2 -2
- package/dist/helpers/inversion/common/keys.js +6 -6
- package/dist/helpers/inversion/common/types.d.ts +4 -4
- package/dist/helpers/inversion/container.d.ts +2 -2
- package/dist/helpers/inversion/index.d.ts +4 -4
- package/dist/helpers/inversion/registry.d.ts +2 -2
- package/dist/helpers/logger/application-logger.d.ts +2 -2
- package/dist/helpers/logger/application-logger.js +8 -8
- package/dist/helpers/logger/default-logger.d.ts +3 -3
- package/dist/helpers/logger/default-logger.js +23 -23
- package/dist/helpers/logger/factory.d.ts +1 -1
- package/dist/helpers/logger/factory.js +1 -1
- package/dist/helpers/logger/index.d.ts +5 -5
- package/dist/helpers/logger/transports/dgram.transport.d.ts +2 -2
- package/dist/helpers/logger/transports/dgram.transport.js +5 -5
- package/dist/helpers/logger/transports/index.d.ts +1 -1
- package/dist/helpers/logger/types.d.ts +1 -1
- package/dist/helpers/logger/types.js +9 -9
- package/dist/helpers/network/http-request/base-network-request.helper.d.ts +12 -12
- package/dist/helpers/network/http-request/base-network-request.helper.js +10 -10
- package/dist/helpers/network/http-request/fetcher/axios-fetcher.d.ts +5 -5
- package/dist/helpers/network/http-request/fetcher/axios-fetcher.js +5 -5
- package/dist/helpers/network/http-request/fetcher/base-fetcher.d.ts +1 -1
- package/dist/helpers/network/http-request/fetcher/base-fetcher.js +8 -8
- package/dist/helpers/network/http-request/fetcher/index.d.ts +3 -3
- package/dist/helpers/network/http-request/fetcher/node-fetcher.d.ts +2 -2
- package/dist/helpers/network/http-request/fetcher/node-fetcher.js +6 -6
- package/dist/helpers/network/http-request/index.d.ts +2 -2
- package/dist/helpers/network/http-request/types.d.ts +4 -4
- package/dist/helpers/network/index.d.ts +3 -3
- package/dist/helpers/network/tcp-socket/base-tcp-client.helper.d.ts +4 -4
- package/dist/helpers/network/tcp-socket/base-tcp-client.helper.js +17 -17
- package/dist/helpers/network/tcp-socket/base-tcp-server.helper.d.ts +6 -6
- package/dist/helpers/network/tcp-socket/base-tcp-server.helper.js +18 -18
- package/dist/helpers/network/tcp-socket/index.d.ts +6 -6
- package/dist/helpers/network/tcp-socket/network-tcp-client.helper.d.ts +4 -4
- package/dist/helpers/network/tcp-socket/network-tcp-server.helper.d.ts +4 -4
- package/dist/helpers/network/tcp-socket/network-tls-tcp-client.helper.d.ts +4 -4
- package/dist/helpers/network/tcp-socket/network-tls-tcp-server.helper.d.ts +4 -4
- package/dist/helpers/network/udp-socket/index.d.ts +1 -1
- package/dist/helpers/network/udp-socket/network-udp-client.helper.d.ts +3 -3
- package/dist/helpers/network/udp-socket/network-udp-client.helper.js +14 -14
- package/dist/helpers/queue/bullmq/helper.d.ts +4 -4
- package/dist/helpers/queue/bullmq/helper.js +10 -10
- package/dist/helpers/queue/bullmq/index.d.ts +1 -1
- package/dist/helpers/queue/common/index.d.ts +1 -1
- package/dist/helpers/queue/common/types.d.ts +1 -1
- package/dist/helpers/queue/index.d.ts +4 -4
- package/dist/helpers/queue/internal/helper.d.ts +3 -3
- package/dist/helpers/queue/internal/helper.js +11 -11
- package/dist/helpers/queue/internal/index.d.ts +1 -1
- package/dist/helpers/queue/mqtt/helper.d.ts +2 -2
- package/dist/helpers/queue/mqtt/helper.js +8 -8
- package/dist/helpers/queue/mqtt/index.d.ts +1 -1
- package/dist/helpers/redis/cluster.helper.d.ts +3 -3
- package/dist/helpers/redis/default.helper.d.ts +3 -3
- package/dist/helpers/redis/default.helper.js +40 -40
- package/dist/helpers/redis/index.d.ts +4 -4
- package/dist/helpers/redis/single.helper.d.ts +3 -3
- package/dist/helpers/redis/types.d.ts +3 -3
- package/dist/helpers/socket-io/client/helper.d.ts +3 -3
- package/dist/helpers/socket-io/client/helper.js +8 -8
- package/dist/helpers/socket-io/client/index.d.ts +1 -1
- package/dist/helpers/socket-io/common/constants.js +10 -10
- package/dist/helpers/socket-io/common/index.d.ts +2 -2
- package/dist/helpers/socket-io/common/types.d.ts +6 -6
- package/dist/helpers/socket-io/index.d.ts +3 -3
- package/dist/helpers/socket-io/server/helper.d.ts +3 -3
- package/dist/helpers/socket-io/server/helper.js +41 -41
- package/dist/helpers/socket-io/server/index.d.ts +1 -1
- package/dist/helpers/storage/in-memory/helper.d.ts +2 -2
- package/dist/helpers/storage/in-memory/index.d.ts +1 -1
- package/dist/helpers/storage/index.d.ts +2 -2
- package/dist/helpers/storage/minio/helper.d.ts +3 -3
- package/dist/helpers/storage/minio/helper.js +7 -7
- package/dist/helpers/storage/minio/index.d.ts +1 -1
- package/dist/helpers/testing/base-test-plan.d.ts +2 -2
- package/dist/helpers/testing/base-test-plan.js +4 -4
- package/dist/helpers/testing/common/constants.d.ts +1 -1
- package/dist/helpers/testing/common/constants.js +3 -3
- package/dist/helpers/testing/common/index.d.ts +2 -2
- package/dist/helpers/testing/common/types.d.ts +3 -3
- package/dist/helpers/testing/describe.d.ts +2 -2
- package/dist/helpers/testing/describe.js +5 -5
- package/dist/helpers/testing/index.d.ts +6 -6
- package/dist/helpers/testing/test-case.d.ts +3 -3
- package/dist/helpers/testing/test-case.js +1 -1
- package/dist/helpers/testing/test-handler.d.ts +3 -3
- package/dist/helpers/testing/test-handler.js +2 -2
- package/dist/helpers/testing/test-plan.d.ts +2 -2
- package/dist/helpers/worker-thread/base.d.ts +6 -6
- package/dist/helpers/worker-thread/base.js +15 -15
- package/dist/helpers/worker-thread/base.js.map +1 -1
- package/dist/helpers/worker-thread/index.d.ts +4 -4
- package/dist/helpers/worker-thread/types.d.ts +2 -2
- package/dist/helpers/worker-thread/worker-bus.d.ts +4 -4
- package/dist/helpers/worker-thread/worker-bus.d.ts.map +1 -1
- package/dist/helpers/worker-thread/worker-bus.js +14 -9
- package/dist/helpers/worker-thread/worker-bus.js.map +1 -1
- package/dist/helpers/worker-thread/worker-pool.d.ts +2 -2
- package/dist/helpers/worker-thread/worker-pool.js +6 -6
- package/dist/index.d.ts +4 -4
- package/dist/utilities/crypto.utility.d.ts +1 -1
- package/dist/utilities/crypto.utility.js +2 -2
- package/dist/utilities/date.utility.d.ts +1 -1
- package/dist/utilities/date.utility.js +6 -6
- package/dist/utilities/index.d.ts +7 -7
- package/dist/utilities/module.utility.js +2 -2
- package/dist/utilities/parse.utility.js +18 -18
- package/dist/utilities/performance.utility.d.ts +1 -1
- package/dist/utilities/performance.utility.js +5 -5
- package/dist/utilities/promise.utility.d.ts +1 -1
- package/dist/utilities/promise.utility.js +3 -3
- package/dist/utilities/request.utility.d.ts +1 -1
- package/dist/utilities/request.utility.js +7 -7
- package/package.json +23 -29
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { BaseHelper } from
|
|
2
|
-
import { Job, Queue, Worker } from
|
|
3
|
-
import Redis from
|
|
4
|
-
import { TBullQueueRole } from
|
|
1
|
+
import { BaseHelper } from '../../../helpers/base';
|
|
2
|
+
import { Job, Queue, Worker } from 'bullmq';
|
|
3
|
+
import Redis from 'ioredis';
|
|
4
|
+
import { TBullQueueRole } from '../common';
|
|
5
5
|
interface IBullMQOptions<TQueueElement = any, TQueueResult = any> {
|
|
6
6
|
queueName: string;
|
|
7
7
|
identifier: string;
|
|
@@ -24,7 +24,7 @@ class BullMQHelper extends base_1.BaseHelper {
|
|
|
24
24
|
}
|
|
25
25
|
configureQueue() {
|
|
26
26
|
if (!this.queueName) {
|
|
27
|
-
this.logger.error(
|
|
27
|
+
this.logger.error('[configureQueue][%s] Invalid queue name', this.identifier);
|
|
28
28
|
return;
|
|
29
29
|
}
|
|
30
30
|
this.queue = new bullmq_1.Queue(this.queueName, {
|
|
@@ -37,7 +37,7 @@ class BullMQHelper extends base_1.BaseHelper {
|
|
|
37
37
|
}
|
|
38
38
|
configureWorker() {
|
|
39
39
|
if (!this.queueName) {
|
|
40
|
-
this.logger.error(
|
|
40
|
+
this.logger.error('[configureWorkers][%s] Invalid worker name', this.identifier);
|
|
41
41
|
return;
|
|
42
42
|
}
|
|
43
43
|
this.worker = new bullmq_1.Worker(this.queueName, async (job) => {
|
|
@@ -46,42 +46,42 @@ class BullMQHelper extends base_1.BaseHelper {
|
|
|
46
46
|
return rs;
|
|
47
47
|
}
|
|
48
48
|
const { id, name, data } = job;
|
|
49
|
-
this.logger.info(
|
|
49
|
+
this.logger.info('[onWorkerData][%s] queue: %s | id: %s | name: %s | data: %j', this.identifier, this.queueName, id, name, data);
|
|
50
50
|
}, {
|
|
51
51
|
connection: this.connection,
|
|
52
52
|
concurrency: this.numberOfWorker,
|
|
53
53
|
lockDuration: this.lockDuration,
|
|
54
54
|
});
|
|
55
|
-
this.worker.on(
|
|
55
|
+
this.worker.on('completed', (job, result) => {
|
|
56
56
|
this.onWorkerDataCompleted?.(job, result)
|
|
57
57
|
.then(() => {
|
|
58
58
|
// Do something after processing completed job
|
|
59
59
|
})
|
|
60
60
|
.catch(error => {
|
|
61
|
-
this.logger.error(
|
|
61
|
+
this.logger.error('[Worker][%s][completed] queue: %s | Error while processing completed job! Error: %s', this.identifier, this.queueName, error);
|
|
62
62
|
});
|
|
63
63
|
});
|
|
64
|
-
this.worker.on(
|
|
64
|
+
this.worker.on('failed', (job, reason) => {
|
|
65
65
|
this.onWorkerDataFail?.(job, reason)
|
|
66
66
|
.then(() => {
|
|
67
67
|
// Do something after processing failed job
|
|
68
68
|
})
|
|
69
69
|
.catch(error => {
|
|
70
|
-
this.logger.error(
|
|
70
|
+
this.logger.error('[Worker][%s][failed] queue: %s | Error while processing completed job! Error: %s', this.identifier, this.queueName, error);
|
|
71
71
|
});
|
|
72
72
|
});
|
|
73
73
|
}
|
|
74
74
|
configure() {
|
|
75
75
|
if (!this.role) {
|
|
76
|
-
this.logger.error(
|
|
76
|
+
this.logger.error('[configure][%s] Invalid client role to configure | Valid roles: [queue|worker]', this.identifier);
|
|
77
77
|
return;
|
|
78
78
|
}
|
|
79
79
|
switch (this.role) {
|
|
80
|
-
case
|
|
80
|
+
case 'queue': {
|
|
81
81
|
this.configureQueue();
|
|
82
82
|
break;
|
|
83
83
|
}
|
|
84
|
-
case
|
|
84
|
+
case 'worker': {
|
|
85
85
|
this.configureWorker();
|
|
86
86
|
break;
|
|
87
87
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from
|
|
1
|
+
export * from './helper';
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from
|
|
1
|
+
export * from './types';
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export type TBullQueueRole =
|
|
1
|
+
export type TBullQueueRole = 'queue' | 'worker';
|
|
2
2
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
1
|
+
export * from './bullmq';
|
|
2
|
+
export * from './common';
|
|
3
|
+
export * from './internal';
|
|
4
|
+
export * from './mqtt';
|
|
5
5
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BaseHelper } from
|
|
2
|
-
import { ValueOf, ValueOrPromise } from
|
|
1
|
+
import { BaseHelper } from '../../../helpers/base';
|
|
2
|
+
import { ValueOf, ValueOrPromise } from '../../../common/types';
|
|
3
3
|
export declare class QueueStatuses {
|
|
4
4
|
static readonly WAITING = "000_WAITING";
|
|
5
5
|
static readonly PROCESSING = "100_PROCESSING";
|
|
@@ -8,7 +8,7 @@ export declare class QueueStatuses {
|
|
|
8
8
|
static readonly SCHEME_SET: Set<string>;
|
|
9
9
|
static isValid(scheme: string): boolean;
|
|
10
10
|
}
|
|
11
|
-
export type TQueueStatus = ValueOf<Omit<typeof QueueStatuses,
|
|
11
|
+
export type TQueueStatus = ValueOf<Omit<typeof QueueStatuses, 'isValid' | 'SCHEME_SET'>>;
|
|
12
12
|
export type TQueueElement<T> = {
|
|
13
13
|
isLocked: boolean;
|
|
14
14
|
payload: T;
|
|
@@ -8,10 +8,10 @@ const base_1 = require("../../../helpers/base");
|
|
|
8
8
|
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
|
|
9
9
|
// --------------------------------------------------------
|
|
10
10
|
class QueueStatuses {
|
|
11
|
-
static { this.WAITING =
|
|
12
|
-
static { this.PROCESSING =
|
|
13
|
-
static { this.LOCKED =
|
|
14
|
-
static { this.SETTLED =
|
|
11
|
+
static { this.WAITING = '000_WAITING'; }
|
|
12
|
+
static { this.PROCESSING = '100_PROCESSING'; }
|
|
13
|
+
static { this.LOCKED = '200_LOCKED'; }
|
|
14
|
+
static { this.SETTLED = '300_SETTLED'; }
|
|
15
15
|
static { this.SCHEME_SET = new Set([this.WAITING, this.PROCESSING, this.LOCKED, this.SETTLED]); }
|
|
16
16
|
static isValid(scheme) {
|
|
17
17
|
return this.SCHEME_SET.has(scheme);
|
|
@@ -40,12 +40,12 @@ class QueueHelper extends base_1.BaseHelper {
|
|
|
40
40
|
async handleMessage() {
|
|
41
41
|
const current = this.getElementAt(0);
|
|
42
42
|
if (!current) {
|
|
43
|
-
this.logger.warn(
|
|
43
|
+
this.logger.warn('[handleMessage] current: %j | Invalid current message to handle!', current);
|
|
44
44
|
return;
|
|
45
45
|
}
|
|
46
46
|
const { isLocked, payload } = current;
|
|
47
47
|
if (isLocked) {
|
|
48
|
-
this.logger.info(
|
|
48
|
+
this.logger.info('[handle] Skip LOCKED message | Payload: %j', payload);
|
|
49
49
|
return;
|
|
50
50
|
}
|
|
51
51
|
if (this.state !== QueueStatuses.LOCKED && this.state !== QueueStatuses.SETTLED) {
|
|
@@ -77,7 +77,7 @@ class QueueHelper extends base_1.BaseHelper {
|
|
|
77
77
|
}
|
|
78
78
|
*_messageListener() {
|
|
79
79
|
if (!this.onMessage) {
|
|
80
|
-
this.logger.warn(
|
|
80
|
+
this.logger.warn('[_messageListener] Queue has no onMessage listener | Skip initializing message iterator!');
|
|
81
81
|
return;
|
|
82
82
|
}
|
|
83
83
|
while (true) {
|
|
@@ -86,14 +86,14 @@ class QueueHelper extends base_1.BaseHelper {
|
|
|
86
86
|
}
|
|
87
87
|
nextMessage() {
|
|
88
88
|
if (this.state !== QueueStatuses.WAITING) {
|
|
89
|
-
this.logger.warn(
|
|
89
|
+
this.logger.warn('[nextMessage] SKIP request next message | Invalid queue state to request next message | currentState: %s', this.state);
|
|
90
90
|
return;
|
|
91
91
|
}
|
|
92
92
|
this.generator.next();
|
|
93
93
|
}
|
|
94
94
|
async enqueue(payload) {
|
|
95
95
|
if (this.isSettleRequested || this.state === QueueStatuses.SETTLED) {
|
|
96
|
-
this.logger.error(
|
|
96
|
+
this.logger.error('[enqueue] isSettled: %s | currentState: %s | Queue was SETTLED | No more element acceptable', this.isSettleRequested, this.state);
|
|
97
97
|
return;
|
|
98
98
|
}
|
|
99
99
|
if (!this.storage) {
|
|
@@ -119,7 +119,7 @@ class QueueHelper extends base_1.BaseHelper {
|
|
|
119
119
|
}
|
|
120
120
|
lock() {
|
|
121
121
|
if (this.state >= QueueStatuses.LOCKED) {
|
|
122
|
-
this.logger.error(
|
|
122
|
+
this.logger.error('[lock] isSettled | currentState: %s | Invalid queue state to request lock queue!', this.isSettleRequested, this.state);
|
|
123
123
|
return;
|
|
124
124
|
}
|
|
125
125
|
const snap = this.state;
|
|
@@ -128,7 +128,7 @@ class QueueHelper extends base_1.BaseHelper {
|
|
|
128
128
|
}
|
|
129
129
|
unlock(opts) {
|
|
130
130
|
if (this.state > QueueStatuses.LOCKED) {
|
|
131
|
-
this.logger.error(
|
|
131
|
+
this.logger.error('[unlock] isSettled | currentState: %s | Invalid queue state to request unlock queue!', this.isSettleRequested, this.state);
|
|
132
132
|
return;
|
|
133
133
|
}
|
|
134
134
|
const { shouldProcessNextElement = true } = opts;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from
|
|
1
|
+
export * from './helper';
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -23,30 +23,30 @@ class MQTTClientHelper extends base_1.BaseHelper {
|
|
|
23
23
|
// -------------------------------------------------------------------------------
|
|
24
24
|
configure() {
|
|
25
25
|
if (this.client) {
|
|
26
|
-
this.logger.info(
|
|
26
|
+
this.logger.info('[configure][%s] MQTT Client already established! Client: %j', this.identifier, this.client);
|
|
27
27
|
return;
|
|
28
28
|
}
|
|
29
29
|
if ((0, isEmpty_1.default)(this.url)) {
|
|
30
30
|
throw (0, error_1.getError)({
|
|
31
31
|
statusCode: 500,
|
|
32
|
-
message:
|
|
32
|
+
message: '[configure] Invalid url to configure mqtt client!',
|
|
33
33
|
});
|
|
34
34
|
}
|
|
35
|
-
this.logger.info(
|
|
35
|
+
this.logger.info('[configure][%s] Start configuring mqtt client | Url: %s | Options: %j', this.identifier, this.url, this.options);
|
|
36
36
|
this.client = mqtt_1.default.connect(this.url, this.options);
|
|
37
|
-
this.client.on(
|
|
37
|
+
this.client.on('connect', () => {
|
|
38
38
|
this.onConnect?.();
|
|
39
39
|
});
|
|
40
|
-
this.client.on(
|
|
40
|
+
this.client.on('disconnect', () => {
|
|
41
41
|
this.onDisconnect?.();
|
|
42
42
|
});
|
|
43
|
-
this.client.on(
|
|
43
|
+
this.client.on('message', (topic, message) => {
|
|
44
44
|
this.onMessage?.({ topic, message });
|
|
45
45
|
});
|
|
46
|
-
this.client.on(
|
|
46
|
+
this.client.on('error', error => {
|
|
47
47
|
this.onError?.(error);
|
|
48
48
|
});
|
|
49
|
-
this.client.on(
|
|
49
|
+
this.client.on('close', (error) => {
|
|
50
50
|
this.onClose?.(error);
|
|
51
51
|
});
|
|
52
52
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from
|
|
1
|
+
export * from './helper';
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Cluster } from
|
|
2
|
-
import { DefaultRedisHelper } from
|
|
3
|
-
import { IRedisClusterHelperOptions } from
|
|
1
|
+
import { Cluster } from 'ioredis';
|
|
2
|
+
import { DefaultRedisHelper } from './default.helper';
|
|
3
|
+
import { IRedisClusterHelperOptions } from './types';
|
|
4
4
|
export declare class RedisClusterHelper extends DefaultRedisHelper {
|
|
5
5
|
constructor(opts: IRedisClusterHelperOptions);
|
|
6
6
|
getClient(): Cluster;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BaseHelper } from
|
|
2
|
-
import { Cluster, Redis } from
|
|
3
|
-
import { IRedisHelperCallbacks } from
|
|
1
|
+
import { BaseHelper } from '../../helpers/base';
|
|
2
|
+
import { Cluster, Redis } from 'ioredis';
|
|
3
|
+
import { IRedisHelperCallbacks } from './types';
|
|
4
4
|
export declare class DefaultRedisHelper extends BaseHelper {
|
|
5
5
|
client: Redis | Cluster;
|
|
6
6
|
name: string;
|
|
@@ -15,20 +15,20 @@ class DefaultRedisHelper extends base_1.BaseHelper {
|
|
|
15
15
|
this.name = opts.identifier;
|
|
16
16
|
this.client = opts.client;
|
|
17
17
|
const { onInitialized, onConnected, onReady, onError } = opts;
|
|
18
|
-
this.client.on(
|
|
19
|
-
this.logger.info(
|
|
18
|
+
this.client.on('connect', () => {
|
|
19
|
+
this.logger.info('[%s][connect] Redis CONNECTED', this.name);
|
|
20
20
|
onConnected?.({ name: this.name, helper: this });
|
|
21
21
|
});
|
|
22
|
-
this.client.on(
|
|
23
|
-
this.logger.info(
|
|
22
|
+
this.client.on('ready', () => {
|
|
23
|
+
this.logger.info('[%s][ready] Redis READY', this.name);
|
|
24
24
|
onReady?.({ name: this.name, helper: this });
|
|
25
25
|
});
|
|
26
|
-
this.client.on(
|
|
27
|
-
this.logger.error(
|
|
26
|
+
this.client.on('error', (error) => {
|
|
27
|
+
this.logger.error('[%s][error] Redis ERROR | Error: %s', this.name, error);
|
|
28
28
|
onError?.({ name: this.name, helper: this, error });
|
|
29
29
|
});
|
|
30
|
-
this.client.on(
|
|
31
|
-
this.logger.warn(
|
|
30
|
+
this.client.on('reconnecting', () => {
|
|
31
|
+
this.logger.warn('[%s][reconnecting] Redis client RECONNECTING', this.name);
|
|
32
32
|
});
|
|
33
33
|
onInitialized?.({ name: this.name, helper: this });
|
|
34
34
|
}
|
|
@@ -41,19 +41,19 @@ class DefaultRedisHelper extends base_1.BaseHelper {
|
|
|
41
41
|
connect() {
|
|
42
42
|
return new Promise((resolve, reject) => {
|
|
43
43
|
const invalidStatuses = [
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
'ready',
|
|
45
|
+
'reconnecting',
|
|
46
|
+
'connecting',
|
|
47
47
|
];
|
|
48
48
|
if (!this.client || invalidStatuses.includes(this.client.status)) {
|
|
49
|
-
this.logger.info(
|
|
49
|
+
this.logger.info('[connect] status: %s | Invalid redis status to invoke connect', this.client.status);
|
|
50
50
|
resolve(false);
|
|
51
51
|
return;
|
|
52
52
|
}
|
|
53
53
|
this.client
|
|
54
54
|
.connect()
|
|
55
55
|
.then(() => {
|
|
56
|
-
resolve(this.client.status ===
|
|
56
|
+
resolve(this.client.status === 'ready');
|
|
57
57
|
})
|
|
58
58
|
.catch(reject);
|
|
59
59
|
});
|
|
@@ -61,16 +61,16 @@ class DefaultRedisHelper extends base_1.BaseHelper {
|
|
|
61
61
|
// ---------------------------------------------------------------------------------
|
|
62
62
|
disconnect() {
|
|
63
63
|
return new Promise((resolve, reject) => {
|
|
64
|
-
const invalidStatuses = [
|
|
64
|
+
const invalidStatuses = ['end', 'close'];
|
|
65
65
|
if (!this.client || invalidStatuses.includes(this.client.status)) {
|
|
66
|
-
this.logger.info(
|
|
66
|
+
this.logger.info('[disconnect] status: %s | Invalid redis status to invoke connect', this.client.status);
|
|
67
67
|
resolve(false);
|
|
68
68
|
return;
|
|
69
69
|
}
|
|
70
70
|
this.client
|
|
71
71
|
.quit()
|
|
72
72
|
.then(rs => {
|
|
73
|
-
resolve(rs ===
|
|
73
|
+
resolve(rs === 'OK');
|
|
74
74
|
})
|
|
75
75
|
.catch(reject);
|
|
76
76
|
});
|
|
@@ -79,7 +79,7 @@ class DefaultRedisHelper extends base_1.BaseHelper {
|
|
|
79
79
|
async set(opts) {
|
|
80
80
|
const { key, value, options = { log: false } } = opts;
|
|
81
81
|
if (!this.client) {
|
|
82
|
-
this.logger.info(
|
|
82
|
+
this.logger.info('[set] No valid Redis connection!');
|
|
83
83
|
return;
|
|
84
84
|
}
|
|
85
85
|
const serialized = JSON.stringify(value);
|
|
@@ -93,7 +93,7 @@ class DefaultRedisHelper extends base_1.BaseHelper {
|
|
|
93
93
|
async get(opts) {
|
|
94
94
|
const { key, transform = (input) => input } = opts;
|
|
95
95
|
if (!this.client) {
|
|
96
|
-
this.logger.info(
|
|
96
|
+
this.logger.info('[get] No valid Redis connection!');
|
|
97
97
|
return null;
|
|
98
98
|
}
|
|
99
99
|
const value = await this.client.get(key);
|
|
@@ -132,7 +132,7 @@ class DefaultRedisHelper extends base_1.BaseHelper {
|
|
|
132
132
|
// ---------------------------------------------------------------------------------
|
|
133
133
|
async hset(opts) {
|
|
134
134
|
if (!this.client) {
|
|
135
|
-
this.logger.info(
|
|
135
|
+
this.logger.info('[hset] No valid Redis connection!');
|
|
136
136
|
return;
|
|
137
137
|
}
|
|
138
138
|
const { key, value, options } = opts;
|
|
@@ -140,7 +140,7 @@ class DefaultRedisHelper extends base_1.BaseHelper {
|
|
|
140
140
|
if (!options?.log) {
|
|
141
141
|
return rs;
|
|
142
142
|
}
|
|
143
|
-
this.logger.info(
|
|
143
|
+
this.logger.info('[hset] Result: %j', rs);
|
|
144
144
|
return rs;
|
|
145
145
|
}
|
|
146
146
|
// ---------------------------------------------------------------------------------
|
|
@@ -151,7 +151,7 @@ class DefaultRedisHelper extends base_1.BaseHelper {
|
|
|
151
151
|
async hgetall(opts) {
|
|
152
152
|
const { key, transform } = opts;
|
|
153
153
|
if (!this.client) {
|
|
154
|
-
this.logger.info(
|
|
154
|
+
this.logger.info('[get] No valid Redis connection!');
|
|
155
155
|
return null;
|
|
156
156
|
}
|
|
157
157
|
const value = await this.client.hgetall(key);
|
|
@@ -167,7 +167,7 @@ class DefaultRedisHelper extends base_1.BaseHelper {
|
|
|
167
167
|
// ---------------------------------------------------------------------------------
|
|
168
168
|
async mset(opts) {
|
|
169
169
|
if (!this.client) {
|
|
170
|
-
this.logger.info(
|
|
170
|
+
this.logger.info('[set] No valid Redis connection!');
|
|
171
171
|
return;
|
|
172
172
|
}
|
|
173
173
|
const { payload, options } = opts;
|
|
@@ -179,7 +179,7 @@ class DefaultRedisHelper extends base_1.BaseHelper {
|
|
|
179
179
|
if (!options?.log) {
|
|
180
180
|
return;
|
|
181
181
|
}
|
|
182
|
-
this.logger.info(
|
|
182
|
+
this.logger.info('[mset] Payload: %j', serialized);
|
|
183
183
|
}
|
|
184
184
|
// ---------------------------------------------------------------------------------
|
|
185
185
|
mSet(opts) {
|
|
@@ -189,7 +189,7 @@ class DefaultRedisHelper extends base_1.BaseHelper {
|
|
|
189
189
|
async mget(opts) {
|
|
190
190
|
const { keys, transform = (input) => input } = opts;
|
|
191
191
|
if (!this.client) {
|
|
192
|
-
this.logger.info(
|
|
192
|
+
this.logger.info('[get] No valid Redis connection!');
|
|
193
193
|
return null;
|
|
194
194
|
}
|
|
195
195
|
const values = await this.client.mget(keys);
|
|
@@ -206,7 +206,7 @@ class DefaultRedisHelper extends base_1.BaseHelper {
|
|
|
206
206
|
async keys(opts) {
|
|
207
207
|
const { key } = opts;
|
|
208
208
|
if (!this.client) {
|
|
209
|
-
this.logger.info(
|
|
209
|
+
this.logger.info('[keys] No valid Redis connection!');
|
|
210
210
|
return [];
|
|
211
211
|
}
|
|
212
212
|
const existedKeys = await this.client.keys(key);
|
|
@@ -215,37 +215,37 @@ class DefaultRedisHelper extends base_1.BaseHelper {
|
|
|
215
215
|
// ---------------------------------------------------------------------------------
|
|
216
216
|
jSet(opts) {
|
|
217
217
|
const { key, path, value } = opts;
|
|
218
|
-
return this.execute(
|
|
218
|
+
return this.execute('JSON.SET', [key, path, JSON.stringify(value)]);
|
|
219
219
|
}
|
|
220
220
|
// ---------------------------------------------------------------------------------
|
|
221
221
|
jGet(opts) {
|
|
222
|
-
const { key, path =
|
|
223
|
-
return this.execute(
|
|
222
|
+
const { key, path = '$' } = opts;
|
|
223
|
+
return this.execute('JSON.GET', [key, path]);
|
|
224
224
|
}
|
|
225
225
|
// ---------------------------------------------------------------------------------
|
|
226
226
|
jDelete(opts) {
|
|
227
|
-
const { key, path =
|
|
228
|
-
return this.execute(
|
|
227
|
+
const { key, path = '$' } = opts;
|
|
228
|
+
return this.execute('JSON.DEL', [key, path]);
|
|
229
229
|
}
|
|
230
230
|
// ---------------------------------------------------------------------------------
|
|
231
231
|
jNumberIncreaseBy(opts) {
|
|
232
232
|
const { key, path, value } = opts;
|
|
233
|
-
return this.execute(
|
|
233
|
+
return this.execute('JSON.NUMINCRBY', [key, path, value]);
|
|
234
234
|
}
|
|
235
235
|
// ---------------------------------------------------------------------------------
|
|
236
236
|
jStringAppend(opts) {
|
|
237
237
|
const { key, path, value } = opts;
|
|
238
|
-
return this.execute(
|
|
238
|
+
return this.execute('JSON.STRAPPEND', [key, path, value]);
|
|
239
239
|
}
|
|
240
240
|
// ---------------------------------------------------------------------------------
|
|
241
241
|
jPush(opts) {
|
|
242
242
|
const { key, path, value } = opts;
|
|
243
|
-
return this.execute(
|
|
243
|
+
return this.execute('JSON.ARRAPPEND', [key, path, JSON.stringify(value)]);
|
|
244
244
|
}
|
|
245
245
|
// ---------------------------------------------------------------------------------
|
|
246
246
|
jPop(opts) {
|
|
247
247
|
const { key, path } = opts;
|
|
248
|
-
return this.execute(
|
|
248
|
+
return this.execute('JSON.ARRPOP', [key, path]);
|
|
249
249
|
}
|
|
250
250
|
// ---------------------------------------------------------------------------------
|
|
251
251
|
execute(command, parameters) {
|
|
@@ -259,15 +259,15 @@ class DefaultRedisHelper extends base_1.BaseHelper {
|
|
|
259
259
|
const { topics, payload, useCompress = false } = opts;
|
|
260
260
|
const validTopics = topics?.filter(topic => !(0, isEmpty_1.default)(topic));
|
|
261
261
|
if (!validTopics?.length) {
|
|
262
|
-
this.logger.error(
|
|
262
|
+
this.logger.error('[publish] No topic(s) to publish!');
|
|
263
263
|
return;
|
|
264
264
|
}
|
|
265
265
|
if (!payload) {
|
|
266
|
-
this.logger.error(
|
|
266
|
+
this.logger.error('[publish] Invalid payload to publish!');
|
|
267
267
|
return;
|
|
268
268
|
}
|
|
269
269
|
if (!this.client) {
|
|
270
|
-
this.logger.error(
|
|
270
|
+
this.logger.error('[publish] No valid Redis connection!');
|
|
271
271
|
return;
|
|
272
272
|
}
|
|
273
273
|
await Promise.all(validTopics.map(topic => {
|
|
@@ -285,11 +285,11 @@ class DefaultRedisHelper extends base_1.BaseHelper {
|
|
|
285
285
|
subscribe(opts) {
|
|
286
286
|
const { topic } = opts;
|
|
287
287
|
if (!topic || (0, isEmpty_1.default)(topic)) {
|
|
288
|
-
this.logger.error(
|
|
288
|
+
this.logger.error('[subscribe] No topic to subscribe!');
|
|
289
289
|
return;
|
|
290
290
|
}
|
|
291
291
|
if (!this.client) {
|
|
292
|
-
this.logger.error(
|
|
292
|
+
this.logger.error('[subscribe] No valid Redis connection!');
|
|
293
293
|
return;
|
|
294
294
|
}
|
|
295
295
|
this.client.subscribe(topic, (error, count) => {
|
|
@@ -299,7 +299,7 @@ class DefaultRedisHelper extends base_1.BaseHelper {
|
|
|
299
299
|
message: `[subscribe] Failed to subscribe to topic: ${topic}`,
|
|
300
300
|
});
|
|
301
301
|
}
|
|
302
|
-
this.logger.info(
|
|
302
|
+
this.logger.info('[subscribe] Subscribed to %s channel(s). Listening to channel: %s', count, topic);
|
|
303
303
|
});
|
|
304
304
|
}
|
|
305
305
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
1
|
+
export * from './cluster.helper';
|
|
2
|
+
export * from './default.helper';
|
|
3
|
+
export * from './single.helper';
|
|
4
|
+
export * from './types';
|
|
5
5
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import Redis from
|
|
2
|
-
import { DefaultRedisHelper } from
|
|
3
|
-
import { IRedisHelperOptions } from
|
|
1
|
+
import Redis from 'ioredis';
|
|
2
|
+
import { DefaultRedisHelper } from './default.helper';
|
|
3
|
+
import { IRedisHelperOptions } from './types';
|
|
4
4
|
export declare class RedisHelper extends DefaultRedisHelper {
|
|
5
5
|
constructor(opts: IRedisHelperOptions);
|
|
6
6
|
getClient(): Redis;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ClusterOptions } from
|
|
2
|
-
import { DefaultRedisHelper } from
|
|
1
|
+
import { ClusterOptions } from 'ioredis';
|
|
2
|
+
import { DefaultRedisHelper } from './default.helper';
|
|
3
3
|
export interface IRedisHelperProps {
|
|
4
4
|
name: string;
|
|
5
5
|
host: string;
|
|
@@ -12,7 +12,7 @@ export interface IRedisHelperProps {
|
|
|
12
12
|
}
|
|
13
13
|
export interface IRedisClusterHelperProps {
|
|
14
14
|
name: string;
|
|
15
|
-
nodes: Array<Pick<IRedisHelperProps,
|
|
15
|
+
nodes: Array<Pick<IRedisHelperProps, 'host' | 'port'> & {
|
|
16
16
|
password?: string;
|
|
17
17
|
}>;
|
|
18
18
|
clusterOptions?: ClusterOptions;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BaseHelper } from
|
|
2
|
-
import { type Socket } from
|
|
3
|
-
import { ISocketIOClientOptions } from
|
|
1
|
+
import { BaseHelper } from '../../../helpers/base';
|
|
2
|
+
import { type Socket } from 'socket.io-client';
|
|
3
|
+
import { ISocketIOClientOptions } from '../common';
|
|
4
4
|
export declare class SocketIOClientHelper extends BaseHelper {
|
|
5
5
|
private host;
|
|
6
6
|
private options;
|
|
@@ -16,7 +16,7 @@ class SocketIOClientHelper extends base_1.BaseHelper {
|
|
|
16
16
|
// -----------------------------------------------------------------
|
|
17
17
|
configure() {
|
|
18
18
|
if (this.client) {
|
|
19
|
-
this.logger.info(
|
|
19
|
+
this.logger.info('[configure][%s] SocketIO Client already established! Client: %j', this.identifier, this.client);
|
|
20
20
|
return;
|
|
21
21
|
}
|
|
22
22
|
// validateModule({ scope: SocketIOClientHelper.name, modules: ['socket.io-client'] });
|
|
@@ -30,15 +30,15 @@ class SocketIOClientHelper extends base_1.BaseHelper {
|
|
|
30
30
|
subscribe(opts) {
|
|
31
31
|
const { events: eventHandlers, ignoreDuplicate = false } = opts;
|
|
32
32
|
const eventNames = Object.keys(eventHandlers);
|
|
33
|
-
this.logger.info(
|
|
33
|
+
this.logger.info('[subscribe][%s] Handling events: %j', this.identifier, eventNames);
|
|
34
34
|
for (const eventName of eventNames) {
|
|
35
35
|
const handler = eventHandlers[eventName];
|
|
36
36
|
if (!handler) {
|
|
37
|
-
this.logger.info(
|
|
37
|
+
this.logger.info('[subscribe][%s] Ignore handling event %s because of no handler!', this.identifier, eventName);
|
|
38
38
|
continue;
|
|
39
39
|
}
|
|
40
40
|
if (ignoreDuplicate && this.client.hasListeners(eventName)) {
|
|
41
|
-
this.logger.info(
|
|
41
|
+
this.logger.info('[subscribe][%s] Ignore handling event %s because of duplicate handler!', this.identifier, eventName);
|
|
42
42
|
continue;
|
|
43
43
|
}
|
|
44
44
|
this.client.on(eventName, (...props) => {
|
|
@@ -49,7 +49,7 @@ class SocketIOClientHelper extends base_1.BaseHelper {
|
|
|
49
49
|
// -----------------------------------------------------------------
|
|
50
50
|
unsubscribe(opts) {
|
|
51
51
|
const { events: eventNames } = opts;
|
|
52
|
-
this.logger.info(
|
|
52
|
+
this.logger.info('[unsubscribe][%s] Handling events: %j', this.identifier, eventNames);
|
|
53
53
|
for (const eventName of eventNames) {
|
|
54
54
|
if (!this.client?.hasListeners(eventName)) {
|
|
55
55
|
continue;
|
|
@@ -60,7 +60,7 @@ class SocketIOClientHelper extends base_1.BaseHelper {
|
|
|
60
60
|
// -----------------------------------------------------------------
|
|
61
61
|
connect() {
|
|
62
62
|
if (!this.client) {
|
|
63
|
-
this.logger.info(
|
|
63
|
+
this.logger.info('[connect][%s] Invalid client to connect!', this.identifier);
|
|
64
64
|
return;
|
|
65
65
|
}
|
|
66
66
|
this.client.connect();
|
|
@@ -68,7 +68,7 @@ class SocketIOClientHelper extends base_1.BaseHelper {
|
|
|
68
68
|
// -----------------------------------------------------------------
|
|
69
69
|
disconnect() {
|
|
70
70
|
if (!this.client) {
|
|
71
|
-
this.logger.info(
|
|
71
|
+
this.logger.info('[disconnect][%s] Invalid client to disconnect!', this.identifier);
|
|
72
72
|
return;
|
|
73
73
|
}
|
|
74
74
|
this.client.disconnect();
|
|
@@ -86,7 +86,7 @@ class SocketIOClientHelper extends base_1.BaseHelper {
|
|
|
86
86
|
if (!doLog) {
|
|
87
87
|
return;
|
|
88
88
|
}
|
|
89
|
-
this.logger.info(
|
|
89
|
+
this.logger.info('[emit][%s] Topic: %s | Message: %j', this.identifier, topic, message);
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
exports.SocketIOClientHelper = SocketIOClientHelper;
|