@imqueue/core 2.0.4 → 2.0.6
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 +17 -17
- package/src/ClusterManager.d.ts +2 -1
- package/src/ClusterManager.js +3 -0
- package/src/ClusteredRedisQueue.d.ts +1 -1
- package/src/ClusteredRedisQueue.js +9 -8
- package/src/RedisQueue.d.ts +4 -4
- package/src/RedisQueue.js +12 -9
- package/src/UDPClusterManager.d.ts +1 -9
- package/src/UDPClusterManager.js +28 -11
- package/debug.d.ts +0 -1
- package/debug_getter.d.ts +0 -9
- package/debug_listener.d.ts +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@imqueue/core",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.6",
|
|
4
4
|
"description": "Simple JSON-based messaging queue for inter service communication",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"message-queue",
|
|
@@ -11,8 +11,9 @@
|
|
|
11
11
|
"json-message"
|
|
12
12
|
],
|
|
13
13
|
"scripts": {
|
|
14
|
+
"benchmark": "node benchmark -c $(( $(nproc) - 2 )) -m 100000",
|
|
14
15
|
"prepare": "./node_modules/.bin/tsc",
|
|
15
|
-
"test": "./node_modules/.bin/tsc && ./node_modules/.bin/nyc mocha && ./node_modules/.bin/nyc report --reporter=text-lcov
|
|
16
|
+
"test": "./node_modules/.bin/tsc && ./node_modules/.bin/nyc mocha && ./node_modules/.bin/nyc report --reporter=text-lcov",
|
|
16
17
|
"test-fast": "./node_modules/.bin/tsc && ./node_modules/.bin/nyc mocha && /usr/bin/env node -e \"import('open').then(open => open.default('file://`pwd`/coverage/index.html', { wait: false }))\"",
|
|
17
18
|
"test-local": "export COVERALLS_REPO_TOKEN=$IMQ_COVERALLS_TOKEN && npm test && /usr/bin/env node -e \"import('open').then(open => open.default('https://coveralls.io/github/imqueue/imq', { wait: false }))\"",
|
|
18
19
|
"test-dev": "npm run test && npm run clean-js && npm run clean-typedefs && npm run clean-maps",
|
|
@@ -37,37 +38,36 @@
|
|
|
37
38
|
"author": "imqueue.com <support@imqueue.com> (https://imqueue.com)",
|
|
38
39
|
"license": "GPL-3.0-only",
|
|
39
40
|
"dependencies": {
|
|
40
|
-
"ioredis": "^5.
|
|
41
|
+
"ioredis": "^5.7.0"
|
|
41
42
|
},
|
|
42
43
|
"devDependencies": {
|
|
43
|
-
"@eslint/js": "^9.
|
|
44
|
+
"@eslint/js": "^9.33.0",
|
|
44
45
|
"@types/chai": "^5.2.2",
|
|
45
46
|
"@types/eslint__eslintrc": "^2.1.2",
|
|
46
47
|
"@types/mocha": "^10.0.0",
|
|
47
48
|
"@types/mock-require": "^3.0.0",
|
|
48
|
-
"@types/node": "^24.
|
|
49
|
+
"@types/node": "^24.2.1",
|
|
49
50
|
"@types/sinon": "^17.0.4",
|
|
50
51
|
"@types/yargs": "^17.0.33",
|
|
51
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
52
|
-
"@typescript-eslint/parser": "^8.
|
|
53
|
-
"@typescript-eslint/typescript-estree": "^8.
|
|
54
|
-
"chai": "^5.2.
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"eslint": "^
|
|
58
|
-
"eslint-plugin-jsdoc": "^51.3.1",
|
|
52
|
+
"@typescript-eslint/eslint-plugin": "^8.39.0",
|
|
53
|
+
"@typescript-eslint/parser": "^8.39.0",
|
|
54
|
+
"@typescript-eslint/typescript-estree": "^8.39.0",
|
|
55
|
+
"chai": "^5.2.1",
|
|
56
|
+
"coveralls-next": "^5.0.0",
|
|
57
|
+
"eslint": "^9.33.0",
|
|
58
|
+
"eslint-plugin-jsdoc": "^52.0.4",
|
|
59
59
|
"mocha": "^11.7.1",
|
|
60
60
|
"mocha-lcov-reporter": "^1.3.0",
|
|
61
61
|
"mock-require": "^3.0.3",
|
|
62
62
|
"nyc": "^17.1.0",
|
|
63
|
-
"open": "^10.
|
|
63
|
+
"open": "^10.2.0",
|
|
64
64
|
"reflect-metadata": "^0.2.2",
|
|
65
65
|
"sinon": "^21.0.0",
|
|
66
66
|
"source-map-support": "^0.5.21",
|
|
67
67
|
"ts-node": "^10.9.2",
|
|
68
|
-
"typedoc": "^0.28.
|
|
69
|
-
"typescript": "^5.
|
|
70
|
-
"yargs": "^
|
|
68
|
+
"typedoc": "^0.28.9",
|
|
69
|
+
"typescript": "^5.9.2",
|
|
70
|
+
"yargs": "^18.0.0"
|
|
71
71
|
},
|
|
72
72
|
"main": "index.js",
|
|
73
73
|
"typescript": {
|
package/src/ClusterManager.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ import { IMessageQueueConnection, IServerInput } from './IMessageQueue';
|
|
|
25
25
|
export interface ICluster {
|
|
26
26
|
add: (server: IServerInput) => void;
|
|
27
27
|
remove: (server: IServerInput) => void;
|
|
28
|
-
find: <T extends IMessageQueueConnection>(server: IServerInput) => T | undefined;
|
|
28
|
+
find: <T extends IMessageQueueConnection>(server: IServerInput, strict?: boolean) => T | undefined;
|
|
29
29
|
}
|
|
30
30
|
export interface InitializedCluster extends ICluster {
|
|
31
31
|
id: string;
|
|
@@ -34,6 +34,7 @@ export declare abstract class ClusterManager {
|
|
|
34
34
|
protected clusters: InitializedCluster[];
|
|
35
35
|
protected constructor();
|
|
36
36
|
init(cluster: ICluster): InitializedCluster;
|
|
37
|
+
anyCluster(fn: (cluster: InitializedCluster) => Promise<void> | void): Promise<void>;
|
|
37
38
|
remove(cluster: string | InitializedCluster, destroy?: boolean): Promise<void>;
|
|
38
39
|
abstract destroy(): Promise<void>;
|
|
39
40
|
}
|
package/src/ClusterManager.js
CHANGED
|
@@ -11,6 +11,9 @@ class ClusterManager {
|
|
|
11
11
|
this.clusters.push(initializedCluster);
|
|
12
12
|
return initializedCluster;
|
|
13
13
|
}
|
|
14
|
+
async anyCluster(fn) {
|
|
15
|
+
await Promise.all(this.clusters.map(cluster => fn(cluster)));
|
|
16
|
+
}
|
|
14
17
|
async remove(cluster, destroy = true) {
|
|
15
18
|
const id = typeof cluster === 'string' ? cluster : cluster.id;
|
|
16
19
|
this.clusters = this.clusters.filter(cluster => cluster.id !== id);
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
* <support@imqueue.com> to get commercial licensing options.
|
|
23
23
|
*/
|
|
24
24
|
import { EventEmitter } from 'events';
|
|
25
|
-
import { ILogger, IMessageQueue, IMQMode, IMQOptions,
|
|
25
|
+
import { EventMap, ILogger, IMessageQueue, IMQMode, IMQOptions, IServerInput, JsonObject } from '.';
|
|
26
26
|
/**
|
|
27
27
|
* Class ClusteredRedisQueue
|
|
28
28
|
* Implements the possibility to scale queues horizontally between several
|
|
@@ -367,7 +367,7 @@ class ClusteredRedisQueue {
|
|
|
367
367
|
* @returns {void}
|
|
368
368
|
*/
|
|
369
369
|
removeServer(server) {
|
|
370
|
-
const remove = this.findServer(server);
|
|
370
|
+
const remove = this.findServer(server, true);
|
|
371
371
|
if (!remove) {
|
|
372
372
|
return;
|
|
373
373
|
}
|
|
@@ -391,14 +391,12 @@ class ClusteredRedisQueue {
|
|
|
391
391
|
const opts = Object.assign(Object.assign({}, this.mqOptions), newServer);
|
|
392
392
|
const imq = new _1.RedisQueue(this.name, opts);
|
|
393
393
|
if (initializeQueue) {
|
|
394
|
-
this.initializeQueue(imq)
|
|
395
|
-
.then(() => {
|
|
394
|
+
this.initializeQueue(imq).then(() => {
|
|
396
395
|
this.clusterEmitter.emit('initialized', {
|
|
397
396
|
server: newServer,
|
|
398
397
|
imq,
|
|
399
398
|
});
|
|
400
|
-
})
|
|
401
|
-
.catch();
|
|
399
|
+
});
|
|
402
400
|
}
|
|
403
401
|
newServer.imq = imq;
|
|
404
402
|
this.imqs.push(imq);
|
|
@@ -418,16 +416,19 @@ class ClusteredRedisQueue {
|
|
|
418
416
|
await imq.subscribe(this.state.subscription.channel, this.state.subscription.handler);
|
|
419
417
|
}
|
|
420
418
|
}
|
|
421
|
-
findServer(server) {
|
|
422
|
-
return this.servers.find(existing => ClusteredRedisQueue.matchServers(existing, server));
|
|
419
|
+
findServer(server, strict = false) {
|
|
420
|
+
return this.servers.find(existing => ClusteredRedisQueue.matchServers(existing, server, strict));
|
|
423
421
|
}
|
|
424
|
-
static matchServers(source, target) {
|
|
422
|
+
static matchServers(source, target, strict = false) {
|
|
425
423
|
const sameAddress = target.host === source.host
|
|
426
424
|
&& target.port === source.port;
|
|
427
425
|
if (!target.id && !source.id) {
|
|
428
426
|
return sameAddress;
|
|
429
427
|
}
|
|
430
428
|
const sameId = target.id === source.id;
|
|
429
|
+
if (strict) {
|
|
430
|
+
return sameId && sameAddress;
|
|
431
|
+
}
|
|
431
432
|
return sameId || sameAddress;
|
|
432
433
|
}
|
|
433
434
|
}
|
package/src/RedisQueue.d.ts
CHANGED
|
@@ -92,7 +92,7 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
|
|
|
92
92
|
*/
|
|
93
93
|
private destroyed;
|
|
94
94
|
/**
|
|
95
|
-
* True if the current instance owns watcher connection, false otherwise
|
|
95
|
+
* True if the current instance owns a watcher connection, false otherwise
|
|
96
96
|
*
|
|
97
97
|
* @type {boolean}
|
|
98
98
|
*/
|
|
@@ -230,7 +230,7 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
|
|
|
230
230
|
*/
|
|
231
231
|
private get watcher();
|
|
232
232
|
/**
|
|
233
|
-
* Watcher setter
|
|
233
|
+
* Watcher setter sets the watcher connection property for this
|
|
234
234
|
* queue instance
|
|
235
235
|
*
|
|
236
236
|
* @param {IRedisClient} conn
|
|
@@ -332,14 +332,14 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
|
|
|
332
332
|
*/
|
|
333
333
|
private process;
|
|
334
334
|
/**
|
|
335
|
-
* Returns number of established watcher connections on redis
|
|
335
|
+
* Returns the number of established watcher connections on redis
|
|
336
336
|
*
|
|
337
337
|
* @access private
|
|
338
338
|
* @returns {Promise<number>}
|
|
339
339
|
*/
|
|
340
340
|
private watcherCount;
|
|
341
341
|
/**
|
|
342
|
-
* Processes delayed message by its given redis key
|
|
342
|
+
* Processes delayed a message by its given redis key
|
|
343
343
|
*
|
|
344
344
|
* @access private
|
|
345
345
|
* @param {string} key
|
package/src/RedisQueue.js
CHANGED
|
@@ -128,7 +128,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
128
128
|
*/
|
|
129
129
|
this.destroyed = false;
|
|
130
130
|
/**
|
|
131
|
-
* True if the current instance owns watcher connection, false otherwise
|
|
131
|
+
* True if the current instance owns a watcher connection, false otherwise
|
|
132
132
|
*
|
|
133
133
|
* @type {boolean}
|
|
134
134
|
*/
|
|
@@ -246,6 +246,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
246
246
|
if (this.initialized) {
|
|
247
247
|
return this;
|
|
248
248
|
}
|
|
249
|
+
this.destroyed = false;
|
|
249
250
|
const connPromises = [];
|
|
250
251
|
// istanbul ignore next
|
|
251
252
|
if (!this.reader && this.isWorker()) {
|
|
@@ -277,7 +278,6 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
277
278
|
}
|
|
278
279
|
await this.initWatcher();
|
|
279
280
|
this.initialized = true;
|
|
280
|
-
this.destroyed = false;
|
|
281
281
|
return this;
|
|
282
282
|
}
|
|
283
283
|
/**
|
|
@@ -297,6 +297,9 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
297
297
|
if (!this.writer) {
|
|
298
298
|
await this.start();
|
|
299
299
|
}
|
|
300
|
+
if (!this.writer) {
|
|
301
|
+
throw new TypeError('IMQ: unable to initialize queue!');
|
|
302
|
+
}
|
|
300
303
|
const id = (0, _1.uuid)();
|
|
301
304
|
const data = { id, message, from: this.name };
|
|
302
305
|
const key = `${this.options.prefix}:${toQueue}`;
|
|
@@ -420,7 +423,7 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
420
423
|
}
|
|
421
424
|
// noinspection JSUnusedLocalSymbols
|
|
422
425
|
/**
|
|
423
|
-
* Watcher setter
|
|
426
|
+
* Watcher setter sets the watcher connection property for this
|
|
424
427
|
* queue instance
|
|
425
428
|
*
|
|
426
429
|
* @param {IRedisClient} conn
|
|
@@ -633,22 +636,22 @@ class RedisQueue extends events_1.EventEmitter {
|
|
|
633
636
|
return this;
|
|
634
637
|
}
|
|
635
638
|
/**
|
|
636
|
-
* Returns number of established watcher connections on redis
|
|
639
|
+
* Returns the number of established watcher connections on redis
|
|
637
640
|
*
|
|
638
641
|
* @access private
|
|
639
642
|
* @returns {Promise<number>}
|
|
640
643
|
*/
|
|
641
644
|
// istanbul ignore next
|
|
642
645
|
async watcherCount() {
|
|
646
|
+
if (!this.writer) {
|
|
647
|
+
return 0;
|
|
648
|
+
}
|
|
643
649
|
const rx = new RegExp(`\\bname=${this.options.prefix}:[\\S]+?:watcher:`);
|
|
644
650
|
const list = await this.writer.client('LIST');
|
|
645
|
-
return (
|
|
646
|
-
.split(/\r?\n/)
|
|
647
|
-
.filter(client => rx.test(client))
|
|
648
|
-
.length;
|
|
651
|
+
return list.split(/\r?\n/).filter(client => rx.test(client)).length;
|
|
649
652
|
}
|
|
650
653
|
/**
|
|
651
|
-
* Processes delayed message by its given redis key
|
|
654
|
+
* Processes delayed a message by its given redis key
|
|
652
655
|
*
|
|
653
656
|
* @access private
|
|
654
657
|
* @param {string} key
|
|
@@ -1,18 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ClusterManager } from './ClusterManager';
|
|
2
2
|
export declare const DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS: {
|
|
3
3
|
broadcastPort: number;
|
|
4
4
|
broadcastAddress: string;
|
|
5
5
|
aliveTimeoutCorrection: number;
|
|
6
6
|
};
|
|
7
7
|
export interface UDPClusterManagerOptions {
|
|
8
|
-
/**
|
|
9
|
-
* Represents the cluster operations that are responsible for managing
|
|
10
|
-
* clusters. This includes operations such as adding, removing, or checking
|
|
11
|
-
* if a cluster server exists.
|
|
12
|
-
*
|
|
13
|
-
* @type {ICluster}
|
|
14
|
-
*/
|
|
15
|
-
cluster?: ICluster;
|
|
16
8
|
/**
|
|
17
9
|
* Message queue broadcast port
|
|
18
10
|
*
|
package/src/UDPClusterManager.js
CHANGED
|
@@ -74,7 +74,7 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
|
74
74
|
}
|
|
75
75
|
if (!server && message.type === MessageType.Up) {
|
|
76
76
|
cluster.add(message);
|
|
77
|
-
const added = cluster.find(message);
|
|
77
|
+
const added = cluster.find(message, true);
|
|
78
78
|
if (added) {
|
|
79
79
|
UDPClusterManager.serverAliveWait(cluster, added, aliveTimeoutCorrection);
|
|
80
80
|
}
|
|
@@ -94,9 +94,9 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
|
94
94
|
&& !UDPClusterManager.verifyHosts(message.host, context.options.includeHosts)) {
|
|
95
95
|
return;
|
|
96
96
|
}
|
|
97
|
-
|
|
97
|
+
context.anyCluster(cluster => {
|
|
98
98
|
UDPClusterManager.processMessageOnCluster(cluster, message, context.options.aliveTimeoutCorrection);
|
|
99
|
-
}
|
|
99
|
+
}).then();
|
|
100
100
|
};
|
|
101
101
|
}
|
|
102
102
|
static parseBroadcastedMessage(input) {
|
|
@@ -112,26 +112,40 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
|
112
112
|
};
|
|
113
113
|
}
|
|
114
114
|
static serverAliveWait(cluster, server, aliveTimeoutCorrection, message) {
|
|
115
|
-
|
|
115
|
+
if (server.timer) {
|
|
116
|
+
clearTimeout(server.timer);
|
|
117
|
+
server.timer = undefined;
|
|
118
|
+
}
|
|
116
119
|
server.timestamp = Date.now();
|
|
117
120
|
if (message) {
|
|
118
121
|
server.timeout = message.timeout;
|
|
119
122
|
}
|
|
120
123
|
const correction = aliveTimeoutCorrection || 0;
|
|
121
124
|
const timeout = (server.timeout || 0) + correction;
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
+
if (timeout <= 0) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const timerId = setTimeout(() => {
|
|
129
|
+
const existing = cluster.find(server, true);
|
|
130
|
+
if (!existing || existing.timer !== timerId) {
|
|
125
131
|
return;
|
|
126
132
|
}
|
|
127
133
|
const now = Date.now();
|
|
128
|
-
|
|
134
|
+
if (!existing.timestamp) {
|
|
135
|
+
clearTimeout(existing.timer);
|
|
136
|
+
existing.timer = undefined;
|
|
137
|
+
cluster.remove(existing);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const delta = now - existing.timestamp;
|
|
129
141
|
const currentTimeout = (existing.timeout || 0) + correction;
|
|
130
142
|
if (delta >= currentTimeout) {
|
|
131
|
-
clearTimeout(
|
|
132
|
-
|
|
143
|
+
clearTimeout(existing.timer);
|
|
144
|
+
existing.timer = undefined;
|
|
145
|
+
cluster.remove(existing);
|
|
133
146
|
}
|
|
134
147
|
}, timeout);
|
|
148
|
+
server.timer = timerId;
|
|
135
149
|
}
|
|
136
150
|
/**
|
|
137
151
|
* Destroys the UDPClusterManager by closing all opened network connections
|
|
@@ -152,7 +166,10 @@ class UDPClusterManager extends ClusterManager_1.ClusterManager {
|
|
|
152
166
|
if (typeof socket.close === 'function') {
|
|
153
167
|
socket.removeAllListeners();
|
|
154
168
|
socket.close(() => {
|
|
155
|
-
|
|
169
|
+
// unref may be missing or not a function on mocked sockets
|
|
170
|
+
if (socket && typeof socket.unref === 'function') {
|
|
171
|
+
socket.unref();
|
|
172
|
+
}
|
|
156
173
|
if (socketKey
|
|
157
174
|
&& UDPClusterManager.sockets[socketKey]) {
|
|
158
175
|
delete UDPClusterManager.sockets[socketKey];
|
package/debug.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/debug_getter.d.ts
DELETED
package/debug_listener.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|