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