mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-07 17:17:23 +08:00
fix Node net.createConnection() and Socket#connect() signatures. (#19456)
* fix net.createConnection() and Socket#connect() signatures. * fix lint errors. * Fix review comments.
This commit is contained in:
47
types/node/index.d.ts
vendored
47
types/node/index.d.ts
vendored
@@ -2401,6 +2401,30 @@ declare module "dns" {
|
||||
declare module "net" {
|
||||
import * as stream from "stream";
|
||||
import * as events from "events";
|
||||
import * as dns from "dns";
|
||||
|
||||
export interface SocketConstructorOpts {
|
||||
fd?: number;
|
||||
allowHalfOpen?: boolean;
|
||||
readable?: boolean;
|
||||
writable?: boolean;
|
||||
}
|
||||
|
||||
export interface TcpSocketConnectOpts {
|
||||
port: number;
|
||||
host?: string;
|
||||
localAddress?: string;
|
||||
localPort?: number;
|
||||
hints?: number;
|
||||
family?: number;
|
||||
lookup?: (hostname: string, options: dns.LookupOneOptions, callback: (err: NodeJS.ErrnoException, address: string, family: number) => void) => void;
|
||||
}
|
||||
|
||||
export interface IpcSocketConnectOpts {
|
||||
path: string;
|
||||
}
|
||||
|
||||
export type SocketConnectOpts = TcpSocketConnectOpts | IpcSocketConnectOpts;
|
||||
|
||||
export interface Socket extends stream.Duplex {
|
||||
// Extended base methods
|
||||
@@ -2411,8 +2435,10 @@ declare module "net" {
|
||||
write(str: string, encoding?: string, fd?: string): boolean;
|
||||
write(data: any, encoding?: string, callback?: Function): void;
|
||||
|
||||
connect(port: number, host?: string, connectionListener?: Function): void;
|
||||
connect(path: string, connectionListener?: Function): void;
|
||||
connect(options: SocketConnectOpts, connectionListener?: Function): this;
|
||||
connect(port: number, host: string, connectionListener?: Function): this;
|
||||
connect(port: number, connectionListener?: Function): this;
|
||||
connect(path: string, connectionListener?: Function): this;
|
||||
bufferSize: number;
|
||||
setEncoding(encoding?: string): this;
|
||||
destroy(err?: any): void;
|
||||
@@ -2515,7 +2541,7 @@ declare module "net" {
|
||||
}
|
||||
|
||||
export var Socket: {
|
||||
new(options?: { fd?: number; allowHalfOpen?: boolean; readable?: boolean; writable?: boolean; }): Socket;
|
||||
new(options?: SocketConstructorOpts): Socket;
|
||||
};
|
||||
|
||||
export interface ListenOptions {
|
||||
@@ -2592,12 +2618,23 @@ declare module "net" {
|
||||
prependOnceListener(event: "error", listener: (err: Error) => void): this;
|
||||
prependOnceListener(event: "listening", listener: () => void): this;
|
||||
}
|
||||
|
||||
export interface TcpNetConnectOpts extends TcpSocketConnectOpts, SocketConstructorOpts {
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
export interface IpcNetConnectOpts extends IpcSocketConnectOpts, SocketConstructorOpts {
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
export type NetConnectOpts = TcpNetConnectOpts | IpcNetConnectOpts;
|
||||
|
||||
export function createServer(connectionListener?: (socket: Socket) => void): Server;
|
||||
export function createServer(options?: { allowHalfOpen?: boolean, pauseOnConnect?: boolean }, connectionListener?: (socket: Socket) => void): Server;
|
||||
export function connect(options: { port: number, host?: string, localAddress?: string, localPort?: number, family?: number, allowHalfOpen?: boolean; }, connectionListener?: Function): Socket;
|
||||
export function connect(options: NetConnectOpts, connectionListener?: Function): Socket;
|
||||
export function connect(port: number, host?: string, connectionListener?: Function): Socket;
|
||||
export function connect(path: string, connectionListener?: Function): Socket;
|
||||
export function createConnection(options: { port: number, host?: string, localAddress?: string, localPort?: string, family?: number, allowHalfOpen?: boolean; }, connectionListener?: Function): Socket;
|
||||
export function createConnection(options: NetConnectOpts, connectionListener?: Function): Socket;
|
||||
export function createConnection(port: number, host?: string, connectionListener?: Function): Socket;
|
||||
export function createConnection(path: string, connectionListener?: Function): Socket;
|
||||
export function isIP(input: string): number;
|
||||
|
||||
@@ -2355,6 +2355,19 @@ namespace console_tests {
|
||||
///////////////////////////////////////////////////
|
||||
|
||||
namespace net_tests {
|
||||
{
|
||||
const connectOpts: net.NetConnectOpts = {
|
||||
allowHalfOpen: true,
|
||||
family: 4,
|
||||
host: "localhost",
|
||||
port: 443,
|
||||
timeout: 10E3
|
||||
};
|
||||
const socket: net.Socket = net.createConnection(connectOpts, (): void => {
|
||||
// nothing
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
let server = net.createServer();
|
||||
// Check methods which return server instances by chaining calls
|
||||
@@ -2375,6 +2388,13 @@ namespace net_tests {
|
||||
}
|
||||
|
||||
{
|
||||
const constructorOpts: net.SocketConstructorOpts = {
|
||||
fd: 1,
|
||||
allowHalfOpen: false,
|
||||
readable: false,
|
||||
writable: false
|
||||
};
|
||||
|
||||
/**
|
||||
* net.Socket - events.EventEmitter
|
||||
* 1. close
|
||||
@@ -2386,12 +2406,7 @@ namespace net_tests {
|
||||
* 7. lookup
|
||||
* 8. timeout
|
||||
*/
|
||||
let _socket: net.Socket = new net.Socket({
|
||||
fd: 1,
|
||||
allowHalfOpen: false,
|
||||
readable: false,
|
||||
writable: false
|
||||
});
|
||||
let _socket: net.Socket = new net.Socket(constructorOpts);
|
||||
|
||||
let bool: boolean;
|
||||
let buffer: Buffer;
|
||||
@@ -2399,6 +2414,29 @@ namespace net_tests {
|
||||
let str: string;
|
||||
let num: number;
|
||||
|
||||
let ipcConnectOpts: net.IpcSocketConnectOpts = {
|
||||
path: "/"
|
||||
};
|
||||
let tcpConnectOpts: net.TcpSocketConnectOpts = {
|
||||
family: 4,
|
||||
hints: 0,
|
||||
host: "localhost",
|
||||
localAddress: "10.0.0.1",
|
||||
localPort: 1234,
|
||||
lookup: (_hostname: string, _options: dns.LookupOneOptions, _callback: (err: NodeJS.ErrnoException, address: string, family: number) => void): void => {
|
||||
// nothing
|
||||
},
|
||||
port: 80
|
||||
};
|
||||
_socket = _socket.connect(ipcConnectOpts);
|
||||
_socket = _socket.connect(ipcConnectOpts, (): void => {});
|
||||
_socket = _socket.connect(tcpConnectOpts);
|
||||
_socket = _socket.connect(tcpConnectOpts, (): void => {});
|
||||
_socket = _socket.connect(80, "localhost");
|
||||
_socket = _socket.connect(80, "localhost", (): void => {});
|
||||
_socket = _socket.connect(80);
|
||||
_socket = _socket.connect(80, (): void => {});
|
||||
|
||||
/// addListener
|
||||
|
||||
_socket = _socket.addListener("close", had_error => {
|
||||
|
||||
Reference in New Issue
Block a user