@ac-kit/net-transport-node 0.1.3 → 0.2.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/CHANGELOG.md +59 -0
- package/README.md +68 -1
- package/dist/_compose-tcp-connection.d.ts +4 -0
- package/dist/_compose-tcp-connection.d.ts.map +1 -0
- package/dist/_compose-tcp-connection.js +13 -0
- package/dist/_compose-tcp-connection.js.map +1 -0
- package/dist/connect-tcp.d.ts +47 -0
- package/dist/connect-tcp.d.ts.map +1 -0
- package/dist/connect-tcp.js +65 -0
- package/dist/connect-tcp.js.map +1 -0
- package/dist/duplex-transport.d.ts +7 -0
- package/dist/duplex-transport.d.ts.map +1 -1
- package/dist/duplex-transport.js +28 -2
- package/dist/duplex-transport.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/tcp-listener.d.ts +75 -0
- package/dist/tcp-listener.d.ts.map +1 -0
- package/dist/tcp-listener.js +104 -0
- package/dist/tcp-listener.js.map +1 -0
- package/package.json +9 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,64 @@
|
|
|
1
1
|
# @ac-kit/net-transport-node
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 1af504e: HTTP/2, and TLS with ALPN.
|
|
8
|
+
|
|
9
|
+
- `format-http`: `Http2FrameCodec`, the RFC 9113 frame codec with field blocks
|
|
10
|
+
HPACK-coded on the way, and `HpackEncoder`/`HpackDecoder` (RFC 7541) on their
|
|
11
|
+
own. Sensitive fields are sent never-indexed; an oversized header list is a
|
|
12
|
+
stream error that keeps both ends' tables in step.
|
|
13
|
+
- `net-http`: `Http2ServerConnection` and `Http2ClientConnection` — concurrent
|
|
14
|
+
streams, flow control as backpressure, trailers, `GOAWAY`, a control-frame
|
|
15
|
+
rate guard against rapid-reset floods, and extended `CONNECT` tunnels.
|
|
16
|
+
`HttpServer` serves HTTP/2 through ALPN `h2` and cleartext prior knowledge
|
|
17
|
+
with its `http2` option; `HttpAgent` offers `h2` and multiplexes one
|
|
18
|
+
connection per origin with its own. A request's `protocol` now also works
|
|
19
|
+
over HTTP/1.1, sent as an `Upgrade`.
|
|
20
|
+
- `net-core`: `peekTransport`, to read a connection's first bytes without
|
|
21
|
+
consuming them.
|
|
22
|
+
- `net-transport-node`: TLS in `TcpListener.listen` (`tls` option, ALPN,
|
|
23
|
+
handshake timeout) and in the new `createTcpConnector`; `connectTcp` opens
|
|
24
|
+
`secure` targets with Node's default trust. Concurrent writes waiting for
|
|
25
|
+
`drain` share one listener.
|
|
26
|
+
- `integration-test-util`: `createSelfSignedCertificate`, a throwaway
|
|
27
|
+
certificate for TLS tests.
|
|
28
|
+
- `net-websocket`: `connectWebSocket` over HTTP/2 (RFC 8441), through the same
|
|
29
|
+
version-neutral request.
|
|
30
|
+
- 1af504e: Accepting and opening connections, and leaving a protocol mid-connection.
|
|
31
|
+
|
|
32
|
+
- `net-core`: `IListener`, `Connector`, `TransportConnection` and
|
|
33
|
+
`TransportEndpoint` types, so a server or a pooling client is written once
|
|
34
|
+
against any host. `FrameLink.detach()` (and `Session.detach()`) hands the
|
|
35
|
+
transport, with the bytes received but not yet decoded, to another protocol —
|
|
36
|
+
the HTTP `Upgrade` primitive.
|
|
37
|
+
- `net-transport-node`: `TcpListener` and `connectTcp`, implementing them over
|
|
38
|
+
`node:net`. Both allow half-open connections.
|
|
39
|
+
- 1af504e: `Transport.setWriteCoalescing(enabled)`, optional: whether a small write may be
|
|
40
|
+
held back to leave with later ones — Nagle's algorithm on TCP. `DuplexTransport`
|
|
41
|
+
maps it to `setNoDelay` on a TCP or TLS socket and ignores it on other
|
|
42
|
+
streams; the transport `net-core` hands over after a protocol switch passes it
|
|
43
|
+
on. Every HTTP/1.1 and HTTP/2 connection turns it off, as `node:http` and
|
|
44
|
+
`node:http2` do, so `TcpListener` and the connector no longer need
|
|
45
|
+
`noDelay: true` for HTTP.
|
|
46
|
+
|
|
47
|
+
### Patch Changes
|
|
48
|
+
|
|
49
|
+
- Updated dependencies [1af504e]
|
|
50
|
+
- Updated dependencies [1af504e]
|
|
51
|
+
- Updated dependencies [1af504e]
|
|
52
|
+
- @ac-kit/net-core@0.2.0
|
|
53
|
+
|
|
54
|
+
## 0.1.4
|
|
55
|
+
|
|
56
|
+
### Patch Changes
|
|
57
|
+
|
|
58
|
+
- Updated dependencies [235ec3c]
|
|
59
|
+
- @ac-kit/core@0.5.0
|
|
60
|
+
- @ac-kit/net-core@0.1.5
|
|
61
|
+
|
|
3
62
|
## 0.1.3
|
|
4
63
|
|
|
5
64
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -23,12 +23,79 @@ Connection setup is deliberately not a transport's concern: it takes a stream
|
|
|
23
23
|
that is already connected, which is what keeps reconnection and TLS negotiation
|
|
24
24
|
policy in the caller's hands.
|
|
25
25
|
|
|
26
|
+
## Accepting and opening TCP connections
|
|
27
|
+
|
|
28
|
+
A server or a pooling client needs connections on demand rather than one stream
|
|
29
|
+
handed over. `TcpListener` implements `@ac-kit/net-core`'s `IListener` and
|
|
30
|
+
emits each accepted connection; `connectTcp` is a `Connector`. Both are thin
|
|
31
|
+
adapters over `@ac-kit/node`'s `TcpServer`, `TlsServer`, `TcpSocket` and
|
|
32
|
+
`TlsSocket`, turning each socket into a `TransportConnection`.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { connectTcp, TcpListener } from "@ac-kit/net-transport-node";
|
|
36
|
+
|
|
37
|
+
const listener = await TcpListener.listen({ host: "127.0.0.1", port: 0 });
|
|
38
|
+
listener.subscribe("connection", ({ transport, remote }) => {
|
|
39
|
+
// Start or destroy `transport`; it belongs to this handler now.
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const { transport } = await connectTcp({
|
|
43
|
+
host: "127.0.0.1",
|
|
44
|
+
port: listener.local.port,
|
|
45
|
+
secure: false,
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The address to bind is required: which interfaces a server listens on is a
|
|
50
|
+
security decision. Both sides allow half-open connections, so a peer's end
|
|
51
|
+
arrives as the transport's `end` and the local side closes only when it ends or
|
|
52
|
+
destroys its transport.
|
|
53
|
+
|
|
54
|
+
### TLS
|
|
55
|
+
|
|
56
|
+
Given `tls`, `TcpListener.listen` accepts TLS connections: `TlsServer`'s
|
|
57
|
+
options (`key`, `cert`, `ca`, `SNICallback`, ...), the ALPN
|
|
58
|
+
protocols it offers in order of preference, and a required handshake timeout.
|
|
59
|
+
A connection is emitted once its handshake completed, with `secure` set and
|
|
60
|
+
the negotiated protocol as `alpnProtocol`; a failed handshake is reported as
|
|
61
|
+
the listener's `error` event.
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { createTcpConnector, TcpListener } from "@ac-kit/net-transport-node";
|
|
65
|
+
|
|
66
|
+
const listener = await TcpListener.listen({
|
|
67
|
+
host: "0.0.0.0",
|
|
68
|
+
port: 8443,
|
|
69
|
+
tls: { key, cert, alpnProtocols: ["h2", "http/1.1"], handshakeTimeoutMs: 5_000 },
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const connect = createTcpConnector({ tls: { ca } });
|
|
73
|
+
const { alpnProtocol } = await connect({
|
|
74
|
+
host: "localhost",
|
|
75
|
+
port: 8443,
|
|
76
|
+
secure: true,
|
|
77
|
+
alpnProtocols: ["h2", "http/1.1"],
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
`connectTcp` is `createTcpConnector()` with no options: it opens a `secure`
|
|
82
|
+
target with Node's default trust store, verifying the certificate against the
|
|
83
|
+
target host. `createTcpConnector` adds trust (`ca`), client certificates and
|
|
84
|
+
`noDelay`.
|
|
85
|
+
|
|
86
|
+
`noDelay` defaults to `false`, as in `node:net`: small writes are coalesced
|
|
87
|
+
(Nagle's algorithm). It is only the starting point: a protocol that needs
|
|
88
|
+
writes sent at once asks for it through the transport's `setWriteCoalescing`,
|
|
89
|
+
as `@ac-kit/net-http` does on every connection, and `DuplexTransport` maps it
|
|
90
|
+
to Nagle on a TCP or TLS socket.
|
|
91
|
+
|
|
26
92
|
Writes are corked around the whole buffer list, so a multi-fragment frame
|
|
27
93
|
reaches the socket as one vectored write rather than one syscall per fragment.
|
|
28
94
|
|
|
29
95
|
These live here rather than in `@ac-kit/node` so that installing the Node
|
|
30
96
|
helpers does not drag in the protocol substrate, and so a consumer supplying its
|
|
31
|
-
own transport never installs these adapters at all.
|
|
97
|
+
own transport never installs these adapters at all. `@ac-kit/node` keeps the
|
|
98
|
+
sockets; this package only adapts them to `Transport`.
|
|
32
99
|
|
|
33
100
|
See the
|
|
34
101
|
[generated API docs](https://anthochamp.pages.grenadine.io/node-essentials/api/net-transport-node/)
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { TransportConnection } from "@ac-kit/net-core";
|
|
2
|
+
import { type TcpSocket, TlsSocket } from "@ac-kit/node";
|
|
3
|
+
export declare function composeTcpConnection_(socket: TcpSocket | TlsSocket): TransportConnection;
|
|
4
|
+
//# sourceMappingURL=_compose-tcp-connection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_compose-tcp-connection.d.ts","sourceRoot":"","sources":["../src/_compose-tcp-connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,EAAE,KAAK,SAAS,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAIzD,wBAAgB,qBAAqB,CACpC,MAAM,EAAE,SAAS,GAAG,SAAS,GAC3B,mBAAmB,CASrB"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { TlsSocket } from "@ac-kit/node";
|
|
2
|
+
import { DuplexTransport } from "./duplex-transport.js";
|
|
3
|
+
export function composeTcpConnection_(socket) {
|
|
4
|
+
const secure = socket instanceof TlsSocket && socket.encrypted;
|
|
5
|
+
return {
|
|
6
|
+
transport: new DuplexTransport(socket.stream),
|
|
7
|
+
local: socket.localEndpoint,
|
|
8
|
+
remote: socket.remoteEndpoint,
|
|
9
|
+
secure,
|
|
10
|
+
alpnProtocol: secure ? socket.alpnProtocol : null,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=_compose-tcp-connection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_compose-tcp-connection.js","sourceRoot":"","sources":["../src/_compose-tcp-connection.ts"],"names":[],"mappings":"AACA,OAAO,EAAkB,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,MAAM,UAAU,qBAAqB,CACpC,MAA6B;IAE7B,MAAM,MAAM,GAAG,MAAM,YAAY,SAAS,IAAI,MAAM,CAAC,SAAS,CAAC;IAC/D,OAAO;QACN,SAAS,EAAE,IAAI,eAAe,CAAC,MAAM,CAAC,MAAM,CAAC;QAC7C,KAAK,EAAE,MAAM,CAAC,aAAa;QAC3B,MAAM,EAAE,MAAM,CAAC,cAAc;QAC7B,MAAM;QACN,YAAY,EAAE,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI;KACjD,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { ConnectTarget, Connector, TransportConnection } from "@ac-kit/net-core";
|
|
2
|
+
import { type TlsSocketConnectOptions } from "@ac-kit/node";
|
|
3
|
+
/**
|
|
4
|
+
* TLS for {@link createTcpConnector}: `TlsSocket`'s client options, less what a
|
|
5
|
+
* target sets.
|
|
6
|
+
*/
|
|
7
|
+
export type TcpConnectTlsOptions = Omit<TlsSocketConnectOptions, "host" | "servername" | "ALPNProtocols" | "allowHalfOpen" | "signal">;
|
|
8
|
+
/** Options for {@link createTcpConnector}. */
|
|
9
|
+
export type TcpConnectorOptions = {
|
|
10
|
+
/**
|
|
11
|
+
* How secure targets are verified — trusted roots (`ca`), client certificate,
|
|
12
|
+
* minimum version. Defaults to Node's: the system and bundled roots, and the
|
|
13
|
+
* peer's name checked against `target.host`.
|
|
14
|
+
*/
|
|
15
|
+
readonly tls?: TcpConnectTlsOptions;
|
|
16
|
+
/** Disable Nagle's algorithm. Defaults to `false`. */
|
|
17
|
+
readonly noDelay?: boolean;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* A `Connector` over TCP: cleartext for a target with `secure` unset, TLS with
|
|
21
|
+
* SNI and the target's ALPN protocols otherwise. Resolves once the TLS
|
|
22
|
+
* handshake completed, with the protocol ALPN chose.
|
|
23
|
+
*
|
|
24
|
+
* The socket allows half-open connections, like the ones `TcpListener` accepts:
|
|
25
|
+
* the local side closes only when its transport is ended or destroyed.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* const connect = createTcpConnector({ tls: { ca: myRootCertificate } });
|
|
30
|
+
* const { transport, alpnProtocol } = await connect({
|
|
31
|
+
* host: "localhost",
|
|
32
|
+
* port: 8443,
|
|
33
|
+
* secure: true,
|
|
34
|
+
* alpnProtocols: ["h2"],
|
|
35
|
+
* });
|
|
36
|
+
* ```;
|
|
37
|
+
*/
|
|
38
|
+
export declare function createTcpConnector(options?: TcpConnectorOptions): Connector;
|
|
39
|
+
/**
|
|
40
|
+
* Open a TCP connection, cleartext or TLS with Node's default verification;
|
|
41
|
+
* `createTcpConnector()` without options.
|
|
42
|
+
*
|
|
43
|
+
* @throws When the connection is refused, the TLS handshake or verification
|
|
44
|
+
* fails, or `signal` aborts first.
|
|
45
|
+
*/
|
|
46
|
+
export declare function connectTcp(target: ConnectTarget, signal?: AbortSignal): Promise<TransportConnection>;
|
|
47
|
+
//# sourceMappingURL=connect-tcp.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect-tcp.d.ts","sourceRoot":"","sources":["../src/connect-tcp.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACX,aAAa,EACb,SAAS,EACT,mBAAmB,EACnB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAGN,KAAK,uBAAuB,EAC5B,MAAM,cAAc,CAAC;AAItB;;;GAGG;AACH,MAAM,MAAM,oBAAoB,GAAG,IAAI,CACtC,uBAAuB,EACvB,MAAM,GAAG,YAAY,GAAG,eAAe,GAAG,eAAe,GAAG,QAAQ,CACpE,CAAC;AAEF,8CAA8C;AAC9C,MAAM,MAAM,mBAAmB,GAAG;IACjC;;;;OAIG;IACH,QAAQ,CAAC,GAAG,CAAC,EAAE,oBAAoB,CAAC;IAEpC,sDAAsD;IACtD,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC;CAC3B,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,CAAC,EAAE,mBAAmB,GAAG,SAAS,CAE3E;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CACzB,MAAM,EAAE,aAAa,EACrB,MAAM,CAAC,EAAE,WAAW,GAClB,OAAO,CAAC,mBAAmB,CAAC,CAE9B"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { isIP } from "node:net";
|
|
2
|
+
import { TcpSocket, TlsSocket, } from "@ac-kit/node";
|
|
3
|
+
import { composeTcpConnection_ } from "./_compose-tcp-connection.js";
|
|
4
|
+
/**
|
|
5
|
+
* A `Connector` over TCP: cleartext for a target with `secure` unset, TLS with
|
|
6
|
+
* SNI and the target's ALPN protocols otherwise. Resolves once the TLS
|
|
7
|
+
* handshake completed, with the protocol ALPN chose.
|
|
8
|
+
*
|
|
9
|
+
* The socket allows half-open connections, like the ones `TcpListener` accepts:
|
|
10
|
+
* the local side closes only when its transport is ended or destroyed.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const connect = createTcpConnector({ tls: { ca: myRootCertificate } });
|
|
15
|
+
* const { transport, alpnProtocol } = await connect({
|
|
16
|
+
* host: "localhost",
|
|
17
|
+
* port: 8443,
|
|
18
|
+
* secure: true,
|
|
19
|
+
* alpnProtocols: ["h2"],
|
|
20
|
+
* });
|
|
21
|
+
* ```;
|
|
22
|
+
*/
|
|
23
|
+
export function createTcpConnector(options) {
|
|
24
|
+
return (target, signal) => connect_(target, options ?? {}, signal);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Open a TCP connection, cleartext or TLS with Node's default verification;
|
|
28
|
+
* `createTcpConnector()` without options.
|
|
29
|
+
*
|
|
30
|
+
* @throws When the connection is refused, the TLS handshake or verification
|
|
31
|
+
* fails, or `signal` aborts first.
|
|
32
|
+
*/
|
|
33
|
+
export function connectTcp(target, signal) {
|
|
34
|
+
return connect_(target, {}, signal);
|
|
35
|
+
}
|
|
36
|
+
async function connect_(target, options, signal) {
|
|
37
|
+
signal?.throwIfAborted();
|
|
38
|
+
let socket;
|
|
39
|
+
if (target.secure) {
|
|
40
|
+
// Let `tls.connect` open the TCP connection itself: wrapping a socket still
|
|
41
|
+
// connecting (`TlsSocket.connect(stream)`) crashed the Vitest worker.
|
|
42
|
+
// Node passes allowHalfOpen through to the TLSSocket; its typings omit it.
|
|
43
|
+
const secureOptions = {
|
|
44
|
+
...options.tls,
|
|
45
|
+
host: target.host,
|
|
46
|
+
...(isIP(target.host) === 0 && { servername: target.host }),
|
|
47
|
+
...(target.alpnProtocols && {
|
|
48
|
+
ALPNProtocols: [...target.alpnProtocols],
|
|
49
|
+
}),
|
|
50
|
+
allowHalfOpen: true,
|
|
51
|
+
...(signal && { signal }),
|
|
52
|
+
};
|
|
53
|
+
socket = await TlsSocket.connect(target.port, secureOptions);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
socket = TcpSocket.from({ allowHalfOpen: true });
|
|
57
|
+
await socket.connect(target.port, {
|
|
58
|
+
host: target.host,
|
|
59
|
+
...(signal && { signal }),
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
socket.setNoDelay(options.noDelay ?? false);
|
|
63
|
+
return composeTcpConnection_(socket);
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=connect-tcp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect-tcp.js","sourceRoot":"","sources":["../src/connect-tcp.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAOhC,OAAO,EACN,SAAS,EACT,SAAS,GAET,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAwBrE;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAA6B;IAC/D,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,IAAI,EAAE,EAAE,MAAM,CAAC,CAAC;AACpE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CACzB,MAAqB,EACrB,MAAoB;IAEpB,OAAO,QAAQ,CAAC,MAAM,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC;AACrC,CAAC;AAED,KAAK,UAAU,QAAQ,CACtB,MAAqB,EACrB,OAA4B,EAC5B,MAA+B;IAE/B,MAAM,EAAE,cAAc,EAAE,CAAC;IAEzB,IAAI,MAA6B,CAAC;IAClC,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;QACnB,4EAA4E;QAC5E,sEAAsE;QACtE,2EAA2E;QAC3E,MAAM,aAAa,GAClB;YACC,GAAG,OAAO,CAAC,GAAG;YACd,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC;YAC3D,GAAG,CAAC,MAAM,CAAC,aAAa,IAAI;gBAC3B,aAAa,EAAE,CAAC,GAAG,MAAM,CAAC,aAAa,CAAC;aACxC,CAAC;YACF,aAAa,EAAE,IAAI;YACnB,GAAG,CAAC,MAAM,IAAI,EAAE,MAAM,EAAE,CAAC;SACzB,CAAC;QACH,MAAM,GAAG,MAAM,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,aAAa,CAAC,CAAC;IAC9D,CAAC;SAAM,CAAC;QACP,MAAM,GAAG,SAAS,CAAC,IAAI,CAAC,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC;QACjD,MAAM,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE;YACjC,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,GAAG,CAAC,MAAM,IAAI,EAAE,MAAM,EAAE,CAAC;SACzB,CAAC,CAAC;IACJ,CAAC;IACD,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,OAAO,IAAI,KAAK,CAAC,CAAC;IAE5C,OAAO,qBAAqB,CAAC,MAAM,CAAC,CAAC;AACtC,CAAC"}
|
|
@@ -19,6 +19,8 @@ export declare class DuplexTransport implements Transport {
|
|
|
19
19
|
private onEnd;
|
|
20
20
|
private onClose;
|
|
21
21
|
private onError;
|
|
22
|
+
private readonly drainWaiters;
|
|
23
|
+
private readonly handleDrain;
|
|
22
24
|
/**
|
|
23
25
|
* @param stream The already-connected duplex stream to carry frames over.
|
|
24
26
|
* Connection setup is deliberately not this class's concern.
|
|
@@ -31,5 +33,10 @@ export declare class DuplexTransport implements Transport {
|
|
|
31
33
|
resume(): void;
|
|
32
34
|
end(): Promise<void>;
|
|
33
35
|
destroy(error?: Error): void;
|
|
36
|
+
/**
|
|
37
|
+
* Nagle's algorithm on a TCP or TLS socket; nothing on any other stream,
|
|
38
|
+
* which never holds writes back.
|
|
39
|
+
*/
|
|
40
|
+
setWriteCoalescing(enabled: boolean): void;
|
|
34
41
|
}
|
|
35
42
|
//# sourceMappingURL=duplex-transport.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"duplex-transport.d.ts","sourceRoot":"","sources":["../src/duplex-transport.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"duplex-transport.d.ts","sourceRoot":"","sources":["../src/duplex-transport.ts"],"names":[],"mappings":"AACA,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;IAuBpC,OAAO,CAAC,QAAQ,CAAC,MAAM;IAtBnC,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;IAGzD,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAyB;IACtD,OAAO,CAAC,QAAQ,CAAC,WAAW,CAM1B;IAEF;;;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,CA0FrE;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;IAED;;;OAGG;IACH,kBAAkB,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAIzC;CACD"}
|
package/dist/duplex-transport.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Socket } from "node:net";
|
|
1
2
|
/**
|
|
2
3
|
* {@link Transport} over any Node duplex stream.
|
|
3
4
|
*
|
|
@@ -17,6 +18,16 @@ export class DuplexTransport {
|
|
|
17
18
|
onEnd = null;
|
|
18
19
|
onClose = null;
|
|
19
20
|
onError = null;
|
|
21
|
+
// Writes waiting for `drain`, behind one listener however many there are:
|
|
22
|
+
// a multiplexed protocol has one per stream sending.
|
|
23
|
+
drainWaiters = new Set();
|
|
24
|
+
handleDrain = () => {
|
|
25
|
+
const waiters = [...this.drainWaiters];
|
|
26
|
+
this.drainWaiters.clear();
|
|
27
|
+
for (const waiter of waiters) {
|
|
28
|
+
waiter();
|
|
29
|
+
}
|
|
30
|
+
};
|
|
20
31
|
/**
|
|
21
32
|
* @param stream The already-connected duplex stream to carry frames over.
|
|
22
33
|
* Connection setup is deliberately not this class's concern.
|
|
@@ -72,7 +83,10 @@ export class DuplexTransport {
|
|
|
72
83
|
let accepted = true;
|
|
73
84
|
let settled = false;
|
|
74
85
|
const cleanup = () => {
|
|
75
|
-
this.
|
|
86
|
+
if (this.drainWaiters.delete(handleDrain) &&
|
|
87
|
+
this.drainWaiters.size === 0) {
|
|
88
|
+
this.stream.off("drain", this.handleDrain);
|
|
89
|
+
}
|
|
76
90
|
if (signal && handleAbort) {
|
|
77
91
|
signal.removeEventListener("abort", handleAbort);
|
|
78
92
|
}
|
|
@@ -117,7 +131,10 @@ export class DuplexTransport {
|
|
|
117
131
|
}
|
|
118
132
|
this.stream.uncork();
|
|
119
133
|
if (!accepted && !settled) {
|
|
120
|
-
this.
|
|
134
|
+
if (this.drainWaiters.size === 0) {
|
|
135
|
+
this.stream.once("drain", this.handleDrain);
|
|
136
|
+
}
|
|
137
|
+
this.drainWaiters.add(handleDrain);
|
|
121
138
|
}
|
|
122
139
|
return promise;
|
|
123
140
|
}
|
|
@@ -145,6 +162,15 @@ export class DuplexTransport {
|
|
|
145
162
|
this.stream.on("error", noop);
|
|
146
163
|
this.stream.destroy(error);
|
|
147
164
|
}
|
|
165
|
+
/**
|
|
166
|
+
* Nagle's algorithm on a TCP or TLS socket; nothing on any other stream,
|
|
167
|
+
* which never holds writes back.
|
|
168
|
+
*/
|
|
169
|
+
setWriteCoalescing(enabled) {
|
|
170
|
+
if (this.stream instanceof Socket) {
|
|
171
|
+
this.stream.setNoDelay(!enabled);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
148
174
|
}
|
|
149
175
|
function noop() { }
|
|
150
176
|
//# sourceMappingURL=duplex-transport.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"duplex-transport.js","sourceRoot":"","sources":["../src/duplex-transport.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"duplex-transport.js","sourceRoot":"","sources":["../src/duplex-transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAUlC;;;;;;;;;;GAUG;AACH,MAAM,OAAO,eAAe;IAuBE,MAAM;IAtB1B,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;IACzD,0EAA0E;IAC1E,qDAAqD;IACpC,YAAY,GAAG,IAAI,GAAG,EAAc,CAAC;IACrC,WAAW,GAAG,GAAG,EAAE;QACnC,MAAM,OAAO,GAAG,CAAC,GAAG,IAAI,CAAC,YAAY,CAAC,CAAC;QACvC,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,CAAC;QAC1B,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC9B,MAAM,EAAE,CAAC;QACV,CAAC;IACF,CAAC,CAAC;IAEF;;;OAGG;IACH,YAA6B,MAAc;sB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C7D,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;IAED;;;OAGG;IACH,kBAAkB,CAAC,OAAgB;QAClC,IAAI,IAAI,CAAC,MAAM,YAAY,MAAM,EAAE,CAAC;YACnC,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,CAAC;QAClC,CAAC;IACF,CAAC;CACD;AAED,SAAS,IAAI,KAAU,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +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"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,mBAAmB,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +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"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { EventDispatcherMapBase } from "@ac-kit/async";
|
|
2
|
+
import type { IListener, ListenerEvents, TransportEndpoint } from "@ac-kit/net-core";
|
|
3
|
+
import { type TlsServerOptions } from "@ac-kit/node";
|
|
4
|
+
/**
|
|
5
|
+
* TLS for {@link TcpListener}: `TlsServer`'s options, plus what it must not
|
|
6
|
+
* default.
|
|
7
|
+
*/
|
|
8
|
+
export type TcpListenTlsOptions = Omit<TlsServerOptions, "ALPNProtocols" | "ALPNCallback" | "handshakeTimeout" | "allowHalfOpen" | "noDelay"> & {
|
|
9
|
+
/**
|
|
10
|
+
* Application protocols this server speaks, in its order of preference
|
|
11
|
+
* (`["h2", "http/1.1"]`). The first one the client also offers is chosen; a
|
|
12
|
+
* client offering none of them is refused. Omitted: no ALPN.
|
|
13
|
+
*/
|
|
14
|
+
alpnProtocols?: readonly string[];
|
|
15
|
+
/**
|
|
16
|
+
* How long, in milliseconds, a client may take to complete the handshake.
|
|
17
|
+
* Required: a client that never finishes it holds a socket.
|
|
18
|
+
*/
|
|
19
|
+
handshakeTimeoutMs: number;
|
|
20
|
+
};
|
|
21
|
+
/** Options for {@link TcpListener.listen}. */
|
|
22
|
+
export type TcpListenOptions = {
|
|
23
|
+
/**
|
|
24
|
+
* Numeric address to bind (`127.0.0.1`, `::1`, `0.0.0.0`). Required: which
|
|
25
|
+
* interfaces a server is reachable on is a security decision, never a
|
|
26
|
+
* default.
|
|
27
|
+
*/
|
|
28
|
+
host: string;
|
|
29
|
+
/** Port to bind; `0` lets the system pick one, read it from `local`. */
|
|
30
|
+
port: number;
|
|
31
|
+
/** Disable Nagle's algorithm on accepted sockets. Defaults to `false`. */
|
|
32
|
+
noDelay?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Accept TLS instead of cleartext: a connection is emitted once its handshake
|
|
35
|
+
* completed, with `secure` set and the ALPN protocol chosen.
|
|
36
|
+
*/
|
|
37
|
+
tls?: TcpListenTlsOptions;
|
|
38
|
+
/** Abandons binding. */
|
|
39
|
+
signal?: AbortSignal;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Accepts TCP connections, cleartext or TLS, and yields each as a
|
|
43
|
+
* `TransportConnection` over a {@link DuplexTransport}. Built on
|
|
44
|
+
* `@ac-kit/node`'s `TcpServer` and `TlsServer`.
|
|
45
|
+
*
|
|
46
|
+
* Accepted sockets allow half-open connections: the peer's end arrives as the
|
|
47
|
+
* transport's `end`, and the local side keeps writing until it ends or destroys
|
|
48
|
+
* the transport itself. A TLS handshake failure is reported as `error`; the
|
|
49
|
+
* listener carries on.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* const listener = await TcpListener.listen({ host: "127.0.0.1", port: 0 });
|
|
54
|
+
* listener.subscribe("connection", ({ transport }) => serve(transport));
|
|
55
|
+
* ```;
|
|
56
|
+
*/
|
|
57
|
+
export declare class TcpListener extends EventDispatcherMapBase<ListenerEvents> implements IListener {
|
|
58
|
+
private readonly server;
|
|
59
|
+
readonly local: TransportEndpoint;
|
|
60
|
+
private closed;
|
|
61
|
+
private constructor();
|
|
62
|
+
/**
|
|
63
|
+
* Bind and start accepting.
|
|
64
|
+
*
|
|
65
|
+
* @param options Address to bind, and accept options.
|
|
66
|
+
* @returns The listener, once bound.
|
|
67
|
+
* @throws When the address cannot be bound (in use, not local), or when
|
|
68
|
+
* `signal` aborts first.
|
|
69
|
+
*/
|
|
70
|
+
static listen(options: TcpListenOptions): Promise<TcpListener>;
|
|
71
|
+
close(): Promise<void>;
|
|
72
|
+
/** Closes the listener, so it can be used with `await using`. */
|
|
73
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
74
|
+
}
|
|
75
|
+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O,CAAC,IAAI,CAAC,CAQrB;IAED,iEAAiE;IACjE,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAErC;CACD"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { EventDispatcherMapBase } from "@ac-kit/async";
|
|
2
|
+
import { TcpServer, TlsServer, } from "@ac-kit/node";
|
|
3
|
+
import { composeTcpConnection_ } from "./_compose-tcp-connection.js";
|
|
4
|
+
/**
|
|
5
|
+
* Accepts TCP connections, cleartext or TLS, and yields each as a
|
|
6
|
+
* `TransportConnection` over a {@link DuplexTransport}. Built on
|
|
7
|
+
* `@ac-kit/node`'s `TcpServer` and `TlsServer`.
|
|
8
|
+
*
|
|
9
|
+
* Accepted sockets allow half-open connections: the peer's end arrives as the
|
|
10
|
+
* transport's `end`, and the local side keeps writing until it ends or destroys
|
|
11
|
+
* the transport itself. A TLS handshake failure is reported as `error`; the
|
|
12
|
+
* listener carries on.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const listener = await TcpListener.listen({ host: "127.0.0.1", port: 0 });
|
|
17
|
+
* listener.subscribe("connection", ({ transport }) => serve(transport));
|
|
18
|
+
* ```;
|
|
19
|
+
*/
|
|
20
|
+
export class TcpListener extends EventDispatcherMapBase {
|
|
21
|
+
server;
|
|
22
|
+
local;
|
|
23
|
+
closed = false;
|
|
24
|
+
constructor(server, local) {
|
|
25
|
+
super();
|
|
26
|
+
this.server = server;
|
|
27
|
+
this.local = local;
|
|
28
|
+
const accept = (socket) => this.dispatch("connection", [composeTcpConnection_(socket)]);
|
|
29
|
+
const fail = (error) => this.dispatch("error", [error]);
|
|
30
|
+
if (server instanceof TlsServer) {
|
|
31
|
+
server.subscribe("secureConnection", accept);
|
|
32
|
+
server.subscribe("tlsClientError", (error, socket) => {
|
|
33
|
+
socket.destroy();
|
|
34
|
+
fail(error);
|
|
35
|
+
});
|
|
36
|
+
server.subscribe("error", fail);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
server.subscribe("connection", accept);
|
|
40
|
+
server.subscribe("error", fail);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Bind and start accepting.
|
|
45
|
+
*
|
|
46
|
+
* @param options Address to bind, and accept options.
|
|
47
|
+
* @returns The listener, once bound.
|
|
48
|
+
* @throws When the address cannot be bound (in use, not local), or when
|
|
49
|
+
* `signal` aborts first.
|
|
50
|
+
*/
|
|
51
|
+
static async listen(options) {
|
|
52
|
+
const { signal } = options;
|
|
53
|
+
signal?.throwIfAborted();
|
|
54
|
+
const server = createServer_(options);
|
|
55
|
+
await server.listen(options.port, { host: options.host });
|
|
56
|
+
if (signal?.aborted) {
|
|
57
|
+
stopAccepting_(server);
|
|
58
|
+
throw signal.reason;
|
|
59
|
+
}
|
|
60
|
+
const local = server.address();
|
|
61
|
+
if (local === null) {
|
|
62
|
+
stopAccepting_(server);
|
|
63
|
+
throw new Error("TCP server is not bound");
|
|
64
|
+
}
|
|
65
|
+
return new TcpListener(server, local);
|
|
66
|
+
}
|
|
67
|
+
close() {
|
|
68
|
+
if (this.closed) {
|
|
69
|
+
return Promise.resolve();
|
|
70
|
+
}
|
|
71
|
+
this.closed = true;
|
|
72
|
+
stopAccepting_(this.server);
|
|
73
|
+
this.dispatch("close", []);
|
|
74
|
+
return Promise.resolve();
|
|
75
|
+
}
|
|
76
|
+
/** Closes the listener, so it can be used with `await using`. */
|
|
77
|
+
[Symbol.asyncDispose]() {
|
|
78
|
+
return this.close();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function createServer_(options) {
|
|
82
|
+
const noDelay = options.noDelay ?? false;
|
|
83
|
+
if (!options.tls) {
|
|
84
|
+
return TcpServer.from({ allowHalfOpen: true, noDelay });
|
|
85
|
+
}
|
|
86
|
+
const { alpnProtocols, handshakeTimeoutMs, ...secureOptions } = options.tls;
|
|
87
|
+
return TlsServer.from({
|
|
88
|
+
...secureOptions,
|
|
89
|
+
allowHalfOpen: true,
|
|
90
|
+
noDelay,
|
|
91
|
+
handshakeTimeout: handshakeTimeoutMs,
|
|
92
|
+
...(alpnProtocols && { ALPNProtocols: [...alpnProtocols] }),
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Stop accepting without waiting: `close()` settles only once every accepted
|
|
97
|
+
* socket ended, and those were handed over and are not the listener's.
|
|
98
|
+
*/
|
|
99
|
+
function stopAccepting_(server) {
|
|
100
|
+
server.close().catch(() => {
|
|
101
|
+
/* Already stopped: nothing left to stop. */
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=tcp-listener.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tcp-listener.js","sourceRoot":"","sources":["../src/tcp-listener.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAMvD,OAAO,EACN,SAAS,EAET,SAAS,GAGT,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAqDrE;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,WACZ,SAAQ,sBAAsC;IAM5B,MAAM;IACd,KAAK;IAJP,MAAM,GAAG,KAAK,CAAC;IAEvB,YACkB,MAA6B,EACrC,KAAwB;QAEjC,KAAK,EAAE,CAAC;sBAHS,MAAM;qBACd,KAAK;QAGd,MAAM,MAAM,GAAG,CAAC,MAA6B,EAAE,EAAE,CAChD,IAAI,CAAC,QAAQ,CAAC,YAAY,EAAE,CAAC,qBAAqB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QAC9D,MAAM,IAAI,GAAG,CAAC,KAAc,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;QACjE,IAAI,MAAM,YAAY,SAAS,EAAE,CAAC;YACjC,M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|
4
4
|
"description": "Node.js Transport adapters for @ac-kit/net-core: TCP, TLS, IPC and UDP.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"nodejs",
|
|
@@ -45,18 +45,21 @@
|
|
|
45
45
|
"prepack": "yarn build"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@ac-kit/
|
|
49
|
-
"@ac-kit/
|
|
48
|
+
"@ac-kit/async": "^0.1.4",
|
|
49
|
+
"@ac-kit/core": "^0.5.0",
|
|
50
|
+
"@ac-kit/net-core": "^0.2.0",
|
|
51
|
+
"@ac-kit/node": "^0.4.0",
|
|
50
52
|
"@types/node": "^26.5.1"
|
|
51
53
|
},
|
|
52
54
|
"devDependencies": {
|
|
53
|
-
"@ac-kit/format-core": "0.1.
|
|
54
|
-
"@ac-kit/format-netstring": "0.1.
|
|
55
|
+
"@ac-kit/format-core": "0.1.6",
|
|
56
|
+
"@ac-kit/format-netstring": "0.1.4",
|
|
57
|
+
"@ac-kit/integration-test-util": "0.3.0",
|
|
55
58
|
"@ac-kit/tsconfig": "0.1.0",
|
|
56
59
|
"@ac-kit/typedoc-config": "0.1.0",
|
|
57
60
|
"@ac-kit/vitest-config": "0.1.1",
|
|
58
61
|
"@typescript/native": "typescript@7.0.2",
|
|
59
|
-
"rimraf": "6.1.
|
|
62
|
+
"rimraf": "6.1.4",
|
|
60
63
|
"typedoc": "0.28.20",
|
|
61
64
|
"typescript": "@typescript/typescript6@6.0.2"
|
|
62
65
|
},
|