Skip to content

Commit 249e0be

Browse files
feat: remove the implicit connection to the default namespace
Related: socketio/socket.io@09b6f23
1 parent be8c314 commit 249e0be

File tree

5 files changed

+47
-43
lines changed

5 files changed

+47
-43
lines changed

build/socket.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export declare class Socket extends Emitter {
66
connected: boolean;
77
disconnected: boolean;
88
private readonly nsp;
9-
private readonly query;
9+
private readonly auth;
1010
private ids;
1111
private acks;
1212
private receiveBuffer;

build/socket.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const component_emitter_1 = __importDefault(require("component-emitter"));
99
const to_array_1 = __importDefault(require("to-array"));
1010
const on_1 = require("./on");
1111
const component_bind_1 = __importDefault(require("component-bind"));
12-
const parseqs_1 = __importDefault(require("parseqs"));
1312
const has_binary2_1 = __importDefault(require("has-binary2"));
1413
const debug = require("debug")("socket.io-client:socket");
1514
/**
@@ -55,8 +54,8 @@ class Socket extends component_emitter_1.default {
5554
this.connected = false;
5655
this.disconnected = true;
5756
this.flags = {};
58-
if (opts && opts.query) {
59-
this.query = opts.query;
57+
if (opts && opts.auth) {
58+
this.auth = opts.auth;
6059
}
6160
if (this.io.autoConnect)
6261
this.open();
@@ -169,18 +168,13 @@ class Socket extends component_emitter_1.default {
169168
*/
170169
onopen() {
171170
debug("transport is open - connecting");
172-
// write connect packet if necessary
173-
if ("/" !== this.nsp) {
174-
if (this.query) {
175-
const query = typeof this.query === "object"
176-
? parseqs_1.default.encode(this.query)
177-
: this.query;
178-
debug("sending connect packet with query %s", query);
179-
this.packet({ type: socket_io_parser_1.PacketType.CONNECT, query: query });
180-
}
181-
else {
182-
this.packet({ type: socket_io_parser_1.PacketType.CONNECT });
183-
}
171+
if (typeof this.auth == "function") {
172+
this.auth((data) => {
173+
this.packet({ type: socket_io_parser_1.PacketType.CONNECT, data });
174+
});
175+
}
176+
else {
177+
this.packet({ type: socket_io_parser_1.PacketType.CONNECT, data: this.auth });
184178
}
185179
}
186180
/**

lib/socket.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import Emitter from "component-emitter";
33
import toArray from "to-array";
44
import { on } from "./on";
55
import bind from "component-bind";
6-
import parseqs from "parseqs";
76
import hasBin from "has-binary2";
87
import { Manager } from "./manager";
98

@@ -40,7 +39,7 @@ export class Socket extends Emitter {
4039
public disconnected: boolean;
4140

4241
private readonly nsp: string;
43-
private readonly query: string | object;
42+
private readonly auth: object | ((cb: (data: object) => void) => void);
4443

4544
private ids: number = 0;
4645
private acks: object = {};
@@ -65,8 +64,8 @@ export class Socket extends Emitter {
6564
this.connected = false;
6665
this.disconnected = true;
6766
this.flags = {};
68-
if (opts && opts.query) {
69-
this.query = opts.query;
67+
if (opts && opts.auth) {
68+
this.auth = opts.auth;
7069
}
7170
if (this.io.autoConnect) this.open();
7271
}
@@ -186,19 +185,12 @@ export class Socket extends Emitter {
186185
*/
187186
onopen() {
188187
debug("transport is open - connecting");
189-
190-
// write connect packet if necessary
191-
if ("/" !== this.nsp) {
192-
if (this.query) {
193-
const query =
194-
typeof this.query === "object"
195-
? parseqs.encode(this.query)
196-
: this.query;
197-
debug("sending connect packet with query %s", query);
198-
this.packet({ type: PacketType.CONNECT, query: query });
199-
} else {
200-
this.packet({ type: PacketType.CONNECT });
201-
}
188+
if (typeof this.auth == "function") {
189+
this.auth((data) => {
190+
this.packet({ type: PacketType.CONNECT, data });
191+
});
192+
} else {
193+
this.packet({ type: PacketType.CONNECT, data: this.auth });
202194
}
203195
}
204196

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"engine.io-client": "~4.0.0",
2424
"has-binary2": "~1.0.2",
2525
"indexof": "0.0.1",
26-
"parseqs": "0.0.6",
2726
"parseuri": "0.0.6",
2827
"socket.io-parser": "github:socketio/socket.io-parser#develop",
2928
"to-array": "0.1.4"

test/socket.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ describe("socket", function () {
112112
});
113113

114114
it("should accept an object", (done) => {
115-
const socket = io("/abc", { query: { a: "b" } });
115+
const socket = io("/abc", { forceNew: true, query: { a: "b" } });
116116

117117
socket.on("handshake", (handshake) => {
118118
expect(handshake.query.a).to.be("b");
@@ -122,7 +122,7 @@ describe("socket", function () {
122122
});
123123

124124
it("should accept a query string", (done) => {
125-
const socket = io("/abc?b=c&d=e");
125+
const socket = io("/abc?b=c&d=e", { forceNew: true });
126126

127127
socket.on("handshake", (handshake) => {
128128
expect(handshake.query.b).to.be("c");
@@ -133,7 +133,7 @@ describe("socket", function () {
133133
});
134134

135135
it("should properly encode the parameters", (done) => {
136-
const socket = io("/abc", { query: { "&a": "&=?a" } });
136+
const socket = io("/abc", { forceNew: true, query: { "&a": "&=?a" } });
137137

138138
socket.on("handshake", (handshake) => {
139139
expect(handshake.query["&a"]).to.be("&=?a");
@@ -143,12 +143,31 @@ describe("socket", function () {
143143
});
144144
});
145145

146-
it("should fire an error event on middleware failure from main namespace", (done) => {
147-
const socket = io("/foo", { forceNew: true, query: { fail: true } });
148-
socket.on("error", (err) => {
149-
expect(err).to.eql("Auth failed (main namespace)");
150-
socket.disconnect();
151-
done();
146+
describe("auth option", () => {
147+
it("should accept an object", (done) => {
148+
const socket = io("/abc", { forceNew: true, auth: { a: "b", c: "d" } });
149+
150+
socket.on("handshake", (handshake) => {
151+
expect(handshake.auth.a).to.be("b");
152+
expect(handshake.auth.c).to.be("d");
153+
expect(handshake.query.a).to.be(undefined);
154+
socket.disconnect();
155+
done();
156+
});
157+
});
158+
159+
it("should accept an function", (done) => {
160+
const socket = io("/abc", {
161+
forceNew: true,
162+
auth: (cb) => cb({ e: "f" }),
163+
});
164+
165+
socket.on("handshake", (handshake) => {
166+
expect(handshake.auth.e).to.be("f");
167+
expect(handshake.query.e).to.be(undefined);
168+
socket.disconnect();
169+
done();
170+
});
152171
});
153172
});
154173

0 commit comments

Comments
 (0)