UNPKG

16.2 kBTypeScriptView Raw
1declare 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: "ready", listener: () => void): this;
119 addListener(event: "timeout", listener: () => void): this;
120
121 emit(event: string | symbol, ...args: any[]): boolean;
122 emit(event: "close", had_error: boolean): boolean;
123 emit(event: "connect"): boolean;
124 emit(event: "data", data: Buffer): boolean;
125 emit(event: "drain"): boolean;
126 emit(event: "end"): boolean;
127 emit(event: "error", err: Error): boolean;
128 emit(event: "lookup", err: Error, address: string, family: string | number, host: string): boolean;
129 emit(event: "ready"): boolean;
130 emit(event: "timeout"): boolean;
131
132 on(event: string, listener: (...args: any[]) => void): this;
133 on(event: "close", listener: (had_error: boolean) => void): this;
134 on(event: "connect", listener: () => void): this;
135 on(event: "data", listener: (data: Buffer) => void): this;
136 on(event: "drain", listener: () => void): this;
137 on(event: "end", listener: () => void): this;
138 on(event: "error", listener: (err: Error) => void): this;
139 on(event: "lookup", listener: (err: Error, address: string, family: string | number, host: string) => void): this;
140 on(event: "ready", listener: () => void): this;
141 on(event: "timeout", listener: () => void): this;
142
143 once(event: string, listener: (...args: any[]) => void): this;
144 once(event: "close", listener: (had_error: boolean) => void): this;
145 once(event: "connect", listener: () => void): this;
146 once(event: "data", listener: (data: Buffer) => void): this;
147 once(event: "drain", listener: () => void): this;
148 once(event: "end", listener: () => void): this;
149 once(event: "error", listener: (err: Error) => void): this;
150 once(event: "lookup", listener: (err: Error, address: string, family: string | number, host: string) => void): this;
151 once(event: "ready", listener: () => void): this;
152 once(event: "timeout", listener: () => void): this;
153
154 prependListener(event: string, listener: (...args: any[]) => void): this;
155 prependListener(event: "close", listener: (had_error: boolean) => void): this;
156 prependListener(event: "connect", listener: () => void): this;
157 prependListener(event: "data", listener: (data: Buffer) => void): this;
158 prependListener(event: "drain", listener: () => void): this;
159 prependListener(event: "end", listener: () => void): this;
160 prependListener(event: "error", listener: (err: Error) => void): this;
161 prependListener(event: "lookup", listener: (err: Error, address: string, family: string | number, host: string) => void): this;
162 prependListener(event: "ready", listener: () => void): this;
163 prependListener(event: "timeout", listener: () => void): this;
164
165 prependOnceListener(event: string, listener: (...args: any[]) => void): this;
166 prependOnceListener(event: "close", listener: (had_error: boolean) => void): this;
167 prependOnceListener(event: "connect", listener: () => void): this;
168 prependOnceListener(event: "data", listener: (data: Buffer) => void): this;
169 prependOnceListener(event: "drain", listener: () => void): this;
170 prependOnceListener(event: "end", listener: () => void): this;
171 prependOnceListener(event: "error", listener: (err: Error) => void): this;
172 prependOnceListener(event: "lookup", listener: (err: Error, address: string, family: string | number, host: string) => void): this;
173 prependOnceListener(event: "ready", listener: () => void): this;
174 prependOnceListener(event: "timeout", listener: () => void): this;
175 }
176
177 interface ListenOptions extends Abortable {
178 port?: number;
179 host?: string;
180 backlog?: number;
181 path?: string;
182 exclusive?: boolean;
183 readableAll?: boolean;
184 writableAll?: boolean;
185 /**
186 * @default false
187 */
188 ipv6Only?: boolean;
189 }
190
191 interface ServerOpts {
192 /**
193 * Indicates whether half-opened TCP connections are allowed.
194 * @default false
195 */
196 allowHalfOpen?: boolean;
197
198 /**
199 * Indicates whether the socket should be paused on incoming connections.
200 * @default false
201 */
202 pauseOnConnect?: boolean;
203 }
204
205 // https://github.com/nodejs/node/blob/master/lib/net.js
206 class Server extends EventEmitter {
207 constructor(connectionListener?: (socket: Socket) => void);
208 constructor(options?: ServerOpts, connectionListener?: (socket: Socket) => void);
209
210 listen(port?: number, hostname?: string, backlog?: number, listeningListener?: () => void): this;
211 listen(port?: number, hostname?: string, listeningListener?: () => void): this;
212 listen(port?: number, backlog?: number, listeningListener?: () => void): this;
213 listen(port?: number, listeningListener?: () => void): this;
214 listen(path: string, backlog?: number, listeningListener?: () => void): this;
215 listen(path: string, listeningListener?: () => void): this;
216 listen(options: ListenOptions, listeningListener?: () => void): this;
217 listen(handle: any, backlog?: number, listeningListener?: () => void): this;
218 listen(handle: any, listeningListener?: () => void): this;
219 close(callback?: (err?: Error) => void): this;
220 address(): AddressInfo | string | null;
221 getConnections(cb: (error: Error | null, count: number) => void): void;
222 ref(): this;
223 unref(): this;
224 maxConnections: number;
225 connections: number;
226 listening: boolean;
227
228 /**
229 * events.EventEmitter
230 * 1. close
231 * 2. connection
232 * 3. error
233 * 4. listening
234 */
235 addListener(event: string, listener: (...args: any[]) => void): this;
236 addListener(event: "close", listener: () => void): this;
237 addListener(event: "connection", listener: (socket: Socket) => void): this;
238 addListener(event: "error", listener: (err: Error) => void): this;
239 addListener(event: "listening", listener: () => void): this;
240
241 emit(event: string | symbol, ...args: any[]): boolean;
242 emit(event: "close"): boolean;
243 emit(event: "connection", socket: Socket): boolean;
244 emit(event: "error", err: Error): boolean;
245 emit(event: "listening"): boolean;
246
247 on(event: string, listener: (...args: any[]) => void): this;
248 on(event: "close", listener: () => void): this;
249 on(event: "connection", listener: (socket: Socket) => void): this;
250 on(event: "error", listener: (err: Error) => void): this;
251 on(event: "listening", listener: () => void): this;
252
253 once(event: string, listener: (...args: any[]) => void): this;
254 once(event: "close", listener: () => void): this;
255 once(event: "connection", listener: (socket: Socket) => void): this;
256 once(event: "error", listener: (err: Error) => void): this;
257 once(event: "listening", listener: () => void): this;
258
259 prependListener(event: string, listener: (...args: any[]) => void): this;
260 prependListener(event: "close", listener: () => void): this;
261 prependListener(event: "connection", listener: (socket: Socket) => void): this;
262 prependListener(event: "error", listener: (err: Error) => void): this;
263 prependListener(event: "listening", listener: () => void): this;
264
265 prependOnceListener(event: string, listener: (...args: any[]) => void): this;
266 prependOnceListener(event: "close", listener: () => void): this;
267 prependOnceListener(event: "connection", listener: (socket: Socket) => void): this;
268 prependOnceListener(event: "error", listener: (err: Error) => void): this;
269 prependOnceListener(event: "listening", listener: () => void): this;
270 }
271
272 type IPVersion = 'ipv4' | 'ipv6';
273
274 class BlockList {
275 /**
276 * Adds a rule to block the given IP address.
277 *
278 * @param address An IPv4 or IPv6 address.
279 * @param type Either 'ipv4' or 'ipv6'. Default: 'ipv4'.
280 */
281 addAddress(address: string, type?: IPVersion): void;
282 addAddress(address: SocketAddress): void;
283
284 /**
285 * Adds a rule to block a range of IP addresses from start (inclusive) to end (inclusive).
286 *
287 * @param start The starting IPv4 or IPv6 address in the range.
288 * @param end The ending IPv4 or IPv6 address in the range.
289 * @param type Either 'ipv4' or 'ipv6'. Default: 'ipv4'.
290 */
291 addRange(start: string, end: string, type?: IPVersion): void;
292 addRange(start: SocketAddress, end: SocketAddress): void;
293
294 /**
295 * Adds a rule to block a range of IP addresses specified as a subnet mask.
296 *
297 * @param net The network IPv4 or IPv6 address.
298 * @param prefix The number of CIDR prefix bits.
299 * For IPv4, this must be a value between 0 and 32. For IPv6, this must be between 0 and 128.
300 * @param type Either 'ipv4' or 'ipv6'. Default: 'ipv4'.
301 */
302 addSubnet(net: SocketAddress, prefix: number): void;
303 addSubnet(net: string, prefix: number, type?: IPVersion): void;
304
305 /**
306 * Returns `true` if the given IP address matches any of the rules added to the `BlockList`.
307 *
308 * @param address The IP address to check
309 * @param type Either 'ipv4' or 'ipv6'. Default: 'ipv4'.
310 */
311 check(address: SocketAddress): boolean;
312 check(address: string, type?: IPVersion): boolean;
313 }
314
315 interface TcpNetConnectOpts extends TcpSocketConnectOpts, SocketConstructorOpts {
316 timeout?: number;
317 }
318
319 interface IpcNetConnectOpts extends IpcSocketConnectOpts, SocketConstructorOpts {
320 timeout?: number;
321 }
322
323 type NetConnectOpts = TcpNetConnectOpts | IpcNetConnectOpts;
324
325 function createServer(connectionListener?: (socket: Socket) => void): Server;
326 function createServer(options?: ServerOpts, connectionListener?: (socket: Socket) => void): Server;
327 function connect(options: NetConnectOpts, connectionListener?: () => void): Socket;
328 function connect(port: number, host?: string, connectionListener?: () => void): Socket;
329 function connect(path: string, connectionListener?: () => void): Socket;
330 function createConnection(options: NetConnectOpts, connectionListener?: () => void): Socket;
331 function createConnection(port: number, host?: string, connectionListener?: () => void): Socket;
332 function createConnection(path: string, connectionListener?: () => void): Socket;
333 function isIP(input: string): number;
334 function isIPv4(input: string): boolean;
335 function isIPv6(input: string): boolean;
336
337 interface SocketAddressInitOptions {
338 /**
339 * The network address as either an IPv4 or IPv6 string.
340 * @default 127.0.0.1
341 */
342 address?: string;
343 /**
344 * @default `'ipv4'`
345 */
346 family?: IPVersion;
347 /**
348 * An IPv6 flow-label used only if `family` is `'ipv6'`.
349 * @default 0
350 */
351 flowlabel?: number;
352 /**
353 * An IP port.
354 * @default 0
355 */
356 port?: number;
357 }
358
359 // TODO: Mark as clonable if `kClone` symbol is set in node.
360 /**
361 * Immutable socket address.
362 */
363 class SocketAddress {
364 constructor(options: SocketAddressInitOptions);
365 readonly address: string;
366 readonly family: IPVersion;
367 readonly port: number;
368 readonly flowlabel: number;
369 }
370}
371
\No newline at end of file