Skip to content

Commit 21a6e12

Browse files
iifawzidarrachequesne
authored andcommitted
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: socketio/socket.io-client#1550 Signed-off-by: iifawzi <[email protected]>
1 parent 9d772e3 commit 21a6e12

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/socket.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ export interface SocketOptions {
206206
*/
207207
path: string;
208208

209+
/**
210+
* Whether we should add a trailing slash to the request path.
211+
* @default true
212+
*/
213+
addTrailingSlash: boolean;
214+
209215
/**
210216
* Either a single protocol string or an array of protocol strings. These strings are used to indicate sub-protocols,
211217
* 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> {
323329
upgrade: true,
324330
timestampParam: "t",
325331
rememberUpgrade: false,
332+
addTrailingSlash: true,
326333
rejectUnauthorized: true,
327334
perMessageDeflate: {
328335
threshold: 1024
@@ -333,7 +340,9 @@ export class Socket extends Emitter<{}, {}, SocketReservedEvents> {
333340
opts
334341
);
335342

336-
this.opts.path = this.opts.path.replace(/\/$/, "") + "/";
343+
this.opts.path =
344+
this.opts.path.replace(/\/$/, "") +
345+
(this.opts.addTrailingSlash ? "/" : "");
337346

338347
if (typeof this.opts.query === "string") {
339348
this.opts.query = decode(this.opts.query);

test/engine.io-client.js

+6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ describe("engine.io-client", () => {
5858
expect(client.port).to.be("8080");
5959
});
6060

61+
it("should properly handle the addTrailingSlash option", () => {
62+
const client = new Socket({ host: "localhost", addTrailingSlash: false });
63+
expect(client.hostname).to.be("localhost");
64+
expect(client.opts.path).to.be("/engine.io");
65+
});
66+
6167
it("should properly parse an IPv6 uri without port", () => {
6268
const client = new Socket("http://[::1]");
6369
expect(client.hostname).to.be("::1");

0 commit comments

Comments
 (0)