@ac-kit/net-transport-node 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 ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024-2026 Anthony Champagne and contributors
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # @ac-kit/net-transport-node
2
+
3
+ Node.js `Transport` implementations for `@ac-kit/net-core`, so a protocol client
4
+ written against that boundary can actually talk to a socket.
5
+
6
+ Two cover everything Node offers. `DuplexTransport` carries frames over any
7
+ `node:stream` duplex — TCP, TLS, Unix domain sockets, child-process pipes, and
8
+ the in-memory streams a test uses. `DatagramTransport` covers UDP, where there
9
+ is no stream to wrap.
10
+
11
+ ```ts
12
+ import { SmtpClient } from "@ac-kit/net-smtp";
13
+ import { DuplexTransport } from "@ac-kit/net-transport-node";
14
+ import { TcpSocket } from "@ac-kit/node";
15
+
16
+ const socket = TcpSocket.from();
17
+ await socket.connect(587, { host: "mail.example.com" });
18
+
19
+ const client = new SmtpClient(new DuplexTransport(socket.stream));
20
+ ```
21
+
22
+ Connection setup is deliberately not a transport's concern: it takes a stream
23
+ that is already connected, which is what keeps reconnection and TLS negotiation
24
+ policy in the caller's hands.
25
+
26
+ Writes are corked around the whole buffer list, so a multi-fragment frame
27
+ reaches the socket as one vectored write rather than one syscall per fragment.
28
+
29
+ These live here rather than in `@ac-kit/node` so that installing the Node
30
+ helpers does not drag in the protocol substrate, and so a consumer supplying its
31
+ own transport never installs these adapters at all.
32
+
33
+ See the
34
+ [generated API docs](https://anthochamp.github.io/node-essentials/api/net-transport-node/)
35
+ for the full reference.
@@ -0,0 +1,46 @@
1
+ import type { Socket as DgramSocket } from "node:dgram";
2
+ import type { Transport, TransportHandlers, TransportMode } from "@ac-kit/net-core";
3
+ /** Construction options for {@link DatagramTransport}. */
4
+ export interface DatagramTransportOptions {
5
+ /**
6
+ * Destination for outbound datagrams.
7
+ *
8
+ * Omit when the socket has been `connect()`ed, in which case Node supplies
9
+ * the destination.
10
+ */
11
+ remote?: {
12
+ port: number;
13
+ address: string;
14
+ };
15
+ }
16
+ /**
17
+ * Message-mode {@link Transport} over a UDP socket.
18
+ *
19
+ * Each datagram is delivered as exactly one complete frame: bytes are never
20
+ * carried across deliveries, so a decoder returning `incomplete` means the
21
+ * message was truncated rather than that more is coming. That distinction is
22
+ * why {@link TransportMode} is part of the transport contract — DNS over UDP and
23
+ * DNS over TCP share a codec but must not share buffering behaviour.
24
+ *
25
+ * Correlation is the caller's problem, and cannot be positional: pair this with
26
+ * a `KeyedRegistry` over the protocol's transaction identifier.
27
+ */
28
+ export declare class DatagramTransport implements Transport {
29
+ private readonly socket;
30
+ readonly mode: TransportMode;
31
+ private readonly remote?;
32
+ private onMessage;
33
+ private onClose;
34
+ private onError;
35
+ private paused;
36
+ private readonly backlog;
37
+ constructor(socket: DgramSocket, options?: DatagramTransportOptions);
38
+ start(handlers: TransportHandlers): void;
39
+ stop(): void;
40
+ write(buffers: readonly Buffer[], signal?: AbortSignal): Promise<void>;
41
+ pause(): void;
42
+ resume(): void;
43
+ end(): Promise<void>;
44
+ destroy(): void;
45
+ }
46
+ //# sourceMappingURL=datagram-transport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"datagram-transport.d.ts","sourceRoot":"","sources":["../src/datagram-transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,IAAI,WAAW,EAAE,MAAM,YAAY,CAAC;AAExD,OAAO,KAAK,EACX,SAAS,EACT,iBAAiB,EACjB,aAAa,EACb,MAAM,kBAAkB,CAAC;AAE1B,0DAA0D;AAC1D,MAAM,WAAW,wBAAwB;IACxC;;;;;OAKG;IACH,MAAM,CAAC,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;CAC3C;AAED;;;;;;;;;;;GAWG;AACH,qBAAa,iBAAkB,YAAW,SAAS;IAYjD,OAAO,CAAC,QAAQ,CAAC,MAAM;IAXxB,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAa;IAEzC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAoC;IAE5D,OAAO,CAAC,SAAS,CAA4C;IAC7D,OAAO,CAAC,OAAO,CAA6B;IAC5C,OAAO,CAAC,OAAO,CAA2C;IAC1D,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAgB;IAExC,YACkB,MAAM,EAAE,WAAW,EACpC,OAAO,CAAC,EAAE,wBAAwB,EAGlC;IAED,KAAK,CAAC,QAAQ,EAAE,iBAAiB,GAAG,IAAI,CAkBvC;IAED,IAAI,IAAI,IAAI,CAcX;IAED,KAAK,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAmCrE;IAED,KAAK,IAAI,IAAI,CAEZ;IAED,MAAM,IAAI,IAAI,CAOb;IAED,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,CAEnB;IAED,OAAO,IAAI,IAAI,CAEd;CACD"}
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Message-mode {@link Transport} over a UDP socket.
3
+ *
4
+ * Each datagram is delivered as exactly one complete frame: bytes are never
5
+ * carried across deliveries, so a decoder returning `incomplete` means the
6
+ * message was truncated rather than that more is coming. That distinction is
7
+ * why {@link TransportMode} is part of the transport contract — DNS over UDP and
8
+ * DNS over TCP share a codec but must not share buffering behaviour.
9
+ *
10
+ * Correlation is the caller's problem, and cannot be positional: pair this with
11
+ * a `KeyedRegistry` over the protocol's transaction identifier.
12
+ */
13
+ export class DatagramTransport {
14
+ socket;
15
+ mode = "message";
16
+ remote;
17
+ onMessage = null;
18
+ onClose = null;
19
+ onError = null;
20
+ paused = false;
21
+ backlog = [];
22
+ constructor(socket, options) {
23
+ this.socket = socket;
24
+ this.remote = options?.remote;
25
+ }
26
+ start(handlers) {
27
+ if (this.onMessage) {
28
+ throw new Error("Transport already started");
29
+ }
30
+ this.onMessage = (message) => {
31
+ if (this.paused) {
32
+ this.backlog.push(message);
33
+ return;
34
+ }
35
+ handlers.data(message);
36
+ };
37
+ this.onClose = () => handlers.close();
38
+ this.onError = (error) => handlers.error(error);
39
+ this.socket.on("message", this.onMessage);
40
+ this.socket.on("close", this.onClose);
41
+ this.socket.on("error", this.onError);
42
+ }
43
+ stop() {
44
+ if (this.onMessage) {
45
+ this.socket.off("message", this.onMessage);
46
+ }
47
+ if (this.onClose) {
48
+ this.socket.off("close", this.onClose);
49
+ }
50
+ if (this.onError) {
51
+ this.socket.off("error", this.onError);
52
+ }
53
+ this.onMessage = null;
54
+ this.onClose = null;
55
+ this.onError = null;
56
+ this.backlog.length = 0;
57
+ }
58
+ write(buffers, signal) {
59
+ if (signal?.aborted) {
60
+ return Promise.reject(new Error("Send aborted before it was issued", {
61
+ cause: signal.reason,
62
+ }));
63
+ }
64
+ // A datagram is atomic, so fragments must be joined rather than written
65
+ // in sequence as they would be on a stream transport.
66
+ const payload = buffers.length === 1 ? buffers[0] : Buffer.concat(buffers);
67
+ const { promise, resolve, reject } = Promise.withResolvers();
68
+ const callback = (error) => {
69
+ if (error) {
70
+ reject(new Error("Datagram send failed", { cause: error }));
71
+ }
72
+ else {
73
+ resolve();
74
+ }
75
+ };
76
+ if (this.remote) {
77
+ this.socket.send(payload, this.remote.port, this.remote.address, callback);
78
+ }
79
+ else {
80
+ this.socket.send(payload, callback);
81
+ }
82
+ return promise;
83
+ }
84
+ pause() {
85
+ this.paused = true;
86
+ }
87
+ resume() {
88
+ this.paused = false;
89
+ // Datagrams cannot be un-received, so they are held rather than dropped.
90
+ while (!this.paused && this.backlog.length > 0) {
91
+ const message = this.backlog.shift();
92
+ this.onMessage?.(message);
93
+ }
94
+ }
95
+ end() {
96
+ return Promise.resolve();
97
+ }
98
+ destroy() {
99
+ this.socket.close();
100
+ }
101
+ }
102
+ //# sourceMappingURL=datagram-transport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"datagram-transport.js","sourceRoot":"","sources":["../src/datagram-transport.ts"],"names":[],"mappings":"AAmBA;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,iBAAiB;IAYX,MAAM;IAXf,IAAI,GAAkB,SAAS,CAAC;IAExB,MAAM,CAAqC;IAEpD,SAAS,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IAAI,CAAC,MAAM,EAAE,CAAC;YACjB,IAAI,CAAC,MAAM,CAAC,IAAI,CACf,OAAO,EACP,IAAI,CAAC,MAAM,CAAC,IAAI,EAChB,IAAI,CAAC,MAAM,CAAC,OAAO,EACnB,QAAQ,CACR,CAAC;QACH,CAAC;aAAM,CAAC;YACP,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QACrC,CAAC;QAED,OAAO,OAAO,CAAC;IAChB,CAAC;IAED,KAAK;QACJ,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;IACpB,CAAC;IAED,MAAM;QACL,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,yEAAyE;QACzE,OAAO,CAAC,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAChD,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAG,CAAC;YACtC,IAAI,CAAC,SAAS,EAAE,CAAC,OAAO,CAAC,CAAC;QAC3B,CAAC;IACF,CAAC;IAED,GAAG;QACF,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;IAC1B,CAAC;IAED,OAAO;QACN,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IACrB,CAAC;CACD"}
@@ -0,0 +1,35 @@
1
+ import type { Duplex } from "node:stream";
2
+ import type { Transport, TransportHandlers, TransportMode } from "@ac-kit/net-core";
3
+ /**
4
+ * {@link Transport} over any Node duplex stream.
5
+ *
6
+ * Covers TCP, TLS, Unix domain sockets, child-process pipes, and in-memory
7
+ * streams used by tests. Instances of `StreamSocket` and its subclasses expose
8
+ * a suitable stream through their `stream` accessor.
9
+ *
10
+ * Writes are corked around the whole buffer list so a multi-fragment frame
11
+ * reaches the socket as one vectored write rather than one syscall per
12
+ * fragment.
13
+ */
14
+ export declare class DuplexTransport implements Transport {
15
+ private readonly stream;
16
+ readonly mode: TransportMode;
17
+ private handlers;
18
+ private onData;
19
+ private onEnd;
20
+ private onClose;
21
+ private onError;
22
+ /**
23
+ * @param stream The already-connected duplex stream to carry frames over.
24
+ * Connection setup is deliberately not this class's concern.
25
+ */
26
+ constructor(stream: Duplex);
27
+ start(handlers: TransportHandlers): void;
28
+ stop(): void;
29
+ write(buffers: readonly Buffer[], signal?: AbortSignal): Promise<void>;
30
+ pause(): void;
31
+ resume(): void;
32
+ end(): Promise<void>;
33
+ destroy(error?: Error): void;
34
+ }
35
+ //# sourceMappingURL=duplex-transport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"duplex-transport.d.ts","sourceRoot":"","sources":["../src/duplex-transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAG1C,OAAO,KAAK,EACX,SAAS,EACT,iBAAiB,EACjB,aAAa,EACb,MAAM,kBAAkB,CAAC;AAE1B;;;;;;;;;;GAUG;AACH,qBAAa,eAAgB,YAAW,SAAS;IAapC,OAAO,CAAC,QAAQ,CAAC,MAAM;IAZnC,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAY;IAExC,OAAO,CAAC,QAAQ,CAAkC;IAClD,OAAO,CAAC,MAAM,CAA0C;IACxD,OAAO,CAAC,KAAK,CAA6B;IAC1C,OAAO,CAAC,OAAO,CAA6B;IAC5C,OAAO,CAAC,OAAO,CAA0C;IAEzD;;;OAGG;IACH,YAA6B,MAAM,EAAE,MAAM,EAAI;IAE/C,KAAK,CAAC,QAAQ,EAAE,iBAAiB,GAAG,IAAI,CAevC;IAED,IAAI,IAAI,IAAI,CAkBX;IAED,KAAK,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAkFrE;IAED,KAAK,IAAI,IAAI,CAEZ;IAED,MAAM,IAAI,IAAI,CAEb;IAED,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,CAUnB;IAED,OAAO,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,IAAI,CAK3B;CACD"}
@@ -0,0 +1,150 @@
1
+ /**
2
+ * {@link Transport} over any Node duplex stream.
3
+ *
4
+ * Covers TCP, TLS, Unix domain sockets, child-process pipes, and in-memory
5
+ * streams used by tests. Instances of `StreamSocket` and its subclasses expose
6
+ * a suitable stream through their `stream` accessor.
7
+ *
8
+ * Writes are corked around the whole buffer list so a multi-fragment frame
9
+ * reaches the socket as one vectored write rather than one syscall per
10
+ * fragment.
11
+ */
12
+ export class DuplexTransport {
13
+ stream;
14
+ mode = "stream";
15
+ handlers = null;
16
+ onData = null;
17
+ onEnd = null;
18
+ onClose = null;
19
+ onError = null;
20
+ /**
21
+ * @param stream The already-connected duplex stream to carry frames over.
22
+ * Connection setup is deliberately not this class's concern.
23
+ */
24
+ constructor(stream) {
25
+ this.stream = stream;
26
+ }
27
+ start(handlers) {
28
+ if (this.handlers) {
29
+ throw new Error("Transport already started");
30
+ }
31
+ this.handlers = handlers;
32
+ this.onData = (chunk) => handlers.data(chunk);
33
+ this.onEnd = () => handlers.end();
34
+ this.onClose = () => handlers.close();
35
+ this.onError = (error) => handlers.error(error);
36
+ this.stream.on("data", this.onData);
37
+ this.stream.on("end", this.onEnd);
38
+ this.stream.on("close", this.onClose);
39
+ this.stream.on("error", this.onError);
40
+ }
41
+ stop() {
42
+ if (this.onData) {
43
+ this.stream.off("data", this.onData);
44
+ }
45
+ if (this.onEnd) {
46
+ this.stream.off("end", this.onEnd);
47
+ }
48
+ if (this.onClose) {
49
+ this.stream.off("close", this.onClose);
50
+ }
51
+ if (this.onError) {
52
+ this.stream.off("error", this.onError);
53
+ }
54
+ this.handlers = null;
55
+ this.onData = null;
56
+ this.onEnd = null;
57
+ this.onClose = null;
58
+ this.onError = null;
59
+ }
60
+ write(buffers, signal) {
61
+ if (signal?.aborted) {
62
+ return Promise.reject(new Error("Write aborted before it was issued", {
63
+ cause: signal.reason,
64
+ }));
65
+ }
66
+ if (buffers.length === 0) {
67
+ return Promise.resolve();
68
+ }
69
+ const { promise, resolve, reject } = Promise.withResolvers();
70
+ // Declared before the write so the write callback can never observe it
71
+ // in the temporal dead zone, even if the stream calls back synchronously.
72
+ let accepted = true;
73
+ let settled = false;
74
+ const cleanup = () => {
75
+ this.stream.off("drain", handleDrain);
76
+ if (signal && handleAbort) {
77
+ signal.removeEventListener("abort", handleAbort);
78
+ }
79
+ };
80
+ const succeed = () => {
81
+ if (settled) {
82
+ return;
83
+ }
84
+ settled = true;
85
+ cleanup();
86
+ resolve();
87
+ };
88
+ const fail = (error) => {
89
+ if (settled) {
90
+ return;
91
+ }
92
+ settled = true;
93
+ cleanup();
94
+ reject(error);
95
+ };
96
+ const handleDrain = () => succeed();
97
+ const handleAbort = signal
98
+ ? () => fail(new Error("Write aborted while waiting for the transport to drain", { cause: signal.reason }))
99
+ : null;
100
+ const handleWritten = (error) => {
101
+ if (error) {
102
+ fail(new Error("Transport write failed", { cause: error }));
103
+ }
104
+ else if (accepted) {
105
+ succeed();
106
+ }
107
+ // Otherwise the stream is backpressured and handleDrain resolves.
108
+ };
109
+ if (signal && handleAbort) {
110
+ signal.addEventListener("abort", handleAbort, { once: true });
111
+ }
112
+ this.stream.cork();
113
+ for (let index = 0; index < buffers.length; index++) {
114
+ const isLast = index === buffers.length - 1;
115
+ const ok = this.stream.write(buffers[index], isLast ? handleWritten : undefined);
116
+ accepted &&= ok;
117
+ }
118
+ this.stream.uncork();
119
+ if (!accepted && !settled) {
120
+ this.stream.once("drain", handleDrain);
121
+ }
122
+ return promise;
123
+ }
124
+ pause() {
125
+ this.stream.pause();
126
+ }
127
+ resume() {
128
+ this.stream.resume();
129
+ }
130
+ end() {
131
+ const { promise, resolve, reject } = Promise.withResolvers();
132
+ this.stream.end((error) => {
133
+ if (error) {
134
+ reject(new Error("Transport end failed", { cause: error }));
135
+ }
136
+ else {
137
+ resolve();
138
+ }
139
+ });
140
+ return promise;
141
+ }
142
+ destroy(error) {
143
+ // destroy(err) re-emits the error, and our handler may already be detached;
144
+ // without an absorbing listener Node escalates it to an uncaught exception.
145
+ this.stream.on("error", noop);
146
+ this.stream.destroy(error);
147
+ }
148
+ }
149
+ function noop() { }
150
+ //# sourceMappingURL=duplex-transport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"duplex-transport.js","sourceRoot":"","sources":["../src/duplex-transport.ts"],"names":[],"mappings":"AASA;;;;;;;;;;GAUG;AACH,MAAM,OAAO,eAAe;IAaE,MAAM;IAZ1B,IAAI,GAAkB,QAAQ,CAAC;IAEhC,QAAQ,GAA6B,IAAI,CAAC;IAC1C,MAAM,GAAqC,IAAI,CAAC;IAChD,KAAK,GAAwB,IAAI,CAAC;IAClC,OAAO,GAAwB,IAAI,CAAC;IACpC,OAAO,GAAqC,IAAI,CAAC;IAEzD;;;OAGG;IACH,YAA6B,MAAc;sBAAd,MAAM;IAAW,CAAC;IAE/C,KAAK,CAAC,QAA2B;QAChC,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YACnB,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;QAC9C,CAAC;QACD,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QAEzB,IAAI,CAAC,MAAM,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACtD,IAAI,CAAC,KAAK,GAAG,GAAG,EAAE,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC;QAClC,IAAI,CAAC,OAAO,GAAG,GAAG,EAAE,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;QACtC,IAAI,CAAC,OAAO,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAExD,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;QACpC,IAAI,CAAC,MAAM,CAAC,EAAE,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MAAM,EAAE,GAAG,OAAO,CAAC,aAAa,EAAQ,CAAC;QACnE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAoB,EAAE,EAAE;YACxC,IAAI,KAAK,EAAE,CAAC;gBACX,MAAM,CAAC,IAAI,KAAK,CAAC,sBAAsB,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;YAC7D,CAAC;iBAAM,CAAC;gBACP,OAAO,EAAE,CAAC;YACX,CAAC;QACF,CAAC,CAAC,CAAC;QACH,OAAO,OAAO,CAAC;IAChB,CAAC;IAED,OAAO,CAAC,KAAa;QACpB,4EAA4E;QAC5E,4EAA4E;QAC5E,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QAC9B,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAC5B,CAAC;CACD;AAED,SAAS,IAAI,KAAU,CAAC"}
@@ -0,0 +1,12 @@
1
+ import type { Duplex } from "node:stream";
2
+ import type { FrameLinkOptions } from "@ac-kit/net-core";
3
+ import { FrameLink } from "@ac-kit/net-core";
4
+ /**
5
+ * Bind a frame link to an already-connected duplex stream.
6
+ *
7
+ * @param stream An already-connected duplex stream.
8
+ * @param options FrameLink options excluding the transport.
9
+ * @returns A frame link bound to a {@link DuplexTransport} over `stream`.
10
+ */
11
+ export declare function frameLinkFromDuplex<In, Out = In>(stream: Duplex, options: Omit<FrameLinkOptions<In, Out>, "transport">): FrameLink<In, Out>;
12
+ //# sourceMappingURL=frame-link-from-duplex.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"frame-link-from-duplex.d.ts","sourceRoot":"","sources":["../src/frame-link-from-duplex.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAI7C;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,EAAE,EAAE,GAAG,GAAG,EAAE,EAC/C,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,IAAI,CAAC,gBAAgB,CAAC,EAAE,EAAE,GAAG,CAAC,EAAE,WAAW,CAAC,GACnD,SAAS,CAAC,EAAE,EAAE,GAAG,CAAC,CAKpB"}
@@ -0,0 +1,16 @@
1
+ import { FrameLink } from "@ac-kit/net-core";
2
+ import { DuplexTransport } from "./duplex-transport.js";
3
+ /**
4
+ * Bind a frame link to an already-connected duplex stream.
5
+ *
6
+ * @param stream An already-connected duplex stream.
7
+ * @param options FrameLink options excluding the transport.
8
+ * @returns A frame link bound to a {@link DuplexTransport} over `stream`.
9
+ */
10
+ export function frameLinkFromDuplex(stream, options) {
11
+ return new FrameLink({
12
+ ...options,
13
+ transport: new DuplexTransport(stream),
14
+ });
15
+ }
16
+ //# sourceMappingURL=frame-link-from-duplex.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"frame-link-from-duplex.js","sourceRoot":"","sources":["../src/frame-link-from-duplex.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB,CAClC,MAAc,EACd,OAAqD;IAErD,OAAO,IAAI,SAAS,CAAU;QAC7B,GAAG,OAAO;QACV,SAAS,EAAE,IAAI,eAAe,CAAC,MAAM,CAAC;KACtC,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,4 @@
1
+ export * from "./datagram-transport.js";
2
+ export * from "./duplex-transport.js";
3
+ export * from "./frame-link-from-duplex.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,6BAA6B,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./datagram-transport.js";
2
+ export * from "./duplex-transport.js";
3
+ export * from "./frame-link-from-duplex.js";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,6BAA6B,CAAC"}
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@ac-kit/net-transport-node",
3
+ "version": "0.1.0",
4
+ "description": "Node.js Transport adapters for @ac-kit/net-core: TCP, TLS, IPC and UDP.",
5
+ "keywords": [
6
+ "nodejs",
7
+ "tcp",
8
+ "tls",
9
+ "transport",
10
+ "udp"
11
+ ],
12
+ "homepage": "https://anthochamp.github.io/node-essentials/api/net-transport-node/",
13
+ "bugs": {
14
+ "url": "https://github.com/anthochamp/node-essentials/issues"
15
+ },
16
+ "license": "BSD-3-Clause",
17
+ "author": "Anthony Champagne <dev@anthonychampagne.fr>",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/anthochamp/node-essentials.git",
21
+ "directory": "packages/net-transport-node"
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "type": "module",
27
+ "sideEffects": false,
28
+ "main": "./dist/index.js",
29
+ "exports": {
30
+ ".": "./dist/index.js"
31
+ },
32
+ "publishConfig": {
33
+ "main": "./dist/index.js",
34
+ "exports": {
35
+ ".": "./dist/index.js"
36
+ },
37
+ "access": "public"
38
+ },
39
+ "scripts": {
40
+ "compile": "tsc -p ./tsconfig.build.json -outDir dist",
41
+ "compile:check": "tsc -noEmit -p ./tsconfig.json",
42
+ "clean": "rimraf dist",
43
+ "build": "yarn clean && yarn compile",
44
+ "test": "yarn run -T vitest run",
45
+ "prepack": "yarn build"
46
+ },
47
+ "dependencies": {
48
+ "@ac-kit/core": "^0.1.0",
49
+ "@ac-kit/net-core": "^0.1.0",
50
+ "@types/node": "^26.5.1"
51
+ },
52
+ "devDependencies": {
53
+ "@ac-kit/format-core": "0.1.0",
54
+ "@ac-kit/format-netstring": "0.1.0",
55
+ "@ac-kit/tsconfig": "0.1.0",
56
+ "@ac-kit/typedoc-config": "0.1.0",
57
+ "@ac-kit/vitest-config": "0.1.0",
58
+ "@typescript/native": "typescript@7.0.2",
59
+ "rimraf": "6.1.3",
60
+ "typedoc": "0.28.20",
61
+ "typescript": "@typescript/typescript6@6.0.2"
62
+ },
63
+ "engines": {
64
+ "node": ">=24"
65
+ }
66
+ }