@ac-kit/node 0.1.0 → 0.3.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 +94 -0
- package/README.md +7 -2
- package/dist/error/process-exit-error.d.ts +16 -3
- package/dist/error/process-exit-error.d.ts.map +1 -1
- package/dist/error/process-exit-error.js +51 -7
- package/dist/error/process-exit-error.js.map +1 -1
- package/dist/fs/path-escape-error.d.ts +20 -0
- package/dist/fs/path-escape-error.d.ts.map +1 -0
- package/dist/fs/path-escape-error.js +23 -0
- package/dist/fs/path-escape-error.js.map +1 -0
- package/dist/fs/path-within.d.ts +121 -0
- package/dist/fs/path-within.d.ts.map +1 -0
- package/dist/fs/path-within.js +196 -0
- package/dist/fs/path-within.js.map +1 -0
- package/dist/index.d.ts +9 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -2
- package/dist/index.js.map +1 -1
- package/dist/net/connection-closed-error.d.ts +12 -0
- package/dist/net/connection-closed-error.d.ts.map +1 -0
- package/dist/net/connection-closed-error.js +15 -0
- package/dist/net/connection-closed-error.js.map +1 -0
- package/dist/net/dgram-socket.d.ts +4 -3
- package/dist/net/dgram-socket.d.ts.map +1 -1
- package/dist/net/dgram-socket.js +38 -38
- package/dist/net/dgram-socket.js.map +1 -1
- package/dist/net/inet-address.d.ts +20 -0
- package/dist/net/inet-address.d.ts.map +1 -0
- package/dist/net/inet-address.js +42 -0
- package/dist/net/inet-address.js.map +1 -0
- package/dist/net/inet-endpoint.d.ts +7 -0
- package/dist/net/inet-endpoint.d.ts.map +1 -0
- package/dist/net/inet-endpoint.js +2 -0
- package/dist/net/inet-endpoint.js.map +1 -0
- package/dist/net/inet-socket.d.ts +65 -0
- package/dist/net/inet-socket.d.ts.map +1 -0
- package/dist/net/inet-socket.js +99 -0
- package/dist/net/inet-socket.js.map +1 -0
- package/dist/net/ipc-server.d.ts +63 -0
- package/dist/net/ipc-server.d.ts.map +1 -0
- package/dist/net/ipc-server.js +75 -0
- package/dist/net/ipc-server.js.map +1 -0
- package/dist/net/ipc-socket.d.ts +55 -22
- package/dist/net/ipc-socket.d.ts.map +1 -1
- package/dist/net/ipc-socket.js +50 -46
- package/dist/net/ipc-socket.js.map +1 -1
- package/dist/net/stream-server.d.ts +80 -0
- package/dist/net/stream-server.d.ts.map +1 -0
- package/dist/net/stream-server.js +129 -0
- package/dist/net/stream-server.js.map +1 -0
- package/dist/net/stream-socket.d.ts +37 -8
- package/dist/net/stream-socket.d.ts.map +1 -1
- package/dist/net/stream-socket.js +80 -27
- package/dist/net/stream-socket.js.map +1 -1
- package/dist/net/tcp-server.d.ts +31 -73
- package/dist/net/tcp-server.d.ts.map +1 -1
- package/dist/net/tcp-server.js +45 -150
- package/dist/net/tcp-server.js.map +1 -1
- package/dist/net/tcp-socket.d.ts +21 -61
- package/dist/net/tcp-socket.d.ts.map +1 -1
- package/dist/net/tcp-socket.js +23 -126
- package/dist/net/tcp-socket.js.map +1 -1
- package/dist/net/tls-server.d.ts +87 -0
- package/dist/net/tls-server.d.ts.map +1 -0
- package/dist/net/tls-server.js +103 -0
- package/dist/net/tls-server.js.map +1 -0
- package/dist/net/tls-socket.d.ts +235 -14
- package/dist/net/tls-socket.d.ts.map +1 -1
- package/dist/net/tls-socket.js +241 -91
- package/dist/net/tls-socket.js.map +1 -1
- package/package.json +4 -4
- package/dist/shell/escape-command-arg.d.ts +0 -9
- package/dist/shell/escape-command-arg.d.ts.map +0 -1
- package/dist/shell/escape-command-arg.js +0 -13
- package/dist/shell/escape-command-arg.js.map +0 -1
- package/dist/shell/escape-command.d.ts +0 -10
- package/dist/shell/escape-command.d.ts.map +0 -1
- package/dist/shell/escape-command.js +0 -12
- package/dist/shell/escape-command.js.map +0 -1
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { EventDispatcherMapBase } from "@ac-kit/async";
|
|
2
|
+
/**
|
|
3
|
+
* Base class for stream-oriented servers.
|
|
4
|
+
*
|
|
5
|
+
* This abstraction wraps a {@link net.Server} instance and exposes the lifecycle
|
|
6
|
+
* that is independent of the transport: binding state, connection accounting,
|
|
7
|
+
* event-loop referencing and closing.
|
|
8
|
+
*
|
|
9
|
+
* Binding and address reporting are transport-specific and are added by the
|
|
10
|
+
* concrete subclasses {@link TcpServer} and {@link IpcServer}, as is the
|
|
11
|
+
* "connection" event, whose payload is a socket of the matching type.
|
|
12
|
+
*/
|
|
13
|
+
export class StreamServer extends EventDispatcherMapBase {
|
|
14
|
+
server;
|
|
15
|
+
handledErrorEvents = new Set();
|
|
16
|
+
/**
|
|
17
|
+
* Creates a new {@link StreamServer} instance.
|
|
18
|
+
*
|
|
19
|
+
* @param server The underlying Node.js server.
|
|
20
|
+
*/
|
|
21
|
+
constructor(server) {
|
|
22
|
+
super();
|
|
23
|
+
this.server = server;
|
|
24
|
+
this.setupEventForwarding();
|
|
25
|
+
}
|
|
26
|
+
/** Whether the server is currently listening for connections. */
|
|
27
|
+
get listening() {
|
|
28
|
+
return this.server.listening;
|
|
29
|
+
}
|
|
30
|
+
/** Maximum number of concurrent connections accepted by the server. */
|
|
31
|
+
get maxConnections() {
|
|
32
|
+
return this.server.maxConnections;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Updates the maximum number of concurrent connections.
|
|
36
|
+
*
|
|
37
|
+
* Connections accepted beyond this limit are dropped.
|
|
38
|
+
*/
|
|
39
|
+
set maxConnections(maxConnections) {
|
|
40
|
+
this.server.maxConnections = maxConnections;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Counts the connections currently handled by the server.
|
|
44
|
+
*
|
|
45
|
+
* @returns A promise that resolves with the connection count.
|
|
46
|
+
*/
|
|
47
|
+
getConnections() {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
this.server.getConnections((error, count) => {
|
|
50
|
+
if (error) {
|
|
51
|
+
reject(error);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
resolve(count);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Marks the server as referenced, preventing the Node.js process from exiting
|
|
60
|
+
* while it is listening.
|
|
61
|
+
*/
|
|
62
|
+
ref() {
|
|
63
|
+
this.server.ref();
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Marks the server as unreferenced, allowing the Node.js process to exit even
|
|
67
|
+
* while it is listening.
|
|
68
|
+
*/
|
|
69
|
+
unref() {
|
|
70
|
+
this.server.unref();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Stops the server from accepting new connections. Established connections
|
|
74
|
+
* are not closed; the promise resolves once the last of them ends.
|
|
75
|
+
*
|
|
76
|
+
* @returns A promise that resolves when the server is fully closed.
|
|
77
|
+
* @throws When the server was not listening.
|
|
78
|
+
*/
|
|
79
|
+
close() {
|
|
80
|
+
return new Promise((resolve, reject) => {
|
|
81
|
+
this.server.close((error) => {
|
|
82
|
+
if (error) {
|
|
83
|
+
reject(error);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
resolve();
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Binds the server and resolves once it is listening.
|
|
92
|
+
*
|
|
93
|
+
* The bind error is consumed rather than dispatched, so that a rejected
|
|
94
|
+
* promise is not also reported as an unhandled "error" event.
|
|
95
|
+
*
|
|
96
|
+
* @param options The listen options to bind with. Without any, the server
|
|
97
|
+
* binds a system-assigned port on every interface.
|
|
98
|
+
* @returns A promise that resolves once the server is listening.
|
|
99
|
+
*/
|
|
100
|
+
listenOn(options) {
|
|
101
|
+
return new Promise((resolve, reject) => {
|
|
102
|
+
const handleError = (error) => {
|
|
103
|
+
this.handledErrorEvents.add(error);
|
|
104
|
+
reject(error);
|
|
105
|
+
};
|
|
106
|
+
this.server.prependOnceListener("error", handleError);
|
|
107
|
+
this.server.listen(options ?? {}, () => {
|
|
108
|
+
this.server.removeListener("error", handleError);
|
|
109
|
+
resolve();
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
setupEventForwarding() {
|
|
114
|
+
this.server.on("close", () => {
|
|
115
|
+
this.dispatch("close", []);
|
|
116
|
+
});
|
|
117
|
+
this.server.on("error", (error) => {
|
|
118
|
+
if (this.handledErrorEvents.has(error)) {
|
|
119
|
+
this.handledErrorEvents.delete(error);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
this.dispatch("error", [error]);
|
|
123
|
+
});
|
|
124
|
+
this.server.on("listening", () => {
|
|
125
|
+
this.dispatch("listening", []);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=stream-server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-server.js","sourceRoot":"","sources":["../../src/net/stream-server.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,sBAAsB,EAAuB,MAAM,eAAe,CAAC;AAe5E;;;;;;;;;;GAUG;AACH,MAAM,OAAO,YAIZ,SAAQ,sBAA+B;IAUR,MAAM;IAPlB,kBAAkB,GAAgB,IAAI,GAAG,EAAE,CAAC;IAE/D;;;;OAIG;IACH,YAA+B,MAAe;QAC7C,KAAK,EAAE,CAAC;sBADsB,MAAM;QAEpC,IAAI,CAAC,oBAAoB,EAAE,CAAC;IAC7B,CAAC;IAED,iEAAiE;IACjE,IAAI,SAAS;QACZ,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC;IAC9B,CAAC;IAED,uEAAuE;IACvE,IAAI,cAAc;QACjB,OAAO,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC;IACnC,CAAC;IAED;;;;OAIG;IACH,IAAI,cAAc,CAAC,cAAsB;QACxC,IAAI,CAAC,MAAM,CAAC,cAAc,GAAG,cAAc,CAAC;IAC7C,CAAC;IAED;;;;OAIG;IACH,cAAc;QACb,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE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,CAAC,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;gBACjD,OAAO,EAAE,CAAC;YACX,CAAC,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC;IACJ,CAAC;IAES,oBAAoB;QAC7B,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE;YAC5B,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;QAC5B,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE;YACjC,IAAI,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;gBACxC,IAAI,CAAC,kBAAkB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBACtC,OAAO;YACR,CAAC;YACD,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;QACjC,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,WAAW,EAAE,GAAG,EAAE;YAChC,IAAI,CAAC,QAAQ,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;QAChC,CAAC,CAAC,CAAC;IACJ,CAAC;CACD"}
|
|
@@ -44,6 +44,23 @@ export type StreamSocketEvents = {
|
|
|
44
44
|
*/
|
|
45
45
|
timeout: [];
|
|
46
46
|
};
|
|
47
|
+
/** Options accepted by {@link StreamSocket.from}. */
|
|
48
|
+
export type StreamSocketOptions = net.SocketConstructorOpts;
|
|
49
|
+
/** Options accepted by {@link StreamSocket.end}. */
|
|
50
|
+
export type StreamSocketEndOptions = {
|
|
51
|
+
/**
|
|
52
|
+
* Waits for the remote side to close as well, instead of resolving as soon as
|
|
53
|
+
* the local FIN is queued.
|
|
54
|
+
*/
|
|
55
|
+
waitForClose?: boolean;
|
|
56
|
+
};
|
|
57
|
+
/** Options accepted by {@link StreamSocket.awaitReady}. */
|
|
58
|
+
export type StreamSocketAwaitReadyOptions = {
|
|
59
|
+
/** Aborts the attempt, destroying the socket. */
|
|
60
|
+
signal?: AbortSignal;
|
|
61
|
+
/** Starts the attempt, once the settlement listeners are in place. */
|
|
62
|
+
start?: () => void;
|
|
63
|
+
};
|
|
47
64
|
/**
|
|
48
65
|
* Base class for stream-oriented client sockets.
|
|
49
66
|
*
|
|
@@ -64,8 +81,8 @@ export type StreamSocketEvents = {
|
|
|
64
81
|
* Unless explicitly documented otherwise, methods mirror the semantics of their
|
|
65
82
|
* underlying {@link net.Socket} counterparts.
|
|
66
83
|
*/
|
|
67
|
-
export declare class StreamSocket<
|
|
68
|
-
protected readonly
|
|
84
|
+
export declare class StreamSocket<TSocket extends net.Socket = net.Socket, TEvents extends StreamSocketEvents = StreamSocketEvents> extends EventDispatcherMapBase<TEvents> implements IEventDispatcherMap<TEvents> {
|
|
85
|
+
protected readonly socket: TSocket;
|
|
69
86
|
protected readonly handledErrorEvents: Set<IError>;
|
|
70
87
|
/**
|
|
71
88
|
* Creates a new {@link StreamSocket} instance.
|
|
@@ -73,13 +90,13 @@ export declare class StreamSocket<TSock extends net.Socket = net.Socket, TEvents
|
|
|
73
90
|
* @param options Options for creating the underlying Node.js socket.
|
|
74
91
|
* @returns A new `StreamSocket` instance.
|
|
75
92
|
*/
|
|
76
|
-
static from(options?:
|
|
93
|
+
static from(options?: StreamSocketOptions): StreamSocket;
|
|
77
94
|
/**
|
|
78
95
|
* Creates a new {@link StreamSocket} instance.
|
|
79
96
|
*
|
|
80
|
-
* @param
|
|
97
|
+
* @param socket The underlying Node.js socket.
|
|
81
98
|
*/
|
|
82
|
-
constructor(
|
|
99
|
+
constructor(socket: TSocket);
|
|
83
100
|
/**
|
|
84
101
|
* Underlying duplex stream for reading and writing data.
|
|
85
102
|
*
|
|
@@ -128,9 +145,7 @@ export declare class StreamSocket<TSock extends net.Socket = net.Socket, TEvents
|
|
|
128
145
|
* @param options Optional settings for ending the socket.
|
|
129
146
|
* @returns A promise that resolves once the socket has ended.
|
|
130
147
|
*/
|
|
131
|
-
end(options?:
|
|
132
|
-
waitForClose?: boolean;
|
|
133
|
-
}): Promise<void>;
|
|
148
|
+
end(options?: StreamSocketEndOptions): Promise<void>;
|
|
134
149
|
/**
|
|
135
150
|
* Writes data to the socket and resolves once that chunk has been flushed.
|
|
136
151
|
*
|
|
@@ -151,6 +166,20 @@ export declare class StreamSocket<TSock extends net.Socket = net.Socket, TEvents
|
|
|
151
166
|
* still be invoked.
|
|
152
167
|
*/
|
|
153
168
|
destroy(): void;
|
|
169
|
+
/**
|
|
170
|
+
* Runs an operation that establishes the connection and resolves once the
|
|
171
|
+
* socket emits `readyEvent`.
|
|
172
|
+
*
|
|
173
|
+
* The promise always settles: an "error" event rejects it and is consumed so
|
|
174
|
+
* it is not also dispatched to subscribers, a "close" event without a
|
|
175
|
+
* preceding error rejects with {@link ConnectionClosedError}, and aborting the
|
|
176
|
+
* signal destroys the socket and rejects with the abort reason.
|
|
177
|
+
*
|
|
178
|
+
* @param readyEvent Underlying socket event signalling a usable connection.
|
|
179
|
+
* @param options Abort signal and the operation that starts the connection.
|
|
180
|
+
* @returns A promise that resolves once the connection is established.
|
|
181
|
+
*/
|
|
182
|
+
protected awaitReady(readyEvent: string, options?: StreamSocketAwaitReadyOptions): Promise<void>;
|
|
154
183
|
protected setupEventForwarding(): void;
|
|
155
184
|
}
|
|
156
185
|
//# sourceMappingURL=stream-socket.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-socket.d.ts","sourceRoot":"","sources":["../../src/net/stream-socket.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAChC,OAAO,KAAK,KAAK,MAAM,MAAM,aAAa,CAAC;AAE3C,OAAO,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAC5E,OAAO,EAAE,KAAK,MAAM,EAAE,MA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I,IAAI,CAwBrC;CACD"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as net from "node:net";
|
|
2
2
|
import { EventDispatcherMapBase } from "@ac-kit/async";
|
|
3
|
+
import { ConnectionClosedError } from "./connection-closed-error.js";
|
|
3
4
|
/**
|
|
4
5
|
* Base class for stream-oriented client sockets.
|
|
5
6
|
*
|
|
@@ -21,7 +22,7 @@ import { EventDispatcherMapBase } from "@ac-kit/async";
|
|
|
21
22
|
* underlying {@link net.Socket} counterparts.
|
|
22
23
|
*/
|
|
23
24
|
export class StreamSocket extends EventDispatcherMapBase {
|
|
24
|
-
|
|
25
|
+
socket;
|
|
25
26
|
handledErrorEvents = new Set();
|
|
26
27
|
/**
|
|
27
28
|
* Creates a new {@link StreamSocket} instance.
|
|
@@ -35,11 +36,11 @@ export class StreamSocket extends EventDispatcherMapBase {
|
|
|
35
36
|
/**
|
|
36
37
|
* Creates a new {@link StreamSocket} instance.
|
|
37
38
|
*
|
|
38
|
-
* @param
|
|
39
|
+
* @param socket The underlying Node.js socket.
|
|
39
40
|
*/
|
|
40
|
-
constructor(
|
|
41
|
+
constructor(socket) {
|
|
41
42
|
super();
|
|
42
|
-
this.
|
|
43
|
+
this.socket = socket;
|
|
43
44
|
this.setupEventForwarding();
|
|
44
45
|
}
|
|
45
46
|
/**
|
|
@@ -49,31 +50,31 @@ export class StreamSocket extends EventDispatcherMapBase {
|
|
|
49
50
|
* to APIs that expect a Node.js stream.
|
|
50
51
|
*/
|
|
51
52
|
get stream() {
|
|
52
|
-
return this.
|
|
53
|
+
return this.socket;
|
|
53
54
|
}
|
|
54
55
|
/** Indicates whether the socket has been fully closed. */
|
|
55
56
|
get closed() {
|
|
56
|
-
return this.
|
|
57
|
+
return this.socket.closed;
|
|
57
58
|
}
|
|
58
59
|
/** Indicates whether the underlying socket has been destroyed. */
|
|
59
60
|
get destroyed() {
|
|
60
|
-
return this.
|
|
61
|
+
return this.socket.destroyed;
|
|
61
62
|
}
|
|
62
63
|
/** Total number of bytes read from the socket so far. */
|
|
63
64
|
get bytesRead() {
|
|
64
|
-
return this.
|
|
65
|
+
return this.socket.bytesRead;
|
|
65
66
|
}
|
|
66
67
|
/** Total number of bytes written to the socket so far. */
|
|
67
68
|
get bytesWritten() {
|
|
68
|
-
return this.
|
|
69
|
+
return this.socket.bytesWritten;
|
|
69
70
|
}
|
|
70
71
|
/** Whether the socket is currently in the process of connecting. */
|
|
71
72
|
get connecting() {
|
|
72
|
-
return this.
|
|
73
|
+
return this.socket.connecting;
|
|
73
74
|
}
|
|
74
75
|
/** Current inactivity timeout in milliseconds, or `null` if disabled. */
|
|
75
76
|
get timeout() {
|
|
76
|
-
return this.
|
|
77
|
+
return this.socket.timeout ?? null;
|
|
77
78
|
}
|
|
78
79
|
/**
|
|
79
80
|
* Updates the inactivity timeout for the socket.
|
|
@@ -83,21 +84,21 @@ export class StreamSocket extends EventDispatcherMapBase {
|
|
|
83
84
|
* `null` disables the timeout entirely.
|
|
84
85
|
*/
|
|
85
86
|
set timeout(timeout) {
|
|
86
|
-
this.
|
|
87
|
+
this.socket.setTimeout(timeout ?? 0);
|
|
87
88
|
}
|
|
88
89
|
/**
|
|
89
90
|
* Marks the socket as referenced, preventing the Node.js process from exiting
|
|
90
91
|
* while the socket is active.
|
|
91
92
|
*/
|
|
92
93
|
ref() {
|
|
93
|
-
this.
|
|
94
|
+
this.socket.ref();
|
|
94
95
|
}
|
|
95
96
|
/**
|
|
96
97
|
* Marks the socket as unreferenced, allowing the Node.js process to exit even
|
|
97
98
|
* if the socket is still active.
|
|
98
99
|
*/
|
|
99
100
|
unref() {
|
|
100
|
-
this.
|
|
101
|
+
this.socket.unref();
|
|
101
102
|
}
|
|
102
103
|
/**
|
|
103
104
|
* Half-closes the socket, optionally waiting for a full close.
|
|
@@ -117,17 +118,17 @@ export class StreamSocket extends EventDispatcherMapBase {
|
|
|
117
118
|
reject(error);
|
|
118
119
|
};
|
|
119
120
|
const handleEnd = () => {
|
|
120
|
-
this.
|
|
121
|
+
this.socket.removeListener("error", handleError);
|
|
121
122
|
resolve();
|
|
122
123
|
};
|
|
123
|
-
this.
|
|
124
|
-
if (options?.waitForClose && !this.
|
|
125
|
-
this.
|
|
126
|
-
this.
|
|
124
|
+
this.socket.prependOnceListener("error", handleError);
|
|
125
|
+
if (options?.waitForClose && !this.socket.closed) {
|
|
126
|
+
this.socket.prependOnceListener("close", () => {
|
|
127
|
+
this.socket.removeListener("error", handleError);
|
|
127
128
|
resolve();
|
|
128
129
|
});
|
|
129
130
|
}
|
|
130
|
-
this.
|
|
131
|
+
this.socket.end(options?.waitForClose && !this.socket.closed ? undefined : handleEnd);
|
|
131
132
|
});
|
|
132
133
|
}
|
|
133
134
|
/**
|
|
@@ -150,7 +151,7 @@ export class StreamSocket extends EventDispatcherMapBase {
|
|
|
150
151
|
}
|
|
151
152
|
resolve();
|
|
152
153
|
};
|
|
153
|
-
this.
|
|
154
|
+
this.socket.write(data, callback);
|
|
154
155
|
});
|
|
155
156
|
}
|
|
156
157
|
/**
|
|
@@ -161,26 +162,78 @@ export class StreamSocket extends EventDispatcherMapBase {
|
|
|
161
162
|
* still be invoked.
|
|
162
163
|
*/
|
|
163
164
|
destroy() {
|
|
164
|
-
this.
|
|
165
|
+
this.socket.destroy();
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Runs an operation that establishes the connection and resolves once the
|
|
169
|
+
* socket emits `readyEvent`.
|
|
170
|
+
*
|
|
171
|
+
* The promise always settles: an "error" event rejects it and is consumed so
|
|
172
|
+
* it is not also dispatched to subscribers, a "close" event without a
|
|
173
|
+
* preceding error rejects with {@link ConnectionClosedError}, and aborting the
|
|
174
|
+
* signal destroys the socket and rejects with the abort reason.
|
|
175
|
+
*
|
|
176
|
+
* @param readyEvent Underlying socket event signalling a usable connection.
|
|
177
|
+
* @param options Abort signal and the operation that starts the connection.
|
|
178
|
+
* @returns A promise that resolves once the connection is established.
|
|
179
|
+
*/
|
|
180
|
+
awaitReady(readyEvent, options) {
|
|
181
|
+
const { signal, start } = options ?? {};
|
|
182
|
+
return new Promise((resolve, reject) => {
|
|
183
|
+
const abortListeners = new AbortController();
|
|
184
|
+
const cleanup = () => {
|
|
185
|
+
abortListeners.abort();
|
|
186
|
+
this.socket.removeListener(readyEvent, handleReady);
|
|
187
|
+
this.socket.removeListener("error", handleError);
|
|
188
|
+
this.socket.removeListener("close", handleClose);
|
|
189
|
+
};
|
|
190
|
+
const handleReady = () => {
|
|
191
|
+
cleanup();
|
|
192
|
+
resolve();
|
|
193
|
+
};
|
|
194
|
+
const handleError = (error) => {
|
|
195
|
+
this.handledErrorEvents.add(error);
|
|
196
|
+
cleanup();
|
|
197
|
+
reject(error);
|
|
198
|
+
};
|
|
199
|
+
// A peer that drops the transport mid-connection closes it without an
|
|
200
|
+
// "error"; without this the promise would stay pending forever.
|
|
201
|
+
const handleClose = () => {
|
|
202
|
+
cleanup();
|
|
203
|
+
reject(new ConnectionClosedError(`Socket closed before the "${readyEvent}" event`));
|
|
204
|
+
};
|
|
205
|
+
if (signal) {
|
|
206
|
+
signal.throwIfAborted();
|
|
207
|
+
signal.addEventListener("abort", () => {
|
|
208
|
+
cleanup();
|
|
209
|
+
this.socket.destroy();
|
|
210
|
+
reject(signal.reason);
|
|
211
|
+
}, { once: true, signal: abortListeners.signal });
|
|
212
|
+
}
|
|
213
|
+
this.socket.once(readyEvent, handleReady);
|
|
214
|
+
this.socket.prependOnceListener("error", handleError);
|
|
215
|
+
this.socket.once("close", handleClose);
|
|
216
|
+
start?.();
|
|
217
|
+
});
|
|
165
218
|
}
|
|
166
219
|
setupEventForwarding() {
|
|
167
|
-
this.
|
|
220
|
+
this.socket.on("error", (err) => {
|
|
168
221
|
if (this.handledErrorEvents.has(err)) {
|
|
169
222
|
this.handledErrorEvents.delete(err);
|
|
170
223
|
return;
|
|
171
224
|
}
|
|
172
225
|
this.dispatch("error", [err]);
|
|
173
226
|
});
|
|
174
|
-
this.
|
|
227
|
+
this.socket.on("close", (hadError) => {
|
|
175
228
|
this.dispatch("close", [hadError]);
|
|
176
229
|
});
|
|
177
|
-
this.
|
|
230
|
+
this.socket.on("connect", () => {
|
|
178
231
|
this.dispatch("connect", []);
|
|
179
232
|
});
|
|
180
|
-
this.
|
|
233
|
+
this.socket.on("ready", () => {
|
|
181
234
|
this.dispatch("ready", []);
|
|
182
235
|
});
|
|
183
|
-
this.
|
|
236
|
+
this.socket.on("timeout", () => {
|
|
184
237
|
this.dispatch("timeout", []);
|
|
185
238
|
});
|
|
186
239
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-socket.js","sourceRoot":"","sources":["../../src/net/stream-socket.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAGhC,OAAO,EAAE,sBAAsB,EAAuB,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"stream-socket.js","sourceRoot":"","sources":["../../src/net/stream-socket.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAGhC,OAAO,EAAE,sBAAsB,EAAuB,MAAM,eAAe,CAAC;AAG5E,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAsErE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,YAIZ,SAAQ,sBAA+B;IAoBR,MAAM;IAjBlB,kBAAkB,GAAgB,IAAI,GAAG,EAAE,CAAC;IAE/D;;;;;OAKG;IACH,MAAM,CAAC,IAAI,CAAC,OAA6B;QACxC,OAAO,IAAI,YAAY,CAAC,IAAI,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;IAClD,CAAC;IAED;;;;OAIG;IACH,YAA+B,MAAe;QAC7C,KAAK,EAAE,CAAC;sBADsB,MAAM;QAEpC,IAAI,CAAC,oBAAoB,EAAE,CAAC;IAC7B,CAAC;IAED;;;;;OAKG;IACH,IAAI,MAAM;QACT,OAAO,IAAI,CAAC,MAA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EAAE,GAAG,EAAE;YAC9B,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;QAC9B,CAAC,CAAC,CAAC;IACJ,CAAC;CACD"}
|
package/dist/net/tcp-server.d.ts
CHANGED
|
@@ -1,110 +1,68 @@
|
|
|
1
1
|
import * as net from "node:net";
|
|
2
|
-
import { EventDispatcherMapBase, IEventDispatcherMap } from "@ac-kit/async";
|
|
3
|
-
import { InetEndpoint, type IError } from "@ac-kit/core";
|
|
4
2
|
import type { Except } from "type-fest";
|
|
3
|
+
import type { InetEndpoint } from "./inet-endpoint.js";
|
|
4
|
+
import { StreamServer, type StreamServerEvents } from "./stream-server.js";
|
|
5
5
|
import { TcpSocket } from "./tcp-socket.js";
|
|
6
|
-
/** Event map for TcpServer
|
|
7
|
-
export type TcpServerEvents = {
|
|
8
|
-
/** Emitted when
|
|
9
|
-
|
|
6
|
+
/** Event map for {@link TcpServer}. */
|
|
7
|
+
export type TcpServerEvents = StreamServerEvents & {
|
|
8
|
+
/** Emitted when a new connection is accepted. */
|
|
9
|
+
connection: [socket: TcpSocket];
|
|
10
10
|
/**
|
|
11
|
-
* Emitted when a
|
|
12
|
-
*
|
|
13
|
-
|
|
14
|
-
connection: [client: TcpSocket];
|
|
15
|
-
/**
|
|
16
|
-
* Emitted when a new connection is dropped. Provides the local and remote
|
|
17
|
-
* endpoint information of the dropped connection.
|
|
11
|
+
* Emitted when a connection is dropped because
|
|
12
|
+
* {@link StreamServer.maxConnections} was reached. Each endpoint is `null`
|
|
13
|
+
* when Node.js did not report it.
|
|
18
14
|
*/
|
|
19
15
|
drop: [
|
|
20
16
|
localEndpoint: InetEndpoint | null,
|
|
21
17
|
remoteEndpoint: InetEndpoint | null
|
|
22
18
|
];
|
|
23
|
-
/** Emitted when an error occurs. */
|
|
24
|
-
error: [err: IError];
|
|
25
|
-
/** Emitted when the server has been bound after calling server.listen(). */
|
|
26
|
-
listening: [];
|
|
27
19
|
};
|
|
20
|
+
/** Options accepted by {@link TcpServer.from}. */
|
|
21
|
+
export type TcpServerOptions = net.ServerOpts;
|
|
22
|
+
/** Options accepted by {@link TcpServer.listen}. */
|
|
23
|
+
export type TcpServerListenOptions = Except<net.ListenOptions, "port" | "path" | "handle">;
|
|
28
24
|
/**
|
|
29
|
-
* TCP server
|
|
25
|
+
* TCP server accepting incoming connections.
|
|
30
26
|
*
|
|
31
27
|
* Example usage:
|
|
32
28
|
*
|
|
33
29
|
* ```ts
|
|
34
30
|
* const server = TcpServer.from();
|
|
35
|
-
* server.
|
|
36
|
-
*
|
|
37
|
-
* client.stream.on("data", (data) => {
|
|
31
|
+
* server.subscribe("connection", (socket) => {
|
|
32
|
+
* socket.stream.on("data", (data) => {
|
|
38
33
|
* console.log("Received:", data.toString());
|
|
39
34
|
* });
|
|
40
35
|
* });
|
|
41
|
-
* await server.listen(8080, "0.0.0.0");
|
|
36
|
+
* await server.listen(8080, { host: "0.0.0.0" });
|
|
42
37
|
* // ... later
|
|
43
38
|
* await server.close();
|
|
44
39
|
* ```
|
|
45
40
|
*/
|
|
46
|
-
export declare class TcpServer extends
|
|
47
|
-
private readonly srv;
|
|
41
|
+
export declare class TcpServer<TServer extends net.Server = net.Server, TEvents extends TcpServerEvents = TcpServerEvents> extends StreamServer<TServer, TEvents> {
|
|
48
42
|
/**
|
|
49
|
-
* Creates a new
|
|
43
|
+
* Creates a new {@link TcpServer} instance.
|
|
50
44
|
*
|
|
51
|
-
* @param options Options for creating the
|
|
45
|
+
* @param options Options for creating the underlying Node.js server.
|
|
52
46
|
* @returns A new `TcpServer` instance.
|
|
53
47
|
*/
|
|
54
|
-
static from(options?:
|
|
55
|
-
private readonly handledErrorEvents;
|
|
56
|
-
constructor(srv: net.Server);
|
|
57
|
-
/**
|
|
58
|
-
* Gets the current number of concurrent connections on the server.
|
|
59
|
-
*
|
|
60
|
-
* @returns A promise that resolves with the connection count.
|
|
61
|
-
*/
|
|
62
|
-
getConnections(): Promise<number>;
|
|
63
|
-
/** Gets whether the server is currently listening for connections. */
|
|
64
|
-
get listening(): boolean;
|
|
48
|
+
static from(options?: TcpServerOptions): TcpServer;
|
|
65
49
|
/**
|
|
66
|
-
*
|
|
50
|
+
* Binds the server and resolves once it is listening.
|
|
67
51
|
*
|
|
68
|
-
* @param
|
|
69
|
-
*/
|
|
70
|
-
set maxConnections(maxConnections: number);
|
|
71
|
-
/** Gets the maximum number of queued pending connections. */
|
|
72
|
-
get maxConnections(): number;
|
|
73
|
-
/**
|
|
74
|
-
* References the server, preventing the process from exiting while the server
|
|
75
|
-
* is active.
|
|
76
|
-
*/
|
|
77
|
-
ref(): void;
|
|
78
|
-
/**
|
|
79
|
-
* Unreferences the server, allowing the process from exiting even if the
|
|
80
|
-
* server is active.
|
|
81
|
-
*/
|
|
82
|
-
unref(): void;
|
|
83
|
-
/**
|
|
84
|
-
* Starts the TCP server listening on the specified port and host.
|
|
85
|
-
*
|
|
86
|
-
* @param port The port to listen on. If 0, a random available port will be
|
|
87
|
-
* assigned.
|
|
88
|
-
* @param host The host/interface to bind to. Defaults to '::' (IPv6) or
|
|
89
|
-
* '0.0.0.0' (IPv4).
|
|
52
|
+
* @param port The port to listen on. Use `0` to let the system assign one.
|
|
90
53
|
* @param options Additional listen options.
|
|
91
|
-
* @returns A promise that resolves
|
|
92
|
-
|
|
93
|
-
listen(port: number, host?: string, options?: Except<net.ListenOptions, "port" | "host">): Promise<void>;
|
|
94
|
-
/**
|
|
95
|
-
* Closes the TCP server, stopping it from accepting new connections. Existing
|
|
96
|
-
* connections are not automatically closed.
|
|
97
|
-
*
|
|
98
|
-
* @returns A promise that resolves when the server is fully closed.
|
|
54
|
+
* @returns A promise that resolves once the server is listening.
|
|
55
|
+
* @throws When the address is already in use or cannot be bound.
|
|
99
56
|
*/
|
|
100
|
-
|
|
57
|
+
listen(port: number, options?: TcpServerListenOptions): Promise<void>;
|
|
101
58
|
/**
|
|
102
|
-
*
|
|
59
|
+
* Returns the endpoint the server is bound to.
|
|
103
60
|
*
|
|
104
|
-
* @returns The
|
|
105
|
-
*
|
|
61
|
+
* @returns The bound endpoint, or `null` when the server is not listening.
|
|
62
|
+
* @throws {UnsupportedError} When the server is bound to a path rather than
|
|
63
|
+
* an IP endpoint. Use {@link IpcServer} for those.
|
|
106
64
|
*/
|
|
107
65
|
address(): InetEndpoint | null;
|
|
108
|
-
|
|
66
|
+
protected setupEventForwarding(): void;
|
|
109
67
|
}
|
|
110
68
|
//# sourceMappingURL=tcp-server.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tcp-server.d.ts","sourceRoot":"","sources":["../../src/net/tcp-server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"tcp-server.d.ts","sourceRoot":"","sources":["../../src/net/tcp-server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAGhC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAGxC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,KAAK,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAC3E,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,uCAAuC;AACvC,MAAM,MAAM,eAAe,GAAG,kBAAkB,GAAG;IAClD,iDAAiD;IACjD,UAAU,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IAEhC;;;;OAIG;IACH,IAAI,EAAE;QACL,aAAa,EAAE,YAAY,GAAG,IAAI;QAClC,cAAc,EAAE,YAAY,GAAG,IAAI;KACnC,CAAC;CACF,CAAC;AAEF,kDAAkD;AAClD,MAAM,MAAM,gBAAgB,GAAG,GAAG,CAAC,UAAU,CAAC;AAE9C,oDAAoD;AACpD,MAAM,MAAM,sBAAsB,GAAG,MAAM,CAC1C,GAAG,CAAC,aAAa,EACjB,MAA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