From baa029c0a37adb580dbc480587fd90593d1c12b6 Mon Sep 17 00:00:00 2001 From: Paul Loyd Date: Mon, 2 Dec 2013 13:34:02 +0400 Subject: [PATCH] Add support `--noImplicitAny`, rename interfaces --- websocket/websocket-tests.ts | 14 +++--- websocket/websocket.d.ts | 82 +++++++++++++++++++----------------- 2 files changed, 51 insertions(+), 45 deletions(-) diff --git a/websocket/websocket-tests.ts b/websocket/websocket-tests.ts index e91e8467d..7de0fa5b0 100644 --- a/websocket/websocket-tests.ts +++ b/websocket/websocket-tests.ts @@ -19,10 +19,10 @@ import http = require('http'); autoAcceptConnections: false }); - function originIsAllowed(origin) { return true; } + function originIsAllowed(origin: string) { return true; } wsServer.on('request', (request: websocket.request) => { - if (!originIsAllowed(request.origin)) { + if(!originIsAllowed(request.origin)) { request.reject(); console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.'); return; @@ -30,7 +30,7 @@ import http = require('http'); var connection = request.accept('echo-protocol', request.origin); console.log((new Date()) + ' Connection accepted.'); - connection.on('message', (message) => { + connection.on('message', (message: websocket.IMessage) => { if (message.type === 'utf8') { console.log('Received Message: ' + message.utf8Data); connection.sendUTF(message.utf8Data); @@ -41,8 +41,8 @@ import http = require('http'); } }); - connection.on('close', (reasonCode, description) => { - console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); + connection.on('close', (code: number) => { + console.log(Date.now() + ' Peer ' + connection.remoteAddress + ' disconnected.'); }); }); } @@ -57,7 +57,7 @@ import http = require('http'); client.on('connect', (connection: websocket.connection) => { console.log('WebSocket client connected'); - connection.on('error', (error) => { + connection.on('error', (error: Error) => { console.log("Connection Error: " + error.toString()); }); @@ -65,7 +65,7 @@ import http = require('http'); console.log('echo-protocol Connection Closed'); }); - connection.on('message', (message) => { + connection.on('message', (message: websocket.IMessage) => { if (message.type === 'utf8') { console.log("Received: '" + message.utf8Data + "'"); } diff --git a/websocket/websocket.d.ts b/websocket/websocket.d.ts index 5f378feb8..7dbd9a67c 100644 --- a/websocket/websocket.d.ts +++ b/websocket/websocket.d.ts @@ -11,7 +11,7 @@ declare module "websocket" { import net = require('net'); import url = require('url'); - export interface ServerConfig { + export interface IServerConfig { /** The http server instance to attach to */ httpServer: http.Server; @@ -107,29 +107,29 @@ declare module "websocket" { } export class server extends events.EventEmitter { - constructor(serverConfig?: ServerConfig); + constructor(serverConfig?: IServerConfig); /** Attach the `server` instance to a Node http.Server instance */ - mount(serverConfig: ServerConfig); + mount(serverConfig: IServerConfig): void; /** * Detach the `server` instance from the Node http.Server instance. * All existing connections are left alone and will not be affected, * but no new WebSocket connections will be accepted. */ - unmount(); + unmount(): void; /** Close all open WebSocket connections */ - closeAllConnections(); + closeAllConnections(): void; /** Close all open WebSocket connections and unmount the server */ - shutDown(); + shutDown(): void; // Events - on(event: string, listener: Function): server; + on(event: string, listener: () => void): server; on(event: 'request', cb: (request: request) => void): server; on(event: 'connect', cb: (connection: connection) => void): server; on(event: 'close', cb: (connection: connection, reason: number, desc: string) => void): server; - addListener(event: string, listener: Function): server; + addListener(event: string, listener: () => void): server; addListener(event: 'request', cb: (request: request) => void): server; addListener(event: 'connect', cb: (connection: connection) => void): server; addListener(event: 'close', cb: (connection: connection, reason: number, desc: string) => void): server; @@ -172,7 +172,7 @@ declare module "websocket" { */ requestedProtocols: string[]; - constructor(socket: net.NodeSocket, httpRequest: http.ClientRequest, config: ServerConfig); + constructor(socket: net.NodeSocket, httpRequest: http.ClientRequest, config: IServerConfig); /** * After inspecting the `request` properties, call this function on the @@ -189,17 +189,23 @@ declare module "websocket" { * description that will be sent to the client in the form of an * `X-WebSocket-Reject-Reason` header. */ - reject(httpStatus?: number, reason?: string); + reject(httpStatus?: number, reason?: string): void; // Events - on(event: string, listener: Function): request; + on(event: string, listener: () => void): request; on(event: 'requestAccepted', cb: (connection: connection) => void): request; on(event: 'requestRejected', cb: () => void): request; - addListener(event: string, listener: Function): request; + addListener(event: string, listener: () => void): request; addListener(event: 'requestAccepted', cb: (connection: connection) => void): request; addListener(event: 'requestRejected', cb: () => void): request; } + export interface IMessage { + type: string; + utf8Data?: string; + binaryData?: NodeBuffer; + } + class connection extends events.EventEmitter { static CLOSE_REASON_NORMAL: number; static CLOSE_REASON_GOING_AWAY: number; @@ -249,7 +255,7 @@ declare module "websocket" { connected: boolean; constructor(socket: net.NodeSocket, extensions: any[], protocol: string, - maskOutgoingPackets: boolean, config: ServerConfig); + maskOutgoingPackets: boolean, config: IServerConfig); /** * Close the connection. A close frame will be sent to the remote peer indicating @@ -257,36 +263,35 @@ declare module "websocket" { * `config.closeTimeout` milliseconds for an acknowledgment from the remote peer * before terminating the underlying socket connection. */ - close(); + close(): void; /** * Send a close frame to the remote peer and immediately close the socket without * waiting for a response. This should generally be used only in error conditions. */ - drop(reasonCode?: number, description?: string); + drop(reasonCode?: number, description?: string): void; /** * Immediately sends the specified string as a UTF-8 WebSocket message to the remote * peer. If `config.fragmentOutgoingMessages` is true the message may be sent as * multiple fragments if it exceeds `config.fragmentationThreshold` bytes. */ - sendUTF(data: {toString: (...args) => string}); - + sendUTF(data: {toString: (...args: any[]) => string}): void; /** * Immediately sends the specified Node Buffer object as a Binary WebSocket message * to the remote peer. If config.fragmentOutgoingMessages is true the message may be * sent as multiple fragments if it exceeds config.fragmentationThreshold bytes. */ - sendBytes(buffer: NodeBuffer); + sendBytes(buffer: NodeBuffer): void; /** Auto-detect the data type and send UTF-8 or Binary message */ - send(data: NodeBuffer); - send(data: {toString: (...args) => string}); + send(data: NodeBuffer): void; + send(data: {toString: (...args: any[]) => string}): void; /** Sends a ping frame. Ping frames must not exceed 125 bytes in length. */ - ping(data: NodeBuffer); - ping(data: {toString: (...args) => string}); + ping(data: NodeBuffer): void; + ping(data: {toString: (...args: any[]) => string}): void; /** * Sends a pong frame. Pong frames may be sent unsolicited and such pong frames will @@ -296,25 +301,23 @@ declare module "websocket" { * be no need to use this method to respond to pings. * Pong frames must not exceed 125 bytes in length. */ - pong(buffer: NodeBuffer); + pong(buffer: NodeBuffer): void; /** * Serializes a `frame` object into binary data and immediately sends it to * the remote peer. This is an advanced function, requiring you to manually compose * your own `frame`. You should probably use sendUTF or sendBytes instead. */ - sendFrame(frame: frame); + sendFrame(frame: frame): void; // Events - on(event: string, listener: Function): connection; - on(event: 'message', cb: (msg: {type: string; utf8Data?: string; - binaryData?: NodeBuffer}) => void): connection; + on(event: string, listener: () => void): connection; + on(event: 'message', cb: (data: IMessage) => void): connection; on(event: 'frame', cb: (frame: frame) => void): connection; on(event: 'close', cb: (code: number, desc: string) => void): connection; on(event: 'error', cb: (err: Error) => void): connection; - addListener(event: string, listener: Function): connection; - addListener(event: 'message', cb: (msg: {type: string; utf8Data?: string; - binaryData?: NodeBuffer}) => void): connection; + addListener(event: string, listener: () => void): connection; + addListener(event: 'message', cb: (data: IMessage) => void): connection; addListener(event: 'frame', cb: (frame: frame) => void): connection; addListener(event: 'close', cb: (code: number, desc: string) => void): connection; addListener(event: 'error', cb: (err: Error) => void): connection; @@ -375,7 +378,7 @@ declare module "websocket" { binaryPayload: NodeBuffer; } - export interface ClientConfig { + export interface IClientConfig { /** * Which version of the WebSocket protocol to use when making the connection. * Currently supported values are 8 and 13. This option will be removed once the @@ -432,7 +435,7 @@ declare module "websocket" { } class client extends events.EventEmitter { - constructor(clientConfig?: ClientConfig); + constructor(clientConfig?: IClientConfig); /** * Establish a connection. The remote server will select the best subprotocol that @@ -442,19 +445,22 @@ declare module "websocket" { * any scripting content that caused the connection to be requested. * @param requestUrl should be a standard websocket url */ - connect(requestUrl: url.Url, protocols?: string[], origin?: string, headers?: any[]); - connect(requestUrl: string, protocols?: string[], origin?: string, headers?: any[]); - connect(requestUrl: url.Url, protocols?: string, origin?: string, headers?: any[]); - connect(requestUrl: string, protocols?: string, origin?: string, headers?: any[]); + connect(requestUrl: url.Url, protocols?: string[], origin?: string, headers?: any[]): void; + connect(requestUrl: string, protocols?: string[], origin?: string, headers?: any[]): void; + connect(requestUrl: url.Url, protocols?: string, origin?: string, headers?: any[]): void; + connect(requestUrl: string, protocols?: string, origin?: string, headers?: any[]): void; // Events - on(event: string, listener: Function): client; + on(event: string, listener: () => void): client; on(event: 'connect', cb: (connection: connection) => void): client; on(event: 'connectFailed', cb: (err: Error) => void): client; - addListener(event: string, listener: Function): client; + addListener(event: string, listener: () => void): client; addListener(event: 'connect', cb: (connection: connection) => void): client; addListener(event: 'connectFailed', cb: (err: Error) => void): client; } export var version: string; + export var constants: { + DEBUG: boolean; + }; }