@ac-kit/node 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +28 -0
- package/README.md +39 -0
- package/dist/child-process/exec-async.d.ts +24 -0
- package/dist/child-process/exec-async.d.ts.map +1 -0
- package/dist/child-process/exec-async.js +33 -0
- package/dist/child-process/exec-async.js.map +1 -0
- package/dist/error/node-aggregate-error.d.ts +18 -0
- package/dist/error/node-aggregate-error.d.ts.map +1 -0
- package/dist/error/node-aggregate-error.js +13 -0
- package/dist/error/node-aggregate-error.js.map +1 -0
- package/dist/error/node-error.d.ts +26 -0
- package/dist/error/node-error.d.ts.map +1 -0
- package/dist/error/node-error.js +21 -0
- package/dist/error/node-error.js.map +1 -0
- package/dist/error/node-exec-error.d.ts +21 -0
- package/dist/error/node-exec-error.d.ts.map +1 -0
- package/dist/error/node-exec-error.js +17 -0
- package/dist/error/node-exec-error.js.map +1 -0
- package/dist/error/node-system-error.d.ts +46 -0
- package/dist/error/node-system-error.d.ts.map +1 -0
- package/dist/error/node-system-error.js +35 -0
- package/dist/error/node-system-error.js.map +1 -0
- package/dist/error/process-exit-error.d.ts +22 -0
- package/dist/error/process-exit-error.d.ts.map +1 -0
- package/dist/error/process-exit-error.js +46 -0
- package/dist/error/process-exit-error.js.map +1 -0
- package/dist/fs/compress-file.d.ts +42 -0
- package/dist/fs/compress-file.d.ts.map +1 -0
- package/dist/fs/compress-file.js +72 -0
- package/dist/fs/compress-file.js.map +1 -0
- package/dist/fs/create-temp-dir.d.ts +13 -0
- package/dist/fs/create-temp-dir.d.ts.map +1 -0
- package/dist/fs/create-temp-dir.js +18 -0
- package/dist/fs/create-temp-dir.js.map +1 -0
- package/dist/fs/escape-path.d.ts +19 -0
- package/dist/fs/escape-path.d.ts.map +1 -0
- package/dist/fs/escape-path.js +20 -0
- package/dist/fs/escape-path.js.map +1 -0
- package/dist/fs/exists-async.d.ts +9 -0
- package/dist/fs/exists-async.d.ts.map +1 -0
- package/dist/fs/exists-async.js +17 -0
- package/dist/fs/exists-async.js.map +1 -0
- package/dist/fs/file-content-equal.d.ts +18 -0
- package/dist/fs/file-content-equal.d.ts.map +1 -0
- package/dist/fs/file-content-equal.js +50 -0
- package/dist/fs/file-content-equal.js.map +1 -0
- package/dist/fs/is-directory-async.d.ts +11 -0
- package/dist/fs/is-directory-async.d.ts.map +1 -0
- package/dist/fs/is-directory-async.js +18 -0
- package/dist/fs/is-directory-async.js.map +1 -0
- package/dist/fs/types.d.ts +19 -0
- package/dist/fs/types.d.ts.map +1 -0
- package/dist/fs/types.js +23 -0
- package/dist/fs/types.js.map +1 -0
- package/dist/fs/walk-paths.d.ts +108 -0
- package/dist/fs/walk-paths.d.ts.map +1 -0
- package/dist/fs/walk-paths.js +197 -0
- package/dist/fs/walk-paths.js.map +1 -0
- package/dist/fs/write-file-atomic.d.ts +42 -0
- package/dist/fs/write-file-atomic.d.ts.map +1 -0
- package/dist/fs/write-file-atomic.js +126 -0
- package/dist/fs/write-file-atomic.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +34 -0
- package/dist/index.js.map +1 -0
- package/dist/module/import-module.d.ts +13 -0
- package/dist/module/import-module.d.ts.map +1 -0
- package/dist/module/import-module.js +18 -0
- package/dist/module/import-module.js.map +1 -0
- package/dist/module/optional-import.d.ts +19 -0
- package/dist/module/optional-import.d.ts.map +1 -0
- package/dist/module/optional-import.js +30 -0
- package/dist/module/optional-import.js.map +1 -0
- package/dist/module/resolve-module.d.ts +13 -0
- package/dist/module/resolve-module.d.ts.map +1 -0
- package/dist/module/resolve-module.js +26 -0
- package/dist/module/resolve-module.js.map +1 -0
- package/dist/net/dgram-socket.d.ts +235 -0
- package/dist/net/dgram-socket.d.ts.map +1 -0
- package/dist/net/dgram-socket.js +341 -0
- package/dist/net/dgram-socket.js.map +1 -0
- package/dist/net/ipc-socket.d.ts +34 -0
- package/dist/net/ipc-socket.d.ts.map +1 -0
- package/dist/net/ipc-socket.js +63 -0
- package/dist/net/ipc-socket.js.map +1 -0
- package/dist/net/stream-socket.d.ts +156 -0
- package/dist/net/stream-socket.d.ts.map +1 -0
- package/dist/net/stream-socket.js +188 -0
- package/dist/net/stream-socket.js.map +1 -0
- package/dist/net/tcp-server.d.ts +110 -0
- package/dist/net/tcp-server.d.ts.map +1 -0
- package/dist/net/tcp-server.js +198 -0
- package/dist/net/tcp-server.js.map +1 -0
- package/dist/net/tcp-socket.d.ts +87 -0
- package/dist/net/tcp-socket.d.ts.map +1 -0
- package/dist/net/tcp-socket.js +153 -0
- package/dist/net/tcp-socket.js.map +1 -0
- package/dist/net/tls-socket.d.ts +45 -0
- package/dist/net/tls-socket.d.ts.map +1 -0
- package/dist/net/tls-socket.js +148 -0
- package/dist/net/tls-socket.js.map +1 -0
- package/dist/process/fork-process.d.ts +63 -0
- package/dist/process/fork-process.d.ts.map +1 -0
- package/dist/process/fork-process.js +98 -0
- package/dist/process/fork-process.js.map +1 -0
- package/dist/process/guarded.d.ts +12 -0
- package/dist/process/guarded.d.ts.map +1 -0
- package/dist/process/guarded.js +25 -0
- package/dist/process/guarded.js.map +1 -0
- package/dist/process/spawn-process.d.ts +37 -0
- package/dist/process/spawn-process.d.ts.map +1 -0
- package/dist/process/spawn-process.js +73 -0
- package/dist/process/spawn-process.js.map +1 -0
- package/dist/process/spawned-process.d.ts +102 -0
- package/dist/process/spawned-process.d.ts.map +1 -0
- package/dist/process/spawned-process.js +226 -0
- package/dist/process/spawned-process.js.map +1 -0
- package/dist/process/start-process.d.ts +54 -0
- package/dist/process/start-process.d.ts.map +1 -0
- package/dist/process/start-process.js +73 -0
- package/dist/process/start-process.js.map +1 -0
- package/dist/shell/escape-command-arg.d.ts +9 -0
- package/dist/shell/escape-command-arg.d.ts.map +1 -0
- package/dist/shell/escape-command-arg.js +13 -0
- package/dist/shell/escape-command-arg.js.map +1 -0
- package/dist/shell/escape-command.d.ts +10 -0
- package/dist/shell/escape-command.d.ts.map +1 -0
- package/dist/shell/escape-command.js +12 -0
- package/dist/shell/escape-command.js.map +1 -0
- package/dist/stream/non-closing-writable-stream.d.ts +14 -0
- package/dist/stream/non-closing-writable-stream.d.ts.map +1 -0
- package/dist/stream/non-closing-writable-stream.js +20 -0
- package/dist/stream/non-closing-writable-stream.js.map +1 -0
- package/dist/stream/write-async.d.ts +12 -0
- package/dist/stream/write-async.d.ts.map +1 -0
- package/dist/stream/write-async.js +22 -0
- package/dist/stream/write-async.js.map +1 -0
- package/package.json +60 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import * as dgram from "node:dgram";
|
|
2
|
+
import { EventDispatcherMapBase } from "@ac-kit/async";
|
|
3
|
+
import { composeInetAddress } from "@ac-kit/core";
|
|
4
|
+
import { isNodeErrorWithCode } from "../error/node-error.js";
|
|
5
|
+
/**
|
|
6
|
+
* High-level wrapper around Node.js
|
|
7
|
+
* {@link dgram.Socket | UDP datagram sockets}.
|
|
8
|
+
*
|
|
9
|
+
* This class delegates almost all behavior to an underlying
|
|
10
|
+
* {@link dgram.Socket} instance while:
|
|
11
|
+
*
|
|
12
|
+
* - Providing strongly-typed events via {@link DgramSocketEvents}.
|
|
13
|
+
* - Using {@link InetEndpoint} instead of Node's `RemoteInfo`.
|
|
14
|
+
* - Exposing promise-based {@link DgramSocket.bind | bind} and
|
|
15
|
+
* {@link DgramSocket.close | close} helpers.
|
|
16
|
+
* - Aggregating send-queue statistics through {@link DgramSocket.getStat}.
|
|
17
|
+
*
|
|
18
|
+
* Unless explicitly documented otherwise, methods have the same semantics as
|
|
19
|
+
* their counterparts on {@link dgram.Socket}.
|
|
20
|
+
*
|
|
21
|
+
* Example usage:
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* const dgramSocket = DgramSocket.from({ type: "udp4" });
|
|
25
|
+
* await dgramSocket.bind(12345, "localhost");
|
|
26
|
+
* dgramSocket.on("message", (msg, msgSize, from) => {
|
|
27
|
+
* console.log(
|
|
28
|
+
* `Received ${msgSize} bytes from ${from.address}:${from.port}`,
|
|
29
|
+
* );
|
|
30
|
+
* });
|
|
31
|
+
* await dgramSocket.send(54321, "localhost", Buffer.from("Hello, UDP!"));
|
|
32
|
+
* await dgramSocket.close();
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export class DgramSocket extends EventDispatcherMapBase {
|
|
36
|
+
sock;
|
|
37
|
+
/** Creates a new `DgramSocket` instance with the specified options. */
|
|
38
|
+
static from(options) {
|
|
39
|
+
return new DgramSocket(dgram.createSocket(options || { type: "udp4" }));
|
|
40
|
+
}
|
|
41
|
+
handledErrorEvents = new Set();
|
|
42
|
+
/**
|
|
43
|
+
* Constructs a new `DgramSocket` instance wrapping the provided
|
|
44
|
+
* `dgram.Socket`.
|
|
45
|
+
*/
|
|
46
|
+
constructor(sock) {
|
|
47
|
+
super();
|
|
48
|
+
this.sock = sock;
|
|
49
|
+
this.setupEventForwarding();
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* References the socket, preventing the process from exiting while the socket
|
|
53
|
+
* is active.
|
|
54
|
+
*/
|
|
55
|
+
ref() {
|
|
56
|
+
this.sock.ref();
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Unreferences the socket, allowing the process to exit even if the socket is
|
|
60
|
+
* active.
|
|
61
|
+
*/
|
|
62
|
+
unref() {
|
|
63
|
+
this.sock.unref();
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Enables or disables sending of broadcast datagrams.
|
|
67
|
+
*
|
|
68
|
+
* This is a thin wrapper around {@link dgram.Socket.setBroadcast}, which
|
|
69
|
+
* toggles the `SO_BROADCAST` socket option. When enabled, UDP packets may be
|
|
70
|
+
* sent to broadcast addresses.
|
|
71
|
+
*
|
|
72
|
+
* @param flag Whether broadcast should be enabled.
|
|
73
|
+
*/
|
|
74
|
+
setBroadcast(flag) {
|
|
75
|
+
this.sock.setBroadcast(flag);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Adds this socket to the given multicast group.
|
|
79
|
+
*
|
|
80
|
+
* This is a thin wrapper around {@link dgram.Socket.addMembership}, which
|
|
81
|
+
* configures `IP_ADD_MEMBERSHIP` for the given multicast group.
|
|
82
|
+
*/
|
|
83
|
+
addMembership(multicastAddress, multicastInterface) {
|
|
84
|
+
this.sock.addMembership(multicastAddress, multicastInterface);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Sets the size in bytes of the underlying receive buffer.
|
|
88
|
+
*
|
|
89
|
+
* This is a thin wrapper around {@link dgram.Socket.setRecvBufferSize}, which
|
|
90
|
+
* sets the `SO_RCVBUF` socket option.
|
|
91
|
+
*/
|
|
92
|
+
setRecvBufferSize(size) {
|
|
93
|
+
this.sock.setRecvBufferSize(size);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Returns the size in bytes of the underlying receive buffer.
|
|
97
|
+
*
|
|
98
|
+
* This is a thin wrapper around {@link dgram.Socket.getRecvBufferSize}, which
|
|
99
|
+
* reads the `SO_RCVBUF` socket option.
|
|
100
|
+
*/
|
|
101
|
+
getRecvBufferSize() {
|
|
102
|
+
return this.sock.getRecvBufferSize();
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Sets the size in bytes of the underlying send buffer.
|
|
106
|
+
*
|
|
107
|
+
* This is a thin wrapper around {@link dgram.Socket.setSendBufferSize}, which
|
|
108
|
+
* sets the `SO_SNDBUF` socket option.
|
|
109
|
+
*/
|
|
110
|
+
setSendBufferSize(size) {
|
|
111
|
+
this.sock.setSendBufferSize(size);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Returns the size in bytes of the underlying send buffer.
|
|
115
|
+
*
|
|
116
|
+
* This is a thin wrapper around {@link dgram.Socket.getSendBufferSize}, which
|
|
117
|
+
* reads the `SO_SNDBUF` socket option.
|
|
118
|
+
*/
|
|
119
|
+
getSendBufferSize() {
|
|
120
|
+
return this.sock.getSendBufferSize();
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Removes this socket from the given multicast group.
|
|
124
|
+
*
|
|
125
|
+
* This is a thin wrapper around {@link dgram.Socket.dropMembership}, which
|
|
126
|
+
* configures `IP_DROP_MEMBERSHIP` for the given multicast group.
|
|
127
|
+
*/
|
|
128
|
+
dropMembership(multicastAddress, multicastInterface) {
|
|
129
|
+
this.sock.dropMembership(multicastAddress, multicastInterface);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Adds this socket to a source-specific multicast group.
|
|
133
|
+
*
|
|
134
|
+
* This is a thin wrapper around
|
|
135
|
+
* {@link dgram.Socket.addSourceSpecificMembership}, configuring
|
|
136
|
+
* source-specific multicast membership for the given source and group.
|
|
137
|
+
*/
|
|
138
|
+
addSourceMembership(sourceAddress, groupAddress, multicastInterface) {
|
|
139
|
+
this.sock.addSourceSpecificMembership(sourceAddress, groupAddress, multicastInterface);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Removes this socket from a source-specific multicast group.
|
|
143
|
+
*
|
|
144
|
+
* This is a thin wrapper around
|
|
145
|
+
* {@link dgram.Socket.dropSourceSpecificMembership}, removing source-specific
|
|
146
|
+
* multicast membership for the given source and group.
|
|
147
|
+
*/
|
|
148
|
+
dropSourceMembership(sourceAddress, groupAddress, multicastInterface) {
|
|
149
|
+
this.sock.dropSourceSpecificMembership(sourceAddress, groupAddress, multicastInterface);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Sets the outbound multicast interface.
|
|
153
|
+
*
|
|
154
|
+
* This is a thin wrapper around {@link dgram.Socket.setMulticastInterface},
|
|
155
|
+
* which controls the default outgoing interface for multicast traffic.
|
|
156
|
+
*/
|
|
157
|
+
setMulticastInterface(multicastInterface) {
|
|
158
|
+
this.sock.setMulticastInterface(multicastInterface);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Enables or disables loopback for multicast packets sent from this socket.
|
|
162
|
+
*
|
|
163
|
+
* This is a thin wrapper around {@link dgram.Socket.setMulticastLoopback},
|
|
164
|
+
* which toggles the `IP_MULTICAST_LOOP` socket option.
|
|
165
|
+
*/
|
|
166
|
+
setMulticastLoop(flag) {
|
|
167
|
+
this.sock.setMulticastLoopback(flag);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Sets the time-to-live (TTL) value for multicast packets.
|
|
171
|
+
*
|
|
172
|
+
* This is a thin wrapper around {@link dgram.Socket.setMulticastTTL}, which
|
|
173
|
+
* configures the `IP_MULTICAST_TTL` socket option.
|
|
174
|
+
*/
|
|
175
|
+
setMulticastTtl(ttl) {
|
|
176
|
+
this.sock.setMulticastTTL(ttl);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Sets the unicast time-to-live (TTL) value for outgoing packets.
|
|
180
|
+
*
|
|
181
|
+
* This is a thin wrapper around {@link dgram.Socket.setTTL}, which configures
|
|
182
|
+
* the `IP_TTL` socket option.
|
|
183
|
+
*/
|
|
184
|
+
setTtl(ttl) {
|
|
185
|
+
this.sock.setTTL(ttl);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Gets the address information of the bound socket.
|
|
189
|
+
*
|
|
190
|
+
* @returns The socket's endpoint information or null if the socket is not
|
|
191
|
+
* bound.
|
|
192
|
+
* @throws {UnsupportedError} If the address type is not an AddressInfo
|
|
193
|
+
* object.
|
|
194
|
+
*/
|
|
195
|
+
address() {
|
|
196
|
+
let addr;
|
|
197
|
+
try {
|
|
198
|
+
addr = this.sock.address();
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
// dgram throws "Not running" error when socket is not bound/closed
|
|
202
|
+
if (isNodeErrorWithCode(error, "EBADF") ||
|
|
203
|
+
(error instanceof Error && error.message === "Not running")) {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
throw error;
|
|
207
|
+
}
|
|
208
|
+
return {
|
|
209
|
+
...composeInetAddress(addr.family, addr.address),
|
|
210
|
+
port: addr.port,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Binds the UDP socket to the specified address and port.
|
|
215
|
+
*
|
|
216
|
+
* @param port The port to bind to. If not specified, a random available port
|
|
217
|
+
* will be used.
|
|
218
|
+
* @param address The address to bind to. If not specified, the socket will
|
|
219
|
+
* bind to all available interfaces.
|
|
220
|
+
* @param options Binding options.
|
|
221
|
+
* @returns A promise that resolves when the socket is successfully bound.
|
|
222
|
+
*/
|
|
223
|
+
bind(port = 0, address, options) {
|
|
224
|
+
return new Promise((resolve, reject) => {
|
|
225
|
+
const handleError = (error) => {
|
|
226
|
+
this.handledErrorEvents.add(error);
|
|
227
|
+
reject(error);
|
|
228
|
+
};
|
|
229
|
+
this.sock.prependOnceListener("error", handleError);
|
|
230
|
+
try {
|
|
231
|
+
this.sock.bind({
|
|
232
|
+
...options,
|
|
233
|
+
address,
|
|
234
|
+
port,
|
|
235
|
+
}, () => {
|
|
236
|
+
this.sock.removeListener("error", handleError);
|
|
237
|
+
resolve();
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
catch (error) {
|
|
241
|
+
this.sock.removeListener("error", handleError);
|
|
242
|
+
reject(error);
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Closes the UDP socket.
|
|
248
|
+
*
|
|
249
|
+
* @returns A promise that resolves when the socket is successfully closed.
|
|
250
|
+
*/
|
|
251
|
+
close() {
|
|
252
|
+
return new Promise((resolve, reject) => {
|
|
253
|
+
const handleError = (error) => {
|
|
254
|
+
this.handledErrorEvents.add(error);
|
|
255
|
+
reject(error);
|
|
256
|
+
};
|
|
257
|
+
this.sock.prependOnceListener("error", handleError);
|
|
258
|
+
this.sock.close(() => {
|
|
259
|
+
this.sock.removeListener("error", handleError);
|
|
260
|
+
resolve();
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Returns statistics about the underlying send queue.
|
|
266
|
+
*
|
|
267
|
+
* This wraps {@link dgram.Socket.getSendQueueCount} and
|
|
268
|
+
* {@link dgram.Socket.getSendQueueSize} into a single object.
|
|
269
|
+
*/
|
|
270
|
+
getStat() {
|
|
271
|
+
return {
|
|
272
|
+
sendQueueCount: this.sock.getSendQueueCount(),
|
|
273
|
+
sendQueueSize: this.sock.getSendQueueSize(),
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Sends a message to the specified address and port.
|
|
278
|
+
*
|
|
279
|
+
* The msg argument contains the message to be sent. Depending on its type,
|
|
280
|
+
* different behavior can apply. If msg is a Buffer, any TypedArray or a
|
|
281
|
+
* DataView, the offset and length specify the offset within the Buffer where
|
|
282
|
+
* the message begins and the number of bytes in the message, respectively.
|
|
283
|
+
*
|
|
284
|
+
* If msg is a String, then it is automatically converted to a Buffer with
|
|
285
|
+
* 'utf8' encoding.
|
|
286
|
+
*
|
|
287
|
+
* With messages that contain multi-byte characters, offset and length will be
|
|
288
|
+
* calculated with respect to byte length and not the character position. If
|
|
289
|
+
* msg is an array, offset and length must not be specified.
|
|
290
|
+
*
|
|
291
|
+
* @param port The destination port.
|
|
292
|
+
* @param address The destination address.
|
|
293
|
+
* @param msg The message to send. This can be a `Buffer`, `TypedArray`, or
|
|
294
|
+
* `DataView`.
|
|
295
|
+
* @param offset Optional offset in the message buffer to start sending from.
|
|
296
|
+
* @param length Optional number of bytes to send from the message buffer.
|
|
297
|
+
* @returns A promise that resolves with the number of bytes sent.
|
|
298
|
+
*/
|
|
299
|
+
send(port, address, msg, offset, length) {
|
|
300
|
+
return new Promise((resolve, reject) => {
|
|
301
|
+
const callback = (error, bytes) => {
|
|
302
|
+
if (error) {
|
|
303
|
+
reject(error);
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
resolve(bytes);
|
|
307
|
+
};
|
|
308
|
+
if (offset === undefined || length === undefined) {
|
|
309
|
+
this.sock.send(msg, port, address, callback);
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
this.sock.send(msg, offset, length, port, address, callback);
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
setupEventForwarding() {
|
|
316
|
+
this.sock.on("close", () => {
|
|
317
|
+
this.dispatch("close", []);
|
|
318
|
+
});
|
|
319
|
+
this.sock.on("connect", () => {
|
|
320
|
+
this.dispatch("connect", []);
|
|
321
|
+
});
|
|
322
|
+
this.sock.on("error", (err) => {
|
|
323
|
+
if (this.handledErrorEvents.has(err)) {
|
|
324
|
+
this.handledErrorEvents.delete(err);
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
this.dispatch("error", [err]);
|
|
328
|
+
});
|
|
329
|
+
this.sock.on("listening", () => {
|
|
330
|
+
this.dispatch("listening", []);
|
|
331
|
+
});
|
|
332
|
+
this.sock.on("message", (msg, rinfo) => {
|
|
333
|
+
const from = {
|
|
334
|
+
...composeInetAddress(rinfo.family, rinfo.address),
|
|
335
|
+
port: rinfo.port,
|
|
336
|
+
};
|
|
337
|
+
this.dispatch("message", [msg, rinfo.size, from]);
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
//# sourceMappingURL=dgram-socket.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dgram-socket.js","sourceRoot":"","sources":["../../src/net/dgram-socket.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,YAAY,CAAC;AAGpC,OAAO,EAAE,sBAAsB,EAAuB,MAAM,eAAe,CAAC;AAC5E,OAAO,EAAE,kBAAkB,EAA6B,MAAM,cAAc,CAAC;AAG7E,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAkC7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,OAAO,WACZ,SAAQ,sBAAyC;IAcpB,IAAI;IAXjC,uEAAuE;IACvE,MAAM,CAAC,IAAI,CAAC,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@
|
|
|
1
|
+
import * as net from "node:net";
|
|
2
|
+
import { InetEndpoint } from "@ac-kit/core";
|
|
3
|
+
import type { Except } from "type-fest";
|
|
4
|
+
import { StreamSocket, type StreamSocketEvents } from "./stream-socket.js";
|
|
5
|
+
export type IpcSocketEvents = StreamSocketEvents & {
|
|
6
|
+
/**
|
|
7
|
+
* Emitted when a new connection attempt is started. May be emitted multiple
|
|
8
|
+
* times if family autoselection is enabled.
|
|
9
|
+
*/
|
|
10
|
+
connectionAttempt: [endpoint: InetEndpoint];
|
|
11
|
+
/**
|
|
12
|
+
* Emitted when a connection attempt failed. May be emitted multiple times if
|
|
13
|
+
* family autoselection is enabled.
|
|
14
|
+
*/
|
|
15
|
+
connectionAttemptFailed: [endpoint: InetEndpoint, error: Error];
|
|
16
|
+
/**
|
|
17
|
+
* Emitted when a connection attempt timed out. May be emitted multiple times
|
|
18
|
+
* if family autoselection is enabled.
|
|
19
|
+
*/
|
|
20
|
+
connectionAttemptTimeout: [endpoint: InetEndpoint];
|
|
21
|
+
};
|
|
22
|
+
export declare class IpcSocket<TSock extends net.Socket = net.Socket, TEvents extends IpcSocketEvents = IpcSocketEvents> extends StreamSocket<TSock, TEvents> {
|
|
23
|
+
/**
|
|
24
|
+
* Creates a new {@link IpcSocket} instance.
|
|
25
|
+
*
|
|
26
|
+
* @param options Options for creating the underlying Node.js socket.
|
|
27
|
+
* @returns A new `StreamSocket` instance.
|
|
28
|
+
*/
|
|
29
|
+
static from(options?: net.SocketConstructorOpts): IpcSocket;
|
|
30
|
+
constructor(sock: TSock);
|
|
31
|
+
connect(path: string, options?: Except<net.IpcSocketConnectOpts, "path">): Promise<void>;
|
|
32
|
+
protected setupEventForwarding(): void;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=ipc-socket.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ipc-socket.d.ts","sourceRoot":"","sources":["../../src/net/ipc-socket.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAEhC,OAAO,EAAsB,YAAY,EAAe,MAAM,cAAc,CAAC;AAC7E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAExC,OAAO,EAAE,YAAY,EAAE,KAAK,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAE3E,MAAM,MAAM,eAAe,GAAG,kBAAkB,GAAG;IAClD;;;OAGG;IACH,iBAAiB,EAAE,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;IAE5C;;;OAGG;IACH,uBAAuB,EAAE,CAAC,QAAQ,EAAE,YAAY,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;IAEhE;;;OAGG;IACH,wBAAwB,EAAE,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;CACnD,CAAC;AAEF,qBAAa,SAAS,CACrB,KAAK,SAAS,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,MAAM,EACrC,OAAO,SAAS,eAAe,GAAG,eAAe,CAChD,SAAQ,YAAY,CAAC,KAAK,EAAE,OAAO,CAAC;IACrC;;;;;OAKG;IACH,OAAgB,IAAI,CAAC,OAAO,CAAC,EAAE,GAAG,CAAC,qBAAqB,GAAG,SAAS,CAEnE;IAED,YAAY,IAAI,EAAE,KAAK,EAGtB;IAED,OAAO,CACN,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,MAAM,CAAC,GAAG,CAAC,oBAAoB,EAAE,MAAM,CAAC,GAChD,OAAO,CAAC,IAAI,CAAC,CAmBf;IAED,UAAmB,oBAAoB,IAAI,IAAI,CA8B9C;CACD"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import * as net from "node:net";
|
|
2
|
+
import { composeInetAddress } from "@ac-kit/core";
|
|
3
|
+
import { StreamSocket } from "./stream-socket.js";
|
|
4
|
+
export class IpcSocket extends StreamSocket {
|
|
5
|
+
/**
|
|
6
|
+
* Creates a new {@link IpcSocket} instance.
|
|
7
|
+
*
|
|
8
|
+
* @param options Options for creating the underlying Node.js socket.
|
|
9
|
+
* @returns A new `StreamSocket` instance.
|
|
10
|
+
*/
|
|
11
|
+
static from(options) {
|
|
12
|
+
return new IpcSocket(new net.Socket(options));
|
|
13
|
+
}
|
|
14
|
+
constructor(sock) {
|
|
15
|
+
super(sock);
|
|
16
|
+
this.setupEventForwarding();
|
|
17
|
+
}
|
|
18
|
+
connect(path, options) {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
const handleError = (error) => {
|
|
21
|
+
this.handledErrorEvents.add(error);
|
|
22
|
+
reject(error);
|
|
23
|
+
};
|
|
24
|
+
this.sock.prependOnceListener("error", handleError);
|
|
25
|
+
this.sock.connect({
|
|
26
|
+
...options,
|
|
27
|
+
path,
|
|
28
|
+
}, () => {
|
|
29
|
+
this.sock.removeListener("error", handleError);
|
|
30
|
+
resolve();
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
setupEventForwarding() {
|
|
35
|
+
super.setupEventForwarding();
|
|
36
|
+
this.sock.on("connectionAttempt", (ip, port, family) => {
|
|
37
|
+
this.dispatch("connectionAttempt", [
|
|
38
|
+
{
|
|
39
|
+
...composeInetAddress(family, ip),
|
|
40
|
+
port,
|
|
41
|
+
},
|
|
42
|
+
]);
|
|
43
|
+
});
|
|
44
|
+
this.sock.on("connectionAttemptFailed", (ip, port, family, error) => {
|
|
45
|
+
this.dispatch("connectionAttemptFailed", [
|
|
46
|
+
{
|
|
47
|
+
...composeInetAddress(family, ip),
|
|
48
|
+
port,
|
|
49
|
+
},
|
|
50
|
+
error,
|
|
51
|
+
]);
|
|
52
|
+
});
|
|
53
|
+
this.sock.on("connectionAttemptTimeout", (ip, port, family) => {
|
|
54
|
+
this.dispatch("connectionAttemptTimeout", [
|
|
55
|
+
{
|
|
56
|
+
...composeInetAddress(family, ip),
|
|
57
|
+
port,
|
|
58
|
+
},
|
|
59
|
+
]);
|
|
60
|
+
});
|
|
61
|
+
}
|
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AAoB,EAAE,CAAC;QAE7B,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,mBAAmB,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE;YACtD,IAAI,CAAC,QAAQ,CAAC,mBAAmB,EAAE;gBAClC;oBACC,GAAG,kBAAkB,CAAC,MAAM,EAAE,EAAE,CAAC;oBACjC,IAAI;iBACJ;aACD,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,yBAAyB,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE;YACnE,IAAI,CAAC,QAAQ,CAAC,yBAAyB,EAAE;gBACxC;oBACC,GAAG,kBAAkB,CAAC,MAAM,EAAE,EAAE,CAAC;oBACjC,IAAI;iBACJ;gBACD,KAAK;aACL,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,0BAA0B,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE;YAC7D,IAAI,CAAC,QAAQ,CAAC,0BAA0B,EAAE;gBACzC;oBACC,GAAG,kBAAkB,CAAC,MAAM,EAAE,EAAE,CAAC;oBACjC,IAAI;iBACJ;aACD,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC;IACJ,CAAC;CACD"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import * as net from "node:net";
|
|
2
|
+
import type * as stream from "node:stream";
|
|
3
|
+
import { EventDispatcherMapBase, IEventDispatcherMap } from "@ac-kit/async";
|
|
4
|
+
import { type IError } from "@ac-kit/core";
|
|
5
|
+
/**
|
|
6
|
+
* Event map for {@link StreamSocket} lifecycle events.
|
|
7
|
+
*
|
|
8
|
+
* These correspond to the core {@link net.Socket} events that are meaningful for
|
|
9
|
+
* any stream-oriented connection, regardless of the underlying transport (TCP,
|
|
10
|
+
* Unix domain socket, etc.).
|
|
11
|
+
*/
|
|
12
|
+
export type StreamSocketEvents = {
|
|
13
|
+
/**
|
|
14
|
+
* Emitted when an error occurs on the underlying socket.
|
|
15
|
+
*
|
|
16
|
+
* After this event the implementation will typically attempt to close the
|
|
17
|
+
* socket and then emit {@link StreamSocketEvents.close}.
|
|
18
|
+
*/
|
|
19
|
+
error: [error: IError];
|
|
20
|
+
/**
|
|
21
|
+
* Emitted once the socket has been fully closed.
|
|
22
|
+
*
|
|
23
|
+
* After this event the socket is no longer usable.
|
|
24
|
+
*/
|
|
25
|
+
close: [hadError: boolean];
|
|
26
|
+
/**
|
|
27
|
+
* Emitted when the socket connection is successfully established.
|
|
28
|
+
*
|
|
29
|
+
* This maps to the underlying {@link net.Socket} "connect" event.
|
|
30
|
+
*/
|
|
31
|
+
connect: [];
|
|
32
|
+
/**
|
|
33
|
+
* Emitted when the socket is ready for I/O.
|
|
34
|
+
*
|
|
35
|
+
* On Node.js this is typically fired immediately after "connect", once the
|
|
36
|
+
* internal initialization is complete.
|
|
37
|
+
*/
|
|
38
|
+
ready: [];
|
|
39
|
+
/**
|
|
40
|
+
* Emitted if the socket times out from inactivity.
|
|
41
|
+
*
|
|
42
|
+
* The underlying connection is not automatically closed; it is up to the
|
|
43
|
+
* caller to decide whether to end or destroy the socket.
|
|
44
|
+
*/
|
|
45
|
+
timeout: [];
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Base class for stream-oriented client sockets.
|
|
49
|
+
*
|
|
50
|
+
* This abstraction wraps a {@link net.Socket} instance and exposes:
|
|
51
|
+
*
|
|
52
|
+
* - A stable, duplex {@link stream} for reading and writing data.
|
|
53
|
+
* - Common lifecycle properties such as {@link closed}, {@link destroyed},
|
|
54
|
+
* {@link bytesRead}, {@link bytesWritten} and {@link connecting}.
|
|
55
|
+
* - Timeout management via the {@link timeout} property.
|
|
56
|
+
* - Promise-based {@link end} and {@link write} helpers.
|
|
57
|
+
* - Strongly-typed lifecycle events via {@link StreamSocketEvents}.
|
|
58
|
+
*
|
|
59
|
+
* It is intentionally transport-agnostic: it does not assume a particular
|
|
60
|
+
* address family or protocol. Concrete subclasses such as {@link TcpSocket} add
|
|
61
|
+
* protocol-specific concerns (for example `InetEndpoint` accessors, TCP
|
|
62
|
+
* keep-alive configuration, or DNS resolution events) on top of this base.
|
|
63
|
+
*
|
|
64
|
+
* Unless explicitly documented otherwise, methods mirror the semantics of their
|
|
65
|
+
* underlying {@link net.Socket} counterparts.
|
|
66
|
+
*/
|
|
67
|
+
export declare class StreamSocket<TSock extends net.Socket = net.Socket, TEvents extends StreamSocketEvents = StreamSocketEvents> extends EventDispatcherMapBase<TEvents> implements IEventDispatcherMap<TEvents> {
|
|
68
|
+
protected readonly sock: TSock;
|
|
69
|
+
protected readonly handledErrorEvents: Set<IError>;
|
|
70
|
+
/**
|
|
71
|
+
* Creates a new {@link StreamSocket} instance.
|
|
72
|
+
*
|
|
73
|
+
* @param options Options for creating the underlying Node.js socket.
|
|
74
|
+
* @returns A new `StreamSocket` instance.
|
|
75
|
+
*/
|
|
76
|
+
static from(options?: net.SocketConstructorOpts): StreamSocket;
|
|
77
|
+
/**
|
|
78
|
+
* Creates a new {@link StreamSocket} instance.
|
|
79
|
+
*
|
|
80
|
+
* @param sock The underlying Node.js socket.
|
|
81
|
+
*/
|
|
82
|
+
constructor(sock: TSock);
|
|
83
|
+
/**
|
|
84
|
+
* Underlying duplex stream for reading and writing data.
|
|
85
|
+
*
|
|
86
|
+
* This is the wrapped {@link net.Socket} instance and can be passed directly
|
|
87
|
+
* to APIs that expect a Node.js stream.
|
|
88
|
+
*/
|
|
89
|
+
get stream(): stream.Duplex;
|
|
90
|
+
/** Indicates whether the socket has been fully closed. */
|
|
91
|
+
get closed(): boolean;
|
|
92
|
+
/** Indicates whether the underlying socket has been destroyed. */
|
|
93
|
+
get destroyed(): boolean;
|
|
94
|
+
/** Total number of bytes read from the socket so far. */
|
|
95
|
+
get bytesRead(): number;
|
|
96
|
+
/** Total number of bytes written to the socket so far. */
|
|
97
|
+
get bytesWritten(): number;
|
|
98
|
+
/** Whether the socket is currently in the process of connecting. */
|
|
99
|
+
get connecting(): boolean;
|
|
100
|
+
/** Current inactivity timeout in milliseconds, or `null` if disabled. */
|
|
101
|
+
get timeout(): number | null;
|
|
102
|
+
/**
|
|
103
|
+
* Updates the inactivity timeout for the socket.
|
|
104
|
+
*
|
|
105
|
+
* When set to a positive number, the socket emits a "timeout" event if no I/O
|
|
106
|
+
* activity occurs within the given number of milliseconds. A value of `0` or
|
|
107
|
+
* `null` disables the timeout entirely.
|
|
108
|
+
*/
|
|
109
|
+
set timeout(timeout: number | null);
|
|
110
|
+
/**
|
|
111
|
+
* Marks the socket as referenced, preventing the Node.js process from exiting
|
|
112
|
+
* while the socket is active.
|
|
113
|
+
*/
|
|
114
|
+
ref(): void;
|
|
115
|
+
/**
|
|
116
|
+
* Marks the socket as unreferenced, allowing the Node.js process to exit even
|
|
117
|
+
* if the socket is still active.
|
|
118
|
+
*/
|
|
119
|
+
unref(): void;
|
|
120
|
+
/**
|
|
121
|
+
* Half-closes the socket, optionally waiting for a full close.
|
|
122
|
+
*
|
|
123
|
+
* If `waitForClose` is omitted or `false`, the promise resolves once the
|
|
124
|
+
* local side has finished sending data and the FIN has been queued. When
|
|
125
|
+
* `waitForClose` is `true`, the promise resolves only after the remote side
|
|
126
|
+
* has also closed and the "close" event has fired.
|
|
127
|
+
*
|
|
128
|
+
* @param options Optional settings for ending the socket.
|
|
129
|
+
* @returns A promise that resolves once the socket has ended.
|
|
130
|
+
*/
|
|
131
|
+
end(options?: {
|
|
132
|
+
waitForClose?: boolean;
|
|
133
|
+
}): Promise<void>;
|
|
134
|
+
/**
|
|
135
|
+
* Writes data to the socket and resolves once that chunk has been flushed.
|
|
136
|
+
*
|
|
137
|
+
* Awaiting the result is what applies backpressure: the promise settles on
|
|
138
|
+
* the chunk's own flush rather than on the whole buffer draining, so a
|
|
139
|
+
* producer that awaits each write never outruns the socket. A caller that
|
|
140
|
+
* does not await is free to queue without bound.
|
|
141
|
+
*
|
|
142
|
+
* @param data The data to write.
|
|
143
|
+
* @throws When the socket errors or is destroyed before the chunk flushes.
|
|
144
|
+
*/
|
|
145
|
+
write(data: Uint8Array): Promise<void>;
|
|
146
|
+
/**
|
|
147
|
+
* Immediately destroys the underlying socket.
|
|
148
|
+
*
|
|
149
|
+
* Pending I/O is discarded, the connection is closed and the socket
|
|
150
|
+
* transitions to a terminal state. Any registered "close" listeners will
|
|
151
|
+
* still be invoked.
|
|
152
|
+
*/
|
|
153
|
+
destroy(): void;
|
|
154
|
+
protected setupEventForwarding(): void;
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=stream-socket.d.ts.map
|
|
@@ -0,0 +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,MAAM,cAAc,CAAC;AAE3C;;;;;;GAMG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAChC;;;;;OAKG;IACH,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAEvB;;;;OAIG;IACH,KAAK,EAAE,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAE3B;;;;OAIG;IACH,OAAO,EAAE,EAAE,CAAC;IAEZ;;;;;OAKG;IACH,KAAK,EAAE,EAAE,CAAC;IAEV;;;;;OAKG;IACH,OAAO,EAAE,EAAE,CAAC;CACZ,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,YAAY,CACxB,KAAK,SAAS,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,MAAM,EACrC,OAAO,SAAS,kBA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