UNPKG

13.3 kBTypeScriptView Raw
1declare module 'node:net' {
2 export * from 'net';
3}
4
5declare module 'net' {
6 import * as stream from 'node:stream';
7 import EventEmitter = require('node:events');
8 import * as dns from 'node:dns';
9
10 type LookupFunction = (
11 hostname: string,
12 options: dns.LookupOneOptions,
13 callback: (err: NodeJS.ErrnoException | null, address: string, family: number) => void,
14 ) => void;
15
16 interface AddressInfo {
17 address: string;
18 family: string;
19 port: number;
20 }
21
22 interface SocketConstructorOpts {
23 fd?: number;
24 allowHalfOpen?: boolean;
25 readable?: boolean;
26 writable?: boolean;
27 }
28
29 interface OnReadOpts {
30 buffer: Uint8Array | (() => Uint8Array);
31 /**
32 * This function is called for every chunk of incoming data.
33 * Two arguments are passed to it: the number of bytes written to buffer and a reference to buffer.
34 * Return false from this function to implicitly pause() the socket.
35 */
36 callback(bytesWritten: number, buf: Uint8Array): boolean;
37 }
38
39 interface ConnectOpts {
40 /**
41 * If specified, incoming data is stored in a single buffer and passed to the supplied callback when data arrives on the socket.
42 * Note: this will cause the streaming functionality to not provide any data, however events like 'error', 'end', and 'close' will
43 * still be emitted as normal and methods like pause() and resume() will also behave as expected.
44 */
45 onread?: OnReadOpts;
46 }
47
48 interface TcpSocketConnectOpts extends ConnectOpts {
49 port: number;
50 host?: string;
51 localAddress?: string;
52 localPort?: number;
53 hints?: number;
54 family?: number;
55 lookup?: LookupFunction;
56 }
57
58 interface IpcSocketConnectOpts extends ConnectOpts {
59 path: string;
60 }
61
62 type SocketConnectOpts = TcpSocketConnectOpts | IpcSocketConnectOpts;
63
64 class Socket extends stream.Duplex {
65 constructor(options?: SocketConstructorOpts);
66
67 // Extended base methods
68 write(buffer: Uint8Array | string, cb?: (err?: Error) => void): boolean;
69 write(str: Uint8Array | string, encoding?: BufferEncoding, cb?: (err?: Error) => void): boolean;
70
71 connect(options: SocketConnectOpts, connectionListener?: () => void): this;
72 connect(port: number, host: string, connectionListener?: () => void): this;
73 connect(port: number, connectionListener?: () => void): this;
74 connect(path: string, connectionListener?: () => void): this;
75
76 setEncoding(encoding?: BufferEncoding): this;
77 pause(): this;
78 resume(): this;
79 setTimeout(timeout: number, callback?: () => void): this;
80 setNoDelay(noDelay?: boolean): this;
81 setKeepAlive(enable?: boolean, initialDelay?: number): this;
82 address(): AddressInfo | {};
83 unref(): this;
84 ref(): this;
85
86 /** @deprecated since v14.6.0 - Use `writableLength` instead. */
87 readonly bufferSize: number;
88 readonly bytesRead: number;
89 readonly bytesWritten: number;
90 readonly connecting: boolean;
91 readonly destroyed: boolean;
92 readonly localAddress: string;
93 readonly localPort: number;
94 readonly remoteAddress?: string;
95 readonly remoteFamily?: string;
96 readonly remotePort?: number;
97
98 // Extended base methods
99 end(cb?: () => void): void;
100 end(buffer: Uint8Array | string, cb?: () => void): void;
101 end(str: Uint8Array | string, encoding?: BufferEncoding, cb?: () => void): void;
102
103 /**
104 * events.EventEmitter
105 * 1. close
106 * 2. connect
107 * 3. data
108 * 4. drain
109 * 5. end
110 * 6. error
111 * 7. lookup
112 * 8. timeout
113 */
114 addListener(event: string, listener: (...args: any[]) => void): this;
115 addListener(event: "close", listener: (had_error: boolean) => void): this;
116 addListener(event: "connect", listener: () => void): this;
117 addListener(event: "data", listener: (data: Buffer) => void): this;
118 addListener(event: "drain", listener: () => void): this;
119 addListener(event: "end", listener: () => void): this;
120 addListener(event: "error", listener: (err: Error) => void): this;
121 addListener(event: "lookup", listener: (err: Error, address: string, family: string | number, host: string) => void): this;
122 addListener(event: "timeout", listener: () => void): this;
123
124 emit(event: string | symbol, ...args: any[]): boolean;
125 emit(event: "close", had_error: boolean): boolean;
126 emit(event: "connect"): boolean;
127 emit(event: "data", data: Buffer): boolean;
128 emit(event: "drain"): boolean;
129 emit(event: "end"): boolean;
130 emit(event: "error", err: Error): boolean;
131 emit(event: "lookup", err: Error, address: string, family: string | number, host: string): boolean;
132 emit(event: "timeout"): boolean;
133
134 on(event: string, listener: (...args: any[]) => void): this;
135 on(event: "close", listener: (had_error: boolean) => void): this;
136 on(event: "connect", listener: () => void): this;
137 on(event: "data", listener: (data: Buffer) => void): this;
138 on(event: "drain", listener: () => void): this;
139 on(event: "end", listener: () => void): this;
140 on(event: "error", listener: (err: Error) => void): this;
141 on(event: "lookup", listener: (err: Error, address: string, family: string | number, host: string) => void): this;
142 on(event: "timeout", listener: () => void): this;
143
144 once(event: string, listener: (...args: any[]) => void): this;
145 once(event: "close", listener: (had_error: boolean) => void): this;
146 once(event: "connect", listener: () => void): this;
147 once(event: "data", listener: (data: Buffer) => void): this;
148 once(event: "drain", listener: () => void): this;
149 once(event: "end", listener: () => void): this;
150 once(event: "error", listener: (err: Error) => void): this;
151 once(event: "lookup", listener: (err: Error, address: string, family: string | number, host: string) => 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: "timeout", listener: () => void): this;
163
164 prependOnceListener(event: string, listener: (...args: any[]) => void): this;
165 prependOnceListener(event: "close", listener: (had_error: boolean) => void): this;
166 prependOnceListener(event: "connect", listener: () => void): this;
167 prependOnceListener(event: "data", listener: (data: Buffer) => void): this;
168 prependOnceListener(event: "drain", listener: () => void): this;
169 prependOnceListener(event: "end", listener: () => void): this;
170 prependOnceListener(event: "error", listener: (err: Error) => void): this;
171 prependOnceListener(event: "lookup", listener: (err: Error, address: string, family: string | number, host: string) => void): this;
172 prependOnceListener(event: "timeout", listener: () => void): this;
173 }
174
175 interface ListenOptions {
176 port?: number;
177 host?: string;
178 backlog?: number;
179 path?: string;
180 exclusive?: boolean;
181 readableAll?: boolean;
182 writableAll?: boolean;
183 /**
184 * @default false
185 */
186 ipv6Only?: boolean;
187 }
188
189 interface ServerOpts {
190 /**
191 * Indicates whether half-opened TCP connections are allowed. __Default:__ `false`.
192 */
193 allowHalfOpen?: boolean;
194
195 /**
196 * Indicates whether the socket should be paused on incoming connections. __Default:__ `false`.
197 */
198 pauseOnConnect?: boolean;
199 }
200
201 // https://github.com/nodejs/node/blob/master/lib/net.js
202 class Server extends EventEmitter {
203 constructor(connectionListener?: (socket: Socket) => void);
204 constructor(options?: ServerOpts, connectionListener?: (socket: Socket) => void);
205
206 listen(port?: number, hostname?: string, backlog?: number, listeningListener?: () => void): this;
207 listen(port?: number, hostname?: string, listeningListener?: () => void): this;
208 listen(port?: number, backlog?: number, listeningListener?: () => void): this;
209 listen(port?: number, listeningListener?: () => void): this;
210 listen(path: string, backlog?: number, listeningListener?: () => void): this;
211 listen(path: string, listeningListener?: () => void): this;
212 listen(options: ListenOptions, listeningListener?: () => void): this;
213 listen(handle: any, backlog?: number, listeningListener?: () => void): this;
214 listen(handle: any, listeningListener?: () => void): this;
215 close(callback?: (err?: Error) => void): this;
216 address(): AddressInfo | string | null;
217 getConnections(cb: (error: Error | null, count: number) => void): void;
218 ref(): this;
219 unref(): this;
220 maxConnections: number;
221 connections: number;
222 listening: boolean;
223
224 /**
225 * events.EventEmitter
226 * 1. close
227 * 2. connection
228 * 3. error
229 * 4. listening
230 */
231 addListener(event: string, listener: (...args: any[]) => void): this;
232 addListener(event: "close", listener: () => void): this;
233 addListener(event: "connection", listener: (socket: Socket) => void): this;
234 addListener(event: "error", listener: (err: Error) => void): this;
235 addListener(event: "listening", listener: () => void): this;
236
237 emit(event: string | symbol, ...args: any[]): boolean;
238 emit(event: "close"): boolean;
239 emit(event: "connection", socket: Socket): boolean;
240 emit(event: "error", err: Error): boolean;
241 emit(event: "listening"): boolean;
242
243 on(event: string, listener: (...args: any[]) => void): this;
244 on(event: "close", listener: () => void): this;
245 on(event: "connection", listener: (socket: Socket) => void): this;
246 on(event: "error", listener: (err: Error) => void): this;
247 on(event: "listening", listener: () => void): this;
248
249 once(event: string, listener: (...args: any[]) => void): this;
250 once(event: "close", listener: () => void): this;
251 once(event: "connection", listener: (socket: Socket) => void): this;
252 once(event: "error", listener: (err: Error) => void): this;
253 once(event: "listening", listener: () => void): this;
254
255 prependListener(event: string, listener: (...args: any[]) => void): this;
256 prependListener(event: "close", listener: () => void): this;
257 prependListener(event: "connection", listener: (socket: Socket) => void): this;
258 prependListener(event: "error", listener: (err: Error) => void): this;
259 prependListener(event: "listening", listener: () => void): this;
260
261 prependOnceListener(event: string, listener: (...args: any[]) => void): this;
262 prependOnceListener(event: "close", listener: () => void): this;
263 prependOnceListener(event: "connection", listener: (socket: Socket) => void): this;
264 prependOnceListener(event: "error", listener: (err: Error) => void): this;
265 prependOnceListener(event: "listening", listener: () => void): this;
266 }
267
268 interface TcpNetConnectOpts extends TcpSocketConnectOpts, SocketConstructorOpts {
269 timeout?: number;
270 }
271
272 interface IpcNetConnectOpts extends IpcSocketConnectOpts, SocketConstructorOpts {
273 timeout?: number;
274 }
275
276 type NetConnectOpts = TcpNetConnectOpts | IpcNetConnectOpts;
277
278 function createServer(connectionListener?: (socket: Socket) => void): Server;
279 function createServer(options?: ServerOpts, connectionListener?: (socket: Socket) => void): Server;
280 function connect(options: NetConnectOpts, connectionListener?: () => void): Socket;
281 function connect(port: number, host?: string, connectionListener?: () => void): Socket;
282 function connect(path: string, connectionListener?: () => void): Socket;
283 function createConnection(options: NetConnectOpts, connectionListener?: () => void): Socket;
284 function createConnection(port: number, host?: string, connectionListener?: () => void): Socket;
285 function createConnection(path: string, connectionListener?: () => void): Socket;
286 function isIP(input: string): number;
287 function isIPv4(input: string): boolean;
288 function isIPv6(input: string): boolean;
289}
290
\No newline at end of file