Skip to content

Commit

Permalink
refactor: add more typing info and upgrade prettier (#3725)
Browse files Browse the repository at this point in the history
This upgrades prettier to 2.2.0 to gain support for TypeScript's new
type-only-imports feature.
  • Loading branch information
david-fong committed Dec 11, 2020
1 parent 81c1f4e commit d1bfe40
Show file tree
Hide file tree
Showing 10 changed files with 483 additions and 476 deletions.
57 changes: 30 additions & 27 deletions lib/client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Decoder, Encoder, Packet, PacketType } from "socket.io-parser";
import debugModule = require("debug");
import { IncomingMessage } from "http";
import { Server } from "./index";
import { Socket } from "./socket";
import { SocketId } from "socket.io-adapter";
import { ParentNamespace } from "./parent-namespace";
import type { IncomingMessage } from "http";
import type { Namespace, Server } from "./index";
import type { Socket } from "./socket";
import type { SocketId } from "socket.io-adapter";

const debug = debugModule("socket.io:client");

Expand All @@ -17,16 +16,16 @@ export class Client {
private readonly decoder: Decoder;
private sockets: Map<SocketId, Socket> = new Map();
private nsps: Map<string, Socket> = new Map();
private connectTimeout: NodeJS.Timeout;
private connectTimeout?: NodeJS.Timeout;

/**
* Client constructor.
*
* @param {Server} server instance
* @param {Socket} conn
* @param server instance
* @param conn
* @package
*/
constructor(server: Server, conn) {
constructor(server: Server, conn: Socket) {
this.server = server;
this.conn = conn;
this.encoder = server.encoder;
Expand Down Expand Up @@ -84,35 +83,39 @@ export class Client {
return this.doConnect(name, auth);
}

this.server._checkNamespace(name, auth, (dynamicNsp: ParentNamespace) => {
if (dynamicNsp) {
debug("dynamic namespace %s was created", dynamicNsp.name);
this.doConnect(name, auth);
} else {
debug("creation of namespace %s was denied", name);
this._packet({
type: PacketType.CONNECT_ERROR,
nsp: name,
data: {
message: "Invalid namespace"
}
});
this.server._checkNamespace(
name,
auth,
(dynamicNspName: Namespace | false) => {
if (dynamicNspName) {
debug("dynamic namespace %s was created", dynamicNspName);
this.doConnect(name, auth);
} else {
debug("creation of namespace %s was denied", name);
this._packet({
type: PacketType.CONNECT_ERROR,
nsp: name,
data: {
message: "Invalid namespace",
},
});
}
}
});
);
}

/**
* Connects a client to a namespace.
*
* @param {String} name - the namespace
* @param name - the namespace
* @param {Object} auth - the auth parameters
*
* @private
*/
private doConnect(name: string, auth: object) {
if (this.connectTimeout) {
clearTimeout(this.connectTimeout);
this.connectTimeout = null;
this.connectTimeout = undefined;
}
const nsp = this.server.of(name);

Expand Down Expand Up @@ -142,7 +145,7 @@ export class Client {
*/
_remove(socket: Socket) {
if (this.sockets.has(socket.id)) {
const nsp = this.sockets.get(socket.id).nsp.name;
const nsp = this.sockets.get(socket.id)!.nsp.name;
this.sockets.delete(socket.id);
this.nsps.delete(nsp);
} else {
Expand Down Expand Up @@ -221,7 +224,7 @@ export class Client {
} else {
const socket = this.nsps.get(packet.nsp);
if (socket) {
process.nextTick(function() {
process.nextTick(function () {
socket._onpacket(packet);
});
} else {
Expand Down
Loading

0 comments on commit d1bfe40

Please sign in to comment.