Skip to content

Commit 9e8f288

Browse files
david-fongdarrachequesne
authored andcommittedFeb 2, 2021
fix(typings): add return types and general-case overload signatures (#3776)
See also: https://stackoverflow.com/questions/52760509/typescript-returntype-of-overloaded-function/52760599#52760599
1 parent 86eb422 commit 9e8f288

File tree

4 files changed

+94
-76
lines changed

4 files changed

+94
-76
lines changed
 

‎lib/client.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class Client {
7777
* @param {Object} auth - the auth parameters
7878
* @private
7979
*/
80-
private connect(name: string, auth: object = {}) {
80+
private connect(name: string, auth: object = {}): void {
8181
if (this.server._nsps.has(name)) {
8282
debug("connecting to namespace %s", name);
8383
return this.doConnect(name, auth);
@@ -112,7 +112,7 @@ export class Client {
112112
*
113113
* @private
114114
*/
115-
private doConnect(name: string, auth: object) {
115+
private doConnect(name: string, auth: object): void {
116116
const nsp = this.server.of(name);
117117

118118
const socket = nsp._add(this, auth, () => {
@@ -131,7 +131,7 @@ export class Client {
131131
*
132132
* @private
133133
*/
134-
_disconnect() {
134+
_disconnect(): void {
135135
for (const socket of this.sockets.values()) {
136136
socket.disconnect();
137137
}
@@ -144,7 +144,7 @@ export class Client {
144144
*
145145
* @private
146146
*/
147-
_remove(socket: Socket) {
147+
_remove(socket: Socket): void {
148148
if (this.sockets.has(socket.id)) {
149149
const nsp = this.sockets.get(socket.id)!.nsp.name;
150150
this.sockets.delete(socket.id);
@@ -159,7 +159,7 @@ export class Client {
159159
*
160160
* @private
161161
*/
162-
private close() {
162+
private close(): void {
163163
if ("open" === this.conn.readyState) {
164164
debug("forcing transport close");
165165
this.conn.close();
@@ -174,12 +174,13 @@ export class Client {
174174
* @param {Object} opts
175175
* @private
176176
*/
177-
_packet(packet, opts?) {
177+
_packet(packet: Packet, opts?: any): void {
178178
opts = opts || {};
179179
const self = this;
180180

181181
// this writes to the actual connection
182-
function writeToEngine(encodedPackets) {
182+
function writeToEngine(encodedPackets: any) {
183+
// TODO clarify this.
183184
if (opts.volatile && !self.conn.transport.writable) return;
184185
for (let i = 0; i < encodedPackets.length; i++) {
185186
self.conn.write(encodedPackets[i], { compress: opts.compress });
@@ -205,7 +206,7 @@ export class Client {
205206
*
206207
* @private
207208
*/
208-
private ondata(data) {
209+
private ondata(data): void {
209210
// try/catch is needed for protocol violations (GH-1880)
210211
try {
211212
this.decoder.add(data);
@@ -219,7 +220,7 @@ export class Client {
219220
*
220221
* @private
221222
*/
222-
private ondecoded(packet: Packet) {
223+
private ondecoded(packet: Packet): void {
223224
if (PacketType.CONNECT === packet.type) {
224225
this.connect(packet.nsp, packet.data);
225226
} else {
@@ -240,7 +241,7 @@ export class Client {
240241
* @param {Object} err object
241242
* @private
242243
*/
243-
private onerror(err) {
244+
private onerror(err): void {
244245
for (const socket of this.sockets.values()) {
245246
socket._onerror(err);
246247
}
@@ -253,7 +254,7 @@ export class Client {
253254
* @param reason
254255
* @private
255256
*/
256-
private onclose(reason: string) {
257+
private onclose(reason: string): void {
257258
debug("client close with reason %s", reason);
258259

259260
// ignore a potential subsequent `close` event
@@ -272,7 +273,7 @@ export class Client {
272273
* Cleans up event listeners.
273274
* @private
274275
*/
275-
private destroy() {
276+
private destroy(): void {
276277
this.conn.removeListener("data", this.ondata);
277278
this.conn.removeListener("error", this.onerror);
278279
this.conn.removeListener("close", this.onclose);

‎lib/index.ts

+39-28
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ export class Server extends EventEmitter {
191191
*/
192192
constructor(opts?: Partial<ServerOptions>);
193193
constructor(srv?: http.Server | number, opts?: Partial<ServerOptions>);
194+
constructor(
195+
srv: undefined | Partial<ServerOptions> | http.Server | number,
196+
opts?: Partial<ServerOptions>
197+
);
194198
constructor(
195199
srv: undefined | Partial<ServerOptions> | http.Server | number,
196200
opts: Partial<ServerOptions> = {}
@@ -222,9 +226,10 @@ export class Server extends EventEmitter {
222226
* @return self when setting or value when getting
223227
* @public
224228
*/
225-
public serveClient(v: boolean): Server;
229+
public serveClient(v: boolean): this;
226230
public serveClient(): boolean;
227-
public serveClient(v?: boolean): Server | boolean {
231+
public serveClient(v?: boolean): this | boolean;
232+
public serveClient(v?: boolean): this | boolean {
228233
if (!arguments.length) return this._serveClient;
229234
this._serveClient = v!;
230235
return this;
@@ -234,7 +239,7 @@ export class Server extends EventEmitter {
234239
* Executes the middleware for an incoming namespace not already created on the server.
235240
*
236241
* @param name - name of incoming namespace
237-
* @param {Object} auth - the auth parameters
242+
* @param auth - the auth parameters
238243
* @param fn - callback
239244
*
240245
* @private
@@ -243,7 +248,7 @@ export class Server extends EventEmitter {
243248
name: string,
244249
auth: object,
245250
fn: (nsp: Namespace | false) => void
246-
) {
251+
): void {
247252
if (this.parentNsps.size === 0) return fn(false);
248253

249254
const keysIterator = this.parentNsps.keys();
@@ -272,9 +277,10 @@ export class Server extends EventEmitter {
272277
* @return {Server|String} self when setting or value when getting
273278
* @public
274279
*/
275-
public path(v: string): Server;
280+
public path(v: string): this;
276281
public path(): string;
277-
public path(v?: string): Server | string {
282+
public path(v?: string): this | string;
283+
public path(v?: string): this | string {
278284
if (!arguments.length) return this._path;
279285

280286
this._path = v!.replace(/\/$/, "");
@@ -293,9 +299,10 @@ export class Server extends EventEmitter {
293299
* @param v
294300
* @public
295301
*/
296-
public connectTimeout(v: number): Server;
302+
public connectTimeout(v: number): this;
297303
public connectTimeout(): number;
298-
public connectTimeout(v?: number): Server | number {
304+
public connectTimeout(v?: number): this | number;
305+
public connectTimeout(v?: number): this | number {
299306
if (v === undefined) return this._connectTimeout;
300307
this._connectTimeout = v;
301308
return this;
@@ -309,8 +316,9 @@ export class Server extends EventEmitter {
309316
* @public
310317
*/
311318
public adapter(): typeof Adapter | undefined;
312-
public adapter(v: typeof Adapter): Server;
313-
public adapter(v?: typeof Adapter): typeof Adapter | undefined | Server {
319+
public adapter(v: typeof Adapter): this;
320+
public adapter(v?: typeof Adapter): typeof Adapter | undefined | this;
321+
public adapter(v?: typeof Adapter): typeof Adapter | undefined | this {
314322
if (!arguments.length) return this._adapter;
315323
this._adapter = v;
316324
for (const nsp of this._nsps.values()) {
@@ -330,7 +338,7 @@ export class Server extends EventEmitter {
330338
public listen(
331339
srv: http.Server | number,
332340
opts: Partial<ServerOptions> = {}
333-
): Server {
341+
): this {
334342
return this.attach(srv, opts);
335343
}
336344

@@ -345,7 +353,7 @@ export class Server extends EventEmitter {
345353
public attach(
346354
srv: http.Server | number,
347355
opts: Partial<ServerOptions> = {}
348-
): Server {
356+
): this {
349357
if ("function" == typeof srv) {
350358
const msg =
351359
"You are trying to attach socket.io to an express " +
@@ -385,7 +393,10 @@ export class Server extends EventEmitter {
385393
* @param opts - options passed to engine.io
386394
* @private
387395
*/
388-
private initEngine(srv: http.Server, opts: Partial<EngineAttachOptions>) {
396+
private initEngine(
397+
srv: http.Server,
398+
opts: Partial<EngineAttachOptions>
399+
): void {
389400
// initialize engine
390401
debug("creating engine.io instance with opts %j", opts);
391402
this.eio = engine.attach(srv, opts);
@@ -406,7 +417,7 @@ export class Server extends EventEmitter {
406417
* @param srv http server
407418
* @private
408419
*/
409-
private attachServe(srv: http.Server) {
420+
private attachServe(srv: http.Server): void {
410421
debug("attaching client serving req handler");
411422

412423
const evs = srv.listeners("request").slice(0);
@@ -429,7 +440,7 @@ export class Server extends EventEmitter {
429440
* @param res
430441
* @private
431442
*/
432-
private serve(req: http.IncomingMessage, res: http.ServerResponse) {
443+
private serve(req: http.IncomingMessage, res: http.ServerResponse): void {
433444
const filename = req.url!.replace(this._path, "");
434445
const isMap = dotMapRegex.test(filename);
435446
const type = isMap ? "map" : "source";
@@ -474,7 +485,7 @@ export class Server extends EventEmitter {
474485
filename: string,
475486
req: http.IncomingMessage,
476487
res: http.ServerResponse
477-
) {
488+
): void {
478489
const readStream = createReadStream(
479490
path.join(__dirname, "../client-dist/", filename)
480491
);
@@ -513,7 +524,7 @@ export class Server extends EventEmitter {
513524
* @return self
514525
* @public
515526
*/
516-
public bind(engine): Server {
527+
public bind(engine): this {
517528
this.engine = engine;
518529
this.engine.on("connection", this.onconnection.bind(this));
519530
return this;
@@ -526,7 +537,7 @@ export class Server extends EventEmitter {
526537
* @return self
527538
* @private
528539
*/
529-
private onconnection(conn): Server {
540+
private onconnection(conn): this {
530541
debug("incoming connection with id %s", conn.id);
531542
const client = new Client(this, conn);
532543
if (conn.protocol === 3) {
@@ -540,13 +551,13 @@ export class Server extends EventEmitter {
540551
* Looks up a namespace.
541552
*
542553
* @param {String|RegExp|Function} name nsp name
543-
* @param [fn] optional, nsp `connection` ev handler
554+
* @param fn optional, nsp `connection` ev handler
544555
* @public
545556
*/
546557
public of(
547558
name: string | RegExp | ParentNspNameMatchFn,
548559
fn?: (socket: Socket) => void
549-
) {
560+
): Namespace {
550561
if (typeof name === "function" || name instanceof RegExp) {
551562
const parentNsp = new ParentNamespace(this);
552563
debug("initializing parent namespace %s", parentNsp.name);
@@ -605,7 +616,7 @@ export class Server extends EventEmitter {
605616
*/
606617
public use(
607618
fn: (socket: Socket, next: (err?: ExtendedError) => void) => void
608-
): Server {
619+
): this {
609620
this.sockets.use(fn);
610621
return this;
611622
}
@@ -617,7 +628,7 @@ export class Server extends EventEmitter {
617628
* @return self
618629
* @public
619630
*/
620-
public to(name: Room): Server {
631+
public to(name: Room): this {
621632
this.sockets.to(name);
622633
return this;
623634
}
@@ -629,7 +640,7 @@ export class Server extends EventEmitter {
629640
* @return self
630641
* @public
631642
*/
632-
public in(name: Room): Server {
643+
public in(name: Room): this {
633644
this.sockets.in(name);
634645
return this;
635646
}
@@ -640,7 +651,7 @@ export class Server extends EventEmitter {
640651
* @return self
641652
* @public
642653
*/
643-
public send(...args: readonly any[]): Server {
654+
public send(...args: readonly any[]): this {
644655
this.sockets.emit("message", ...args);
645656
return this;
646657
}
@@ -651,7 +662,7 @@ export class Server extends EventEmitter {
651662
* @return self
652663
* @public
653664
*/
654-
public write(...args: readonly any[]): Server {
665+
public write(...args: readonly any[]): this {
655666
this.sockets.emit("message", ...args);
656667
return this;
657668
}
@@ -672,7 +683,7 @@ export class Server extends EventEmitter {
672683
* @return self
673684
* @public
674685
*/
675-
public compress(compress: boolean): Server {
686+
public compress(compress: boolean): this {
676687
this.sockets.compress(compress);
677688
return this;
678689
}
@@ -685,7 +696,7 @@ export class Server extends EventEmitter {
685696
* @return self
686697
* @public
687698
*/
688-
public get volatile(): Server {
699+
public get volatile(): this {
689700
this.sockets.volatile;
690701
return this;
691702
}
@@ -696,7 +707,7 @@ export class Server extends EventEmitter {
696707
* @return self
697708
* @public
698709
*/
699-
public get local(): Server {
710+
public get local(): this {
700711
this.sockets.local;
701712
return this;
702713
}

‎lib/namespace.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class Namespace extends EventEmitter {
6767
*/
6868
public use(
6969
fn: (socket: Socket, next: (err?: ExtendedError) => void) => void
70-
): Namespace {
70+
): this {
7171
this._fns.push(fn);
7272
return this;
7373
}
@@ -106,7 +106,7 @@ export class Namespace extends EventEmitter {
106106
* @return self
107107
* @public
108108
*/
109-
public to(name: Room): Namespace {
109+
public to(name: Room): this {
110110
this._rooms.add(name);
111111
return this;
112112
}
@@ -118,7 +118,7 @@ export class Namespace extends EventEmitter {
118118
* @return self
119119
* @public
120120
*/
121-
public in(name: Room): Namespace {
121+
public in(name: Room): this {
122122
this._rooms.add(name);
123123
return this;
124124
}
@@ -222,7 +222,7 @@ export class Namespace extends EventEmitter {
222222
* @return self
223223
* @public
224224
*/
225-
public send(...args: readonly any[]): Namespace {
225+
public send(...args: readonly any[]): this {
226226
this.emit("message", ...args);
227227
return this;
228228
}
@@ -233,7 +233,7 @@ export class Namespace extends EventEmitter {
233233
* @return self
234234
* @public
235235
*/
236-
public write(...args: readonly any[]): Namespace {
236+
public write(...args: readonly any[]): this {
237237
this.emit("message", ...args);
238238
return this;
239239
}
@@ -262,7 +262,7 @@ export class Namespace extends EventEmitter {
262262
* @return self
263263
* @public
264264
*/
265-
public compress(compress: boolean): Namespace {
265+
public compress(compress: boolean): this {
266266
this._flags.compress = compress;
267267
return this;
268268
}
@@ -275,7 +275,7 @@ export class Namespace extends EventEmitter {
275275
* @return self
276276
* @public
277277
*/
278-
public get volatile(): Namespace {
278+
public get volatile(): this {
279279
this._flags.volatile = true;
280280
return this;
281281
}
@@ -286,7 +286,7 @@ export class Namespace extends EventEmitter {
286286
* @return self
287287
* @public
288288
*/
289-
public get local(): Namespace {
289+
public get local(): this {
290290
this._flags.local = true;
291291
return this;
292292
}

‎lib/socket.ts

+34-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EventEmitter } from "events";
2-
import { PacketType } from "socket.io-parser";
2+
import { Packet, PacketType } from "socket.io-parser";
33
import url = require("url");
44
import debugModule from "debug";
55
import type { Server } from "./index";
@@ -190,7 +190,7 @@ export class Socket extends EventEmitter {
190190
* @return self
191191
* @public
192192
*/
193-
public to(name: Room): Socket {
193+
public to(name: Room): this {
194194
this._rooms.add(name);
195195
return this;
196196
}
@@ -202,7 +202,7 @@ export class Socket extends EventEmitter {
202202
* @return self
203203
* @public
204204
*/
205-
public in(name: Room): Socket {
205+
public in(name: Room): this {
206206
this._rooms.add(name);
207207
return this;
208208
}
@@ -213,7 +213,7 @@ export class Socket extends EventEmitter {
213213
* @return self
214214
* @public
215215
*/
216-
public send(...args: readonly any[]): Socket {
216+
public send(...args: readonly any[]): this {
217217
this.emit("message", ...args);
218218
return this;
219219
}
@@ -224,7 +224,7 @@ export class Socket extends EventEmitter {
224224
* @return self
225225
* @public
226226
*/
227-
public write(...args: readonly any[]): Socket {
227+
public write(...args: readonly any[]): this {
228228
this.emit("message", ...args);
229229
return this;
230230
}
@@ -236,10 +236,13 @@ export class Socket extends EventEmitter {
236236
* @param {Object} opts - options
237237
* @private
238238
*/
239-
private packet(packet, opts: any = {}) {
239+
private packet(
240+
packet: Omit<Packet, "nsp"> & Partial<Pick<Packet, "nsp">>,
241+
opts: any = {}
242+
): void {
240243
packet.nsp = this.nsp.name;
241244
opts.compress = false !== opts.compress;
242-
this.client._packet(packet, opts);
245+
this.client._packet(packet as Packet, opts);
243246
}
244247

245248
/**
@@ -304,7 +307,7 @@ export class Socket extends EventEmitter {
304307
* @param {Object} packet
305308
* @private
306309
*/
307-
_onpacket(packet) {
310+
_onpacket(packet: Packet): void {
308311
debug("got packet %j", packet);
309312
switch (packet.type) {
310313
case PacketType.EVENT:
@@ -335,10 +338,10 @@ export class Socket extends EventEmitter {
335338
/**
336339
* Called upon event packet.
337340
*
338-
* @param {Object} packet - packet object
341+
* @param {Packet} packet - packet object
339342
* @private
340343
*/
341-
private onevent(packet): void {
344+
private onevent(packet: Packet): void {
342345
const args = packet.data || [];
343346
debug("emitting event %j", args);
344347

@@ -362,7 +365,7 @@ export class Socket extends EventEmitter {
362365
* @param {Number} id - packet id
363366
* @private
364367
*/
365-
private ack(id: number) {
368+
private ack(id: number): () => void {
366369
const self = this;
367370
let sent = false;
368371
return function () {
@@ -386,12 +389,12 @@ export class Socket extends EventEmitter {
386389
*
387390
* @private
388391
*/
389-
private onack(packet): void {
390-
const ack = this.acks.get(packet.id);
392+
private onack(packet: Packet): void {
393+
const ack = this.acks.get(packet.id!);
391394
if ("function" == typeof ack) {
392395
debug("calling ack %s with %j", packet.id, packet.data);
393396
ack.apply(this, packet.data);
394-
this.acks.delete(packet.id);
397+
this.acks.delete(packet.id!);
395398
} else {
396399
debug("bad ack %s", packet.id);
397400
}
@@ -429,7 +432,7 @@ export class Socket extends EventEmitter {
429432
*
430433
* @private
431434
*/
432-
_onclose(reason: string): Socket | undefined {
435+
_onclose(reason: string): this | undefined {
433436
if (!this.connected) return this;
434437
debug("closing socket - reason %s", reason);
435438
super.emit("disconnecting", reason);
@@ -449,7 +452,7 @@ export class Socket extends EventEmitter {
449452
*
450453
* @private
451454
*/
452-
_error(err) {
455+
_error(err): void {
453456
this.packet({ type: PacketType.CONNECT_ERROR, data: err });
454457
}
455458

@@ -461,7 +464,7 @@ export class Socket extends EventEmitter {
461464
*
462465
* @public
463466
*/
464-
public disconnect(close = false): Socket {
467+
public disconnect(close = false): this {
465468
if (!this.connected) return this;
466469
if (close) {
467470
this.client._disconnect();
@@ -479,7 +482,7 @@ export class Socket extends EventEmitter {
479482
* @return {Socket} self
480483
* @public
481484
*/
482-
public compress(compress: boolean): Socket {
485+
public compress(compress: boolean): this {
483486
this.flags.compress = compress;
484487
return this;
485488
}
@@ -492,7 +495,7 @@ export class Socket extends EventEmitter {
492495
* @return {Socket} self
493496
* @public
494497
*/
495-
public get volatile(): Socket {
498+
public get volatile(): this {
496499
this.flags.volatile = true;
497500
return this;
498501
}
@@ -504,7 +507,7 @@ export class Socket extends EventEmitter {
504507
* @return {Socket} self
505508
* @public
506509
*/
507-
public get broadcast(): Socket {
510+
public get broadcast(): this {
508511
this.flags.broadcast = true;
509512
return this;
510513
}
@@ -515,7 +518,7 @@ export class Socket extends EventEmitter {
515518
* @return {Socket} self
516519
* @public
517520
*/
518-
public get local(): Socket {
521+
public get local(): this {
519522
this.flags.local = true;
520523
return this;
521524
}
@@ -526,7 +529,7 @@ export class Socket extends EventEmitter {
526529
* @param {Array} event - event that will get emitted
527530
* @private
528531
*/
529-
private dispatch(event): void {
532+
private dispatch(event: [eventName: string, ...args: any[]]): void {
530533
debug("dispatching an event %j", event);
531534
this.run(event, (err) => {
532535
process.nextTick(() => {
@@ -547,7 +550,7 @@ export class Socket extends EventEmitter {
547550
*/
548551
public use(
549552
fn: (event: Array<any>, next: (err: Error) => void) => void
550-
): Socket {
553+
): this {
551554
this.fns.push(fn);
552555
return this;
553556
}
@@ -559,11 +562,14 @@ export class Socket extends EventEmitter {
559562
* @param {Function} fn - last fn call in the middleware
560563
* @private
561564
*/
562-
private run(event: Array<any>, fn: (err: Error | null) => void) {
565+
private run(
566+
event: [eventName: string, ...args: any[]],
567+
fn: (err: Error | null) => void
568+
): void {
563569
const fns = this.fns.slice(0);
564570
if (!fns.length) return fn(null);
565571

566-
function run(i) {
572+
function run(i: number) {
567573
fns[i](event, function (err) {
568574
// upon error, short-circuit
569575
if (err) return fn(err);
@@ -611,7 +617,7 @@ export class Socket extends EventEmitter {
611617
* @param listener
612618
* @public
613619
*/
614-
public onAny(listener: (...args: any[]) => void): Socket {
620+
public onAny(listener: (...args: any[]) => void): this {
615621
this._anyListeners = this._anyListeners || [];
616622
this._anyListeners.push(listener);
617623
return this;
@@ -624,7 +630,7 @@ export class Socket extends EventEmitter {
624630
* @param listener
625631
* @public
626632
*/
627-
public prependAny(listener: (...args: any[]) => void): Socket {
633+
public prependAny(listener: (...args: any[]) => void): this {
628634
this._anyListeners = this._anyListeners || [];
629635
this._anyListeners.unshift(listener);
630636
return this;
@@ -636,7 +642,7 @@ export class Socket extends EventEmitter {
636642
* @param listener
637643
* @public
638644
*/
639-
public offAny(listener?: (...args: any[]) => void): Socket {
645+
public offAny(listener?: (...args: any[]) => void): this {
640646
if (!this._anyListeners) {
641647
return this;
642648
}

0 commit comments

Comments
 (0)
Please sign in to comment.