@imqueue/rpc 3.4.3 → 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.
- package/CHANGELOG.md +19 -1
- package/index.d.ts +58 -0
- package/index.js +58 -0
- package/package.json +2 -2
- package/src/IMQCache.d.ts +20 -11
- package/src/IMQCache.js +20 -11
- package/src/IMQClient.d.ts +191 -38
- package/src/IMQClient.js +204 -57
- package/src/IMQDelay.d.ts +29 -6
- package/src/IMQDelay.js +20 -6
- package/src/IMQLock.d.ts +81 -35
- package/src/IMQLock.js +38 -35
- package/src/IMQMetadata.d.ts +9 -1
- package/src/IMQMetadata.js +1 -1
- package/src/IMQRPCDescription.d.ts +183 -9
- package/src/IMQRPCDescription.js +22 -0
- package/src/IMQRPCError.d.ts +91 -9
- package/src/IMQRPCError.js +23 -7
- package/src/IMQRPCOptions.d.ts +221 -18
- package/src/IMQRPCOptions.js +22 -6
- package/src/IMQRPCRequest.d.ts +39 -1
- package/src/IMQRPCResponse.d.ts +34 -0
- package/src/IMQRequestContext.d.ts +2 -5
- package/src/IMQRequestContext.js +2 -5
- package/src/IMQService.d.ts +144 -19
- package/src/IMQService.js +151 -33
- package/src/cache/ICache.d.ts +58 -16
- package/src/cache/RedisCache.d.ts +58 -17
- package/src/cache/RedisCache.js +43 -17
- package/src/decorators/cache.d.ts +46 -2
- package/src/decorators/cache.js +2 -2
- package/src/decorators/classType.d.ts +28 -13
- package/src/decorators/classType.js +28 -13
- package/src/decorators/expose.d.ts +59 -7
- package/src/decorators/expose.js +77 -30
- package/src/decorators/indexed.d.ts +27 -8
- package/src/decorators/indexed.js +27 -8
- package/src/decorators/lock.d.ts +20 -2
- package/src/decorators/lock.js +2 -2
- package/src/decorators/logged.d.ts +29 -2
- package/src/decorators/logged.js +2 -2
- package/src/decorators/property.d.ts +97 -4
- package/src/decorators/property.js +84 -6
- package/src/decorators/remote.d.ts +1 -1
- package/src/decorators/remote.js +1 -1
- package/src/helpers/fs.d.ts +5 -7
- package/src/helpers/fs.js +8 -10
- package/src/helpers/os-uuid.d.ts +0 -2
- package/src/helpers/os-uuid.js +2 -10
- package/src/helpers/pid.d.ts +7 -7
- package/src/helpers/pid.js +10 -11
- package/src/helpers/signature.d.ts +4 -4
- package/src/helpers/signature.js +7 -7
package/src/IMQClient.js
CHANGED
|
@@ -74,7 +74,27 @@ const cjsRequire = createRequire(import.meta.url);
|
|
|
74
74
|
const tsOptions = JSON.parse(readFileSync(new URL('../tsconfig.json', import.meta.url), 'utf8')).compilerOptions;
|
|
75
75
|
const RX_SEMICOLON = /;+$/g;
|
|
76
76
|
/**
|
|
77
|
-
*
|
|
77
|
+
* Base class for service clients.
|
|
78
|
+
*
|
|
79
|
+
* Subclass it and declare every remote method as
|
|
80
|
+
* `@remote() async m(...args) \{ return await this.remoteCall<T>(...arguments); \}`,
|
|
81
|
+
* or let {@link IMQClient.create} generate the subclass from a running service's
|
|
82
|
+
* description.
|
|
83
|
+
*
|
|
84
|
+
* @remarks
|
|
85
|
+
* Abstractness is enforced at runtime as well as by the type system: constructing
|
|
86
|
+
* `IMQClient` directly throws a `TypeError`.
|
|
87
|
+
*
|
|
88
|
+
* Instances are `EventEmitter`s. Besides ordinary calls, any response that no
|
|
89
|
+
* longer has a pending caller is emitted as an event named after the remote
|
|
90
|
+
* method, carrying the raw {@link IMQRPCResponse} — the escape hatch for replies to
|
|
91
|
+
* calls made by a process that has since died.
|
|
92
|
+
*
|
|
93
|
+
* The identity properties pair up as follows: {@link IMQClient.serviceName} is
|
|
94
|
+
* where calls go, {@link IMQClient.queueName} is where answers come back,
|
|
95
|
+
* {@link IMQClient.name} identifies this client, {@link IMQClient.hostName}
|
|
96
|
+
* identifies the machine plus instance, and {@link IMQClient.id} is the per-host
|
|
97
|
+
* instance slot.
|
|
78
98
|
*/
|
|
79
99
|
let IMQClient = (() => {
|
|
80
100
|
let _classSuper = EventEmitter;
|
|
@@ -87,11 +107,56 @@ let IMQClient = (() => {
|
|
|
87
107
|
__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);
|
|
88
108
|
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
89
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* The effective options for this client: {@link DEFAULT_IMQ_CLIENT_OPTIONS}
|
|
112
|
+
* merged with the values passed to the constructor.
|
|
113
|
+
*/
|
|
90
114
|
options = __runInitializers(this, _instanceExtraInitializers);
|
|
115
|
+
/**
|
|
116
|
+
* Per-host instance slot number — not an OS process id.
|
|
117
|
+
*
|
|
118
|
+
* @remarks
|
|
119
|
+
* Allocated by claiming the lowest free pid file under `$TMPDIR/.imq-rpc`; the
|
|
120
|
+
* file itself contains the real `process.pid`. Ids released by
|
|
121
|
+
* {@link IMQClient.destroy} are not re-issued within the same process.
|
|
122
|
+
*/
|
|
91
123
|
id;
|
|
124
|
+
/**
|
|
125
|
+
* This client's unique identity, `"<baseName>-<hostName>"`.
|
|
126
|
+
*
|
|
127
|
+
* @remarks
|
|
128
|
+
* Also used as the name of the dedicated subscription queue in `singleQueue`
|
|
129
|
+
* mode.
|
|
130
|
+
*/
|
|
92
131
|
name;
|
|
132
|
+
/**
|
|
133
|
+
* Machine-scoped identity, `"<osUuid>-<id>:client"`, where `osUuid` is a hash of
|
|
134
|
+
* the OS machine id.
|
|
135
|
+
*
|
|
136
|
+
* @remarks
|
|
137
|
+
* This is not a network hostname. The `:client` suffix is what makes the
|
|
138
|
+
* default `cleanupFilter` of `'*:client'` match client queues. In `singleQueue`
|
|
139
|
+
* mode it is inherited from the shared queue's name instead.
|
|
140
|
+
*/
|
|
93
141
|
hostName;
|
|
142
|
+
/**
|
|
143
|
+
* The remote side: the queue every request is addressed to, and the pub/sub
|
|
144
|
+
* channel {@link IMQClient.subscribe} listens on.
|
|
145
|
+
*
|
|
146
|
+
* @remarks
|
|
147
|
+
* Defaults to this client's base name with a trailing `Client` removed, so
|
|
148
|
+
* `UserClient` talks to the queue `User`.
|
|
149
|
+
*/
|
|
94
150
|
serviceName;
|
|
151
|
+
/**
|
|
152
|
+
* The local side: the queue replies come back on, and the value placed in
|
|
153
|
+
* `request.from`.
|
|
154
|
+
*
|
|
155
|
+
* @remarks
|
|
156
|
+
* Equals {@link IMQClient.name}, or {@link IMQClient.hostName} when
|
|
157
|
+
* `singleQueue` is enabled. Also the channel {@link IMQClient.broadcast}
|
|
158
|
+
* publishes to.
|
|
159
|
+
*/
|
|
95
160
|
queueName;
|
|
96
161
|
baseName;
|
|
97
162
|
imq;
|
|
@@ -104,11 +169,28 @@ let IMQClient = (() => {
|
|
|
104
169
|
logger;
|
|
105
170
|
resolvers = {};
|
|
106
171
|
/**
|
|
107
|
-
*
|
|
172
|
+
* Constructs a client.
|
|
173
|
+
*
|
|
174
|
+
* @param options - client options, merged over
|
|
175
|
+
* {@link DEFAULT_IMQ_CLIENT_OPTIONS}
|
|
176
|
+
* @param serviceName - the queue calls are addressed to. Defaults to this
|
|
177
|
+
* client's own name with a trailing `Client` removed.
|
|
178
|
+
* @param name - this client's base name. Defaults to the constructor's name, so
|
|
179
|
+
* pass it explicitly if your build renames classes — a minifier would
|
|
180
|
+
* otherwise silently retarget the RPC.
|
|
181
|
+
* @throws TypeError when {@link IMQClient} is constructed directly rather than
|
|
182
|
+
* through a subclass
|
|
108
183
|
*
|
|
109
|
-
* @
|
|
110
|
-
*
|
|
111
|
-
*
|
|
184
|
+
* @remarks
|
|
185
|
+
* Construction has side effects. It reserves an instance id by creating a pid
|
|
186
|
+
* file under `$TMPDIR/.imq-rpc`, opens its message queue (two in `singleQueue`
|
|
187
|
+
* mode), raises the process max-listener limit once per process, and installs
|
|
188
|
+
* `SIGTERM`/`SIGINT`/`SIGHUP`/`SIGQUIT` handlers that destroy the client and
|
|
189
|
+
* then exit the process after `IMQ_SHUTDOWN_TIMEOUT`. Any library embedding
|
|
190
|
+
* a client inherits that process-terminating behaviour.
|
|
191
|
+
*
|
|
192
|
+
* Only {@link IMQClient.destroy} releases the pid file and removes those
|
|
193
|
+
* handlers.
|
|
112
194
|
*/
|
|
113
195
|
constructor(options, serviceName, name) {
|
|
114
196
|
super();
|
|
@@ -163,11 +245,34 @@ let IMQClient = (() => {
|
|
|
163
245
|
return IMQ.create(this.name, this.options);
|
|
164
246
|
}
|
|
165
247
|
/**
|
|
166
|
-
* Sends call to remote service method
|
|
248
|
+
* Sends a call to the remote service method.
|
|
249
|
+
*
|
|
250
|
+
* Intended to be invoked as `this.remoteCall<T>(...arguments)` from a method
|
|
251
|
+
* decorated with {@link remote}, which appends the method name for you.
|
|
167
252
|
*
|
|
168
|
-
* @
|
|
169
|
-
* @
|
|
170
|
-
* @
|
|
253
|
+
* @typeParam T - the type the remote method resolves to
|
|
254
|
+
* @param args - the method's own arguments, followed by the remote method
|
|
255
|
+
* name as the last element. An optional trailing {@link IMQDelay}
|
|
256
|
+
* (delivery delay) and {@link IMQMetadata} (tracing metadata) are
|
|
257
|
+
* consumed by the framework and never reach the service; on a delayed
|
|
258
|
+
* call, trailing `undefined` placeholders are dropped so service-side
|
|
259
|
+
* defaults still apply.
|
|
260
|
+
* @returns the service's response payload, cast to `T`
|
|
261
|
+
*
|
|
262
|
+
* @remarks
|
|
263
|
+
* {@link IMQClient.start} must have completed first. Otherwise the request is
|
|
264
|
+
* sent but no reply router is installed, so the returned promise never
|
|
265
|
+
* settles.
|
|
266
|
+
*
|
|
267
|
+
* Rejections are plain {@link IMQRPCError} objects, not `Error`
|
|
268
|
+
* instances — `err instanceof Error` is false, and `err.stack` describes the
|
|
269
|
+
* remote process. The code is `IMQ_RPC_CALL_ERROR` for a service-side failure
|
|
270
|
+
* and `IMQ_RPC_CALL_TIMEOUT` once {@link IMQClientOptions.callTimeout} elapses
|
|
271
|
+
* (plus any requested delay). With `callTimeout` unset — the default — a hung
|
|
272
|
+
* service leaves the promise pending indefinitely.
|
|
273
|
+
*
|
|
274
|
+
* `beforeCall` and `afterCall` hook failures are logged as warnings and
|
|
275
|
+
* otherwise ignored.
|
|
171
276
|
*/
|
|
172
277
|
async remoteCall(...args) {
|
|
173
278
|
const logger = this.options.logger || console;
|
|
@@ -256,36 +361,54 @@ let IMQClient = (() => {
|
|
|
256
361
|
});
|
|
257
362
|
}
|
|
258
363
|
/**
|
|
259
|
-
*
|
|
364
|
+
* Subscribes to the service's event channel, named after
|
|
365
|
+
* {@link IMQClient.serviceName} — the same channel a service's `publish()`
|
|
366
|
+
* writes to, so every client of that service receives every published payload.
|
|
367
|
+
*
|
|
368
|
+
* @param handler - invoked with the parsed JSON payload of each published
|
|
369
|
+
* message
|
|
260
370
|
*
|
|
261
|
-
* @
|
|
262
|
-
*
|
|
371
|
+
* @remarks
|
|
372
|
+
* This is fan-out, not a private channel. Subscribing uses a dedicated
|
|
373
|
+
* connection and does not require {@link IMQClient.start}. Calling it more
|
|
374
|
+
* than once registers additional handlers rather than replacing the existing
|
|
375
|
+
* one, and the subscription is re-established automatically on reconnect.
|
|
263
376
|
*/
|
|
264
377
|
async subscribe(handler) {
|
|
265
378
|
return this.subscriptionImq.subscribe(this.serviceName, handler);
|
|
266
379
|
}
|
|
267
380
|
/**
|
|
268
|
-
*
|
|
381
|
+
* Stops receiving service events: unsubscribes the channel, discards every
|
|
382
|
+
* handler registered through {@link IMQClient.subscribe}, and closes the
|
|
383
|
+
* dedicated subscription connection.
|
|
269
384
|
*
|
|
270
|
-
* @
|
|
385
|
+
* @remarks
|
|
386
|
+
* Safe when not subscribed, and it does not affect remote calls.
|
|
387
|
+
* {@link IMQClient.destroy} performs it automatically; afterwards
|
|
388
|
+
* {@link IMQClient.subscribe} can be used again with a fresh connection.
|
|
271
389
|
*/
|
|
272
390
|
async unsubscribe() {
|
|
273
391
|
return this.subscriptionImq.unsubscribe();
|
|
274
392
|
}
|
|
275
393
|
/**
|
|
276
|
-
*
|
|
277
|
-
*
|
|
394
|
+
* Publishes the given payload on this client's own queue channel
|
|
395
|
+
* ({@link IMQClient.queueName}).
|
|
278
396
|
*
|
|
279
|
-
* @param
|
|
280
|
-
* @
|
|
397
|
+
* @param payload - data to publish
|
|
398
|
+
* @throws TypeError when the client's queue has no writer connection, i.e. when
|
|
399
|
+
* called before {@link IMQClient.start}
|
|
400
|
+
*
|
|
401
|
+
* @remarks
|
|
402
|
+
* Note that {@link IMQClient.subscribe} listens on the service channel, so a
|
|
403
|
+
* broadcast is not received by other clients of the same service through the
|
|
404
|
+
* standard client API — a consumer must subscribe to this client's `queueName`
|
|
405
|
+
* channel explicitly.
|
|
281
406
|
*/
|
|
282
407
|
async broadcast(payload) {
|
|
283
408
|
return this.imq.publish(payload, this.queueName);
|
|
284
409
|
}
|
|
285
410
|
/**
|
|
286
411
|
* Initializes client work
|
|
287
|
-
*
|
|
288
|
-
* @returns {Promise<void>}
|
|
289
412
|
*/
|
|
290
413
|
async start() {
|
|
291
414
|
this.imq.on('message', (message) => {
|
|
@@ -314,16 +437,12 @@ let IMQClient = (() => {
|
|
|
314
437
|
}
|
|
315
438
|
/**
|
|
316
439
|
* Stops client work
|
|
317
|
-
*
|
|
318
|
-
* @returns {Promise<void>}
|
|
319
440
|
*/
|
|
320
441
|
async stop() {
|
|
321
442
|
await this.imq.stop();
|
|
322
443
|
}
|
|
323
444
|
/**
|
|
324
445
|
* Destroys client
|
|
325
|
-
*
|
|
326
|
-
* @returns {Promise<void>}
|
|
327
446
|
*/
|
|
328
447
|
async destroy() {
|
|
329
448
|
if (this.destroyed) {
|
|
@@ -359,19 +478,53 @@ let IMQClient = (() => {
|
|
|
359
478
|
/**
|
|
360
479
|
* Returns service description metadata.
|
|
361
480
|
*
|
|
362
|
-
* @param
|
|
481
|
+
* @param _delay - optional delivery delay; forwarded to the
|
|
363
482
|
* service through `arguments` by the `@remote` decorator
|
|
364
|
-
* @returns {Promise<Description>}
|
|
365
483
|
*/
|
|
366
484
|
async describe(_delay) {
|
|
367
485
|
return await this.remoteCall(...arguments);
|
|
368
486
|
}
|
|
369
487
|
/**
|
|
370
|
-
*
|
|
488
|
+
* Generates a client for the service registered under the given queue name and
|
|
489
|
+
* resolves to the generated namespace object — not to a client instance, and
|
|
490
|
+
* not to an {@link IMQClient}.
|
|
491
|
+
*
|
|
492
|
+
* @param name - name of the queue the target service listens on (its service
|
|
493
|
+
* name, which is the class name by default). Also used as the base name
|
|
494
|
+
* of the generated files.
|
|
495
|
+
* @param options - client options, merged over
|
|
496
|
+
* {@link DEFAULT_IMQ_CLIENT_OPTIONS}
|
|
497
|
+
* @returns the generated namespace object, exposing the generated client class
|
|
498
|
+
* (`<Service>Client`, with a trailing `Service` replaced by `Client`)
|
|
499
|
+
* plus one interface per registered service type. It is produced at
|
|
500
|
+
* runtime, so it is typed `any` — cast it or type the call site
|
|
501
|
+
* yourself. Resolves to `null` instead when
|
|
502
|
+
* {@link IMQClientOptions.compile} is false.
|
|
503
|
+
* @throws EvalError when the target service does not answer within
|
|
504
|
+
* {@link IMQClientOptions.timeout} milliseconds
|
|
505
|
+
*
|
|
506
|
+
* @example
|
|
507
|
+
* ```typescript
|
|
508
|
+
* const ns = await IMQClient.create('UserService');
|
|
509
|
+
* const client = new ns.UserClient();
|
|
510
|
+
*
|
|
511
|
+
* await client.start();
|
|
512
|
+
* ```
|
|
513
|
+
*
|
|
514
|
+
* @remarks
|
|
515
|
+
* Generating a client requires the target service to be running: this sends
|
|
516
|
+
* a live description request over the queue and fails if no answer arrives in
|
|
517
|
+
* time.
|
|
518
|
+
*
|
|
519
|
+
* It always transpiles the generated source by spawning TypeScript
|
|
520
|
+
* synchronously, so the call blocks the event loop, and it rejects if
|
|
521
|
+
* transpilation emits nothing. When {@link IMQClientOptions.write} is set — the
|
|
522
|
+
* default — it writes both `<path>/<name>.ts` and `<path>/<name>.js`, silently
|
|
523
|
+
* overwriting any existing files, and creates `path` only one level deep.
|
|
371
524
|
*
|
|
372
|
-
*
|
|
373
|
-
*
|
|
374
|
-
*
|
|
525
|
+
* The identifiers inside the generated module come from the service's own
|
|
526
|
+
* reported class name, so if that differs from `name` the file name and the
|
|
527
|
+
* namespace/class names differ too.
|
|
375
528
|
*/
|
|
376
529
|
static async create(name, options) {
|
|
377
530
|
const clientOptions = {
|
|
@@ -387,10 +540,10 @@ export { IMQClient };
|
|
|
387
540
|
* Builds a call resolver that resolves the pending promise and then runs the
|
|
388
541
|
* optional after-call hook.
|
|
389
542
|
*
|
|
390
|
-
* @param
|
|
391
|
-
* @param
|
|
392
|
-
* @param
|
|
393
|
-
* @
|
|
543
|
+
* @param resolve - the underlying promise resolver
|
|
544
|
+
* @param req - the originating request message
|
|
545
|
+
* @param client - the client the call belongs to
|
|
546
|
+
* @returns a hook-aware resolver
|
|
394
547
|
*/
|
|
395
548
|
export function imqCallResolver(resolve, req, client) {
|
|
396
549
|
return async (data, res) => {
|
|
@@ -411,10 +564,10 @@ export function imqCallResolver(resolve, req, client) {
|
|
|
411
564
|
* Builds a call rejector that rejects the pending promise and then runs the
|
|
412
565
|
* optional after-call hook.
|
|
413
566
|
*
|
|
414
|
-
* @param
|
|
415
|
-
* @param
|
|
416
|
-
* @param
|
|
417
|
-
* @
|
|
567
|
+
* @param reject - the underlying promise rejector
|
|
568
|
+
* @param req - the originating request message
|
|
569
|
+
* @param client - the client the call belongs to
|
|
570
|
+
* @returns a hook-aware rejector
|
|
418
571
|
*/
|
|
419
572
|
export function imqCallRejector(reject, req, client) {
|
|
420
573
|
return async (err, res) => {
|
|
@@ -440,9 +593,8 @@ class GeneratorClient extends IMQClient {
|
|
|
440
593
|
* Fetches and returns service description using the timeout (to handle
|
|
441
594
|
* situations when the service is not started)
|
|
442
595
|
*
|
|
443
|
-
* @param
|
|
444
|
-
* @param
|
|
445
|
-
* @returns {Promise<Description>}
|
|
596
|
+
* @param name -
|
|
597
|
+
* @param options -
|
|
446
598
|
*/
|
|
447
599
|
async function getDescription(name, options) {
|
|
448
600
|
return new Promise((resolve, reject) => {
|
|
@@ -465,9 +617,8 @@ async function getDescription(name, options) {
|
|
|
465
617
|
/**
|
|
466
618
|
* Client generator helper function
|
|
467
619
|
*
|
|
468
|
-
* @param
|
|
469
|
-
* @param
|
|
470
|
-
* @returns {Promise<string>}
|
|
620
|
+
* @param name -
|
|
621
|
+
* @param options -
|
|
471
622
|
*/
|
|
472
623
|
async function generator(name, options) {
|
|
473
624
|
const description = await getDescription(name, options);
|
|
@@ -604,8 +755,7 @@ export namespace ${namespaceName} {\n`;
|
|
|
604
755
|
/**
|
|
605
756
|
* Return the promised typedef of a given type if its missing
|
|
606
757
|
*
|
|
607
|
-
* @param
|
|
608
|
-
* @returns {string}
|
|
758
|
+
* @param typedef -
|
|
609
759
|
*/
|
|
610
760
|
function promisedType(typedef) {
|
|
611
761
|
if (!typedef.startsWith('Promise<')) {
|
|
@@ -616,8 +766,7 @@ function promisedType(typedef) {
|
|
|
616
766
|
/**
|
|
617
767
|
* Removes Promise from type definition if any
|
|
618
768
|
*
|
|
619
|
-
* @param
|
|
620
|
-
* @returns {string}
|
|
769
|
+
* @param typedef -
|
|
621
770
|
*/
|
|
622
771
|
function cleanType(typedef) {
|
|
623
772
|
return typedef.replace(/^Promise<([\s\S]+?)>$/, '$1');
|
|
@@ -625,9 +774,8 @@ function cleanType(typedef) {
|
|
|
625
774
|
/**
|
|
626
775
|
* Type to comment
|
|
627
776
|
*
|
|
628
|
-
* @param
|
|
629
|
-
* @param
|
|
630
|
-
* @returns {string}
|
|
777
|
+
* @param typedef -
|
|
778
|
+
* @param promised -
|
|
631
779
|
*/
|
|
632
780
|
function toComment(typedef, promised = false) {
|
|
633
781
|
if (promised) {
|
|
@@ -648,8 +796,8 @@ function toComment(typedef, promised = false) {
|
|
|
648
796
|
* module-resolution errors" behaviour the old single-file transpile provided,
|
|
649
797
|
* built on the stable CLI rather than the unstable programmatic API.
|
|
650
798
|
*
|
|
651
|
-
* @param
|
|
652
|
-
* @returns
|
|
799
|
+
* @param src - generated client source
|
|
800
|
+
* @returns emitted CommonJS JavaScript
|
|
653
801
|
*/
|
|
654
802
|
function transpileClient(src) {
|
|
655
803
|
const dir = mkdtempSync(join(tmpdir(), 'imq-client-'));
|
|
@@ -699,10 +847,9 @@ function transpileClient(src) {
|
|
|
699
847
|
/**
|
|
700
848
|
* Compiles client source code and returns loaded module
|
|
701
849
|
*
|
|
702
|
-
* @param
|
|
703
|
-
* @param
|
|
704
|
-
* @param
|
|
705
|
-
* @returns {any}
|
|
850
|
+
* @param name -
|
|
851
|
+
* @param src -
|
|
852
|
+
* @param options -
|
|
706
853
|
*/
|
|
707
854
|
async function compile(name, src, options) {
|
|
708
855
|
const path = options.path;
|
package/src/IMQDelay.d.ts
CHANGED
|
@@ -26,18 +26,41 @@
|
|
|
26
26
|
* Used to defer IMQ request processing.
|
|
27
27
|
*/
|
|
28
28
|
export declare class IMQDelay {
|
|
29
|
+
/**
|
|
30
|
+
* The delay magnitude, expressed in {@link IMQDelay.unit}. Mutable after
|
|
31
|
+
* construction, and read by {@link IMQDelay.ms}.
|
|
32
|
+
*/
|
|
29
33
|
timer: number;
|
|
34
|
+
/**
|
|
35
|
+
* Time unit of {@link IMQDelay.timer} — milliseconds, seconds, minutes,
|
|
36
|
+
* hours or days. Defaults to `'ms'`, meaning `timer` is already a
|
|
37
|
+
* millisecond count.
|
|
38
|
+
*/
|
|
30
39
|
unit: 'ms' | 's' | 'm' | 'h' | 'd';
|
|
31
40
|
/**
|
|
32
|
-
* Returns
|
|
41
|
+
* Returns {@link IMQDelay.timer} converted to milliseconds.
|
|
33
42
|
*
|
|
34
|
-
* @
|
|
43
|
+
* @remarks
|
|
44
|
+
* Not rounded, so a fractional `timer` yields a fractional result. If
|
|
45
|
+
* {@link IMQDelay.unit} has been set to a value outside the supported set —
|
|
46
|
+
* possible from untyped callers — the result is `undefined`; a client treats any
|
|
47
|
+
* non-finite or negative value as no delay.
|
|
35
48
|
*/
|
|
36
49
|
get ms(): number;
|
|
37
50
|
/**
|
|
38
|
-
* @param
|
|
39
|
-
* @param
|
|
40
|
-
|
|
51
|
+
* @param timer - delay value expressed in the given unit
|
|
52
|
+
* @param unit - time unit of the timer value; defaults to `'ms'`
|
|
53
|
+
*/
|
|
54
|
+
constructor(
|
|
55
|
+
/**
|
|
56
|
+
* The delay magnitude, expressed in {@link IMQDelay.unit}. Mutable after
|
|
57
|
+
* construction, and read by {@link IMQDelay.ms}.
|
|
58
|
+
*/
|
|
59
|
+
timer: number,
|
|
60
|
+
/**
|
|
61
|
+
* Time unit of {@link IMQDelay.timer} — milliseconds, seconds, minutes,
|
|
62
|
+
* hours or days. Defaults to `'ms'`, meaning `timer` is already a
|
|
63
|
+
* millisecond count.
|
|
41
64
|
*/
|
|
42
|
-
|
|
65
|
+
unit?: 'ms' | 's' | 'm' | 'h' | 'd');
|
|
43
66
|
}
|
package/src/IMQDelay.js
CHANGED
|
@@ -29,9 +29,13 @@ export class IMQDelay {
|
|
|
29
29
|
timer;
|
|
30
30
|
unit;
|
|
31
31
|
/**
|
|
32
|
-
* Returns
|
|
32
|
+
* Returns {@link IMQDelay.timer} converted to milliseconds.
|
|
33
33
|
*
|
|
34
|
-
* @
|
|
34
|
+
* @remarks
|
|
35
|
+
* Not rounded, so a fractional `timer` yields a fractional result. If
|
|
36
|
+
* {@link IMQDelay.unit} has been set to a value outside the supported set —
|
|
37
|
+
* possible from untyped callers — the result is `undefined`; a client treats any
|
|
38
|
+
* non-finite or negative value as no delay.
|
|
35
39
|
*/
|
|
36
40
|
get ms() {
|
|
37
41
|
switch (this.unit) {
|
|
@@ -48,11 +52,21 @@ export class IMQDelay {
|
|
|
48
52
|
}
|
|
49
53
|
}
|
|
50
54
|
/**
|
|
51
|
-
* @param
|
|
52
|
-
* @param
|
|
53
|
-
* value, defaults to 'ms'
|
|
55
|
+
* @param timer - delay value expressed in the given unit
|
|
56
|
+
* @param unit - time unit of the timer value; defaults to `'ms'`
|
|
54
57
|
*/
|
|
55
|
-
constructor(
|
|
58
|
+
constructor(
|
|
59
|
+
/**
|
|
60
|
+
* The delay magnitude, expressed in {@link IMQDelay.unit}. Mutable after
|
|
61
|
+
* construction, and read by {@link IMQDelay.ms}.
|
|
62
|
+
*/
|
|
63
|
+
timer,
|
|
64
|
+
/**
|
|
65
|
+
* Time unit of {@link IMQDelay.timer} — milliseconds, seconds, minutes,
|
|
66
|
+
* hours or days. Defaults to `'ms'`, meaning `timer` is already a
|
|
67
|
+
* millisecond count.
|
|
68
|
+
*/
|
|
69
|
+
unit = 'ms') {
|
|
56
70
|
this.timer = timer;
|
|
57
71
|
this.unit = unit;
|
|
58
72
|
}
|
package/src/IMQLock.d.ts
CHANGED
|
@@ -22,42 +22,98 @@
|
|
|
22
22
|
* <support@imqueue.com> to get commercial licensing options.
|
|
23
23
|
*/
|
|
24
24
|
import { type ILogger } from '@imqueue/core';
|
|
25
|
+
/**
|
|
26
|
+
* What {@link IMQLock.acquire} resolves to: the literal `true` when this caller
|
|
27
|
+
* acquired the lock and must perform the work, or the value the lock holder passed
|
|
28
|
+
* to {@link IMQLock.release} when this caller had to wait.
|
|
29
|
+
*
|
|
30
|
+
* @remarks
|
|
31
|
+
* Because the acquired case is the literal `true`, it cannot be told apart from a
|
|
32
|
+
* resolved value of `true`. Always use {@link IMQLock.locked} immediately after
|
|
33
|
+
* awaiting to decide whether you are the holder — never inspect the returned
|
|
34
|
+
* value.
|
|
35
|
+
*/
|
|
25
36
|
export type AcquiredLock<T> = T | boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Internal representation of one queued waiter: its promise's
|
|
39
|
+
* `[resolve, reject]` pair, selected by {@link IMQLock.release} according to
|
|
40
|
+
* whether an error was supplied.
|
|
41
|
+
*/
|
|
26
42
|
export type IMQLockTask = [(...args: any[]) => any, (...args: any[]) => any];
|
|
43
|
+
/**
|
|
44
|
+
* The FIFO queue of callers waiting on a single lock key, drained in arrival
|
|
45
|
+
* order when the lock is released.
|
|
46
|
+
*/
|
|
27
47
|
export type IMQLockQueue = Array<IMQLockTask>;
|
|
48
|
+
/**
|
|
49
|
+
* Diagnostic description of a locked call.
|
|
50
|
+
*
|
|
51
|
+
* @remarks
|
|
52
|
+
* Used only to enrich the deadlock-timeout error message. Values that cannot be
|
|
53
|
+
* serialized degrade to a placeholder rather than failing.
|
|
54
|
+
*/
|
|
28
55
|
export interface IMQLockMetadataItem {
|
|
56
|
+
/**
|
|
57
|
+
* Name of the class whose method holds the lock.
|
|
58
|
+
*/
|
|
29
59
|
className: string;
|
|
60
|
+
/**
|
|
61
|
+
* Name of the locked method.
|
|
62
|
+
*/
|
|
30
63
|
methodName: string | symbol;
|
|
64
|
+
/**
|
|
65
|
+
* Arguments the locked method was called with.
|
|
66
|
+
*/
|
|
31
67
|
args: any[];
|
|
32
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Map from lock key to the metadata describing the call currently associated with
|
|
71
|
+
* that key.
|
|
72
|
+
*/
|
|
33
73
|
export interface IMQLockMetadata {
|
|
74
|
+
/**
|
|
75
|
+
* Metadata for the given lock key.
|
|
76
|
+
*/
|
|
34
77
|
[key: string]: IMQLockMetadataItem;
|
|
35
78
|
}
|
|
36
79
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
80
|
+
* In-process, promise-based locks used to collapse concurrent identical calls: the
|
|
81
|
+
* first caller executes the work while later callers for the same key wait and are
|
|
82
|
+
* then resolved with the first caller's result.
|
|
83
|
+
*
|
|
84
|
+
* @remarks
|
|
85
|
+
* These are not distributed locks. The lock table is a set of plain static
|
|
86
|
+
* objects held in memory, and nothing here touches Redis, the network or any
|
|
87
|
+
* shared store. Separate processes, cluster workers and service replicas each
|
|
88
|
+
* maintain their own independent locks and will run the guarded code
|
|
89
|
+
* concurrently. {@link lock} inherits the same limitation. Use a Redis- or
|
|
90
|
+
* database-backed lock if you need mutual exclusion across processes.
|
|
91
|
+
*
|
|
92
|
+
* Keys are used verbatim, with no prefixing or namespacing, so they are global to
|
|
93
|
+
* the process and unrelated call sites sharing a string share a lock.
|
|
39
94
|
*
|
|
40
95
|
* @example
|
|
41
|
-
*
|
|
42
|
-
* import { IMQLock, AcquiredLock } from '
|
|
96
|
+
* ```typescript
|
|
97
|
+
* import { IMQLock, type AcquiredLock } from '@imqueue/rpc';
|
|
43
98
|
*
|
|
44
99
|
* async function doSomething(): Promise<number | AcquiredLock<number>> {
|
|
45
|
-
* const lock: AcquiredLock<number> =
|
|
100
|
+
* const lock: AcquiredLock<number> =
|
|
101
|
+
* await IMQLock.acquire<number>('doSomething');
|
|
46
102
|
*
|
|
103
|
+
* // locked() is the only reliable way to tell holder from waiter
|
|
47
104
|
* if (IMQLock.locked('doSomething')) {
|
|
48
|
-
* //
|
|
49
|
-
* //
|
|
50
|
-
* // BTW, IMQLock uses timeouts to avoid dead-locks
|
|
105
|
+
* // always wrap locked work in try/catch and release on both paths,
|
|
106
|
+
* // otherwise waiters hang until the deadlock timeout fires
|
|
51
107
|
* try {
|
|
52
|
-
* //
|
|
53
|
-
* //
|
|
108
|
+
* // runs only once across all concurrent calls; every waiter
|
|
109
|
+
* // resolves with this same value
|
|
54
110
|
* const res = Math.random();
|
|
111
|
+
*
|
|
55
112
|
* IMQLock.release('doSomething', res);
|
|
56
|
-
* return res;
|
|
57
|
-
* }
|
|
58
113
|
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
114
|
+
* return res;
|
|
115
|
+
* } catch (err) {
|
|
116
|
+
* // reject every waiter with the same error
|
|
61
117
|
* IMQLock.release('doSomething', null, err);
|
|
62
118
|
* throw err;
|
|
63
119
|
* }
|
|
@@ -66,13 +122,10 @@ export interface IMQLockMetadata {
|
|
|
66
122
|
* return lock;
|
|
67
123
|
* }
|
|
68
124
|
*
|
|
69
|
-
* (
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
* }
|
|
74
|
-
* })();
|
|
75
|
-
* ~~~
|
|
125
|
+
* for (let i = 0; i < 10; ++i) {
|
|
126
|
+
* doSomething().then(res => console.log(res));
|
|
127
|
+
* }
|
|
128
|
+
* ```
|
|
76
129
|
*/
|
|
77
130
|
export declare class IMQLock {
|
|
78
131
|
private static acquiredLocks;
|
|
@@ -80,40 +133,33 @@ export declare class IMQLock {
|
|
|
80
133
|
private static metadata;
|
|
81
134
|
/**
|
|
82
135
|
* Deadlock timeout in milliseconds
|
|
83
|
-
*
|
|
84
|
-
* @type {number}
|
|
85
136
|
*/
|
|
86
137
|
static deadlockTimeout: number;
|
|
87
138
|
/**
|
|
88
139
|
* Logger used to log errors that appear during locked calls
|
|
89
|
-
*
|
|
90
|
-
* @type {ILogger}
|
|
91
140
|
*/
|
|
92
141
|
static logger: ILogger;
|
|
93
142
|
/**
|
|
94
143
|
* Acquires a lock for a given key.
|
|
95
144
|
*
|
|
96
|
-
* @param
|
|
97
|
-
* @param
|
|
145
|
+
* @param key - key to acquire the lock for
|
|
146
|
+
* @param callback - callback invoked on
|
|
98
147
|
* lock resolution
|
|
99
|
-
* @param
|
|
100
|
-
* @returns {Promise<AcquiredLock<T>>}
|
|
148
|
+
* @param metadata - metadata for the locked call
|
|
101
149
|
*/
|
|
102
150
|
static acquire<T>(key: string, callback?: (...args: any[]) => any, metadata?: IMQLockMetadataItem): Promise<AcquiredLock<T>>;
|
|
103
151
|
/**
|
|
104
152
|
* Releases a previously acquired lock for a given key.
|
|
105
153
|
*
|
|
106
|
-
* @param
|
|
107
|
-
* @param
|
|
108
|
-
* @param
|
|
109
|
-
* @returns {void}
|
|
154
|
+
* @param key - key to release the lock for
|
|
155
|
+
* @param value - value to resolve pending calls with
|
|
156
|
+
* @param err - error to reject pending calls with
|
|
110
157
|
*/
|
|
111
158
|
static release<T, E>(key: string, value?: T, err?: E): void;
|
|
112
159
|
/**
|
|
113
160
|
* Returns true if the given key is locked, false otherwise.
|
|
114
161
|
*
|
|
115
|
-
* @param
|
|
116
|
-
* @returns {boolean}
|
|
162
|
+
* @param key - key to check the lock state for
|
|
117
163
|
*/
|
|
118
164
|
static locked(key: string): boolean;
|
|
119
165
|
}
|