From eba2b89b3c2cb836ef4342cd5f30da9968e9ff93 Mon Sep 17 00:00:00 2001 From: Flarna Date: Mon, 23 Oct 2017 22:17:14 +0200 Subject: [PATCH] [node] Some fixes for net and https (#20662) Sync Lookup function type in net, tls and dgram. --- types/node/index.d.ts | 113 +++++++++++++++------------------------ types/node/node-tests.ts | 40 ++++++++------ 2 files changed, 69 insertions(+), 84 deletions(-) diff --git a/types/node/index.d.ts b/types/node/index.d.ts index 5d0ba05b93..fcaa3aed4a 100644 --- a/types/node/index.d.ts +++ b/types/node/index.d.ts @@ -869,10 +869,10 @@ declare module "http" { export class Server extends net.Server { constructor(requestListener?: (req: IncomingMessage, res: ServerResponse) => void); - setTimeout(msecs: number, callback: () => void): this; + setTimeout(msecs?: number, callback?: () => void): this; + setTimeout(callback: () => void): this; maxHeadersCount: number; timeout: number; - listening: boolean; keepAliveTimeout: number; } /** @@ -1604,7 +1604,7 @@ declare module "https" { requestCert?: boolean; rejectUnauthorized?: boolean; NPNProtocols?: any; - SNICallback?: (servername: string, cb: (err: Error, ctx: tls.SecureContext) => any) => any; + SNICallback?: (servername: string, cb: (err: Error | null, ctx: tls.SecureContext) => void) => void; } export interface RequestOptions extends http.RequestOptions { @@ -1616,16 +1616,9 @@ declare module "https" { ciphers?: string; rejectUnauthorized?: boolean; secureProtocol?: string; + servername?: string; } - export interface ClientRequest extends http.ClientRequest { } - - export interface IncomingMessage extends http.IncomingMessage { } - - export interface ServerResponse extends http.ServerResponse { } - - export interface Agent extends http.Agent { } - export interface AgentOptions extends http.AgentOptions { pfx?: any; key?: any; @@ -1634,17 +1627,25 @@ declare module "https" { ca?: any; ciphers?: string; rejectUnauthorized?: boolean; + serverName?: string; secureProtocol?: string; maxCachedSessions?: number; } - export var Agent: { - new(options?: AgentOptions): Agent; - }; - export interface Server extends tls.Server { } - export function createServer(options: ServerOptions, requestListener?: (req: IncomingMessage, res: ServerResponse) => void): Server; - export function request(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest; - export function get(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest; + export class Agent extends http.Agent { + constructor(options?: AgentOptions); + } + + export class Server extends tls.Server { + setTimeout(callback: () => void): this; + setTimeout(msecs?: number, callback?: () => void): this; + timeout: number; + keepAliveTimeout: number; + } + + export function createServer(options: ServerOptions, requestListener?: (req: http.IncomingMessage, res: http.ServerResponse) => void): Server; + export function request(options: RequestOptions | string | URL, callback?: (res: http.IncomingMessage) => void): http.ClientRequest; + export function get(options: RequestOptions | string | URL, callback?: (res: http.IncomingMessage) => void): http.ClientRequest; export var globalAgent: Agent; } @@ -2410,6 +2411,8 @@ declare module "net" { import * as events from "events"; import * as dns from "dns"; + type LookupFunction = (hostname: string, options: dns.LookupOneOptions, callback: (err: NodeJS.ErrnoException | null, address: string, family: number) => void) => void; + export interface SocketConstructorOpts { fd?: number; allowHalfOpen?: boolean; @@ -2424,7 +2427,7 @@ declare module "net" { localPort?: number; hints?: number; family?: number; - lookup?: (hostname: string, options: dns.LookupOneOptions, callback: (err: NodeJS.ErrnoException, address: string, family: number) => void) => void; + lookup?: LookupFunction; } export interface IpcSocketConnectOpts { @@ -2434,7 +2437,7 @@ declare module "net" { export type SocketConnectOpts = TcpSocketConnectOpts | IpcSocketConnectOpts; export class Socket extends stream.Duplex { - constructor(options?: { fd?: number; allowHalfOpen?: boolean; readable?: boolean; writable?: boolean; }); + constructor(options?: SocketConstructorOpts); // Extended base methods write(buffer: Buffer): boolean; @@ -2448,6 +2451,7 @@ declare module "net" { 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; @@ -2562,20 +2566,20 @@ declare module "net" { constructor(connectionListener?: (socket: Socket) => void); constructor(options?: { allowHalfOpen?: boolean, pauseOnConnect?: boolean }, connectionListener?: (socket: Socket) => void); - listen(port?: number, hostname?: string, backlog?: number, listeningListener?: Function): Server; - listen(port?: number, hostname?: string, listeningListener?: Function): Server; - listen(port?: number, backlog?: number, listeningListener?: Function): Server; - listen(port?: number, listeningListener?: Function): Server; - listen(path: string, backlog?: number, listeningListener?: Function): Server; - listen(path: string, listeningListener?: Function): Server; - listen(options: ListenOptions, listeningListener?: Function): Server; - listen(handle: any, backlog?: number, listeningListener?: Function): Server; - listen(handle: any, listeningListener?: Function): Server; - close(callback?: Function): Server; + listen(port?: number, hostname?: string, backlog?: number, listeningListener?: Function): this; + listen(port?: number, hostname?: string, listeningListener?: Function): this; + listen(port?: number, backlog?: number, listeningListener?: Function): this; + listen(port?: number, listeningListener?: Function): this; + listen(path: string, backlog?: number, listeningListener?: Function): this; + listen(path: string, listeningListener?: Function): this; + listen(options: ListenOptions, listeningListener?: Function): this; + listen(handle: any, backlog?: number, listeningListener?: Function): this; + listen(handle: any, listeningListener?: Function): this; + close(callback?: Function): this; address(): { port: number; family: string; address: string; }; getConnections(cb: (error: Error | null, count: number) => void): void; - ref(): Server; - unref(): Server; + ref(): this; + unref(): this; maxConnections: number; connections: number; listening: boolean; @@ -4658,7 +4662,7 @@ declare module "tls" { * SecureContext.) If SNICallback wasn't provided the default callback * with high-level API will be used (see below). */ - SNICallback?: Function, + SNICallback?: (servername: string, cb: (err: Error | null, ctx: SecureContext) => void) => void, /** * An optional Buffer instance containing a TLS session. */ @@ -4670,12 +4674,7 @@ declare module "tls" { */ requestOCSP?: boolean }); - /** - * Returns the bound address, the address family name and port of the underlying socket as reported by - * the operating system. - * @returns {any} - An object with three properties, e.g. { port: 12346, family: 'IPv4', address: '127.0.0.1' }. - */ - address(): { port: number; family: string; address: string }; + /** * A boolean that is true if the peer certificate was signed by one of the specified CAs, otherwise false. */ @@ -4719,27 +4718,6 @@ declare module "tls" { * @returns {any} - TLS session ticket or undefined if none was negotiated. */ getTLSTicket(): any; - /** - * The string representation of the local IP address. - */ - localAddress: string; - /** - * The numeric representation of the local port. - */ - localPort: number; - /** - * The string representation of the remote IP address. - * For example, '74.125.127.100' or '2001:4860:a005::68'. - */ - remoteAddress: string; - /** - * The string representation of the remote IP family. 'IPv4' or 'IPv6'. - */ - remoteFamily: string; - /** - * The numeric representation of the remote port. For example, 443. - */ - remotePort: number; /** * Initiate TLS renegotiation process. * @@ -4750,7 +4728,7 @@ declare module "tls" { * @param {Function} callback - callback(err) will be executed with null as err, once the renegotiation * is successfully completed. */ - renegotiate(options: TlsOptions, callback: (err: Error) => any): any; + renegotiate(options: TlsOptions, callback: (err: Error | null) => void): any; /** * Set maximum TLS fragment size (default and maximum value is: 16384, minimum is: 512). * Smaller fragment size decreases buffering latency on the client: large fragments are buffered by @@ -4807,15 +4785,16 @@ declare module "tls" { requestCert?: boolean; rejectUnauthorized?: boolean; NPNProtocols?: string[] | Buffer; - SNICallback?: (servername: string, cb: (err: Error, ctx: SecureContext) => any) => any; + SNICallback?: (servername: string, cb: (err: Error | null, ctx: SecureContext) => void) => void; ecdhCurve?: string; dhparam?: string | Buffer; handshakeTimeout?: number; ALPNProtocols?: string[] | Buffer; sessionTimeout?: number; - ticketKeys?: any; + ticketKeys?: Buffer; sessionIdContext?: string; secureProtocol?: string; + secureOptions?: number; } export interface ConnectionOptions { @@ -4837,19 +4816,15 @@ declare module "tls" { secureContext?: Object; session?: Buffer; minDHSize?: number; - lookup?: (hostname: string, options: dns.LookupOneOptions, callback: (err: NodeJS.ErrnoException, address: string, family: number) => void) => void; + lookup?: net.LookupFunction; } - export interface Server extends net.Server { - close(callback?: Function): Server; - address(): { port: number; family: string; address: string; }; + export class Server extends net.Server { addContext(hostName: string, credentials: { key: string; cert: string; ca: string; }): void; - maxConnections: number; - connections: number; /** * events.EventEmitter diff --git a/types/node/node-tests.ts b/types/node/node-tests.ts index 7b36c859c7..1dbea4a773 100644 --- a/types/node/node-tests.ts +++ b/types/node/node-tests.ts @@ -1198,6 +1198,7 @@ namespace http_tests { const timeout: number = server.timeout; const listening: boolean = server.listening; const keepAliveTimeout: number = server.keepAliveTimeout; + server.setTimeout().setTimeout(1000).setTimeout(() => {}).setTimeout(100, () => {}); } // http IncomingMessage @@ -1347,6 +1348,15 @@ namespace https_tests { }); https.request('http://www.example.com/xyz'); + + { + const server = new https.Server(); + + const timeout: number = server.timeout; + const listening: boolean = server.listening; + const keepAliveTimeout: number = server.keepAliveTimeout; + server.setTimeout().setTimeout(1000).setTimeout(() => {}).setTimeout(100, () => {}); + } } //////////////////////////////////////////////////// @@ -1857,23 +1867,23 @@ namespace child_process_tests { childProcess.spawnSync("echo test"); } - { - childProcess.execFile("npm", () => {}); - childProcess.execFile("npm", ["-v"], () => {}); - childProcess.execFile("npm", ["-v"], { encoding: 'utf-8' }, (stdout, stderr) => { assert(stdout instanceof String); }); - childProcess.execFile("npm", ["-v"], { encoding: 'buffer' }, (stdout, stderr) => { assert(stdout instanceof Buffer); }); - childProcess.execFile("npm", { encoding: 'utf-8' }, (stdout, stderr) => { assert(stdout instanceof String); }); - childProcess.execFile("npm", { encoding: 'buffer' }, (stdout, stderr) => { assert(stdout instanceof Buffer); }); - } + { + childProcess.execFile("npm", () => {}); + childProcess.execFile("npm", ["-v"], () => {}); + childProcess.execFile("npm", ["-v"], { encoding: 'utf-8' }, (stdout, stderr) => { assert(stdout instanceof String); }); + childProcess.execFile("npm", ["-v"], { encoding: 'buffer' }, (stdout, stderr) => { assert(stdout instanceof Buffer); }); + childProcess.execFile("npm", { encoding: 'utf-8' }, (stdout, stderr) => { assert(stdout instanceof String); }); + childProcess.execFile("npm", { encoding: 'buffer' }, (stdout, stderr) => { assert(stdout instanceof Buffer); }); + } async function testPromisify() { const execFile = util.promisify(childProcess.execFile); - let r: { stdout: string | Buffer, stderr: string | Buffer } = await execFile("npm"); - r = await execFile("npm", ["-v"]); - r = await execFile("npm", ["-v"], { encoding: 'utf-8' }); - r = await execFile("npm", ["-v"], { encoding: 'buffer' }); - r = await execFile("npm", { encoding: 'utf-8' }); - r = await execFile("npm", { encoding: 'buffer' }); + let r: { stdout: string | Buffer, stderr: string | Buffer } = await execFile("npm"); + r = await execFile("npm", ["-v"]); + r = await execFile("npm", ["-v"], { encoding: 'utf-8' }); + r = await execFile("npm", ["-v"], { encoding: 'buffer' }); + r = await execFile("npm", { encoding: 'utf-8' }); + r = await execFile("npm", { encoding: 'buffer' }); } { @@ -2529,7 +2539,7 @@ namespace net_tests { 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 => { + lookup: (_hostname: string, _options: dns.LookupOneOptions, _callback: (err: NodeJS.ErrnoException | null, address: string, family: number) => void): void => { // nothing }, port: 80