[6a3a178] | 1 | declare module 'net' {
|
---|
| 2 | import * as stream from 'stream';
|
---|
| 3 | import EventEmitter = require('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 | undefined;
|
---|
| 20 | allowHalfOpen?: boolean | undefined;
|
---|
| 21 | readable?: boolean | undefined;
|
---|
| 22 | writable?: boolean | undefined;
|
---|
| 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 | undefined;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | interface TcpSocketConnectOpts extends ConnectOpts {
|
---|
| 45 | port: number;
|
---|
| 46 | host?: string | undefined;
|
---|
| 47 | localAddress?: string | undefined;
|
---|
| 48 | localPort?: number | undefined;
|
---|
| 49 | hints?: number | undefined;
|
---|
| 50 | family?: number | undefined;
|
---|
| 51 | lookup?: LookupFunction | undefined;
|
---|
| 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?: string, 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?: string): 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 | string;
|
---|
| 79 | unref(): void;
|
---|
| 80 | ref(): void;
|
---|
| 81 |
|
---|
| 82 | readonly bufferSize: number;
|
---|
| 83 | readonly bytesRead: number;
|
---|
| 84 | readonly bytesWritten: number;
|
---|
| 85 | readonly connecting: boolean;
|
---|
| 86 | readonly destroyed: boolean;
|
---|
| 87 | readonly localAddress: string;
|
---|
| 88 | readonly localPort: number;
|
---|
| 89 | readonly remoteAddress?: string | undefined;
|
---|
| 90 | readonly remoteFamily?: string | undefined;
|
---|
| 91 | readonly remotePort?: number | undefined;
|
---|
| 92 |
|
---|
| 93 | // Extended base methods
|
---|
| 94 | end(cb?: () => void): void;
|
---|
| 95 | end(buffer: Uint8Array | string, cb?: () => void): void;
|
---|
| 96 | end(str: Uint8Array | string, encoding?: string, cb?: () => void): void;
|
---|
| 97 |
|
---|
| 98 | /**
|
---|
| 99 | * events.EventEmitter
|
---|
| 100 | * 1. close
|
---|
| 101 | * 2. connect
|
---|
| 102 | * 3. data
|
---|
| 103 | * 4. drain
|
---|
| 104 | * 5. end
|
---|
| 105 | * 6. error
|
---|
| 106 | * 7. lookup
|
---|
| 107 | * 8. timeout
|
---|
| 108 | */
|
---|
| 109 | addListener(event: string, listener: (...args: any[]) => void): this;
|
---|
| 110 | addListener(event: "close", listener: (had_error: boolean) => void): this;
|
---|
| 111 | addListener(event: "connect", listener: () => void): this;
|
---|
| 112 | addListener(event: "data", listener: (data: Buffer) => void): this;
|
---|
| 113 | addListener(event: "drain", listener: () => void): this;
|
---|
| 114 | addListener(event: "end", listener: () => void): this;
|
---|
| 115 | addListener(event: "error", listener: (err: Error) => void): this;
|
---|
| 116 | addListener(event: "lookup", listener: (err: Error, address: string, family: string | number, host: string) => void): this;
|
---|
| 117 | addListener(event: "ready", listener: () => 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: "ready"): boolean;
|
---|
| 129 | emit(event: "timeout"): boolean;
|
---|
| 130 |
|
---|
| 131 | on(event: string, listener: (...args: any[]) => void): this;
|
---|
| 132 | on(event: "close", listener: (had_error: boolean) => void): this;
|
---|
| 133 | on(event: "connect", listener: () => void): this;
|
---|
| 134 | on(event: "data", listener: (data: Buffer) => void): this;
|
---|
| 135 | on(event: "drain", listener: () => void): this;
|
---|
| 136 | on(event: "end", listener: () => void): this;
|
---|
| 137 | on(event: "error", listener: (err: Error) => void): this;
|
---|
| 138 | on(event: "lookup", listener: (err: Error, address: string, family: string | number, host: string) => void): this;
|
---|
| 139 | on(event: "ready", listener: () => void): this;
|
---|
| 140 | on(event: "timeout", listener: () => void): this;
|
---|
| 141 |
|
---|
| 142 | once(event: string, listener: (...args: any[]) => void): this;
|
---|
| 143 | once(event: "close", listener: (had_error: boolean) => void): this;
|
---|
| 144 | once(event: "connect", listener: () => void): this;
|
---|
| 145 | once(event: "data", listener: (data: Buffer) => void): this;
|
---|
| 146 | once(event: "drain", listener: () => void): this;
|
---|
| 147 | once(event: "end", listener: () => void): this;
|
---|
| 148 | once(event: "error", listener: (err: Error) => void): this;
|
---|
| 149 | once(event: "lookup", listener: (err: Error, address: string, family: string | number, host: string) => void): this;
|
---|
| 150 | once(event: "ready", listener: () => void): this;
|
---|
| 151 | once(event: "timeout", listener: () => void): this;
|
---|
| 152 |
|
---|
| 153 | prependListener(event: string, listener: (...args: any[]) => void): this;
|
---|
| 154 | prependListener(event: "close", listener: (had_error: boolean) => void): this;
|
---|
| 155 | prependListener(event: "connect", listener: () => void): this;
|
---|
| 156 | prependListener(event: "data", listener: (data: Buffer) => void): this;
|
---|
| 157 | prependListener(event: "drain", listener: () => void): this;
|
---|
| 158 | prependListener(event: "end", listener: () => void): this;
|
---|
| 159 | prependListener(event: "error", listener: (err: Error) => void): this;
|
---|
| 160 | prependListener(event: "lookup", listener: (err: Error, address: string, family: string | number, host: string) => void): this;
|
---|
| 161 | prependListener(event: "ready", listener: () => 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: "ready", listener: () => void): this;
|
---|
| 173 | prependOnceListener(event: "timeout", listener: () => void): this;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | interface ListenOptions {
|
---|
| 177 | port?: number | undefined;
|
---|
| 178 | host?: string | undefined;
|
---|
| 179 | backlog?: number | undefined;
|
---|
| 180 | path?: string | undefined;
|
---|
| 181 | exclusive?: boolean | undefined;
|
---|
| 182 | readableAll?: boolean | undefined;
|
---|
| 183 | writableAll?: boolean | undefined;
|
---|
| 184 | /**
|
---|
| 185 | * @default false
|
---|
| 186 | */
|
---|
| 187 | ipv6Only?: boolean | undefined;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | // https://github.com/nodejs/node/blob/master/lib/net.js
|
---|
| 191 | class Server extends EventEmitter {
|
---|
| 192 | constructor(connectionListener?: (socket: Socket) => void);
|
---|
| 193 | constructor(options?: { allowHalfOpen?: boolean | undefined, pauseOnConnect?: boolean | undefined }, connectionListener?: (socket: Socket) => void);
|
---|
| 194 |
|
---|
| 195 | listen(port?: number, hostname?: string, backlog?: number, listeningListener?: () => void): this;
|
---|
| 196 | listen(port?: number, hostname?: string, listeningListener?: () => void): this;
|
---|
| 197 | listen(port?: number, backlog?: number, listeningListener?: () => void): this;
|
---|
| 198 | listen(port?: number, listeningListener?: () => void): this;
|
---|
| 199 | listen(path: string, backlog?: number, listeningListener?: () => void): this;
|
---|
| 200 | listen(path: string, listeningListener?: () => void): this;
|
---|
| 201 | listen(options: ListenOptions, listeningListener?: () => void): this;
|
---|
| 202 | listen(handle: any, backlog?: number, listeningListener?: () => void): this;
|
---|
| 203 | listen(handle: any, listeningListener?: () => void): this;
|
---|
| 204 | close(callback?: (err?: Error) => void): this;
|
---|
| 205 | address(): AddressInfo | string | null;
|
---|
| 206 | getConnections(cb: (error: Error | null, count: number) => void): void;
|
---|
| 207 | ref(): this;
|
---|
| 208 | unref(): this;
|
---|
| 209 | maxConnections: number;
|
---|
| 210 | connections: number;
|
---|
| 211 | listening: boolean;
|
---|
| 212 |
|
---|
| 213 | /**
|
---|
| 214 | * events.EventEmitter
|
---|
| 215 | * 1. close
|
---|
| 216 | * 2. connection
|
---|
| 217 | * 3. error
|
---|
| 218 | * 4. listening
|
---|
| 219 | */
|
---|
| 220 | addListener(event: string, listener: (...args: any[]) => void): this;
|
---|
| 221 | addListener(event: "close", listener: () => void): this;
|
---|
| 222 | addListener(event: "connection", listener: (socket: Socket) => void): this;
|
---|
| 223 | addListener(event: "error", listener: (err: Error) => void): this;
|
---|
| 224 | addListener(event: "listening", listener: () => void): this;
|
---|
| 225 |
|
---|
| 226 | emit(event: string | symbol, ...args: any[]): boolean;
|
---|
| 227 | emit(event: "close"): boolean;
|
---|
| 228 | emit(event: "connection", socket: Socket): boolean;
|
---|
| 229 | emit(event: "error", err: Error): boolean;
|
---|
| 230 | emit(event: "listening"): boolean;
|
---|
| 231 |
|
---|
| 232 | on(event: string, listener: (...args: any[]) => void): this;
|
---|
| 233 | on(event: "close", listener: () => void): this;
|
---|
| 234 | on(event: "connection", listener: (socket: Socket) => void): this;
|
---|
| 235 | on(event: "error", listener: (err: Error) => void): this;
|
---|
| 236 | on(event: "listening", listener: () => void): this;
|
---|
| 237 |
|
---|
| 238 | once(event: string, listener: (...args: any[]) => void): this;
|
---|
| 239 | once(event: "close", listener: () => void): this;
|
---|
| 240 | once(event: "connection", listener: (socket: Socket) => void): this;
|
---|
| 241 | once(event: "error", listener: (err: Error) => void): this;
|
---|
| 242 | once(event: "listening", listener: () => void): this;
|
---|
| 243 |
|
---|
| 244 | prependListener(event: string, listener: (...args: any[]) => void): this;
|
---|
| 245 | prependListener(event: "close", listener: () => void): this;
|
---|
| 246 | prependListener(event: "connection", listener: (socket: Socket) => void): this;
|
---|
| 247 | prependListener(event: "error", listener: (err: Error) => void): this;
|
---|
| 248 | prependListener(event: "listening", listener: () => void): this;
|
---|
| 249 |
|
---|
| 250 | prependOnceListener(event: string, listener: (...args: any[]) => void): this;
|
---|
| 251 | prependOnceListener(event: "close", listener: () => void): this;
|
---|
| 252 | prependOnceListener(event: "connection", listener: (socket: Socket) => void): this;
|
---|
| 253 | prependOnceListener(event: "error", listener: (err: Error) => void): this;
|
---|
| 254 | prependOnceListener(event: "listening", listener: () => void): this;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | interface TcpNetConnectOpts extends TcpSocketConnectOpts, SocketConstructorOpts {
|
---|
| 258 | timeout?: number | undefined;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | interface IpcNetConnectOpts extends IpcSocketConnectOpts, SocketConstructorOpts {
|
---|
| 262 | timeout?: number | undefined;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | type NetConnectOpts = TcpNetConnectOpts | IpcNetConnectOpts;
|
---|
| 266 |
|
---|
| 267 | function createServer(connectionListener?: (socket: Socket) => void): Server;
|
---|
| 268 | function createServer(options?: { allowHalfOpen?: boolean | undefined, pauseOnConnect?: boolean | undefined }, connectionListener?: (socket: Socket) => void): Server;
|
---|
| 269 | function connect(options: NetConnectOpts, connectionListener?: () => void): Socket;
|
---|
| 270 | function connect(port: number, host?: string, connectionListener?: () => void): Socket;
|
---|
| 271 | function connect(path: string, connectionListener?: () => void): Socket;
|
---|
| 272 | function createConnection(options: NetConnectOpts, connectionListener?: () => void): Socket;
|
---|
| 273 | function createConnection(port: number, host?: string, connectionListener?: () => void): Socket;
|
---|
| 274 | function createConnection(path: string, connectionListener?: () => void): Socket;
|
---|
| 275 | function isIP(input: string): number;
|
---|
| 276 | function isIPv4(input: string): boolean;
|
---|
| 277 | function isIPv6(input: string): boolean;
|
---|
| 278 | }
|
---|