From 249e0bef9071e7afd785485961c4eef0094254e8 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Thu, 8 Oct 2020 02:04:03 +0200 Subject: [PATCH] feat: remove the implicit connection to the default namespace Related: https://github.com/socketio/socket.io/commit/09b6f2333950b8afc8c1400b504b01ad757876bd --- build/socket.d.ts | 2 +- build/socket.js | 24 +++++++++--------------- lib/socket.ts | 26 +++++++++----------------- package.json | 1 - test/socket.js | 37 ++++++++++++++++++++++++++++--------- 5 files changed, 47 insertions(+), 43 deletions(-) diff --git a/build/socket.d.ts b/build/socket.d.ts index 45d17f14b..1d7dd7c72 100644 --- a/build/socket.d.ts +++ b/build/socket.d.ts @@ -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; diff --git a/build/socket.js b/build/socket.js index 304447910..ba49abd67 100644 --- a/build/socket.js +++ b/build/socket.js @@ -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"); /** @@ -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(); @@ -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 }); } } /** diff --git a/lib/socket.ts b/lib/socket.ts index 7081b6ff6..d3b09c51d 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -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"; @@ -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 = {}; @@ -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(); } @@ -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 }); } } diff --git a/package.json b/package.json index 55c3a9a07..484e7eebf 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/socket.js b/test/socket.js index 1a883e3c6..803215c5a 100644 --- a/test/socket.js +++ b/test/socket.js @@ -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"); @@ -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"); @@ -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"); @@ -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(); + }); }); });