@imqueue/core 3.5.4 → 3.7.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/CHANGELOG.md +199 -134
- package/README.md +3 -1
- package/package.json +1 -1
- package/src/ClusterManager.d.ts +13 -1
- package/src/ClusteredRedisQueue.d.ts +9 -1
- package/src/ClusteredRedisQueue.js +41 -3
- package/src/IMessageQueue.d.ts +23 -6
- package/src/RedisQueue.d.ts +50 -4
- package/src/RedisQueue.js +122 -10
- package/src/UDPClusterManager.js +5 -1
- package/src/UDPWorker.d.ts +2 -0
- package/src/UDPWorker.js +5 -2
- package/src/helpers/compress.d.ts +38 -0
- package/src/helpers/compress.js +98 -0
- package/src/helpers/index.d.ts +1 -0
- package/src/helpers/index.js +1 -0
- package/.claude/settings.local.json +0 -9
- package/AGENTS.md +0 -150
- package/CONTRIBUTION-TERMS.md +0 -79
- package/signatures/version1/cla.json +0 -12
package/src/RedisQueue.js
CHANGED
|
@@ -59,7 +59,7 @@ import { EventEmitter } from 'node:events';
|
|
|
59
59
|
import { randomUUID } from 'node:crypto';
|
|
60
60
|
import { hostname } from 'node:os';
|
|
61
61
|
import { IMQMode, profile, } from './index.js';
|
|
62
|
-
import { buildOptions, escapeRegExp, randomInt,
|
|
62
|
+
import { buildOptions, escapeRegExp, randomInt, packer, sha1, unpackAny, envInt, envTls, tlsFingerprint, errorCode, LOG_MAX_KEYS, } from './helpers/index.js';
|
|
63
63
|
import Redis from './redis.js';
|
|
64
64
|
const RX_CLIENT_NAME = /name=(\S+)/g;
|
|
65
65
|
const RX_CLIENT_TEST = /:(reader|writer|watcher)/;
|
|
@@ -269,6 +269,20 @@ let RedisQueue = (() => {
|
|
|
269
269
|
* Reader connection associated with this queue instance
|
|
270
270
|
*/
|
|
271
271
|
reader;
|
|
272
|
+
/**
|
|
273
|
+
* The reader's id on the broker, which {@link RedisQueue.drain} unblocks
|
|
274
|
+
* it by from another connection.
|
|
275
|
+
*/
|
|
276
|
+
readerId;
|
|
277
|
+
/**
|
|
278
|
+
* The read loop running on the reader, settled once it stops.
|
|
279
|
+
*/
|
|
280
|
+
reading;
|
|
281
|
+
/**
|
|
282
|
+
* Set while {@link RedisQueue.drain} runs: the read loop hands on what it
|
|
283
|
+
* already read and stops instead of reading again.
|
|
284
|
+
*/
|
|
285
|
+
draining = false;
|
|
272
286
|
/**
|
|
273
287
|
* Channel connection associated with this queue instance
|
|
274
288
|
* Specially designed for client subscriptions to server-emitted events
|
|
@@ -410,8 +424,9 @@ let RedisQueue = (() => {
|
|
|
410
424
|
* Serializes a given data object into string
|
|
411
425
|
*
|
|
412
426
|
* @param data - the value to serialize; must be JSON-serializable
|
|
413
|
-
* @returns the wire representation
|
|
414
|
-
* {@link IMQOptions.useGzip} is on
|
|
427
|
+
* @returns the wire representation: gzipped when
|
|
428
|
+
* {@link IMQOptions.useGzip} is on, zstd-compressed when it is
|
|
429
|
+
* longer than {@link IMQOptions.compressAbove}, JSON otherwise
|
|
415
430
|
*/
|
|
416
431
|
pack;
|
|
417
432
|
/**
|
|
@@ -419,8 +434,11 @@ let RedisQueue = (() => {
|
|
|
419
434
|
*
|
|
420
435
|
* @param data - the wire representation, as produced by `pack`
|
|
421
436
|
* @returns the deserialized value
|
|
422
|
-
* @throws when the payload is malformed
|
|
423
|
-
*
|
|
437
|
+
* @throws when the payload is malformed
|
|
438
|
+
*
|
|
439
|
+
* @remarks
|
|
440
|
+
* Reads plain, gzipped and zstd messages alike, whatever this queue's own
|
|
441
|
+
* settings, so writers can change format one at a time.
|
|
424
442
|
*/
|
|
425
443
|
unpack;
|
|
426
444
|
/**
|
|
@@ -476,8 +494,11 @@ let RedisQueue = (() => {
|
|
|
476
494
|
this.options.tls = fromEnv;
|
|
477
495
|
}
|
|
478
496
|
}
|
|
479
|
-
|
|
480
|
-
|
|
497
|
+
if (this.options.compressAbove === undefined) {
|
|
498
|
+
this.options.compressAbove = envInt('IMQ_COMPRESS_ABOVE', 0);
|
|
499
|
+
}
|
|
500
|
+
this.pack = packer(!!this.options.useGzip, this.options.compressAbove);
|
|
501
|
+
this.unpack = unpackAny;
|
|
481
502
|
this.redisKey = `${this.options.host}:${this.options.port}`;
|
|
482
503
|
this.poolKey = this.options.tls
|
|
483
504
|
? `${this.redisKey}#${tlsFingerprint(this.options.tls)}`
|
|
@@ -1138,6 +1159,84 @@ let RedisQueue = (() => {
|
|
|
1138
1159
|
get available() {
|
|
1139
1160
|
return !this.writer || this.writer.status === 'ready';
|
|
1140
1161
|
}
|
|
1162
|
+
/**
|
|
1163
|
+
* Whether the broker answers this queue's writer within the given time.
|
|
1164
|
+
*
|
|
1165
|
+
* @param timeoutMs - how long to wait for its answer, in milliseconds
|
|
1166
|
+
* @returns true where a PING came back in time, false otherwise
|
|
1167
|
+
*
|
|
1168
|
+
* @remarks
|
|
1169
|
+
* A connection that only reports itself ready may sit on a socket nobody
|
|
1170
|
+
* answers; this asks the broker itself. Never rejects.
|
|
1171
|
+
*/
|
|
1172
|
+
async answers(timeoutMs) {
|
|
1173
|
+
const writer = this.writer;
|
|
1174
|
+
if (!writer || writer.status !== 'ready' || this.destroyed) {
|
|
1175
|
+
return false;
|
|
1176
|
+
}
|
|
1177
|
+
let timer;
|
|
1178
|
+
try {
|
|
1179
|
+
return await Promise.race([
|
|
1180
|
+
writer.ping().then(reply => reply === 'PONG'),
|
|
1181
|
+
new Promise(resolve => {
|
|
1182
|
+
timer = setTimeout(() => resolve(false), timeoutMs);
|
|
1183
|
+
timer.unref();
|
|
1184
|
+
}),
|
|
1185
|
+
]);
|
|
1186
|
+
}
|
|
1187
|
+
catch {
|
|
1188
|
+
return false;
|
|
1189
|
+
}
|
|
1190
|
+
finally {
|
|
1191
|
+
clearTimeout(timer);
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
/**
|
|
1195
|
+
* Stops reading and hands on every message the reader already took from
|
|
1196
|
+
* the broker, before a teardown that would drop the reader at once.
|
|
1197
|
+
*
|
|
1198
|
+
* @param timeoutMs - how long to wait for the read loop to stop, in
|
|
1199
|
+
* milliseconds
|
|
1200
|
+
*
|
|
1201
|
+
* @remarks
|
|
1202
|
+
* The reader is released from its blocking read by `CLIENT UNBLOCK` sent
|
|
1203
|
+
* on the writer. A message the broker already handed it is still in the
|
|
1204
|
+
* socket, ahead of the release, so it reaches the `message` listeners
|
|
1205
|
+
* before the loop stops; nothing further is taken. Without this, a reply
|
|
1206
|
+
* popped but not yet read was lost with the socket, and its caller waited
|
|
1207
|
+
* out its whole timeout. Never rejects; a host that does not answer is
|
|
1208
|
+
* given up on after `timeoutMs`.
|
|
1209
|
+
*/
|
|
1210
|
+
async drain(timeoutMs = IMQ_CONNECTION_QUIT_TIMEOUT) {
|
|
1211
|
+
const reading = this.reading;
|
|
1212
|
+
if (!this.reader || !reading || this.destroyed) {
|
|
1213
|
+
return;
|
|
1214
|
+
}
|
|
1215
|
+
this.draining = true;
|
|
1216
|
+
let timer;
|
|
1217
|
+
try {
|
|
1218
|
+
if (this.readerId !== undefined && this.writer) {
|
|
1219
|
+
await Promise.race([
|
|
1220
|
+
this.writer.client('UNBLOCK', this.readerId),
|
|
1221
|
+
new Promise(resolve => {
|
|
1222
|
+
timer = setTimeout(resolve, timeoutMs);
|
|
1223
|
+
timer.unref();
|
|
1224
|
+
}),
|
|
1225
|
+
]).catch(() => undefined);
|
|
1226
|
+
clearTimeout(timer);
|
|
1227
|
+
}
|
|
1228
|
+
await Promise.race([
|
|
1229
|
+
reading,
|
|
1230
|
+
new Promise(resolve => {
|
|
1231
|
+
timer = setTimeout(resolve, timeoutMs);
|
|
1232
|
+
timer.unref();
|
|
1233
|
+
}),
|
|
1234
|
+
]);
|
|
1235
|
+
}
|
|
1236
|
+
finally {
|
|
1237
|
+
clearTimeout(timer);
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1141
1240
|
/**
|
|
1142
1241
|
* Writer connection associated with this queue instance. Shared by every
|
|
1143
1242
|
* queue in the process that targets the same {@link RedisQueue.poolKey}.
|
|
@@ -1384,6 +1483,12 @@ let RedisQueue = (() => {
|
|
|
1384
1483
|
this.logger.info('%s: %s channel connected, host %s, pid %s', this.name, channel, this.redisKey, process.pid);
|
|
1385
1484
|
switch (channel) {
|
|
1386
1485
|
case 'reader':
|
|
1486
|
+
// asked before the first blocking read: a blocked reader
|
|
1487
|
+
// answers nothing else until its read returns
|
|
1488
|
+
this.readerId = await redis
|
|
1489
|
+
.client('ID')
|
|
1490
|
+
.then(id => Number(id))
|
|
1491
|
+
.catch(() => undefined);
|
|
1387
1492
|
this.read();
|
|
1388
1493
|
break;
|
|
1389
1494
|
case 'writer':
|
|
@@ -2034,7 +2139,7 @@ let RedisQueue = (() => {
|
|
|
2034
2139
|
try {
|
|
2035
2140
|
const key = this.key;
|
|
2036
2141
|
while (true) {
|
|
2037
|
-
if (!this.reader) {
|
|
2142
|
+
if (!this.reader || this.draining) {
|
|
2038
2143
|
break;
|
|
2039
2144
|
}
|
|
2040
2145
|
try {
|
|
@@ -2093,7 +2198,10 @@ let RedisQueue = (() => {
|
|
|
2093
2198
|
const timeout = Math.max(0.1, Math.min(Number(this.options.safeDeliveryTtl) / 2, READ_MAX_BLOCK) /
|
|
2094
2199
|
1000);
|
|
2095
2200
|
while (true) {
|
|
2096
|
-
if (!this.reader ||
|
|
2201
|
+
if (!this.reader ||
|
|
2202
|
+
!this.writer ||
|
|
2203
|
+
this.destroyed ||
|
|
2204
|
+
this.draining) {
|
|
2097
2205
|
break;
|
|
2098
2206
|
}
|
|
2099
2207
|
const expire = Date.now() + Number(this.options.safeDeliveryTtl);
|
|
@@ -2168,7 +2276,11 @@ let RedisQueue = (() => {
|
|
|
2168
2276
|
const runReader = this.options.safeDelivery
|
|
2169
2277
|
? this.readSafe
|
|
2170
2278
|
: this.readUnsafe;
|
|
2171
|
-
|
|
2279
|
+
this.reading = new Promise(resolve => {
|
|
2280
|
+
process.nextTick(() => {
|
|
2281
|
+
runReader.call(this).then(resolve, () => resolve());
|
|
2282
|
+
});
|
|
2283
|
+
});
|
|
2172
2284
|
return this;
|
|
2173
2285
|
}
|
|
2174
2286
|
/**
|
package/src/UDPClusterManager.js
CHANGED
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
*/
|
|
24
24
|
import { join } from 'node:path';
|
|
25
25
|
import { Worker } from 'node:worker_threads';
|
|
26
|
-
import { ClusterManager } from './ClusterManager.js';
|
|
26
|
+
import { ClusterManager, } from './ClusterManager.js';
|
|
27
27
|
import {} from './IMessageQueue.js';
|
|
28
28
|
const IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK = !!+(process.env.IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK || 1);
|
|
29
29
|
/** Delay (ms) before an unexpectedly dead worker is re-spawned */
|
|
@@ -312,6 +312,10 @@ export class UDPClusterManager extends ClusterManager {
|
|
|
312
312
|
return;
|
|
313
313
|
}
|
|
314
314
|
const handler = cluster[action];
|
|
315
|
+
if (action === 'remove') {
|
|
316
|
+
handler?.(server, { lapsed: message.lapsed === true });
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
315
319
|
handler?.(server);
|
|
316
320
|
});
|
|
317
321
|
}
|
package/src/UDPWorker.d.ts
CHANGED
|
@@ -96,6 +96,8 @@ export declare class UDPWorker {
|
|
|
96
96
|
*
|
|
97
97
|
* @param message - the announcement that named it, whether a `down` notice
|
|
98
98
|
* or the stale `up` whose liveness timer expired
|
|
99
|
+
* @param lapsed - true where only its announcements stopped arriving, and
|
|
100
|
+
* no `down` notice said it went away
|
|
99
101
|
*/
|
|
100
102
|
private removeServer;
|
|
101
103
|
/**
|
package/src/UDPWorker.js
CHANGED
|
@@ -155,12 +155,15 @@ export class UDPWorker {
|
|
|
155
155
|
*
|
|
156
156
|
* @param message - the announcement that named it, whether a `down` notice
|
|
157
157
|
* or the stale `up` whose liveness timer expired
|
|
158
|
+
* @param lapsed - true where only its announcements stopped arriving, and
|
|
159
|
+
* no `down` notice said it went away
|
|
158
160
|
*/
|
|
159
|
-
removeServer(message) {
|
|
161
|
+
removeServer(message, lapsed = false) {
|
|
160
162
|
this.servers.delete(UDPWorker.getServerKey(message));
|
|
161
163
|
this.messagePort.postMessage({
|
|
162
164
|
type: 'cluster:remove',
|
|
163
165
|
server: UDPWorker.mapMessage(message),
|
|
166
|
+
lapsed,
|
|
164
167
|
});
|
|
165
168
|
}
|
|
166
169
|
/**
|
|
@@ -203,7 +206,7 @@ export class UDPWorker {
|
|
|
203
206
|
this.servers.set(key, stamp);
|
|
204
207
|
const timer = setTimeout(() => setImmediate(() => {
|
|
205
208
|
if (this.servers.get(key) === stamp) {
|
|
206
|
-
this.removeServer(message);
|
|
209
|
+
this.removeServer(message, true);
|
|
207
210
|
}
|
|
208
211
|
}), effectiveTimeout);
|
|
209
212
|
// a pending liveness timer must not keep the worker alive on its own
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a zstd-compressed message starts with. A JSON envelope starts with
|
|
3
|
+
* `{` and a gzipped one with `\x1f`, so one character tells the three apart.
|
|
4
|
+
*/
|
|
5
|
+
export declare const ZSTD_MARK = "Z";
|
|
6
|
+
/** Whether this Node can write and read zstd at all (22.15 and later). */
|
|
7
|
+
export declare const ZSTD_AVAILABLE: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Builds the serializer a queue writes with.
|
|
10
|
+
*
|
|
11
|
+
* @param useGzip - gzip every message, as {@link IMQOptions.useGzip} says
|
|
12
|
+
* @param compressAbove - zstd any message whose JSON is longer than this many
|
|
13
|
+
* characters; `0` never does
|
|
14
|
+
* @returns the serializer
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* zstd at its fastest level shrinks a 4.6 MB description about sixteen times
|
|
18
|
+
* for about 3 ms of CPU, and is skipped below the threshold, where a message
|
|
19
|
+
* is cheaper to send than to compress. The compressed bytes travel as base64,
|
|
20
|
+
* because the queue reads strings and a binary string does not survive the
|
|
21
|
+
* UTF-8 round trip unchanged in size.
|
|
22
|
+
*/
|
|
23
|
+
export declare function packer(useGzip: boolean, compressAbove: number): (data: unknown) => string;
|
|
24
|
+
/**
|
|
25
|
+
* Reads a message in whichever of the three formats it was written: plain
|
|
26
|
+
* JSON, gzip, or zstd.
|
|
27
|
+
*
|
|
28
|
+
* @param data - the message as read off the queue
|
|
29
|
+
* @returns the deserialized value
|
|
30
|
+
* @throws when the message is malformed, or is zstd on a Node that cannot
|
|
31
|
+
* read it
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* A reader taking every format is what lets a fleet turn compression on one
|
|
35
|
+
* writer at a time: every reader is upgraded first, and no writer has to
|
|
36
|
+
* agree with a reader about a setting.
|
|
37
|
+
*/
|
|
38
|
+
export declare function unpackAny(data: string): unknown;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Helper: compress — zstd for large messages, and a reader for every format
|
|
3
|
+
*
|
|
4
|
+
* I'm Queue Software Project
|
|
5
|
+
* Copyright (C) 2025 imqueue.com <support@imqueue.com>
|
|
6
|
+
*
|
|
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.
|
|
16
|
+
*
|
|
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.
|
|
23
|
+
*/
|
|
24
|
+
import zlib from 'node:zlib';
|
|
25
|
+
import { unpack as gunpack } from './unpack.js';
|
|
26
|
+
/**
|
|
27
|
+
* What a zstd-compressed message starts with. A JSON envelope starts with
|
|
28
|
+
* `{` and a gzipped one with `\x1f`, so one character tells the three apart.
|
|
29
|
+
*/
|
|
30
|
+
export const ZSTD_MARK = 'Z';
|
|
31
|
+
const GZIP_MARK = 0x1f;
|
|
32
|
+
/** Whether this Node can write and read zstd at all (22.15 and later). */
|
|
33
|
+
export const ZSTD_AVAILABLE = typeof zlib.zstdCompressSync === 'function' &&
|
|
34
|
+
typeof zlib.zstdDecompressSync === 'function';
|
|
35
|
+
/**
|
|
36
|
+
* Builds the serializer a queue writes with.
|
|
37
|
+
*
|
|
38
|
+
* @param useGzip - gzip every message, as {@link IMQOptions.useGzip} says
|
|
39
|
+
* @param compressAbove - zstd any message whose JSON is longer than this many
|
|
40
|
+
* characters; `0` never does
|
|
41
|
+
* @returns the serializer
|
|
42
|
+
*
|
|
43
|
+
* @remarks
|
|
44
|
+
* zstd at its fastest level shrinks a 4.6 MB description about sixteen times
|
|
45
|
+
* for about 3 ms of CPU, and is skipped below the threshold, where a message
|
|
46
|
+
* is cheaper to send than to compress. The compressed bytes travel as base64,
|
|
47
|
+
* because the queue reads strings and a binary string does not survive the
|
|
48
|
+
* UTF-8 round trip unchanged in size.
|
|
49
|
+
*/
|
|
50
|
+
export function packer(useGzip, compressAbove) {
|
|
51
|
+
if (useGzip) {
|
|
52
|
+
return data => zlib.gzipSync(JSON.stringify(data)).toString('binary');
|
|
53
|
+
}
|
|
54
|
+
if (!(compressAbove > 0) || !ZSTD_AVAILABLE) {
|
|
55
|
+
return data => JSON.stringify(data);
|
|
56
|
+
}
|
|
57
|
+
return data => {
|
|
58
|
+
const json = JSON.stringify(data);
|
|
59
|
+
if (json.length <= compressAbove) {
|
|
60
|
+
return json;
|
|
61
|
+
}
|
|
62
|
+
return (ZSTD_MARK +
|
|
63
|
+
zlib
|
|
64
|
+
.zstdCompressSync(Buffer.from(json), {
|
|
65
|
+
params: { [zlib.constants.ZSTD_c_compressionLevel]: 1 },
|
|
66
|
+
})
|
|
67
|
+
.toString('base64'));
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Reads a message in whichever of the three formats it was written: plain
|
|
72
|
+
* JSON, gzip, or zstd.
|
|
73
|
+
*
|
|
74
|
+
* @param data - the message as read off the queue
|
|
75
|
+
* @returns the deserialized value
|
|
76
|
+
* @throws when the message is malformed, or is zstd on a Node that cannot
|
|
77
|
+
* read it
|
|
78
|
+
*
|
|
79
|
+
* @remarks
|
|
80
|
+
* A reader taking every format is what lets a fleet turn compression on one
|
|
81
|
+
* writer at a time: every reader is upgraded first, and no writer has to
|
|
82
|
+
* agree with a reader about a setting.
|
|
83
|
+
*/
|
|
84
|
+
export function unpackAny(data) {
|
|
85
|
+
if (data.charCodeAt(0) === GZIP_MARK) {
|
|
86
|
+
return gunpack(data);
|
|
87
|
+
}
|
|
88
|
+
if (data.startsWith(ZSTD_MARK)) {
|
|
89
|
+
if (!ZSTD_AVAILABLE) {
|
|
90
|
+
throw new TypeError('IMQ: a zstd message arrived, and this Node cannot read zstd');
|
|
91
|
+
}
|
|
92
|
+
return JSON.parse(zlib
|
|
93
|
+
.zstdDecompressSync(Buffer.from(data.slice(1), 'base64'))
|
|
94
|
+
.toString());
|
|
95
|
+
}
|
|
96
|
+
return JSON.parse(data);
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=compress.js.map
|
package/src/helpers/index.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export * from './sha1.js';
|
|
|
26
26
|
export * from './randomInt.js';
|
|
27
27
|
export * from './pack.js';
|
|
28
28
|
export * from './unpack.js';
|
|
29
|
+
export * from './compress.js';
|
|
29
30
|
export * from './escapeRegExp.js';
|
|
30
31
|
export * from './copyEventEmitter.js';
|
|
31
32
|
export * from './envInt.js';
|
package/src/helpers/index.js
CHANGED
|
@@ -26,6 +26,7 @@ export * from './sha1.js';
|
|
|
26
26
|
export * from './randomInt.js';
|
|
27
27
|
export * from './pack.js';
|
|
28
28
|
export * from './unpack.js';
|
|
29
|
+
export * from './compress.js';
|
|
29
30
|
export * from './escapeRegExp.js';
|
|
30
31
|
export * from './copyEventEmitter.js';
|
|
31
32
|
export * from './envInt.js';
|
package/AGENTS.md
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
# AGENTS.md — orientation for coding agents
|
|
2
|
-
|
|
3
|
-
This file is for AI coding agents (and humans who like density) working on
|
|
4
|
-
`@imqueue/core`. It captures how the codebase is built, tested and structured,
|
|
5
|
-
plus the invariants that are easy to get wrong. Read it before making changes.
|
|
6
|
-
For contribution *process/terms* see [CONTRIBUTING.md](./CONTRIBUTING.md); for
|
|
7
|
-
end-user docs see the [README](./README.md) and https://imqueue.org/.
|
|
8
|
-
|
|
9
|
-
## What this is
|
|
10
|
-
|
|
11
|
-
`@imqueue/core` is the foundation of the @imqueue framework: a fast,
|
|
12
|
-
JSON-message queue for inter-service communication, implemented over **Redis**.
|
|
13
|
-
It is the transport that [`@imqueue/rpc`](https://github.com/imqueue/rpc) builds
|
|
14
|
-
its typed RPC layer on top of. No timers, no polling — delivery is driven by
|
|
15
|
-
Redis blocking list moves and keyspace notifications.
|
|
16
|
-
|
|
17
|
-
## Toolchain & invariants (do not fight these)
|
|
18
|
-
|
|
19
|
-
- **ESM only**, `"type": "module"`. Use `import`, not `require()`. Import
|
|
20
|
-
sibling modules with the **`.js`** extension (NodeNext resolves it to the
|
|
21
|
-
`.ts` source), e.g. `import { RedisQueue } from './RedisQueue.js'`.
|
|
22
|
-
- **TypeScript, `module`/`moduleResolution: nodenext`**, `target: es2024`,
|
|
23
|
-
`verbatimModuleSyntax: true`, `isolatedModules: true`, `strict: true`. Use
|
|
24
|
-
`import type` / `import { type X }` for type-only imports.
|
|
25
|
-
- **Node ≥ 22.12.**
|
|
26
|
-
- **Single runtime dependency: `ioredis`.** Do not add heavyweight deps; this
|
|
27
|
-
package is meant to stay small.
|
|
28
|
-
- **Lint/format:** `oxlint` + `oxfmt`. Run `npm run format` before committing;
|
|
29
|
-
CI checks `npm run format:check`.
|
|
30
|
-
- Build **emits `.js`/`.d.ts`/`.js.map` next to sources**; these are
|
|
31
|
-
**gitignored, not committed** (`build` runs `clean-compiled` first, so stale
|
|
32
|
-
artifacts never linger). Never commit compiled output.
|
|
33
|
-
- `removeComments` is intentionally **`false`** — downstream tooling and
|
|
34
|
-
`@imqueue/rpc` rely on doc-blocks surviving compilation. Keep it that way.
|
|
35
|
-
|
|
36
|
-
## Commands
|
|
37
|
-
|
|
38
|
-
```bash
|
|
39
|
-
npm install
|
|
40
|
-
npm run build # clean-compiled + tsc (emits alongside sources)
|
|
41
|
-
npm test # build + node:test over test/unit/**/*.spec.js (mocked)
|
|
42
|
-
npm run test-integration # build + test/integration/**/*.spec.js (real redis)
|
|
43
|
-
npm run lint # oxlint
|
|
44
|
-
npm run format # oxfmt (write) | npm run format:check (verify)
|
|
45
|
-
npm run test-coverage # tests + experimental coverage summary
|
|
46
|
-
npm run test-lcov # writes coverage/lcov.info
|
|
47
|
-
npm run benchmark # message-throughput benchmark (see below)
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
Unit specs live in `test/unit/` and use the native `node:test` runner with
|
|
51
|
-
`--experimental-test-module-mocks` and a preload of `./test/mocks/index.js`;
|
|
52
|
-
timeout is 15s per test. Run a single spec after a build with
|
|
53
|
-
`node --experimental-test-module-mocks --import ./test/mocks/index.js --test test/unit/RedisQueue.spec.js`.
|
|
54
|
-
|
|
55
|
-
`npm test` deliberately globs `test/unit` rather than `test`: the integration
|
|
56
|
-
specs must not run under a preload that replaces `ioredis` wholesale. Keep that
|
|
57
|
-
glob scoped when adding runners.
|
|
58
|
-
|
|
59
|
-
**The unit specs never open a socket** — `test/mocks/redis.ts` replaces
|
|
60
|
-
`ioredis` wholesale. That is fine for everything except the TLS option, whose
|
|
61
|
-
entire point is what happens during a handshake the mock does not perform.
|
|
62
|
-
`test/integration/` covers that against a real broker, and anything touching
|
|
63
|
-
`tls`, the connection literal in `RedisQueue.connect()`, the pool key or
|
|
64
|
-
connection teardown must be checked with `npm run test-integration` as well as
|
|
65
|
-
`npm test`.
|
|
66
|
-
|
|
67
|
-
`test/integration/tlsBroker.ts` issues a throwaway CA, server and client
|
|
68
|
-
certificate with `openssl`, then starts `redis-server` on a port picked at run
|
|
69
|
-
time with `--port 0 --tls-port <n>`. `--port 0` is the part that matters: with
|
|
70
|
-
no plaintext listener at all, a queue that reaches the broker has demonstrably
|
|
71
|
-
done so over TLS. It is started twice, once with `--tls-auth-clients no` and
|
|
72
|
-
once with `yes`, so both server-authenticated and mutual TLS are exercised.
|
|
73
|
-
|
|
74
|
-
**The integration specs skip, never fail, when the machine cannot host a
|
|
75
|
-
broker.** `startTlsBroker()` returns a reason string instead of throwing when
|
|
76
|
-
`redis-server` or `openssl` is missing, or when redis will not start with TLS
|
|
77
|
-
enabled, and that reason becomes the suite's `skip`. CI has no redis and must
|
|
78
|
-
stay green, so keep that contract: report a skip reason, do not throw, and do
|
|
79
|
-
not add these specs to a runner that CI invokes.
|
|
80
|
-
|
|
81
|
-
## Layout
|
|
82
|
-
|
|
83
|
-
| Path | Role |
|
|
84
|
-
|---|---|
|
|
85
|
-
| `index.ts` | Public entry: default export `IMQ` (factory — `IMQ.create(name, opts)`) plus `export * from './src/index.js'` |
|
|
86
|
-
| `src/IMessageQueue.ts` | The `IMessageQueue` interface + options/types + the `'message'`/`'error'` event contract. The abstraction every adapter implements. |
|
|
87
|
-
| `src/RedisQueue.ts` | Single-Redis-instance queue implementation (the default). |
|
|
88
|
-
| `src/ClusteredRedisQueue.ts` | Round-robin queue across multiple Redis instances for horizontal scaling. |
|
|
89
|
-
| `src/ClusterManager.ts`, `src/UDPClusterManager.ts`, `src/UDPWorker.ts` | Cluster membership / worker coordination (UDP-based discovery). |
|
|
90
|
-
| `src/IMQMode.ts` | `IMQMode` enum (queue role/mode). |
|
|
91
|
-
| `src/redis.ts` | `ioredis` wiring / connection helpers. |
|
|
92
|
-
| `src/profile.ts` | Profiling helper used across the library. |
|
|
93
|
-
| `benchmark/**` | Throughput benchmark (CPU-affinity pinned). |
|
|
94
|
-
| `test/**` | `node:test` specs + `test/mocks/` preload. |
|
|
95
|
-
|
|
96
|
-
## Delivery model (behavioural invariants)
|
|
97
|
-
|
|
98
|
-
- **Two delivery modes**, selected by the `safeDelivery` option: *unreliable*
|
|
99
|
-
(fast; a message is lost if a consumer grabs it and dies) and *safe/guaranteed*
|
|
100
|
-
(1.5–2× slower; a grabbed-then-lost message is rescheduled). Safe delivery
|
|
101
|
-
covers the **processing**, not only the hand-off: the worker key is held until
|
|
102
|
-
the `message` listener is done, and **a listener says so by what it
|
|
103
|
-
returns** — a returned promise keeps the message checked out until it
|
|
104
|
-
settles, anything else releases it as the listener returns. Listeners are
|
|
105
|
-
therefore invoked through `rawListeners()` rather than `emit()`, which
|
|
106
|
-
discards return values. Safe delivery relies on Redis `LMOVE`/`BLMOVE` —
|
|
107
|
-
**Redis 6.2+ is required.** Unreliable delivery uses `BRPOP` alone and works
|
|
108
|
-
on 3.2+, so quote 6.2+ as the requirement unless the sentence is specifically
|
|
109
|
-
about the unreliable mode.
|
|
110
|
-
- **No polling / no timers.** Delivery uses blocking Redis ops + keyspace
|
|
111
|
-
notification events. This is why a lease is reclaimed by asking the broker who
|
|
112
|
-
is still connected (`CLIENT LIST`, already read once per maintenance tick for
|
|
113
|
-
the cleanup pass) rather than by renewing a deadline: a per-message heartbeat
|
|
114
|
-
would be a timer, and would prove only that the event loop is free rather than
|
|
115
|
-
that work is progressing. `safeDeliveryTtl` bounds processing on top of that,
|
|
116
|
-
for the handler that wedges inside a process which is otherwise healthy. If
|
|
117
|
-
Redis has the `CONFIG` command disabled (e.g. AWS ElastiCache), keyspace
|
|
118
|
-
notifications must be enabled out of band:
|
|
119
|
-
`notify-keyspace-events Ex`.
|
|
120
|
-
- **Keyspace-notification flags are merged, never overwritten.**
|
|
121
|
-
`notify-keyspace-events` is server-global, so `watch()` reads the current
|
|
122
|
-
value and appends only the missing `E`/`x` flags (`A` counts as covering
|
|
123
|
-
`x`), skipping `CONFIG SET` when the config already suffices. Do not go back
|
|
124
|
-
to setting a literal — that silently breaks other consumers of the same
|
|
125
|
-
Redis.
|
|
126
|
-
- **Messages must be JSON-serializable** (`IJson`/`JsonObject`). Delayed
|
|
127
|
-
delivery is supported via the send `delay` argument.
|
|
128
|
-
- Scaling the number of workers must **not** increase Redis traffic — preserve
|
|
129
|
-
this property in any queue change. `ClusteredRedisQueue` balances round-robin
|
|
130
|
-
across instances.
|
|
131
|
-
- Idle queues must consume no resources — do not introduce background intervals.
|
|
132
|
-
|
|
133
|
-
## Using this package correctly (for consumer-facing code an agent writes)
|
|
134
|
-
|
|
135
|
-
```typescript
|
|
136
|
-
import IMQ, { IMessageQueue, IJson } from '@imqueue/core';
|
|
137
|
-
|
|
138
|
-
const q: IMessageQueue = IMQ.create('MyQueue'); // default: RedisQueue
|
|
139
|
-
await q.start();
|
|
140
|
-
q.on('message', (msg: IJson, id: string, from: string) => { /* handle */ });
|
|
141
|
-
await q.send('OtherQueue', { hello: 'world' }); // JSON-serializable only
|
|
142
|
-
await q.send('MyQueue', { later: true }, 1000); // delayed 1000ms
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
Prefer `@imqueue/rpc` for typed service-to-service calls; use `core` directly
|
|
146
|
-
only when you need raw queue semantics.
|
|
147
|
-
|
|
148
|
-
## License
|
|
149
|
-
|
|
150
|
-
GPL-3.0. Commercial licensing for closed-source products: https://imqueue.com/.
|
package/CONTRIBUTION-TERMS.md
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
# @imqueue Contribution Terms (v1.0)
|
|
2
|
-
|
|
3
|
-
These terms apply to every contribution to any repository of the **@imqueue**
|
|
4
|
-
project — the packages published under the `@imqueue` npm scope and any repository
|
|
5
|
-
in the imqueue GitHub organisation.
|
|
6
|
-
|
|
7
|
-
**By submitting a contribution — opening a pull request, pushing a commit, sending
|
|
8
|
-
a patch, or otherwise offering work for inclusion — you accept these terms in
|
|
9
|
-
full. If you do not agree with them, do not contribute.**
|
|
10
|
-
|
|
11
|
-
In these terms, **"Owner"** means Mykhailo Stadnyk, together with his successors,
|
|
12
|
-
assigns and any entity he controls (including VaryLogic s.r.o.), and
|
|
13
|
-
**"Contribution"** means any work of authorship — code, documentation or other
|
|
14
|
-
material — you submit to an @imqueue repository.
|
|
15
|
-
|
|
16
|
-
## 1. What you grant
|
|
17
|
-
|
|
18
|
-
You grant the Owner a **perpetual, worldwide, irrevocable, royalty-free,
|
|
19
|
-
non-exclusive, transferable and sublicensable** license to use, reproduce,
|
|
20
|
-
modify, adapt, publish, distribute and otherwise exploit your Contribution and
|
|
21
|
-
derivative works of it, **and to license and sub-license it under any terms the
|
|
22
|
-
Owner chooses — including commercial and proprietary (closed-source) licenses,
|
|
23
|
-
without any obligation to release source code.**
|
|
24
|
-
|
|
25
|
-
In plain terms: the Owner may include your Contribution in @imqueue and **sell
|
|
26
|
-
commercial licenses that cover it**, alongside the open-source edition.
|
|
27
|
-
|
|
28
|
-
## 2. No royalties, fees or compensation
|
|
29
|
-
|
|
30
|
-
The rights above are granted **free of charge**. You **waive** any right to
|
|
31
|
-
royalties, license fees, revenue share or any other compensation arising from the
|
|
32
|
-
Owner's use or licensing of your Contribution, including its commercial licensing,
|
|
33
|
-
and you agree not to seek any such payment now or in the future.
|
|
34
|
-
|
|
35
|
-
## 3. You keep your copyright
|
|
36
|
-
|
|
37
|
-
You **retain ownership** of the copyright in your Contribution and may use it
|
|
38
|
-
elsewhere for your own purposes. You are granting the Owner the rights in Section
|
|
39
|
-
1 — you are not required to transfer ownership. Your Contribution also remains
|
|
40
|
-
available to the public under the repository's open-source license (GPL-3.0).
|
|
41
|
-
|
|
42
|
-
## 4. Patent license
|
|
43
|
-
|
|
44
|
-
You grant the Owner and all downstream recipients a perpetual, worldwide,
|
|
45
|
-
royalty-free, irrevocable (except as stated) patent license to make, use, sell,
|
|
46
|
-
offer to sell, import and transfer your Contribution, limited to the patent claims
|
|
47
|
-
you can license that are necessarily infringed by your Contribution alone or in
|
|
48
|
-
combination with the project. If you start patent litigation alleging the project
|
|
49
|
-
or your Contribution infringes your patents, the patent license you received
|
|
50
|
-
terminates.
|
|
51
|
-
|
|
52
|
-
## 5. Your promises
|
|
53
|
-
|
|
54
|
-
You confirm that: (a) the Contribution is your own original work, or you have the
|
|
55
|
-
right to submit it and to grant these rights; (b) if your employer has any rights
|
|
56
|
-
in your work, you have their permission to contribute, or they have waived those
|
|
57
|
-
rights; and (c) you have identified any third-party material in your Contribution
|
|
58
|
-
and its license.
|
|
59
|
-
|
|
60
|
-
## 6. Moral rights
|
|
61
|
-
|
|
62
|
-
To the maximum extent permitted by applicable law, you waive, or agree not to
|
|
63
|
-
exercise, any moral rights in your Contribution in a way that would prevent the
|
|
64
|
-
Owner from exercising the rights in Section 1. (Certain moral rights are
|
|
65
|
-
inalienable under Slovak/EU law; nothing here purports to transfer those.)
|
|
66
|
-
|
|
67
|
-
## 7. No obligation; "as is"
|
|
68
|
-
|
|
69
|
-
The Owner is under no obligation to use your Contribution. It is provided **"as
|
|
70
|
-
is"**, without warranty of any kind.
|
|
71
|
-
|
|
72
|
-
## 8. Governing law
|
|
73
|
-
|
|
74
|
-
These terms are governed by the laws of the Slovak Republic, without regard to its
|
|
75
|
-
conflict-of-laws rules.
|
|
76
|
-
|
|
77
|
-
---
|
|
78
|
-
|
|
79
|
-
**If you do not agree to all of the above, do not contribute to @imqueue.**
|