Skip to content

Commit a5581a9

Browse files
refactor: migrate to TypeScript
1 parent af165ae commit a5581a9

File tree

10 files changed

+462
-382
lines changed

10 files changed

+462
-382
lines changed

.eslintrc.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

lib/client.js renamed to lib/client.ts

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,51 @@
1-
const parser = require("socket.io-parser");
2-
const debug = require("debug")("socket.io:client");
3-
const url = require("url");
1+
import parser from "socket.io-parser";
2+
import url from "url";
3+
import debugModule = require("debug");
4+
import { IncomingMessage } from "http";
5+
import { Server } from "./index";
6+
import { Socket } from "./socket";
7+
8+
const debug = debugModule("socket.io:client");
9+
10+
export class Client {
11+
public readonly conn;
12+
/** @package */
13+
public readonly id: string;
14+
15+
private readonly server;
16+
private readonly encoder;
17+
private readonly decoder;
18+
private sockets: object = {};
19+
private nsps: object = {};
20+
private connectBuffer: Array<string> = [];
421

5-
class Client {
622
/**
723
* Client constructor.
824
*
925
* @param {Server} server instance
1026
* @param {Socket} conn
11-
* @api private
27+
* @package
1228
*/
13-
constructor(server, conn) {
29+
constructor(server: Server, conn) {
1430
this.server = server;
1531
this.conn = conn;
1632
this.encoder = server.encoder;
1733
this.decoder = new server.parser.Decoder();
1834
this.id = conn.id;
19-
this.request = conn.request;
2035
this.setup();
21-
this.sockets = {};
22-
this.nsps = {};
23-
this.connectBuffer = [];
36+
}
37+
38+
/**
39+
* @return the reference to the request that originated the Engine.IO connection
40+
*/
41+
public get request(): IncomingMessage {
42+
return this.conn.request;
2443
}
2544

2645
/**
2746
* Sets up event listeners.
28-
*
29-
* @api private
3047
*/
31-
setup() {
48+
private setup() {
3249
this.onclose = this.onclose.bind(this);
3350
this.ondata = this.ondata.bind(this);
3451
this.onerror = this.onerror.bind(this);
@@ -45,9 +62,9 @@ class Client {
4562
*
4663
* @param {String} name namespace
4764
* @param {Object} query the query parameters
48-
* @api private
65+
* @package
4966
*/
50-
connect(name, query) {
67+
public connect(name, query = {}) {
5168
if (this.server.nsps[name]) {
5269
debug("connecting to namespace %s", name);
5370
return this.doConnect(name, query);
@@ -73,9 +90,8 @@ class Client {
7390
*
7491
* @param {String} name namespace
7592
* @param {String} query the query parameters
76-
* @api private
7793
*/
78-
doConnect(name, query) {
94+
private doConnect(name, query) {
7995
const nsp = this.server.of(name);
8096

8197
if ("/" != name && !this.nsps["/"]) {
@@ -98,9 +114,9 @@ class Client {
98114
/**
99115
* Disconnects from all namespaces and closes transport.
100116
*
101-
* @api private
117+
* @package
102118
*/
103-
disconnect() {
119+
public disconnect() {
104120
for (const id in this.sockets) {
105121
if (this.sockets.hasOwnProperty(id)) {
106122
this.sockets[id].disconnect();
@@ -113,9 +129,9 @@ class Client {
113129
/**
114130
* Removes a socket. Called by each `Socket`.
115131
*
116-
* @api private
132+
* @package
117133
*/
118-
remove(socket) {
134+
public remove(socket: Socket) {
119135
if (this.sockets.hasOwnProperty(socket.id)) {
120136
const nsp = this.sockets[socket.id].nsp.name;
121137
delete this.sockets[socket.id];
@@ -127,10 +143,8 @@ class Client {
127143

128144
/**
129145
* Closes the underlying connection.
130-
*
131-
* @api private
132146
*/
133-
close() {
147+
private close() {
134148
if ("open" == this.conn.readyState) {
135149
debug("forcing transport close");
136150
this.conn.close();
@@ -143,9 +157,9 @@ class Client {
143157
*
144158
* @param {Object} packet object
145159
* @param {Object} opts
146-
* @api private
160+
* @package
147161
*/
148-
packet(packet, opts) {
162+
public packet(packet, opts?) {
149163
opts = opts || {};
150164
const self = this;
151165

@@ -173,10 +187,8 @@ class Client {
173187

174188
/**
175189
* Called with incoming transport data.
176-
*
177-
* @api private
178190
*/
179-
ondata(data) {
191+
private ondata(data) {
180192
// try/catch is needed for protocol violations (GH-1880)
181193
try {
182194
this.decoder.add(data);
@@ -187,10 +199,8 @@ class Client {
187199

188200
/**
189201
* Called when parser fully decodes a packet.
190-
*
191-
* @api private
192202
*/
193-
ondecoded(packet) {
203+
private ondecoded(packet) {
194204
if (parser.CONNECT == packet.type) {
195205
this.connect(
196206
url.parse(packet.nsp).pathname,
@@ -212,9 +222,8 @@ class Client {
212222
* Handles an error.
213223
*
214224
* @param {Object} err object
215-
* @api private
216225
*/
217-
onerror(err) {
226+
private onerror(err) {
218227
for (const id in this.sockets) {
219228
if (this.sockets.hasOwnProperty(id)) {
220229
this.sockets[id].onerror(err);
@@ -226,10 +235,9 @@ class Client {
226235
/**
227236
* Called upon transport close.
228237
*
229-
* @param {String} reason
230-
* @api private
238+
* @param reason
231239
*/
232-
onclose(reason) {
240+
private onclose(reason: string) {
233241
debug("client close with reason %s", reason);
234242

235243
// ignore a potential subsequent `close` event
@@ -248,15 +256,11 @@ class Client {
248256

249257
/**
250258
* Cleans up event listeners.
251-
*
252-
* @api private
253259
*/
254-
destroy() {
260+
private destroy() {
255261
this.conn.removeListener("data", this.ondata);
256262
this.conn.removeListener("error", this.onerror);
257263
this.conn.removeListener("close", this.onclose);
258264
this.decoder.removeListener("decoded", this.ondecoded);
259265
}
260266
}
261-
262-
module.exports = Client;

0 commit comments

Comments
 (0)