@amqp-contract/client 0.21.0 → 0.22.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 +227 -5
- package/dist/index.d.cts +120 -11
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +120 -11
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +226 -6
- package/dist/index.mjs.map +1 -1
- package/docs/index.md +441 -17
- package/package.json +5 -5
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { CompressionAlgorithm, ContractDefinition, InferPublisherNames, InferRpcNames, MessageDefinition, PublisherEntry, RpcDefinition } from "@amqp-contract/contract";
|
|
1
2
|
import { Logger, MessageValidationError, PublishOptions as PublishOptions$1, TechnicalError, TelemetryProvider } from "@amqp-contract/core";
|
|
2
3
|
import { Future, Result } from "@swan-io/boxed";
|
|
3
|
-
import { CompressionAlgorithm, ContractDefinition, InferPublisherNames, PublisherEntry } from "@amqp-contract/contract";
|
|
4
4
|
import * as amqp from "amqplib";
|
|
5
5
|
import { TcpSocketConnectOpts } from "net";
|
|
6
6
|
import { ConnectionOptions } from "tls";
|
|
@@ -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
|
-
*
|
|
96
|
+
* Input type accepted by `client.publish(name, ...)` for a specific publisher.
|
|
67
97
|
*/
|
|
68
|
-
type
|
|
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
|
-
*
|
|
102
|
+
* Input type accepted by `client.call(name, request, ...)`.
|
|
71
103
|
*/
|
|
72
|
-
type
|
|
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
|
-
*
|
|
106
|
+
* Output (validated) response type returned by `client.call(name, ...)`.
|
|
75
107
|
*/
|
|
76
|
-
type
|
|
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>`. Without this option,
|
|
145
|
+
* `create()` waits forever — the underlying amqp-connection-manager retries
|
|
146
|
+
* indefinitely.
|
|
147
|
+
*/
|
|
148
|
+
connectTimeoutMs?: number | 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,22 @@ 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 (e.g. arriving after the call timed out) are dropped
|
|
217
|
+
* with a debug log.
|
|
218
|
+
*/
|
|
219
|
+
private handleRpcReply;
|
|
139
220
|
/**
|
|
140
221
|
* Publish a message using a defined publisher
|
|
141
222
|
*
|
|
@@ -156,11 +237,39 @@ declare class TypedAmqpClient<TContract extends ContractDefinition> {
|
|
|
156
237
|
*/
|
|
157
238
|
publish<TName extends InferPublisherNames<TContract>>(publisherName: TName, message: ClientInferPublisherInput<TContract, TName>, options?: PublishOptions): Future<Result<void, TechnicalError | MessageValidationError>>;
|
|
158
239
|
/**
|
|
159
|
-
*
|
|
240
|
+
* Invoke an RPC defined via `defineRpc` and await the typed response.
|
|
241
|
+
*
|
|
242
|
+
* The request payload is validated against the RPC's request schema, then
|
|
243
|
+
* published to the AMQP default exchange with the server's queue name as
|
|
244
|
+
* routing key, `replyTo` set to `amq.rabbitmq.reply-to`, and a fresh UUID
|
|
245
|
+
* `correlationId`. The returned Future resolves once a matching reply
|
|
246
|
+
* arrives and validates against the response schema, or once `timeoutMs`
|
|
247
|
+
* elapses (whichever comes first).
|
|
248
|
+
*
|
|
249
|
+
* @typeParam TName - An RPC name from `contract.rpcs`.
|
|
250
|
+
* @param rpcName - The RPC name from the contract.
|
|
251
|
+
* @param request - The request payload, validated against the request schema.
|
|
252
|
+
* @param options - Per-call options. `timeoutMs` is required.
|
|
253
|
+
*
|
|
254
|
+
* @returns `Result.Ok(response)` on a successful round-trip; `Result.Error`
|
|
255
|
+
* on validation, transport, timeout, or cancel.
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```typescript
|
|
259
|
+
* const result = await client
|
|
260
|
+
* .call('calculate', { a: 1, b: 2 }, { timeoutMs: 5_000 })
|
|
261
|
+
* .toPromise();
|
|
262
|
+
* if (result.isOk()) console.log(result.value.sum); // 3
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
call<TName extends InferRpcNames<TContract>>(rpcName: TName, request: ClientInferRpcRequestInput<TContract, TName>, options: CallOptions): Future<Result<ClientInferRpcResponseOutput<TContract, TName>, TechnicalError | MessageValidationError | RpcTimeoutError | RpcCancelledError>>;
|
|
266
|
+
/**
|
|
267
|
+
* Close the channel and connection. Cancels the reply consumer (if any) and
|
|
268
|
+
* rejects every in-flight RPC call with `RpcCancelledError`.
|
|
160
269
|
*/
|
|
161
270
|
close(): Future<Result<void, TechnicalError>>;
|
|
162
271
|
private waitForConnectionReady;
|
|
163
272
|
}
|
|
164
273
|
//#endregion
|
|
165
|
-
export { type ClientInferPublisherInput, type CreateClientOptions, MessageValidationError, type PublishOptions, TypedAmqpClient };
|
|
274
|
+
export { type CallOptions, type ClientInferPublisherInput, type ClientInferRpcRequestInput, type ClientInferRpcResponseOutput, type CreateClientOptions, MessageValidationError, type PublishOptions, RpcCancelledError, RpcTimeoutError, TypedAmqpClient };
|
|
166
275
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","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/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;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.mts","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,OA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GAC/C,OAAA,EAAS,WAAA,GACR,MAAA,CACD,MAAA,CACE,4BAAA,CAA6B,SAAA,EAAW,KAAA,GACxC,cAAA,GAAiB,sBAAA,GAAyB,eAAA,GAAkB,iBAAA;ED5YnC;AAAA;;;EC6hB7B,KAAA,CAAA,GAAS,MAAA,CAAO,MAAA,OAAa,cAAA;EAAA,QAiBrB,sBAAA;AAAA"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { extractQueue } from "@amqp-contract/contract";
|
|
1
2
|
import { AmqpClient, MessageValidationError, MessagingSemanticConventions, TechnicalError, defaultTelemetryProvider, endSpanError, endSpanSuccess, recordPublishMetric, startPublishSpan } from "@amqp-contract/core";
|
|
2
3
|
import { Future, Result } from "@swan-io/boxed";
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
3
5
|
import { deflate, gzip } from "node:zlib";
|
|
4
6
|
import { match } from "ts-pattern";
|
|
5
7
|
import { promisify } from "node:util";
|
|
@@ -19,11 +21,69 @@ function compressBuffer(buffer, algorithm) {
|
|
|
19
21
|
return match(algorithm).with("gzip", () => Future.fromPromise(gzipAsync(buffer)).mapError((error) => new TechnicalError("Failed to compress with gzip", error))).with("deflate", () => Future.fromPromise(deflateAsync(buffer)).mapError((error) => new TechnicalError("Failed to compress with deflate", error))).exhaustive();
|
|
20
22
|
}
|
|
21
23
|
//#endregion
|
|
24
|
+
//#region src/errors.ts
|
|
25
|
+
/**
|
|
26
|
+
* Captured `Error.captureStackTrace` shim — only present on Node.js.
|
|
27
|
+
*/
|
|
28
|
+
function captureStack(target, ctor) {
|
|
29
|
+
const ErrorConstructor = Error;
|
|
30
|
+
if (typeof ErrorConstructor.captureStackTrace === "function") ErrorConstructor.captureStackTrace(target, ctor);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Returned from `TypedAmqpClient.call()` when the configured `timeoutMs` elapses
|
|
34
|
+
* before the RPC server publishes a reply with the matching `correlationId`.
|
|
35
|
+
*
|
|
36
|
+
* The pending call is removed from the in-memory correlation map; if a reply
|
|
37
|
+
* arrives after the timeout it is dropped (and a debug log is emitted by the
|
|
38
|
+
* client if a logger is configured).
|
|
39
|
+
*/
|
|
40
|
+
var RpcTimeoutError = class extends Error {
|
|
41
|
+
constructor(rpcName, timeoutMs) {
|
|
42
|
+
super(`RPC call to "${rpcName}" timed out after ${timeoutMs}ms with no reply received`);
|
|
43
|
+
this.rpcName = rpcName;
|
|
44
|
+
this.timeoutMs = timeoutMs;
|
|
45
|
+
this.name = "RpcTimeoutError";
|
|
46
|
+
captureStack(this, this.constructor);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Returned from any in-flight RPC call when the client is closed before the
|
|
51
|
+
* reply is received. The correlation map is cleared on close and every pending
|
|
52
|
+
* caller's promise resolves with `Result.Error(RpcCancelledError)`.
|
|
53
|
+
*/
|
|
54
|
+
var RpcCancelledError = class extends Error {
|
|
55
|
+
constructor(rpcName) {
|
|
56
|
+
super(`RPC call to "${rpcName}" was cancelled because the client was closed`);
|
|
57
|
+
this.rpcName = rpcName;
|
|
58
|
+
this.name = "RpcCancelledError";
|
|
59
|
+
captureStack(this, this.constructor);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
//#endregion
|
|
22
63
|
//#region src/client.ts
|
|
23
64
|
/**
|
|
65
|
+
* The RabbitMQ direct-reply-to pseudo-queue. Publishing with `replyTo` set to
|
|
66
|
+
* this value tells the server to deliver the response back to the consumer
|
|
67
|
+
* subscribed on this queue on the same channel — no real queue is created and
|
|
68
|
+
* no setup is required beyond consuming from it once with `noAck: true`.
|
|
69
|
+
*
|
|
70
|
+
* @see https://www.rabbitmq.com/docs/direct-reply-to
|
|
71
|
+
*/
|
|
72
|
+
const DIRECT_REPLY_TO = "amq.rabbitmq.reply-to";
|
|
73
|
+
/**
|
|
24
74
|
* Type-safe AMQP client for publishing messages
|
|
25
75
|
*/
|
|
26
76
|
var TypedAmqpClient = class TypedAmqpClient {
|
|
77
|
+
/**
|
|
78
|
+
* In-flight RPC calls keyed by `correlationId`. Cleared when a reply is
|
|
79
|
+
* received, when the call times out, or when the client is closed.
|
|
80
|
+
*/
|
|
81
|
+
pendingCalls = /* @__PURE__ */ new Map();
|
|
82
|
+
/**
|
|
83
|
+
* Consumer tag of the reply consumer subscribed on `amq.rabbitmq.reply-to`.
|
|
84
|
+
* Set when the contract has at least one entry in `rpcs`; undefined otherwise.
|
|
85
|
+
*/
|
|
86
|
+
replyConsumerTag;
|
|
27
87
|
constructor(contract, amqpClient, defaultPublishOptions, logger, telemetry = defaultTelemetryProvider) {
|
|
28
88
|
this.contract = contract;
|
|
29
89
|
this.amqpClient = amqpClient;
|
|
@@ -41,15 +101,77 @@ var TypedAmqpClient = class TypedAmqpClient {
|
|
|
41
101
|
* Connections are automatically shared across clients with the same URLs and
|
|
42
102
|
* connection options, following RabbitMQ best practices.
|
|
43
103
|
*/
|
|
44
|
-
static create({ contract, urls, connectionOptions, defaultPublishOptions, logger, telemetry }) {
|
|
104
|
+
static create({ contract, urls, connectionOptions, defaultPublishOptions, logger, telemetry, connectTimeoutMs }) {
|
|
45
105
|
const client = new TypedAmqpClient(contract, new AmqpClient(contract, {
|
|
46
106
|
urls,
|
|
47
|
-
connectionOptions
|
|
107
|
+
connectionOptions,
|
|
108
|
+
connectTimeoutMs
|
|
48
109
|
}), {
|
|
49
110
|
persistent: true,
|
|
50
111
|
...defaultPublishOptions
|
|
51
112
|
}, logger, telemetry ?? defaultTelemetryProvider);
|
|
52
|
-
return client.waitForConnectionReady().
|
|
113
|
+
return client.waitForConnectionReady().flatMapOk(() => client.setupReplyConsumerIfNeeded()).flatMap((result) => result.match({
|
|
114
|
+
Ok: () => Future.value(Result.Ok(client)),
|
|
115
|
+
Error: (error) => client.close().tapError((closeError) => {
|
|
116
|
+
logger?.warn("Failed to close client after connection failure", { error: closeError });
|
|
117
|
+
}).map(() => Result.Error(error))
|
|
118
|
+
}));
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* If the contract has any RPC entry, subscribe to `amq.rabbitmq.reply-to`
|
|
122
|
+
* once. Replies for every in-flight call arrive on this single consumer and
|
|
123
|
+
* are demultiplexed by `correlationId`.
|
|
124
|
+
*/
|
|
125
|
+
setupReplyConsumerIfNeeded() {
|
|
126
|
+
const rpcs = this.contract.rpcs ?? {};
|
|
127
|
+
if (Object.keys(rpcs).length === 0) return Future.value(Result.Ok(void 0));
|
|
128
|
+
return this.amqpClient.consume(DIRECT_REPLY_TO, (msg) => this.handleRpcReply(msg), { noAck: true }).tapOk((tag) => {
|
|
129
|
+
this.replyConsumerTag = tag;
|
|
130
|
+
}).mapOk(() => void 0);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Demultiplex an RPC reply by `correlationId`, validate the body against the
|
|
134
|
+
* call's response schema, and resolve the awaiting caller. Replies with no
|
|
135
|
+
* matching pending call (e.g. arriving after the call timed out) are dropped
|
|
136
|
+
* with a debug log.
|
|
137
|
+
*/
|
|
138
|
+
handleRpcReply(msg) {
|
|
139
|
+
if (!msg) return;
|
|
140
|
+
const correlationId = msg.properties.correlationId;
|
|
141
|
+
if (typeof correlationId !== "string") {
|
|
142
|
+
this.logger?.warn("Received RPC reply without correlationId; dropping", { deliveryTag: msg.fields.deliveryTag });
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const pending = this.pendingCalls.get(correlationId);
|
|
146
|
+
if (!pending) {
|
|
147
|
+
this.logger?.debug("Received RPC reply for unknown correlationId", { correlationId });
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
this.pendingCalls.delete(correlationId);
|
|
151
|
+
clearTimeout(pending.timer);
|
|
152
|
+
let parsed;
|
|
153
|
+
try {
|
|
154
|
+
parsed = JSON.parse(msg.content.toString());
|
|
155
|
+
} catch (error) {
|
|
156
|
+
pending.resolve(Result.Error(new TechnicalError(`Failed to parse RPC reply JSON for "${pending.rpcName}"`, error)));
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
let rawValidation;
|
|
160
|
+
try {
|
|
161
|
+
rawValidation = pending.responseSchema["~standard"].validate(parsed);
|
|
162
|
+
} catch (error) {
|
|
163
|
+
pending.resolve(Result.Error(new TechnicalError(`RPC reply validation threw for "${pending.rpcName}"`, error)));
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
(rawValidation instanceof Promise ? rawValidation : Promise.resolve(rawValidation)).then((validation) => {
|
|
167
|
+
if (validation.issues) {
|
|
168
|
+
pending.resolve(Result.Error(new MessageValidationError(pending.rpcName, validation.issues)));
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
pending.resolve(Result.Ok(validation.value));
|
|
172
|
+
}, (error) => {
|
|
173
|
+
pending.resolve(Result.Error(new TechnicalError(`RPC reply validation threw for "${pending.rpcName}"`, error)));
|
|
174
|
+
});
|
|
53
175
|
}
|
|
54
176
|
/**
|
|
55
177
|
* Publish a message using a defined publisher
|
|
@@ -117,16 +239,114 @@ var TypedAmqpClient = class TypedAmqpClient {
|
|
|
117
239
|
});
|
|
118
240
|
}
|
|
119
241
|
/**
|
|
120
|
-
*
|
|
242
|
+
* Invoke an RPC defined via `defineRpc` and await the typed response.
|
|
243
|
+
*
|
|
244
|
+
* The request payload is validated against the RPC's request schema, then
|
|
245
|
+
* published to the AMQP default exchange with the server's queue name as
|
|
246
|
+
* routing key, `replyTo` set to `amq.rabbitmq.reply-to`, and a fresh UUID
|
|
247
|
+
* `correlationId`. The returned Future resolves once a matching reply
|
|
248
|
+
* arrives and validates against the response schema, or once `timeoutMs`
|
|
249
|
+
* elapses (whichever comes first).
|
|
250
|
+
*
|
|
251
|
+
* @typeParam TName - An RPC name from `contract.rpcs`.
|
|
252
|
+
* @param rpcName - The RPC name from the contract.
|
|
253
|
+
* @param request - The request payload, validated against the request schema.
|
|
254
|
+
* @param options - Per-call options. `timeoutMs` is required.
|
|
255
|
+
*
|
|
256
|
+
* @returns `Result.Ok(response)` on a successful round-trip; `Result.Error`
|
|
257
|
+
* on validation, transport, timeout, or cancel.
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* ```typescript
|
|
261
|
+
* const result = await client
|
|
262
|
+
* .call('calculate', { a: 1, b: 2 }, { timeoutMs: 5_000 })
|
|
263
|
+
* .toPromise();
|
|
264
|
+
* if (result.isOk()) console.log(result.value.sum); // 3
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
call(rpcName, request, options) {
|
|
268
|
+
const TIMEOUT_MAX_MS = 2147483647;
|
|
269
|
+
if (typeof options.timeoutMs !== "number" || !Number.isFinite(options.timeoutMs) || options.timeoutMs <= 0 || options.timeoutMs > TIMEOUT_MAX_MS) return Future.value(Result.Error(new TechnicalError(`Invalid timeoutMs for RPC call to "${String(rpcName)}": expected a finite positive number ≤ ${TIMEOUT_MAX_MS}, got ${String(options.timeoutMs)}`)));
|
|
270
|
+
const startTime = Date.now();
|
|
271
|
+
const rpc = this.contract.rpcs[rpcName];
|
|
272
|
+
const requestSchema = rpc.request.payload;
|
|
273
|
+
const responseSchema = rpc.response.payload;
|
|
274
|
+
const queueName = extractQueue(rpc.queue).name;
|
|
275
|
+
const span = startPublishSpan(this.telemetry, "", queueName, { [MessagingSemanticConventions.AMQP_PUBLISHER_NAME]: String(rpcName) });
|
|
276
|
+
const correlationId = randomUUID();
|
|
277
|
+
const callFuture = Future.make((resolve) => {
|
|
278
|
+
const timer = setTimeout(() => {
|
|
279
|
+
if (!this.pendingCalls.get(correlationId)) return;
|
|
280
|
+
this.pendingCalls.delete(correlationId);
|
|
281
|
+
resolve(Result.Error(new RpcTimeoutError(String(rpcName), options.timeoutMs)));
|
|
282
|
+
}, options.timeoutMs);
|
|
283
|
+
this.pendingCalls.set(correlationId, {
|
|
284
|
+
rpcName: String(rpcName),
|
|
285
|
+
responseSchema,
|
|
286
|
+
resolve,
|
|
287
|
+
timer
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
const validateRequest = () => {
|
|
291
|
+
let rawValidation;
|
|
292
|
+
try {
|
|
293
|
+
rawValidation = requestSchema["~standard"].validate(request);
|
|
294
|
+
} catch (error) {
|
|
295
|
+
return Future.value(Result.Error(new TechnicalError("RPC request validation threw", error)));
|
|
296
|
+
}
|
|
297
|
+
const validationPromise = rawValidation instanceof Promise ? rawValidation : Promise.resolve(rawValidation);
|
|
298
|
+
return Future.fromPromise(validationPromise).mapError((error) => new TechnicalError("RPC request validation threw", error)).mapOkToResult((validation) => validation.issues ? Result.Error(new MessageValidationError(String(rpcName), validation.issues)) : Result.Ok(validation.value));
|
|
299
|
+
};
|
|
300
|
+
const publishRequest = (validatedRequest) => {
|
|
301
|
+
const { compression: _ignoredCompression, ...defaultsWithoutCompression } = this.defaultPublishOptions;
|
|
302
|
+
const publishOptions = {
|
|
303
|
+
...defaultsWithoutCompression,
|
|
304
|
+
...options.publishOptions,
|
|
305
|
+
replyTo: DIRECT_REPLY_TO,
|
|
306
|
+
correlationId,
|
|
307
|
+
contentType: "application/json"
|
|
308
|
+
};
|
|
309
|
+
return this.amqpClient.publish("", queueName, validatedRequest, publishOptions).mapOkToResult((published) => published ? Result.Ok(void 0) : Result.Error(new TechnicalError(`Failed to publish RPC request for "${String(rpcName)}": channel buffer full`)));
|
|
310
|
+
};
|
|
311
|
+
return validateRequest().flatMapOk((validated) => publishRequest(validated)).flatMap((preflight) => {
|
|
312
|
+
if (preflight.isError()) {
|
|
313
|
+
const pending = this.pendingCalls.get(correlationId);
|
|
314
|
+
if (pending) {
|
|
315
|
+
clearTimeout(pending.timer);
|
|
316
|
+
this.pendingCalls.delete(correlationId);
|
|
317
|
+
}
|
|
318
|
+
return Future.value(Result.Error(preflight.error));
|
|
319
|
+
}
|
|
320
|
+
return callFuture;
|
|
321
|
+
}).tapOk(() => {
|
|
322
|
+
const durationMs = Date.now() - startTime;
|
|
323
|
+
endSpanSuccess(span);
|
|
324
|
+
recordPublishMetric(this.telemetry, "", queueName, true, durationMs);
|
|
325
|
+
}).tapError((error) => {
|
|
326
|
+
const durationMs = Date.now() - startTime;
|
|
327
|
+
endSpanError(span, error);
|
|
328
|
+
recordPublishMetric(this.telemetry, "", queueName, false, durationMs);
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Close the channel and connection. Cancels the reply consumer (if any) and
|
|
333
|
+
* rejects every in-flight RPC call with `RpcCancelledError`.
|
|
121
334
|
*/
|
|
122
335
|
close() {
|
|
123
|
-
|
|
336
|
+
for (const [, pending] of this.pendingCalls) {
|
|
337
|
+
clearTimeout(pending.timer);
|
|
338
|
+
pending.resolve(Result.Error(new RpcCancelledError(pending.rpcName)));
|
|
339
|
+
}
|
|
340
|
+
this.pendingCalls.clear();
|
|
341
|
+
return (this.replyConsumerTag ? this.amqpClient.cancel(this.replyConsumerTag).tapError((error) => {
|
|
342
|
+
this.logger?.warn("Failed to cancel RPC reply consumer during close", { error });
|
|
343
|
+
}) : Future.value(Result.Ok(void 0))).flatMap(() => this.amqpClient.close()).mapOk(() => void 0);
|
|
124
344
|
}
|
|
125
345
|
waitForConnectionReady() {
|
|
126
346
|
return this.amqpClient.waitForConnect();
|
|
127
347
|
}
|
|
128
348
|
};
|
|
129
349
|
//#endregion
|
|
130
|
-
export { MessageValidationError, TypedAmqpClient };
|
|
350
|
+
export { MessageValidationError, RpcCancelledError, RpcTimeoutError, TypedAmqpClient };
|
|
131
351
|
|
|
132
352
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/compression.ts","../src/client.ts"],"sourcesContent":["import { Future, Result } from \"@swan-io/boxed\";\nimport { deflate, gzip } from \"node:zlib\";\nimport type { CompressionAlgorithm } from \"@amqp-contract/contract\";\nimport { TechnicalError } from \"@amqp-contract/core\";\nimport { match } from \"ts-pattern\";\nimport { promisify } from \"node:util\";\n\nconst gzipAsync = promisify(gzip);\nconst deflateAsync = promisify(deflate);\n\n/**\n * Compress a buffer using the specified compression algorithm.\n *\n * @param buffer - The buffer to compress\n * @param algorithm - The compression algorithm to use\n * @returns A Future with the compressed buffer or a TechnicalError\n *\n * @internal\n */\nexport function compressBuffer(\n buffer: Buffer,\n algorithm: CompressionAlgorithm,\n): Future<Result<Buffer, TechnicalError>> {\n return match(algorithm)\n .with(\"gzip\", () =>\n Future.fromPromise(gzipAsync(buffer)).mapError(\n (error) => new TechnicalError(\"Failed to compress with gzip\", error),\n ),\n )\n .with(\"deflate\", () =>\n Future.fromPromise(deflateAsync(buffer)).mapError(\n (error) => new TechnicalError(\"Failed to compress with deflate\", error),\n ),\n )\n .exhaustive();\n}\n","import type {\n CompressionAlgorithm,\n ContractDefinition,\n InferPublisherNames,\n} from \"@amqp-contract/contract\";\nimport {\n AmqpClient,\n PublishOptions as AmqpClientPublishOptions,\n type Logger,\n MessagingSemanticConventions,\n TechnicalError,\n type TelemetryProvider,\n defaultTelemetryProvider,\n endSpanError,\n endSpanSuccess,\n recordPublishMetric,\n startPublishSpan,\n} from \"@amqp-contract/core\";\nimport { Future, Result } from \"@swan-io/boxed\";\nimport type { AmqpConnectionManagerOptions, ConnectionUrl } from \"amqp-connection-manager\";\nimport { compressBuffer } from \"./compression.js\";\nimport { MessageValidationError } from \"./errors.js\";\nimport type { ClientInferPublisherInput } from \"./types.js\";\n\n/**\n * Publish options that extend amqp-client's PublishOptions with optional compression support.\n */\nexport type PublishOptions = AmqpClientPublishOptions & {\n /**\n * Optional compression algorithm to use for the message payload.\n * When specified, the message will be compressed using the chosen algorithm\n * and the contentEncoding header will be set automatically.\n */\n compression?: CompressionAlgorithm | undefined;\n};\n\n/**\n * Options for creating a client\n */\nexport type CreateClientOptions<TContract extends ContractDefinition> = {\n contract: TContract;\n urls: ConnectionUrl[];\n connectionOptions?: AmqpConnectionManagerOptions | undefined;\n logger?: Logger | undefined;\n /**\n * Optional telemetry provider for tracing and metrics.\n * If not provided, uses the default provider which attempts to load OpenTelemetry.\n * OpenTelemetry instrumentation is automatically enabled if @opentelemetry/api is installed.\n */\n telemetry?: TelemetryProvider | undefined;\n /**\n * Default publish options that will be applied to all publish operations.\n * These can be overridden by options passed to the publish method.\n * By default, persistent is set to true for message durability.\n */\n defaultPublishOptions?: PublishOptions | undefined;\n};\n\n/**\n * Type-safe AMQP client for publishing messages\n */\nexport class TypedAmqpClient<TContract extends ContractDefinition> {\n private constructor(\n private readonly contract: TContract,\n private readonly amqpClient: AmqpClient,\n private readonly defaultPublishOptions: PublishOptions,\n private readonly logger?: Logger,\n private readonly telemetry: TelemetryProvider = defaultTelemetryProvider,\n ) {}\n\n /**\n * Create a type-safe AMQP client from a contract.\n *\n * Connection management (including automatic reconnection) is handled internally\n * by amqp-connection-manager via the {@link AmqpClient}. The client establishes\n * infrastructure asynchronously in the background once the connection is ready.\n *\n * Connections are automatically shared across clients with the same URLs and\n * connection options, following RabbitMQ best practices.\n */\n static create<TContract extends ContractDefinition>({\n contract,\n urls,\n connectionOptions,\n defaultPublishOptions,\n logger,\n telemetry,\n }: CreateClientOptions<TContract>): Future<Result<TypedAmqpClient<TContract>, TechnicalError>> {\n const client = new TypedAmqpClient(\n contract,\n new AmqpClient(contract, { urls, connectionOptions }),\n { persistent: true, ...defaultPublishOptions },\n logger,\n telemetry ?? defaultTelemetryProvider,\n );\n\n return client.waitForConnectionReady().mapOk(() => client);\n }\n\n /**\n * Publish a message using a defined publisher\n *\n * @param publisherName - The name of the publisher to use\n * @param message - The message to publish\n * @param options - Optional publish options including compression, headers, priority, etc.\n *\n * @remarks\n * If `options.compression` is specified, the message will be compressed before publishing\n * and the `contentEncoding` property will be set automatically. Any `contentEncoding`\n * value already in options will be overwritten by the compression algorithm.\n *\n * @returns Result.Ok(void) on success, or Result.Error with specific error on failure\n */\n /**\n * Publish a message using a defined publisher.\n * TypeScript guarantees publisher exists for valid publisher names.\n */\n publish<TName extends InferPublisherNames<TContract>>(\n publisherName: TName,\n message: ClientInferPublisherInput<TContract, TName>,\n options?: PublishOptions,\n ): Future<Result<void, TechnicalError | MessageValidationError>> {\n const startTime = Date.now();\n // Non-null assertions safe: TypeScript guarantees these exist for valid TName\n const publisher = this.contract.publishers![publisherName as string]!;\n const { exchange, routingKey } = publisher;\n\n // Start telemetry span\n const span = startPublishSpan(this.telemetry, exchange.name, routingKey, {\n [MessagingSemanticConventions.AMQP_PUBLISHER_NAME]: String(publisherName),\n });\n\n const validateMessage = () => {\n const validationResult = publisher.message.payload[\"~standard\"].validate(message);\n return Future.fromPromise(\n validationResult instanceof Promise ? validationResult : Promise.resolve(validationResult),\n )\n .mapError((error) => new TechnicalError(`Validation failed`, error))\n .mapOkToResult((validation) => {\n if (validation.issues) {\n return Result.Error(\n new MessageValidationError(String(publisherName), validation.issues),\n );\n }\n\n return Result.Ok(validation.value);\n });\n };\n\n const publishMessage = (validatedMessage: unknown): Future<Result<void, TechnicalError>> => {\n // Merge default options with provided options\n const mergedOptions = { ...this.defaultPublishOptions, ...options };\n\n // Extract compression from merged options and create publish options without it\n const { compression, ...restOptions } = mergedOptions;\n const publishOptions: AmqpClientPublishOptions = { ...restOptions };\n\n // Prepare payload and options based on compression configuration\n const preparePayload = (): Future<Result<Buffer | unknown, TechnicalError>> => {\n if (compression) {\n // Compress the message payload\n const messageBuffer = Buffer.from(JSON.stringify(validatedMessage));\n publishOptions.contentEncoding = compression;\n\n return compressBuffer(messageBuffer, compression);\n }\n\n // No compression: use the channel's built-in JSON serialization\n return Future.value(Result.Ok(validatedMessage));\n };\n\n // Publish the prepared payload\n return preparePayload().flatMapOk((payload) =>\n this.amqpClient\n .publish(publisher.exchange.name, publisher.routingKey ?? \"\", payload, publishOptions)\n .mapOkToResult((published) => {\n if (!published) {\n return Result.Error(\n new TechnicalError(\n `Failed to publish message for publisher \"${String(publisherName)}\": Channel rejected the message (buffer full or other channel issue)`,\n ),\n );\n }\n\n this.logger?.info(\"Message published successfully\", {\n publisherName: String(publisherName),\n exchange: publisher.exchange.name,\n routingKey: publisher.routingKey,\n compressed: !!compression,\n });\n\n return Result.Ok(undefined);\n }),\n );\n };\n\n // Validate message using schema\n return validateMessage()\n .flatMapOk((validatedMessage) => publishMessage(validatedMessage))\n .tapOk(() => {\n const durationMs = Date.now() - startTime;\n endSpanSuccess(span);\n recordPublishMetric(this.telemetry, exchange.name, routingKey, true, durationMs);\n })\n .tapError((error) => {\n const durationMs = Date.now() - startTime;\n endSpanError(span, error);\n recordPublishMetric(this.telemetry, exchange.name, routingKey, false, durationMs);\n });\n }\n\n /**\n * Close the channel and connection\n */\n close(): Future<Result<void, TechnicalError>> {\n return this.amqpClient.close().mapOk(() => undefined);\n }\n\n private waitForConnectionReady(): Future<Result<void, TechnicalError>> {\n return this.amqpClient.waitForConnect();\n }\n}\n"],"mappings":";;;;;;AAOA,MAAM,YAAY,UAAU,KAAK;AACjC,MAAM,eAAe,UAAU,QAAQ;;;;;;;;;;AAWvC,SAAgB,eACd,QACA,WACwC;AACxC,QAAO,MAAM,UAAU,CACpB,KAAK,cACJ,OAAO,YAAY,UAAU,OAAO,CAAC,CAAC,UACnC,UAAU,IAAI,eAAe,gCAAgC,MAAM,CACrE,CACF,CACA,KAAK,iBACJ,OAAO,YAAY,aAAa,OAAO,CAAC,CAAC,UACtC,UAAU,IAAI,eAAe,mCAAmC,MAAM,CACxE,CACF,CACA,YAAY;;;;;;;AC2BjB,IAAa,kBAAb,MAAa,gBAAsD;CACjE,YACE,UACA,YACA,uBACA,QACA,YAAgD,0BAChD;AALiB,OAAA,WAAA;AACA,OAAA,aAAA;AACA,OAAA,wBAAA;AACA,OAAA,SAAA;AACA,OAAA,YAAA;;;;;;;;;;;;CAanB,OAAO,OAA6C,EAClD,UACA,MACA,mBACA,uBACA,QACA,aAC6F;EAC7F,MAAM,SAAS,IAAI,gBACjB,UACA,IAAI,WAAW,UAAU;GAAE;GAAM;GAAmB,CAAC,EACrD;GAAE,YAAY;GAAM,GAAG;GAAuB,EAC9C,QACA,aAAa,yBACd;A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,SAAK,QAAQ,KAAK,kCAAkC;KAClD,eAAe,OAAO,cAAc;KACpC,UAAU,UAAU,SAAS;KAC7B,YAAY,UAAU;KACtB,YAAY,CAAC,CAAC;KACf,CAAC;AAEF,WAAO,OAAO,GAAG,KAAA,EAAU;KAC3B,CACL;;AAIH,SAAO,iBAAiB,CACrB,WAAW,qBAAqB,eAAe,iBAAiB,CAAC,CACjE,YAAY;GACX,MAAM,aAAa,KAAK,KAAK,GAAG;AAChC,kBAAe,KAAK;AACpB,uBAAoB,KAAK,WAAW,SAAS,MAAM,YAAY,MAAM,WAAW;IAChF,CACD,UAAU,UAAU;GACnB,MAAM,aAAa,KAAK,KAAK,GAAG;AAChC,gBAAa,MAAM,MAAM;AACzB,uBAAoB,KAAK,WAAW,SAAS,MAAM,YAAY,OAAO,WAAW;IACjF;;;;;CAMN,QAA8C;AAC5C,SAAO,KAAK,WAAW,OAAO,CAAC,YAAY,KAAA,EAAU;;CAGvD,yBAAuE;AACrE,SAAO,KAAK,WAAW,gBAAgB"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/compression.ts","../src/errors.ts","../src/client.ts"],"sourcesContent":["import { Future, Result } from \"@swan-io/boxed\";\nimport { deflate, gzip } from \"node:zlib\";\nimport type { CompressionAlgorithm } from \"@amqp-contract/contract\";\nimport { TechnicalError } from \"@amqp-contract/core\";\nimport { match } from \"ts-pattern\";\nimport { promisify } from \"node:util\";\n\nconst gzipAsync = promisify(gzip);\nconst deflateAsync = promisify(deflate);\n\n/**\n * Compress a buffer using the specified compression algorithm.\n *\n * @param buffer - The buffer to compress\n * @param algorithm - The compression algorithm to use\n * @returns A Future with the compressed buffer or a TechnicalError\n *\n * @internal\n */\nexport function compressBuffer(\n buffer: Buffer,\n algorithm: CompressionAlgorithm,\n): Future<Result<Buffer, TechnicalError>> {\n return match(algorithm)\n .with(\"gzip\", () =>\n Future.fromPromise(gzipAsync(buffer)).mapError(\n (error) => new TechnicalError(\"Failed to compress with gzip\", error),\n ),\n )\n .with(\"deflate\", () =>\n Future.fromPromise(deflateAsync(buffer)).mapError(\n (error) => new TechnicalError(\"Failed to compress with deflate\", error),\n ),\n )\n .exhaustive();\n}\n","export { MessageValidationError } from \"@amqp-contract/core\";\n\n/**\n * Captured `Error.captureStackTrace` shim — only present on Node.js.\n */\nfunction captureStack(target: object, ctor: Function): void {\n const ErrorConstructor = Error as unknown as {\n captureStackTrace?: (target: object, constructor: Function) => void;\n };\n if (typeof ErrorConstructor.captureStackTrace === \"function\") {\n ErrorConstructor.captureStackTrace(target, ctor);\n }\n}\n\n/**\n * Returned from `TypedAmqpClient.call()` when the configured `timeoutMs` elapses\n * before the RPC server publishes a reply with the matching `correlationId`.\n *\n * The pending call is removed from the in-memory correlation map; if a reply\n * arrives after the timeout it is dropped (and a debug log is emitted by the\n * client if a logger is configured).\n */\nexport class RpcTimeoutError extends Error {\n constructor(\n public readonly rpcName: string,\n public readonly timeoutMs: number,\n ) {\n super(`RPC call to \"${rpcName}\" timed out after ${timeoutMs}ms with no reply received`);\n this.name = \"RpcTimeoutError\";\n captureStack(this, this.constructor);\n }\n}\n\n/**\n * Returned from any in-flight RPC call when the client is closed before the\n * reply is received. The correlation map is cleared on close and every pending\n * caller's promise resolves with `Result.Error(RpcCancelledError)`.\n */\nexport class RpcCancelledError extends Error {\n constructor(public readonly rpcName: string) {\n super(`RPC call to \"${rpcName}\" was cancelled because the client was closed`);\n this.name = \"RpcCancelledError\";\n captureStack(this, this.constructor);\n }\n}\n","import {\n extractQueue,\n type CompressionAlgorithm,\n type ContractDefinition,\n type InferPublisherNames,\n type InferRpcNames,\n} from \"@amqp-contract/contract\";\nimport {\n AmqpClient,\n PublishOptions as AmqpClientPublishOptions,\n type Logger,\n MessagingSemanticConventions,\n TechnicalError,\n type TelemetryProvider,\n defaultTelemetryProvider,\n endSpanError,\n endSpanSuccess,\n recordPublishMetric,\n startPublishSpan,\n} from \"@amqp-contract/core\";\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport { Future, Result } from \"@swan-io/boxed\";\nimport type { AmqpConnectionManagerOptions, ConnectionUrl } from \"amqp-connection-manager\";\nimport { randomUUID } from \"node:crypto\";\nimport { compressBuffer } from \"./compression.js\";\nimport { MessageValidationError, RpcCancelledError, RpcTimeoutError } from \"./errors.js\";\nimport type {\n ClientInferPublisherInput,\n ClientInferRpcRequestInput,\n ClientInferRpcResponseOutput,\n} from \"./types.js\";\n\n/**\n * The RabbitMQ direct-reply-to pseudo-queue. Publishing with `replyTo` set to\n * this value tells the server to deliver the response back to the consumer\n * subscribed on this queue on the same channel — no real queue is created and\n * no setup is required beyond consuming from it once with `noAck: true`.\n *\n * @see https://www.rabbitmq.com/docs/direct-reply-to\n */\nconst DIRECT_REPLY_TO = \"amq.rabbitmq.reply-to\";\n\n/**\n * In-flight RPC call tracked by `TypedAmqpClient`. The reply consumer\n * looks up entries by `correlationId` when responses arrive.\n */\ntype PendingCall = {\n rpcName: string;\n responseSchema: StandardSchemaV1;\n resolve: (\n result: Result<\n unknown,\n TechnicalError | MessageValidationError | RpcTimeoutError | RpcCancelledError\n >,\n ) => void;\n timer: ReturnType<typeof setTimeout>;\n};\n\n/**\n * Publish options that extend amqp-client's PublishOptions with optional compression support.\n */\nexport type PublishOptions = AmqpClientPublishOptions & {\n /**\n * Optional compression algorithm to use for the message payload.\n * When specified, the message will be compressed using the chosen algorithm\n * and the contentEncoding header will be set automatically.\n */\n compression?: CompressionAlgorithm | undefined;\n};\n\n/**\n * Options for creating a client\n */\nexport type CreateClientOptions<TContract extends ContractDefinition> = {\n contract: TContract;\n urls: ConnectionUrl[];\n connectionOptions?: AmqpConnectionManagerOptions | undefined;\n logger?: Logger | undefined;\n /**\n * Optional telemetry provider for tracing and metrics.\n * If not provided, uses the default provider which attempts to load OpenTelemetry.\n * OpenTelemetry instrumentation is automatically enabled if @opentelemetry/api is installed.\n */\n telemetry?: TelemetryProvider | undefined;\n /**\n * Default publish options that will be applied to all publish operations.\n * These can be overridden by options passed to the publish method.\n * By default, persistent is set to true for message durability.\n */\n defaultPublishOptions?: PublishOptions | undefined;\n /**\n * Maximum time in ms to wait for the AMQP connection to become ready before\n * `create()` resolves to `Result.Error<TechnicalError>`. Without this option,\n * `create()` waits forever — the underlying amqp-connection-manager retries\n * indefinitely.\n */\n connectTimeoutMs?: number | undefined;\n};\n\n/**\n * Per-call options for `client.call()`.\n */\nexport type CallOptions = {\n /**\n * Maximum time in ms to wait for an RPC reply. If exceeded, the call resolves\n * to `Result.Error<RpcTimeoutError>` and the in-memory correlation entry is\n * cleared. A late reply arriving after the timeout is silently dropped.\n *\n * Required: RPC without a timeout is a footgun.\n */\n timeoutMs: number;\n\n /**\n * Optional AMQP message properties to merge into the request. `replyTo` and\n * `correlationId` are managed by the client and cannot be overridden.\n */\n publishOptions?: Omit<AmqpClientPublishOptions, \"replyTo\" | \"correlationId\">;\n};\n\n/**\n * Type-safe AMQP client for publishing messages\n */\nexport class TypedAmqpClient<TContract extends ContractDefinition> {\n /**\n * In-flight RPC calls keyed by `correlationId`. Cleared when a reply is\n * received, when the call times out, or when the client is closed.\n */\n private readonly pendingCalls = new Map<string, PendingCall>();\n\n /**\n * Consumer tag of the reply consumer subscribed on `amq.rabbitmq.reply-to`.\n * Set when the contract has at least one entry in `rpcs`; undefined otherwise.\n */\n private replyConsumerTag?: string;\n\n private constructor(\n private readonly contract: TContract,\n private readonly amqpClient: AmqpClient,\n private readonly defaultPublishOptions: PublishOptions,\n private readonly logger?: Logger,\n private readonly telemetry: TelemetryProvider = defaultTelemetryProvider,\n ) {}\n\n /**\n * Create a type-safe AMQP client from a contract.\n *\n * Connection management (including automatic reconnection) is handled internally\n * by amqp-connection-manager via the {@link AmqpClient}. The client establishes\n * infrastructure asynchronously in the background once the connection is ready.\n *\n * Connections are automatically shared across clients with the same URLs and\n * connection options, following RabbitMQ best practices.\n */\n static create<TContract extends ContractDefinition>({\n contract,\n urls,\n connectionOptions,\n defaultPublishOptions,\n logger,\n telemetry,\n connectTimeoutMs,\n }: CreateClientOptions<TContract>): Future<Result<TypedAmqpClient<TContract>, TechnicalError>> {\n const client = new TypedAmqpClient(\n contract,\n new AmqpClient(contract, { urls, connectionOptions, connectTimeoutMs }),\n { persistent: true, ...defaultPublishOptions },\n logger,\n telemetry ?? defaultTelemetryProvider,\n );\n\n return client\n .waitForConnectionReady()\n .flatMapOk(() => client.setupReplyConsumerIfNeeded())\n .flatMap((result) =>\n result.match({\n Ok: () => Future.value(Result.Ok<TypedAmqpClient<TContract>, TechnicalError>(client)),\n // Release the AmqpClient's connection ref-count so a failed create() does not leak.\n Error: (error) =>\n client\n .close()\n .tapError((closeError) => {\n logger?.warn(\"Failed to close client after connection failure\", {\n error: closeError,\n });\n })\n .map(() => Result.Error<TypedAmqpClient<TContract>, TechnicalError>(error)),\n }),\n );\n }\n\n /**\n * If the contract has any RPC entry, subscribe to `amq.rabbitmq.reply-to`\n * once. Replies for every in-flight call arrive on this single consumer and\n * are demultiplexed by `correlationId`.\n */\n private setupReplyConsumerIfNeeded(): Future<Result<void, TechnicalError>> {\n const rpcs = this.contract.rpcs ?? {};\n if (Object.keys(rpcs).length === 0) {\n return Future.value(Result.Ok(undefined));\n }\n\n return this.amqpClient\n .consume(DIRECT_REPLY_TO, (msg) => this.handleRpcReply(msg), { noAck: true })\n .tapOk((tag) => {\n this.replyConsumerTag = tag;\n })\n .mapOk(() => undefined);\n }\n\n /**\n * Demultiplex an RPC reply by `correlationId`, validate the body against the\n * call's response schema, and resolve the awaiting caller. Replies with no\n * matching pending call (e.g. arriving after the call timed out) are dropped\n * with a debug log.\n */\n private handleRpcReply(msg: Parameters<Parameters<AmqpClient[\"consume\"]>[1]>[0]): void {\n if (!msg) return;\n const correlationId = msg.properties.correlationId;\n if (typeof correlationId !== \"string\") {\n this.logger?.warn(\"Received RPC reply without correlationId; dropping\", {\n deliveryTag: msg.fields.deliveryTag,\n });\n return;\n }\n const pending = this.pendingCalls.get(correlationId);\n if (!pending) {\n this.logger?.debug(\"Received RPC reply for unknown correlationId\", { correlationId });\n return;\n }\n this.pendingCalls.delete(correlationId);\n clearTimeout(pending.timer);\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(msg.content.toString());\n } catch (error: unknown) {\n pending.resolve(\n Result.Error(\n new TechnicalError(`Failed to parse RPC reply JSON for \"${pending.rpcName}\"`, error),\n ),\n );\n return;\n }\n\n // Wrap the validate call itself — a Standard Schema implementation may\n // throw synchronously, and the throw would otherwise escape the consume\n // callback and could crash the reply consumer.\n let rawValidation: ReturnType<StandardSchemaV1[\"~standard\"][\"validate\"]>;\n try {\n rawValidation = pending.responseSchema[\"~standard\"].validate(parsed);\n } catch (error: unknown) {\n pending.resolve(\n Result.Error(\n new TechnicalError(`RPC reply validation threw for \"${pending.rpcName}\"`, error),\n ),\n );\n return;\n }\n const validationPromise =\n rawValidation instanceof Promise ? rawValidation : Promise.resolve(rawValidation);\n\n validationPromise.then(\n (validation) => {\n if (validation.issues) {\n pending.resolve(\n Result.Error(new MessageValidationError(pending.rpcName, validation.issues)),\n );\n return;\n }\n pending.resolve(Result.Ok(validation.value));\n },\n (error: unknown) => {\n pending.resolve(\n Result.Error(\n new TechnicalError(`RPC reply validation threw for \"${pending.rpcName}\"`, error),\n ),\n );\n },\n );\n }\n\n /**\n * Publish a message using a defined publisher\n *\n * @param publisherName - The name of the publisher to use\n * @param message - The message to publish\n * @param options - Optional publish options including compression, headers, priority, etc.\n *\n * @remarks\n * If `options.compression` is specified, the message will be compressed before publishing\n * and the `contentEncoding` property will be set automatically. Any `contentEncoding`\n * value already in options will be overwritten by the compression algorithm.\n *\n * @returns Result.Ok(void) on success, or Result.Error with specific error on failure\n */\n /**\n * Publish a message using a defined publisher.\n * TypeScript guarantees publisher exists for valid publisher names.\n */\n publish<TName extends InferPublisherNames<TContract>>(\n publisherName: TName,\n message: ClientInferPublisherInput<TContract, TName>,\n options?: PublishOptions,\n ): Future<Result<void, TechnicalError | MessageValidationError>> {\n const startTime = Date.now();\n // Non-null assertions safe: TypeScript guarantees these exist for valid TName\n const publisher = this.contract.publishers![publisherName as string]!;\n const { exchange, routingKey } = publisher;\n\n // Start telemetry span\n const span = startPublishSpan(this.telemetry, exchange.name, routingKey, {\n [MessagingSemanticConventions.AMQP_PUBLISHER_NAME]: String(publisherName),\n });\n\n const validateMessage = () => {\n const validationResult = publisher.message.payload[\"~standard\"].validate(message);\n return Future.fromPromise(\n validationResult instanceof Promise ? validationResult : Promise.resolve(validationResult),\n )\n .mapError((error) => new TechnicalError(`Validation failed`, error))\n .mapOkToResult((validation) => {\n if (validation.issues) {\n return Result.Error(\n new MessageValidationError(String(publisherName), validation.issues),\n );\n }\n\n return Result.Ok(validation.value);\n });\n };\n\n const publishMessage = (validatedMessage: unknown): Future<Result<void, TechnicalError>> => {\n // Merge default options with provided options\n const mergedOptions = { ...this.defaultPublishOptions, ...options };\n\n // Extract compression from merged options and create publish options without it\n const { compression, ...restOptions } = mergedOptions;\n const publishOptions: AmqpClientPublishOptions = { ...restOptions };\n\n // Prepare payload and options based on compression configuration\n const preparePayload = (): Future<Result<Buffer | unknown, TechnicalError>> => {\n if (compression) {\n // Compress the message payload\n const messageBuffer = Buffer.from(JSON.stringify(validatedMessage));\n publishOptions.contentEncoding = compression;\n\n return compressBuffer(messageBuffer, compression);\n }\n\n // No compression: use the channel's built-in JSON serialization\n return Future.value(Result.Ok(validatedMessage));\n };\n\n // Publish the prepared payload\n return preparePayload().flatMapOk((payload) =>\n this.amqpClient\n .publish(publisher.exchange.name, publisher.routingKey ?? \"\", payload, publishOptions)\n .mapOkToResult((published) => {\n if (!published) {\n return Result.Error(\n new TechnicalError(\n `Failed to publish message for publisher \"${String(publisherName)}\": Channel rejected the message (buffer full or other channel issue)`,\n ),\n );\n }\n\n this.logger?.info(\"Message published successfully\", {\n publisherName: String(publisherName),\n exchange: publisher.exchange.name,\n routingKey: publisher.routingKey,\n compressed: !!compression,\n });\n\n return Result.Ok(undefined);\n }),\n );\n };\n\n // Validate message using schema\n return validateMessage()\n .flatMapOk((validatedMessage) => publishMessage(validatedMessage))\n .tapOk(() => {\n const durationMs = Date.now() - startTime;\n endSpanSuccess(span);\n recordPublishMetric(this.telemetry, exchange.name, routingKey, true, durationMs);\n })\n .tapError((error) => {\n const durationMs = Date.now() - startTime;\n endSpanError(span, error);\n recordPublishMetric(this.telemetry, exchange.name, routingKey, false, durationMs);\n });\n }\n\n /**\n * Invoke an RPC defined via `defineRpc` and await the typed response.\n *\n * The request payload is validated against the RPC's request schema, then\n * published to the AMQP default exchange with the server's queue name as\n * routing key, `replyTo` set to `amq.rabbitmq.reply-to`, and a fresh UUID\n * `correlationId`. The returned Future resolves once a matching reply\n * arrives and validates against the response schema, or once `timeoutMs`\n * elapses (whichever comes first).\n *\n * @typeParam TName - An RPC name from `contract.rpcs`.\n * @param rpcName - The RPC name from the contract.\n * @param request - The request payload, validated against the request schema.\n * @param options - Per-call options. `timeoutMs` is required.\n *\n * @returns `Result.Ok(response)` on a successful round-trip; `Result.Error`\n * on validation, transport, timeout, or cancel.\n *\n * @example\n * ```typescript\n * const result = await client\n * .call('calculate', { a: 1, b: 2 }, { timeoutMs: 5_000 })\n * .toPromise();\n * if (result.isOk()) console.log(result.value.sum); // 3\n * ```\n */\n call<TName extends InferRpcNames<TContract>>(\n rpcName: TName,\n request: ClientInferRpcRequestInput<TContract, TName>,\n options: CallOptions,\n ): Future<\n Result<\n ClientInferRpcResponseOutput<TContract, TName>,\n TechnicalError | MessageValidationError | RpcTimeoutError | RpcCancelledError\n >\n > {\n type CallResult = Result<\n ClientInferRpcResponseOutput<TContract, TName>,\n TechnicalError | MessageValidationError | RpcTimeoutError | RpcCancelledError\n >;\n\n // setTimeout truncates fractional ms and clamps anything outside the\n // 32-bit signed integer range (~24.8 days) to 1ms, so reject those up\n // front as user errors rather than producing surprising behavior.\n const TIMEOUT_MAX_MS = 2_147_483_647;\n if (\n typeof options.timeoutMs !== \"number\" ||\n !Number.isFinite(options.timeoutMs) ||\n options.timeoutMs <= 0 ||\n options.timeoutMs > TIMEOUT_MAX_MS\n ) {\n return Future.value(\n Result.Error(\n new TechnicalError(\n `Invalid timeoutMs for RPC call to \"${String(rpcName)}\": expected a finite positive number ≤ ${TIMEOUT_MAX_MS}, got ${String(options.timeoutMs)}`,\n ),\n ) as CallResult,\n );\n }\n\n const startTime = Date.now();\n // Non-null assertion safe: TName is constrained to RPC names in the contract.\n const rpc = this.contract.rpcs![rpcName as string]!;\n const requestSchema = rpc.request.payload;\n const responseSchema = rpc.response.payload;\n const queueName = extractQueue(rpc.queue).name;\n\n // RPC publishes to the default exchange with the queue name as routing key.\n const span = startPublishSpan(this.telemetry, \"\", queueName, {\n [MessagingSemanticConventions.AMQP_PUBLISHER_NAME]: String(rpcName),\n });\n\n const correlationId = randomUUID();\n const callFuture = Future.make<CallResult>((resolve) => {\n const timer = setTimeout(() => {\n const pending = this.pendingCalls.get(correlationId);\n if (!pending) return;\n this.pendingCalls.delete(correlationId);\n resolve(Result.Error(new RpcTimeoutError(String(rpcName), options.timeoutMs)));\n }, options.timeoutMs);\n\n this.pendingCalls.set(correlationId, {\n rpcName: String(rpcName),\n responseSchema,\n resolve: resolve as PendingCall[\"resolve\"],\n timer,\n });\n });\n\n const validateRequest = (): Future<\n Result<unknown, TechnicalError | MessageValidationError>\n > => {\n // Wrap the validate call — a Standard Schema implementation may throw\n // synchronously, and that throw would otherwise escape the Future chain\n // and leave the pending-call entry/timer dangling until timeout.\n let rawValidation: ReturnType<StandardSchemaV1[\"~standard\"][\"validate\"]>;\n try {\n rawValidation = requestSchema[\"~standard\"].validate(request);\n } catch (error: unknown) {\n return Future.value(\n Result.Error<unknown, TechnicalError | MessageValidationError>(\n new TechnicalError(\"RPC request validation threw\", error),\n ),\n );\n }\n const validationPromise =\n rawValidation instanceof Promise ? rawValidation : Promise.resolve(rawValidation);\n return Future.fromPromise(validationPromise)\n .mapError((error) => new TechnicalError(\"RPC request validation threw\", error))\n .mapOkToResult((validation) =>\n validation.issues\n ? Result.Error<unknown, TechnicalError | MessageValidationError>(\n new MessageValidationError(String(rpcName), validation.issues),\n )\n : Result.Ok<unknown, TechnicalError | MessageValidationError>(validation.value),\n );\n };\n\n const publishRequest = (validatedRequest: unknown): Future<Result<void, TechnicalError>> => {\n // Merge `defaultPublishOptions` (persistent, priority, headers, …) with\n // the per-call options, then layer the RPC-managed fields on top so they\n // cannot be overridden. `compression` is intentionally dropped: RPC v1\n // does not implement reply-side decompression, so request-side\n // compression would break the round-trip.\n const { compression: _ignoredCompression, ...defaultsWithoutCompression } =\n this.defaultPublishOptions;\n const publishOptions: AmqpClientPublishOptions = {\n ...defaultsWithoutCompression,\n ...options.publishOptions,\n replyTo: DIRECT_REPLY_TO,\n correlationId,\n contentType: \"application/json\",\n };\n return this.amqpClient\n .publish(\"\", queueName, validatedRequest, publishOptions)\n .mapOkToResult((published) =>\n published\n ? Result.Ok<void, TechnicalError>(undefined)\n : Result.Error<void, TechnicalError>(\n new TechnicalError(\n `Failed to publish RPC request for \"${String(rpcName)}\": channel buffer full`,\n ),\n ),\n );\n };\n\n // Validate the request, publish it, and await the reply (or timeout).\n return validateRequest()\n .flatMapOk((validated) => publishRequest(validated))\n .flatMap((preflight) => {\n if (preflight.isError()) {\n // Publish/validation failed before the request hit the broker — clean\n // up the pending entry so the timer never fires.\n const pending = this.pendingCalls.get(correlationId);\n if (pending) {\n clearTimeout(pending.timer);\n this.pendingCalls.delete(correlationId);\n }\n return Future.value(Result.Error(preflight.error) as CallResult);\n }\n return callFuture;\n })\n .tapOk(() => {\n const durationMs = Date.now() - startTime;\n endSpanSuccess(span);\n recordPublishMetric(this.telemetry, \"\", queueName, true, durationMs);\n })\n .tapError((error) => {\n const durationMs = Date.now() - startTime;\n endSpanError(span, error);\n recordPublishMetric(this.telemetry, \"\", queueName, false, durationMs);\n });\n }\n\n /**\n * Close the channel and connection. Cancels the reply consumer (if any) and\n * rejects every in-flight RPC call with `RpcCancelledError`.\n */\n close(): Future<Result<void, TechnicalError>> {\n // Reject pending calls first — once close() runs, no reply will arrive.\n for (const [, pending] of this.pendingCalls) {\n clearTimeout(pending.timer);\n pending.resolve(Result.Error(new RpcCancelledError(pending.rpcName)));\n }\n this.pendingCalls.clear();\n\n const cancelReply = this.replyConsumerTag\n ? this.amqpClient.cancel(this.replyConsumerTag).tapError((error) => {\n this.logger?.warn(\"Failed to cancel RPC reply consumer during close\", { error });\n })\n : Future.value(Result.Ok<void, TechnicalError>(undefined));\n\n return cancelReply.flatMap(() => this.amqpClient.close()).mapOk(() => undefined);\n }\n\n private waitForConnectionReady(): Future<Result<void, TechnicalError>> {\n return this.amqpClient.waitForConnect();\n }\n}\n"],"mappings":";;;;;;;;AAOA,MAAM,YAAY,UAAU,KAAK;AACjC,MAAM,eAAe,UAAU,QAAQ;;;;;;;;;;AAWvC,SAAgB,eACd,QACA,WACwC;AACxC,QAAO,MAAM,UAAU,CACpB,KAAK,cACJ,OAAO,YAAY,UAAU,OAAO,CAAC,CAAC,UACnC,UAAU,IAAI,eAAe,gCAAgC,MAAM,CACrE,CACF,CACA,KAAK,iBACJ,OAAO,YAAY,aAAa,OAAO,CAAC,CAAC,UACtC,UAAU,IAAI,eAAe,mCAAmC,MAAM,CACxE,CACF,CACA,YAAY;;;;;;;AC7BjB,SAAS,aAAa,QAAgB,MAAsB;CAC1D,MAAM,mBAAmB;AAGzB,KAAI,OAAO,iBAAiB,sBAAsB,WAChD,kBAAiB,kBAAkB,QAAQ,KAAK;;;;;;;;;;AAYpD,IAAa,kBAAb,cAAqC,MAAM;CACzC,YACE,SACA,WACA;AACA,QAAM,gBAAgB,QAAQ,oBAAoB,UAAU,2BAA2B;AAHvE,OAAA,UAAA;AACA,OAAA,YAAA;AAGhB,OAAK,OAAO;AACZ,eAAa,MAAM,KAAK,YAAY;;;;;;;;AASxC,IAAa,oBAAb,cAAuC,MAAM;CAC3C,YAAY,SAAiC;AAC3C,QAAM,gBAAgB,QAAQ,+CAA+C;AADnD,OAAA,UAAA;AAE1B,OAAK,OAA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