ioredis-toolkit 0.0.4 → 0.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/README.md +772 -316
- package/dist/cache.d.ts +502 -6
- package/dist/cache.js +501 -5
- package/dist/client.d.ts +107 -2
- package/dist/client.js +107 -3
- package/dist/health.d.ts +141 -0
- package/dist/health.js +133 -0
- package/dist/lock.d.ts +55 -1
- package/dist/lock.js +12 -22
- package/dist/pubsub.d.ts +259 -7
- package/dist/pubsub.js +259 -7
- package/dist/ratelimiter.d.ts +279 -1
- package/dist/ratelimiter.js +250 -0
- package/dist/session/session-manager.d.ts +14 -0
- package/dist/types.js +0 -2
- package/package.json +1 -1
package/dist/lock.d.ts
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
import { RedisClientWrapper } from './client.js';
|
|
2
2
|
import { LoggerLike } from './logger.js';
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Information about a distributed lock.
|
|
5
|
+
*
|
|
6
|
+
* **Fields:**
|
|
7
|
+
* - `locked`: Whether the lock is currently held.
|
|
8
|
+
* - `ttl`: Remaining TTL in seconds (when held and TTL set).
|
|
9
|
+
* - `lockId`: Unique owner id of the lock.
|
|
10
|
+
*
|
|
11
|
+
* **Example:**
|
|
12
|
+
* ```ts
|
|
13
|
+
* const info = await lock.getLockInfo('order:42');
|
|
14
|
+
* // { locked: true, ttl: 29, lockId: 'a1b2c3...' }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export type LockInfo = {
|
|
18
|
+
/** Whether the lock is currently held. */
|
|
19
|
+
locked: boolean;
|
|
20
|
+
/** Remaining TTL in seconds (when held and TTL set). */
|
|
21
|
+
ttl?: number;
|
|
22
|
+
/** Unique owner id of the lock. */
|
|
23
|
+
lockId?: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Options for the distributed lock.
|
|
27
|
+
*
|
|
28
|
+
* **Fields:**
|
|
29
|
+
* - `ttl`: Lock TTL in milliseconds. Default: `30000`.
|
|
30
|
+
* - `retryCount`: Number of acquisition attempts. Default: `3`.
|
|
31
|
+
* - `retryDelay`: Base delay between retries in ms (grows exponentially). Default: `200`.
|
|
32
|
+
*
|
|
33
|
+
* **Example:**
|
|
34
|
+
* ```ts
|
|
35
|
+
* const lock = new DistributedLock(client, { ttl: 10000, retryCount: 5 });
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export interface DistributedLockOptions {
|
|
39
|
+
/** Lock TTL in milliseconds. Default: `30000`. */
|
|
40
|
+
ttl?: number;
|
|
41
|
+
/** Number of acquisition attempts. Default: `3`. */
|
|
42
|
+
retryCount?: number;
|
|
43
|
+
/** Base delay between retries in ms (grows exponentially). Default: `200`. */
|
|
44
|
+
retryDelay?: number;
|
|
45
|
+
}
|
|
4
46
|
/**
|
|
5
47
|
* Distributed mutual-exclusion lock backed by Redis.
|
|
6
48
|
*
|
|
@@ -53,11 +95,13 @@ export declare class DistributedLock {
|
|
|
53
95
|
*
|
|
54
96
|
* @param key - The resource to lock, e.g. `'order:42'` (stored as `lock:order:42`).
|
|
55
97
|
* @param ttl - Lock TTL in milliseconds (default: `30000`).
|
|
98
|
+
*
|
|
56
99
|
* @returns `true` when the lock was acquired.
|
|
57
100
|
*
|
|
58
101
|
* @example
|
|
59
102
|
* ```ts
|
|
60
103
|
* const acquired = await lock.acquire('order:42', 10000);
|
|
104
|
+
* // acquired === true when lock was successfully acquired
|
|
61
105
|
* ```
|
|
62
106
|
*/
|
|
63
107
|
acquire(key: string, ttl?: number): Promise<boolean>;
|
|
@@ -68,6 +112,7 @@ export declare class DistributedLock {
|
|
|
68
112
|
* re-acquired by someone else) is never removed by the old owner.
|
|
69
113
|
*
|
|
70
114
|
* @param key - The locked resource.
|
|
115
|
+
*
|
|
71
116
|
* @returns `true` if the lock was released, `false` if not owned or missing.
|
|
72
117
|
*
|
|
73
118
|
* @example
|
|
@@ -83,6 +128,7 @@ export declare class DistributedLock {
|
|
|
83
128
|
* be gone. This is what `withLock` falls back to when a normal release fails.
|
|
84
129
|
*
|
|
85
130
|
* @param key - The locked resource.
|
|
131
|
+
*
|
|
86
132
|
* @returns `true` if a lock existed and was deleted.
|
|
87
133
|
*
|
|
88
134
|
* @example
|
|
@@ -99,11 +145,13 @@ export declare class DistributedLock {
|
|
|
99
145
|
*
|
|
100
146
|
* @param key - The locked resource.
|
|
101
147
|
* @param ttl - New TTL in milliseconds (default: `30000`).
|
|
148
|
+
*
|
|
102
149
|
* @returns `true` if the lock was extended.
|
|
103
150
|
*
|
|
104
151
|
* @example
|
|
105
152
|
* ```ts
|
|
106
153
|
* const extended = await lock.extend('order:42', 30000);
|
|
154
|
+
* // extended === true when lock TTL was renewed
|
|
107
155
|
* ```
|
|
108
156
|
*/
|
|
109
157
|
extend(key: string, ttl?: number): Promise<boolean>;
|
|
@@ -117,7 +165,9 @@ export declare class DistributedLock {
|
|
|
117
165
|
* @param key - The resource to lock.
|
|
118
166
|
* @param fn - The critical section to run exclusively.
|
|
119
167
|
* @param options - Per-call `ttl` (ms), `retryCount`, `retryDelay`.
|
|
168
|
+
*
|
|
120
169
|
* @returns The return value of `fn`.
|
|
170
|
+
*
|
|
121
171
|
* @throws {@link RedisError} with code `LOCK_ACQUISITION_FAILED` when the lock
|
|
122
172
|
* cannot be acquired, or `LOCK_LOST` when the lock expired mid-execution.
|
|
123
173
|
*
|
|
@@ -133,6 +183,7 @@ export declare class DistributedLock {
|
|
|
133
183
|
* Checks whether a lock is currently held.
|
|
134
184
|
*
|
|
135
185
|
* @param key - The locked resource.
|
|
186
|
+
*
|
|
136
187
|
* @returns `true` if the lock exists (held by anyone).
|
|
137
188
|
*
|
|
138
189
|
* @example
|
|
@@ -145,6 +196,7 @@ export declare class DistributedLock {
|
|
|
145
196
|
* Returns details about a lock.
|
|
146
197
|
*
|
|
147
198
|
* @param key - The locked resource.
|
|
199
|
+
*
|
|
148
200
|
* @returns `{ locked: false }` when not held, otherwise `{ locked: true, ttl, lockId }`.
|
|
149
201
|
*
|
|
150
202
|
* @example
|
|
@@ -158,6 +210,7 @@ export declare class DistributedLock {
|
|
|
158
210
|
* Returns the owner id of a lock.
|
|
159
211
|
*
|
|
160
212
|
* @param key - The locked resource.
|
|
213
|
+
*
|
|
161
214
|
* @returns The lock id (random hex token), or `null` when not held.
|
|
162
215
|
*
|
|
163
216
|
* @example
|
|
@@ -170,6 +223,7 @@ export declare class DistributedLock {
|
|
|
170
223
|
* Returns the remaining TTL of a lock in seconds.
|
|
171
224
|
*
|
|
172
225
|
* @param key - The locked resource.
|
|
226
|
+
*
|
|
173
227
|
* @returns Remaining seconds (`0` when not held or expired).
|
|
174
228
|
*
|
|
175
229
|
* @example
|
package/dist/lock.js
CHANGED
|
@@ -1,28 +1,6 @@
|
|
|
1
1
|
import { RedisError } from './errors.js';
|
|
2
2
|
import { randomBytes } from 'node:crypto';
|
|
3
3
|
import { defaultLogger } from './logger.js';
|
|
4
|
-
// /**
|
|
5
|
-
// * Information about a distributed lock.
|
|
6
|
-
// */
|
|
7
|
-
// export interface LockInfo {
|
|
8
|
-
// /** Whether the lock is currently held. */
|
|
9
|
-
// locked: boolean;
|
|
10
|
-
// /** Remaining TTL in seconds (when held and TTL set). */
|
|
11
|
-
// ttl?: number;
|
|
12
|
-
// /** Unique owner id of the lock. */
|
|
13
|
-
// lockId?: string;
|
|
14
|
-
// }
|
|
15
|
-
// /**
|
|
16
|
-
// * Options for the distributed lock.
|
|
17
|
-
// */
|
|
18
|
-
// export interface DistributedLockOptions {
|
|
19
|
-
// /** Lock TTL in milliseconds. Default: `30000`. */
|
|
20
|
-
// ttl?: number;
|
|
21
|
-
// /** Number of acquisition attempts. Default: `3`. */
|
|
22
|
-
// retryCount?: number;
|
|
23
|
-
// /** Base delay between retries in ms (grows exponentially). Default: `200`. */
|
|
24
|
-
// retryDelay?: number;
|
|
25
|
-
// }
|
|
26
4
|
/**
|
|
27
5
|
* Distributed mutual-exclusion lock backed by Redis.
|
|
28
6
|
*
|
|
@@ -100,11 +78,13 @@ export class DistributedLock {
|
|
|
100
78
|
*
|
|
101
79
|
* @param key - The resource to lock, e.g. `'order:42'` (stored as `lock:order:42`).
|
|
102
80
|
* @param ttl - Lock TTL in milliseconds (default: `30000`).
|
|
81
|
+
*
|
|
103
82
|
* @returns `true` when the lock was acquired.
|
|
104
83
|
*
|
|
105
84
|
* @example
|
|
106
85
|
* ```ts
|
|
107
86
|
* const acquired = await lock.acquire('order:42', 10000);
|
|
87
|
+
* // acquired === true when lock was successfully acquired
|
|
108
88
|
* ```
|
|
109
89
|
*/
|
|
110
90
|
async acquire(key, ttl = this.defaultTTL) {
|
|
@@ -123,6 +103,7 @@ export class DistributedLock {
|
|
|
123
103
|
* re-acquired by someone else) is never removed by the old owner.
|
|
124
104
|
*
|
|
125
105
|
* @param key - The locked resource.
|
|
106
|
+
*
|
|
126
107
|
* @returns `true` if the lock was released, `false` if not owned or missing.
|
|
127
108
|
*
|
|
128
109
|
* @example
|
|
@@ -161,6 +142,7 @@ export class DistributedLock {
|
|
|
161
142
|
* be gone. This is what `withLock` falls back to when a normal release fails.
|
|
162
143
|
*
|
|
163
144
|
* @param key - The locked resource.
|
|
145
|
+
*
|
|
164
146
|
* @returns `true` if a lock existed and was deleted.
|
|
165
147
|
*
|
|
166
148
|
* @example
|
|
@@ -181,11 +163,13 @@ export class DistributedLock {
|
|
|
181
163
|
*
|
|
182
164
|
* @param key - The locked resource.
|
|
183
165
|
* @param ttl - New TTL in milliseconds (default: `30000`).
|
|
166
|
+
*
|
|
184
167
|
* @returns `true` if the lock was extended.
|
|
185
168
|
*
|
|
186
169
|
* @example
|
|
187
170
|
* ```ts
|
|
188
171
|
* const extended = await lock.extend('order:42', 30000);
|
|
172
|
+
* // extended === true when lock TTL was renewed
|
|
189
173
|
* ```
|
|
190
174
|
*/
|
|
191
175
|
async extend(key, ttl = this.defaultTTL) {
|
|
@@ -220,7 +204,9 @@ export class DistributedLock {
|
|
|
220
204
|
* @param key - The resource to lock.
|
|
221
205
|
* @param fn - The critical section to run exclusively.
|
|
222
206
|
* @param options - Per-call `ttl` (ms), `retryCount`, `retryDelay`.
|
|
207
|
+
*
|
|
223
208
|
* @returns The return value of `fn`.
|
|
209
|
+
*
|
|
224
210
|
* @throws {@link RedisError} with code `LOCK_ACQUISITION_FAILED` when the lock
|
|
225
211
|
* cannot be acquired, or `LOCK_LOST` when the lock expired mid-execution.
|
|
226
212
|
*
|
|
@@ -308,6 +294,7 @@ export class DistributedLock {
|
|
|
308
294
|
* Checks whether a lock is currently held.
|
|
309
295
|
*
|
|
310
296
|
* @param key - The locked resource.
|
|
297
|
+
*
|
|
311
298
|
* @returns `true` if the lock exists (held by anyone).
|
|
312
299
|
*
|
|
313
300
|
* @example
|
|
@@ -324,6 +311,7 @@ export class DistributedLock {
|
|
|
324
311
|
* Returns details about a lock.
|
|
325
312
|
*
|
|
326
313
|
* @param key - The locked resource.
|
|
314
|
+
*
|
|
327
315
|
* @returns `{ locked: false }` when not held, otherwise `{ locked: true, ttl, lockId }`.
|
|
328
316
|
*
|
|
329
317
|
* @example
|
|
@@ -356,6 +344,7 @@ export class DistributedLock {
|
|
|
356
344
|
* Returns the owner id of a lock.
|
|
357
345
|
*
|
|
358
346
|
* @param key - The locked resource.
|
|
347
|
+
*
|
|
359
348
|
* @returns The lock id (random hex token), or `null` when not held.
|
|
360
349
|
*
|
|
361
350
|
* @example
|
|
@@ -371,6 +360,7 @@ export class DistributedLock {
|
|
|
371
360
|
* Returns the remaining TTL of a lock in seconds.
|
|
372
361
|
*
|
|
373
362
|
* @param key - The locked resource.
|
|
363
|
+
*
|
|
374
364
|
* @returns Remaining seconds (`0` when not held or expired).
|
|
375
365
|
*
|
|
376
366
|
* @example
|
package/dist/pubsub.d.ts
CHANGED
|
@@ -5,21 +5,50 @@ import { LoggerLike } from './logger.js';
|
|
|
5
5
|
/**
|
|
6
6
|
* Redis Pub/Sub with a dedicated publisher and subscriber connection.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
* **Description:**
|
|
9
|
+
* Provides a higher-level interface over native Redis Pub/Sub with the following features:
|
|
10
|
+
* - Dedicated publisher connection (configured at construction)
|
|
11
|
+
* - Dedicated subscriber connection (opened via {@link connectSubscriber})
|
|
12
|
+
* - JSON serialization on publish, auto-parsing on delivery
|
|
13
|
+
* - Pattern subscription support (`psubscribe`, `punsubscribe`)
|
|
14
|
+
* - Extends Node.js `EventEmitter` for event-based handling
|
|
15
|
+
* - Emits `'error'` events on subscriber failures
|
|
10
16
|
*
|
|
11
|
-
*
|
|
17
|
+
* **Type Parameters:**
|
|
18
|
+
* - `T` - The type of message payload. When publishing non-string values, they are
|
|
19
|
+
* JSON-serialized. When subscribing, messages are auto-parsed from JSON when possible.
|
|
12
20
|
*
|
|
13
|
-
*
|
|
21
|
+
* **Mode Support:**
|
|
22
|
+
* - Standalone, Sentinel, and Cluster modes are all supported.
|
|
23
|
+
* - The subscriber connection is created per the configured mode.
|
|
24
|
+
*
|
|
25
|
+
* **Example:**
|
|
14
26
|
* ```ts
|
|
15
27
|
* const pubsub = new PubSub(client);
|
|
16
|
-
* await pubsub.connectSubscriber(
|
|
28
|
+
* await pubsub.connectSubscriber({ mode: 'standalone', host: 'localhost', port: 6379 });
|
|
17
29
|
*
|
|
30
|
+
* // Subscribe to a channel
|
|
18
31
|
* await pubsub.subscribe('orders:created', (message) => {
|
|
19
|
-
* console.log(message); // { id: 1 }
|
|
32
|
+
* console.log(message); // { id: 1 } - JSON-parsed
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* // Publish a message
|
|
36
|
+
* const receivers = await pubsub.publish('orders:created', { id: 1 });
|
|
37
|
+
* // receivers === number of subscribers that received the message
|
|
38
|
+
*
|
|
39
|
+
* // Pattern subscription
|
|
40
|
+
* await pubsub.psubscribe('orders:*', ({ channel, message }) => {
|
|
41
|
+
* console.log(channel, message);
|
|
20
42
|
* });
|
|
21
|
-
* await pubsub.publish('orders:created', { id: 1 });
|
|
22
43
|
* ```
|
|
44
|
+
*
|
|
45
|
+
* **Event Emissions:**
|
|
46
|
+
* - `message`: Emitted when a message is received on a subscribed channel.
|
|
47
|
+
* Payload: `{ channel: string, message: string }`
|
|
48
|
+
* - `pmessage`: Emitted when a pattern message is received.
|
|
49
|
+
* Payload: `{ pattern: string, channel: string, message: string }`
|
|
50
|
+
* - `error`: Emitted on subscriber errors.
|
|
51
|
+
* Payload: `Error`
|
|
23
52
|
*/
|
|
24
53
|
export declare class PubSub extends EventEmitter {
|
|
25
54
|
private publisher;
|
|
@@ -27,6 +56,18 @@ export declare class PubSub extends EventEmitter {
|
|
|
27
56
|
private logger;
|
|
28
57
|
private subscriptions;
|
|
29
58
|
private patternSubscriptions;
|
|
59
|
+
/**
|
|
60
|
+
* Creates a pub/sub instance. Publishing works immediately; subscribing
|
|
61
|
+
* requires calling {@link connectSubscriber} first.
|
|
62
|
+
*
|
|
63
|
+
* @param publisher - A {@link RedisClientWrapper} used for publishing.
|
|
64
|
+
* @param logger - Optional pino-compatible logger; defaults to `console`.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* const pubsub = new PubSub(client);
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
30
71
|
/**
|
|
31
72
|
* Creates a pub/sub instance. Publishing works immediately; subscribing
|
|
32
73
|
* requires calling {@link connectSubscriber} first.
|
|
@@ -53,9 +94,73 @@ export declare class PubSub extends EventEmitter {
|
|
|
53
94
|
* await pubsub.connectSubscriber({ mode: 'standalone', host: 'localhost', port: 6379 });
|
|
54
95
|
* ```
|
|
55
96
|
*/
|
|
97
|
+
/**
|
|
98
|
+
* Opens a dedicated subscriber connection.
|
|
99
|
+
*
|
|
100
|
+
* **Behavior:**
|
|
101
|
+
* - Idempotent: a second call is a no-op while a subscriber is connected.
|
|
102
|
+
* - Subscriber errors are emitted as `'error'` events on the instance.
|
|
103
|
+
*
|
|
104
|
+
* **Parameters:**
|
|
105
|
+
* - `config` - Redis config for the subscriber connection (any mode: standalone, sentinel, or cluster).
|
|
106
|
+
*
|
|
107
|
+
* **Example:**
|
|
108
|
+
* ```ts
|
|
109
|
+
* // Connect with standalone configuration
|
|
110
|
+
* await pubsub.connectSubscriber({ mode: 'standalone', host: 'localhost', port: 6379 });
|
|
111
|
+
*
|
|
112
|
+
* // Connect with cluster configuration
|
|
113
|
+
* await pubsub.connectSubscriber({
|
|
114
|
+
* mode: 'cluster',
|
|
115
|
+
* clusterNodes: [{ host: 'redis1', port: 7000 }, { host: 'redis2', port: 7001 }],
|
|
116
|
+
* });
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* @returns `Promise<void>` that resolves when the subscriber connection is established.
|
|
120
|
+
*/
|
|
56
121
|
connectSubscriber(config: RedisConfig): Promise<void>;
|
|
122
|
+
/**
|
|
123
|
+
* Sets up event listeners on the subscriber connection.
|
|
124
|
+
*
|
|
125
|
+
* **Behavior:**
|
|
126
|
+
* - Listens for `message` events and dispatches to {@link handleMessage}.
|
|
127
|
+
* - Listens for `pmessage` events (pattern subscriptions) and dispatches to {@link handlePatternMessage}.
|
|
128
|
+
* - Listens for `error` events and emits them on the instance.
|
|
129
|
+
*
|
|
130
|
+
* @internal
|
|
131
|
+
*/
|
|
57
132
|
private setupSubscriber;
|
|
133
|
+
/**
|
|
134
|
+
* Handles a standard message event from the subscriber.
|
|
135
|
+
*
|
|
136
|
+
* **Behavior:**
|
|
137
|
+
* - Parses the message payload as JSON when possible.
|
|
138
|
+
* - Invokes all registered handlers for the channel.
|
|
139
|
+
* - Catches and logs handler errors without crashing.
|
|
140
|
+
*
|
|
141
|
+
* **Parameters:**
|
|
142
|
+
* - `channel` - The Redis channel name.
|
|
143
|
+
* - `message` - The raw message string from Redis (JSON-encoded).
|
|
144
|
+
*
|
|
145
|
+
* @internal
|
|
146
|
+
*/
|
|
58
147
|
private handleMessage;
|
|
148
|
+
/**
|
|
149
|
+
* Handles a pattern message event from the subscriber.
|
|
150
|
+
*
|
|
151
|
+
* **Behavior:**
|
|
152
|
+
* - Parses the message payload as JSON when possible.
|
|
153
|
+
* - Invokes all registered handlers for the pattern.
|
|
154
|
+
* - Each handler receives an object with `channel` and `message` properties.
|
|
155
|
+
* - Catches and logs handler errors without crashing.
|
|
156
|
+
*
|
|
157
|
+
* **Parameters:**
|
|
158
|
+
* - `pattern` - The pattern that matched.
|
|
159
|
+
* - `channel` - The specific channel that was matched.
|
|
160
|
+
* - `message` - The raw message string from Redis (JSON-encoded).
|
|
161
|
+
*
|
|
162
|
+
* @internal
|
|
163
|
+
*/
|
|
59
164
|
private handlePatternMessage;
|
|
60
165
|
/**
|
|
61
166
|
* Publishes a message to a channel.
|
|
@@ -71,6 +176,32 @@ export declare class PubSub extends EventEmitter {
|
|
|
71
176
|
* const receivers = await pubsub.publish('orders:created', { id: 1, total: 99 });
|
|
72
177
|
* ```
|
|
73
178
|
*/
|
|
179
|
+
/**
|
|
180
|
+
* Publishes a message to a channel.
|
|
181
|
+
*
|
|
182
|
+
* **Behavior:**
|
|
183
|
+
* - Non-string values are JSON-serialized automatically.
|
|
184
|
+
* - The raw message is published to the Redis channel.
|
|
185
|
+
* - Returns the number of subscribers that received the message.
|
|
186
|
+
*
|
|
187
|
+
* **Type Parameters:**
|
|
188
|
+
* - `T` - The type of the message payload. Non-string values are JSON-serialized.
|
|
189
|
+
*
|
|
190
|
+
* **Returns:**
|
|
191
|
+
* - The number of subscribers that received the message.
|
|
192
|
+
*
|
|
193
|
+
* **Example:**
|
|
194
|
+
* ```ts
|
|
195
|
+
* const receivers = await pubsub.publish('orders:created', { id: 1, total: 99 });
|
|
196
|
+
* // receivers === number of subscribed clients
|
|
197
|
+
* ```
|
|
198
|
+
*
|
|
199
|
+
* **Parameters:**
|
|
200
|
+
* - `channel` - The channel name.
|
|
201
|
+
* - `message` - The message payload (string or any JSON-serializable value).
|
|
202
|
+
*
|
|
203
|
+
* @returns The number of subscribers that received the message.
|
|
204
|
+
*/
|
|
74
205
|
publish<T = any>(channel: string, message: T): Promise<number>;
|
|
75
206
|
/**
|
|
76
207
|
* Subscribes a handler to a channel.
|
|
@@ -89,6 +220,34 @@ export declare class PubSub extends EventEmitter {
|
|
|
89
220
|
* });
|
|
90
221
|
* ```
|
|
91
222
|
*/
|
|
223
|
+
/**
|
|
224
|
+
* Subscribes a handler to a channel.
|
|
225
|
+
*
|
|
226
|
+
* **Behavior:**
|
|
227
|
+
* - Multiple handlers per channel are supported; the channel is subscribed on
|
|
228
|
+
* Redis only once.
|
|
229
|
+
* - Delivered payloads are JSON-parsed when possible.
|
|
230
|
+
* - Throws an error if the subscriber connection is not open.
|
|
231
|
+
*
|
|
232
|
+
* **Type Parameters:**
|
|
233
|
+
* - `T` - The type of the message data received in the handler.
|
|
234
|
+
*
|
|
235
|
+
* **Returns:**
|
|
236
|
+
* - `Promise<void>` that resolves when the subscription is established.
|
|
237
|
+
*
|
|
238
|
+
* **Example:**
|
|
239
|
+
* ```ts
|
|
240
|
+
* await pubsub.subscribe('orders:created', (order) => {
|
|
241
|
+
* console.log(order.id);
|
|
242
|
+
* });
|
|
243
|
+
* ```
|
|
244
|
+
*
|
|
245
|
+
* **Parameters:**
|
|
246
|
+
* - `channel` - The channel name.
|
|
247
|
+
* - `handler` - Callback receiving the (parsed) message.
|
|
248
|
+
*
|
|
249
|
+
* @throws `Error` if the subscriber connection is not open.
|
|
250
|
+
*/
|
|
92
251
|
subscribe<T = any>(channel: string, handler: (data: T) => void): Promise<void>;
|
|
93
252
|
/**
|
|
94
253
|
* Removes a handler (or all handlers) from a channel.
|
|
@@ -106,6 +265,28 @@ export declare class PubSub extends EventEmitter {
|
|
|
106
265
|
* await pubsub.unsubscribe('orders:created'); // remove everything
|
|
107
266
|
* ```
|
|
108
267
|
*/
|
|
268
|
+
/**
|
|
269
|
+
* Removes a handler (or all handlers) from a channel.
|
|
270
|
+
*
|
|
271
|
+
* **Behavior:**
|
|
272
|
+
* - The Redis subscription is dropped once the last handler for the channel is
|
|
273
|
+
* removed.
|
|
274
|
+
* - Without a handler, the whole channel is unsubscribed.
|
|
275
|
+
*
|
|
276
|
+
* **Returns:**
|
|
277
|
+
* - `Promise<void>` that resolves when the unsubscription is complete.
|
|
278
|
+
*
|
|
279
|
+
* **Example:**
|
|
280
|
+
* ```ts
|
|
281
|
+
* await pubsub.unsubscribe('orders:created', myHandler);
|
|
282
|
+
* await pubsub.unsubscribe('orders:created'); // remove everything
|
|
283
|
+
* ```
|
|
284
|
+
*
|
|
285
|
+
* **Parameters:**
|
|
286
|
+
* - `channel` - The channel name.
|
|
287
|
+
* - `handler` - Optional specific handler to remove; when omitted all
|
|
288
|
+
* handlers for the channel are removed.
|
|
289
|
+
*/
|
|
109
290
|
unsubscribe<T = any>(channel: string, handler?: (data: T) => void): Promise<void>;
|
|
110
291
|
/**
|
|
111
292
|
* Subscribes a handler to all channels matching a glob pattern.
|
|
@@ -123,6 +304,32 @@ export declare class PubSub extends EventEmitter {
|
|
|
123
304
|
* });
|
|
124
305
|
* ```
|
|
125
306
|
*/
|
|
307
|
+
/**
|
|
308
|
+
* Subscribes a handler to all channels matching a glob pattern.
|
|
309
|
+
*
|
|
310
|
+
* **Behavior:**
|
|
311
|
+
* - Pattern handlers receive `{ channel, message }` (message JSON-parsed).
|
|
312
|
+
* - The Redis subscription is set up once per pattern.
|
|
313
|
+
*
|
|
314
|
+
* **Type Parameters:**
|
|
315
|
+
* - `T` - The type of the message data received in the handler.
|
|
316
|
+
*
|
|
317
|
+
* **Returns:**
|
|
318
|
+
* - `Promise<void>` that resolves when the pattern subscription is established.
|
|
319
|
+
*
|
|
320
|
+
* **Example:**
|
|
321
|
+
* ```ts
|
|
322
|
+
* await pubsub.psubscribe('orders:*', ({ channel, message }) => {
|
|
323
|
+
* console.log(channel, message);
|
|
324
|
+
* });
|
|
325
|
+
* ```
|
|
326
|
+
*
|
|
327
|
+
* **Parameters:**
|
|
328
|
+
* - `pattern` - Glob pattern, e.g. `'orders:*'`.
|
|
329
|
+
* - `handler` - Callback receiving `{ channel, message }`.
|
|
330
|
+
*
|
|
331
|
+
* @throws `Error` if the subscriber connection is not open.
|
|
332
|
+
*/
|
|
126
333
|
psubscribe<T = any>(pattern: string, handler: (data: {
|
|
127
334
|
channel: string;
|
|
128
335
|
message: T;
|
|
@@ -140,6 +347,23 @@ export declare class PubSub extends EventEmitter {
|
|
|
140
347
|
* await pubsub.punsubscribe('orders:*');
|
|
141
348
|
* ```
|
|
142
349
|
*/
|
|
350
|
+
/**
|
|
351
|
+
* Removes a handler (or all handlers) from a pattern subscription.
|
|
352
|
+
*
|
|
353
|
+
* **Returns:**
|
|
354
|
+
* - `Promise<void>` that resolves when the punsubscription is complete.
|
|
355
|
+
*
|
|
356
|
+
* **Example:**
|
|
357
|
+
* ```ts
|
|
358
|
+
* await pubsub.punsubscribe('orders:*', myHandler);
|
|
359
|
+
* await pubsub.punsubscribe('orders:*');
|
|
360
|
+
* ```
|
|
361
|
+
*
|
|
362
|
+
* **Parameters:**
|
|
363
|
+
* - `pattern` - The glob pattern.
|
|
364
|
+
* - `handler` - Optional specific handler to remove; when omitted all
|
|
365
|
+
* handlers for the pattern are removed.
|
|
366
|
+
*/
|
|
143
367
|
punsubscribe(pattern: string, handler?: (data: any) => void): Promise<void>;
|
|
144
368
|
/**
|
|
145
369
|
* Closes the subscriber connection and clears all subscriptions.
|
|
@@ -151,6 +375,20 @@ export declare class PubSub extends EventEmitter {
|
|
|
151
375
|
* await pubsub.close();
|
|
152
376
|
* ```
|
|
153
377
|
*/
|
|
378
|
+
/**
|
|
379
|
+
* Closes the subscriber connection and clears all subscriptions.
|
|
380
|
+
*
|
|
381
|
+
* **Behavior:**
|
|
382
|
+
* - The publisher client is not closed (it is owned by the caller).
|
|
383
|
+
* - All subscriptions are cleared from memory.
|
|
384
|
+
*
|
|
385
|
+
* **Example:**
|
|
386
|
+
* ```ts
|
|
387
|
+
* await pubsub.close();
|
|
388
|
+
* ```
|
|
389
|
+
*
|
|
390
|
+
* @returns `Promise<void>` that resolves when the subscriber is closed.
|
|
391
|
+
*/
|
|
154
392
|
close(): Promise<void>;
|
|
155
393
|
/**
|
|
156
394
|
* Returns subscription statistics.
|
|
@@ -163,6 +401,20 @@ export declare class PubSub extends EventEmitter {
|
|
|
163
401
|
* // { subscriptions: 2, patternSubscriptions: 1, connected: true }
|
|
164
402
|
* ```
|
|
165
403
|
*/
|
|
404
|
+
/**
|
|
405
|
+
* Returns subscription statistics.
|
|
406
|
+
*
|
|
407
|
+
* **Returns:**
|
|
408
|
+
* - A {@link PubSubStats} object with the current subscription state.
|
|
409
|
+
*
|
|
410
|
+
* **Example:**
|
|
411
|
+
* ```ts
|
|
412
|
+
* const stats = pubsub.getStats();
|
|
413
|
+
* // { subscriptions: 2, patternSubscriptions: 1, connected: true }
|
|
414
|
+
* ```
|
|
415
|
+
*
|
|
416
|
+
* @returns `{ subscriptions, patternSubscriptions, connected }`.
|
|
417
|
+
*/
|
|
166
418
|
getStats(): {
|
|
167
419
|
subscriptions: number;
|
|
168
420
|
patternSubscriptions: number;
|