@ac-kit/net-core 0.1.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.
@@ -0,0 +1,352 @@
1
+ import { EventDispatcherMapBase } from "@ac-kit/async";
2
+ import { ByteAccumulator, Timer } from "@ac-kit/core";
3
+ import { BodyReader, DecodeDriver, } from "@ac-kit/format-core";
4
+ export class FrameLinkError extends Error {
5
+ constructor(message, options) {
6
+ super(message, options);
7
+ this.name = "FrameLinkError";
8
+ }
9
+ }
10
+ export class FrameLinkTransportError extends FrameLinkError {
11
+ constructor(options) {
12
+ super("Transport failed", options);
13
+ this.name = "FrameLinkTransportError";
14
+ }
15
+ }
16
+ export class FrameLinkProtocolError extends FrameLinkError {
17
+ constructor(message, options) {
18
+ super(message, options);
19
+ this.name = "FrameLinkProtocolError";
20
+ }
21
+ }
22
+ export class FrameLinkDestroyedError extends FrameLinkError {
23
+ constructor() {
24
+ super("Connection closed");
25
+ this.name = "FrameLinkClosedError";
26
+ }
27
+ }
28
+ /**
29
+ * Turns a byte {@link Transport} into a stream of typed values, and typed values
30
+ * back into bytes.
31
+ *
32
+ * Owns everything that is protocol-independent: the decode loop (via
33
+ * {@link DecodeDriver}), streamed payloads (via `BodyReader`), read and write
34
+ * backpressure, silence timeouts, transport and codec swapping, and lifecycle.
35
+ * Protocol knowledge lives entirely in the injected {@link Codec}, and
36
+ * correlation lives entirely above in the session.
37
+ *
38
+ * Message-mode datagrams bypass the decode loop entirely — a datagram is always
39
+ * exactly one complete value with no state carried to the next one, which
40
+ * `DecodeDriver` (built for accumulation across calls) has nothing to offer.
41
+ *
42
+ * @typeParam In - Decoded (inbound) value type.
43
+ * @typeParam Out - Encoded (outbound) value type. Separate from `In` because
44
+ * most non-trivial protocols are asymmetric.
45
+ */
46
+ export class FrameLink extends EventDispatcherMapBase {
47
+ transport;
48
+ codec;
49
+ sink;
50
+ driver;
51
+ silenceTimer = null;
52
+ body = null;
53
+ ended = false;
54
+ closed = false;
55
+ constructor(options) {
56
+ super();
57
+ this.transport = options.transport;
58
+ this.codec = options.codec;
59
+ this.sink = options.sink;
60
+ const sink = {
61
+ onDecoded: (value, _consumed, extra) => this.handleDecoded(value, extra.body),
62
+ onError: (error) => this.dispatch("protocolError", [error]),
63
+ onFatal: (error) => this.fail(error),
64
+ onIncomplete: () => {
65
+ /* Wait for more bytes. */
66
+ },
67
+ onPending: () => {
68
+ /* The idle timer is armed by the schedule callback below. */
69
+ },
70
+ onTruncated: () => this.fail(new Error("Connection ended with an incomplete value")),
71
+ };
72
+ this.driver = new DecodeDriver({
73
+ decoder: this.codec,
74
+ buffer: new ByteAccumulator(options.maxBufferSize, {
75
+ initialCapacity: options.initialCapacity,
76
+ }),
77
+ sink,
78
+ idle: {
79
+ kind: "timer",
80
+ schedule: (delayMs, run) => {
81
+ this.silenceTimer = new Timer(run, delayMs);
82
+ this.silenceTimer.start();
83
+ return () => {
84
+ this.silenceTimer?.cancel();
85
+ this.silenceTimer = null;
86
+ };
87
+ },
88
+ },
89
+ });
90
+ this.transport.start(this.buildHandlers());
91
+ }
92
+ /** Bytes received but not yet decoded. */
93
+ get buffered() {
94
+ return this.driver.buffered;
95
+ }
96
+ /**
97
+ * Encode and send one value.
98
+ *
99
+ * @param value The value to transmit.
100
+ * @param signal Cancels the wait for transport capacity. Bytes already
101
+ * accepted by the transport cannot be unsent.
102
+ * @returns Resolves once the transport has accepted the bytes.
103
+ */
104
+ send(value, signal) {
105
+ return this.sendRaw(this.codec.encode(value), signal);
106
+ }
107
+ /**
108
+ * Send raw bytes, bypassing the encoder.
109
+ *
110
+ * Required by protocols that interleave opaque payloads with framed commands,
111
+ * such as an SMTP `DATA` body.
112
+ *
113
+ * @param data One or more buffers, written as a single vectored write.
114
+ * @param signal Cancels the wait for transport capacity.
115
+ * @returns Resolves once the transport has accepted the bytes.
116
+ */
117
+ sendRaw(data, signal) {
118
+ return this.transport.write(ArrayBuffer.isView(data) ? [data] : data, signal);
119
+ }
120
+ /** Suspend value delivery by pausing the transport. */
121
+ pause() {
122
+ this.transport.pause();
123
+ }
124
+ /** Resume value delivery suspended by {@link pause}. */
125
+ resume() {
126
+ this.transport.resume();
127
+ }
128
+ /**
129
+ * Discard undecoded bytes, decoder state and any silence timer.
130
+ *
131
+ * Safe to call from inside a {@link FrameSink} invocation: `DecodeDriver`
132
+ * detects the reentrant call and stops rather than continuing over a buffer
133
+ * that no longer exists.
134
+ */
135
+ reset() {
136
+ this.driver.clear();
137
+ this.discardBody();
138
+ }
139
+ /**
140
+ * Remove and return the undecoded bytes without discarding them.
141
+ *
142
+ * The residue after a protocol switch belongs to the next protocol: an HTTP
143
+ * `Upgrade` response may be followed immediately by WebSocket bytes in the
144
+ * same segment. Discarding them silently corrupts the upgraded connection.
145
+ *
146
+ * @returns A copy of the bytes received but not yet decoded.
147
+ */
148
+ takeResidue() {
149
+ return this.driver.takeResidue();
150
+ }
151
+ /**
152
+ * Replace the codec, for example after an RTMP handshake completes or an SMTP
153
+ * session negotiates a new mode.
154
+ *
155
+ * @param codec The codec to decode and encode with from now on.
156
+ */
157
+ swapCodec(codec) {
158
+ this.codec = codec;
159
+ this.driver.swapDecoder(codec);
160
+ }
161
+ /**
162
+ * Replace the transport, carrying over any undecoded bytes.
163
+ *
164
+ * This is the STARTTLS primitive: the plaintext transport is detached (not
165
+ * destroyed, since the TLS transport wraps the same socket) and framing
166
+ * continues over the new one.
167
+ *
168
+ * @param transport The transport to read from and write to from now on.
169
+ * @param options `keepResidue` retains undecoded bytes across the swap. Leave
170
+ * it `false` for security-sensitive switches such as STARTTLS, where
171
+ * buffered plaintext is a command-injection vector.
172
+ */
173
+ swapTransport(transport, options) {
174
+ this.discardBody();
175
+ if (options?.keepResidue) {
176
+ this.driver.resync();
177
+ }
178
+ else {
179
+ this.driver.discardBuffer();
180
+ }
181
+ this.transport.stop();
182
+ this.transport = transport;
183
+ this.transport.start(this.buildHandlers());
184
+ }
185
+ /**
186
+ * Half-close the outbound direction and flush pending writes.
187
+ *
188
+ * @returns Resolves once the transport has flushed.
189
+ */
190
+ end() {
191
+ return this.transport.end();
192
+ }
193
+ /**
194
+ * Tear down the link and its transport.
195
+ *
196
+ * @param error Optional cause reported to listeners.
197
+ */
198
+ destroy(error) {
199
+ if (this.closed) {
200
+ return;
201
+ }
202
+ error ??= new FrameLinkDestroyedError();
203
+ this.discardBody(error);
204
+ this.transport.destroy(error);
205
+ this.transport.stop();
206
+ }
207
+ /** Destroys the link, so it can be used with `await using`. */
208
+ async [Symbol.asyncDispose]() {
209
+ this.destroy();
210
+ }
211
+ buildHandlers() {
212
+ return {
213
+ data: (chunk) => this.handleData(chunk),
214
+ end: () => this.handleEnd(),
215
+ close: () => this.handleClose(),
216
+ error: (error) => this.fail(new FrameLinkTransportError({ cause: error })),
217
+ };
218
+ }
219
+ handleData(chunk) {
220
+ if (this.closed) {
221
+ return;
222
+ }
223
+ // Body bytes bypass the decode loop entirely; this is what keeps large
224
+ // payloads from ever being accumulated or copied.
225
+ if (this.body) {
226
+ const consumed = this.body.feed(chunk);
227
+ if (consumed === chunk.length) {
228
+ return;
229
+ }
230
+ chunk = chunk.subarray(consumed);
231
+ }
232
+ if (this.transport.mode === "message") {
233
+ this.decodeMessage(chunk);
234
+ return;
235
+ }
236
+ try {
237
+ this.driver.write(chunk);
238
+ }
239
+ catch (error) {
240
+ this.fail(error);
241
+ }
242
+ }
243
+ decodeMessage(chunk) {
244
+ const context = {
245
+ timedOut: false,
246
+ atEof: this.ended,
247
+ atMessageBoundary: true,
248
+ };
249
+ const result = this.codec.decode(chunk, context);
250
+ switch (result.status) {
251
+ case "decoded": {
252
+ const seed = chunk.subarray(result.consumed);
253
+ this.handleDecoded(result.value, result.body, seed);
254
+ break;
255
+ }
256
+ case "error":
257
+ this.dispatch("protocolError", [result.error]);
258
+ break;
259
+ case "fatal":
260
+ this.fail(result.error);
261
+ break;
262
+ default:
263
+ this.fail(new Error("Datagram did not contain a complete value"));
264
+ break;
265
+ }
266
+ }
267
+ /**
268
+ * Emits one decoded value and, when the codec declared one, starts its body
269
+ * stream.
270
+ *
271
+ * @param seed Message-mode only: the bytes of the same datagram that follow
272
+ * the value. `undefined` in stream mode, where any already-buffered bytes
273
+ * are pulled from the driver instead (see
274
+ * {@link DecodeDriver["takeResidue"]}).
275
+ */
276
+ handleDecoded(value, body, seed) {
277
+ if (!body) {
278
+ this.sink(value);
279
+ return;
280
+ }
281
+ const stream = this.startBody(body);
282
+ this.sink(value, stream);
283
+ if (seed !== undefined) {
284
+ // Message mode: the body can only ever be as long as what's left in
285
+ // this one datagram, so it always finishes here regardless of mode.
286
+ if (seed.length > 0) {
287
+ this.body.feed(seed);
288
+ }
289
+ this.body.finish();
290
+ return;
291
+ }
292
+ // Stream mode: anything already buffered beyond this value's own bytes
293
+ // belongs to the body, not the next decode attempt.
294
+ const pending = this.driver.takeResidue();
295
+ if (pending.length > 0) {
296
+ const taken = this.body.feed(pending);
297
+ if (taken < pending.length) {
298
+ this.driver.write(pending.subarray(taken));
299
+ }
300
+ }
301
+ else if (this.ended) {
302
+ this.body.finishAtEof();
303
+ }
304
+ }
305
+ startBody(spec) {
306
+ const reader = new BodyReader(spec, {
307
+ onBackpressure: (paused) => {
308
+ if (paused) {
309
+ this.transport.pause();
310
+ }
311
+ else {
312
+ this.transport.resume();
313
+ }
314
+ },
315
+ });
316
+ this.body = reader;
317
+ return reader.stream;
318
+ }
319
+ discardBody(error) {
320
+ this.body?.discard(error);
321
+ this.body = null;
322
+ }
323
+ handleEnd() {
324
+ this.ended = true;
325
+ if (this.body) {
326
+ this.body.finishAtEof();
327
+ }
328
+ this.driver.close();
329
+ this.dispatch("end", []);
330
+ }
331
+ handleClose() {
332
+ if (this.closed) {
333
+ return;
334
+ }
335
+ this.closed = true;
336
+ this.discardBody(new Error("Connection closed"));
337
+ this.transport.stop();
338
+ this.dispatch("close", []);
339
+ }
340
+ fail(error) {
341
+ if (this.closed) {
342
+ return;
343
+ }
344
+ this.closed = true;
345
+ this.discardBody(error);
346
+ this.transport.destroy(error);
347
+ this.transport.stop();
348
+ this.dispatch("error", [error]);
349
+ this.dispatch("close", []);
350
+ }
351
+ }
352
+ //# sourceMappingURL=frame-link.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"frame-link.js","sourceRoot":"","sources":["../src/frame-link.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAuB,MAAM,eAAe,CAAC;AAC5E,OAAO,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACtD,OAAO,EACN,UAAU,EAIV,YAAY,GAEZ,MAAM,qBAAqB,CAAC;AAK7B,MAAM,OAAO,cAAe,SAAQ,KAAK;IACxC,YAAY,OAAe,EAAE,OAAsB;QAClD,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;IAC9B,CAAC;CACD;AAED,MAAM,OAAO,uBAAwB,SAAQ,cAAc;IAC1D,YAAY,OAAsB;QACjC,KAAK,CAAC,kBAAkB,EAAE,OAAO,CAAC,CAAC;QACnC,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC;IACvC,CAAC;CACD;AAED,MAAM,OAAO,sBAAuB,SAAQ,cAAc;IACzD,YAAY,OAAe,EAAE,OAAsB;QAClD,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAC;IACtC,CAAC;CACD;AAED,MAAM,OAAO,uBAAwB,SAAQ,cAAc;IAC1D;QACC,KAAK,CAAC,mBAAmB,CAAC,CAAC;QAC3B,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACpC,CAAC;CACD;AAmDD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,OAAO,SACZ,SAAQ,sBAAuC;IAGvC,SAAS,CAAY;IACrB,KAAK,CAAiB;IACb,IAAI,CAAgB;IACpB,MAAM,CAAoD;IAEnE,YAAY,GAAiB,IA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QACpB,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAC1B,CAAC;IAEO,WAAW;QAClB,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YACjB,OAAO;QACR,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACnB,IAAI,CAAC,WAAW,CAAC,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC,CAAC;QACjD,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;QACtB,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IAC5B,CAAC;IAEO,IAAI,CAAC,KAAc;QAC1B,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YACjB,OAAO;QACR,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACnB,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;QACxB,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QAC9B,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;QACtB,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;QAChC,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IAC5B,CAAC;CACD"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Receives decoded values.
3
+ *
4
+ * A plain callback rather than an event: a link has exactly one consumer (the
5
+ * session above it) and this is the hottest path in the stack, so the
6
+ * dispatcher machinery is not worth its per-value cost here. Lifecycle
7
+ * concerns, which are rare and may have many listeners, remain events.
8
+ *
9
+ * @param value The decoded value.
10
+ * @param body Present when the codec declared a streamed payload. The consumer
11
+ * must drain it; until it does, the transport stays paused.
12
+ */
13
+ export type FrameSink<In> = (value: In, body?: ReadableStream<Uint8Array>) => void;
14
+ //# sourceMappingURL=frame-sink.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"frame-sink.d.ts","sourceRoot":"","sources":["../src/frame-sink.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,SAAS,CAAC,EAAE,IAAI,CAC3B,KAAK,EAAE,EAAE,EACT,IAAI,CAAC,EAAE,cAAc,CAAC,UAAU,CAAC,KAC7B,IAAI,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=frame-sink.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"frame-sink.js","sourceRoot":"","sources":["../src/frame-sink.ts"],"names":[],"mappings":""}
@@ -0,0 +1,9 @@
1
+ export * from "./exchange-registry.js";
2
+ export * from "./frame-link.js";
3
+ export * from "./frame-sink.js";
4
+ export * from "./keyed-registry.js";
5
+ export * from "./pending-exchange.js";
6
+ export * from "./scanning-registry.js";
7
+ export * from "./session.js";
8
+ export * from "./transport.js";
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ export * from "./exchange-registry.js";
2
+ export * from "./frame-link.js";
3
+ export * from "./frame-sink.js";
4
+ export * from "./keyed-registry.js";
5
+ export * from "./pending-exchange.js";
6
+ export * from "./scanning-registry.js";
7
+ export * from "./session.js";
8
+ export * from "./transport.js";
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC"}
@@ -0,0 +1,43 @@
1
+ import { ExchangeRegistry } from "./exchange-registry.js";
2
+ import { PendingExchange } from "./pending-exchange.js";
3
+ export type KeyedRegistryOptions<In, Key> = {
4
+ /**
5
+ * Extracts the correlation key carried by an inbound frame.
6
+ *
7
+ * Returning `undefined` marks the frame as uncorrelated — a server push, a
8
+ * heartbeat, an event notification — so it is surfaced as unsolicited rather
9
+ * than mis-attributed to a waiting request.
10
+ */
11
+ keyOf: (frame: In) => Key | undefined;
12
+ };
13
+ /**
14
+ * Key-based {@link ExchangeRegistry} for protocols that answer out of order.
15
+ *
16
+ * MQTT packet identifiers, JSON-RPC `id`s, DNS transaction identifiers, RTMP
17
+ * transaction identifiers and AMQP channel identifiers all correlate this way,
18
+ * and none of them can be served by an order-based registry: a response to the
19
+ * third request may legitimately arrive first.
20
+ *
21
+ * @typeParam In - Inbound frame type.
22
+ * @typeParam Key - Correlation key type, anything usable as a `Map` key.
23
+ */
24
+ export declare class KeyedRegistry<In, Key> implements ExchangeRegistry<In, Key> {
25
+ private readonly pending;
26
+ private readonly keys;
27
+ private readonly keyOf;
28
+ constructor(options: KeyedRegistryOptions<In, Key>);
29
+ get size(): number;
30
+ /**
31
+ * Track an exchange under the key the peer will echo back.
32
+ *
33
+ * @param exchange The exchange awaiting a response.
34
+ * @param key The correlation key.
35
+ * @throws {TypeError} If no key was supplied, since positional matching is
36
+ * not meaningful for a protocol that answers out of order.
37
+ */
38
+ register(exchange: PendingExchange<In>, key?: Key): void;
39
+ offer(frame: In, body?: ReadableStream<Uint8Array>): boolean;
40
+ remove(exchange: PendingExchange<In>): void;
41
+ rejectAll(error: Error): void;
42
+ }
43
+ //# sourceMappingURL=keyed-registry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keyed-registry.d.ts","sourceRoot":"","sources":["../src/keyed-registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,MAAM,MAAM,oBAAoB,CAAC,EAAE,EAAE,GAAG,IAAI;IAC3C;;;;;;OAMG;IACH,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,KAAK,GAAG,GAAG,SAAS,CAAC;CACtC,CAAC;AAEF;;;;;;;;;;GAUG;AACH,qBAAa,aAAa,CAAC,EAAE,EAAE,GAAG,CAAE,YAAW,gBAAgB,CAAC,EAAE,EAAE,GAAG,CAAC;IACvE,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAuC;IAC/D,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAuC;IAC5D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAiC;IAEvD,YAAY,OAAO,EAAE,oBAAoB,CAAC,EAAE,EAAE,GAAG,CAAC,EAEjD;IAED,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;;;;;;OAOG;IACH,QAAQ,CAAC,QAAQ,EAAE,eAAe,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,GAAG,GAAG,IAAI,CAQvD;IAED,KAAK,CAAC,KAAK,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,cAAc,CAAC,UAAU,CAAC,GAAG,OAAO,CA0B3D;IAED,MAAM,CAAC,QAAQ,EAAE,eAAe,CAAC,EAAE,CAAC,GAAG,IAAI,CAO1C;IAED,SAAS,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAO5B;CACD"}
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Key-based {@link ExchangeRegistry} for protocols that answer out of order.
3
+ *
4
+ * MQTT packet identifiers, JSON-RPC `id`s, DNS transaction identifiers, RTMP
5
+ * transaction identifiers and AMQP channel identifiers all correlate this way,
6
+ * and none of them can be served by an order-based registry: a response to the
7
+ * third request may legitimately arrive first.
8
+ *
9
+ * @typeParam In - Inbound frame type.
10
+ * @typeParam Key - Correlation key type, anything usable as a `Map` key.
11
+ */
12
+ export class KeyedRegistry {
13
+ pending = new Map();
14
+ keys = new Map();
15
+ keyOf;
16
+ constructor(options) {
17
+ this.keyOf = options.keyOf;
18
+ }
19
+ get size() {
20
+ return this.pending.size;
21
+ }
22
+ /**
23
+ * Track an exchange under the key the peer will echo back.
24
+ *
25
+ * @param exchange The exchange awaiting a response.
26
+ * @param key The correlation key.
27
+ * @throws {TypeError} If no key was supplied, since positional matching is
28
+ * not meaningful for a protocol that answers out of order.
29
+ */
30
+ register(exchange, key) {
31
+ if (key === undefined) {
32
+ throw new TypeError("KeyedRegistry requires a correlation key; pass options.key");
33
+ }
34
+ this.pending.set(key, exchange);
35
+ this.keys.set(exchange, key);
36
+ }
37
+ offer(frame, body) {
38
+ const key = this.keyOf(frame);
39
+ if (key === undefined) {
40
+ return false;
41
+ }
42
+ const exchange = this.pending.get(key);
43
+ if (!exchange) {
44
+ return false;
45
+ }
46
+ const disposition = exchange.accept(frame);
47
+ if (disposition === "skip") {
48
+ return false;
49
+ }
50
+ exchange.frames.push(frame);
51
+ if (body && exchange.onBody) {
52
+ exchange.onBody(frame, body);
53
+ }
54
+ if (disposition === "complete") {
55
+ this.pending.delete(key);
56
+ this.keys.delete(exchange);
57
+ exchange.complete();
58
+ }
59
+ return true;
60
+ }
61
+ remove(exchange) {
62
+ const key = this.keys.get(exchange);
63
+ if (key === undefined) {
64
+ return;
65
+ }
66
+ this.keys.delete(exchange);
67
+ this.pending.delete(key);
68
+ }
69
+ rejectAll(error) {
70
+ const outstanding = [...this.pending.values()];
71
+ this.pending.clear();
72
+ this.keys.clear();
73
+ for (const exchange of outstanding) {
74
+ exchange.fail(error);
75
+ }
76
+ }
77
+ }
78
+ //# sourceMappingURL=keyed-registry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"key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,IAAI,IAAI,IAAI,QAAQ,CAAC,MAAM,EAAE,CAAC;YAC7B,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAC9B,CAAC;QACD,IAAI,WAAW,KAAK,UAAU,EAAE,CAAC;YAChC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YACzB,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YAC3B,QAAQ,CAAC,QAAQ,EAAE,CAAC;QACrB,CAAC;QACD,OAAO,IAAI,CAAC;IACb,CAAC;IAED,MAAM,CAAC,QAA6B;QACnC,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACpC,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;YACvB,OAAO;QACR,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC3B,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;IAED,SAAS,CAAC,KAAY;QACrB,MAAM,WAAW,GAAG,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;QAC/C,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACrB,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;QAClB,KAAK,MAAM,QAAQ,IAAI,WAAW,EAAE,CAAC;YACpC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC;IACF,CAAC;CACD"}
@@ -0,0 +1,49 @@
1
+ /**
2
+ * How a pending exchange classifies an inbound frame.
3
+ *
4
+ * - `accumulate` — the frame belongs to this exchange; keep waiting.
5
+ * - `complete` — the frame belongs to this exchange and ends it.
6
+ * - `skip` — the frame is not part of this exchange; offer it to the next
7
+ * candidate, or fall through to unsolicited handling.
8
+ *
9
+ * `skip` is what makes uncorrelated protocols expressible. IRC interleaves
10
+ * `PRIVMSG` and `PING` with the numeric replies to whatever command is
11
+ * outstanding, and only the exchange itself knows which is which.
12
+ */
13
+ export type Disposition = "accumulate" | "complete" | "skip";
14
+ /** Classifies inbound frames for one pending exchange. */
15
+ export type ExchangeAccept<In> = (frame: In) => Disposition;
16
+ /**
17
+ * One outstanding request awaiting its response frames.
18
+ *
19
+ * Created by a session and held by an {@link ExchangeRegistry} until it
20
+ * completes, is abandoned, or the connection fails.
21
+ */
22
+ export declare class PendingExchange<In> {
23
+ readonly accept: ExchangeAccept<In>;
24
+ private readonly resolve;
25
+ private readonly reject;
26
+ readonly onBody?: ((frame: In, body: ReadableStream<Uint8Array>) => void) | undefined;
27
+ /** Frames accepted so far, in arrival order. */
28
+ readonly frames: In[];
29
+ /**
30
+ * Set when the caller abandoned the exchange under the `discard` policy.
31
+ *
32
+ * The exchange stays registered so it can absorb the response that is still
33
+ * in flight, but its result is thrown away instead of delivered.
34
+ */
35
+ orphaned: boolean;
36
+ private settled;
37
+ constructor(accept: ExchangeAccept<In>, resolve: (frames: In[]) => void, reject: (error: Error) => void, onBody?: ((frame: In, body: ReadableStream<Uint8Array>) => void) | undefined);
38
+ /** Whether this exchange has already resolved or rejected. */
39
+ get isSettled(): boolean;
40
+ /** Deliver the collected frames, unless the exchange was orphaned. */
41
+ complete(): void;
42
+ /**
43
+ * Fail the exchange.
44
+ *
45
+ * @param error The reason to reject with.
46
+ */
47
+ fail(error: Error): void;
48
+ }
49
+ //# sourceMappingURL=pending-exchange.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pending-exchange.d.ts","sourceRoot":"","sources":["../src/pending-exchange.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,WAAW,GAAG,YAAY,GAAG,UAAU,GAAG,MAAM,CAAC;AAE7D,0DAA0D;AAC1D,MAAM,MAAM,cAAc,CAAC,EAAE,IAAI,CAAC,KAAK,EAAE,EAAE,KAAK,WAAW,CAAC;AAE5D;;;;;GAKG;AACH,qBAAa,eAAe,CAAC,EAAE;IAe7B,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC,EAAE,CAAC;IACnC,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,QAAQ,CAAC,MAAM,CAAC,GAAE,CAAC,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,KAAK,IAAI;IAjBxE,gDAAgD;IAChD,QAAQ,CAAC,MAAM,EAAE,EAAE,EAAE,CAAM;IAE3B;;;;;OAKG;IACH,QAAQ,UAAS;IAEjB,OAAO,CAAC,OAAO,CAAS;IAExB,YACU,MAAM,EAAE,cAAc,CAAC,EAAE,CAAC,EAClB,OAAO,EAAE,CAAC,MAAM,EAAE,EAAE,EAAE,KAAK,IAAI,EAC/B,MAAM,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,EACtC,MAAM,CAAC,GAAE,CAAC,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,KAAK,IAAI,aAAA,EACpE;IAEJ,8DAA8D;IAC9D,IAAI,SAAS,IAAI,OAAO,CAEvB;IAED,sEAAsE;IACtE,QAAQ,IAAI,IAAI,CAQf;IAED;;;;OAIG;IACH,IAAI,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAQvB;CACD"}
@@ -0,0 +1,57 @@
1
+ /**
2
+ * One outstanding request awaiting its response frames.
3
+ *
4
+ * Created by a session and held by an {@link ExchangeRegistry} until it
5
+ * completes, is abandoned, or the connection fails.
6
+ */
7
+ export class PendingExchange {
8
+ accept;
9
+ resolve;
10
+ reject;
11
+ onBody;
12
+ /** Frames accepted so far, in arrival order. */
13
+ frames = [];
14
+ /**
15
+ * Set when the caller abandoned the exchange under the `discard` policy.
16
+ *
17
+ * The exchange stays registered so it can absorb the response that is still
18
+ * in flight, but its result is thrown away instead of delivered.
19
+ */
20
+ orphaned = false;
21
+ settled = false;
22
+ constructor(accept, resolve, reject, onBody) {
23
+ this.accept = accept;
24
+ this.resolve = resolve;
25
+ this.reject = reject;
26
+ this.onBody = onBody;
27
+ }
28
+ /** Whether this exchange has already resolved or rejected. */
29
+ get isSettled() {
30
+ return this.settled;
31
+ }
32
+ /** Deliver the collected frames, unless the exchange was orphaned. */
33
+ complete() {
34
+ if (this.settled) {
35
+ return;
36
+ }
37
+ this.settled = true;
38
+ if (!this.orphaned) {
39
+ this.resolve(this.frames);
40
+ }
41
+ }
42
+ /**
43
+ * Fail the exchange.
44
+ *
45
+ * @param error The reason to reject with.
46
+ */
47
+ fail(error) {
48
+ if (this.settled) {
49
+ return;
50
+ }
51
+ this.settled = true;
52
+ if (!this.orphaned) {
53
+ this.reject(error);
54
+ }
55
+ }
56
+ }
57
+ //# sourceMappingURL=pending-exchange.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pending-exchange.js","sourceRoot":"","sources":["../src/pending-exchange.ts"],"names":[],"mappings":"AAiBA;;;;;GAKG;AACH,MAAM,OAAO,eAAe;IAejB,MAAM;IACE,OAAO;IACP,MAAM;IACd,MAAM;IAjBhB,gDAAgD;IACvC,MAAM,GAAS,EAAE,CAAC;IAE3B;;;;;OAKG;IACH,QAAQ,GAAG,KAAK,CAAC;IAET,OAAO,GAAG,KAAK,CAAC;IAExB,YACU,MAA0B,EAClB,OAA+B,EAC/B,MAA8B,EACtC,MAA8D;sBAH9D,MAAM;uBACE,OAAO;sBACP,MAAM;sBACd,MAAM;IACb,CAAC;IAEJ,8DAA8D;IAC9D,IAAI,SAAS;QACZ,OAAO,IAAI,CAAC,OAAO,CAAC;IACrB,CAAC;IAED,sEAAsE;IACtE,QAAQ;QACP,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YAClB,OAAO;QACR,CAAC;QACD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACpB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC3B,CAAC;IACF,CAAC;IAED;;;;OAIG;IACH,IAAI,CAAC,KAAY;QAChB,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YAClB,OAAO;QACR,CAAC;QACD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACpB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACpB,CAAC;IACF,CAAC;CACD"}