Skip to content

Commit

Permalink
feat: add the "addTrailingSlash" option (#694)
Browse files Browse the repository at this point in the history
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: socketio/socket.io-client#1550

Signed-off-by: iifawzi <[email protected]>
  • Loading branch information
iifawzi authored and darrachequesne committed Dec 9, 2022
1 parent 9d772e3 commit 21a6e12
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -323,6 +329,7 @@ export class Socket extends Emitter<{}, {}, SocketReservedEvents> {
upgrade: true,
timestampParam: "t",
rememberUpgrade: false,
addTrailingSlash: true,
rejectUnauthorized: true,
perMessageDeflate: {
threshold: 1024
Expand All @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions test/engine.io-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 21a6e12

Please sign in to comment.