Fix ws type definitions (#41905)

* Make `url.URL` a valid type for the `address` parameter

* Fix `this` type on server event listeners
This commit is contained in:
Luigi Pinca
2020-01-29 22:48:55 +01:00
committed by GitHub
parent 493004fbee
commit c7fb197a53
2 changed files with 8 additions and 8 deletions

14
types/ws/index.d.ts vendored
View File

@@ -43,8 +43,8 @@ declare class WebSocket extends events.EventEmitter {
onclose: (event: WebSocket.CloseEvent) => void;
onmessage: (event: WebSocket.MessageEvent) => void;
constructor(address: string, options?: WebSocket.ClientOptions);
constructor(address: string, protocols?: string | string[], options?: WebSocket.ClientOptions);
constructor(address: string | url.URL, options?: WebSocket.ClientOptions);
constructor(address: string | url.URL, protocols?: string | string[], options?: WebSocket.ClientOptions);
close(code?: number, data?: string): void;
ping(data?: any, mask?: boolean, cb?: (err: Error) => void): void;
@@ -231,11 +231,11 @@ declare namespace WebSocket {
shouldHandle(request: http.IncomingMessage): boolean;
// Events
on(event: 'connection', cb: (this: WebSocket, socket: WebSocket, request: http.IncomingMessage) => void): this;
on(event: 'error', cb: (this: WebSocket, error: Error) => void): this;
on(event: 'headers', cb: (this: WebSocket, headers: string[], request: http.IncomingMessage) => void): this;
on(event: 'close' | 'listening', cb: (this: WebSocket) => void): this;
on(event: string | symbol, listener: (this: WebSocket, ...args: any[]) => void): this;
on(event: 'connection', cb: (this: Server, socket: WebSocket, request: http.IncomingMessage) => void): this;
on(event: 'error', cb: (this: Server, error: Error) => void): this;
on(event: 'headers', cb: (this: Server, headers: string[], request: http.IncomingMessage) => void): this;
on(event: 'close' | 'listening', cb: (this: Server) => void): this;
on(event: string | symbol, listener: (this: Server, ...args: any[]) => void): this;
addListener(event: 'connection', cb: (client: WebSocket) => void): this;
addListener(event: 'error', cb: (err: Error) => void): this;

View File

@@ -10,7 +10,7 @@ import * as url from 'url';
}
{
const addr = 'ws://www.host.com/path';
const addr = new url.URL('ws://www.host.com/path');
const ws = new WebSocket(addr);
ws.on('open', () => {
const array = new Float32Array(5);