1 | declare module 'net' {
|
2 | import * as stream from 'stream';
|
3 | import { Abortable, EventEmitter } from 'events';
|
4 | import * as dns from '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 |
|
12 | interface AddressInfo {
|
13 | address: string;
|
14 | family: string;
|
15 | port: number;
|
16 | }
|
17 |
|
18 | interface SocketConstructorOpts {
|
19 | fd?: number;
|
20 | allowHalfOpen?: boolean;
|
21 | readable?: boolean;
|
22 | writable?: boolean;
|
23 | }
|
24 |
|
25 | interface OnReadOpts {
|
26 | buffer: Uint8Array | (() => Uint8Array);
|
27 | /**
|
28 | * This function is called for every chunk of incoming data.
|
29 | * Two arguments are passed to it: the number of bytes written to buffer and a reference to buffer.
|
30 | * Return false from this function to implicitly pause() the socket.
|
31 | */
|
32 | callback(bytesWritten: number, buf: Uint8Array): boolean;
|
33 | }
|
34 |
|
35 | interface ConnectOpts {
|
36 | /**
|
37 | * If specified, incoming data is stored in a single buffer and passed to the supplied callback when data arrives on the socket.
|
38 | * Note: this will cause the streaming functionality to not provide any data, however events like 'error', 'end', and 'close' will
|
39 | * still be emitted as normal and methods like pause() and resume() will also behave as expected.
|
40 | */
|
41 | onread?: OnReadOpts;
|
42 | }
|
43 |
|
44 | interface TcpSocketConnectOpts extends ConnectOpts {
|
45 | port: number;
|
46 | host?: string;
|
47 | localAddress?: string;
|
48 | localPort?: number;
|
49 | hints?: number;
|
50 | family?: number;
|
51 | lookup?: LookupFunction;
|
52 | }
|
53 |
|
54 | interface IpcSocketConnectOpts extends ConnectOpts {
|
55 | path: string;
|
56 | }
|
57 |
|
58 | type SocketConnectOpts = TcpSocketConnectOpts | IpcSocketConnectOpts;
|
59 |
|
60 | class Socket extends stream.Duplex {
|
61 | constructor(options?: SocketConstructorOpts);
|
62 |
|
63 | // Extended base methods
|
64 | write(buffer: Uint8Array | string, cb?: (err?: Error) => void): boolean;
|
65 | write(str: Uint8Array | string, encoding?: BufferEncoding, cb?: (err?: Error) => void): boolean;
|
66 |
|
67 | connect(options: SocketConnectOpts, connectionListener?: () => void): this;
|
68 | connect(port: number, host: string, connectionListener?: () => void): this;
|
69 | connect(port: number, connectionListener?: () => void): this;
|
70 | connect(path: string, connectionListener?: () => void): this;
|
71 |
|
72 | setEncoding(encoding?: BufferEncoding): this;
|
73 | pause(): this;
|
74 | resume(): this;
|
75 | setTimeout(timeout: number, callback?: () => void): this;
|
76 | setNoDelay(noDelay?: boolean): this;
|
77 | setKeepAlive(enable?: boolean, initialDelay?: number): this;
|
78 | address(): AddressInfo | {};
|
79 | unref(): this;
|
80 | ref(): this;
|
81 |
|
82 | /** @deprecated since v14.6.0 - Use `writableLength` instead. */
|
83 | readonly bufferSize: number;
|
84 | readonly bytesRead: number;
|
85 | readonly bytesWritten: number;
|
86 | readonly connecting: boolean;
|
87 | readonly destroyed: boolean;
|
88 | readonly localAddress: string;
|
89 | readonly localPort: number;
|
90 | readonly remoteAddress?: string;
|
91 | readonly remoteFamily?: string;
|
92 | readonly remotePort?: number;
|
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 |
|
99 | /**
|
100 | * events.EventEmitter
|
101 | * 1. close
|
102 | * 2. connect
|
103 | * 3. data
|
104 | * 4. drain
|
105 | * 5. end
|
106 | * 6. error
|
107 | * 7. lookup
|
108 | * 8. timeout
|
109 | */
|
110 | 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 |
|
120 | 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 |
|
130 | 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 |
|
140 | 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 |
|
150 | 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 |
|
160 | 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;
|
169 | }
|
170 |
|
171 | interface ListenOptions extends Abortable {
|
172 | port?: number;
|
173 | host?: string;
|
174 | backlog?: number;
|
175 | path?: string;
|
176 | exclusive?: boolean;
|
177 | readableAll?: boolean;
|
178 | writableAll?: boolean;
|
179 | /**
|
180 | * @default false
|
181 | */
|
182 | ipv6Only?: boolean;
|
183 | }
|
184 |
|
185 | interface ServerOpts {
|
186 | /**
|
187 | * Indicates whether half-opened TCP connections are allowed.
|
188 | * @default false
|
189 | */
|
190 | allowHalfOpen?: boolean;
|
191 |
|
192 | /**
|
193 | * Indicates whether the socket should be paused on incoming connections.
|
194 | * @default false
|
195 | */
|
196 | pauseOnConnect?: boolean;
|
197 | }
|
198 |
|
199 | // https://github.com/nodejs/node/blob/master/lib/net.js
|
200 | class Server extends EventEmitter {
|
201 | constructor(connectionListener?: (socket: Socket) => void);
|
202 | constructor(options?: ServerOpts, connectionListener?: (socket: Socket) => void);
|
203 |
|
204 | listen(port?: number, hostname?: string, backlog?: number, listeningListener?: () => void): this;
|
205 | listen(port?: number, hostname?: string, listeningListener?: () => void): this;
|
206 | listen(port?: number, backlog?: number, listeningListener?: () => void): this;
|
207 | listen(port?: number, listeningListener?: () => void): this;
|
208 | listen(path: string, backlog?: number, listeningListener?: () => void): this;
|
209 | listen(path: string, listeningListener?: () => void): this;
|
210 | listen(options: ListenOptions, listeningListener?: () => void): this;
|
211 | listen(handle: any, backlog?: number, listeningListener?: () => void): this;
|
212 | listen(handle: any, listeningListener?: () => void): this;
|
213 | close(callback?: (err?: Error) => void): this;
|
214 | address(): AddressInfo | string | null;
|
215 | getConnections(cb: (error: Error | null, count: number) => void): void;
|
216 | ref(): this;
|
217 | unref(): this;
|
218 | maxConnections: number;
|
219 | connections: number;
|
220 | listening: boolean;
|
221 |
|
222 | /**
|
223 | * events.EventEmitter
|
224 | * 1. close
|
225 | * 2. connection
|
226 | * 3. error
|
227 | * 4. listening
|
228 | */
|
229 | 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 |
|
235 | 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 |
|
241 | 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 |
|
247 | 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 |
|
253 | 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 |
|
259 | 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;
|
264 | }
|
265 |
|
266 | type IPVersion = 'ipv4' | 'ipv6';
|
267 |
|
268 | class BlockList {
|
269 | /**
|
270 | * Adds a rule to block the given IP address.
|
271 | *
|
272 | * @param address An IPv4 or IPv6 address.
|
273 | * @param type Either 'ipv4' or 'ipv6'. Default: 'ipv4'.
|
274 | */
|
275 | addAddress(address: string, type?: IPVersion): void;
|
276 | addAddress(address: SocketAddress): void;
|
277 |
|
278 | /**
|
279 | * Adds a rule to block a range of IP addresses from start (inclusive) to end (inclusive).
|
280 | *
|
281 | * @param start The starting IPv4 or IPv6 address in the range.
|
282 | * @param end The ending IPv4 or IPv6 address in the range.
|
283 | * @param type Either 'ipv4' or 'ipv6'. Default: 'ipv4'.
|
284 | */
|
285 | addRange(start: string, end: string, type?: IPVersion): void;
|
286 | addRange(start: SocketAddress, end: SocketAddress): void;
|
287 |
|
288 | /**
|
289 | * Adds a rule to block a range of IP addresses specified as a subnet mask.
|
290 | *
|
291 | * @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'.
|
295 | */
|
296 | addSubnet(net: SocketAddress, prefix: number): void;
|
297 | addSubnet(net: string, prefix: number, type?: IPVersion): void;
|
298 |
|
299 | /**
|
300 | * Returns `true` if the given IP address matches any of the rules added to the `BlockList`.
|
301 | *
|
302 | * @param address The IP address to check
|
303 | * @param type Either 'ipv4' or 'ipv6'. Default: 'ipv4'.
|
304 | */
|
305 | check(address: SocketAddress): boolean;
|
306 | check(address: string, type?: IPVersion): boolean;
|
307 | }
|
308 |
|
309 | interface TcpNetConnectOpts extends TcpSocketConnectOpts, SocketConstructorOpts {
|
310 | timeout?: number;
|
311 | }
|
312 |
|
313 | interface IpcNetConnectOpts extends IpcSocketConnectOpts, SocketConstructorOpts {
|
314 | timeout?: number;
|
315 | }
|
316 |
|
317 | type NetConnectOpts = TcpNetConnectOpts | IpcNetConnectOpts;
|
318 |
|
319 | function createServer(connectionListener?: (socket: Socket) => void): Server;
|
320 | function createServer(options?: ServerOpts, connectionListener?: (socket: Socket) => void): Server;
|
321 | function connect(options: NetConnectOpts, connectionListener?: () => void): Socket;
|
322 | function connect(port: number, host?: string, connectionListener?: () => void): Socket;
|
323 | function connect(path: string, connectionListener?: () => void): Socket;
|
324 | function createConnection(options: NetConnectOpts, connectionListener?: () => void): Socket;
|
325 | function createConnection(port: number, host?: string, connectionListener?: () => void): Socket;
|
326 | function createConnection(path: string, connectionListener?: () => void): Socket;
|
327 | function isIP(input: string): number;
|
328 | function isIPv4(input: string): boolean;
|
329 | function isIPv6(input: string): boolean;
|
330 |
|
331 | interface SocketAddressInitOptions {
|
332 | /**
|
333 | * The network address as either an IPv4 or IPv6 string.
|
334 | * @default 127.0.0.1
|
335 | */
|
336 | address?: string;
|
337 | /**
|
338 | * @default `'ipv4'`
|
339 | */
|
340 | family?: IPVersion;
|
341 | /**
|
342 | * An IPv6 flow-label used only if `family` is `'ipv6'`.
|
343 | * @default 0
|
344 | */
|
345 | flowlabel?: number;
|
346 | /**
|
347 | * An IP port.
|
348 | * @default 0
|
349 | */
|
350 | port?: number;
|
351 | }
|
352 |
|
353 | // TODO: Mark as clonable if `kClone` symbol is set in node.
|
354 | /**
|
355 | * Immutable socket address.
|
356 | */
|
357 | class SocketAddress {
|
358 | constructor(options: SocketAddressInitOptions);
|
359 | readonly address: string;
|
360 | readonly family: IPVersion;
|
361 | readonly port: number;
|
362 | readonly flowlabel: number;
|
363 | }
|
364 | }
|
365 |
|
\ | No newline at end of file |