import Koa = require("koa"); import compose = require("koa-compose"); import * as http from "http"; import * as https from "https"; import ws = require("ws"); declare module "koa" { interface Context { websocket: ws; path: string; } } declare namespace KoaWebsocket { type Middleware = compose.Middleware< MiddlewareContext & ContextT >; interface MiddlewareContext extends Koa.Context { // Limitation: Declaration merging cannot overwrap existing properties. // That's why this property is here, not in the merged declaration above. app: App; state: StateT; } class Server { app: App; middleware: Array>; server?: ws.Server | undefined; constructor(app: Koa); listen(options: ws.ServerOptions): ws.Server; onConnection(socket: ws, request: http.IncomingMessage): void; use(middleware: Middleware): this; } interface App extends Koa { ws: Server; } } declare function KoaWebsocket< StateT = Koa.DefaultState, ContextT = Koa.DefaultContext, >( app: Koa, wsOptions?: ws.ServerOptions, httpsOptions?: https.ServerOptions, ): KoaWebsocket.App; export = KoaWebsocket;