@amqp-contract/client 0.21.0 → 0.23.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/dist/index.cjs CHANGED
@@ -1,6 +1,8 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let _amqp_contract_contract = require("@amqp-contract/contract");
2
3
  let _amqp_contract_core = require("@amqp-contract/core");
3
4
  let _swan_io_boxed = require("@swan-io/boxed");
5
+ let node_crypto = require("node:crypto");
4
6
  let node_zlib = require("node:zlib");
5
7
  let ts_pattern = require("ts-pattern");
6
8
  let node_util = require("node:util");
@@ -20,11 +22,69 @@ function compressBuffer(buffer, algorithm) {
20
22
  return (0, ts_pattern.match)(algorithm).with("gzip", () => _swan_io_boxed.Future.fromPromise(gzipAsync(buffer)).mapError((error) => new _amqp_contract_core.TechnicalError("Failed to compress with gzip", error))).with("deflate", () => _swan_io_boxed.Future.fromPromise(deflateAsync(buffer)).mapError((error) => new _amqp_contract_core.TechnicalError("Failed to compress with deflate", error))).exhaustive();
21
23
  }
22
24
  //#endregion
25
+ //#region src/errors.ts
26
+ /**
27
+ * Captured `Error.captureStackTrace` shim — only present on Node.js.
28
+ */
29
+ function captureStack(target, ctor) {
30
+ const ErrorConstructor = Error;
31
+ if (typeof ErrorConstructor.captureStackTrace === "function") ErrorConstructor.captureStackTrace(target, ctor);
32
+ }
33
+ /**
34
+ * Returned from `TypedAmqpClient.call()` when the configured `timeoutMs` elapses
35
+ * before the RPC server publishes a reply with the matching `correlationId`.
36
+ *
37
+ * The pending call is removed from the in-memory correlation map; if a reply
38
+ * arrives after the timeout it is dropped (and a debug log is emitted by the
39
+ * client if a logger is configured).
40
+ */
41
+ var RpcTimeoutError = class extends Error {
42
+ constructor(rpcName, timeoutMs) {
43
+ super(`RPC call to "${rpcName}" timed out after ${timeoutMs}ms with no reply received`);
44
+ this.rpcName = rpcName;
45
+ this.timeoutMs = timeoutMs;
46
+ this.name = "RpcTimeoutError";
47
+ captureStack(this, this.constructor);
48
+ }
49
+ };
50
+ /**
51
+ * Returned from any in-flight RPC call when the client is closed before the
52
+ * reply is received. The correlation map is cleared on close and every pending
53
+ * caller's promise resolves with `Result.Error(RpcCancelledError)`.
54
+ */
55
+ var RpcCancelledError = class extends Error {
56
+ constructor(rpcName) {
57
+ super(`RPC call to "${rpcName}" was cancelled because the client was closed`);
58
+ this.rpcName = rpcName;
59
+ this.name = "RpcCancelledError";
60
+ captureStack(this, this.constructor);
61
+ }
62
+ };
63
+ //#endregion
23
64
  //#region src/client.ts
24
65
  /**
66
+ * The RabbitMQ direct-reply-to pseudo-queue. Publishing with `replyTo` set to
67
+ * this value tells the server to deliver the response back to the consumer
68
+ * subscribed on this queue on the same channel — no real queue is created and
69
+ * no setup is required beyond consuming from it once with `noAck: true`.
70
+ *
71
+ * @see https://www.rabbitmq.com/docs/direct-reply-to
72
+ */
73
+ const DIRECT_REPLY_TO = "amq.rabbitmq.reply-to";
74
+ /**
25
75
  * Type-safe AMQP client for publishing messages
26
76
  */
27
77
  var TypedAmqpClient = class TypedAmqpClient {
78
+ /**
79
+ * In-flight RPC calls keyed by `correlationId`. Cleared when a reply is
80
+ * received, when the call times out, or when the client is closed.
81
+ */
82
+ pendingCalls = /* @__PURE__ */ new Map();
83
+ /**
84
+ * Consumer tag of the reply consumer subscribed on `amq.rabbitmq.reply-to`.
85
+ * Set when the contract has at least one entry in `rpcs`; undefined otherwise.
86
+ */
87
+ replyConsumerTag;
28
88
  constructor(contract, amqpClient, defaultPublishOptions, logger, telemetry = _amqp_contract_core.defaultTelemetryProvider) {
29
89
  this.contract = contract;
30
90
  this.amqpClient = amqpClient;
@@ -42,15 +102,82 @@ var TypedAmqpClient = class TypedAmqpClient {
42
102
  * Connections are automatically shared across clients with the same URLs and
43
103
  * connection options, following RabbitMQ best practices.
44
104
  */
45
- static create({ contract, urls, connectionOptions, defaultPublishOptions, logger, telemetry }) {
105
+ static create({ contract, urls, connectionOptions, defaultPublishOptions, logger, telemetry, connectTimeoutMs }) {
46
106
  const client = new TypedAmqpClient(contract, new _amqp_contract_core.AmqpClient(contract, {
47
107
  urls,
48
- connectionOptions
108
+ connectionOptions,
109
+ connectTimeoutMs
49
110
  }), {
50
111
  persistent: true,
51
112
  ...defaultPublishOptions
52
113
  }, logger, telemetry ?? _amqp_contract_core.defaultTelemetryProvider);
53
- return client.waitForConnectionReady().mapOk(() => client);
114
+ return client.waitForConnectionReady().flatMapOk(() => client.setupReplyConsumerIfNeeded()).flatMap((result) => result.match({
115
+ Ok: () => _swan_io_boxed.Future.value(_swan_io_boxed.Result.Ok(client)),
116
+ Error: (error) => client.close().tapError((closeError) => {
117
+ logger?.warn("Failed to close client after connection failure", { error: closeError });
118
+ }).map(() => _swan_io_boxed.Result.Error(error))
119
+ }));
120
+ }
121
+ /**
122
+ * If the contract has any RPC entry, subscribe to `amq.rabbitmq.reply-to`
123
+ * once. Replies for every in-flight call arrive on this single consumer and
124
+ * are demultiplexed by `correlationId`.
125
+ */
126
+ setupReplyConsumerIfNeeded() {
127
+ const rpcs = this.contract.rpcs ?? {};
128
+ if (Object.keys(rpcs).length === 0) return _swan_io_boxed.Future.value(_swan_io_boxed.Result.Ok(void 0));
129
+ return this.amqpClient.consume(DIRECT_REPLY_TO, (msg) => this.handleRpcReply(msg), { noAck: true }).tapOk((tag) => {
130
+ this.replyConsumerTag = tag;
131
+ }).mapOk(() => void 0);
132
+ }
133
+ /**
134
+ * Demultiplex an RPC reply by `correlationId`, validate the body against the
135
+ * call's response schema, and resolve the awaiting caller. Replies with no
136
+ * matching pending call (the call already timed out, was cancelled, or the
137
+ * correlationId is unknown) are logged at warn — a non-zero rate of these
138
+ * usually indicates a tuning problem (handler latency exceeds caller
139
+ * timeout). The `messaging.rpc.late_reply` counter lets dashboards alert on
140
+ * sustained drift without parsing logs.
141
+ */
142
+ handleRpcReply(msg) {
143
+ if (!msg) return;
144
+ const correlationId = msg.properties.correlationId;
145
+ if (typeof correlationId !== "string") {
146
+ this.logger?.warn("Received RPC reply without correlationId; dropping", { deliveryTag: msg.fields.deliveryTag });
147
+ (0, _amqp_contract_core.recordLateRpcReply)(this.telemetry, "missing-correlation-id");
148
+ return;
149
+ }
150
+ const pending = this.pendingCalls.get(correlationId);
151
+ if (!pending) {
152
+ this.logger?.warn("Received RPC reply for unknown correlationId (caller already timed out or cancelled)", { correlationId });
153
+ (0, _amqp_contract_core.recordLateRpcReply)(this.telemetry, "unknown-correlation-id");
154
+ return;
155
+ }
156
+ this.pendingCalls.delete(correlationId);
157
+ clearTimeout(pending.timer);
158
+ let parsed;
159
+ try {
160
+ parsed = JSON.parse(msg.content.toString());
161
+ } catch (error) {
162
+ pending.resolve(_swan_io_boxed.Result.Error(new _amqp_contract_core.TechnicalError(`Failed to parse RPC reply JSON for "${pending.rpcName}"`, error)));
163
+ return;
164
+ }
165
+ let rawValidation;
166
+ try {
167
+ rawValidation = pending.responseSchema["~standard"].validate(parsed);
168
+ } catch (error) {
169
+ pending.resolve(_swan_io_boxed.Result.Error(new _amqp_contract_core.TechnicalError(`RPC reply validation threw for "${pending.rpcName}"`, error)));
170
+ return;
171
+ }
172
+ (rawValidation instanceof Promise ? rawValidation : Promise.resolve(rawValidation)).then((validation) => {
173
+ if (validation.issues) {
174
+ pending.resolve(_swan_io_boxed.Result.Error(new _amqp_contract_core.MessageValidationError(pending.rpcName, validation.issues)));
175
+ return;
176
+ }
177
+ pending.resolve(_swan_io_boxed.Result.Ok(validation.value));
178
+ }, (error) => {
179
+ pending.resolve(_swan_io_boxed.Result.Error(new _amqp_contract_core.TechnicalError(`RPC reply validation threw for "${pending.rpcName}"`, error)));
180
+ });
54
181
  }
55
182
  /**
56
183
  * Publish a message using a defined publisher
@@ -118,10 +245,108 @@ var TypedAmqpClient = class TypedAmqpClient {
118
245
  });
119
246
  }
120
247
  /**
121
- * Close the channel and connection
248
+ * Invoke an RPC defined via `defineRpc` and await the typed response.
249
+ *
250
+ * The request payload is validated against the RPC's request schema, then
251
+ * published to the AMQP default exchange with the server's queue name as
252
+ * routing key, `replyTo` set to `amq.rabbitmq.reply-to`, and a fresh UUID
253
+ * `correlationId`. The returned Future resolves once a matching reply
254
+ * arrives and validates against the response schema, or once `timeoutMs`
255
+ * elapses (whichever comes first).
256
+ *
257
+ * @typeParam TName - An RPC name from `contract.rpcs`.
258
+ * @param rpcName - The RPC name from the contract.
259
+ * @param request - The request payload, validated against the request schema.
260
+ * @param options - Per-call options. `timeoutMs` is required.
261
+ *
262
+ * @returns `Result.Ok(response)` on a successful round-trip; `Result.Error`
263
+ * on validation, transport, timeout, or cancel.
264
+ *
265
+ * @example
266
+ * ```typescript
267
+ * const result = await client
268
+ * .call('calculate', { a: 1, b: 2 }, { timeoutMs: 5_000 })
269
+ * .toPromise();
270
+ * if (result.isOk()) console.log(result.value.sum); // 3
271
+ * ```
272
+ */
273
+ call(rpcName, request, options) {
274
+ const TIMEOUT_MAX_MS = 2147483647;
275
+ if (typeof options.timeoutMs !== "number" || !Number.isFinite(options.timeoutMs) || options.timeoutMs <= 0 || options.timeoutMs > TIMEOUT_MAX_MS) return _swan_io_boxed.Future.value(_swan_io_boxed.Result.Error(new _amqp_contract_core.TechnicalError(`Invalid timeoutMs for RPC call to "${String(rpcName)}": expected a finite positive number ≤ ${TIMEOUT_MAX_MS}, got ${String(options.timeoutMs)}`)));
276
+ const startTime = Date.now();
277
+ const rpc = this.contract.rpcs[rpcName];
278
+ const requestSchema = rpc.request.payload;
279
+ const responseSchema = rpc.response.payload;
280
+ const queueName = (0, _amqp_contract_contract.extractQueue)(rpc.queue).name;
281
+ const span = (0, _amqp_contract_core.startPublishSpan)(this.telemetry, "", queueName, { [_amqp_contract_core.MessagingSemanticConventions.AMQP_PUBLISHER_NAME]: String(rpcName) });
282
+ const correlationId = (0, node_crypto.randomUUID)();
283
+ const callFuture = _swan_io_boxed.Future.make((resolve) => {
284
+ const timer = setTimeout(() => {
285
+ if (!this.pendingCalls.get(correlationId)) return;
286
+ this.pendingCalls.delete(correlationId);
287
+ resolve(_swan_io_boxed.Result.Error(new RpcTimeoutError(String(rpcName), options.timeoutMs)));
288
+ }, options.timeoutMs);
289
+ this.pendingCalls.set(correlationId, {
290
+ rpcName: String(rpcName),
291
+ responseSchema,
292
+ resolve,
293
+ timer
294
+ });
295
+ });
296
+ const validateRequest = () => {
297
+ let rawValidation;
298
+ try {
299
+ rawValidation = requestSchema["~standard"].validate(request);
300
+ } catch (error) {
301
+ return _swan_io_boxed.Future.value(_swan_io_boxed.Result.Error(new _amqp_contract_core.TechnicalError("RPC request validation threw", error)));
302
+ }
303
+ const validationPromise = rawValidation instanceof Promise ? rawValidation : Promise.resolve(rawValidation);
304
+ return _swan_io_boxed.Future.fromPromise(validationPromise).mapError((error) => new _amqp_contract_core.TechnicalError("RPC request validation threw", error)).mapOkToResult((validation) => validation.issues ? _swan_io_boxed.Result.Error(new _amqp_contract_core.MessageValidationError(String(rpcName), validation.issues)) : _swan_io_boxed.Result.Ok(validation.value));
305
+ };
306
+ const publishRequest = (validatedRequest) => {
307
+ const { compression: _ignoredCompression, ...defaultsWithoutCompression } = this.defaultPublishOptions;
308
+ const publishOptions = {
309
+ ...defaultsWithoutCompression,
310
+ ...options.publishOptions,
311
+ replyTo: DIRECT_REPLY_TO,
312
+ correlationId,
313
+ contentType: "application/json"
314
+ };
315
+ return this.amqpClient.publish("", queueName, validatedRequest, publishOptions).mapOkToResult((published) => published ? _swan_io_boxed.Result.Ok(void 0) : _swan_io_boxed.Result.Error(new _amqp_contract_core.TechnicalError(`Failed to publish RPC request for "${String(rpcName)}": channel buffer full`)));
316
+ };
317
+ return validateRequest().flatMapOk((validated) => publishRequest(validated)).flatMap((preflight) => {
318
+ if (preflight.isError()) {
319
+ const pending = this.pendingCalls.get(correlationId);
320
+ if (pending) {
321
+ clearTimeout(pending.timer);
322
+ this.pendingCalls.delete(correlationId);
323
+ }
324
+ return _swan_io_boxed.Future.value(_swan_io_boxed.Result.Error(preflight.error));
325
+ }
326
+ return callFuture;
327
+ }).tapOk(() => {
328
+ const durationMs = Date.now() - startTime;
329
+ (0, _amqp_contract_core.endSpanSuccess)(span);
330
+ (0, _amqp_contract_core.recordPublishMetric)(this.telemetry, "", queueName, true, durationMs);
331
+ }).tapError((error) => {
332
+ const durationMs = Date.now() - startTime;
333
+ (0, _amqp_contract_core.endSpanError)(span, error);
334
+ (0, _amqp_contract_core.recordPublishMetric)(this.telemetry, "", queueName, false, durationMs);
335
+ });
336
+ }
337
+ /**
338
+ * Close the channel and connection. Cancels the reply consumer (if any) and
339
+ * rejects every in-flight RPC call with `RpcCancelledError`.
122
340
  */
123
341
  close() {
124
- return this.amqpClient.close().mapOk(() => void 0);
342
+ for (const [, pending] of this.pendingCalls) {
343
+ clearTimeout(pending.timer);
344
+ pending.resolve(_swan_io_boxed.Result.Error(new RpcCancelledError(pending.rpcName)));
345
+ }
346
+ this.pendingCalls.clear();
347
+ return (this.replyConsumerTag ? this.amqpClient.cancel(this.replyConsumerTag).tapError((error) => {
348
+ this.logger?.warn("Failed to cancel RPC reply consumer during close", { error });
349
+ }) : _swan_io_boxed.Future.value(_swan_io_boxed.Result.Ok(void 0))).flatMap(() => this.amqpClient.close()).mapOk(() => void 0);
125
350
  }
126
351
  waitForConnectionReady() {
127
352
  return this.amqpClient.waitForConnect();
@@ -134,4 +359,6 @@ Object.defineProperty(exports, "MessageValidationError", {
134
359
  return _amqp_contract_core.MessageValidationError;
135
360
  }
136
361
  });
362
+ exports.RpcCancelledError = RpcCancelledError;
363
+ exports.RpcTimeoutError = RpcTimeoutError;
137
364
  exports.TypedAmqpClient = TypedAmqpClient;
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { CompressionAlgorithm, ContractDefinition, InferPublisherNames, PublisherEntry } from "@amqp-contract/contract";
1
+ import { CompressionAlgorithm, ContractDefinition, InferPublisherNames, InferRpcNames, MessageDefinition, PublisherEntry, RpcDefinition } from "@amqp-contract/contract";
2
2
  import { Logger, MessageValidationError, PublishOptions as PublishOptions$1, TechnicalError, TelemetryProvider } from "@amqp-contract/core";
3
3
  import { Future, Result } from "@swan-io/boxed";
4
4
  import * as amqp from "amqplib";
@@ -47,11 +47,39 @@ interface AmqpConnectionManagerOptions {
47
47
  connectionOptions?: AmqpConnectionOptions;
48
48
  }
49
49
  //#endregion
50
+ //#region src/errors.d.ts
51
+ /**
52
+ * Returned from `TypedAmqpClient.call()` when the configured `timeoutMs` elapses
53
+ * before the RPC server publishes a reply with the matching `correlationId`.
54
+ *
55
+ * The pending call is removed from the in-memory correlation map; if a reply
56
+ * arrives after the timeout it is dropped (and a debug log is emitted by the
57
+ * client if a logger is configured).
58
+ */
59
+ declare class RpcTimeoutError extends Error {
60
+ readonly rpcName: string;
61
+ readonly timeoutMs: number;
62
+ constructor(rpcName: string, timeoutMs: number);
63
+ }
64
+ /**
65
+ * Returned from any in-flight RPC call when the client is closed before the
66
+ * reply is received. The correlation map is cleared on close and every pending
67
+ * caller's promise resolves with `Result.Error(RpcCancelledError)`.
68
+ */
69
+ declare class RpcCancelledError extends Error {
70
+ readonly rpcName: string;
71
+ constructor(rpcName: string);
72
+ }
73
+ //#endregion
50
74
  //#region src/types.d.ts
51
75
  /**
52
- * Infer the TypeScript type from a schema
76
+ * Infer the TypeScript type from a schema (input side, used for publish payloads).
53
77
  */
54
78
  type InferSchemaInput<TSchema extends StandardSchemaV1> = TSchema extends StandardSchemaV1<infer TInput> ? TInput : never;
79
+ /**
80
+ * Infer the TypeScript type from a schema (output side, used for validated responses).
81
+ */
82
+ type InferSchemaOutput<TSchema extends StandardSchemaV1> = TSchema extends StandardSchemaV1<infer _TInput, infer TOutput> ? TOutput : never;
55
83
  /**
56
84
  * Infer publisher message input type.
57
85
  * Works with both PublisherDefinition and EventPublisherConfig since both have
@@ -62,18 +90,22 @@ type PublisherInferInput<TPublisher extends PublisherEntry> = TPublisher extends
62
90
  payload: StandardSchemaV1;
63
91
  };
64
92
  } ? InferSchemaInput<TPublisher["message"]["payload"]> : never;
93
+ type InferPublishers<TContract extends ContractDefinition> = NonNullable<TContract["publishers"]>;
94
+ type InferPublisher<TContract extends ContractDefinition, TName extends InferPublisherNames<TContract>> = InferPublishers<TContract>[TName];
65
95
  /**
66
- * Infer all publishers from contract
96
+ * Input type accepted by `client.publish(name, ...)` for a specific publisher.
67
97
  */
68
- type InferPublishers<TContract extends ContractDefinition> = NonNullable<TContract["publishers"]>;
98
+ type ClientInferPublisherInput<TContract extends ContractDefinition, TName extends InferPublisherNames<TContract>> = PublisherInferInput<InferPublisher<TContract, TName>>;
99
+ type InferRpcs<TContract extends ContractDefinition> = NonNullable<TContract["rpcs"]>;
100
+ type InferRpc<TContract extends ContractDefinition, TName extends InferRpcNames<TContract>> = InferRpcs<TContract>[TName];
69
101
  /**
70
- * Get specific publisher definition from contract
102
+ * Input type accepted by `client.call(name, request, ...)`.
71
103
  */
72
- type InferPublisher<TContract extends ContractDefinition, TName extends InferPublisherNames<TContract>> = InferPublishers<TContract>[TName];
104
+ type ClientInferRpcRequestInput<TContract extends ContractDefinition, TName extends InferRpcNames<TContract>> = InferRpc<TContract, TName> extends RpcDefinition<infer TRequest, MessageDefinition> ? TRequest extends MessageDefinition ? InferSchemaInput<TRequest["payload"]> : never : never;
73
105
  /**
74
- * Infer publisher input type (message payload) for a specific publisher in a contract
106
+ * Output (validated) response type returned by `client.call(name, ...)`.
75
107
  */
76
- type ClientInferPublisherInput<TContract extends ContractDefinition, TName extends InferPublisherNames<TContract>> = PublisherInferInput<InferPublisher<TContract, TName>>;
108
+ type ClientInferRpcResponseOutput<TContract extends ContractDefinition, TName extends InferRpcNames<TContract>> = InferRpc<TContract, TName> extends RpcDefinition<MessageDefinition, infer TResponse> ? TResponse extends MessageDefinition ? InferSchemaOutput<TResponse["payload"]> : never : never;
77
109
  //#endregion
78
110
  //#region src/client.d.ts
79
111
  /**
@@ -107,6 +139,31 @@ type CreateClientOptions<TContract extends ContractDefinition> = {
107
139
  * By default, persistent is set to true for message durability.
108
140
  */
109
141
  defaultPublishOptions?: PublishOptions | undefined;
142
+ /**
143
+ * Maximum time in ms to wait for the AMQP connection to become ready before
144
+ * `create()` resolves to `Result.Error<TechnicalError>`. Defaults to 30s
145
+ * (the {@link AmqpClient}'s `DEFAULT_CONNECT_TIMEOUT_MS`). Pass `null` to
146
+ * disable the timeout and let amqp-connection-manager retry indefinitely.
147
+ */
148
+ connectTimeoutMs?: number | null | undefined;
149
+ };
150
+ /**
151
+ * Per-call options for `client.call()`.
152
+ */
153
+ type CallOptions = {
154
+ /**
155
+ * Maximum time in ms to wait for an RPC reply. If exceeded, the call resolves
156
+ * to `Result.Error<RpcTimeoutError>` and the in-memory correlation entry is
157
+ * cleared. A late reply arriving after the timeout is silently dropped.
158
+ *
159
+ * Required: RPC without a timeout is a footgun.
160
+ */
161
+ timeoutMs: number;
162
+ /**
163
+ * Optional AMQP message properties to merge into the request. `replyTo` and
164
+ * `correlationId` are managed by the client and cannot be overridden.
165
+ */
166
+ publishOptions?: Omit<PublishOptions$1, "replyTo" | "correlationId">;
110
167
  };
111
168
  /**
112
169
  * Type-safe AMQP client for publishing messages
@@ -117,6 +174,16 @@ declare class TypedAmqpClient<TContract extends ContractDefinition> {
117
174
  private readonly defaultPublishOptions;
118
175
  private readonly logger?;
119
176
  private readonly telemetry;
177
+ /**
178
+ * In-flight RPC calls keyed by `correlationId`. Cleared when a reply is
179
+ * received, when the call times out, or when the client is closed.
180
+ */
181
+ private readonly pendingCalls;
182
+ /**
183
+ * Consumer tag of the reply consumer subscribed on `amq.rabbitmq.reply-to`.
184
+ * Set when the contract has at least one entry in `rpcs`; undefined otherwise.
185
+ */
186
+ private replyConsumerTag?;
120
187
  private constructor();
121
188
  /**
122
189
  * Create a type-safe AMQP client from a contract.
@@ -134,8 +201,25 @@ declare class TypedAmqpClient<TContract extends ContractDefinition> {
134
201
  connectionOptions,
135
202
  defaultPublishOptions,
136
203
  logger,
137
- telemetry
204
+ telemetry,
205
+ connectTimeoutMs
138
206
  }: CreateClientOptions<TContract>): Future<Result<TypedAmqpClient<TContract>, TechnicalError>>;
207
+ /**
208
+ * If the contract has any RPC entry, subscribe to `amq.rabbitmq.reply-to`
209
+ * once. Replies for every in-flight call arrive on this single consumer and
210
+ * are demultiplexed by `correlationId`.
211
+ */
212
+ private setupReplyConsumerIfNeeded;
213
+ /**
214
+ * Demultiplex an RPC reply by `correlationId`, validate the body against the
215
+ * call's response schema, and resolve the awaiting caller. Replies with no
216
+ * matching pending call (the call already timed out, was cancelled, or the
217
+ * correlationId is unknown) are logged at warn — a non-zero rate of these
218
+ * usually indicates a tuning problem (handler latency exceeds caller
219
+ * timeout). The `messaging.rpc.late_reply` counter lets dashboards alert on
220
+ * sustained drift without parsing logs.
221
+ */
222
+ private handleRpcReply;
139
223
  /**
140
224
  * Publish a message using a defined publisher
141
225
  *
@@ -156,11 +240,39 @@ declare class TypedAmqpClient<TContract extends ContractDefinition> {
156
240
  */
157
241
  publish<TName extends InferPublisherNames<TContract>>(publisherName: TName, message: ClientInferPublisherInput<TContract, TName>, options?: PublishOptions): Future<Result<void, TechnicalError | MessageValidationError>>;
158
242
  /**
159
- * Close the channel and connection
243
+ * Invoke an RPC defined via `defineRpc` and await the typed response.
244
+ *
245
+ * The request payload is validated against the RPC's request schema, then
246
+ * published to the AMQP default exchange with the server's queue name as
247
+ * routing key, `replyTo` set to `amq.rabbitmq.reply-to`, and a fresh UUID
248
+ * `correlationId`. The returned Future resolves once a matching reply
249
+ * arrives and validates against the response schema, or once `timeoutMs`
250
+ * elapses (whichever comes first).
251
+ *
252
+ * @typeParam TName - An RPC name from `contract.rpcs`.
253
+ * @param rpcName - The RPC name from the contract.
254
+ * @param request - The request payload, validated against the request schema.
255
+ * @param options - Per-call options. `timeoutMs` is required.
256
+ *
257
+ * @returns `Result.Ok(response)` on a successful round-trip; `Result.Error`
258
+ * on validation, transport, timeout, or cancel.
259
+ *
260
+ * @example
261
+ * ```typescript
262
+ * const result = await client
263
+ * .call('calculate', { a: 1, b: 2 }, { timeoutMs: 5_000 })
264
+ * .toPromise();
265
+ * if (result.isOk()) console.log(result.value.sum); // 3
266
+ * ```
267
+ */
268
+ call<TName extends InferRpcNames<TContract>>(rpcName: TName, request: ClientInferRpcRequestInput<TContract, TName>, options: CallOptions): Future<Result<ClientInferRpcResponseOutput<TContract, TName>, TechnicalError | MessageValidationError | RpcTimeoutError | RpcCancelledError>>;
269
+ /**
270
+ * Close the channel and connection. Cancels the reply consumer (if any) and
271
+ * rejects every in-flight RPC call with `RpcCancelledError`.
160
272
  */
161
273
  close(): Future<Result<void, TechnicalError>>;
162
274
  private waitForConnectionReady;
163
275
  }
164
276
  //#endregion
165
- export { type ClientInferPublisherInput, type CreateClientOptions, MessageValidationError, type PublishOptions, TypedAmqpClient };
277
+ export { type CallOptions, type ClientInferPublisherInput, type ClientInferRpcRequestInput, type ClientInferRpcResponseOutput, type CreateClientOptions, MessageValidationError, type PublishOptions, RpcCancelledError, RpcTimeoutError, TypedAmqpClient };
166
278
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","names":["amqp","EventEmitter","TcpSocketConnectOpts","ConnectionOptions","ChannelWrapper","CreateChannelOpts","ConnectionUrl","Options","Connect","AmqpConnectionOptions","url","connectionOptions","ConnectListener","Connection","connection","arg","ConnectFailedListener","Error","err","Buffer","noDelay","timeout","keepAlive","keepAliveDelay","clientProperties","credentials","mechanism","username","password","response","AmqpConnectionManagerOptions","Promise","heartbeatIntervalInSeconds","reconnectTimeInSeconds","findServers","urls","callback","IAmqpConnectionManager","Function","ChannelModel","addListener","event","args","listener","reason","listeners","eventName","on","once","prependListener","prependOnceListener","removeListener","connect","options","reconnect","createChannel","close","isConnected","channelCount","AmqpConnectionManager","_channels","_currentUrl","_closed","_cancelRetriesHandler","_connectPromise","_currentConnection","_findServers","_urls","constructor","_connect","default"],"sources":["../../../node_modules/.pnpm/amqp-connection-manager@5.0.0_amqplib@1.0.3/node_modules/amqp-connection-manager/dist/types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gBAAA;EFtBJ;;;;;EE4BvB,WAAA,GAAc,oBAAA;AAAA;;;;KAMJ,mBAAA,mBAAsC,kBAAA;EAChD,QAAA,EAAU,SAAA;EACV,IAAA,EAAM,aAAA;EACN,iBAAA,GAAoB,4BAAA;EACpB,MAAA,GAAS,MAAA;EFtB8CP;;;;;EE4BvD,SAAA,GAAY,iBAAA;EF5B2CA;;;;;EEkCvD,qBAAA,GAAwB,cAAA;AAAA;;;;cAMb,eAAA,mBAAkC,kBAAA;EAAA,iBAE1B,QAAA;EAAA,iBACA,UAAA;EAAA,iBACA,qBAAA;EAAA,iBACA,MAAA;EAAA,iBACA,SAAA;EAAA,QALZ,WAAA,CAAA;EFzBQ4B;;;;;;;;;;EAAAA,OE2CR,MAAA,mBAAyB,kBAAA,CAAA,CAAA;IAC9B,QAAA;IACA,IAAA;IACA,iBAAA;IACA,qBAAA;IACA,MAAA;IACA;EAAA,GACC,mBAAA,CAAoB,SAAA,IAAa,MAAA,CAAO,MAAA,CAAO,eAAA,CAAgB,SAAA,GAAY,cAAA;EFhD5EE;;;;;;;;;;;;;;;;;AClC0D;ECgH5D,OAAA,eAAsB,mBAAA,CAAoB,SAAA,EAAA,CACxC,aAAA,EAAe,KAAA,EACf,OAAA,EAAS,yBAAA,CAA0B,SAAA,EAAW,KAAA,GAC9C,OAAA,GAAU,cAAA,GACT,MAAA,CAAO,MAAA,OAAa,cAAA,GAAiB,sBAAA;ED/GrB;;;EC4MnB,KAAA,CAAA,GAAS,MAAA,CAAO,MAAA,OAAa,cAAA;EAAA,QAIrB,sBAAA;AAAA"}
1
+ {"version":3,"file":"index.d.cts","names":["amqp","EventEmitter","TcpSocketConnectOpts","ConnectionOptions","ChannelWrapper","CreateChannelOpts","ConnectionUrl","Options","Connect","AmqpConnectionOptions","url","connectionOptions","ConnectListener","Connection","connection","arg","ConnectFailedListener","Error","err","Buffer","noDelay","timeout","keepAlive","keepAliveDelay","clientProperties","credentials","mechanism","username","password","response","AmqpConnectionManagerOptions","Promise","heartbeatIntervalInSeconds","reconnectTimeInSeconds","findServers","urls","callback","IAmqpConnectionManager","Function","ChannelModel","addListener","event","args","listener","reason","listeners","eventName","on","once","prependListener","prependOnceListener","removeListener","connect","options","reconnect","createChannel","close","isConnected","channelCount","AmqpConnectionManager","_channels","_currentUrl","_closed","_cancelRetriesHandler","_connectPromise","_currentConnection","_findServers","_urls","constructor","_connect","default"],"sources":["../../../node_modules/.pnpm/amqp-connection-manager@5.0.0_amqplib@1.0.3/node_modules/amqp-connection-manager/dist/types/AmqpConnectionManager.d.ts","../src/errors.ts","../src/types.ts","../src/client.ts"],"x_google_ignoreList":[0],"mappings":";;;;;;;;;KAKYM,aAAAA,YAAyBN,IAAAA,CAAKO,OAAAA,CAAQC,OAAAA;EAC9CE,GAAAA;EACAC,iBAAAA,GAAoBF,qBAAAA;AAAAA;AAAAA,KAcZA,qBAAAA,IAAyBN,iBAAAA,GAAoBD,oBAAAA;EACrDkB,OAAAA;EACAC,OAAAA;EACAC,SAAAA;EACAC,cAAAA;EACAC,gBAAAA;EACAC,WAAAA;IACIC,SAAAA;IACAC,QAAAA;IACAC,QAAAA;IACAC,QAAAA,QAAgBV,MAAAA;EAAAA;IAEhBO,SAAAA;IACAG,QAAAA,QAAgBV,MAAAA;EAAAA;AAAAA;AAAAA,UAGPW,4BAAAA;EATTJ;EAWJM,0BAAAA;EATIJ;;;;EAcJK,sBAAAA;EAVoBd;;;AAGxB;;;;EAeIe,WAAAA,KAAgBE,QAAAA,GAAWD,IAAAA,EAAM7B,aAAAA,GAAgBA,aAAAA,+BAA4CyB,OAAAA,CAAQzB,aAAAA,GAAgBA,aAAAA;EAAhBA;EAErGK,iBAAAA,GAAoBF,qBAAAA;AAAAA;;;;;;;;;;;cChCX,eAAA,SAAwB,KAAA;EAAA,SAEjB,OAAA;EAAA,SACA,SAAA;cADA,OAAA,UACA,SAAA;AAAA;;;;;;cAaP,iBAAA,SAA0B,KAAA;EAAA,SACT,OAAA;cAAA,OAAA;A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