@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.
- package/LICENSE +28 -0
- package/README.md +40 -0
- package/dist/exchange-registry.d.ts +94 -0
- package/dist/exchange-registry.d.ts.map +1 -0
- package/dist/exchange-registry.js +2 -0
- package/dist/exchange-registry.js.map +1 -0
- package/dist/frame-link.d.ts +181 -0
- package/dist/frame-link.d.ts.map +1 -0
- package/dist/frame-link.js +352 -0
- package/dist/frame-link.js.map +1 -0
- package/dist/frame-sink.d.ts +14 -0
- package/dist/frame-sink.d.ts.map +1 -0
- package/dist/frame-sink.js +2 -0
- package/dist/frame-sink.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/keyed-registry.d.ts +43 -0
- package/dist/keyed-registry.d.ts.map +1 -0
- package/dist/keyed-registry.js +78 -0
- package/dist/keyed-registry.js.map +1 -0
- package/dist/pending-exchange.d.ts +49 -0
- package/dist/pending-exchange.d.ts.map +1 -0
- package/dist/pending-exchange.js +57 -0
- package/dist/pending-exchange.js.map +1 -0
- package/dist/scanning-registry.d.ts +40 -0
- package/dist/scanning-registry.d.ts.map +1 -0
- package/dist/scanning-registry.js +57 -0
- package/dist/scanning-registry.js.map +1 -0
- package/dist/session.d.ts +169 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +220 -0
- package/dist/session.js.map +1 -0
- package/dist/transport.d.ts +103 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +2 -0
- package/dist/transport.js.map +1 -0
- package/package.json +65 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ExchangeRegistry } from "./exchange-registry.js";
|
|
2
|
+
import { PendingExchange } from "./pending-exchange.js";
|
|
3
|
+
export interface ScanningRegistryOptions {
|
|
4
|
+
/**
|
|
5
|
+
* How many outstanding exchanges an inbound frame may be offered to before it
|
|
6
|
+
* is treated as unsolicited.
|
|
7
|
+
*
|
|
8
|
+
* - `1` — strict FIFO. Correct for protocols where responses are guaranteed to
|
|
9
|
+
* arrive in request order: SMTP, LMTP, POP3, Postfix socketmap.
|
|
10
|
+
* - `Infinity` — opportunistic. Correct for protocols where several commands
|
|
11
|
+
* may be outstanding with no correlation key and unrelated traffic can
|
|
12
|
+
* arrive between them, such as IRC.
|
|
13
|
+
*
|
|
14
|
+
* Defaults to `1`, the conservative choice: a frame that no exchange should
|
|
15
|
+
* have claimed is better surfaced as unsolicited than mis-attributed.
|
|
16
|
+
*/
|
|
17
|
+
maxDepth?: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Order-based {@link ExchangeRegistry}.
|
|
21
|
+
*
|
|
22
|
+
* Offers each inbound frame to outstanding exchanges from oldest to newest,
|
|
23
|
+
* stopping at the first that does not return `skip`, and giving up after
|
|
24
|
+
* {@link ScanningRegistryOptions.maxDepth} candidates.
|
|
25
|
+
*
|
|
26
|
+
* Strict FIFO is the depth-1 case rather than a separate implementation, which
|
|
27
|
+
* keeps the two behaviours from drifting apart.
|
|
28
|
+
*/
|
|
29
|
+
export declare class ScanningRegistry<In> implements ExchangeRegistry<In, never> {
|
|
30
|
+
private readonly pending;
|
|
31
|
+
private readonly maxDepth;
|
|
32
|
+
constructor(options?: ScanningRegistryOptions);
|
|
33
|
+
get size(): number;
|
|
34
|
+
/** Correlation is positional here, so any supplied key is ignored. */
|
|
35
|
+
register(exchange: PendingExchange<In>): void;
|
|
36
|
+
offer(frame: In, body?: ReadableStream<Uint8Array>): boolean;
|
|
37
|
+
remove(exchange: PendingExchange<In>): void;
|
|
38
|
+
rejectAll(error: Error): void;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=scanning-registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scanning-registry.d.ts","sourceRoot":"","sources":["../src/scanning-registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,MAAM,WAAW,uBAAuB;IACvC;;;;;;;;;;;;OAYG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;;GASG;AACH,qBAAa,gBAAgB,CAAC,EAAE,CAAE,YAAW,gBAAgB,CAAC,EAAE,EAAE,KAAK,CAAC;IACvE,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAkC;IAC1D,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAElC,YAAY,OAAO,CAAC,EAAE,uBAAuB,EAE5C;IAED,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,sEAAsE;IACtE,QAAQ,CAAC,QAAQ,EAAE,eAAe,CAAC,EAAE,CAAC,GAAG,IAAI,CAE5C;IAED,KAAK,CAAC,KAAK,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,cAAc,CAAC,UAAU,CAAC,GAAG,OAAO,CAuB3D;IAED,MAAM,CAAC,QAAQ,EAAE,eAAe,CAAC,EAAE,CAAC,GAAG,IAAI,CAK1C;IAED,SAAS,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAK5B;CACD"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Order-based {@link ExchangeRegistry}.
|
|
3
|
+
*
|
|
4
|
+
* Offers each inbound frame to outstanding exchanges from oldest to newest,
|
|
5
|
+
* stopping at the first that does not return `skip`, and giving up after
|
|
6
|
+
* {@link ScanningRegistryOptions.maxDepth} candidates.
|
|
7
|
+
*
|
|
8
|
+
* Strict FIFO is the depth-1 case rather than a separate implementation, which
|
|
9
|
+
* keeps the two behaviours from drifting apart.
|
|
10
|
+
*/
|
|
11
|
+
export class ScanningRegistry {
|
|
12
|
+
pending = [];
|
|
13
|
+
maxDepth;
|
|
14
|
+
constructor(options) {
|
|
15
|
+
this.maxDepth = options?.maxDepth ?? 1;
|
|
16
|
+
}
|
|
17
|
+
get size() {
|
|
18
|
+
return this.pending.length;
|
|
19
|
+
}
|
|
20
|
+
/** Correlation is positional here, so any supplied key is ignored. */
|
|
21
|
+
register(exchange) {
|
|
22
|
+
this.pending.push(exchange);
|
|
23
|
+
}
|
|
24
|
+
offer(frame, body) {
|
|
25
|
+
const limit = this.maxDepth < this.pending.length ? this.maxDepth : this.pending.length;
|
|
26
|
+
for (let index = 0; index < limit; index++) {
|
|
27
|
+
const exchange = this.pending[index];
|
|
28
|
+
const disposition = exchange.accept(frame);
|
|
29
|
+
if (disposition === "skip") {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
exchange.frames.push(frame);
|
|
33
|
+
if (body && exchange.onBody) {
|
|
34
|
+
exchange.onBody(frame, body);
|
|
35
|
+
}
|
|
36
|
+
if (disposition === "complete") {
|
|
37
|
+
this.pending.splice(index, 1);
|
|
38
|
+
exchange.complete();
|
|
39
|
+
}
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
remove(exchange) {
|
|
45
|
+
const index = this.pending.indexOf(exchange);
|
|
46
|
+
if (index !== -1) {
|
|
47
|
+
this.pending.splice(index, 1);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
rejectAll(error) {
|
|
51
|
+
const outstanding = this.pending.splice(0);
|
|
52
|
+
for (const exchange of outstanding) {
|
|
53
|
+
exchange.fail(error);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=scanning-registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scanning-registry.js","sourceRoot":"","sources":["../src/scanning-registry.ts"],"names":[],"mappings":"AAoBA;;;;;;;;;GASG;AACH,MAAM,OAAO,gBAAgB;IACX,OAAO,GAA+B,EAAE,CAAC;IACzC,QAAQ,CAAS;IAElC,YAAY,OAAiC;QA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AAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QAC7C,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;YAClB,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;QAC/B,CAAC;IACF,CAAC;IAED,SAAS,CAAC,KAAY;QACrB,MAAM,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QAC3C,KAAK,MAAM,QAAQ,IAAI,WAAW,EAAE,CAAC;YACpC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC;IACF,CAAC;CACD"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { EventDispatcherMapBase } from "@ac-kit/async";
|
|
2
|
+
import type { Codec } from "@ac-kit/format-core";
|
|
3
|
+
import { type ExchangeOptions, type ExchangeRegistry } from "./exchange-registry.js";
|
|
4
|
+
import { ExchangeAccept } from "./pending-exchange.js";
|
|
5
|
+
import type { Transport } from "./transport.js";
|
|
6
|
+
/** Events every session emits, regardless of protocol. */
|
|
7
|
+
export type BaseSessionEvents = {
|
|
8
|
+
/** A recoverable protocol violation. Outstanding exchanges are unaffected. */
|
|
9
|
+
protocolError: [error: unknown];
|
|
10
|
+
/** A fatal error. Outstanding exchanges have already been rejected. */
|
|
11
|
+
error: [error: unknown];
|
|
12
|
+
/** The peer half-closed. */
|
|
13
|
+
end: [];
|
|
14
|
+
/** The connection is fully closed. */
|
|
15
|
+
close: [];
|
|
16
|
+
};
|
|
17
|
+
/** Construction options for {@link Session}. */
|
|
18
|
+
export type SessionOptions<In, Out, Key = unknown> = {
|
|
19
|
+
/** Carrier for the framed bytes. */
|
|
20
|
+
transport: Transport;
|
|
21
|
+
/** Decoder and encoder for this protocol. */
|
|
22
|
+
codec: Codec<In, Out>;
|
|
23
|
+
/**
|
|
24
|
+
* Hard ceiling, in bytes, on undecoded received data. Required; see
|
|
25
|
+
* {@link FrameLinkOptions.maxBufferSize}.
|
|
26
|
+
*/
|
|
27
|
+
maxBufferSize: number;
|
|
28
|
+
/** Initial receive arena size. Defaults to 8 KiB. */
|
|
29
|
+
initialCapacity?: number;
|
|
30
|
+
/**
|
|
31
|
+
* Correlation strategy for inbound frames. Defaults to strict FIFO, which
|
|
32
|
+
* suits ordered text protocols; supply a {@link KeyedRegistry} for protocols
|
|
33
|
+
* that answer out of order, or a wider {@link ScanningRegistry} for
|
|
34
|
+
* uncorrelated ones.
|
|
35
|
+
*/
|
|
36
|
+
registry?: ExchangeRegistry<In, Key>;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Peer-symmetric protocol session.
|
|
40
|
+
*
|
|
41
|
+
* Owns a {@link FrameLink}, an {@link ExchangeRegistry} for requests this side
|
|
42
|
+
* has issued, and the lifecycle that ties them together. There is no
|
|
43
|
+
* client/server type split: many protocols — RTMP, AMQP, MQTT, WebSocket, IRC —
|
|
44
|
+
* have both peers issuing and answering requests, so a client is simply a
|
|
45
|
+
* session that registers no inbound handlers and a server one that never issues
|
|
46
|
+
* requests.
|
|
47
|
+
*
|
|
48
|
+
* Subclasses implement {@link routeFrame} to decide, per frame, whether it
|
|
49
|
+
* belongs to an outstanding exchange, is an unsolicited server message, or
|
|
50
|
+
* both.
|
|
51
|
+
*
|
|
52
|
+
* @typeParam In - Inbound frame type.
|
|
53
|
+
* @typeParam Out - Outbound frame type.
|
|
54
|
+
* @typeParam Events - Protocol-specific event map, extending
|
|
55
|
+
* {@link BaseSessionEvents}.
|
|
56
|
+
*/
|
|
57
|
+
export declare abstract class Session<In, Out, Events extends BaseSessionEvents, Key = unknown> extends EventDispatcherMapBase<Events> {
|
|
58
|
+
private readonly link;
|
|
59
|
+
private readonly registry;
|
|
60
|
+
protected constructor(options: SessionOptions<In, Out, Key>);
|
|
61
|
+
/**
|
|
62
|
+
* Route one decoded frame.
|
|
63
|
+
*
|
|
64
|
+
* Called for every inbound frame in arrival order. A typical implementation
|
|
65
|
+
* calls {@link offer} and, when that returns `false`, dispatches the frame as
|
|
66
|
+
* an unsolicited protocol event.
|
|
67
|
+
*
|
|
68
|
+
* @param frame The decoded frame.
|
|
69
|
+
* @param body Streamed payload declared by the codec, if any.
|
|
70
|
+
*/
|
|
71
|
+
protected abstract routeFrame(frame: In, body?: ReadableStream<Uint8Array>): void;
|
|
72
|
+
/**
|
|
73
|
+
* Offer a frame to the outstanding exchanges.
|
|
74
|
+
*
|
|
75
|
+
* @param frame The decoded frame.
|
|
76
|
+
* @param body Streamed payload declared by the codec, if any.
|
|
77
|
+
* @returns `true` if an exchange claimed it.
|
|
78
|
+
*/
|
|
79
|
+
protected offer(frame: In, body?: ReadableStream<Uint8Array>): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Send a frame and collect the response frames it elicits.
|
|
82
|
+
*
|
|
83
|
+
* The exchange is registered before the write is issued, so a response cannot
|
|
84
|
+
* arrive in the window between sending and waiting.
|
|
85
|
+
*
|
|
86
|
+
* @param frame The request frame.
|
|
87
|
+
* @param accept Classifies inbound frames as part of this exchange.
|
|
88
|
+
* @param options Abort, timeout, abort policy and body delivery.
|
|
89
|
+
* @returns The frames the exchange accepted, in arrival order.
|
|
90
|
+
*/
|
|
91
|
+
protected request(frame: Out, accept: ExchangeAccept<In>, options?: ExchangeOptions<In, Key>): Promise<In[]>;
|
|
92
|
+
/**
|
|
93
|
+
* Register an exchange without sending anything.
|
|
94
|
+
*
|
|
95
|
+
* Needed when the request and its response are separated by raw traffic, as
|
|
96
|
+
* in an SMTP `DATA` body or an LMTP per-recipient response burst.
|
|
97
|
+
*
|
|
98
|
+
* @param accept Classifies inbound frames as part of this exchange.
|
|
99
|
+
* @param options Abort, timeout, abort policy and body delivery.
|
|
100
|
+
* @returns The frames the exchange accepted, in arrival order.
|
|
101
|
+
*/
|
|
102
|
+
protected collect(accept: ExchangeAccept<In>, options?: ExchangeOptions<In, Key>): Promise<In[]>;
|
|
103
|
+
/**
|
|
104
|
+
* Encode and send one frame without awaiting a response.
|
|
105
|
+
*
|
|
106
|
+
* @param frame The frame to transmit.
|
|
107
|
+
* @param signal Cancels the wait for transport capacity.
|
|
108
|
+
*/
|
|
109
|
+
protected send(frame: Out, signal?: AbortSignal): Promise<void>;
|
|
110
|
+
/**
|
|
111
|
+
* Send raw bytes, bypassing the encoder.
|
|
112
|
+
*
|
|
113
|
+
* @param data One or more buffers, written as a single vectored write.
|
|
114
|
+
* @param signal Cancels the wait for transport capacity.
|
|
115
|
+
*/
|
|
116
|
+
protected sendRaw(data: Uint8Array | readonly Uint8Array[], signal?: AbortSignal): Promise<void>;
|
|
117
|
+
/** Discard undecoded bytes and decoder state. */
|
|
118
|
+
protected reset(): void;
|
|
119
|
+
/**
|
|
120
|
+
* Replace the transport, for example after negotiating TLS.
|
|
121
|
+
*
|
|
122
|
+
* @param transport The transport to use from now on.
|
|
123
|
+
* @param options `keepResidue` retains undecoded bytes; leave it unset for
|
|
124
|
+
* STARTTLS, where buffered plaintext must be rejected.
|
|
125
|
+
*/
|
|
126
|
+
protected swapTransport(transport: Transport, options?: {
|
|
127
|
+
keepResidue?: boolean;
|
|
128
|
+
}): void;
|
|
129
|
+
/**
|
|
130
|
+
* Take the undecoded bytes, for handing to another protocol after an upgrade.
|
|
131
|
+
*
|
|
132
|
+
* @returns A copy of the bytes received but not yet decoded.
|
|
133
|
+
*/
|
|
134
|
+
protected takeResidue(): Uint8Array;
|
|
135
|
+
/** Bytes received but not yet decoded. */
|
|
136
|
+
get buffered(): number;
|
|
137
|
+
/** Number of outstanding exchanges, including abort tombstones. */
|
|
138
|
+
get pendingExchanges(): number;
|
|
139
|
+
/**
|
|
140
|
+
* Half-close the outbound direction and flush pending writes.
|
|
141
|
+
*
|
|
142
|
+
* Prefer this over {@link destroy} for protocols with a farewell command, so
|
|
143
|
+
* the peer sees an orderly shutdown.
|
|
144
|
+
*/
|
|
145
|
+
end(): Promise<void>;
|
|
146
|
+
/**
|
|
147
|
+
* Tear the session down, rejecting every outstanding exchange.
|
|
148
|
+
*
|
|
149
|
+
* Rejection happens here rather than relying on the transport emitting
|
|
150
|
+
* `close`, so callers are never left with promises that hang against a
|
|
151
|
+
* transport that closes silently.
|
|
152
|
+
*
|
|
153
|
+
* @param error Optional cause reported to outstanding exchanges.
|
|
154
|
+
*/
|
|
155
|
+
destroy(error?: unknown): void;
|
|
156
|
+
/** Destroys the session, so it can be used with `await using`. */
|
|
157
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
158
|
+
/**
|
|
159
|
+
* Builds an exchange and wires abort, timeout and abort-policy handling.
|
|
160
|
+
*
|
|
161
|
+
* Centralised so the three call sites cannot drift apart, which is where
|
|
162
|
+
* subtle abort bugs breed.
|
|
163
|
+
*/
|
|
164
|
+
private createExchange;
|
|
165
|
+
private combineSignals;
|
|
166
|
+
/** Narrow dispatch helper for the events declared on the base map. */
|
|
167
|
+
private dispatchBase;
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAEjD,OAAO,EACN,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAAE,cAAc,EAAmB,MAAM,uBAAuB,CAAC;AAExE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD,0DAA0D;AAC1D,MAAM,MAAM,iBAAiB,GAAG;IAC/B,8EAA8E;IAC9E,aAAa,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAEhC,uEAAuE;IACvE,KAAK,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAExB,4BAA4B;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sE;IACtE,OAAO,CAAC,YAAY;CASpB"}
|
package/dist/session.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { EventDispatcherMapBase } from "@ac-kit/async";
|
|
2
|
+
import { FrameLink } from "./frame-link.js";
|
|
3
|
+
import { PendingExchange } from "./pending-exchange.js";
|
|
4
|
+
import { ScanningRegistry } from "./scanning-registry.js";
|
|
5
|
+
/**
|
|
6
|
+
* Peer-symmetric protocol session.
|
|
7
|
+
*
|
|
8
|
+
* Owns a {@link FrameLink}, an {@link ExchangeRegistry} for requests this side
|
|
9
|
+
* has issued, and the lifecycle that ties them together. There is no
|
|
10
|
+
* client/server type split: many protocols — RTMP, AMQP, MQTT, WebSocket, IRC —
|
|
11
|
+
* have both peers issuing and answering requests, so a client is simply a
|
|
12
|
+
* session that registers no inbound handlers and a server one that never issues
|
|
13
|
+
* requests.
|
|
14
|
+
*
|
|
15
|
+
* Subclasses implement {@link routeFrame} to decide, per frame, whether it
|
|
16
|
+
* belongs to an outstanding exchange, is an unsolicited server message, or
|
|
17
|
+
* both.
|
|
18
|
+
*
|
|
19
|
+
* @typeParam In - Inbound frame type.
|
|
20
|
+
* @typeParam Out - Outbound frame type.
|
|
21
|
+
* @typeParam Events - Protocol-specific event map, extending
|
|
22
|
+
* {@link BaseSessionEvents}.
|
|
23
|
+
*/
|
|
24
|
+
export class Session extends EventDispatcherMapBase {
|
|
25
|
+
link;
|
|
26
|
+
registry;
|
|
27
|
+
constructor(options) {
|
|
28
|
+
super();
|
|
29
|
+
this.registry = options.registry ?? new ScanningRegistry();
|
|
30
|
+
this.link = new FrameLink({
|
|
31
|
+
transport: options.transport,
|
|
32
|
+
codec: options.codec,
|
|
33
|
+
maxBufferSize: options.maxBufferSize,
|
|
34
|
+
initialCapacity: options.initialCapacity,
|
|
35
|
+
sink: (frame, body) => this.routeFrame(frame, body),
|
|
36
|
+
});
|
|
37
|
+
this.link.subscribe("protocolError", (error) => {
|
|
38
|
+
this.dispatchBase("protocolError", error);
|
|
39
|
+
});
|
|
40
|
+
this.link.subscribe("error", (error) => {
|
|
41
|
+
this.registry.rejectAll(error);
|
|
42
|
+
this.dispatchBase("error", error);
|
|
43
|
+
});
|
|
44
|
+
this.link.subscribe("end", () => {
|
|
45
|
+
this.dispatchBase("end");
|
|
46
|
+
});
|
|
47
|
+
this.link.subscribe("close", () => {
|
|
48
|
+
this.registry.rejectAll(new Error("Connection closed"));
|
|
49
|
+
this.dispatchBase("close");
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Offer a frame to the outstanding exchanges.
|
|
54
|
+
*
|
|
55
|
+
* @param frame The decoded frame.
|
|
56
|
+
* @param body Streamed payload declared by the codec, if any.
|
|
57
|
+
* @returns `true` if an exchange claimed it.
|
|
58
|
+
*/
|
|
59
|
+
offer(frame, body) {
|
|
60
|
+
return this.registry.offer(frame, body);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Send a frame and collect the response frames it elicits.
|
|
64
|
+
*
|
|
65
|
+
* The exchange is registered before the write is issued, so a response cannot
|
|
66
|
+
* arrive in the window between sending and waiting.
|
|
67
|
+
*
|
|
68
|
+
* @param frame The request frame.
|
|
69
|
+
* @param accept Classifies inbound frames as part of this exchange.
|
|
70
|
+
* @param options Abort, timeout, abort policy and body delivery.
|
|
71
|
+
* @returns The frames the exchange accepted, in arrival order.
|
|
72
|
+
*/
|
|
73
|
+
async request(frame, accept, options) {
|
|
74
|
+
const { exchange, promise } = this.createExchange(accept, options);
|
|
75
|
+
try {
|
|
76
|
+
await this.link.send(frame, options?.signal);
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
this.registry.remove(exchange);
|
|
80
|
+
exchange.fail(error);
|
|
81
|
+
}
|
|
82
|
+
return promise;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Register an exchange without sending anything.
|
|
86
|
+
*
|
|
87
|
+
* Needed when the request and its response are separated by raw traffic, as
|
|
88
|
+
* in an SMTP `DATA` body or an LMTP per-recipient response burst.
|
|
89
|
+
*
|
|
90
|
+
* @param accept Classifies inbound frames as part of this exchange.
|
|
91
|
+
* @param options Abort, timeout, abort policy and body delivery.
|
|
92
|
+
* @returns The frames the exchange accepted, in arrival order.
|
|
93
|
+
*/
|
|
94
|
+
collect(accept, options) {
|
|
95
|
+
return this.createExchange(accept, options).promise;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Encode and send one frame without awaiting a response.
|
|
99
|
+
*
|
|
100
|
+
* @param frame The frame to transmit.
|
|
101
|
+
* @param signal Cancels the wait for transport capacity.
|
|
102
|
+
*/
|
|
103
|
+
send(frame, signal) {
|
|
104
|
+
return this.link.send(frame, signal);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Send raw bytes, bypassing the encoder.
|
|
108
|
+
*
|
|
109
|
+
* @param data One or more buffers, written as a single vectored write.
|
|
110
|
+
* @param signal Cancels the wait for transport capacity.
|
|
111
|
+
*/
|
|
112
|
+
sendRaw(data, signal) {
|
|
113
|
+
return this.link.sendRaw(data, signal);
|
|
114
|
+
}
|
|
115
|
+
/** Discard undecoded bytes and decoder state. */
|
|
116
|
+
reset() {
|
|
117
|
+
this.link.reset();
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Replace the transport, for example after negotiating TLS.
|
|
121
|
+
*
|
|
122
|
+
* @param transport The transport to use from now on.
|
|
123
|
+
* @param options `keepResidue` retains undecoded bytes; leave it unset for
|
|
124
|
+
* STARTTLS, where buffered plaintext must be rejected.
|
|
125
|
+
*/
|
|
126
|
+
swapTransport(transport, options) {
|
|
127
|
+
this.link.swapTransport(transport, options);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Take the undecoded bytes, for handing to another protocol after an upgrade.
|
|
131
|
+
*
|
|
132
|
+
* @returns A copy of the bytes received but not yet decoded.
|
|
133
|
+
*/
|
|
134
|
+
takeResidue() {
|
|
135
|
+
return this.link.takeResidue();
|
|
136
|
+
}
|
|
137
|
+
/** Bytes received but not yet decoded. */
|
|
138
|
+
get buffered() {
|
|
139
|
+
return this.link.buffered;
|
|
140
|
+
}
|
|
141
|
+
/** Number of outstanding exchanges, including abort tombstones. */
|
|
142
|
+
get pendingExchanges() {
|
|
143
|
+
return this.registry.size;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Half-close the outbound direction and flush pending writes.
|
|
147
|
+
*
|
|
148
|
+
* Prefer this over {@link destroy} for protocols with a farewell command, so
|
|
149
|
+
* the peer sees an orderly shutdown.
|
|
150
|
+
*/
|
|
151
|
+
end() {
|
|
152
|
+
return this.link.end();
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Tear the session down, rejecting every outstanding exchange.
|
|
156
|
+
*
|
|
157
|
+
* Rejection happens here rather than relying on the transport emitting
|
|
158
|
+
* `close`, so callers are never left with promises that hang against a
|
|
159
|
+
* transport that closes silently.
|
|
160
|
+
*
|
|
161
|
+
* @param error Optional cause reported to outstanding exchanges.
|
|
162
|
+
*/
|
|
163
|
+
destroy(error) {
|
|
164
|
+
this.registry.rejectAll(error ?? new Error("Session destroyed"));
|
|
165
|
+
this.link.destroy(error);
|
|
166
|
+
}
|
|
167
|
+
/** Destroys the session, so it can be used with `await using`. */
|
|
168
|
+
async [Symbol.asyncDispose]() {
|
|
169
|
+
this.destroy();
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Builds an exchange and wires abort, timeout and abort-policy handling.
|
|
173
|
+
*
|
|
174
|
+
* Centralised so the three call sites cannot drift apart, which is where
|
|
175
|
+
* subtle abort bugs breed.
|
|
176
|
+
*/
|
|
177
|
+
createExchange(accept, options) {
|
|
178
|
+
const signal = this.combineSignals(options);
|
|
179
|
+
signal?.throwIfAborted();
|
|
180
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
181
|
+
const exchange = new PendingExchange(accept, resolve, reject, options?.onBody);
|
|
182
|
+
if (signal) {
|
|
183
|
+
const onAbort = () => {
|
|
184
|
+
const policy = options?.abortPolicy ?? "discard";
|
|
185
|
+
const error = new Error("Exchange aborted", {
|
|
186
|
+
cause: signal.reason,
|
|
187
|
+
});
|
|
188
|
+
if (policy === "discard") {
|
|
189
|
+
// Keep the exchange registered so the response still in flight is
|
|
190
|
+
// absorbed and the frame stream stays synchronised.
|
|
191
|
+
exchange.orphaned = true;
|
|
192
|
+
reject(error);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
this.registry.remove(exchange);
|
|
196
|
+
exchange.fail(error);
|
|
197
|
+
if (policy === "close") {
|
|
198
|
+
this.destroy(error);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
202
|
+
}
|
|
203
|
+
this.registry.register(exchange, options?.key);
|
|
204
|
+
return { exchange, promise };
|
|
205
|
+
}
|
|
206
|
+
combineSignals(options) {
|
|
207
|
+
if (options?.timeoutMs === undefined) {
|
|
208
|
+
return options?.signal;
|
|
209
|
+
}
|
|
210
|
+
const timeout = AbortSignal.timeout(options.timeoutMs);
|
|
211
|
+
return options.signal
|
|
212
|
+
? AbortSignal.any([options.signal, timeout])
|
|
213
|
+
: timeout;
|
|
214
|
+
}
|
|
215
|
+
/** Narrow dispatch helper for the events declared on the base map. */
|
|
216
|
+
dispatchBase(event, ...args) {
|
|
217
|
+
this.dispatch(event, ...args);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
//# sourceMappingURL=session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAOvD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAkB,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxE,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CA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,OAAO,CAAC,CAAC;YAC5C,CAAC,CAAC,OAAO,CAAC;IACZ,CAAC;IAED,sEAAsE;IAC9D,YAAY,CACnB,KAAQ,EACR,GAAG,IAA0B;QAE5B,IAAI,CAAC,QAA8D,CACnE,KAAK,EACL,GAAG,IAAI,CACP,CAAC;IACH,CAAC;CACD"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a transport delivers bytes.
|
|
3
|
+
*
|
|
4
|
+
* - `stream` — an ordered, reliable byte stream with no inherent message
|
|
5
|
+
* boundaries (TCP, TLS, Unix sockets, a QUIC stream). Received chunks are
|
|
6
|
+
* accumulated and a value may span any number of them.
|
|
7
|
+
* - `message` — discrete, self-delimiting messages (UDP datagrams). Each delivery
|
|
8
|
+
* is exactly one value; bytes are never carried across deliveries, and a
|
|
9
|
+
* message that does not decode completely is truncated rather than
|
|
10
|
+
* incomplete.
|
|
11
|
+
*
|
|
12
|
+
* The mode changes how a {@link FrameLink} treats an `incomplete` decode
|
|
13
|
+
* result.
|
|
14
|
+
*/
|
|
15
|
+
export type TransportMode = "stream" | "message";
|
|
16
|
+
/**
|
|
17
|
+
* Callbacks a {@link FrameLink} installs on a transport.
|
|
18
|
+
*
|
|
19
|
+
* These are plain function properties rather than events: the link has a single
|
|
20
|
+
* consumer, and this is the hottest path in the stack.
|
|
21
|
+
*/
|
|
22
|
+
export type TransportHandlers = {
|
|
23
|
+
/**
|
|
24
|
+
* One chunk (stream mode) or one complete message (message mode) arrived.
|
|
25
|
+
*
|
|
26
|
+
* The buffer is owned by the callee for the duration of the call only.
|
|
27
|
+
*/
|
|
28
|
+
data(chunk: Uint8Array): void;
|
|
29
|
+
/** The peer half-closed. No further {@link data} calls will occur. */
|
|
30
|
+
end(): void;
|
|
31
|
+
/** The transport is fully closed and no longer usable. */
|
|
32
|
+
close(): void;
|
|
33
|
+
/** The transport failed. Always terminal. */
|
|
34
|
+
error(error: unknown): void;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Abstraction over anything that can carry framed bytes.
|
|
38
|
+
*
|
|
39
|
+
* Must not assume a `Duplex`. HTTP/3 runs over QUIC streams, DNS runs over UDP
|
|
40
|
+
* datagrams, and DNS-over-HTTPS runs over the body of another session — none of
|
|
41
|
+
* which are sockets, and all of which reuse the same codecs and correlation
|
|
42
|
+
* logic.
|
|
43
|
+
*
|
|
44
|
+
* Kept as a bespoke contract rather than a bare `ReadableStream`/
|
|
45
|
+
* `WritableStream` pair. Collapsing to Web Streams costs ~112 ns per received
|
|
46
|
+
* chunk (negligible) but forces either a per-value concatenation (16-233x
|
|
47
|
+
* slower, measured) or the loss of `writev` batching (3.9x more syscalls,
|
|
48
|
+
* measured) on the send side — and a codec is a symmetric pair that should not
|
|
49
|
+
* need two plumbing vocabularies to follow one round trip.
|
|
50
|
+
*/
|
|
51
|
+
export interface Transport {
|
|
52
|
+
/** Whether bytes arrive as a stream or as discrete messages. */
|
|
53
|
+
readonly mode: TransportMode;
|
|
54
|
+
/**
|
|
55
|
+
* Begin delivering data to `handlers`.
|
|
56
|
+
*
|
|
57
|
+
* Called once by the `FrameLink` that owns this transport.
|
|
58
|
+
*
|
|
59
|
+
* @param handlers Callbacks to receive data and lifecycle notifications.
|
|
60
|
+
*/
|
|
61
|
+
start(handlers: TransportHandlers): void;
|
|
62
|
+
/**
|
|
63
|
+
* Detach the installed handlers without closing the transport.
|
|
64
|
+
*
|
|
65
|
+
* Used when handing an already-connected transport to another consumer, as in
|
|
66
|
+
* an HTTP `Upgrade`. Contrast with {@link destroy}, which tears the transport
|
|
67
|
+
* down.
|
|
68
|
+
*/
|
|
69
|
+
stop(): void;
|
|
70
|
+
/**
|
|
71
|
+
* Write buffers as a single logical unit and resolve once accepted.
|
|
72
|
+
*
|
|
73
|
+
* Fragments are written in order and, where the transport supports it,
|
|
74
|
+
* coalesced into one vectored write. In message mode the buffers form exactly
|
|
75
|
+
* one outbound message.
|
|
76
|
+
*
|
|
77
|
+
* @param buffers Ordered payload fragments.
|
|
78
|
+
* @param signal Cancels the wait for capacity. Note that bytes already handed
|
|
79
|
+
* to the transport cannot be unsent; aborting rejects the caller but does
|
|
80
|
+
* not retract the write.
|
|
81
|
+
* @returns Resolves once the transport has accepted the bytes and any
|
|
82
|
+
* backpressure has cleared.
|
|
83
|
+
*/
|
|
84
|
+
write(buffers: readonly Uint8Array[], signal?: AbortSignal): Promise<void>;
|
|
85
|
+
/** Stop delivering {@link TransportHandlers["data"]} until {@link resume}. */
|
|
86
|
+
pause(): void;
|
|
87
|
+
/** Resume delivery suspended by {@link pause}. */
|
|
88
|
+
resume(): void;
|
|
89
|
+
/**
|
|
90
|
+
* Half-close the outbound direction and resolve once flushed.
|
|
91
|
+
*
|
|
92
|
+
* @returns Resolves when all pending writes have been sent.
|
|
93
|
+
*/
|
|
94
|
+
end(): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* Tear the transport down immediately, discarding pending I/O.
|
|
97
|
+
*
|
|
98
|
+
* @param error Optional cause, propagated to {@link TransportHandlers.error}
|
|
99
|
+
* where the transport supports it.
|
|
100
|
+
*/
|
|
101
|
+
destroy(error?: unknown): void;
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=transport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,SAAS,CAAC;AAEjD;;;;;GAKG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC/B;;;;OAIG;IACH,IAAI,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAAC;IAE9B,sEAAsE;IACtE,GAAG,IAAI,IAAI,CAAC;IAEZ,0DAA0D;IAC1D,KAAK,IAAI,IAAI,CAAC;IAEd,6CAA6C;IAC7C,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAAC;CAC5B,CAAC;AAEF;;;;;;;;;;;;;;GAcG;AACH,MAAM,WAAW,SAAS;IACzB,gEAAgE;IAChE,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;IAE7B;;;;;;OAMG;IACH,KAAK,CAAC,QAAQ,EAAE,iBAAiB,GAAG,IAAI,CAAC;IAEzC;;;;;;OAMG;IACH,IAAI,IAAI,IAAI,CAAC;IAEb;;;;;;;;;;;;;OAaG;IACH,KAAK,CAAC,OAAO,EAAE,SAAS,UAAU,EAAE,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE3E,8EAA8E;IAC9E,KAAK,IAAI,IAAI,CAAC;IAEd,kDAAkD;IAClD,MAAM,IAAI,IAAI,CAAC;IAEf;;;;OAIG;IACH,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAErB;;;;;OAKG;IACH,OAAO,CAAC,KAAK,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;CAC/B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.js","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":""}
|