@imqueue/core 3.2.5 → 3.3.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/index.d.ts CHANGED
@@ -21,25 +21,86 @@
21
21
  * purchase a proprietary commercial license. Please contact us at
22
22
  * <support@imqueue.com> to get commercial licensing options.
23
23
  */
24
+ /**
25
+ * Redis-backed message queue engine for the `@imqueue` framework — the
26
+ * transport shared by `@imqueue/rpc` and the job packages.
27
+ *
28
+ * Start from `IMQ.create()`, which picks a queue adapter from the options and
29
+ * returns an unstarted `IMessageQueue`. The two concrete adapters are
30
+ * `RedisQueue` (a single Redis server) and `ClusteredRedisQueue` (several
31
+ * servers, with sends distributed between them), and either can also be
32
+ * constructed directly.
33
+ *
34
+ * @remarks
35
+ * Every queue follows the same lifecycle: construct, `start()`, then either
36
+ * consume `message` events or `send()`, and finally `destroy()` to release the
37
+ * connections. `start()` is required before `publish()` or `subscribe()`;
38
+ * `send()` starts the queue implicitly. `stop()` only stops consuming — it
39
+ * keeps the writer, the watcher lock and the maintenance timers alive, so
40
+ * `destroy()` is what actually releases resources.
41
+ *
42
+ * Delivery is at-least-once, so message handlers must be idempotent. Within a
43
+ * single process, writer and watcher connections are shared per `host:port` and
44
+ * reference-counted, and exactly one queue per key prefix is elected as the
45
+ * watcher that releases delayed messages and performs maintenance.
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * import IMQ, { IMQMode, type IMessageQueue } from '@imqueue/core';
50
+ *
51
+ * const queue: IMessageQueue = IMQ.create('my-queue', {
52
+ * host: 'localhost',
53
+ * port: 6379,
54
+ * });
55
+ *
56
+ * queue.on('message', (message, id, from) => {
57
+ * console.log(`got ${id} from ${from}`, message);
58
+ * });
59
+ *
60
+ * await queue.start();
61
+ * await queue.send('my-queue', { hello: 'world' });
62
+ * ```
63
+ *
64
+ * @packageDocumentation
65
+ */
24
66
  import { type IMessageQueue, IMQMode, type IMQOptions } from './src/index.js';
25
67
  export * from './src/index.js';
26
68
  /**
27
- * Message Queue Factory
69
+ * Message queue factory. This is also the default export of `@imqueue/core`.
70
+ *
71
+ * @remarks
72
+ * The factory only selects and instantiates an adapter — it performs no I/O, so
73
+ * the queue it returns is not connected. Call `start()` on it (or `send()`,
74
+ * which starts the queue implicitly) before use.
28
75
  */
29
76
  export default class IMQ {
30
77
  /**
31
- * Base generic queue factory options
32
- *
33
- * @type {Partial<IMQOptions>}
78
+ * Base generic queue factory options.
34
79
  */
35
80
  private static options;
36
81
  /**
37
- * Instantiate proper message queue instance using given user-options
82
+ * Instantiates the queue adapter matching the given options and returns it
83
+ * ready to be started.
84
+ *
85
+ * The adapter is chosen from `options.vendor`, of which only `'Redis'` is
86
+ * supported. When either `options.cluster` or `options.clusterManagers` is
87
+ * present, the clustered adapter is returned instead of the single-server
88
+ * one.
89
+ *
90
+ * @param name - queue name; the underlying Redis key becomes
91
+ * `<prefix>:<name>`
92
+ * @param options - queue options; anything omitted falls back to
93
+ * {@link DEFAULT_IMQ_OPTIONS}
94
+ * @param mode - whether the queue produces, consumes, or both; defaults to
95
+ * {@link IMQMode.BOTH}
96
+ * @returns an unstarted queue instance — a {@link ClusteredRedisQueue} when
97
+ * cluster options were supplied, otherwise a {@link RedisQueue}
98
+ * @throws TypeError when `options.vendor` names an unknown adapter
38
99
  *
39
- * @param {string} name
40
- * @param {IMQOptions} options
41
- * @param {IMQMode} mode
42
- * @return {IMessageQueue}
100
+ * @remarks
101
+ * `options.vendor` is honoured only here. Constructing a queue class
102
+ * directly ignores it, because {@link DEFAULT_IMQ_OPTIONS} carries no
103
+ * `vendor` key.
43
104
  */
44
105
  static create(name: string, options?: Partial<IMQOptions>, mode?: IMQMode): IMessageQueue;
45
106
  }
package/index.js CHANGED
@@ -21,6 +21,48 @@
21
21
  * purchase a proprietary commercial license. Please contact us at
22
22
  * <support@imqueue.com> to get commercial licensing options.
23
23
  */
24
+ /**
25
+ * Redis-backed message queue engine for the `@imqueue` framework — the
26
+ * transport shared by `@imqueue/rpc` and the job packages.
27
+ *
28
+ * Start from `IMQ.create()`, which picks a queue adapter from the options and
29
+ * returns an unstarted `IMessageQueue`. The two concrete adapters are
30
+ * `RedisQueue` (a single Redis server) and `ClusteredRedisQueue` (several
31
+ * servers, with sends distributed between them), and either can also be
32
+ * constructed directly.
33
+ *
34
+ * @remarks
35
+ * Every queue follows the same lifecycle: construct, `start()`, then either
36
+ * consume `message` events or `send()`, and finally `destroy()` to release the
37
+ * connections. `start()` is required before `publish()` or `subscribe()`;
38
+ * `send()` starts the queue implicitly. `stop()` only stops consuming — it
39
+ * keeps the writer, the watcher lock and the maintenance timers alive, so
40
+ * `destroy()` is what actually releases resources.
41
+ *
42
+ * Delivery is at-least-once, so message handlers must be idempotent. Within a
43
+ * single process, writer and watcher connections are shared per `host:port` and
44
+ * reference-counted, and exactly one queue per key prefix is elected as the
45
+ * watcher that releases delayed messages and performs maintenance.
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * import IMQ, { IMQMode, type IMessageQueue } from '@imqueue/core';
50
+ *
51
+ * const queue: IMessageQueue = IMQ.create('my-queue', {
52
+ * host: 'localhost',
53
+ * port: 6379,
54
+ * });
55
+ *
56
+ * queue.on('message', (message, id, from) => {
57
+ * console.log(`got ${id} from ${from}`, message);
58
+ * });
59
+ *
60
+ * await queue.start();
61
+ * await queue.send('my-queue', { hello: 'world' });
62
+ * ```
63
+ *
64
+ * @packageDocumentation
65
+ */
24
66
  import { IMQMode, } from './src/index.js';
25
67
  import { ClusteredRedisQueue } from './src/index.js';
26
68
  import { RedisQueue } from './src/index.js';
@@ -33,24 +75,43 @@ const ADAPTERS = {
33
75
  ClusteredRedisQueue: ClusteredRedisQueue,
34
76
  };
35
77
  /**
36
- * Message Queue Factory
78
+ * Message queue factory. This is also the default export of `@imqueue/core`.
79
+ *
80
+ * @remarks
81
+ * The factory only selects and instantiates an adapter — it performs no I/O, so
82
+ * the queue it returns is not connected. Call `start()` on it (or `send()`,
83
+ * which starts the queue implicitly) before use.
37
84
  */
38
85
  export default class IMQ {
39
86
  /**
40
- * Base generic queue factory options
41
- *
42
- * @type {Partial<IMQOptions>}
87
+ * Base generic queue factory options.
43
88
  */
44
89
  static options = {
45
90
  vendor: 'Redis', // default vendor
46
91
  };
47
92
  /**
48
- * Instantiate proper message queue instance using given user-options
93
+ * Instantiates the queue adapter matching the given options and returns it
94
+ * ready to be started.
95
+ *
96
+ * The adapter is chosen from `options.vendor`, of which only `'Redis'` is
97
+ * supported. When either `options.cluster` or `options.clusterManagers` is
98
+ * present, the clustered adapter is returned instead of the single-server
99
+ * one.
100
+ *
101
+ * @param name - queue name; the underlying Redis key becomes
102
+ * `<prefix>:<name>`
103
+ * @param options - queue options; anything omitted falls back to
104
+ * {@link DEFAULT_IMQ_OPTIONS}
105
+ * @param mode - whether the queue produces, consumes, or both; defaults to
106
+ * {@link IMQMode.BOTH}
107
+ * @returns an unstarted queue instance — a {@link ClusteredRedisQueue} when
108
+ * cluster options were supplied, otherwise a {@link RedisQueue}
109
+ * @throws TypeError when `options.vendor` names an unknown adapter
49
110
  *
50
- * @param {string} name
51
- * @param {IMQOptions} options
52
- * @param {IMQMode} mode
53
- * @return {IMessageQueue}
111
+ * @remarks
112
+ * `options.vendor` is honoured only here. Constructing a queue class
113
+ * directly ignores it, because {@link DEFAULT_IMQ_OPTIONS} carries no
114
+ * `vendor` key.
54
115
  */
55
116
  static create(name, options = {}, mode = IMQMode.BOTH) {
56
117
  options = Object.assign({}, IMQ.options, options);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imqueue/core",
3
- "version": "3.2.5",
3
+ "version": "3.3.0",
4
4
  "description": "Fast JSON message queue over Redis for inter-service communication in Node.js & TypeScript microservices — the transport layer of the @imqueue framework",
5
5
  "keywords": [
6
6
  "message-queue",
@@ -68,10 +68,6 @@
68
68
  "types": "./index.d.ts",
69
69
  "default": "./index.js"
70
70
  },
71
- "./debug": {
72
- "types": "./debug.d.ts",
73
- "default": "./debug.js"
74
- },
75
71
  "./package.json": "./package.json"
76
72
  }
77
73
  }
@@ -1,15 +1,71 @@
1
1
  import { type IMessageQueueConnection, type IServerInput } from './IMessageQueue.js';
2
+ /**
3
+ * Membership callbacks a clustered queue hands to a {@link ClusterManager} so
4
+ * the manager can add and remove servers as it discovers them.
5
+ *
6
+ * Implement this to feed a clustered queue from your own discovery mechanism;
7
+ * {@link ClusteredRedisQueue} supplies an implementation of its own.
8
+ */
2
9
  export interface ICluster {
10
+ /**
11
+ * Adds a server to the cluster and returns its registration record.
12
+ * Registration is idempotent — an already-known server is returned
13
+ * unchanged.
14
+ */
3
15
  add: (server: IServerInput) => IMessageQueueConnection;
16
+ /**
17
+ * Removes a server from the cluster. A no-op when the server is unknown.
18
+ */
4
19
  remove: (server: IServerInput) => void;
20
+ /**
21
+ * Looks a server up, matching by `id` when both sides carry one and by host
22
+ * and port otherwise. Returns `undefined` when it is not registered.
23
+ */
5
24
  find: (server: IServerInput) => IMessageQueueConnection | undefined;
6
25
  }
26
+ /**
27
+ * A cluster that has been registered with a {@link ClusterManager}, carrying
28
+ * the generated id that identifies it for {@link ClusterManager.remove}.
29
+ */
7
30
  export interface InitializedCluster extends ICluster {
31
+ /**
32
+ * Identifier generated when the cluster was registered.
33
+ */
8
34
  id: string;
9
35
  }
36
+ /**
37
+ * Abstract base for cluster-membership discovery. A manager tracks the clusters
38
+ * it feeds and pushes server add/remove events into each of them, so several
39
+ * clustered queues can share one discovery mechanism.
40
+ *
41
+ * Supply instances through {@link IMQOptions.clusterManagers}.
42
+ * {@link UDPClusterManager} is the implementation shipped with the framework.
43
+ *
44
+ * @remarks
45
+ * Subclasses must implement {@link ClusterManager.destroy} to release their
46
+ * transport, and that implementation must be idempotent — `remove()` can invoke
47
+ * it more than once.
48
+ */
10
49
  export declare abstract class ClusterManager {
50
+ /**
51
+ * Clusters currently registered with this manager.
52
+ */
11
53
  protected clusters: InitializedCluster[];
12
54
  protected constructor();
55
+ /**
56
+ * Registers a cluster's membership callbacks with this manager and returns
57
+ * an identified handle.
58
+ *
59
+ * @param cluster - the membership callbacks to feed
60
+ * @returns the registered cluster, carrying the id that
61
+ * {@link ClusterManager.remove} accepts
62
+ *
63
+ * @remarks
64
+ * Retain the returned handle (or its `id`) — it is the only thing `remove()`
65
+ * accepts. Registering does not start discovery: an implementation may
66
+ * already be listening from construction, in which case events that arrive
67
+ * before this call are discarded.
68
+ */
13
69
  init(cluster: ICluster): InitializedCluster;
14
70
  /**
15
71
  * Applies the given callback to every registered cluster. Each cluster
@@ -17,10 +73,33 @@ export declare abstract class ClusterManager {
17
73
  * asynchronously) for one cluster never prevents the remaining clusters
18
74
  * from being processed.
19
75
  *
20
- * @param {(cluster: InitializedCluster) => Promise<void> | void} fn
21
- * @returns {Promise<void>}
76
+ * @param fn - callback to apply to each registered cluster
77
+ *
78
+ * @remarks
79
+ * Callback failures are discarded: the returned promise always resolves and
80
+ * errors are neither rethrown nor logged, so callbacks must handle and
81
+ * report their own failures.
22
82
  */
23
83
  forEachCluster(fn: (cluster: InitializedCluster) => Promise<void> | void): Promise<void>;
84
+ /**
85
+ * Unregisters a cluster from this manager.
86
+ *
87
+ * @param cluster - the cluster handle returned by {@link ClusterManager.init},
88
+ * or its `id`
89
+ * @param destroy - when true (the default), destroy the manager once no
90
+ * clusters remain; pass false to unregister without tearing it down
91
+ *
92
+ * @remarks
93
+ * Unregistering the last cluster destroys the manager and releases its
94
+ * transport — for {@link UDPClusterManager} that means shutting down the
95
+ * shared broadcast worker. Calling this with an unknown id while no clusters
96
+ * remain also triggers that shutdown, which is why `destroy()`
97
+ * implementations must be idempotent.
98
+ */
24
99
  remove(cluster: string | InitializedCluster, destroy?: boolean): Promise<void>;
100
+ /**
101
+ * Releases the manager's transport. Implemented by subclasses, and must be
102
+ * safe to call more than once.
103
+ */
25
104
  abstract destroy(): Promise<void>;
26
105
  }
@@ -23,9 +23,39 @@
23
23
  */
24
24
  import { randomUUID } from 'node:crypto';
25
25
  import {} from './IMessageQueue.js';
26
+ /**
27
+ * Abstract base for cluster-membership discovery. A manager tracks the clusters
28
+ * it feeds and pushes server add/remove events into each of them, so several
29
+ * clustered queues can share one discovery mechanism.
30
+ *
31
+ * Supply instances through {@link IMQOptions.clusterManagers}.
32
+ * {@link UDPClusterManager} is the implementation shipped with the framework.
33
+ *
34
+ * @remarks
35
+ * Subclasses must implement {@link ClusterManager.destroy} to release their
36
+ * transport, and that implementation must be idempotent — `remove()` can invoke
37
+ * it more than once.
38
+ */
26
39
  export class ClusterManager {
40
+ /**
41
+ * Clusters currently registered with this manager.
42
+ */
27
43
  clusters = [];
28
44
  constructor() { }
45
+ /**
46
+ * Registers a cluster's membership callbacks with this manager and returns
47
+ * an identified handle.
48
+ *
49
+ * @param cluster - the membership callbacks to feed
50
+ * @returns the registered cluster, carrying the id that
51
+ * {@link ClusterManager.remove} accepts
52
+ *
53
+ * @remarks
54
+ * Retain the returned handle (or its `id`) — it is the only thing `remove()`
55
+ * accepts. Registering does not start discovery: an implementation may
56
+ * already be listening from construction, in which case events that arrive
57
+ * before this call are discarded.
58
+ */
29
59
  init(cluster) {
30
60
  const initializedCluster = {
31
61
  ...cluster,
@@ -40,12 +70,31 @@ export class ClusterManager {
40
70
  * asynchronously) for one cluster never prevents the remaining clusters
41
71
  * from being processed.
42
72
  *
43
- * @param {(cluster: InitializedCluster) => Promise<void> | void} fn
44
- * @returns {Promise<void>}
73
+ * @param fn - callback to apply to each registered cluster
74
+ *
75
+ * @remarks
76
+ * Callback failures are discarded: the returned promise always resolves and
77
+ * errors are neither rethrown nor logged, so callbacks must handle and
78
+ * report their own failures.
45
79
  */
46
80
  async forEachCluster(fn) {
47
81
  await Promise.allSettled(this.clusters.map(async (cluster) => fn(cluster)));
48
82
  }
83
+ /**
84
+ * Unregisters a cluster from this manager.
85
+ *
86
+ * @param cluster - the cluster handle returned by {@link ClusterManager.init},
87
+ * or its `id`
88
+ * @param destroy - when true (the default), destroy the manager once no
89
+ * clusters remain; pass false to unregister without tearing it down
90
+ *
91
+ * @remarks
92
+ * Unregistering the last cluster destroys the manager and releases its
93
+ * transport — for {@link UDPClusterManager} that means shutting down the
94
+ * shared broadcast worker. Calling this with an unknown id while no clusters
95
+ * remain also triggers that shutdown, which is why `destroy()`
96
+ * implementations must be idempotent.
97
+ */
49
98
  async remove(cluster, destroy = true) {
50
99
  const id = typeof cluster === 'string' ? cluster : cluster.id;
51
100
  this.clusters = this.clusters.filter(cluster => cluster.id !== id);