@platformatic/itc 3.63.0-alpha.3 → 3.64.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/lib/index.d.ts CHANGED
@@ -11,6 +11,10 @@ export interface ITCConstructorOptions {
11
11
  throwOnMissingHandler?: boolean
12
12
  }
13
13
 
14
+ export interface ITCSendOptions extends Record<string, any> {
15
+ signal?: AbortSignal
16
+ }
17
+
14
18
  export interface OutgoingMessagingSpanOptions {
15
19
  telemetryContext?: Context
16
20
  telemetryMetadata?: Record<string, string>
@@ -30,7 +34,7 @@ export interface OutgoingMessagingSpan {
30
34
  export class ITC extends EventEmitter {
31
35
  constructor (options: ITCConstructorOptions)
32
36
 
33
- send (name: string, message: any, options?: Record<string, any>): Promise<any>
37
+ send (name: string, message: any, options?: ITCSendOptions): Promise<any>
34
38
  notify (name: string, message: any, options?: Record<string, any>): void
35
39
  process (name: string, message: any, context?: Record<string, any>): Promise<any>
36
40
  handle (message: string, handler: Handler): void
package/lib/index.js CHANGED
@@ -307,24 +307,53 @@ export class ITC extends EventEmitter {
307
307
  }
308
308
 
309
309
  let reqId
310
+ let abortHandler
311
+ const signal = options?.signal
312
+
310
313
  try {
311
314
  this._enableKeepAlive()
315
+ signal?.throwIfAborted()
312
316
 
313
317
  const request = generateRequest(name, message, options?.meta)
314
- this._send(request, options)
315
-
316
318
  const promiseWithResolvers = Promise.withResolvers()
317
319
  reqId = request.reqId
318
320
  this.#waitingRequests.set(request.reqId, promiseWithResolvers)
319
321
 
320
- const { error, data } = await Unpromise.race([promiseWithResolvers.promise, this.#closePromise])
322
+ const responses = [promiseWithResolvers.promise, this.#closePromise]
323
+ if (signal) {
324
+ const aborted = Promise.withResolvers()
325
+ abortHandler = () => {
326
+ const error = signal.reason ?? new DOMException('This operation was aborted', 'AbortError')
327
+ aborted.resolve({ error, data: null })
328
+ }
329
+
330
+ if (signal.aborted) {
331
+ abortHandler()
332
+ } else {
333
+ signal.addEventListener('abort', abortHandler, { once: true })
334
+ }
335
+
336
+ responses.push(aborted.promise)
337
+ }
338
+
339
+ this._send(request, options)
340
+
341
+ const { error, data } = await Unpromise.race(responses)
321
342
 
322
343
  if (error !== null) throw error
323
344
  return data
324
345
  } finally {
346
+ if (abortHandler) {
347
+ signal.removeEventListener('abort', abortHandler)
348
+ }
349
+
325
350
  // Clean up the waiting requests map even if an error occurred
326
351
  this.#waitingRequests.delete(reqId)
327
352
  this._manageKeepAlive()
353
+
354
+ if (reqId) {
355
+ this._onRequestSettled?.(reqId, options)
356
+ }
328
357
  }
329
358
  }
330
359
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/itc",
3
- "version": "3.63.0-alpha.3",
3
+ "version": "3.64.0",
4
4
  "description": "Platformatic ITC",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",
@@ -27,7 +27,7 @@
27
27
  "@fastify/error": "^4.0.0",
28
28
  "@opentelemetry/api": "^1.9.0",
29
29
  "@watchable/unpromise": "^1.0.2",
30
- "@platformatic/globals": "3.63.0-alpha.3"
30
+ "@platformatic/globals": "3.64.0"
31
31
  },
32
32
  "engines": {
33
33
  "node": ">=22.19.0"