From 891b1870e92d1ec38910f03bb839817e2d6be65a Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Tue, 11 May 2021 23:36:17 +0200 Subject: [PATCH] fix(typings): properly type the adapter attribute Related: https://github.com/socketio/socket.io/issues/3796 --- lib/index.ts | 15 +++++++++------ lib/namespace.ts | 1 + test/socket.io.test-d.ts | 24 +++++++++++++++++++++++- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 62c457412e..7f9d91209b 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -37,6 +37,8 @@ type ParentNspNameMatchFn = ( fn: (err: Error | null, success: boolean) => void ) => void; +type AdapterConstructor = typeof Adapter | ((nsp: Namespace) => Adapter); + interface EngineOptions { /** * how many ms without a pong packet to consider the connection closed @@ -152,7 +154,7 @@ interface ServerOptions extends EngineAttachOptions { * the adapter to use * @default the in-memory adapter (https://github.com/socketio/socket.io-adapter) */ - adapter: any; + adapter: AdapterConstructor; /** * the parser to use * @default the default parser (https://github.com/socketio/socket.io-parser) @@ -207,7 +209,7 @@ export class Server< ParentNspNameMatchFn, ParentNamespace > = new Map(); - private _adapter?: typeof Adapter; + private _adapter?: AdapterConstructor; private _serveClient: boolean; private opts: Partial; private eio; @@ -360,10 +362,11 @@ export class Server< * @return self when setting or value when getting * @public */ - public adapter(): typeof Adapter | undefined; - public adapter(v: typeof Adapter): this; - public adapter(v?: typeof Adapter): typeof Adapter | undefined | this; - public adapter(v?: typeof Adapter): typeof Adapter | undefined | this { + public adapter(): AdapterConstructor | undefined; + public adapter(v: AdapterConstructor): this; + public adapter( + v?: AdapterConstructor + ): AdapterConstructor | undefined | this { if (!arguments.length) return this._adapter; this._adapter = v; for (const nsp of this._nsps.values()) { diff --git a/lib/namespace.ts b/lib/namespace.ts index 1e8a26cdd3..86ee06ad8b 100644 --- a/lib/namespace.ts +++ b/lib/namespace.ts @@ -102,6 +102,7 @@ export class Namespace< * @private */ _initAdapter(): void { + // @ts-ignore this.adapter = new (this.server.adapter()!)(this); } diff --git a/test/socket.io.test-d.ts b/test/socket.io.test-d.ts index 321cd65b27..6a9f838958 100644 --- a/test/socket.io.test-d.ts +++ b/test/socket.io.test-d.ts @@ -1,8 +1,9 @@ "use strict"; -import { Server, Socket } from ".."; +import { Namespace, Server, Socket } from ".."; import type { DefaultEventsMap } from "../lib/typed-events"; import { createServer } from "http"; import { expectError, expectType } from "tsd"; +import { Adapter } from "socket.io-adapter"; // This file is run by tsd, not mocha. @@ -275,4 +276,25 @@ describe("server", () => { }); }); }); + + describe("adapter", () => { + it("accepts arguments of the correct types", () => { + const io = new Server({ + adapter: (nsp) => new Adapter(nsp), + }); + io.adapter(Adapter); + + class MyCustomAdapter extends Adapter { + constructor(nsp, readonly opts) { + super(nsp); + } + } + io.adapter((nsp) => new MyCustomAdapter(nsp, { test: "123" })); + }); + + it("does not accept arguments of wrong types", () => { + const io = new Server(); + expectError(io.adapter((nsp) => "nope")); + }); + }); });