Skip to content

Commit

Permalink
fix(typings): add return types and general-case overload signatures (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
david-fong authored and darrachequesne committed Feb 2, 2021
1 parent 86eb422 commit 9e8f288
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 76 deletions.
25 changes: 13 additions & 12 deletions lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class Client {
* @param {Object} auth - the auth parameters
* @private
*/
private connect(name: string, auth: object = {}) {
private connect(name: string, auth: object = {}): void {
if (this.server._nsps.has(name)) {
debug("connecting to namespace %s", name);
return this.doConnect(name, auth);
Expand Down Expand Up @@ -112,7 +112,7 @@ export class Client {
*
* @private
*/
private doConnect(name: string, auth: object) {
private doConnect(name: string, auth: object): void {
const nsp = this.server.of(name);

const socket = nsp._add(this, auth, () => {
Expand All @@ -131,7 +131,7 @@ export class Client {
*
* @private
*/
_disconnect() {
_disconnect(): void {
for (const socket of this.sockets.values()) {
socket.disconnect();
}
Expand All @@ -144,7 +144,7 @@ export class Client {
*
* @private
*/
_remove(socket: Socket) {
_remove(socket: Socket): void {
if (this.sockets.has(socket.id)) {
const nsp = this.sockets.get(socket.id)!.nsp.name;
this.sockets.delete(socket.id);
Expand All @@ -159,7 +159,7 @@ export class Client {
*
* @private
*/
private close() {
private close(): void {
if ("open" === this.conn.readyState) {
debug("forcing transport close");
this.conn.close();
Expand All @@ -174,12 +174,13 @@ export class Client {
* @param {Object} opts
* @private
*/
_packet(packet, opts?) {
_packet(packet: Packet, opts?: any): void {
opts = opts || {};
const self = this;

// this writes to the actual connection
function writeToEngine(encodedPackets) {
function writeToEngine(encodedPackets: any) {
// TODO clarify this.
if (opts.volatile && !self.conn.transport.writable) return;
for (let i = 0; i < encodedPackets.length; i++) {
self.conn.write(encodedPackets[i], { compress: opts.compress });
Expand All @@ -205,7 +206,7 @@ export class Client {
*
* @private
*/
private ondata(data) {
private ondata(data): void {
// try/catch is needed for protocol violations (GH-1880)
try {
this.decoder.add(data);
Expand All @@ -219,7 +220,7 @@ export class Client {
*
* @private
*/
private ondecoded(packet: Packet) {
private ondecoded(packet: Packet): void {
if (PacketType.CONNECT === packet.type) {
this.connect(packet.nsp, packet.data);
} else {
Expand All @@ -240,7 +241,7 @@ export class Client {
* @param {Object} err object
* @private
*/
private onerror(err) {
private onerror(err): void {
for (const socket of this.sockets.values()) {
socket._onerror(err);
}
Expand All @@ -253,7 +254,7 @@ export class Client {
* @param reason
* @private
*/
private onclose(reason: string) {
private onclose(reason: string): void {
debug("client close with reason %s", reason);

// ignore a potential subsequent `close` event
Expand All @@ -272,7 +273,7 @@ export class Client {
* Cleans up event listeners.
* @private
*/
private destroy() {
private destroy(): void {
this.conn.removeListener("data", this.ondata);
this.conn.removeListener("error", this.onerror);
this.conn.removeListener("close", this.onclose);
Expand Down
67 changes: 39 additions & 28 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ export class Server extends EventEmitter {
*/
constructor(opts?: Partial<ServerOptions>);
constructor(srv?: http.Server | number, opts?: Partial<ServerOptions>);
constructor(
srv: undefined | Partial<ServerOptions> | http.Server | number,
opts?: Partial<ServerOptions>
);
constructor(
srv: undefined | Partial<ServerOptions> | http.Server | number,
opts: Partial<ServerOptions> = {}
Expand Down Expand Up @@ -222,9 +226,10 @@ export class Server extends EventEmitter {
* @return self when setting or value when getting
* @public
*/
public serveClient(v: boolean): Server;
public serveClient(v: boolean): this;
public serveClient(): boolean;
public serveClient(v?: boolean): Server | boolean {
public serveClient(v?: boolean): this | boolean;
public serveClient(v?: boolean): this | boolean {
if (!arguments.length) return this._serveClient;
this._serveClient = v!;
return this;
Expand All @@ -234,7 +239,7 @@ export class Server extends EventEmitter {
* Executes the middleware for an incoming namespace not already created on the server.
*
* @param name - name of incoming namespace
* @param {Object} auth - the auth parameters
* @param auth - the auth parameters
* @param fn - callback
*
* @private
Expand All @@ -243,7 +248,7 @@ export class Server extends EventEmitter {
name: string,
auth: object,
fn: (nsp: Namespace | false) => void
) {
): void {
if (this.parentNsps.size === 0) return fn(false);

const keysIterator = this.parentNsps.keys();
Expand Down Expand Up @@ -272,9 +277,10 @@ export class Server extends EventEmitter {
* @return {Server|String} self when setting or value when getting
* @public
*/
public path(v: string): Server;
public path(v: string): this;
public path(): string;
public path(v?: string): Server | string {
public path(v?: string): this | string;
public path(v?: string): this | string {
if (!arguments.length) return this._path;

this._path = v!.replace(/\/$/, "");
Expand All @@ -293,9 +299,10 @@ export class Server extends EventEmitter {
* @param v
* @public
*/
public connectTimeout(v: number): Server;
public connectTimeout(v: number): this;
public connectTimeout(): number;
public connectTimeout(v?: number): Server | number {
public connectTimeout(v?: number): this | number;
public connectTimeout(v?: number): this | number {
if (v === undefined) return this._connectTimeout;
this._connectTimeout = v;
return this;
Expand All @@ -309,8 +316,9 @@ export class Server extends EventEmitter {
* @public
*/
public adapter(): typeof Adapter | undefined;
public adapter(v: typeof Adapter): Server;
public adapter(v?: typeof Adapter): typeof Adapter | undefined | Server {
public adapter(v: typeof Adapter): this;
public adapter(v?: typeof Adapter): typeof Adapter | undefined | this;
public adapter(v?: typeof Adapter): typeof Adapter | undefined | this {
if (!arguments.length) return this._adapter;
this._adapter = v;
for (const nsp of this._nsps.values()) {
Expand All @@ -330,7 +338,7 @@ export class Server extends EventEmitter {
public listen(
srv: http.Server | number,
opts: Partial<ServerOptions> = {}
): Server {
): this {
return this.attach(srv, opts);
}

Expand All @@ -345,7 +353,7 @@ export class Server extends EventEmitter {
public attach(
srv: http.Server | number,
opts: Partial<ServerOptions> = {}
): Server {
): this {
if ("function" == typeof srv) {
const msg =
"You are trying to attach socket.io to an express " +
Expand Down Expand Up @@ -385,7 +393,10 @@ export class Server extends EventEmitter {
* @param opts - options passed to engine.io
* @private
*/
private initEngine(srv: http.Server, opts: Partial<EngineAttachOptions>) {
private initEngine(
srv: http.Server,
opts: Partial<EngineAttachOptions>
): void {
// initialize engine
debug("creating engine.io instance with opts %j", opts);
this.eio = engine.attach(srv, opts);
Expand All @@ -406,7 +417,7 @@ export class Server extends EventEmitter {
* @param srv http server
* @private
*/
private attachServe(srv: http.Server) {
private attachServe(srv: http.Server): void {
debug("attaching client serving req handler");

const evs = srv.listeners("request").slice(0);
Expand All @@ -429,7 +440,7 @@ export class Server extends EventEmitter {
* @param res
* @private
*/
private serve(req: http.IncomingMessage, res: http.ServerResponse) {
private serve(req: http.IncomingMessage, res: http.ServerResponse): void {
const filename = req.url!.replace(this._path, "");
const isMap = dotMapRegex.test(filename);
const type = isMap ? "map" : "source";
Expand Down Expand Up @@ -474,7 +485,7 @@ export class Server extends EventEmitter {
filename: string,
req: http.IncomingMessage,
res: http.ServerResponse
) {
): void {
const readStream = createReadStream(
path.join(__dirname, "../client-dist/", filename)
);
Expand Down Expand Up @@ -513,7 +524,7 @@ export class Server extends EventEmitter {
* @return self
* @public
*/
public bind(engine): Server {
public bind(engine): this {
this.engine = engine;
this.engine.on("connection", this.onconnection.bind(this));
return this;
Expand All @@ -526,7 +537,7 @@ export class Server extends EventEmitter {
* @return self
* @private
*/
private onconnection(conn): Server {
private onconnection(conn): this {
debug("incoming connection with id %s", conn.id);
const client = new Client(this, conn);
if (conn.protocol === 3) {
Expand All @@ -540,13 +551,13 @@ export class Server extends EventEmitter {
* Looks up a namespace.
*
* @param {String|RegExp|Function} name nsp name
* @param [fn] optional, nsp `connection` ev handler
* @param fn optional, nsp `connection` ev handler
* @public
*/
public of(
name: string | RegExp | ParentNspNameMatchFn,
fn?: (socket: Socket) => void
) {
): Namespace {
if (typeof name === "function" || name instanceof RegExp) {
const parentNsp = new ParentNamespace(this);
debug("initializing parent namespace %s", parentNsp.name);
Expand Down Expand Up @@ -605,7 +616,7 @@ export class Server extends EventEmitter {
*/
public use(
fn: (socket: Socket, next: (err?: ExtendedError) => void) => void
): Server {
): this {
this.sockets.use(fn);
return this;
}
Expand All @@ -617,7 +628,7 @@ export class Server extends EventEmitter {
* @return self
* @public
*/
public to(name: Room): Server {
public to(name: Room): this {
this.sockets.to(name);
return this;
}
Expand All @@ -629,7 +640,7 @@ export class Server extends EventEmitter {
* @return self
* @public
*/
public in(name: Room): Server {
public in(name: Room): this {
this.sockets.in(name);
return this;
}
Expand All @@ -640,7 +651,7 @@ export class Server extends EventEmitter {
* @return self
* @public
*/
public send(...args: readonly any[]): Server {
public send(...args: readonly any[]): this {
this.sockets.emit("message", ...args);
return this;
}
Expand All @@ -651,7 +662,7 @@ export class Server extends EventEmitter {
* @return self
* @public
*/
public write(...args: readonly any[]): Server {
public write(...args: readonly any[]): this {
this.sockets.emit("message", ...args);
return this;
}
Expand All @@ -672,7 +683,7 @@ export class Server extends EventEmitter {
* @return self
* @public
*/
public compress(compress: boolean): Server {
public compress(compress: boolean): this {
this.sockets.compress(compress);
return this;
}
Expand All @@ -685,7 +696,7 @@ export class Server extends EventEmitter {
* @return self
* @public
*/
public get volatile(): Server {
public get volatile(): this {
this.sockets.volatile;
return this;
}
Expand All @@ -696,7 +707,7 @@ export class Server extends EventEmitter {
* @return self
* @public
*/
public get local(): Server {
public get local(): this {
this.sockets.local;
return this;
}
Expand Down
Loading

0 comments on commit 9e8f288

Please sign in to comment.