From 21a6e1219add92157c5442537d24fbe1129a50f5 Mon Sep 17 00:00:00 2001 From: iifawzi Date: Sat, 15 Oct 2022 22:06:30 +0200 Subject: [PATCH] feat: add the "addTrailingSlash" option (#694) The "addTrailingSlash" option allows to control whether a trailing slash is added to the path of the HTTP requests created by the library: - true (default): "/engine.io/" - false: "/engine.io" Related: https://github.com/socketio/socket.io-client/issues/1550 Signed-off-by: iifawzi --- lib/socket.ts | 11 ++++++++++- test/engine.io-client.js | 6 ++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/socket.ts b/lib/socket.ts index 90b4840ab..91f2d7862 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -206,6 +206,12 @@ export interface SocketOptions { */ path: string; + /** + * Whether we should add a trailing slash to the request path. + * @default true + */ + addTrailingSlash: 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,6 +329,7 @@ export class Socket extends Emitter<{}, {}, SocketReservedEvents> { upgrade: true, timestampParam: "t", rememberUpgrade: false, + addTrailingSlash: true, rejectUnauthorized: true, perMessageDeflate: { threshold: 1024 @@ -333,7 +340,9 @@ export class Socket extends Emitter<{}, {}, SocketReservedEvents> { opts ); - this.opts.path = this.opts.path.replace(/\/$/, "") + "/"; + this.opts.path = + this.opts.path.replace(/\/$/, "") + + (this.opts.addTrailingSlash ? "/" : ""); if (typeof this.opts.query === "string") { this.opts.query = decode(this.opts.query); diff --git a/test/engine.io-client.js b/test/engine.io-client.js index 1d7677dd0..b4a290043 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 addTrailingSlash option", () => { + const client = new Socket({ host: "localhost", addTrailingSlash: 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");