Skip to content

Commit 58b66f8

Browse files
refactor: hide internal methods and properties
There is no concept of package-private methods in TypeScript, so we'll just prefix them with "_" and mark them as private in the JSDoc.
1 parent 669592d commit 58b66f8

File tree

6 files changed

+198
-103
lines changed

6 files changed

+198
-103
lines changed

lib/client.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ const debug = debugModule("socket.io:client");
99

1010
export class Client {
1111
public readonly conn;
12-
/** @package */
13-
public readonly id: string;
1412

13+
private readonly id: string;
1514
private readonly server;
1615
private readonly encoder: Encoder;
1716
private readonly decoder: Decoder;
@@ -29,20 +28,24 @@ export class Client {
2928
this.server = server;
3029
this.conn = conn;
3130
this.encoder = server.encoder;
32-
this.decoder = new server.parser.Decoder();
31+
this.decoder = new server._parser.Decoder();
3332
this.id = conn.id;
3433
this.setup();
3534
}
3635

3736
/**
3837
* @return the reference to the request that originated the Engine.IO connection
38+
*
39+
* @public
3940
*/
4041
public get request(): IncomingMessage {
4142
return this.conn.request;
4243
}
4344

4445
/**
4546
* Sets up event listeners.
47+
*
48+
* @private
4649
*/
4750
private setup() {
4851
this.onclose = this.onclose.bind(this);
@@ -62,21 +65,21 @@ export class Client {
6265
*
6366
* @param {String} name - the namespace
6467
* @param {Object} auth - the auth parameters
65-
* @package
68+
* @private
6669
*/
67-
public connect(name: string, auth: object = {}) {
68-
if (this.server.nsps.has(name)) {
70+
private connect(name: string, auth: object = {}) {
71+
if (this.server._nsps.has(name)) {
6972
debug("connecting to namespace %s", name);
7073
return this.doConnect(name, auth);
7174
}
7275

73-
this.server.checkNamespace(name, auth, dynamicNsp => {
76+
this.server._checkNamespace(name, auth, dynamicNsp => {
7477
if (dynamicNsp) {
7578
debug("dynamic namespace %s was created", dynamicNsp.name);
7679
this.doConnect(name, auth);
7780
} else {
7881
debug("creation of namespace %s was denied", name);
79-
this.packet({
82+
this._packet({
8083
type: PacketType.ERROR,
8184
nsp: name,
8285
data: "Invalid namespace"
@@ -90,6 +93,8 @@ export class Client {
9093
*
9194
* @param {String} name - the namespace
9295
* @param {Object} auth - the auth parameters
96+
*
97+
* @private
9398
*/
9499
private doConnect(name: string, auth: object) {
95100
const nsp = this.server.of(name);
@@ -103,9 +108,9 @@ export class Client {
103108
/**
104109
* Disconnects from all namespaces and closes transport.
105110
*
106-
* @package
111+
* @private
107112
*/
108-
public disconnect() {
113+
_disconnect() {
109114
for (const socket of this.sockets.values()) {
110115
socket.disconnect();
111116
}
@@ -116,9 +121,9 @@ export class Client {
116121
/**
117122
* Removes a socket. Called by each `Socket`.
118123
*
119-
* @package
124+
* @private
120125
*/
121-
public remove(socket: Socket) {
126+
_remove(socket: Socket) {
122127
if (this.sockets.has(socket.id)) {
123128
const nsp = this.sockets.get(socket.id).nsp.name;
124129
this.sockets.delete(socket.id);
@@ -130,6 +135,8 @@ export class Client {
130135

131136
/**
132137
* Closes the underlying connection.
138+
*
139+
* @private
133140
*/
134141
private close() {
135142
if ("open" == this.conn.readyState) {
@@ -144,9 +151,9 @@ export class Client {
144151
*
145152
* @param {Object} packet object
146153
* @param {Object} opts
147-
* @package
154+
* @private
148155
*/
149-
public packet(packet, opts?) {
156+
_packet(packet, opts?) {
150157
opts = opts || {};
151158
const self = this;
152159

@@ -174,6 +181,8 @@ export class Client {
174181

175182
/**
176183
* Called with incoming transport data.
184+
*
185+
* @private
177186
*/
178187
private ondata(data) {
179188
// try/catch is needed for protocol violations (GH-1880)
@@ -186,6 +195,8 @@ export class Client {
186195

187196
/**
188197
* Called when parser fully decodes a packet.
198+
*
199+
* @private
189200
*/
190201
private ondecoded(packet: Packet) {
191202
if (PacketType.CONNECT == packet.type) {
@@ -194,7 +205,7 @@ export class Client {
194205
const socket = this.nsps.get(packet.nsp);
195206
if (socket) {
196207
process.nextTick(function() {
197-
socket.onpacket(packet);
208+
socket._onpacket(packet);
198209
});
199210
} else {
200211
debug("no socket for namespace %s", packet.nsp);
@@ -206,10 +217,11 @@ export class Client {
206217
* Handles an error.
207218
*
208219
* @param {Object} err object
220+
* @private
209221
*/
210222
private onerror(err) {
211223
for (const socket of this.sockets.values()) {
212-
socket.onerror(err);
224+
socket._onerror(err);
213225
}
214226
this.conn.close();
215227
}
@@ -218,6 +230,7 @@ export class Client {
218230
* Called upon transport close.
219231
*
220232
* @param reason
233+
* @private
221234
*/
222235
private onclose(reason: string) {
223236
debug("client close with reason %s", reason);
@@ -227,7 +240,7 @@ export class Client {
227240

228241
// `nsps` and `sockets` are cleaned up seamlessly
229242
for (const socket of this.sockets.values()) {
230-
socket.onclose(reason);
243+
socket._onclose(reason);
231244
}
232245
this.sockets.clear();
233246

@@ -236,6 +249,7 @@ export class Client {
236249

237250
/**
238251
* Cleans up event listeners.
252+
* @private
239253
*/
240254
private destroy() {
241255
this.conn.removeListener("data", this.ondata);

0 commit comments

Comments
 (0)