Skip to content

Commit

Permalink
feat: remove the implicit connection to the default namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Oct 8, 2020
1 parent be8c314 commit 249e0be
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 43 deletions.
2 changes: 1 addition & 1 deletion build/socket.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export declare class Socket extends Emitter {
connected: boolean;
disconnected: boolean;
private readonly nsp;
private readonly query;
private readonly auth;
private ids;
private acks;
private receiveBuffer;
Expand Down
24 changes: 9 additions & 15 deletions build/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const component_emitter_1 = __importDefault(require("component-emitter"));
const to_array_1 = __importDefault(require("to-array"));
const on_1 = require("./on");
const component_bind_1 = __importDefault(require("component-bind"));
const parseqs_1 = __importDefault(require("parseqs"));
const has_binary2_1 = __importDefault(require("has-binary2"));
const debug = require("debug")("socket.io-client:socket");
/**
Expand Down Expand Up @@ -55,8 +54,8 @@ class Socket extends component_emitter_1.default {
this.connected = false;
this.disconnected = true;
this.flags = {};
if (opts && opts.query) {
this.query = opts.query;
if (opts && opts.auth) {
this.auth = opts.auth;
}
if (this.io.autoConnect)
this.open();
Expand Down Expand Up @@ -169,18 +168,13 @@ class Socket extends component_emitter_1.default {
*/
onopen() {
debug("transport is open - connecting");
// write connect packet if necessary
if ("/" !== this.nsp) {
if (this.query) {
const query = typeof this.query === "object"
? parseqs_1.default.encode(this.query)
: this.query;
debug("sending connect packet with query %s", query);
this.packet({ type: socket_io_parser_1.PacketType.CONNECT, query: query });
}
else {
this.packet({ type: socket_io_parser_1.PacketType.CONNECT });
}
if (typeof this.auth == "function") {
this.auth((data) => {
this.packet({ type: socket_io_parser_1.PacketType.CONNECT, data });
});
}
else {
this.packet({ type: socket_io_parser_1.PacketType.CONNECT, data: this.auth });
}
}
/**
Expand Down
26 changes: 9 additions & 17 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Emitter from "component-emitter";
import toArray from "to-array";
import { on } from "./on";
import bind from "component-bind";
import parseqs from "parseqs";
import hasBin from "has-binary2";
import { Manager } from "./manager";

Expand Down Expand Up @@ -40,7 +39,7 @@ export class Socket extends Emitter {
public disconnected: boolean;

private readonly nsp: string;
private readonly query: string | object;
private readonly auth: object | ((cb: (data: object) => void) => void);

private ids: number = 0;
private acks: object = {};
Expand All @@ -65,8 +64,8 @@ export class Socket extends Emitter {
this.connected = false;
this.disconnected = true;
this.flags = {};
if (opts && opts.query) {
this.query = opts.query;
if (opts && opts.auth) {
this.auth = opts.auth;
}
if (this.io.autoConnect) this.open();
}
Expand Down Expand Up @@ -186,19 +185,12 @@ export class Socket extends Emitter {
*/
onopen() {
debug("transport is open - connecting");

// write connect packet if necessary
if ("/" !== this.nsp) {
if (this.query) {
const query =
typeof this.query === "object"
? parseqs.encode(this.query)
: this.query;
debug("sending connect packet with query %s", query);
this.packet({ type: PacketType.CONNECT, query: query });
} else {
this.packet({ type: PacketType.CONNECT });
}
if (typeof this.auth == "function") {
this.auth((data) => {
this.packet({ type: PacketType.CONNECT, data });
});
} else {
this.packet({ type: PacketType.CONNECT, data: this.auth });
}
}

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"engine.io-client": "~4.0.0",
"has-binary2": "~1.0.2",
"indexof": "0.0.1",
"parseqs": "0.0.6",
"parseuri": "0.0.6",
"socket.io-parser": "github:socketio/socket.io-parser#develop",
"to-array": "0.1.4"
Expand Down
37 changes: 28 additions & 9 deletions test/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe("socket", function () {
});

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

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

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

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

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

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

it("should fire an error event on middleware failure from main namespace", (done) => {
const socket = io("/foo", { forceNew: true, query: { fail: true } });
socket.on("error", (err) => {
expect(err).to.eql("Auth failed (main namespace)");
socket.disconnect();
done();
describe("auth option", () => {
it("should accept an object", (done) => {
const socket = io("/abc", { forceNew: true, auth: { a: "b", c: "d" } });

socket.on("handshake", (handshake) => {
expect(handshake.auth.a).to.be("b");
expect(handshake.auth.c).to.be("d");
expect(handshake.query.a).to.be(undefined);
socket.disconnect();
done();
});
});

it("should accept an function", (done) => {
const socket = io("/abc", {
forceNew: true,
auth: (cb) => cb({ e: "f" }),
});

socket.on("handshake", (handshake) => {
expect(handshake.auth.e).to.be("f");
expect(handshake.query.e).to.be(undefined);
socket.disconnect();
done();
});
});
});

Expand Down

0 comments on commit 249e0be

Please sign in to comment.