UNPKG

36.2 kBTypeScriptView Raw
1/**
2 * > Stability: 2 - Stable
3 *
4 * The `net` module provides an asynchronous network API for creating stream-based
5 * TCP or `IPC` servers ({@link createServer}) and clients
6 * ({@link createConnection}).
7 *
8 * It can be accessed using:
9 *
10 * ```js
11 * const net = require('net');
12 * ```
13 * @see [source](https://github.com/nodejs/node/blob/v17.0.0/lib/net.js)
14 */
15declare module 'net' {
16 import * as stream from 'node:stream';
17 import { Abortable, EventEmitter } from 'node:events';
18 import * as dns from 'node:dns';
19 type LookupFunction = (hostname: string, options: dns.LookupOneOptions, callback: (err: NodeJS.ErrnoException | null, address: string, family: number) => void) => void;
20 interface AddressInfo {
21 address: string;
22 family: string;
23 port: number;
24 }
25 interface SocketConstructorOpts {
26 fd?: number | undefined;
27 allowHalfOpen?: boolean | undefined;
28 readable?: boolean | undefined;
29 writable?: boolean | undefined;
30 signal?: AbortSignal;
31 }
32 interface OnReadOpts {
33 buffer: Uint8Array | (() => Uint8Array);
34 /**
35 * This function is called for every chunk of incoming data.
36 * Two arguments are passed to it: the number of bytes written to buffer and a reference to buffer.
37 * Return false from this function to implicitly pause() the socket.
38 */
39 callback(bytesWritten: number, buf: Uint8Array): boolean;
40 }
41 interface ConnectOpts {
42 /**
43 * If specified, incoming data is stored in a single buffer and passed to the supplied callback when data arrives on the socket.
44 * Note: this will cause the streaming functionality to not provide any data, however events like 'error', 'end', and 'close' will
45 * still be emitted as normal and methods like pause() and resume() will also behave as expected.
46 */
47 onread?: OnReadOpts | undefined;
48 }
49 interface TcpSocketConnectOpts extends ConnectOpts {
50 port: number;
51 host?: string | undefined;
52 localAddress?: string | undefined;
53 localPort?: number | undefined;
54 hints?: number | undefined;
55 family?: number | undefined;
56 lookup?: LookupFunction | undefined;
57 }
58 interface IpcSocketConnectOpts extends ConnectOpts {
59 path: string;
60 }
61 type SocketConnectOpts = TcpSocketConnectOpts | IpcSocketConnectOpts;
62 /**
63 * This class is an abstraction of a TCP socket or a streaming `IPC` endpoint
64 * (uses named pipes on Windows, and Unix domain sockets otherwise). It is also
65 * an `EventEmitter`.
66 *
67 * A `net.Socket` can be created by the user and used directly to interact with
68 * a server. For example, it is returned by {@link createConnection},
69 * so the user can use it to talk to the server.
70 *
71 * It can also be created by Node.js and passed to the user when a connection
72 * is received. For example, it is passed to the listeners of a `'connection'` event emitted on a {@link Server}, so the user can use
73 * it to interact with the client.
74 * @since v0.3.4
75 */
76 class Socket extends stream.Duplex {
77 constructor(options?: SocketConstructorOpts);
78 /**
79 * Sends data on the socket. The second parameter specifies the encoding in the
80 * case of a string. It defaults to UTF8 encoding.
81 *
82 * Returns `true` if the entire data was flushed successfully to the kernel
83 * buffer. Returns `false` if all or part of the data was queued in user memory.`'drain'` will be emitted when the buffer is again free.
84 *
85 * The optional `callback` parameter will be executed when the data is finally
86 * written out, which may not be immediately.
87 *
88 * See `Writable` stream `write()` method for more
89 * information.
90 * @since v0.1.90
91 * @param [encoding='utf8'] Only used when data is `string`.
92 */
93 write(buffer: Uint8Array | string, cb?: (err?: Error) => void): boolean;
94 write(str: Uint8Array | string, encoding?: BufferEncoding, cb?: (err?: Error) => void): boolean;
95 /**
96 * Initiate a connection on a given socket.
97 *
98 * Possible signatures:
99 *
100 * * `socket.connect(options[, connectListener])`
101 * * `socket.connect(path[, connectListener])` for `IPC` connections.
102 * * `socket.connect(port[, host][, connectListener])` for TCP connections.
103 * * Returns: `net.Socket` The socket itself.
104 *
105 * This function is asynchronous. When the connection is established, the `'connect'` event will be emitted. If there is a problem connecting,
106 * instead of a `'connect'` event, an `'error'` event will be emitted with
107 * the error passed to the `'error'` listener.
108 * The last parameter `connectListener`, if supplied, will be added as a listener
109 * for the `'connect'` event **once**.
110 *
111 * This function should only be used for reconnecting a socket after`'close'` has been emitted or otherwise it may lead to undefined
112 * behavior.
113 */
114 connect(options: SocketConnectOpts, connectionListener?: () => void): this;
115 connect(port: number, host: string, connectionListener?: () => void): this;
116 connect(port: number, connectionListener?: () => void): this;
117 connect(path: string, connectionListener?: () => void): this;
118 /**
119 * Set the encoding for the socket as a `Readable Stream`. See `readable.setEncoding()` for more information.
120 * @since v0.1.90
121 * @return The socket itself.
122 */
123 setEncoding(encoding?: BufferEncoding): this;
124 /**
125 * Pauses the reading of data. That is, `'data'` events will not be emitted.
126 * Useful to throttle back an upload.
127 * @return The socket itself.
128 */
129 pause(): this;
130 /**
131 * Resumes reading after a call to `socket.pause()`.
132 * @return The socket itself.
133 */
134 resume(): this;
135 /**
136 * Sets the socket to timeout after `timeout` milliseconds of inactivity on
137 * the socket. By default `net.Socket` do not have a timeout.
138 *
139 * When an idle timeout is triggered the socket will receive a `'timeout'` event but the connection will not be severed. The user must manually call `socket.end()` or `socket.destroy()` to
140 * end the connection.
141 *
142 * ```js
143 * socket.setTimeout(3000);
144 * socket.on('timeout', () => {
145 * console.log('socket timeout');
146 * socket.end();
147 * });
148 * ```
149 *
150 * If `timeout` is 0, then the existing idle timeout is disabled.
151 *
152 * The optional `callback` parameter will be added as a one-time listener for the `'timeout'` event.
153 * @since v0.1.90
154 * @return The socket itself.
155 */
156 setTimeout(timeout: number, callback?: () => void): this;
157 /**
158 * Enable/disable the use of Nagle's algorithm.
159 *
160 * When a TCP connection is created, it will have Nagle's algorithm enabled.
161 *
162 * Nagle's algorithm delays data before it is sent via the network. It attempts
163 * to optimize throughput at the expense of latency.
164 *
165 * Passing `true` for `noDelay` or not passing an argument will disable Nagle's
166 * algorithm for the socket. Passing `false` for `noDelay` will enable Nagle's
167 * algorithm.
168 * @since v0.1.90
169 * @param [noDelay=true]
170 * @return The socket itself.
171 */
172 setNoDelay(noDelay?: boolean): this;
173 /**
174 * Enable/disable keep-alive functionality, and optionally set the initial
175 * delay before the first keepalive probe is sent on an idle socket.
176 *
177 * Set `initialDelay` (in milliseconds) to set the delay between the last
178 * data packet received and the first keepalive probe. Setting `0` for`initialDelay` will leave the value unchanged from the default
179 * (or previous) setting.
180 *
181 * Enabling the keep-alive functionality will set the following socket options:
182 *
183 * * `SO_KEEPALIVE=1`
184 * * `TCP_KEEPIDLE=initialDelay`
185 * * `TCP_KEEPCNT=10`
186 * * `TCP_KEEPINTVL=1`
187 * @since v0.1.92
188 * @param [enable=false]
189 * @param [initialDelay=0]
190 * @return The socket itself.
191 */
192 setKeepAlive(enable?: boolean, initialDelay?: number): this;
193 /**
194 * Returns the bound `address`, the address `family` name and `port` of the
195 * socket as reported by the operating system:`{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`
196 * @since v0.1.90
197 */
198 address(): AddressInfo | {};
199 /**
200 * Calling `unref()` on a socket will allow the program to exit if this is the only
201 * active socket in the event system. If the socket is already `unref`ed calling`unref()` again will have no effect.
202 * @since v0.9.1
203 * @return The socket itself.
204 */
205 unref(): this;
206 /**
207 * Opposite of `unref()`, calling `ref()` on a previously `unref`ed socket will_not_ let the program exit if it's the only socket left (the default behavior).
208 * If the socket is `ref`ed calling `ref` again will have no effect.
209 * @since v0.9.1
210 * @return The socket itself.
211 */
212 ref(): this;
213 /**
214 * This property shows the number of characters buffered for writing. The buffer
215 * may contain strings whose length after encoding is not yet known. So this number
216 * is only an approximation of the number of bytes in the buffer.
217 *
218 * `net.Socket` has the property that `socket.write()` always works. This is to
219 * help users get up and running quickly. The computer cannot always keep up
220 * with the amount of data that is written to a socket. The network connection
221 * simply might be too slow. Node.js will internally queue up the data written to a
222 * socket and send it out over the wire when it is possible.
223 *
224 * The consequence of this internal buffering is that memory may grow.
225 * Users who experience large or growing `bufferSize` should attempt to
226 * "throttle" the data flows in their program with `socket.pause()` and `socket.resume()`.
227 * @since v0.3.8
228 * @deprecated Since v14.6.0 - Use `writableLength` instead.
229 */
230 readonly bufferSize: number;
231 /**
232 * The amount of received bytes.
233 * @since v0.5.3
234 */
235 readonly bytesRead: number;
236 /**
237 * The amount of bytes sent.
238 * @since v0.5.3
239 */
240 readonly bytesWritten: number;
241 /**
242 * If `true`,`socket.connect(options[, connectListener])` was
243 * called and has not yet finished. It will stay `true` until the socket becomes
244 * connected, then it is set to `false` and the `'connect'` event is emitted. Note
245 * that the `socket.connect(options[, connectListener])` callback is a listener for the `'connect'` event.
246 * @since v6.1.0
247 */
248 readonly connecting: boolean;
249 /**
250 * See `writable.destroyed` for further details.
251 */
252 readonly destroyed: boolean;
253 /**
254 * The string representation of the local IP address the remote client is
255 * connecting on. For example, in a server listening on `'0.0.0.0'`, if a client
256 * connects on `'192.168.1.1'`, the value of `socket.localAddress` would be`'192.168.1.1'`.
257 * @since v0.9.6
258 */
259 readonly localAddress?: string;
260 /**
261 * The numeric representation of the local port. For example, `80` or `21`.
262 * @since v0.9.6
263 */
264 readonly localPort?: number;
265 /**
266 * The string representation of the remote IP address. For example,`'74.125.127.100'` or `'2001:4860:a005::68'`. Value may be `undefined` if
267 * the socket is destroyed (for example, if the client disconnected).
268 * @since v0.5.10
269 */
270 readonly remoteAddress?: string | undefined;
271 /**
272 * The string representation of the remote IP family. `'IPv4'` or `'IPv6'`.
273 * @since v0.11.14
274 */
275 readonly remoteFamily?: string | undefined;
276 /**
277 * The numeric representation of the remote port. For example, `80` or `21`.
278 * @since v0.5.10
279 */
280 readonly remotePort?: number | undefined;
281 /**
282 * Half-closes the socket. i.e., it sends a FIN packet. It is possible the
283 * server will still send some data.
284 *
285 * See `writable.end()` for further details.
286 * @since v0.1.90
287 * @param [encoding='utf8'] Only used when data is `string`.
288 * @param callback Optional callback for when the socket is finished.
289 * @return The socket itself.
290 */
291 end(callback?: () => void): this;
292 end(buffer: Uint8Array | string, callback?: () => void): this;
293 end(str: Uint8Array | string, encoding?: BufferEncoding, callback?: () => void): this;
294 /**
295 * events.EventEmitter
296 * 1. close
297 * 2. connect
298 * 3. data
299 * 4. drain
300 * 5. end
301 * 6. error
302 * 7. lookup
303 * 8. timeout
304 */
305 addListener(event: string, listener: (...args: any[]) => void): this;
306 addListener(event: 'close', listener: (hadError: boolean) => void): this;
307 addListener(event: 'connect', listener: () => void): this;
308 addListener(event: 'data', listener: (data: Buffer) => void): this;
309 addListener(event: 'drain', listener: () => void): this;
310 addListener(event: 'end', listener: () => void): this;
311 addListener(event: 'error', listener: (err: Error) => void): this;
312 addListener(event: 'lookup', listener: (err: Error, address: string, family: string | number, host: string) => void): this;
313 addListener(event: 'ready', listener: () => void): this;
314 addListener(event: 'timeout', listener: () => void): this;
315 emit(event: string | symbol, ...args: any[]): boolean;
316 emit(event: 'close', hadError: boolean): boolean;
317 emit(event: 'connect'): boolean;
318 emit(event: 'data', data: Buffer): boolean;
319 emit(event: 'drain'): boolean;
320 emit(event: 'end'): boolean;
321 emit(event: 'error', err: Error): boolean;
322 emit(event: 'lookup', err: Error, address: string, family: string | number, host: string): boolean;
323 emit(event: 'ready'): boolean;
324 emit(event: 'timeout'): boolean;
325 on(event: string, listener: (...args: any[]) => void): this;
326 on(event: 'close', listener: (hadError: boolean) => void): this;
327 on(event: 'connect', listener: () => void): this;
328 on(event: 'data', listener: (data: Buffer) => void): this;
329 on(event: 'drain', listener: () => void): this;
330 on(event: 'end', listener: () => void): this;
331 on(event: 'error', listener: (err: Error) => void): this;
332 on(event: 'lookup', listener: (err: Error, address: string, family: string | number, host: string) => void): this;
333 on(event: 'ready', listener: () => void): this;
334 on(event: 'timeout', listener: () => void): this;
335 once(event: string, listener: (...args: any[]) => void): this;
336 once(event: 'close', listener: (hadError: boolean) => void): this;
337 once(event: 'connect', listener: () => void): this;
338 once(event: 'data', listener: (data: Buffer) => void): this;
339 once(event: 'drain', listener: () => void): this;
340 once(event: 'end', listener: () => void): this;
341 once(event: 'error', listener: (err: Error) => void): this;
342 once(event: 'lookup', listener: (err: Error, address: string, family: string | number, host: string) => void): this;
343 once(event: 'ready', listener: () => void): this;
344 once(event: 'timeout', listener: () => void): this;
345 prependListener(event: string, listener: (...args: any[]) => void): this;
346 prependListener(event: 'close', listener: (hadError: boolean) => void): this;
347 prependListener(event: 'connect', listener: () => void): this;
348 prependListener(event: 'data', listener: (data: Buffer) => void): this;
349 prependListener(event: 'drain', listener: () => void): this;
350 prependListener(event: 'end', listener: () => void): this;
351 prependListener(event: 'error', listener: (err: Error) => void): this;
352 prependListener(event: 'lookup', listener: (err: Error, address: string, family: string | number, host: string) => void): this;
353 prependListener(event: 'ready', listener: () => void): this;
354 prependListener(event: 'timeout', listener: () => void): this;
355 prependOnceListener(event: string, listener: (...args: any[]) => void): this;
356 prependOnceListener(event: 'close', listener: (hadError: boolean) => void): this;
357 prependOnceListener(event: 'connect', listener: () => void): this;
358 prependOnceListener(event: 'data', listener: (data: Buffer) => void): this;
359 prependOnceListener(event: 'drain', listener: () => void): this;
360 prependOnceListener(event: 'end', listener: () => void): this;
361 prependOnceListener(event: 'error', listener: (err: Error) => void): this;
362 prependOnceListener(event: 'lookup', listener: (err: Error, address: string, family: string | number, host: string) => void): this;
363 prependOnceListener(event: 'ready', listener: () => void): this;
364 prependOnceListener(event: 'timeout', listener: () => void): this;
365 }
366 interface ListenOptions extends Abortable {
367 port?: number | undefined;
368 host?: string | undefined;
369 backlog?: number | undefined;
370 path?: string | undefined;
371 exclusive?: boolean | undefined;
372 readableAll?: boolean | undefined;
373 writableAll?: boolean | undefined;
374 /**
375 * @default false
376 */
377 ipv6Only?: boolean | undefined;
378 }
379 interface ServerOpts {
380 /**
381 * Indicates whether half-opened TCP connections are allowed.
382 * @default false
383 */
384 allowHalfOpen?: boolean | undefined;
385 /**
386 * Indicates whether the socket should be paused on incoming connections.
387 * @default false
388 */
389 pauseOnConnect?: boolean | undefined;
390 }
391 /**
392 * This class is used to create a TCP or `IPC` server.
393 * @since v0.1.90
394 */
395 class Server extends EventEmitter {
396 constructor(connectionListener?: (socket: Socket) => void);
397 constructor(options?: ServerOpts, connectionListener?: (socket: Socket) => void);
398 /**
399 * Start a server listening for connections. A `net.Server` can be a TCP or
400 * an `IPC` server depending on what it listens to.
401 *
402 * Possible signatures:
403 *
404 * * `server.listen(handle[, backlog][, callback])`
405 * * `server.listen(options[, callback])`
406 * * `server.listen(path[, backlog][, callback])` for `IPC` servers
407 * * `server.listen([port[, host[, backlog]]][, callback])` for TCP servers
408 *
409 * This function is asynchronous. When the server starts listening, the `'listening'` event will be emitted. The last parameter `callback`will be added as a listener for the `'listening'`
410 * event.
411 *
412 * All `listen()` methods can take a `backlog` parameter to specify the maximum
413 * length of the queue of pending connections. The actual length will be determined
414 * by the OS through sysctl settings such as `tcp_max_syn_backlog` and `somaxconn`on Linux. The default value of this parameter is 511 (not 512).
415 *
416 * All {@link Socket} are set to `SO_REUSEADDR` (see [`socket(7)`](https://man7.org/linux/man-pages/man7/socket.7.html) for
417 * details).
418 *
419 * The `server.listen()` method can be called again if and only if there was an
420 * error during the first `server.listen()` call or `server.close()` has been
421 * called. Otherwise, an `ERR_SERVER_ALREADY_LISTEN` error will be thrown.
422 *
423 * One of the most common errors raised when listening is `EADDRINUSE`.
424 * This happens when another server is already listening on the requested`port`/`path`/`handle`. One way to handle this would be to retry
425 * after a certain amount of time:
426 *
427 * ```js
428 * server.on('error', (e) => {
429 * if (e.code === 'EADDRINUSE') {
430 * console.log('Address in use, retrying...');
431 * setTimeout(() => {
432 * server.close();
433 * server.listen(PORT, HOST);
434 * }, 1000);
435 * }
436 * });
437 * ```
438 */
439 listen(port?: number, hostname?: string, backlog?: number, listeningListener?: () => void): this;
440 listen(port?: number, hostname?: string, listeningListener?: () => void): this;
441 listen(port?: number, backlog?: number, listeningListener?: () => void): this;
442 listen(port?: number, listeningListener?: () => void): this;
443 listen(path: string, backlog?: number, listeningListener?: () => void): this;
444 listen(path: string, listeningListener?: () => void): this;
445 listen(options: ListenOptions, listeningListener?: () => void): this;
446 listen(handle: any, backlog?: number, listeningListener?: () => void): this;
447 listen(handle: any, listeningListener?: () => void): this;
448 /**
449 * Stops the server from accepting new connections and keeps existing
450 * connections. This function is asynchronous, the server is finally closed
451 * when all connections are ended and the server emits a `'close'` event.
452 * The optional `callback` will be called once the `'close'` event occurs. Unlike
453 * that event, it will be called with an `Error` as its only argument if the server
454 * was not open when it was closed.
455 * @since v0.1.90
456 * @param callback Called when the server is closed.
457 */
458 close(callback?: (err?: Error) => void): this;
459 /**
460 * Returns the bound `address`, the address `family` name, and `port` of the server
461 * as reported by the operating system if listening on an IP socket
462 * (useful to find which port was assigned when getting an OS-assigned address):`{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`.
463 *
464 * For a server listening on a pipe or Unix domain socket, the name is returned
465 * as a string.
466 *
467 * ```js
468 * const server = net.createServer((socket) => {
469 * socket.end('goodbye\n');
470 * }).on('error', (err) => {
471 * // Handle errors here.
472 * throw err;
473 * });
474 *
475 * // Grab an arbitrary unused port.
476 * server.listen(() => {
477 * console.log('opened server on', server.address());
478 * });
479 * ```
480 *
481 * `server.address()` returns `null` before the `'listening'` event has been
482 * emitted or after calling `server.close()`.
483 * @since v0.1.90
484 */
485 address(): AddressInfo | string | null;
486 /**
487 * Asynchronously get the number of concurrent connections on the server. Works
488 * when sockets were sent to forks.
489 *
490 * Callback should take two arguments `err` and `count`.
491 * @since v0.9.7
492 */
493 getConnections(cb: (error: Error | null, count: number) => void): void;
494 /**
495 * Opposite of `unref()`, calling `ref()` on a previously `unref`ed server will_not_ let the program exit if it's the only server left (the default behavior).
496 * If the server is `ref`ed calling `ref()` again will have no effect.
497 * @since v0.9.1
498 */
499 ref(): this;
500 /**
501 * Calling `unref()` on a server will allow the program to exit if this is the only
502 * active server in the event system. If the server is already `unref`ed calling`unref()` again will have no effect.
503 * @since v0.9.1
504 */
505 unref(): this;
506 /**
507 * Set this property to reject connections when the server's connection count gets
508 * high.
509 *
510 * It is not recommended to use this option once a socket has been sent to a child
511 * with `child_process.fork()`.
512 * @since v0.2.0
513 */
514 maxConnections: number;
515 connections: number;
516 /**
517 * Indicates whether or not the server is listening for connections.
518 * @since v5.7.0
519 */
520 listening: boolean;
521 /**
522 * events.EventEmitter
523 * 1. close
524 * 2. connection
525 * 3. error
526 * 4. listening
527 */
528 addListener(event: string, listener: (...args: any[]) => void): this;
529 addListener(event: 'close', listener: () => void): this;
530 addListener(event: 'connection', listener: (socket: Socket) => void): this;
531 addListener(event: 'error', listener: (err: Error) => void): this;
532 addListener(event: 'listening', listener: () => void): this;
533 emit(event: string | symbol, ...args: any[]): boolean;
534 emit(event: 'close'): boolean;
535 emit(event: 'connection', socket: Socket): boolean;
536 emit(event: 'error', err: Error): boolean;
537 emit(event: 'listening'): boolean;
538 on(event: string, listener: (...args: any[]) => void): this;
539 on(event: 'close', listener: () => void): this;
540 on(event: 'connection', listener: (socket: Socket) => void): this;
541 on(event: 'error', listener: (err: Error) => void): this;
542 on(event: 'listening', listener: () => void): this;
543 once(event: string, listener: (...args: any[]) => void): this;
544 once(event: 'close', listener: () => void): this;
545 once(event: 'connection', listener: (socket: Socket) => void): this;
546 once(event: 'error', listener: (err: Error) => void): this;
547 once(event: 'listening', listener: () => void): this;
548 prependListener(event: string, listener: (...args: any[]) => void): this;
549 prependListener(event: 'close', listener: () => void): this;
550 prependListener(event: 'connection', listener: (socket: Socket) => void): this;
551 prependListener(event: 'error', listener: (err: Error) => void): this;
552 prependListener(event: 'listening', listener: () => void): this;
553 prependOnceListener(event: string, listener: (...args: any[]) => void): this;
554 prependOnceListener(event: 'close', listener: () => void): this;
555 prependOnceListener(event: 'connection', listener: (socket: Socket) => void): this;
556 prependOnceListener(event: 'error', listener: (err: Error) => void): this;
557 prependOnceListener(event: 'listening', listener: () => void): this;
558 }
559 type IPVersion = 'ipv4' | 'ipv6';
560 /**
561 * The `BlockList` object can be used with some network APIs to specify rules for
562 * disabling inbound or outbound access to specific IP addresses, IP ranges, or
563 * IP subnets.
564 * @since v15.0.0, v14.18.0
565 */
566 class BlockList {
567 /**
568 * Adds a rule to block the given IP address.
569 * @since v15.0.0, v14.18.0
570 * @param address An IPv4 or IPv6 address.
571 * @param [type='ipv4'] Either `'ipv4'` or `'ipv6'`.
572 */
573 addAddress(address: string, type?: IPVersion): void;
574 addAddress(address: SocketAddress): void;
575 /**
576 * Adds a rule to block a range of IP addresses from `start` (inclusive) to`end` (inclusive).
577 * @since v15.0.0, v14.18.0
578 * @param start The starting IPv4 or IPv6 address in the range.
579 * @param end The ending IPv4 or IPv6 address in the range.
580 * @param [type='ipv4'] Either `'ipv4'` or `'ipv6'`.
581 */
582 addRange(start: string, end: string, type?: IPVersion): void;
583 addRange(start: SocketAddress, end: SocketAddress): void;
584 /**
585 * Adds a rule to block a range of IP addresses specified as a subnet mask.
586 * @since v15.0.0, v14.18.0
587 * @param net The network IPv4 or IPv6 address.
588 * @param prefix The number of CIDR prefix bits. For IPv4, this must be a value between `0` and `32`. For IPv6, this must be between `0` and `128`.
589 * @param [type='ipv4'] Either `'ipv4'` or `'ipv6'`.
590 */
591 addSubnet(net: SocketAddress, prefix: number): void;
592 addSubnet(net: string, prefix: number, type?: IPVersion): void;
593 /**
594 * Returns `true` if the given IP address matches any of the rules added to the`BlockList`.
595 *
596 * ```js
597 * const blockList = new net.BlockList();
598 * blockList.addAddress('123.123.123.123');
599 * blockList.addRange('10.0.0.1', '10.0.0.10');
600 * blockList.addSubnet('8592:757c:efae:4e45::', 64, 'ipv6');
601 *
602 * console.log(blockList.check('123.123.123.123')); // Prints: true
603 * console.log(blockList.check('10.0.0.3')); // Prints: true
604 * console.log(blockList.check('222.111.111.222')); // Prints: false
605 *
606 * // IPv6 notation for IPv4 addresses works:
607 * console.log(blockList.check('::ffff:7b7b:7b7b', 'ipv6')); // Prints: true
608 * console.log(blockList.check('::ffff:123.123.123.123', 'ipv6')); // Prints: true
609 * ```
610 * @since v15.0.0, v14.18.0
611 * @param address The IP address to check
612 * @param [type='ipv4'] Either `'ipv4'` or `'ipv6'`.
613 */
614 check(address: SocketAddress): boolean;
615 check(address: string, type?: IPVersion): boolean;
616 }
617 interface TcpNetConnectOpts extends TcpSocketConnectOpts, SocketConstructorOpts {
618 timeout?: number | undefined;
619 }
620 interface IpcNetConnectOpts extends IpcSocketConnectOpts, SocketConstructorOpts {
621 timeout?: number | undefined;
622 }
623 type NetConnectOpts = TcpNetConnectOpts | IpcNetConnectOpts;
624 /**
625 * Creates a new TCP or `IPC` server.
626 *
627 * If `allowHalfOpen` is set to `true`, when the other end of the socket
628 * signals the end of transmission, the server will only send back the end of
629 * transmission when `socket.end()` is explicitly called. For example, in the
630 * context of TCP, when a FIN packed is received, a FIN packed is sent
631 * back only when `socket.end()` is explicitly called. Until then the
632 * connection is half-closed (non-readable but still writable). See `'end'` event and [RFC 1122](https://tools.ietf.org/html/rfc1122) (section 4.2.2.13) for more information.
633 *
634 * If `pauseOnConnect` is set to `true`, then the socket associated with each
635 * incoming connection will be paused, and no data will be read from its handle.
636 * This allows connections to be passed between processes without any data being
637 * read by the original process. To begin reading data from a paused socket, call `socket.resume()`.
638 *
639 * The server can be a TCP server or an `IPC` server, depending on what it `listen()` to.
640 *
641 * Here is an example of an TCP echo server which listens for connections
642 * on port 8124:
643 *
644 * ```js
645 * const net = require('net');
646 * const server = net.createServer((c) => {
647 * // 'connection' listener.
648 * console.log('client connected');
649 * c.on('end', () => {
650 * console.log('client disconnected');
651 * });
652 * c.write('hello\r\n');
653 * c.pipe(c);
654 * });
655 * server.on('error', (err) => {
656 * throw err;
657 * });
658 * server.listen(8124, () => {
659 * console.log('server bound');
660 * });
661 * ```
662 *
663 * Test this by using `telnet`:
664 *
665 * ```console
666 * $ telnet localhost 8124
667 * ```
668 *
669 * To listen on the socket `/tmp/echo.sock`:
670 *
671 * ```js
672 * server.listen('/tmp/echo.sock', () => {
673 * console.log('server bound');
674 * });
675 * ```
676 *
677 * Use `nc` to connect to a Unix domain socket server:
678 *
679 * ```console
680 * $ nc -U /tmp/echo.sock
681 * ```
682 * @since v0.5.0
683 * @param connectionListener Automatically set as a listener for the {@link 'connection'} event.
684 */
685 function createServer(connectionListener?: (socket: Socket) => void): Server;
686 function createServer(options?: ServerOpts, connectionListener?: (socket: Socket) => void): Server;
687 /**
688 * Aliases to {@link createConnection}.
689 *
690 * Possible signatures:
691 *
692 * * {@link connect}
693 * * {@link connect} for `IPC` connections.
694 * * {@link connect} for TCP connections.
695 */
696 function connect(options: NetConnectOpts, connectionListener?: () => void): Socket;
697 function connect(port: number, host?: string, connectionListener?: () => void): Socket;
698 function connect(path: string, connectionListener?: () => void): Socket;
699 /**
700 * A factory function, which creates a new {@link Socket},
701 * immediately initiates connection with `socket.connect()`,
702 * then returns the `net.Socket` that starts the connection.
703 *
704 * When the connection is established, a `'connect'` event will be emitted
705 * on the returned socket. The last parameter `connectListener`, if supplied,
706 * will be added as a listener for the `'connect'` event **once**.
707 *
708 * Possible signatures:
709 *
710 * * {@link createConnection}
711 * * {@link createConnection} for `IPC` connections.
712 * * {@link createConnection} for TCP connections.
713 *
714 * The {@link connect} function is an alias to this function.
715 */
716 function createConnection(options: NetConnectOpts, connectionListener?: () => void): Socket;
717 function createConnection(port: number, host?: string, connectionListener?: () => void): Socket;
718 function createConnection(path: string, connectionListener?: () => void): Socket;
719 /**
720 * Tests if input is an IP address. Returns `0` for invalid strings,
721 * returns `4` for IP version 4 addresses, and returns `6` for IP version 6
722 * addresses.
723 * @since v0.3.0
724 */
725 function isIP(input: string): number;
726 /**
727 * Returns `true` if input is a version 4 IP address, otherwise returns `false`.
728 * @since v0.3.0
729 */
730 function isIPv4(input: string): boolean;
731 /**
732 * Returns `true` if input is a version 6 IP address, otherwise returns `false`.
733 * @since v0.3.0
734 */
735 function isIPv6(input: string): boolean;
736 interface SocketAddressInitOptions {
737 /**
738 * The network address as either an IPv4 or IPv6 string.
739 * @default 127.0.0.1
740 */
741 address?: string | undefined;
742 /**
743 * @default `'ipv4'`
744 */
745 family?: IPVersion | undefined;
746 /**
747 * An IPv6 flow-label used only if `family` is `'ipv6'`.
748 * @default 0
749 */
750 flowlabel?: number | undefined;
751 /**
752 * An IP port.
753 * @default 0
754 */
755 port?: number | undefined;
756 }
757 /**
758 * @since v15.14.0, v14.18.0
759 */
760 class SocketAddress {
761 constructor(options: SocketAddressInitOptions);
762 /**
763 * Either \`'ipv4'\` or \`'ipv6'\`.
764 * @since v15.14.0, v14.18.0
765 */
766 readonly address: string;
767 /**
768 * Either \`'ipv4'\` or \`'ipv6'\`.
769 * @since v15.14.0, v14.18.0
770 */
771 readonly family: IPVersion;
772 /**
773 * @since v15.14.0, v14.18.0
774 */
775 readonly port: number;
776 /**
777 * @since v15.14.0, v14.18.0
778 */
779 readonly flowlabel: number;
780 }
781}
782declare module 'node:net' {
783 export * from 'net';
784}
785
\No newline at end of file