@imqueue/core 2.0.26 → 3.1.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.
Files changed (47) hide show
  1. package/README.md +73 -63
  2. package/index.d.ts +0 -1
  3. package/index.js +8 -9
  4. package/package.json +19 -71
  5. package/src/ClusterManager.d.ts +12 -3
  6. package/src/ClusterManager.js +21 -10
  7. package/src/ClusteredRedisQueue.d.ts +45 -14
  8. package/src/ClusteredRedisQueue.js +186 -168
  9. package/src/IMessageQueue.d.ts +89 -96
  10. package/src/IMessageQueue.js +2 -2
  11. package/src/RedisQueue.d.ts +193 -97
  12. package/src/RedisQueue.js +1328 -1068
  13. package/src/UDPClusterManager.d.ts +100 -26
  14. package/src/UDPClusterManager.js +225 -70
  15. package/src/UDPWorker.d.ts +53 -1
  16. package/src/UDPWorker.js +79 -40
  17. package/src/{promisify.d.ts → helpers/buildOptions.d.ts} +8 -15
  18. package/src/helpers/buildOptions.js +39 -0
  19. package/src/{copyEventEmitter.d.ts → helpers/copyEventEmitter.d.ts} +1 -1
  20. package/src/{copyEventEmitter.js → helpers/copyEventEmitter.js} +3 -4
  21. package/src/helpers/envInt.d.ts +32 -0
  22. package/src/helpers/envInt.js +39 -0
  23. package/src/helpers/escapeRegExp.d.ts +30 -0
  24. package/src/helpers/escapeRegExp.js +36 -0
  25. package/src/helpers/index.d.ts +31 -0
  26. package/src/helpers/index.js +48 -0
  27. package/src/helpers/pack.d.ts +7 -0
  28. package/src/helpers/pack.js +37 -0
  29. package/src/helpers/randomInt.d.ts +31 -0
  30. package/src/helpers/randomInt.js +37 -0
  31. package/src/helpers/sha1.d.ts +7 -0
  32. package/src/helpers/sha1.js +39 -0
  33. package/src/helpers/unpack.d.ts +7 -0
  34. package/src/helpers/unpack.js +37 -0
  35. package/src/index.d.ts +1 -14
  36. package/src/index.js +1 -19
  37. package/src/profile.d.ts +28 -38
  38. package/src/profile.js +100 -95
  39. package/src/redis.d.ts +1 -1
  40. package/src/redis.js +4 -2
  41. package/debug.d.ts +0 -1
  42. package/debug_getter.d.ts +0 -9
  43. package/debug_listener.d.ts +0 -1
  44. package/eslint.config.mjs +0 -98
  45. package/src/promisify.js +0 -106
  46. package/src/uuid.d.ts +0 -6
  47. package/src/uuid.js +0 -65
@@ -1,44 +1,44 @@
1
- import { EventEmitter } from 'events';
2
- import { IMessageQueue, JsonObject, IMQOptions, IMQMode, EventMap } from '.';
3
- export declare const DEFAULT_IMQ_OPTIONS: IMQOptions;
4
- export declare const IMQ_SHUTDOWN_TIMEOUT: number;
5
- /**
6
- * Returns SHA1 hash sum of the given string
1
+ /*!
2
+ * Fast messaging queue over Redis
7
3
  *
8
- * @param {string} str
9
- * @returns {string}
10
- */
11
- export declare function sha1(str: string): string;
12
- /**
13
- * Returns random integer between given min and max
4
+ * I'm Queue Software Project
5
+ * Copyright (C) 2025 imqueue.com <support@imqueue.com>
14
6
  *
15
- * @param {number} min
16
- * @param {number} max
17
- * @returns {number}
18
- */
19
- export declare function intrand(min: number, max: number): number;
20
- /**
21
- * Compress given data and returns binary string
7
+ * This program is free software: you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation, either version 3 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * This program is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU General Public License for more details.
22
16
  *
23
- * @param {any} data
24
- * @returns {string}
17
+ * You should have received a copy of the GNU General Public License
18
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
+ *
20
+ * If you want to use this code in a closed source (commercial) project, you can
21
+ * purchase a proprietary commercial license. Please contact us at
22
+ * <support@imqueue.com> to get commercial licensing options.
25
23
  */
26
- export declare function pack(data: any): string;
24
+ import { EventEmitter } from 'node:events';
25
+ import { IMessageQueue, JsonObject, IMQOptions, IMQMode, EventMap } from '.';
26
+ export declare const DEFAULT_IMQ_OPTIONS: IMQOptions;
27
+ export declare const IMQ_SHUTDOWN_TIMEOUT: number;
27
28
  /**
28
- * Decompress binary string and returns plain data
29
- *
30
- * @param {string} data
31
- * @returns {any}
29
+ * Grace period (ms) for a graceful QUIT to complete before a channel is
30
+ * forcibly disconnected. A reader blocked on an infinite BRPOP/BLMOVE can
31
+ * never let QUIT through, so without this the socket would leak and keep
32
+ * the process alive.
32
33
  */
33
- export declare function unpack(data: string): any;
34
+ export declare const IMQ_CONNECTION_QUIT_TIMEOUT: number;
34
35
  /**
35
36
  * Class RedisQueue
36
- * Implements simple messaging queue over redis.
37
+ * Implements a simple messaging queue over redis.
37
38
  */
38
39
  export declare class RedisQueue extends EventEmitter<EventMap> implements IMessageQueue {
39
40
  name: string;
40
41
  private readonly mode;
41
- [name: string]: any;
42
42
  /**
43
43
  * Writer connections collection
44
44
  *
@@ -51,6 +51,24 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
51
51
  * @type {{}}
52
52
  */
53
53
  private static watchers;
54
+ /**
55
+ * Number of started queue instances per shared writer connection key
56
+ *
57
+ * @type {{}}
58
+ */
59
+ private static writerRefs;
60
+ /**
61
+ * All started queue instances within the current process
62
+ *
63
+ * @type {Set<RedisQueue>}
64
+ */
65
+ private static readonly instances;
66
+ /**
67
+ * True when process-level signal handlers were bound
68
+ *
69
+ * @type {boolean}
70
+ */
71
+ private static signalsBound;
54
72
  /**
55
73
  * @event message (message: JsonObject, id: string, from: string)
56
74
  */
@@ -98,15 +116,34 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
98
116
  */
99
117
  private watchOwner;
100
118
  /**
101
- * Signals initialization state
119
+ * Subscription handlers registered through subscribe(). Kept to be
120
+ * able to restore the subscription after a connection replacement.
102
121
  *
103
- * @type {boolean}
122
+ * @type {Array<(data: JsonObject) => void>}
104
123
  */
105
- private signalsInitialized;
124
+ private subscriptionHandlers;
106
125
  /**
107
126
  * Will store check interval reference
108
127
  */
109
- private safeCheckInterval;
128
+ private safeCheckInterval?;
129
+ /**
130
+ * Periodic watcher existence check interval reference
131
+ */
132
+ private watcherCheckInterval?;
133
+ /**
134
+ * Guards overlapping watcher check runs
135
+ */
136
+ private watcherCheckBusy;
137
+ /**
138
+ * Connected client keys seen during the previous cleanup sweep. Used
139
+ * to give temporarily disconnected clients one sweep of grace before
140
+ * removing their keys.
141
+ */
142
+ private lastConnectedKeys;
143
+ /**
144
+ * True while this instance holds a reference to the shared writer
145
+ */
146
+ private writerAcquired;
110
147
  /**
111
148
  * Internal per-channel reconnection state
112
149
  */
@@ -118,27 +155,26 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
118
155
  */
119
156
  readonly redisKey: string;
120
157
  /**
121
- * LUA scripts for redis
158
+ * Lua scripts for redis
122
159
  *
123
160
  * @type {{moveDelayed: {code: string}}}
124
161
  */
125
162
  private scripts;
126
163
  /**
127
- * Serializes given data object into string
164
+ * Serializes a given data object into string
128
165
  *
129
- * @param {any} data
166
+ * @param {unknown} data
130
167
  * @returns {string}
131
168
  */
132
169
  private readonly pack;
133
170
  /**
134
- * Deserialize string data into object
171
+ * Deserialize string data into an object
135
172
  *
136
173
  * @param {string} data
137
- * @returns {any}
174
+ * @returns {unknown}
138
175
  */
139
176
  private readonly unpack;
140
177
  /**
141
- * @constructor
142
178
  * @param {string} name
143
179
  * @param {IMQOptions} [options]
144
180
  * @param {IMQMode} [mode]
@@ -150,14 +186,30 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
150
186
  * data read handler
151
187
  *
152
188
  * @param {string} channel
153
- * @param {(data: JsonObject) => any} handler
154
- * @return {Promise<void>}
189
+ * @param {(data: JsonObject) => void} handler
190
+ * @returns {Promise<void>}
155
191
  */
156
- subscribe(channel: string, handler: (data: JsonObject) => any): Promise<void>;
192
+ subscribe(channel: string, handler: (data: JsonObject) => void): Promise<void>;
193
+ /**
194
+ * Attaches a subscription message handler to a given channel
195
+ * connection
196
+ *
197
+ * @param {IRedisClient} chan
198
+ * @param {(data: JsonObject) => void} handler
199
+ */
200
+ private attachSubscriptionHandler;
201
+ /**
202
+ * Restores the subscription state on a freshly created subscription
203
+ * connection. Used after a connection replacement on reconnection,
204
+ * otherwise the new connection would silently stay unsubscribed.
205
+ *
206
+ * @returns {Promise<void>}
207
+ */
208
+ private restoreSubscription;
157
209
  /**
158
210
  * Closes subscription channel
159
211
  *
160
- * @return {Promise<void>}
212
+ * @returns {Promise<void>}
161
213
  */
162
214
  unsubscribe(): Promise<void>;
163
215
  /**
@@ -178,6 +230,37 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
178
230
  * @returns {Promise<RedisQueue>}
179
231
  */
180
232
  start(): Promise<RedisQueue>;
233
+ /**
234
+ * Binds process-level signal handlers once per process. On shutdown
235
+ * signals, frees watcher locks held by any queue instance and exits.
236
+ */
237
+ private static bindSignals;
238
+ /**
239
+ * Frees watcher locks held by all started queue instances and exits
240
+ * the process, forcing exit after IMQ_SHUTDOWN_TIMEOUT at the latest.
241
+ *
242
+ * @returns {Promise<void>}
243
+ */
244
+ private static freeAndExit;
245
+ /**
246
+ * Starts a periodic check ensuring a watcher connection exists across
247
+ * the queue network, re-electing an owner when the previous one died.
248
+ * Also serves as a polling fallback moving due to delayed messages when
249
+ * keyspace notifications are unavailable.
250
+ */
251
+ private startWatcherCheck;
252
+ /**
253
+ * A single watcher-existence check tick: re-elects a watcher owner when
254
+ * none exists and moves due to delayed messages as a keyspace-notification
255
+ * fallback. Errors are contained so the interval never crashes.
256
+ *
257
+ * @returns {Promise<void>}
258
+ */
259
+ private runWatcherCheck;
260
+ /**
261
+ * Stops the periodic watcher check
262
+ */
263
+ private stopWatcherCheck;
181
264
  /**
182
265
  * Sends a given message to a given queue (by name)
183
266
  *
@@ -195,11 +278,15 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
195
278
  */
196
279
  stop(): Promise<RedisQueue>;
197
280
  /**
198
- * Gracefully destroys this queue
281
+ * Gracefully destroys this queue handle. Does not remove queue data
282
+ * from redis unless clearData is explicitly set to true, so that
283
+ * destroying one handle (e.g., on scale-down) never wipes messages
284
+ * still pending for other producers/consumers.
199
285
  *
286
+ * @param {boolean} [clearData] - when true, also clears queue data
200
287
  * @returns {Promise<void>}
201
288
  */
202
- destroy(): Promise<void>;
289
+ destroy(clearData?: boolean): Promise<void>;
203
290
  /**
204
291
  * Clears queue data in redis
205
292
  *
@@ -215,15 +302,25 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
215
302
  /**
216
303
  * Returns true if publisher mode is enabled on this queue, false otherwise.
217
304
  *
218
- * @return {boolean}
305
+ * @returns {boolean}
219
306
  */
220
307
  isPublisher(): boolean;
221
308
  /**
222
309
  * Returns true if worker mode is enabled on this queue, false otherwise.
223
310
  *
224
- * @return {boolean}
311
+ * @returns {boolean}
225
312
  */
226
313
  isWorker(): boolean;
314
+ /**
315
+ * Returns false only when this queue is known to be unable to accept
316
+ * writes right now — i.e., it has a writer connection currently
317
+ * in a non-ready (reconnecting/closed) state. A queue that has not yet
318
+ * connected is considered available, since a sending lazily connects it.
319
+ * Used for health-aware routing in the clustered queue.
320
+ *
321
+ * @returns {boolean}
322
+ */
323
+ get available(): boolean;
227
324
  /**
228
325
  * Writer connection associated with this queue instance
229
326
  *
@@ -249,6 +346,20 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
249
346
  * @param {IRedisClient} conn
250
347
  */
251
348
  private set watcher(value);
349
+ /**
350
+ * Returns the connection currently bound to the given channel, if any.
351
+ *
352
+ * @param {RedisConnectionChannel} channel
353
+ * @returns {IRedisClient | undefined}
354
+ */
355
+ private connectionOf;
356
+ /**
357
+ * Binds the given connection to the given channel.
358
+ *
359
+ * @param {RedisConnectionChannel} channel
360
+ * @param {IRedisClient} conn
361
+ */
362
+ private bindConnection;
252
363
  /**
253
364
  * Logger instance associated with the current queue instance
254
365
  * @type {ILogger}
@@ -257,52 +368,35 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
257
368
  /**
258
369
  * Return a lock key for watcher connection
259
370
  *
260
- * @access private
261
371
  * @returns {string}
262
372
  */
263
373
  private get lockKey();
264
374
  /**
265
375
  * Returns current queue key
266
376
  *
267
- * @access private
268
377
  * @returns {string}
269
378
  */
270
379
  private get key();
271
380
  /**
272
381
  * Destroys watcher channel
273
- *
274
- * @access private
275
382
  */
276
383
  private destroyWatcher;
277
384
  /**
278
385
  * Destroys writer channel
279
- *
280
- * @access private
281
386
  */
282
387
  private destroyWriter;
283
388
  /**
284
389
  * Destroys any channel
285
- *
286
- * @access private
287
390
  */
288
391
  private destroyChannel;
289
392
  /**
290
393
  * Establishes a given connection channel by its name
291
394
  *
292
- * @access private
293
395
  * @param {RedisConnectionChannel} channel
294
396
  * @param {IMQOptions} options
295
- * @param {any} context
296
397
  * @returns {Promise<IRedisClient>}
297
398
  */
298
399
  private connect;
299
- /**
300
- * Builds and returns redis reconnection strategy
301
- *
302
- * @returns {() => (number | void | null)}
303
- * @private
304
- */
305
- private retryStrategy;
306
400
  /**
307
401
  * Schedules custom reconnection for a given channel with capped
308
402
  * exponential backoff
@@ -311,52 +405,54 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
311
405
  * @private
312
406
  */
313
407
  private scheduleReconnect;
408
+ /**
409
+ * Performs a single reconnection attempt for the given channel,
410
+ * rescheduling itself on failure. Errors are handled internally, so the
411
+ * scheduled timer never produces an unhandled rejection.
412
+ *
413
+ * @param {RedisConnectionChannel} channel
414
+ * @returns {Promise<void>}
415
+ */
416
+ private reconnectNow;
314
417
  /**
315
418
  * Generates channel name
316
419
  *
317
420
  * @param {string} contextName
318
421
  * @param {string} prefix
319
422
  * @param {RedisConnectionChannel} name
320
- * @return {string}
423
+ * @returns {string}
321
424
  */
322
425
  private getChannelName;
323
426
  /**
324
427
  * Builds and returns connection error handler
325
428
  *
326
- * @access private
327
- * @param {RedisQueue} context
328
429
  * @param {RedisConnectionChannel} channel
329
- * @return {(err: Error) => void}
430
+ * @returns {(err: Error) => void}
330
431
  */
331
432
  private onErrorHandler;
332
433
  /**
333
434
  * Builds and returns redis connection close handler
334
435
  *
335
- * @access private
336
- * @param {RedisQueue} context
337
436
  * @param {RedisConnectionChannel} channel
338
- * @return {(...args: any[]) => any}
437
+ * @returns {() => void}
339
438
  */
340
439
  private onCloseHandler;
341
440
  /**
342
441
  * Processes given redis-queue message
343
442
  *
344
- * @access private
345
- * @param {[any, any]} msg
443
+ * @param {[string, string]} msg
346
444
  * @returns {RedisQueue}
347
445
  */
348
446
  private process;
349
447
  /**
350
448
  * Returns the number of established watcher connections on redis
351
449
  *
352
- * @access private
353
450
  * @returns {Promise<number>}
354
451
  */
355
452
  private watcherCount;
356
453
  /**
357
454
  * Processes delayed a message by its given redis key
358
455
  *
359
- * @access private
360
456
  * @param {string} key
361
457
  * @returns {Promise<void>}
362
458
  */
@@ -364,44 +460,44 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
364
460
  /**
365
461
  * Watch routine
366
462
  *
367
- * @access private
368
- * @return {Promise<void>}
463
+ * @returns {Promise<void>}
369
464
  */
370
465
  private processWatch;
371
466
  /**
372
467
  * Process given keys from a message queue
373
468
  *
374
- * @access private
375
469
  * @param {string[]} keys
376
470
  * @param {number} now
377
- * @return {Promise<void>}
471
+ * @returns {Promise<void>}
378
472
  */
379
473
  private processKeys;
380
474
  /**
381
475
  * Watch message processor
382
476
  *
383
- * @access private
384
477
  * @param {...any[]} args
385
- * @return {Promise<void>}
478
+ * @returns {Promise<void>}
386
479
  */
387
480
  private onWatchMessage;
388
481
  /**
389
482
  * Clears safe check interval
390
- *
391
- * @access private
392
483
  */
393
484
  private cleanSafeCheckInterval;
394
485
  /**
395
486
  * Setups watch a process on delayed messages
396
487
  *
397
- * @access private
398
488
  * @returns {RedisQueue}
399
489
  */
400
490
  private watch;
491
+ /**
492
+ * A single safe-delivery maintenance tick: recovers messages from dead
493
+ * workers (when safe delivery is on) and prunes orphaned keys.
494
+ *
495
+ * @returns {Promise<void>}
496
+ */
497
+ private runSafeCheck;
401
498
  /**
402
499
  * Cleans up orphaned keys from redis
403
500
  *
404
- * @access private
405
501
  * @returns {Promise<RedisQueue | undefined>}
406
502
  */
407
503
  private processCleanup;
@@ -410,7 +506,12 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
410
506
  */
411
507
  private readUnsafe;
412
508
  /**
413
- * Reliable but slow method of message handling by message queue
509
+ * Reliable but slow method of message handling by message queue.
510
+ *
511
+ * Uses a bounded blocking pop so the lease deadline embedded into the
512
+ * worker key never goes stale: with an infinite block a message
513
+ * arriving long after the pop started would be born with an already
514
+ * expired lease and be immediately re-queued by the watcher.
414
515
  */
415
516
  private readSafe;
416
517
  /**
@@ -422,31 +523,27 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
422
523
  /**
423
524
  * Checks if the watcher connection is locked
424
525
  *
425
- * @access private
426
526
  * @returns {Promise<boolean>}
427
527
  */
428
528
  private isLocked;
429
529
  /**
430
530
  * Locks watcher connection
431
531
  *
432
- * @access private
433
532
  * @returns {Promise<boolean>}
434
533
  */
435
534
  private lock;
436
535
  /**
437
536
  * Unlocks watcher connection
438
537
  *
439
- * @access private
440
538
  * @returns {Promise<boolean>}
441
539
  */
442
540
  private unlock;
443
541
  /**
444
542
  * Emits error
445
543
  *
446
- * @access private
447
544
  * @param {string} eventName
448
545
  * @param {string} message
449
- * @param {Error} err
546
+ * @param {unknown} err
450
547
  */
451
548
  private emitError;
452
549
  /**
@@ -456,14 +553,13 @@ export declare class RedisQueue extends EventEmitter<EventMap> implements IMessa
456
553
  */
457
554
  private ownWatch;
458
555
  /**
459
- * This method returns a watcher lock resolver function
556
+ * Attempts to take over an orphaned watcher lock: if the lock is held
557
+ * but no watcher connection is actually alive, releases and re-acquires
558
+ * ownership. Used to resolve a possible watcher deadlock.
460
559
  *
461
- * @access private
462
- * @param {(...args: any[]) => void} resolve
463
- * @param {(...args: any[]) => void} reject
464
- * @return {() => Promise<any>}
560
+ * @returns {Promise<void>}
465
561
  */
466
- private watchLockResolver;
562
+ private resolveWatchLock;
467
563
  /**
468
564
  * Initializes a single watcher connection across all queues with the same
469
565
  * prefix.