Skip to content

Commit 3289f7e

Browse files
committedOct 13, 2020
feat: remove the implicit connection to the default namespace
In previous versions, a client was always connected to the default namespace, even if it requested access to another namespace. This meant that the middlewares registered for the default namespace were triggered in any case, which is a surprising behavior for end users. This also meant that the query option of the Socket on the client-side was not sent in the Socket.IO CONNECT packet for the default namespace: ```js // default namespace: query sent in the query params const socket = io({ query: { abc: "def" } }); // another namespace: query sent in the query params + the CONNECT packet const socket = io("/admin", { query: { abc: "def" } }); ``` The client will now send a CONNECT packet in any case, and the query option of the Socket is renamed to "auth", in order to make a clear distinction with the query option of the Manager (included in the query parameters of the HTTP requests). ```js // server-side io.use((socket, next) => { // not triggered anymore }); io.of("/admin").use((socket, next => { // triggered console.log(socket.handshake.query.abc); // "def" console.log(socket.handshake.auth.abc); // "123" }); // client-side const socket = io("/admin", { query: { abc: "def" }, auth: { abc: "123" } }); ```
1 parent 7a51c76 commit 3289f7e

13 files changed

+85
-196
lines changed
 

‎dist/client.d.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export declare class Client {
1111
private readonly decoder;
1212
private sockets;
1313
private nsps;
14-
private connectBuffer;
1514
/**
1615
* Client constructor.
1716
*
@@ -31,16 +30,16 @@ export declare class Client {
3130
/**
3231
* Connects a client to a namespace.
3332
*
34-
* @param {String} name namespace
35-
* @param {Object} query the query parameters
33+
* @param {String} name - the namespace
34+
* @param {Object} auth - the auth parameters
3635
* @package
3736
*/
38-
connect(name: any, query?: {}): void;
37+
connect(name: string, auth?: object): void;
3938
/**
4039
* Connects a client to a namespace.
4140
*
42-
* @param {String} name namespace
43-
* @param {String} query the query parameters
41+
* @param {String} name - the namespace
42+
* @param {Object} auth - the auth parameters
4443
*/
4544
private doConnect;
4645
/**

‎dist/client.js

+11-24
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
"use strict";
2-
var __importDefault = (this && this.__importDefault) || function (mod) {
3-
return (mod && mod.__esModule) ? mod : { "default": mod };
4-
};
52
Object.defineProperty(exports, "__esModule", { value: true });
63
exports.Client = void 0;
74
const socket_io_parser_1 = require("socket.io-parser");
8-
const url_1 = __importDefault(require("url"));
95
const debugModule = require("debug");
106
const debug = debugModule("socket.io:client");
117
class Client {
@@ -19,7 +15,6 @@ class Client {
1915
constructor(server, conn) {
2016
this.sockets = new Map();
2117
this.nsps = new Map();
22-
this.connectBuffer = [];
2318
this.server = server;
2419
this.conn = conn;
2520
this.encoder = server.encoder;
@@ -50,19 +45,19 @@ class Client {
5045
/**
5146
* Connects a client to a namespace.
5247
*
53-
* @param {String} name namespace
54-
* @param {Object} query the query parameters
48+
* @param {String} name - the namespace
49+
* @param {Object} auth - the auth parameters
5550
* @package
5651
*/
57-
connect(name, query = {}) {
52+
connect(name, auth = {}) {
5853
if (this.server.nsps.has(name)) {
5954
debug("connecting to namespace %s", name);
60-
return this.doConnect(name, query);
55+
return this.doConnect(name, auth);
6156
}
62-
this.server.checkNamespace(name, query, dynamicNsp => {
57+
this.server.checkNamespace(name, auth, dynamicNsp => {
6358
if (dynamicNsp) {
6459
debug("dynamic namespace %s was created", dynamicNsp.name);
65-
this.doConnect(name, query);
60+
this.doConnect(name, auth);
6661
}
6762
else {
6863
debug("creation of namespace %s was denied", name);
@@ -77,22 +72,14 @@ class Client {
7772
/**
7873
* Connects a client to a namespace.
7974
*
80-
* @param {String} name namespace
81-
* @param {String} query the query parameters
75+
* @param {String} name - the namespace
76+
* @param {Object} auth - the auth parameters
8277
*/
83-
doConnect(name, query) {
78+
doConnect(name, auth) {
8479
const nsp = this.server.of(name);
85-
if ("/" != name && !this.nsps.has("/")) {
86-
this.connectBuffer.push(name);
87-
return;
88-
}
89-
const socket = nsp.add(this, query, () => {
80+
const socket = nsp.add(this, auth, () => {
9081
this.sockets.set(socket.id, socket);
9182
this.nsps.set(nsp.name, socket);
92-
if ("/" == nsp.name && this.connectBuffer.length > 0) {
93-
this.connectBuffer.forEach(this.connect, this);
94-
this.connectBuffer = [];
95-
}
9683
});
9784
}
9885
/**
@@ -182,7 +169,7 @@ class Client {
182169
*/
183170
ondecoded(packet) {
184171
if (socket_io_parser_1.PacketType.CONNECT == packet.type) {
185-
this.connect(url_1.default.parse(packet.nsp).pathname, url_1.default.parse(packet.nsp, true).query);
172+
this.connect(packet.nsp, packet.data);
186173
}
187174
else {
188175
const socket = this.nsps.get(packet.nsp);

‎dist/index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ declare class Server extends EventEmitter {
149149
* Executes the middleware for an incoming namespace not already created on the server.
150150
*
151151
* @param {String} name - name of incoming namespace
152-
* @param {Object} query - the query parameters
152+
* @param {Object} auth - the auth parameters
153153
* @param {Function} fn - callback
154154
*
155155
* @package
156156
*/
157-
checkNamespace(name: string, query: object, fn: (nsp: Namespace | boolean) => void): void;
157+
checkNamespace(name: string, auth: object, fn: (nsp: Namespace | boolean) => void): void;
158158
/**
159159
* Sets the client serving path.
160160
*

‎dist/index.js

+4-14
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const parent_namespace_1 = require("./parent-namespace");
3939
Object.defineProperty(exports, "ParentNamespace", { enumerable: true, get: function () { return parent_namespace_1.ParentNamespace; } });
4040
const socket_io_adapter_1 = require("socket.io-adapter");
4141
const parser = __importStar(require("socket.io-parser"));
42-
const socket_io_parser_1 = require("socket.io-parser");
4342
const url_1 = __importDefault(require("url"));
4443
const debug_1 = __importDefault(require("debug"));
4544
const debug = debug_1.default("socket.io:server");
@@ -131,12 +130,12 @@ class Server extends events_1.EventEmitter {
131130
* Executes the middleware for an incoming namespace not already created on the server.
132131
*
133132
* @param {String} name - name of incoming namespace
134-
* @param {Object} query - the query parameters
133+
* @param {Object} auth - the auth parameters
135134
* @param {Function} fn - callback
136135
*
137136
* @package
138137
*/
139-
checkNamespace(name, query, fn) {
138+
checkNamespace(name, auth, fn) {
140139
if (this.parentNsps.size === 0)
141140
return fn(false);
142141
const keysIterator = this.parentNsps.keys();
@@ -145,7 +144,7 @@ class Server extends events_1.EventEmitter {
145144
if (nextFn.done) {
146145
return fn(false);
147146
}
148-
nextFn.value(name, query, (err, allow) => {
147+
nextFn.value(name, auth, (err, allow) => {
149148
if (err || !allow) {
150149
run();
151150
}
@@ -221,14 +220,6 @@ class Server extends events_1.EventEmitter {
221220
opts.path = opts.path || this._path;
222221
// set origins verification
223222
opts.allowRequest = opts.allowRequest || this.checkRequest.bind(this);
224-
if (this.sockets.fns.length > 0) {
225-
this.initEngine(srv, opts);
226-
return this;
227-
}
228-
const connectPacket = { type: socket_io_parser_1.PacketType.CONNECT, nsp: "/" };
229-
// the CONNECT packet will be merged with Engine.IO handshake,
230-
// to reduce the number of round trips
231-
opts.initialPacket = this.encoder.encode(connectPacket);
232223
this.initEngine(srv, opts);
233224
return this;
234225
}
@@ -346,8 +337,7 @@ class Server extends events_1.EventEmitter {
346337
*/
347338
onconnection(conn) {
348339
debug("incoming connection with id %s", conn.id);
349-
const client = new client_1.Client(this, conn);
350-
client.connect("/");
340+
new client_1.Client(this, conn);
351341
return this;
352342
}
353343
/**

‎dist/namespace.js

-4
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ class Namespace extends events_1.EventEmitter {
5858
* @return {Namespace} self
5959
*/
6060
use(fn) {
61-
if (this.server.eio && this.name === "/") {
62-
debug("removing initial packet");
63-
delete this.server.eio.opts.initialPacket;
64-
}
6561
this.fns.push(fn);
6662
return this;
6763
}

‎dist/socket.d.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export interface Handshake {
3939
* The query object
4040
*/
4141
query: object;
42+
/**
43+
* The auth object
44+
*/
45+
auth: object;
4246
}
4347
export declare class Socket extends EventEmitter {
4448
readonly nsp: Namespace;
@@ -58,10 +62,10 @@ export declare class Socket extends EventEmitter {
5862
*
5963
* @param {Namespace} nsp
6064
* @param {Client} client
61-
* @param {Object} query
65+
* @param {Object} auth
6266
* @package
6367
*/
64-
constructor(nsp: Namespace, client: Client, query: any);
68+
constructor(nsp: Namespace, client: Client, auth: object);
6569
/**
6670
* Builds the `handshake` BC object
6771
*/

‎dist/socket.js

+7-18
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ class Socket extends events_1.EventEmitter {
2727
*
2828
* @param {Namespace} nsp
2929
* @param {Client} client
30-
* @param {Object} query
30+
* @param {Object} auth
3131
* @package
3232
*/
33-
constructor(nsp, client, query) {
33+
constructor(nsp, client, auth) {
3434
super();
3535
this.nsp = nsp;
3636
this.client = client;
@@ -43,18 +43,12 @@ class Socket extends events_1.EventEmitter {
4343
this.id = nsp.name !== "/" ? nsp.name + "#" + client.id : client.id;
4444
this.connected = true;
4545
this.disconnected = false;
46-
this.handshake = this.buildHandshake(query);
46+
this.handshake = this.buildHandshake(auth);
4747
}
4848
/**
4949
* Builds the `handshake` BC object
5050
*/
51-
buildHandshake(query) {
52-
const self = this;
53-
function buildQuery() {
54-
const requestQuery = url_1.default.parse(self.request.url, true).query;
55-
//if socket-specific query exist, replace query strings in requestQuery
56-
return Object.assign({}, query, requestQuery);
57-
}
51+
buildHandshake(auth) {
5852
return {
5953
headers: this.request.headers,
6054
time: new Date() + "",
@@ -64,7 +58,8 @@ class Socket extends events_1.EventEmitter {
6458
secure: !!this.request.connection.encrypted,
6559
issued: +new Date(),
6660
url: this.request.url,
67-
query: buildQuery()
61+
query: url_1.default.parse(this.request.url, true).query,
62+
auth
6863
};
6964
}
7065
/**
@@ -211,13 +206,7 @@ class Socket extends events_1.EventEmitter {
211206
debug("socket connected - writing packet");
212207
this.nsp.connected.set(this.id, this);
213208
this.join(this.id);
214-
const skip = this.nsp.name === "/" && this.nsp.fns.length === 0;
215-
if (skip) {
216-
debug("packet already sent in initial handshake");
217-
}
218-
else {
219-
this.packet({ type: socket_io_parser_1.PacketType.CONNECT });
220-
}
209+
this.packet({ type: socket_io_parser_1.PacketType.CONNECT });
221210
}
222211
/**
223212
* Called with each packet. Called by `Client`.

‎lib/client.ts

+13-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { Decoder, Encoder, PacketType } from "socket.io-parser";
2-
import url from "url";
1+
import { Decoder, Encoder, Packet, PacketType } from "socket.io-parser";
32
import debugModule = require("debug");
43
import { IncomingMessage } from "http";
54
import { Server } from "./index";
@@ -18,7 +17,6 @@ export class Client {
1817
private readonly decoder: Decoder;
1918
private sockets: Map<SocketId, Socket> = new Map();
2019
private nsps: Map<string, Socket> = new Map();
21-
private connectBuffer: Array<string> = [];
2220

2321
/**
2422
* Client constructor.
@@ -62,20 +60,20 @@ export class Client {
6260
/**
6361
* Connects a client to a namespace.
6462
*
65-
* @param {String} name namespace
66-
* @param {Object} query the query parameters
63+
* @param {String} name - the namespace
64+
* @param {Object} auth - the auth parameters
6765
* @package
6866
*/
69-
public connect(name, query = {}) {
67+
public connect(name: string, auth: object = {}) {
7068
if (this.server.nsps.has(name)) {
7169
debug("connecting to namespace %s", name);
72-
return this.doConnect(name, query);
70+
return this.doConnect(name, auth);
7371
}
7472

75-
this.server.checkNamespace(name, query, dynamicNsp => {
73+
this.server.checkNamespace(name, auth, dynamicNsp => {
7674
if (dynamicNsp) {
7775
debug("dynamic namespace %s was created", dynamicNsp.name);
78-
this.doConnect(name, query);
76+
this.doConnect(name, auth);
7977
} else {
8078
debug("creation of namespace %s was denied", name);
8179
this.packet({
@@ -90,25 +88,15 @@ export class Client {
9088
/**
9189
* Connects a client to a namespace.
9290
*
93-
* @param {String} name namespace
94-
* @param {String} query the query parameters
91+
* @param {String} name - the namespace
92+
* @param {Object} auth - the auth parameters
9593
*/
96-
private doConnect(name, query) {
94+
private doConnect(name: string, auth: object) {
9795
const nsp = this.server.of(name);
9896

99-
if ("/" != name && !this.nsps.has("/")) {
100-
this.connectBuffer.push(name);
101-
return;
102-
}
103-
104-
const socket = nsp.add(this, query, () => {
97+
const socket = nsp.add(this, auth, () => {
10598
this.sockets.set(socket.id, socket);
10699
this.nsps.set(nsp.name, socket);
107-
108-
if ("/" == nsp.name && this.connectBuffer.length > 0) {
109-
this.connectBuffer.forEach(this.connect, this);
110-
this.connectBuffer = [];
111-
}
112100
});
113101
}
114102

@@ -199,12 +187,9 @@ export class Client {
199187
/**
200188
* Called when parser fully decodes a packet.
201189
*/
202-
private ondecoded(packet) {
190+
private ondecoded(packet: Packet) {
203191
if (PacketType.CONNECT == packet.type) {
204-
this.connect(
205-
url.parse(packet.nsp).pathname,
206-
url.parse(packet.nsp, true).query
207-
);
192+
this.connect(packet.nsp, packet.data);
208193
} else {
209194
const socket = this.nsps.get(packet.nsp);
210195
if (socket) {

0 commit comments

Comments
 (0)
Please sign in to comment.