Skip to content

Commit 4865f2e

Browse files
committedOct 21, 2024
fix(eio-client): prevent infinite loop with Node.js built-in WebSocket
Related: #5194
1 parent d4b3dde commit 4865f2e

File tree

6 files changed

+39
-3
lines changed

6 files changed

+39
-3
lines changed
 

‎.github/workflows/ci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@ jobs:
6060
- name: Run tests with fetch instead of XHR (engine.io-client)
6161
run: npm run test:node-fetch --workspace=engine.io-client
6262
if: ${{ matrix.node-version == '18' }}
63+
64+
- name: Run tests with Node.js native WebSocket (engine.io-client)
65+
run: npm run test:node-builtin-ws --workspace=engine.io-client
66+
if: ${{ matrix.node-version == '22' }}

‎packages/engine.io-client/lib/transports/websocket.ts

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export abstract class BaseWS extends Transport {
123123

124124
override doClose() {
125125
if (typeof this.ws !== "undefined") {
126+
this.ws.onerror = () => {};
126127
this.ws.close();
127128
this.ws = null;
128129
}

‎packages/engine.io-client/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"test": "npm run format:check && npm run compile && if test \"$BROWSERS\" = \"1\" ; then npm run test:browser; else npm run test:node; fi",
6464
"test:node": "mocha --bail --require test/support/hooks.js test/index.js test/webtransport.mjs",
6565
"test:node-fetch": "USE_FETCH=1 npm run test:node",
66+
"test:node-builtin-ws": "USE_BUILTIN_WS=1 npm run test:node",
6667
"test:browser": "zuul test/index.js",
6768
"build": "rimraf ./dist && rollup -c support/rollup.config.umd.js && rollup -c support/rollup.config.esm.js",
6869
"bundle-size": "node support/bundle-size.js",

‎packages/engine.io-client/test/connection.js

+13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ describe("connection", function () {
1717
});
1818
});
1919

20+
it("should connect to localhost (ws)", (done) => {
21+
const socket = new Socket({
22+
transports: ["websocket"],
23+
});
24+
socket.on("open", () => {
25+
socket.on("message", (data) => {
26+
expect(data).to.equal("hi");
27+
socket.close();
28+
done();
29+
});
30+
});
31+
});
32+
2033
it("should receive multibyte utf-8 strings with polling", (done) => {
2134
const socket = new Socket();
2235
socket.on("open", () => {

‎packages/engine.io-client/test/support/env.js

+8
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,11 @@ if (exports.useFetch) {
3535
const { transports, Fetch } = require("../..");
3636
transports.polling = Fetch;
3737
}
38+
39+
exports.useBuiltinWs = process.env.USE_BUILTIN_WS !== undefined;
40+
41+
if (exports.useBuiltinWs) {
42+
console.warn("testing with built-in WebSocket object");
43+
const { transports, WebSocket } = require("../..");
44+
transports.websocket = WebSocket;
45+
}

‎packages/engine.io-client/test/transport.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,10 @@ describe("Transport", () => {
210210
// these are server only
211211
if (!env.browser) {
212212
describe("options", () => {
213-
it("should accept an `agent` option for WebSockets", (done) => {
213+
it("should accept an `agent` option for WebSockets", function (done) {
214+
if (env.useBuiltinWs) {
215+
return this.skip();
216+
}
214217
const polling = new eio.transports.websocket({
215218
path: "/engine.io",
216219
hostname: "localhost",
@@ -269,7 +272,10 @@ describe("Transport", () => {
269272
});
270273

271274
describe("perMessageDeflate", () => {
272-
it("should set threshold", (done) => {
275+
it("should set threshold", function (done) {
276+
if (env.useBuiltinWs) {
277+
return this.skip();
278+
}
273279
const socket = new eio.Socket({
274280
transports: ["websocket"],
275281
perMessageDeflate: { threshold: 0 },
@@ -289,7 +295,10 @@ describe("Transport", () => {
289295
});
290296
});
291297

292-
it("should not compress when the byte size is below threshold", (done) => {
298+
it("should not compress when the byte size is below threshold", function (done) {
299+
if (env.useBuiltinWs) {
300+
return this.skip();
301+
}
293302
const socket = new eio.Socket({ transports: ["websocket"] });
294303
socket.on("open", () => {
295304
const ws = socket.transport.ws;

0 commit comments

Comments
 (0)