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