From d1754841e9330048ecb9d6bfc84d6fae8d1ee3ba Mon Sep 17 00:00:00 2001 From: iifawzi Date: Sat, 15 Oct 2022 22:06:30 +0200 Subject: [PATCH] Adding the trailingSlash option Signed-off-by: iifawzi --- lib/socket.ts | 26 +++++++++++++++++--------- test/engine.io-client.js | 6 ++++++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/socket.ts b/lib/socket.ts index 90b4840ab..6864752ba 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -206,6 +206,12 @@ export interface SocketOptions { */ path: string; + /** + * Should we add a trailing slash to the path? + * @default true + */ + trailingSlash: boolean; + /** * Either a single protocol string or an array of protocol strings. These strings are used to indicate sub-protocols, * so that a single server can implement multiple WebSocket sub-protocols (for example, you might want one server to @@ -323,17 +329,19 @@ export class Socket extends Emitter<{}, {}, SocketReservedEvents> { upgrade: true, timestampParam: "t", rememberUpgrade: false, + trailingSlash: true, rejectUnauthorized: true, perMessageDeflate: { - threshold: 1024 + threshold: 1024, }, transportOptions: {}, - closeOnBeforeunload: true + closeOnBeforeunload: true, }, opts ); - this.opts.path = this.opts.path.replace(/\/$/, "") + "/"; + this.opts.path = + this.opts.path.replace(/\/$/, "") + (this.opts.trailingSlash ? "/" : ""); if (typeof this.opts.query === "string") { this.opts.query = decode(this.opts.query); @@ -365,7 +373,7 @@ export class Socket extends Emitter<{}, {}, SocketReservedEvents> { if (this.hostname !== "localhost") { this.offlineEventListener = () => { this.onClose("transport close", { - description: "network connection lost" + description: "network connection lost", }); }; addEventListener("offline", this.offlineEventListener, false); @@ -404,7 +412,7 @@ export class Socket extends Emitter<{}, {}, SocketReservedEvents> { socket: this, hostname: this.hostname, secure: this.secure, - port: this.port + port: this.port, } ); @@ -472,7 +480,7 @@ export class Socket extends Emitter<{}, {}, SocketReservedEvents> { .on("drain", this.onDrain.bind(this)) .on("packet", this.onPacket.bind(this)) .on("error", this.onError.bind(this)) - .on("close", reason => this.onClose("transport close", reason)); + .on("close", (reason) => this.onClose("transport close", reason)); } /** @@ -493,7 +501,7 @@ export class Socket extends Emitter<{}, {}, SocketReservedEvents> { debug('probe transport "%s" opened', name); transport.send([{ type: "ping", data: "probe" }]); - transport.once("packet", msg => { + transport.once("packet", (msg) => { if (failed) return; if ("pong" === msg.type && "probe" === msg.data) { debug('probe transport "%s" pong', name); @@ -540,7 +548,7 @@ export class Socket extends Emitter<{}, {}, SocketReservedEvents> { } // Handle any error that happens while probing - const onerror = err => { + const onerror = (err) => { const error = new Error("probe error: " + err); // @ts-ignore error.transport = transport.name; @@ -819,7 +827,7 @@ export class Socket extends Emitter<{}, {}, SocketReservedEvents> { const packet = { type: type, data: data, - options: options + options: options, }; this.emitReserved("packetCreate", packet); this.writeBuffer.push(packet); diff --git a/test/engine.io-client.js b/test/engine.io-client.js index 1d7677dd0..97508218b 100644 --- a/test/engine.io-client.js +++ b/test/engine.io-client.js @@ -58,6 +58,12 @@ describe("engine.io-client", () => { expect(client.port).to.be("8080"); }); + it("should properly handle the trailingSlash option", () => { + const client = new Socket({ host: "localhost", trailingSlash: false }); + expect(client.hostname).to.be("localhost"); + expect(client.opts.path).to.be("/engine.io"); + }); + it("should properly parse an IPv6 uri without port", () => { const client = new Socket("http://[::1]"); expect(client.hostname).to.be("::1");