@zukhruf/net 0.0.0-stage → 0.8.222

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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ezzabuzaid
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,260 @@
1
- # Temporary Holding Version
1
+ # @zukhruf/net
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ > **In build (backlog #6078).** The API on paper was approved on 2026-11-28, and it is built as the paper shows; it is not on npm yet, and no consumer has moved. Each decision is an accepted ADR, and each detail that the review decided is in [Decided in the review](#decided-in-the-review).
4
+
5
+ A server of this program that accepts connections, and that ends each one at its end. A `Server` says what to do with each connection. `server.serve(endpoint)` listens on a TCP port of the loopback address or on a pipe, and gives a `Serving`. The serving keeps each socket that it accepted. Its `close()` destroys each one and waits for its close, and its `shutdown(signal)` waits until each one ends by itself and closes the rest at the abort. `@zukhruf/net/http` is the same server for HTTP.
6
+
7
+ A test needs this server most: 48 test files of 9 repositories have their own copy of it, and 13 files have their own `portOf` ([ADR 0001](./docs/adr/0001-a-base-package-above-pipes-owns-the-connections-that-a-server-accepts-and-testings-httpserver-converges-into-it.md)). It is a base package, so shipped code can use it too.
8
+
9
+ The words in these documents have one meaning each. See the glossary in [CONTEXT.md](./CONTEXT.md).
10
+
11
+ ## Use it
12
+
13
+ ```sh
14
+ npm install @zukhruf/net
15
+ ```
16
+
17
+ ```ts
18
+ import { Server, TcpEndpoint } from '@zukhruf/net';
19
+
20
+ const echo = new Server((socket) => socket.pipe(socket));
21
+ await using serving = await echo.serve(new TcpEndpoint());
22
+ console.log(serving.address.port); // a free port of 127.0.0.1
23
+ ```
24
+
25
+ ## Listen on a free port
26
+
27
+ `new TcpEndpoint({ host, port })` says where to listen. The defaults are `host: '127.0.0.1'` and `port: 0`, a free port that the system chooses. `serving.address` is a `net.SocketAddress` with the port that the serving got ([ADR 0003](./docs/adr/0003-an-endpoint-listens-and-gives-a-listener-that-never-ends-an-accepted-socket.md)).
28
+
29
+ ```ts
30
+ await using serving = await server.serve(new TcpEndpoint());
31
+ const client = connect(serving.address.port, serving.address.address);
32
+ ```
33
+
34
+ - `host` is an IP address. The endpoint does not look up a host name.
35
+ - To listen again on the same port, give it: `server.serve(new TcpEndpoint({ port: serving.address.port }))`.
36
+
37
+ ## Listen on a pipe
38
+
39
+ A `PipeEndpoint` of `@zukhruf/pipes` is an endpoint too: a Unix socket on macOS and Linux, a named pipe on Windows. `serving.address` is then its `PipeAddress`, and it has no port.
40
+
41
+ ```ts
42
+ import { Server } from '@zukhruf/net';
43
+ import { PipeEndpoint } from '@zukhruf/pipes';
44
+
45
+ await using serving = await new Server(onPeer).serve(
46
+ new PipeEndpoint(socketPath, { prefix: 'mutex' }),
47
+ );
48
+ const peer = await serving.address.connect();
49
+ ```
50
+
51
+ ## Say what to do with each connection
52
+
53
+ `new Server(handler, options)` holds the handler and listens on nothing. `handler(socket)` gets each accepted `net.Socket`. A server can serve more than once, also on two endpoints at once ([ADR 0004](./docs/adr/0004-a-server-is-a-value-and-serve-gives-a-serving-that-owns-each-socket-that-it-accepted.md)).
54
+
55
+ | Copy today | With the package |
56
+ | ----------------------------------------------- | -------------------------------------------------------------- |
57
+ | ssh `startSilentServer()` | `new Server(() => {}).serve(new TcpEndpoint())` |
58
+ | mutex `standInServer(path, onPeer)` | `new Server(onPeer).serve(new PipeEndpoint(path, { prefix }))` |
59
+ | ssh `listen(path, accepted, { allowHalfOpen })` | `new Server(accepted, { allowHalfOpen }).serve(endpoint)` |
60
+ | `portOf(server)` | `serving.address.port` |
61
+
62
+ ## End a serving
63
+
64
+ | Call | What happens underneath |
65
+ | ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
66
+ | `await serving.close()` | The listener stops its accepts. Each accepted socket gets `destroy()`. It resolves when the listener has closed and each socket has emitted `close`. |
67
+ | `await using serving = ...` | `close()` at the end of the scope. |
68
+ | `await serving.shutdown(signal)` | The listener stops its accepts. It waits until each accepted socket has closed by itself. When `signal` aborts first, it destroys the rest and waits for their `close`. |
69
+ | a second `close()` or `shutdown()` | It waits for the end of the first call and resolves. |
70
+
71
+ The listener's own close never ends an accepted socket, as Go's `net.Listener` does not. Only the serving ends them ([ADR 0005](./docs/adr/0005-close-ends-each-accepted-socket-and-waits-for-its-close-and-shutdown-waits-for-the-sockets-until-its-signal-aborts.md)).
72
+
73
+ ## Serve HTTP
74
+
75
+ `new HttpServer(handler, options)` of `@zukhruf/net/http` is a server whose handler gives each accepted socket to its own `http.Server`. `handler` is a request listener of `node:http`. On a TCP endpoint, the serving has `origin` ([ADR 0006](./docs/adr/0006-the-http-view-is-a-server-whose-handler-gives-each-socket-to-an-http-server.md)).
76
+
77
+ ```ts
78
+ import { TcpEndpoint } from '@zukhruf/net';
79
+ import { HttpServer } from '@zukhruf/net/http';
80
+
81
+ await using site = await new HttpServer((request, response) => {
82
+ response.end('hello');
83
+ }).serve(new TcpEndpoint());
84
+ const answer = await fetch(`${site.origin}/`);
85
+ ```
86
+
87
+ - The close destroys each accepted socket, also a socket that an `upgrade` took. Then it destroys each client socket of this process that belonged to a destroyed connection, and waits for its `close`, so the timers of `fetch` are cleared before the next test (testing ADR 0030).
88
+ - `shutdown(signal)` ends each idle connection at once, and each other connection after its response, as Go's `Shutdown`.
89
+ - `options` are those of `http.createServer` (`keepAliveTimeout`, `highWaterMark`, ...) and `upgrade`, a listener of the `upgrade` event. docker's fake engine needs all three.
90
+
91
+ | `@zukhruf/testing/http` today | `@zukhruf/net/http` |
92
+ | -------------------------------------------- | ---------------------------------------------------------- |
93
+ | `await new HttpServer().start(handler)` | `await new HttpServer(handler).serve(new TcpEndpoint())` |
94
+ | `handle.origin` | `serving.origin` |
95
+ | `await handle.cleanup()` | `await serving.close()` |
96
+ | `handle.server.close()`, then `listen(port)` | `serving.close()`, then `serve(new TcpEndpoint({ port }))` |
97
+ | `HttpServerHandle` | `HttpServing` |
98
+
99
+ ## Errors
100
+
101
+ The package has no error class of its own. A caller does not do a different next step for an error of this package.
102
+
103
+ | Error | When |
104
+ | -------------------------------- | ----------------------------------------------------------------------------------------------------- |
105
+ | the error of `net.Server.listen` | `serve()` rejects with it: `EADDRINUSE` for a port in use, `EACCES`, `EADDRNOTAVAIL`. |
106
+ | the errors of `@zukhruf/pipes` | `serve()` on a `PipeEndpoint` rejects with them: `UnsafeFolderError`, a `RangeError` for a long path. |
107
+
108
+ ## What the package does not do
109
+
110
+ - **Faults.** Latency, a partition, a reset or a stall are in the tool package `@zukhruf/chaos`, which uses this one.
111
+ - **Look up a host name.** `TcpEndpoint` takes an IP address, as `net.SocketAddress` does.
112
+ - **Tell a raw handler that a shutdown began.** Kestrel does it with `ConnectionClosedRequested`. It is on the map as later.
113
+ - **TLS.** A host can write a TLS endpoint: an `Endpoint` is an interface.
114
+ - **Later, on the map:** `unref()` and the five shipped servers (mutex, election, single-flight, ssh twice), testing's `EngineProxy` ([ADR 0007](./docs/adr/0007-the-tests-move-first-and-each-shipped-server-moves-when-close-or-shutdown-is-its-kind-of-end.md)).
115
+
116
+ ## Who uses it
117
+
118
+ | Consumer | How | Record |
119
+ | ---------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- | ------------- |
120
+ | 106 test files of elt (49), text2sql (20), sdk-it (18), finetuning (9), mteb (6), deepagents (2), context-compiler (1), offers-evals (1) | `HttpServer`, today from `@zukhruf/testing/http` | backlog #6078 |
121
+ | docker's 12 test files of the engine | `HttpServer` on a `PipeEndpoint`, today `fakeEngine` | backlog #6078 |
122
+ | ssh's `client`, `config-dial-route` and `config-dial` tests | `Server` with no handler, today `startSilentServer` | backlog #6078 |
123
+ | 6 mutex and 3 single-flight test files | `Server` on a `PipeEndpoint`, today `standInServer` | backlog #6078 |
124
+ | ssh `control.test.ts`, `owner-death.test.ts` | `Server` with `allowHalfOpen`, today `listen` | backlog #6078 |
125
+ | 13 files of docker, ssh, testing and tools/eslint-rules | `serving.address.port`, today `portOf` | backlog #5947 |
126
+ | `@zukhruf/chaos` | `Proxy` is a handler that a `Server` serves | backlog #6078 |
127
+
128
+ ## The same concepts in other systems
129
+
130
+ | Concept | Other systems |
131
+ | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
132
+ | An endpoint | .NET `EndPoint`, `IPEndPoint`, `UnixDomainSocketEndPoint`; Go x/net [`nettest.NewLocalListener`](https://pkg.go.dev/golang.org/x/net/nettest#NewLocalListener) |
133
+ | A listener that leaves its connections | Go [`net.Listener`](https://pkg.go.dev/net#Listener), .NET `IConnectionListener`, Node.js [`net.Server.close`](https://nodejs.org/api/net.html#serverclosecallback), `@zukhruf/pipes` `PipeServer` |
134
+ | A close that ends the connections | Go [`http.Server.Close`](https://pkg.go.dev/net/http#Server.Close), [`httptest.Server.Close`](https://pkg.go.dev/net/http/httptest#Server.Close), Kestrel `AbortAllConnectionsAsync` |
135
+ | A shutdown with a limit | Go [`http.Server.Shutdown(ctx)`](https://pkg.go.dev/net/http#Server.Shutdown), Kestrel [`CloseAllConnectionsAsync` then `AbortAllConnectionsAsync`](https://github.com/dotnet/aspnetcore/blob/v9.0.0/src/Servers/Kestrel/Core/src/Internal/Infrastructure/TransportManager.cs) |
136
+ | HTTP over a listener | Go [`http.Server.Serve(l)`](https://pkg.go.dev/net/http#Server.Serve), Node.js [`emit('connection')`](https://nodejs.org/api/http.html#event-connection) |
137
+ | The origin of a test server | Go httptest [`Server.URL`](https://pkg.go.dev/net/http/httptest#Server) |
138
+
139
+ ## Why not a package of npm
140
+
141
+ The leaders' standard libraries split listener and server, and Node.js 26 gives every part but the tracking of accepted sockets on a `net.Server`: `net.Server` has no `closeAllConnections` (probe of 2026-11-28). The package adds that one part. No npm package does that part (probe of 2026-11-28 on Node.js 26). `stoppable` 1.1.0 (last release 2018) ends and destroys the sockets of a raw `net.Server`, but its grace is a fixed time with no abort signal, and with sockets that `emit('connection')` gave an `http.Server` it calls back before their close. `http-terminator` 3.2.0 (last release 2022) skips the sockets of a raw `net.Server` and polls every 10 ms. Both lean on `server.close()`, so neither waits for sockets that the server did not accept itself.
142
+
143
+ ## API
144
+
145
+ ```ts
146
+ import type {
147
+ IncomingMessage,
148
+ ServerOptions as NodeHttpServerOptions,
149
+ ServerResponse,
150
+ } from 'node:http';
151
+ import type { Socket, SocketAddress } from 'node:net';
152
+ import type { Duplex } from 'node:stream';
153
+
154
+ /** Gets each accepted socket. */
155
+ export type Handler = (socket: Socket) => void;
156
+
157
+ /** Accepts connections and gives each one to a handler. Its close leaves each accepted socket open. Go's net.Listener. */
158
+ export interface Listener<A> extends AsyncDisposable {
159
+ readonly address: A;
160
+ close(): Promise<void>;
161
+ }
162
+
163
+ /** Where a server listens. PipeEndpoint of @zukhruf/pipes is one. */
164
+ export interface Endpoint<A> {
165
+ listen(handler: Handler): Promise<Listener<A>>;
166
+ }
167
+
168
+ export interface TcpEndpointOptions {
169
+ /** An IP address. Default '127.0.0.1'. */
170
+ readonly host?: string;
171
+ /** Default 0: a free port that the system chooses. */
172
+ readonly port?: number;
173
+ }
174
+
175
+ export class TcpEndpoint implements Endpoint<SocketAddress> {
176
+ constructor(options?: TcpEndpointOptions);
177
+ listen(handler: Handler): Promise<Listener<SocketAddress>>;
178
+ }
179
+
180
+ export interface ServerOptions {
181
+ /** Set on each accepted socket before the handler gets it. Default false, as node:net. */
182
+ readonly allowHalfOpen?: boolean;
183
+ }
184
+
185
+ /** What to do with each connection. It listens on nothing until serve(). */
186
+ export class Server {
187
+ constructor(handler: Handler, options?: ServerOptions);
188
+ serve<A>(endpoint: Endpoint<A>): Promise<Serving<A>>;
189
+ }
190
+
191
+ /** A server while it listens on one endpoint. Only serve() makes one. */
192
+ export interface Serving<A> extends AsyncDisposable {
193
+ readonly address: A;
194
+ /** The accepted sockets that are open now. */
195
+ readonly connections: number;
196
+ /** Stops the accepts, destroys each accepted socket, waits for each close. */
197
+ close(): Promise<void>;
198
+ /** Stops the accepts, waits until each accepted socket has closed; at the abort of signal, closes the rest. */
199
+ shutdown(signal: AbortSignal): Promise<void>;
200
+ [Symbol.asyncDispose](): Promise<void>;
201
+ }
202
+ ```
203
+
204
+ ```ts
205
+ // @zukhruf/net/http
206
+ /** A request listener of node:http. A rejection stays unhandled and fails the process. */
207
+ export type HttpHandler = (
208
+ request: IncomingMessage,
209
+ response: ServerResponse,
210
+ ) => unknown;
211
+
212
+ export interface HttpServerOptions extends NodeHttpServerOptions {
213
+ /** A listener of the upgrade event. The serving destroys its socket at the close. */
214
+ readonly upgrade?: (
215
+ request: IncomingMessage,
216
+ socket: Duplex,
217
+ head: Buffer,
218
+ ) => void;
219
+ }
220
+
221
+ export class HttpServer {
222
+ constructor(handler: HttpHandler, options?: HttpServerOptions);
223
+ serve(endpoint: Endpoint<SocketAddress>): Promise<HttpServing<SocketAddress>>;
224
+ serve<A>(endpoint: Endpoint<A>): Promise<HttpServing<A>>;
225
+ }
226
+
227
+ /** An HTTP serving. Only an endpoint with a SocketAddress gives it an origin. */
228
+ export type HttpServing<A> = Serving<A> &
229
+ (A extends SocketAddress ? { readonly origin: string } : unknown);
230
+ ```
231
+
232
+ ## Decided in the review
233
+
234
+ Each item is a detail that a caller sees. Ezz approved each proposal on 2026-11-28 ("Approve as written"; item 2, `address` on `PipeServer`: "Add address"), and each follows the leader that it names. The npm candidates are probed before the build.
235
+
236
+ 1. **The type of `serving.address`.** The record says `net.SocketAddress`. It cannot hold a pipe's path. Proposal: `Endpoint<A>` and `Serving<A>`, so the address is a `net.SocketAddress` for TCP and a `PipeAddress` for a pipe (.NET's `IConnectionListener.EndPoint`, typed by the endpoint) ([ADR 0003](./docs/adr/0003-an-endpoint-listens-and-gives-a-listener-that-never-ends-an-accepted-socket.md)).
237
+ 2. **`address` on `PipeServer`.** Ezz: "Add address". `PipeServer.address` is the address of its endpoint, after Go's `Listener.Addr()`, since 663e84b0, so a `PipeEndpoint` meets `Endpoint`.
238
+ 3. **`allowHalfOpen` is an option of `Server`, not of `TcpEndpoint`.** `Endpoint#listen(handler)` takes no options, so the serving sets `socket.allowHalfOpen = true` on each accepted socket before the handler (probe of 2026-11-28: a server that sets it after the accept answers after the client's end). Node.js has it as an option of `net.createServer`. ssh's `listen` and testing's `slowProxy` use it.
239
+ 4. **The serving ignores the `error` event of each accepted socket.** Each copy adds `socket.on('error', () => {})`, because a reset of the peer is normal in a test and an `error` with no listener ends the process. The handler still sees each error in its reads and writes, as a Go `Read` and `Write` return it. Proposal: yes.
240
+ 5. **`serving.connections`.** docker's `fakeEngine` has `openConnections`, and `slowProxy` has `connections()`, which 3 database tests of testing read. Proposal: `connections`, the accepted sockets that are open now. Go httptest and Kestrel expose no count; Toxiproxy lists the connections of a proxy.
241
+ 6. **docker's `allConnectionsClosed`.** It waits until each connection has closed, and the engine still listens. Proposal: no member; those tests call `shutdown(t.signal)`, which waits for the same thing and stops the accepts.
242
+ 7. **What `shutdown(signal)` gives after an abort.** Go's `Shutdown` returns the error of the context. Proposal: it closes the rest, then rejects with `signal.reason`, so a caller knows that the end was not graceful; this is also how `Command#run` of `@zukhruf/process` rejects at the abort of its signal.
243
+ 8. **No fallback to `[::1]`.** httptest and nettest try `[::1]:0` when `127.0.0.1:0` fails. Proposal: no fallback; the default host is a ruling, and a caller gives `host: '::1'`.
244
+ 9. **The type of `Handler`.** Proposal: `(socket: Socket) => void`, as the `connectionListener` of `net.createServer` and of `PipeEndpoint#listen`.
245
+ 10. **`origin` only on TCP.** Proposal: `HttpServing<A>` has `origin` only when `A` is a `net.SocketAddress`, so a serving on a pipe has no URL to misuse (httptest's `URL`).
246
+ 11. **`HttpServer` options.** Proposal: the options of `http.createServer` and `upgrade`. Go's `http.Server` has its fields; Node's `http.createServer(options)` has these.
247
+ 12. **The call of the 106 importers changes.** The record of 2026-11-27 says "change one import, no alias"; the composable shape of 2026-11-28 changes the call too (see the table in [Serve HTTP](#serve-http)). Proposal: the later ruling, with no `start()` kept.
248
+ 13. **npm candidates.** Probed on 2026-11-28: `stoppable` and `http-terminator` do part of the job and fail at the rest (see [Why not a package of npm](#why-not-a-package-of-npm) above), so the package tracks its sockets itself.
249
+
250
+ ## Documentation
251
+
252
+ **Decisions**
253
+
254
+ - [ADR 0001](./docs/adr/0001-a-base-package-above-pipes-owns-the-connections-that-a-server-accepts-and-testings-httpserver-converges-into-it.md): a base package above pipes owns the connections that a server accepts, and testing's HttpServer converges into it.
255
+ - [ADR 0002](./docs/adr/0002-the-name-is-net-after-go-net-and-node-net.md): the name is net, after Go net and Node net.
256
+ - [ADR 0003](./docs/adr/0003-an-endpoint-listens-and-gives-a-listener-that-never-ends-an-accepted-socket.md): an endpoint listens and gives a listener that never ends an accepted socket.
257
+ - [ADR 0004](./docs/adr/0004-a-server-is-a-value-and-serve-gives-a-serving-that-owns-each-socket-that-it-accepted.md): a server is a value, and serve gives a serving that owns each socket that it accepted.
258
+ - [ADR 0005](./docs/adr/0005-close-ends-each-accepted-socket-and-waits-for-its-close-and-shutdown-waits-for-the-sockets-until-its-signal-aborts.md): close ends each accepted socket and waits for its close, and shutdown waits for the sockets until its signal aborts.
259
+ - [ADR 0006](./docs/adr/0006-the-http-view-is-a-server-whose-handler-gives-each-socket-to-an-http-server.md): the HTTP view is a server whose handler gives each socket to an http.Server.
260
+ - [ADR 0007](./docs/adr/0007-the-tests-move-first-and-each-shipped-server-moves-when-close-or-shutdown-is-its-kind-of-end.md): the tests move first, and each shipped server moves when close or shutdown is its kind of end.
@@ -0,0 +1,16 @@
1
+ import type { Socket } from 'node:net';
2
+ /** Gets each accepted socket, as the `connectionListener` of `net.createServer`. */
3
+ export type Handler = (socket: Socket) => void;
4
+ /** Accepts connections and gives each one to a handler. Its close leaves each accepted socket open. Go's net.Listener. */
5
+ export interface Listener<A> extends AsyncDisposable {
6
+ /** Where a client connects. Go's `Listener.Addr()`. */
7
+ readonly address: A;
8
+ /** Stops the accepts; resolves when the listener has closed. */
9
+ close(): Promise<void>;
10
+ }
11
+ /** Where a server listens. `PipeEndpoint` of `@zukhruf/pipes` is one. */
12
+ export interface Endpoint<A> {
13
+ /** Resolves with a listener when it listens. */
14
+ listen(handler: Handler): Promise<Listener<A>>;
15
+ }
16
+ //# sourceMappingURL=endpoint.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"endpoint.d.ts","sourceRoot":"","sources":["../src/endpoint.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAEvC,oFAAoF;AACpF,MAAM,MAAM,OAAO,GAAG,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;AAE/C,0HAA0H;AAC1H,MAAM,WAAW,QAAQ,CAAC,CAAC,CAAE,SAAQ,eAAe;IAClD,uDAAuD;IACvD,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;IACpB,gEAAgE;IAChE,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAED,yEAAyE;AACzE,MAAM,WAAW,QAAQ,CAAC,CAAC;IACzB,gDAAgD;IAChD,MAAM,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;CAChD"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=endpoint.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"endpoint.js","sourceRoot":"","sources":["../src/endpoint.ts"],"names":[],"mappings":""}
@@ -0,0 +1,23 @@
1
+ import { type IncomingMessage, type ServerOptions as NodeHttpServerOptions, type ServerResponse } from 'node:http';
2
+ import { SocketAddress } from 'node:net';
3
+ import type { Duplex } from 'node:stream';
4
+ import type { Endpoint } from '../endpoint.ts';
5
+ import { type Serving } from '../server.ts';
6
+ /** A request listener of node:http. A rejection stays unhandled and fails the process. */
7
+ export type HttpHandler = (request: IncomingMessage, response: ServerResponse) => unknown;
8
+ export interface HttpServerOptions extends NodeHttpServerOptions {
9
+ /** A listener of the upgrade event. The serving destroys its socket at the close. */
10
+ readonly upgrade?: (request: IncomingMessage, socket: Duplex, head: Buffer) => void;
11
+ }
12
+ /** An HTTP serving. Only an endpoint with a SocketAddress gives it an origin. */
13
+ export type HttpServing<A> = Serving<A> & (A extends SocketAddress ? {
14
+ readonly origin: string;
15
+ } : unknown);
16
+ /** A server whose handler gives each accepted socket to an `http.Server` of its serving, as Go's `http.Server.Serve(l)`. */
17
+ export declare class HttpServer {
18
+ #private;
19
+ constructor(handler: HttpHandler, options?: HttpServerOptions);
20
+ serve(endpoint: Endpoint<SocketAddress>): Promise<HttpServing<SocketAddress>>;
21
+ serve<A>(endpoint: Endpoint<A>): Promise<HttpServing<A>>;
22
+ }
23
+ //# sourceMappingURL=http-server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http-server.d.ts","sourceRoot":"","sources":["../../src/http/http-server.ts"],"names":[],"mappings":"AACA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import { Socket, SocketAddress } from 'node:net';
4
+ import { Server } from '../server.js';
5
+ /** A server whose handler gives each accepted socket to an `http.Server` of its serving, as Go's `http.Server.Serve(l)`. */
6
+ export class HttpServer {
7
+ #handler;
8
+ #options;
9
+ constructor(handler, options = {}) {
10
+ this.#handler = handler;
11
+ this.#options = options;
12
+ }
13
+ async serve(endpoint) {
14
+ const { upgrade, ...options } = this.#options;
15
+ const handler = this.#handler;
16
+ const connections = new HttpConnections(createServer(options, (request, response) => {
17
+ connections.request(request.socket, response);
18
+ void handler(request, response);
19
+ }), upgrade);
20
+ const clients = new ClientSockets();
21
+ try {
22
+ const serving = await new Server((socket) => {
23
+ connections.accept(socket);
24
+ }).serve(endpoint);
25
+ const { address } = serving;
26
+ if (address instanceof SocketAddress) {
27
+ return new TcpHttpServing(serving, address, connections, clients);
28
+ }
29
+ clients.stop();
30
+ return new HttpServingOf(serving, connections, noClients);
31
+ }
32
+ catch (error) {
33
+ clients.stop();
34
+ throw error;
35
+ }
36
+ }
37
+ }
38
+ /** The sockets that a serving gave its `http.Server`, and whether a request is in flight on each, as Go keeps `ConnState`. */
39
+ class HttpConnections {
40
+ /** Each accepted socket until its close, also an upgraded one. */
41
+ accepted = new Set();
42
+ /** The requests in flight on each HTTP connection; an upgraded socket leaves it. */
43
+ #inFlight = new Map();
44
+ #http;
45
+ /** What happens to a connection when its last request in flight ends: it stays open until the serving shuts down. */
46
+ #idle = keepOpen;
47
+ constructor(http, upgrade) {
48
+ this.#http = http;
49
+ if (upgrade === undefined)
50
+ return;
51
+ http.on('upgrade', (request, socket, head) => {
52
+ this.#inFlight.delete(request.socket);
53
+ upgrade(request, socket, head);
54
+ });
55
+ }
56
+ accept(socket) {
57
+ this.accepted.add(socket);
58
+ this.#inFlight.set(socket, 0);
59
+ socket.once('close', () => {
60
+ this.accepted.delete(socket);
61
+ this.#inFlight.delete(socket);
62
+ });
63
+ this.#http.emit('connection', socket);
64
+ }
65
+ request(socket, response) {
66
+ this.#inFlight.set(socket, (this.#inFlight.get(socket) ?? 0) + 1);
67
+ response.once('close', () => {
68
+ const left = (this.#inFlight.get(socket) ?? 1) - 1;
69
+ this.#inFlight.set(socket, left);
70
+ if (left === 0)
71
+ this.#idle(socket);
72
+ });
73
+ }
74
+ /** Ends each idle connection now, and each other one after its response, as Go's `Shutdown`. */
75
+ shutDown() {
76
+ this.#idle = (socket) => {
77
+ socket.destroySoon();
78
+ };
79
+ for (const [socket, inFlight] of this.#inFlight) {
80
+ if (inFlight === 0)
81
+ socket.destroy();
82
+ }
83
+ }
84
+ }
85
+ /** A pipe gives a client socket no address to pair with a connection (Node.js 26: `remoteAddress` and `localPort` are undefined). */
86
+ const noClients = {
87
+ pairs: () => [],
88
+ stop() { },
89
+ };
90
+ class HttpServingOf {
91
+ #serving;
92
+ #connections;
93
+ #clients;
94
+ constructor(serving, connections, clients) {
95
+ this.#serving = serving;
96
+ this.#connections = connections;
97
+ this.#clients = clients;
98
+ }
99
+ get address() {
100
+ return this.#serving.address;
101
+ }
102
+ get connections() {
103
+ return this.#serving.connections;
104
+ }
105
+ /**
106
+ * The close of the serving; then it destroys each client socket of this
107
+ * process that pairs with a connection that the close destroyed, and waits
108
+ * for the first `close` of each pair, so that undici clears its timers
109
+ * before the next test fakes them (testing ADR 0030 and its amendment).
110
+ */
111
+ async close() {
112
+ const pairs = this.#clients.pairs(this.#connections.accepted);
113
+ try {
114
+ await this.#serving.close();
115
+ }
116
+ finally {
117
+ await Promise.all(pairs
118
+ .filter(({ connection, peers }) => connection.destroyed && peers.length > 0)
119
+ .map(({ peers }) => Promise.race(peers.map(destroyed))));
120
+ this.#clients.stop();
121
+ }
122
+ }
123
+ /** The shutdown of the serving, which ends each idle connection at once and each other one after its response. A client of this process reads the end of each connection itself. */
124
+ async shutdown(signal) {
125
+ const end = this.#serving.shutdown(signal);
126
+ this.#connections.shutDown();
127
+ try {
128
+ await end;
129
+ }
130
+ finally {
131
+ this.#clients.stop();
132
+ }
133
+ }
134
+ [Symbol.asyncDispose]() {
135
+ return this.close();
136
+ }
137
+ }
138
+ /** An HTTP serving on TCP: it has an origin, as httptest's `Server.URL`. */
139
+ class TcpHttpServing extends HttpServingOf {
140
+ origin;
141
+ constructor(serving, address, connections, clients) {
142
+ super(serving, connections, clients.at(address));
143
+ const host = address.family === 'ipv6' ? `[${address.address}]` : address.address;
144
+ this.origin = `http://${host}:${address.port}`;
145
+ }
146
+ }
147
+ /** Each client socket of this process, from Node's `net.client.socket` diagnostics channel. */
148
+ class ClientSockets {
149
+ #sockets = new Set();
150
+ #channel = channel('net.client.socket');
151
+ #record = (message) => {
152
+ const socket = Reflect.get(Object(message), 'socket');
153
+ if (!(socket instanceof Socket))
154
+ return;
155
+ this.#sockets.add(socket);
156
+ socket.once('close', () => {
157
+ this.#sockets.delete(socket);
158
+ });
159
+ };
160
+ constructor() {
161
+ this.#channel.subscribe(this.#record);
162
+ }
163
+ /** Pairs each connection with the open client sockets connected to `address` from its remote port. */
164
+ at(address) {
165
+ return {
166
+ pairs: (connections) => [...connections].map((connection) => ({
167
+ connection,
168
+ peers: [...this.#sockets].filter((client) => !client.destroyed &&
169
+ client.remoteAddress === address.address &&
170
+ client.remotePort === address.port &&
171
+ client.localPort === connection.remotePort),
172
+ })),
173
+ stop: () => {
174
+ this.stop();
175
+ },
176
+ };
177
+ }
178
+ stop() {
179
+ this.#channel.unsubscribe(this.#record);
180
+ }
181
+ }
182
+ /** Destroys `client` and resolves at its `close`, or at once when it has closed. */
183
+ function destroyed(client) {
184
+ if (client.closed)
185
+ return Promise.resolve();
186
+ return new Promise((resolve) => {
187
+ client.once('close', () => {
188
+ resolve();
189
+ });
190
+ client.destroy();
191
+ });
192
+ }
193
+ function keepOpen() { }
194
+ //# sourceMappingURL=http-server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http-server.js","sourceRoot":"","sources":["../../src/http/http-server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACnD,OA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,WAAW,CAAC,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC;gBACpC,UAAU;gBACV,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,MAAM,CAC9B,CAAC,MAAM,EAAE,EAAE,CACT,CAAC,MAAM,CAAC,SAAS;oBACjB,MAAM,CAAC,aAAa,KAAK,OAAO,CAAC,OAAO;oBACxC,MAAM,CAAC,UAAU,KAAK,OAAO,CAAC,IAAI;oBAClC,MAAM,CAAC,SAAS,KAAK,UAAU,CAAC,UAAU,CAC7C;aACF,CAAC,CAAC;YACL,IAAI,EAAE,GAAG,EAAE;gBACT,IAAI,CAAC,IAAI,EAAE,CAAC;YACd,CAAC;SACF,CAAC;IACJ,CAAC;IAED,IAAI;QACF,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC1C,CAAC;CACF;AAED,oFAAoF;AACpF,SAAS,SAAS,CAAC,MAAc;IAC/B,IAAI,MAAM,CAAC,MAAM;QAAE,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;IAC5C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE;YACxB,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC,CAAC;QACH,MAAM,CAAC,OAAO,EAAE,CAAC;IACnB,CAAC,CAAC,CAAC;AACL,CAAC;AAED,SAAS,QAAQ,KAAU,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { HttpServer, type HttpHandler, type HttpServerOptions, type HttpServing, } from './http-server.ts';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/http/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,KAAK,WAAW,EAChB,KAAK,iBAAiB,EACtB,KAAK,WAAW,GACjB,MAAM,kBAAkB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { HttpServer, } from './http-server.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/http/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,GAIX,MAAM,kBAAkB,CAAC"}
@@ -0,0 +1,4 @@
1
+ export type { Endpoint, Handler, Listener } from './endpoint.ts';
2
+ export { Server, type ServerOptions, type Serving } from './server.ts';
3
+ export { TcpEndpoint, type TcpEndpointOptions } from './tcp-endpoint.ts';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACjE,OAAO,EAAE,MAAM,EAAE,KAAK,aAAa,EAAE,KAAK,OAAO,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,WAAW,EAAE,KAAK,kBAAkB,EAAE,MAAM,mBAAmB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { Server } from './server.js';
2
+ export { TcpEndpoint } from './tcp-endpoint.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAoC,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,WAAW,EAA2B,MAAM,mBAAmB,CAAC"}
@@ -0,0 +1,29 @@
1
+ import type { Endpoint, Handler } from './endpoint.ts';
2
+ export interface ServerOptions {
3
+ /** Set on each accepted socket before the handler gets it. Default false, as node:net. */
4
+ readonly allowHalfOpen?: boolean;
5
+ }
6
+ /** A server while it listens on one endpoint. Only `Server#serve` makes one. */
7
+ export interface Serving<A> extends AsyncDisposable {
8
+ /** Where a client connects: the address of the endpoint's listener. */
9
+ readonly address: A;
10
+ /** The accepted sockets that are open now. */
11
+ readonly connections: number;
12
+ /** Stops the accepts, destroys each accepted socket, waits for each close. */
13
+ close(): Promise<void>;
14
+ /**
15
+ * Stops the accepts, waits until each accepted socket has closed; at the
16
+ * abort of signal, destroys the rest, waits for their close, and rejects
17
+ * with `signal.reason`.
18
+ */
19
+ shutdown(signal: AbortSignal): Promise<void>;
20
+ [Symbol.asyncDispose](): Promise<void>;
21
+ }
22
+ /** What to do with each connection. It listens on nothing until `serve()`. */
23
+ export declare class Server {
24
+ #private;
25
+ constructor(handler: Handler, { allowHalfOpen }?: ServerOptions);
26
+ /** Listens on `endpoint`; resolves when it listens. */
27
+ serve<A>(endpoint: Endpoint<A>): Promise<Serving<A>>;
28
+ }
29
+ //# sourceMappingURL=server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAY,MAAM,eAAe,CAAC;AAEjE,MAAM,WAAW,aAAa;IAC5B,0FAA0F;IAC1F,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC;CAClC;AAED,gFAAgF;AAChF,MAAM,WAAW,OAAO,CAAC,CAAC,CAAE,SAAQ,eAAe;IACjD,uEAAuE;IACvE,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;IACpB,8CAA8C;IAC9C,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,8EAA8E;IAC9E,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7C,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxC;AAED,8EAA8E;AAC9E,qBAAa,MAAM;;IAIjB,YAAY,OAAO,EAAE,OAAO,EAAE,EAAE,aAAqB,EAAE,GAAE,aAAkB,EAG1E;IAED,uDAAuD;IACjD,KAAK,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAQzD;CACF"}
package/dist/server.js ADDED
@@ -0,0 +1,125 @@
1
+ /** What to do with each connection. It listens on nothing until `serve()`. */
2
+ export class Server {
3
+ #handler;
4
+ #allowHalfOpen;
5
+ constructor(handler, { allowHalfOpen = false } = {}) {
6
+ this.#handler = handler;
7
+ this.#allowHalfOpen = allowHalfOpen;
8
+ }
9
+ /** Listens on `endpoint`; resolves when it listens. */
10
+ async serve(endpoint) {
11
+ const sockets = new AcceptedSockets();
12
+ const listener = await endpoint.listen((socket) => {
13
+ socket.allowHalfOpen = this.#allowHalfOpen;
14
+ sockets.add(socket);
15
+ this.#handler(socket);
16
+ });
17
+ return new ListeningServing(listener, sockets);
18
+ }
19
+ }
20
+ /** Each socket that a serving accepted, from the accept to its `close` event. */
21
+ class AcceptedSockets {
22
+ #open = new Set();
23
+ get size() {
24
+ return this.#open.size;
25
+ }
26
+ /** A reset of the peer is normal; the handler still sees each error in its reads and writes. */
27
+ add(socket) {
28
+ socket.on('error', ignore);
29
+ this.#open.add(socket);
30
+ socket.once('close', () => {
31
+ this.#open.delete(socket);
32
+ });
33
+ }
34
+ /** Destroys each open socket; true when one was open. */
35
+ destroy() {
36
+ for (const socket of this.#open)
37
+ socket.destroy();
38
+ return this.#open.size > 0;
39
+ }
40
+ /** Resolves when each socket that is open now has emitted `close`. */
41
+ async closed() {
42
+ await Promise.all([...this.#open].map((socket) => new Promise((resolve) => {
43
+ socket.once('close', () => {
44
+ resolve();
45
+ });
46
+ })));
47
+ }
48
+ }
49
+ /** The listener's close waits until each connection has ended, so the sockets are destroyed before the wait (ADR 0005). */
50
+ async function closed({ listener, sockets }) {
51
+ const closing = listener.close();
52
+ sockets.destroy();
53
+ await Promise.all([closing, sockets.closed()]);
54
+ }
55
+ /** The listener has closed and each socket that is open now has emitted `close`. */
56
+ async function ended({ listener, sockets }) {
57
+ await Promise.all([listener.close(), sockets.closed()]);
58
+ }
59
+ /** The serving listens: the first close or shutdown ends it. */
60
+ const listening = {
61
+ close: closed,
62
+ async shutdown(parts, signal) {
63
+ const end = ended(parts);
64
+ const { promise: abort, resolve } = Promise.withResolvers();
65
+ const onAbort = () => {
66
+ resolve('aborted');
67
+ };
68
+ signal.addEventListener('abort', onAbort, { once: true });
69
+ if (signal.aborted)
70
+ onAbort();
71
+ try {
72
+ const first = await Promise.race([
73
+ end.then(() => 'ended'),
74
+ abort,
75
+ ]);
76
+ const forced = first === 'aborted' && parts.sockets.destroy();
77
+ await end;
78
+ if (forced)
79
+ throw signal.reason;
80
+ }
81
+ finally {
82
+ signal.removeEventListener('abort', onAbort);
83
+ }
84
+ },
85
+ };
86
+ /** A close or shutdown began: a later call waits for the same end, and a close is the abort of a shutdown (ADR 0005). */
87
+ const ending = {
88
+ close: closed,
89
+ shutdown: ended,
90
+ };
91
+ class ListeningServing {
92
+ #listener;
93
+ #sockets;
94
+ #phase = listening;
95
+ constructor(listener, sockets) {
96
+ this.#listener = listener;
97
+ this.#sockets = sockets;
98
+ }
99
+ get address() {
100
+ return this.#listener.address;
101
+ }
102
+ get connections() {
103
+ return this.#sockets.size;
104
+ }
105
+ close() {
106
+ return this.#next().close(this.#parts());
107
+ }
108
+ shutdown(signal) {
109
+ return this.#next().shutdown(this.#parts(), signal);
110
+ }
111
+ [Symbol.asyncDispose]() {
112
+ return this.close();
113
+ }
114
+ #next() {
115
+ const phase = this.#phase;
116
+ this.#phase = ending;
117
+ return phase;
118
+ }
119
+ #parts() {
120
+ return { listener: this.#listener, sockets: this.#sockets };
121
+ }
122
+ }
123
+ /** A reset of the peer is normal in a test, and an `error` with no listener ends the process. */
124
+ function ignore() { }
125
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@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,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;IAC3C,CAAC;IAED,QAAQ,CAAC,MAAmB;QAC1B,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,EAAE,EAAE,MAAM,CAAC,CAAC;IACtD,CAAC;IAED,CAAC,MAAM,CAAC,YAAY,CAAC;QACnB,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;IAED,KAAK;QACH,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM;QACJ,OAAO,EAAE,QAAQ,EAAE,IAAI,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,CAAC,QAAQ,EAAE,CAAC;IAC9D,CAAC;CACF;AAED,iGAAiG;AACjG,SAAS,MAAM,KAAU,CAAC"}
@@ -0,0 +1,15 @@
1
+ import { SocketAddress } from 'node:net';
2
+ import type { Endpoint, Handler, Listener } from './endpoint.ts';
3
+ export interface TcpEndpointOptions {
4
+ /** An IP address. Default '127.0.0.1'. */
5
+ readonly host?: string;
6
+ /** Default 0: a free port that the system chooses. */
7
+ readonly port?: number;
8
+ }
9
+ /** A TCP host and port to listen on, over `net.Server.listen(port, host)`. .NET's `IPEndPoint`. */
10
+ export declare class TcpEndpoint implements Endpoint<SocketAddress> {
11
+ #private;
12
+ constructor({ host, port }?: TcpEndpointOptions);
13
+ listen(handler: Handler): Promise<Listener<SocketAddress>>;
14
+ }
15
+ //# sourceMappingURL=tcp-endpoint.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tcp-endpoint.d.ts","sourceRoot":"","sources":["../src/tcp-endpoint.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,aAAa,EAEd,MAAM,UAAU,CAAC;AAElB,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEjE,MAAM,WAAW,kBAAkB;IACjC,0CAA0C;IAC1C,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,sDAAsD;IACtD,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,mGAAmG;AACnG,qBAAa,WAAY,YAAW,QAAQ,CAAC,aAAa,CAAC;;IAIzD,YAAY,EAAE,IAAkB,EAAE,IAAQ,EAAE,GAAE,kBAAuB,EAGpE;IAEK,MAAM,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAU/D;CACF"}
@@ -0,0 +1,51 @@
1
+ import assert from 'node:assert/strict';
2
+ import { once } from 'node:events';
3
+ import { SocketAddress, createServer, } from 'node:net';
4
+ /** A TCP host and port to listen on, over `net.Server.listen(port, host)`. .NET's `IPEndPoint`. */
5
+ export class TcpEndpoint {
6
+ #host;
7
+ #port;
8
+ constructor({ host = '127.0.0.1', port = 0 } = {}) {
9
+ this.#host = host;
10
+ this.#port = port;
11
+ }
12
+ async listen(handler) {
13
+ const server = createServer(handler);
14
+ try {
15
+ server.listen(this.#port, this.#host);
16
+ await once(server, 'listening');
17
+ }
18
+ catch (error) {
19
+ server.close();
20
+ throw error;
21
+ }
22
+ return new TcpListener(server);
23
+ }
24
+ }
25
+ class TcpListener {
26
+ address;
27
+ #server;
28
+ constructor(server) {
29
+ this.#server = server;
30
+ const info = server.address();
31
+ assert.ok(info !== null && typeof info === 'object');
32
+ const { address, port, family } = info;
33
+ this.address = new SocketAddress({
34
+ address,
35
+ port,
36
+ family: family === 'IPv6' ? 'ipv6' : 'ipv4',
37
+ });
38
+ }
39
+ /** Node calls back at the `close` event, with `ERR_SERVER_NOT_RUNNING` only when an earlier call already closed the server. */
40
+ close() {
41
+ return new Promise((resolve, reject) => {
42
+ this.#server.close((error) => error && Reflect.get(error, 'code') !== 'ERR_SERVER_NOT_RUNNING'
43
+ ? reject(error)
44
+ : resolve());
45
+ });
46
+ }
47
+ [Symbol.asyncDispose]() {
48
+ return this.close();
49
+ }
50
+ }
51
+ //# sourceMappingURL=tcp-endpoint.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tcp-endpoint.js","sourceRoot":"","sources":["../src/tcp-endpoint.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,EAEL,aAAa,EACb,YAAY,GACb,MAAM,UAAU,CAAC;AAWlB,mGAAmG;AACnG,MAAM,OAAO,WAAW;IACb,KAAK,CAAS;IACd,KAAK,CAAS;IAEvB,YAAY,EAAE,IAAI,GAAG,WAAW,EAAE,IAAI,GAAG,CAAC,EAAE,GAAuB,EAAE;QACnE,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACpB,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,OAAgB;QAC3B,MAAM,MAAM,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;QACrC,IAAI,CAAC;YACH,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;YACtC,MAAM,IAAI,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;QAClC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,CAAC,KAAK,EAAE,CAAC;YACf,MAAM,KAAK,CAAC;QACd,CAAC;QACD,OAAO,IAAI,WAAW,CAAC,MAAM,CAAC,CAAC;IACjC,CAAC;CACF;AAED,MAAM,WAAW;IACN,OAAO,CAAgB;IACvB,OAAO,CAAY;IAE5B,YAAY,MAAiB;QAC3B,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC;QAC9B,MAAM,CAAC,EAAE,CAAC,IAAI,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC;QACrD,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;QACvC,IAAI,CAAC,OAAO,GAAG,IAAI,aAAa,CAAC;YAC/B,OAAO;YACP,IAAI;YACJ,MAAM,EAAE,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM;SAC5C,CAAC,CAAC;IACL,CAAC;IAED,+HAA+H;IAC/H,KAAK;QACH,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAC3B,KAAK,IAAI,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,KAAK,wBAAwB;gBAC9D,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;gBACf,CAAC,CAAC,OAAO,EAAE,CACd,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IAED,CAAC,MAAM,CAAC,YAAY,CAAC;QACnB,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
2
  "name": "@zukhruf/net",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.8.222",
4
+ "description": "A server that accepts connections on a TCP port or a pipe and owns each socket: close destroys each one and waits for its close, shutdown waits until its signal aborts; @zukhruf/net/http serves HTTP the same way. After Go's net and net/http.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/JanuaryLabs/zukhruf",
9
+ "directory": "packages/net"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/JanuaryLabs/zukhruf/issues"
13
+ },
14
+ "homepage": "https://github.com/JanuaryLabs/zukhruf/tree/main/packages/net#readme",
15
+ "keywords": [
16
+ "net",
17
+ "server",
18
+ "listener",
19
+ "endpoint",
20
+ "tcp",
21
+ "unix-socket",
22
+ "http",
23
+ "graceful-shutdown",
24
+ "test-server"
25
+ ],
26
+ "type": "module",
27
+ "exports": {
28
+ "./package.json": "./package.json",
29
+ ".": {
30
+ "@zukhruf/source": "./src/index.ts",
31
+ "types": "./dist/index.d.ts",
32
+ "import": "./dist/index.js",
33
+ "default": "./dist/index.js"
34
+ },
35
+ "./http": {
36
+ "@zukhruf/source": "./src/http/index.ts",
37
+ "types": "./dist/http/index.d.ts",
38
+ "import": "./dist/http/index.js",
39
+ "default": "./dist/http/index.js"
40
+ }
41
+ },
42
+ "files": [
43
+ "dist",
44
+ "!dist/spec/**",
45
+ "!**/*.tsbuildinfo",
46
+ "!**/*.test.*"
47
+ ],
48
+ "publishConfig": {
49
+ "access": "public"
50
+ },
51
+ "devDependencies": {
52
+ "@zukhruf/fs": "^0.8.222",
53
+ "@zukhruf/parameterized-test": "^0.8.222",
54
+ "@zukhruf/pipes": "^0.8.222",
55
+ "@zukhruf/process": "^0.8.222"
56
+ }
57
+ }