Skip to content

Commit

Permalink
fix(eio-client): prevent infinite loop with Node.js built-in WebSocket
Browse files Browse the repository at this point in the history
Related: #5194
  • Loading branch information
darrachequesne committed Oct 21, 2024
1 parent d4b3dde commit 4865f2e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ jobs:
- name: Run tests with fetch instead of XHR (engine.io-client)
run: npm run test:node-fetch --workspace=engine.io-client
if: ${{ matrix.node-version == '18' }}

- name: Run tests with Node.js native WebSocket (engine.io-client)
run: npm run test:node-builtin-ws --workspace=engine.io-client
if: ${{ matrix.node-version == '22' }}
1 change: 1 addition & 0 deletions packages/engine.io-client/lib/transports/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export abstract class BaseWS extends Transport {

override doClose() {
if (typeof this.ws !== "undefined") {
this.ws.onerror = () => {};
this.ws.close();
this.ws = null;
}
Expand Down
1 change: 1 addition & 0 deletions packages/engine.io-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"test": "npm run format:check && npm run compile && if test \"$BROWSERS\" = \"1\" ; then npm run test:browser; else npm run test:node; fi",
"test:node": "mocha --bail --require test/support/hooks.js test/index.js test/webtransport.mjs",
"test:node-fetch": "USE_FETCH=1 npm run test:node",
"test:node-builtin-ws": "USE_BUILTIN_WS=1 npm run test:node",
"test:browser": "zuul test/index.js",
"build": "rimraf ./dist && rollup -c support/rollup.config.umd.js && rollup -c support/rollup.config.esm.js",
"bundle-size": "node support/bundle-size.js",
Expand Down
13 changes: 13 additions & 0 deletions packages/engine.io-client/test/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ describe("connection", function () {
});
});

it("should connect to localhost (ws)", (done) => {
const socket = new Socket({
transports: ["websocket"],
});
socket.on("open", () => {
socket.on("message", (data) => {
expect(data).to.equal("hi");
socket.close();
done();
});
});
});

it("should receive multibyte utf-8 strings with polling", (done) => {
const socket = new Socket();
socket.on("open", () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/engine.io-client/test/support/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ if (exports.useFetch) {
const { transports, Fetch } = require("../..");
transports.polling = Fetch;
}

exports.useBuiltinWs = process.env.USE_BUILTIN_WS !== undefined;

if (exports.useBuiltinWs) {
console.warn("testing with built-in WebSocket object");
const { transports, WebSocket } = require("../..");
transports.websocket = WebSocket;
}
15 changes: 12 additions & 3 deletions packages/engine.io-client/test/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ describe("Transport", () => {
// these are server only
if (!env.browser) {
describe("options", () => {
it("should accept an `agent` option for WebSockets", (done) => {
it("should accept an `agent` option for WebSockets", function (done) {
if (env.useBuiltinWs) {
return this.skip();
}
const polling = new eio.transports.websocket({
path: "/engine.io",
hostname: "localhost",
Expand Down Expand Up @@ -269,7 +272,10 @@ describe("Transport", () => {
});

describe("perMessageDeflate", () => {
it("should set threshold", (done) => {
it("should set threshold", function (done) {
if (env.useBuiltinWs) {
return this.skip();
}
const socket = new eio.Socket({
transports: ["websocket"],
perMessageDeflate: { threshold: 0 },
Expand All @@ -289,7 +295,10 @@ describe("Transport", () => {
});
});

it("should not compress when the byte size is below threshold", (done) => {
it("should not compress when the byte size is below threshold", function (done) {
if (env.useBuiltinWs) {
return this.skip();
}
const socket = new eio.Socket({ transports: ["websocket"] });
socket.on("open", () => {
const ws = socket.transport.ws;
Expand Down

0 comments on commit 4865f2e

Please sign in to comment.