diff --git a/types/simple-websocket/index.d.ts b/types/simple-websocket/index.d.ts new file mode 100644 index 0000000000..7e2ec0f6b8 --- /dev/null +++ b/types/simple-websocket/index.d.ts @@ -0,0 +1,32 @@ +// Type definitions for simple-websocket 7.0 +// Project: https://github.com/feross/simple-websocket +// Definitions by: Piotr Roszatycki +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +import { Duplex, DuplexOptions } from "stream"; +import WebSocket = require("ws"); + +declare namespace Socket { + interface Options extends DuplexOptions { + /** websocket server url */ + url?: string; + /** raw websocket instance to wrap */ + socket?: WebSocket; + } +} + +declare class Socket extends Duplex { + static WEBSOCKET_SUPPORT: boolean; + + constructor(options: Socket.Options | string); + + /** Send text/binary data to the WebSocket server */ + send(chunk: any): void; + + /** Destroy and cleanup this websocket connection */ + destroy(err?: Error): void; +} + +export = Socket; diff --git a/types/simple-websocket/server.d.ts b/types/simple-websocket/server.d.ts new file mode 100644 index 0000000000..72f4c59c30 --- /dev/null +++ b/types/simple-websocket/server.d.ts @@ -0,0 +1,70 @@ +/// + +import { EventEmitter } from "events"; +import * as http from "http"; +import * as net from "net"; +import WebSocket = require("ws"); + +import Socket = require("."); + +declare namespace SocketServer { + type Options = WebSocket.ServerOptions; +} + +declare class SocketServer extends EventEmitter { + options: SocketServer.Options; + path: string; + clients: Set; + + constructor(options?: SocketServer.Options, callback?: () => void); + + close(cb?: (err?: Error) => void): void; + handleUpgrade(request: http.IncomingMessage, socket: net.Socket, + upgradeHead: Buffer, callback: (client: WebSocket) => void): void; + shouldHandle(request: http.IncomingMessage): boolean; + + // Events + addListener(event: "connection", cb: (client: Socket) => void): this; + addListener(event: "error", cb: (err: Error) => void): this; + addListener(event: "headers", cb: (headers: string[], request: http.IncomingMessage) => void): this; + addListener(event: "listening", cb: () => void): this; + addListener(event: string | symbol, listener: (...args: any[]) => void): this; + + emit(event: "connection", socket: Socket): boolean; + emit(event: "error", error: Error): boolean; + emit(event: "headers", headers: string[], request: http.IncomingMessage): boolean; + emit(event: "listening"): boolean; + emit(event: string | symbol, ...args: any[]): boolean; + + on(event: "connection", cb: (socket: Socket, request: http.IncomingMessage) => void): this; + on(event: "error", cb: (error: Error) => void): this; + on(event: "headers", cb: (headers: string[], request: http.IncomingMessage) => void): this; + on(event: "listening", cb: () => void): this; + on(event: string | symbol, listener: (...args: any[]) => void): this; + + once(event: "connection", cb: (socket: Socket, request: http.IncomingMessage) => void): this; + once(event: "error", cb: (error: Error) => void): this; + once(event: "headers", cb: (headers: string[], request: http.IncomingMessage) => void): this; + once(event: "listening", cb: () => void): this; + once(event: string | symbol, listener: (...args: any[]) => void): this; + + prependListener(event: "connection", cb: (client: Socket) => void): this; + prependListener(event: "error", cb: (err: Error) => void): this; + prependListener(event: "headers", cb: (headers: string[], request: http.IncomingMessage) => void): this; + prependListener(event: "listening", cb: () => void): this; + prependListener(event: string | symbol, listener: (...args: any[]) => void): this; + + prependOnceListener(event: "connection", cb: (client: Socket) => void): this; + prependOnceListener(event: "error", cb: (err: Error) => void): this; + prependOnceListener(event: "headers", cb: (headers: string[], request: http.IncomingMessage) => void): this; + prependOnceListener(event: "listening", cb: () => void): this; + prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this; + + removeListener(event: "connection", cb: (client: Socket) => void): this; + removeListener(event: "error", cb: (err: Error) => void): this; + removeListener(event: "headers", cb: (headers: string[], request: http.IncomingMessage) => void): this; + removeListener(event: "listening", cb: () => void): this; + removeListener(event: string | symbol, listener: (...args: any[]) => void): this; +} + +export = SocketServer; diff --git a/types/simple-websocket/simple-websocket-tests.ts b/types/simple-websocket/simple-websocket-tests.ts new file mode 100644 index 0000000000..0a31b08f22 --- /dev/null +++ b/types/simple-websocket/simple-websocket-tests.ts @@ -0,0 +1,30 @@ +import Socket = require("simple-websocket"); + +if (Socket.WEBSOCKET_SUPPORT) { + const socket = new Socket('ws://echo.websocket.org'); + socket.on('connect', () => { + // socket is connected! + socket.send('sup!'); + }); + + socket.on('data', (data: string | Buffer) => { + console.log('got message: ' + data); + }); + + socket.on('error', (err: Error) => { + socket.destroy(err); + }); +} + +import Server = require("simple-websocket/server"); + +const server = new Server({ port: 8080 }); + +server.on('connection', (socket: Socket) => { + socket.write('pong'); + socket.on('data', (data: string | Buffer) => {}); + socket.on('close', () => {}); + socket.on('error', (err: Error) => {}); +}); + +server.close(); diff --git a/types/simple-websocket/tsconfig.json b/types/simple-websocket/tsconfig.json new file mode 100644 index 0000000000..06b0fdd9da --- /dev/null +++ b/types/simple-websocket/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "esModuleInterop": true + }, + "files": [ + "index.d.ts", + "server.d.ts", + "simple-websocket-tests.ts" + ] +} diff --git a/types/simple-websocket/tslint.json b/types/simple-websocket/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/simple-websocket/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }