@types/node 16.4.2 → 16.4.6

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