Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding an option to avoid the added trailing slash to the path #694

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -404,7 +412,7 @@ export class Socket extends Emitter<{}, {}, SocketReservedEvents> {
socket: this,
hostname: this.hostname,
secure: this.secure,
port: this.port
port: this.port,
}
);

Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
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 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");
Expand Down