Skip to content
Open
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
8 changes: 6 additions & 2 deletions packages/engine.io-client/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,8 @@ export class Socket extends SocketWithUpgrade {
constructor(uri?: string, opts?: SocketOptions);
constructor(opts: SocketOptions);
constructor(uri?: string | SocketOptions, opts: SocketOptions = {}) {
const o = typeof uri === "object" ? uri : opts;
const isOptionsOnly = typeof uri === "object";
const o = isOptionsOnly ? { ...uri } : { ...opts };

if (
!o.transports ||
Expand All @@ -1178,6 +1179,9 @@ export class Socket extends SocketWithUpgrade {
.filter((t) => !!t);
}

super(uri as string, o as BaseSocketOptions);
super(
isOptionsOnly ? (o as BaseSocketOptions) : uri,
o as BaseSocketOptions,
);
}
}
13 changes: 13 additions & 0 deletions packages/engine.io-client/test/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ describe("Socket", function () {
});
});

it("should not mutate the caller's transports option", () => {
const optsWithUri = { transports: ["websocket", "polling"] };
const optsWithoutUri = { transports: ["polling"] };

const socketWithUri = new Socket("http://localhost", optsWithUri);
const socketWithoutUri = new Socket(optsWithoutUri);

expect(optsWithUri.transports).to.eql(["websocket", "polling"]);
expect(optsWithoutUri.transports).to.eql(["polling"]);
socketWithUri.close();
socketWithoutUri.close();
});

it("throws an error when no transports are available", (done) => {
const socket = new Socket({ transports: [] });
let errorMessage = "";
Expand Down