@imqueue/rpc 3.4.4 → 3.5.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 (53) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/index.d.ts +58 -0
  3. package/index.js +58 -0
  4. package/package.json +1 -1
  5. package/src/IMQCache.d.ts +20 -11
  6. package/src/IMQCache.js +20 -11
  7. package/src/IMQClient.d.ts +191 -38
  8. package/src/IMQClient.js +204 -57
  9. package/src/IMQDelay.d.ts +29 -6
  10. package/src/IMQDelay.js +20 -6
  11. package/src/IMQLock.d.ts +81 -35
  12. package/src/IMQLock.js +38 -35
  13. package/src/IMQMetadata.d.ts +9 -1
  14. package/src/IMQMetadata.js +1 -1
  15. package/src/IMQRPCDescription.d.ts +183 -9
  16. package/src/IMQRPCDescription.js +22 -0
  17. package/src/IMQRPCError.d.ts +91 -9
  18. package/src/IMQRPCError.js +23 -7
  19. package/src/IMQRPCOptions.d.ts +221 -18
  20. package/src/IMQRPCOptions.js +22 -6
  21. package/src/IMQRPCRequest.d.ts +39 -1
  22. package/src/IMQRPCResponse.d.ts +34 -0
  23. package/src/IMQRequestContext.d.ts +2 -5
  24. package/src/IMQRequestContext.js +2 -5
  25. package/src/IMQService.d.ts +144 -19
  26. package/src/IMQService.js +151 -33
  27. package/src/cache/ICache.d.ts +58 -16
  28. package/src/cache/RedisCache.d.ts +58 -17
  29. package/src/cache/RedisCache.js +43 -17
  30. package/src/decorators/cache.d.ts +46 -2
  31. package/src/decorators/cache.js +2 -2
  32. package/src/decorators/classType.d.ts +28 -13
  33. package/src/decorators/classType.js +28 -13
  34. package/src/decorators/expose.d.ts +59 -7
  35. package/src/decorators/expose.js +77 -30
  36. package/src/decorators/indexed.d.ts +27 -8
  37. package/src/decorators/indexed.js +27 -8
  38. package/src/decorators/lock.d.ts +20 -2
  39. package/src/decorators/lock.js +2 -2
  40. package/src/decorators/logged.d.ts +29 -2
  41. package/src/decorators/logged.js +2 -2
  42. package/src/decorators/property.d.ts +97 -4
  43. package/src/decorators/property.js +84 -6
  44. package/src/decorators/remote.d.ts +1 -1
  45. package/src/decorators/remote.js +1 -1
  46. package/src/helpers/fs.d.ts +5 -7
  47. package/src/helpers/fs.js +8 -10
  48. package/src/helpers/os-uuid.d.ts +0 -2
  49. package/src/helpers/os-uuid.js +2 -10
  50. package/src/helpers/pid.d.ts +7 -7
  51. package/src/helpers/pid.js +10 -11
  52. package/src/helpers/signature.d.ts +4 -4
  53. package/src/helpers/signature.js +7 -7
@@ -24,11 +24,41 @@
24
24
  import { type JsonObject, type ILogger, type IMessageQueue } from '@imqueue/core';
25
25
  import { type TypesDescription, type IMQRPCRequest, type IMQRPCResponse, type IMQServiceOptions, type ICache, type MethodsCollectionDescription } from './index.js';
26
26
  import { type Server } from 'node:http';
27
+ /**
28
+ * The self-description a service serves to its clients, and the input to client
29
+ * generation.
30
+ *
31
+ * @remarks
32
+ * Declared as a class for historical reasons but never instantiated: a service
33
+ * returns a plain object literal, and a client receives its JSON round-trip. So do
34
+ * not use `new Description()` or `instanceof Description`.
35
+ *
36
+ * A service memoizes its description after the first request for it.
37
+ */
27
38
  export declare class Description {
39
+ /**
40
+ * The service itself: its name, and every exposed method with the inheritance
41
+ * chain already flattened.
42
+ */
28
43
  service: {
44
+ /**
45
+ * The service name, which is also its queue name.
46
+ */
29
47
  name: string;
48
+ /**
49
+ * Every exposed method, keyed by name, with parent-class methods merged in
50
+ * and overridden by same-named subclass methods.
51
+ */
30
52
  methods: MethodsCollectionDescription;
31
53
  };
54
+ /**
55
+ * Every complex type registered in the service's process.
56
+ *
57
+ * @remarks
58
+ * This is the process-global type registry, embedded by reference and
59
+ * unfiltered — so it includes types this service does not use, and each becomes
60
+ * a generated interface in every client.
61
+ */
32
62
  types: TypesDescription;
33
63
  }
34
64
  /**
@@ -36,27 +66,81 @@ export declare class Description {
36
66
  * Basic abstract service (server-side) implementation
37
67
  */
38
68
  export declare abstract class IMQService {
69
+ /**
70
+ * Allows methods to be dispatched dynamically by name.
71
+ *
72
+ * @remarks
73
+ * A consequence worth knowing: arbitrary property access on a service instance
74
+ * type-checks as `any`, so typos in subclass code are not caught by the
75
+ * compiler.
76
+ */
39
77
  [property: string]: any;
78
+ /**
79
+ * The underlying message queue this service consumes requests from.
80
+ */
40
81
  protected imq: IMessageQueue;
82
+ /**
83
+ * The effective logger — `options.logger`, defaulting to `console`.
84
+ */
41
85
  protected logger: ILogger;
86
+ /**
87
+ * The cache adapter used by cached methods.
88
+ *
89
+ * @remarks
90
+ * Never assigned by the service itself: it is populated lazily by the
91
+ * {@link cache} decorator on first use, so it is `undefined` on a service that
92
+ * has no cached methods.
93
+ */
42
94
  protected cache: ICache;
95
+ /**
96
+ * The metrics HTTP server, present only while
97
+ * {@link IMQMetricsServerOptions.enabled} is set.
98
+ *
99
+ * @remarks
100
+ * {@link IMQService.destroy} does not close it — do that yourself, or the
101
+ * open listener keeps the process alive.
102
+ */
43
103
  protected metricsServer?: Server<any, any>;
44
104
  private readonly signalHandlers;
105
+ /**
106
+ * This service's name, which is also its queue name and the key its description
107
+ * is cached under.
108
+ */
45
109
  name: string;
110
+ /**
111
+ * The effective options for this service: {@link DEFAULT_IMQ_SERVICE_OPTIONS}
112
+ * merged with the values passed to the constructor.
113
+ */
46
114
  options: IMQServiceOptions;
47
115
  /**
48
- * Class constructor
116
+ * Constructs a service.
117
+ *
118
+ * @param options - service options, shallow-merged over
119
+ * {@link DEFAULT_IMQ_SERVICE_OPTIONS}, except `metricsServer` which is
120
+ * merged separately over
121
+ * {@link DEFAULT_IMQ_METRICS_SERVER_OPTIONS}
122
+ * @param name - overrides the class name as the service/queue name, and as the
123
+ * description cache key
124
+ * @throws TypeError when {@link IMQService} is instantiated directly. Note the
125
+ * check is by resolved name, so passing `'IMQService'` as the `name` of a
126
+ * subclass throws as well.
49
127
  *
50
- * @param {Partial<IMQServiceOptions>} options
51
- * @param {string} [name]
128
+ * @remarks
129
+ * Constructing a service installs process signal handlers for `SIGTERM`,
130
+ * `SIGINT`, `SIGHUP` and `SIGQUIT`. On any of them the service tears the queue
131
+ * down, closes the metrics server, and then force-exits with code 0 after
132
+ * `IMQ_SHUTDOWN_TIMEOUT` — a fixed timer, not a drain: requests still being
133
+ * processed are not awaited.
134
+ *
135
+ * The merged options are passed straight to the queue factory, so `vendor` and
136
+ * `cluster`/`clusterManagers` select the transport implementation.
52
137
  */
53
138
  constructor(options?: Partial<IMQServiceOptions>, name?: string);
54
139
  /**
55
140
  * Handles incoming request and produces corresponding response
56
141
  *
57
- * @param {IMQRPCRequest} request - request message
58
- * @param {string} id - message unique identifier
59
- * @return {Promise<string>}
142
+ * @param request - request message
143
+ * @param id - message unique identifier
60
144
  */
61
145
  private handleRequest;
62
146
  private processRequest;
@@ -64,31 +148,72 @@ export declare abstract class IMQService {
64
148
  * Initializes this instance of service and starts handling request
65
149
  * messages.
66
150
  *
67
- * @return {Promise<IMessageQueue | undefined>}
151
+ * @returns the started message queue instance
152
+ *
153
+ * @remarks
154
+ * In single-process mode this starts the queue in the current process. With
155
+ * {@link IMQServiceOptions.multiProcess} the cluster primary forks
156
+ * `cpus().length * childrenPerCore` workers, each receiving its index in the
157
+ * `workerId` environment variable, and installs an exit watcher that terminates
158
+ * the process with code 1 as soon as one worker dies — workers are never
159
+ * respawned, so supervision is left to the process manager.
160
+ *
161
+ * Note that the primary also starts its own queue consumer and metrics listener
162
+ * after forking, so a service configured for N workers runs N+1 consumers and
163
+ * N+1 processes attempt to bind the metrics port.
68
164
  */
69
165
  start(): Promise<IMessageQueue | undefined>;
70
166
  /**
71
- * Sends given data to service subscription channel
167
+ * Broadcasts data on this service's pub/sub channel, delivering it to every
168
+ * client currently subscribed to this service.
169
+ *
170
+ * @param data - JSON-serializable payload to broadcast
171
+ * @throws TypeError when the service has not been started, so the queue's writer
172
+ * is not connected
72
173
  *
73
- * @param {JsonObject} data
174
+ * @remarks
175
+ * Fire-and-forget: plain pub/sub with no persistence, ordering guarantee or
176
+ * delivery confirmation, so subscribers that are offline lose the message.
74
177
  */
75
178
  publish(data: JsonObject): Promise<void>;
76
179
  /**
77
- * Stops service from handling messages
180
+ * Stops consuming messages from the queue.
78
181
  *
79
- * @return {Promise<void>}
182
+ * @remarks
183
+ * Connections, signal handlers and the metrics server are all left in place —
184
+ * use {@link IMQService.destroy} for full teardown. Calling this first is not a
185
+ * prerequisite for `destroy()`.
80
186
  */
81
187
  stop(): Promise<void>;
82
188
  /**
83
- * Destroys this instance of service
189
+ * Removes this instance's signal handlers, then unsubscribes and destroys the
190
+ * underlying queue.
84
191
  *
85
- * @return {Promise<void>}
192
+ * @remarks
193
+ * It does not close the metrics server — if
194
+ * {@link IMQMetricsServerOptions.enabled} was set, close
195
+ * `service.metricsServer` yourself, otherwise the open listener keeps the
196
+ * process alive. In-flight requests are not awaited.
86
197
  */
87
198
  destroy(): Promise<void>;
88
199
  /**
89
- * Returns service description metadata.
200
+ * Returns this service's description metadata — the exposed method list plus the
201
+ * registered complex types — synchronously.
202
+ *
203
+ * @returns a plain object matching the {@link Description} shape, not a class
204
+ * instance
205
+ *
206
+ * @remarks
207
+ * This method is itself exposed, so a remote caller invoking it through a
208
+ * generated client receives a `Promise<Description>` — but called locally it is
209
+ * not a promise and must not be awaited as one.
90
210
  *
91
- * @returns {Promise<Description>}
211
+ * The result is built once per service name and cached process-wide for the
212
+ * lifetime of the process, so methods attached after the first call are not
213
+ * reflected. The cache key is the service `name` while the method list is
214
+ * resolved from the concrete class name, so constructing two differently-classed
215
+ * services under the same `name` in one process makes them share a single
216
+ * description.
92
217
  */
93
218
  describe(): Description;
94
219
  private startWithMetricsServer;
@@ -96,9 +221,9 @@ export declare abstract class IMQService {
96
221
  /**
97
222
  * Sends IMQ response with support of after call optional hook
98
223
  *
99
- * @param {IMQRPCRequest} request - from message identifier
100
- * @param {IMQRPCResponse} response - response to send
101
- * @param {IMQService} service - imq service to bind
102
- * @return {Promise<string>} - send result message identifier
224
+ * @param request - from message identifier
225
+ * @param response - response to send
226
+ * @param service - imq service to bind
227
+ * @returns send result message identifier
103
228
  */
104
229
  export declare function send(request: IMQRPCRequest, response: IMQRPCResponse, service: IMQService): Promise<string>;
package/src/IMQService.js CHANGED
@@ -64,8 +64,31 @@ import {} from './IMQRPCDescription.js';
64
64
  import {} from './IMQRPCOptions.js';
65
65
  import { runWithRequest } from './IMQRequestContext.js';
66
66
  import { createServer } from 'node:http';
67
+ /**
68
+ * The self-description a service serves to its clients, and the input to client
69
+ * generation.
70
+ *
71
+ * @remarks
72
+ * Declared as a class for historical reasons but never instantiated: a service
73
+ * returns a plain object literal, and a client receives its JSON round-trip. So do
74
+ * not use `new Description()` or `instanceof Description`.
75
+ *
76
+ * A service memoizes its description after the first request for it.
77
+ */
67
78
  export class Description {
79
+ /**
80
+ * The service itself: its name, and every exposed method with the inheritance
81
+ * chain already flattened.
82
+ */
68
83
  service;
84
+ /**
85
+ * Every complex type registered in the service's process.
86
+ *
87
+ * @remarks
88
+ * This is the process-global type registry, embedded by reference and
89
+ * unfiltered — so it includes types this service does not use, and each becomes
90
+ * a generated interface in every client.
91
+ */
69
92
  types;
70
93
  }
71
94
  const serviceDescriptions = new Map();
@@ -73,25 +96,33 @@ const serviceDescriptions = new Map();
73
96
  * Returns collection of class methods metadata even those are inherited
74
97
  * from a chain of parent classes
75
98
  *
76
- * @param {string} className
77
- * @return {MethodsCollectionDescription}
99
+ * @param ctor - constructor of the class to collect exposed methods for
78
100
  */
79
- function getClassMethods(className) {
80
- let methods = {};
81
- let classInfo = IMQRPCDescription.serviceDescription[className];
82
- if (classInfo.inherits &&
83
- IMQRPCDescription.serviceDescription[classInfo.inherits]) {
84
- Object.assign(methods, getClassMethods(classInfo.inherits));
101
+ function getClassMethods(ctor) {
102
+ const methods = {};
103
+ // Resolve the chain from the runtime prototype chain rather than from the
104
+ // registry's `inherits` links. @expose() only registers the class that
105
+ // *declares* a method, so a class exposing nothing of its own has no
106
+ // registry entry at all — walking `inherits` would either dereference
107
+ // undefined or stop dead at that gap and lose the ancestors' methods.
108
+ const chain = [];
109
+ for (let current = ctor; typeof current === 'function'; current = Object.getPrototypeOf(current)) {
110
+ chain.unshift(current);
111
+ }
112
+ // root-first, so a subclass method overrides a same-named parent one
113
+ for (const link of chain) {
114
+ const info = IMQRPCDescription.serviceDescription[link.name];
115
+ if (info) {
116
+ Object.assign(methods, info.methods);
117
+ }
85
118
  }
86
- Object.assign(methods, classInfo.methods);
87
119
  return methods;
88
120
  }
89
121
  /**
90
122
  * Checks if given args match the given args description at least by args count
91
123
  *
92
- * @param {ArgDescription[]} argsInfo
93
- * @param {any[]} args
94
- * @returns {boolean}
124
+ * @param argsInfo -
125
+ * @param args -
95
126
  */
96
127
  function isValidArgsCount(argsInfo, args) {
97
128
  return argsInfo.some(argInfo => argInfo.isOptional)
@@ -121,18 +152,65 @@ let IMQService = (() => {
121
152
  __esDecorate(this, null, _describe_decorators, { kind: "method", name: "describe", static: false, private: false, access: { has: obj => "describe" in obj, get: obj => obj.describe }, metadata: _metadata }, null, _instanceExtraInitializers);
122
153
  if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
123
154
  }
155
+ /**
156
+ * The underlying message queue this service consumes requests from.
157
+ */
124
158
  imq = __runInitializers(this, _instanceExtraInitializers);
159
+ /**
160
+ * The effective logger — `options.logger`, defaulting to `console`.
161
+ */
125
162
  logger;
163
+ /**
164
+ * The cache adapter used by cached methods.
165
+ *
166
+ * @remarks
167
+ * Never assigned by the service itself: it is populated lazily by the
168
+ * {@link cache} decorator on first use, so it is `undefined` on a service that
169
+ * has no cached methods.
170
+ */
126
171
  cache;
172
+ /**
173
+ * The metrics HTTP server, present only while
174
+ * {@link IMQMetricsServerOptions.enabled} is set.
175
+ *
176
+ * @remarks
177
+ * {@link IMQService.destroy} does not close it — do that yourself, or the
178
+ * open listener keeps the process alive.
179
+ */
127
180
  metricsServer;
128
181
  signalHandlers = [];
182
+ /**
183
+ * This service's name, which is also its queue name and the key its description
184
+ * is cached under.
185
+ */
129
186
  name;
187
+ /**
188
+ * The effective options for this service: {@link DEFAULT_IMQ_SERVICE_OPTIONS}
189
+ * merged with the values passed to the constructor.
190
+ */
130
191
  options;
131
192
  /**
132
- * Class constructor
193
+ * Constructs a service.
194
+ *
195
+ * @param options - service options, shallow-merged over
196
+ * {@link DEFAULT_IMQ_SERVICE_OPTIONS}, except `metricsServer` which is
197
+ * merged separately over
198
+ * {@link DEFAULT_IMQ_METRICS_SERVER_OPTIONS}
199
+ * @param name - overrides the class name as the service/queue name, and as the
200
+ * description cache key
201
+ * @throws TypeError when {@link IMQService} is instantiated directly. Note the
202
+ * check is by resolved name, so passing `'IMQService'` as the `name` of a
203
+ * subclass throws as well.
133
204
  *
134
- * @param {Partial<IMQServiceOptions>} options
135
- * @param {string} [name]
205
+ * @remarks
206
+ * Constructing a service installs process signal handlers for `SIGTERM`,
207
+ * `SIGINT`, `SIGHUP` and `SIGQUIT`. On any of them the service tears the queue
208
+ * down, closes the metrics server, and then force-exits with code 0 after
209
+ * `IMQ_SHUTDOWN_TIMEOUT` — a fixed timer, not a drain: requests still being
210
+ * processed are not awaited.
211
+ *
212
+ * The merged options are passed straight to the queue factory, so `vendor` and
213
+ * `cluster`/`clusterManagers` select the transport implementation.
136
214
  */
137
215
  constructor(options, name) {
138
216
  this.name = name || this.constructor.name;
@@ -171,9 +249,8 @@ let IMQService = (() => {
171
249
  /**
172
250
  * Handles incoming request and produces corresponding response
173
251
  *
174
- * @param {IMQRPCRequest} request - request message
175
- * @param {string} id - message unique identifier
176
- * @return {Promise<string>}
252
+ * @param request - request message
253
+ * @param id - message unique identifier
177
254
  */
178
255
  async handleRequest(request, id) {
179
256
  return runWithRequest(request, () => this.processRequest(request, id));
@@ -246,7 +323,19 @@ let IMQService = (() => {
246
323
  * Initializes this instance of service and starts handling request
247
324
  * messages.
248
325
  *
249
- * @return {Promise<IMessageQueue | undefined>}
326
+ * @returns the started message queue instance
327
+ *
328
+ * @remarks
329
+ * In single-process mode this starts the queue in the current process. With
330
+ * {@link IMQServiceOptions.multiProcess} the cluster primary forks
331
+ * `cpus().length * childrenPerCore` workers, each receiving its index in the
332
+ * `workerId` environment variable, and installs an exit watcher that terminates
333
+ * the process with code 1 as soon as one worker dies — workers are never
334
+ * respawned, so supervision is left to the process manager.
335
+ *
336
+ * Note that the primary also starts its own queue consumer and metrics listener
337
+ * after forking, so a service configured for N workers runs N+1 consumers and
338
+ * N+1 processes attempt to bind the metrics port.
250
339
  */
251
340
  async start() {
252
341
  if (!this.options.multiProcess) {
@@ -278,25 +367,40 @@ let IMQService = (() => {
278
367
  return this.startWithMetricsServer();
279
368
  }
280
369
  /**
281
- * Sends given data to service subscription channel
370
+ * Broadcasts data on this service's pub/sub channel, delivering it to every
371
+ * client currently subscribed to this service.
282
372
  *
283
- * @param {JsonObject} data
373
+ * @param data - JSON-serializable payload to broadcast
374
+ * @throws TypeError when the service has not been started, so the queue's writer
375
+ * is not connected
376
+ *
377
+ * @remarks
378
+ * Fire-and-forget: plain pub/sub with no persistence, ordering guarantee or
379
+ * delivery confirmation, so subscribers that are offline lose the message.
284
380
  */
285
381
  async publish(data) {
286
382
  await this.imq.publish(data);
287
383
  }
288
384
  /**
289
- * Stops service from handling messages
385
+ * Stops consuming messages from the queue.
290
386
  *
291
- * @return {Promise<void>}
387
+ * @remarks
388
+ * Connections, signal handlers and the metrics server are all left in place —
389
+ * use {@link IMQService.destroy} for full teardown. Calling this first is not a
390
+ * prerequisite for `destroy()`.
292
391
  */
293
392
  async stop() {
294
393
  await this.imq.stop();
295
394
  }
296
395
  /**
297
- * Destroys this instance of service
396
+ * Removes this instance's signal handlers, then unsubscribes and destroys the
397
+ * underlying queue.
298
398
  *
299
- * @return {Promise<void>}
399
+ * @remarks
400
+ * It does not close the metrics server — if
401
+ * {@link IMQMetricsServerOptions.enabled} was set, close
402
+ * `service.metricsServer` yourself, otherwise the open listener keeps the
403
+ * process alive. In-flight requests are not awaited.
300
404
  */
301
405
  async destroy() {
302
406
  // unregister this instance's process signal handlers so destroyed
@@ -309,9 +413,23 @@ let IMQService = (() => {
309
413
  await this.imq.destroy();
310
414
  }
311
415
  /**
312
- * Returns service description metadata.
416
+ * Returns this service's description metadata — the exposed method list plus the
417
+ * registered complex types — synchronously.
418
+ *
419
+ * @returns a plain object matching the {@link Description} shape, not a class
420
+ * instance
421
+ *
422
+ * @remarks
423
+ * This method is itself exposed, so a remote caller invoking it through a
424
+ * generated client receives a `Promise<Description>` — but called locally it is
425
+ * not a promise and must not be awaited as one.
313
426
  *
314
- * @returns {Promise<Description>}
427
+ * The result is built once per service name and cached process-wide for the
428
+ * lifetime of the process, so methods attached after the first call are not
429
+ * reflected. The cache key is the service `name` while the method list is
430
+ * resolved from the concrete class name, so constructing two differently-classed
431
+ * services under the same `name` in one process makes them share a single
432
+ * description.
315
433
  */
316
434
  describe() {
317
435
  let description = serviceDescriptions.get(this.name) || null;
@@ -319,7 +437,7 @@ let IMQService = (() => {
319
437
  description = {
320
438
  service: {
321
439
  name: this.name,
322
- methods: getClassMethods(this.constructor.name),
440
+ methods: getClassMethods(this.constructor),
323
441
  },
324
442
  types: IMQRPCDescription.typesDescription,
325
443
  };
@@ -338,7 +456,7 @@ let IMQService = (() => {
338
456
  if (req.url === '/metrics') {
339
457
  const length = await this.imq.queueLength();
340
458
  const content = metricServerOptions.queueLengthFormatter?.(length, 'queue_length') || String(length);
341
- res.setHeader('Content-Type', 'plain/text');
459
+ res.setHeader('Content-Type', 'text/plain');
342
460
  res.setHeader('Content-Length', Buffer.byteLength(content));
343
461
  res.writeHead(200);
344
462
  res.end(content);
@@ -358,10 +476,10 @@ export { IMQService };
358
476
  /**
359
477
  * Sends IMQ response with support of after call optional hook
360
478
  *
361
- * @param {IMQRPCRequest} request - from message identifier
362
- * @param {IMQRPCResponse} response - response to send
363
- * @param {IMQService} service - imq service to bind
364
- * @return {Promise<string>} - send result message identifier
479
+ * @param request - from message identifier
480
+ * @param response - response to send
481
+ * @param service - imq service to bind
482
+ * @returns send result message identifier
365
483
  */
366
484
  export async function send(request, response, service) {
367
485
  const logger = service.options.logger || console;
@@ -22,9 +22,18 @@
22
22
  * <support@imqueue.com> to get commercial licensing options.
23
23
  */
24
24
  /**
25
- * Constructor signature for cache adapter implementations.
25
+ * Constructor signature the registry uses to instantiate a cache adapter class.
26
+ *
27
+ * @remarks
28
+ * The registry always calls it with no arguments. The optional `name` argument
29
+ * is a legacy affordance that no built-in adapter accepts and that the registry
30
+ * never supplies, so implementations must be constructible with zero arguments and
31
+ * set their own {@link ICache.name}.
26
32
  */
27
33
  export interface ICacheConstructor {
34
+ /**
35
+ * @param name - legacy, and never supplied by the registry
36
+ */
28
37
  new (name?: string): ICache;
29
38
  }
30
39
  /**
@@ -35,50 +44,83 @@ export interface ICache {
35
44
  /**
36
45
  * Adapter (cache) name.
37
46
  *
38
- * @type {string}
47
+ * @remarks
48
+ * The name is load-bearing twice over: it is the key the adapter is registered
49
+ * under, and it is a segment of every physical cache key
50
+ * (`<prefix>:<name>:<key>` in the Redis adapter). It must therefore be stable —
51
+ * changing it at runtime moves the namespace and orphans existing entries.
39
52
  */
40
53
  name: string;
41
54
  /**
42
55
  * Whether the cache adapter is initialized and ready to use.
43
56
  *
44
- * @type {boolean}
57
+ * @remarks
58
+ * For the built-in Redis adapter this flag is never reset — destroying the
59
+ * shared connection leaves it `true`, so a `true` value does not guarantee a
60
+ * live connection after an explicit destroy.
45
61
  */
46
62
  ready: boolean;
47
63
  /**
48
64
  * Initializes the cache adapter with the given adapter-specific options.
49
65
  *
50
- * @param {any} [options] - adapter-specific options
51
- * @returns {void}
66
+ * @param options - adapter-specific options
67
+ *
68
+ * @remarks
69
+ * May be asynchronous: the registry awaits whatever is returned, so an
70
+ * implementation that needs I/O must return a promise that settles once the
71
+ * adapter is ready. Implementations must set {@link ICache.ready} on success.
52
72
  */
53
73
  init(options?: any): void;
54
74
  /**
55
75
  * Returns the value stored in the cache under the given key.
56
76
  *
57
- * @param {string} key - key to read the value for
58
- * @returns {Promise<any>} - stored value, or undefined if not found
77
+ * @param key - key to read the value for
78
+ * @returns the deserialized stored value, or `undefined` when the key is absent
79
+ * or expired
80
+ *
81
+ * @remarks
82
+ * A stored `null` is returned as `null` and is therefore distinguishable from a
83
+ * miss; a stored `undefined` is not. Callers that must tell "cached as
84
+ * empty" from "not cached" should store an explicit sentinel rather than
85
+ * `undefined`.
59
86
  */
60
87
  get(key: string): Promise<any>;
61
88
  /**
62
89
  * Stores the given value in the cache under the given key.
63
90
  *
64
- * @param {string} key - key to store the value under
65
- * @param {any} value - value to store
66
- * @param {number} [ttl] - time-to-live in milliseconds
67
- * @returns {Promise<boolean>}
91
+ * @param key - key to store the value under
92
+ * @param value - value to store
93
+ * @param ttl - time-to-live in milliseconds
94
+ * @returns a truthy value when the write happened, falsy when it did not
95
+ *
96
+ * @remarks
97
+ * Test the result for truthiness rather than comparing against `true` — the
98
+ * built-in Redis adapter resolves to the string `'OK'` or to `null`, never to a
99
+ * boolean.
100
+ *
101
+ * The framework additionally passes a fourth argument, an `nx` flag meaning
102
+ * "only create the key if it does not already exist". Adapters that cannot
103
+ * honour it should say so, because the {@link cache} decorator always passes it.
68
104
  */
69
105
  set(key: string, value: any, ttl?: number): Promise<boolean>;
70
106
  /**
71
107
  * Removes the value stored in the cache under the given key.
72
108
  *
73
- * @param {string} key - key to remove
74
- * @returns {Promise<boolean>}
109
+ * @param key - key to remove
110
+ * @returns true if a key was actually removed, false if it did not exist —
111
+ * which is not an error
75
112
  */
76
113
  del(key: string): Promise<boolean>;
77
114
  /**
78
- * Purges all keys from the cache matching the given wildcard mask.
115
+ * Deletes every key matching the given wildcard mask.
116
+ *
117
+ * @param keyMask - wildcard mask matched against fully qualified key names
118
+ * @returns whether the purge ran
79
119
  *
80
- * @param {string} keyMask - wildcard mask to match keys against
81
- * @returns {Promise<boolean>}
120
+ * @remarks
121
+ * The mask is not automatically scoped to this cache — callers must include
122
+ * the namespace themselves (`<prefix>:<name>:...`). A broad mask such as `'*'`
123
+ * will delete unrelated data, including message-queue keys.
82
124
  */
83
125
  purge(keyMask: string): Promise<boolean>;
84
126
  }