Skip to content

Commit a3961e0

Browse files
refactor(eio-client): do not mutate caller's options (#5501)
Related: #5462
1 parent 4c969a7 commit a3961e0

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

packages/engine.io-client/lib/socket.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,8 @@ export class Socket extends SocketWithUpgrade {
11671167
constructor(uri?: string, opts?: SocketOptions);
11681168
constructor(opts: SocketOptions);
11691169
constructor(uri?: string | SocketOptions, opts: SocketOptions = {}) {
1170-
const o = typeof uri === "object" ? uri : opts;
1170+
const isOptionsOnly = typeof uri === "object";
1171+
const o = isOptionsOnly ? { ...uri } : { ...opts };
11711172

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

1181-
super(uri as string, o as BaseSocketOptions);
1182+
super(
1183+
isOptionsOnly ? (o as BaseSocketOptions) : uri,
1184+
o as BaseSocketOptions,
1185+
);
11821186
}
11831187
}

packages/engine.io-client/test/socket.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ describe("Socket", function () {
2323
});
2424
});
2525

26+
it("should not mutate the caller's transports option", () => {
27+
const optsWithUri = { transports: ["websocket", "polling"] };
28+
const optsWithoutUri = { transports: ["polling"] };
29+
30+
const socketWithUri = new Socket("http://localhost", optsWithUri);
31+
const socketWithoutUri = new Socket(optsWithoutUri);
32+
33+
expect(optsWithUri.transports).to.eql(["websocket", "polling"]);
34+
expect(optsWithoutUri.transports).to.eql(["polling"]);
35+
socketWithUri.close();
36+
socketWithoutUri.close();
37+
});
38+
2639
it("throws an error when no transports are available", (done) => {
2740
const socket = new Socket({ transports: [] });
2841
let errorMessage = "";

0 commit comments

Comments
 (0)