diff --git a/packages/engine.io-client/lib/socket.ts b/packages/engine.io-client/lib/socket.ts index 2bd3603c2..ee028caea 100644 --- a/packages/engine.io-client/lib/socket.ts +++ b/packages/engine.io-client/lib/socket.ts @@ -22,6 +22,9 @@ const withEventListeners = const OFFLINE_EVENT_LISTENERS = []; +// the maximum delay of setTimeout() (about 24.8 days), a larger value makes the timer fire immediately +const MAX_TIMER_DELAY = 2147483647; + if (withEventListeners) { // within a ServiceWorker, any event handler for the 'offline' event must be added on the initial evaluation of the // script, so we create one single event listener here which will forward the event to the socket instances @@ -819,7 +822,10 @@ export class Socket extends Emitter< */ private _resetPingTimeout() { this.clearTimeoutFn(this._pingTimeoutTimer); - const delay = this._pingInterval + this._pingTimeout; + const delay = Math.min( + this._pingInterval + this._pingTimeout, + MAX_TIMER_DELAY, + ); this._pingTimeoutTime = Date.now() + delay; this._pingTimeoutTimer = this.setTimeoutFn(() => { this._onClose("ping timeout"); diff --git a/packages/engine.io-client/test/node.js b/packages/engine.io-client/test/node.js index 1f756a13b..f84409206 100644 --- a/packages/engine.io-client/test/node.js +++ b/packages/engine.io-client/test/node.js @@ -1,5 +1,7 @@ const path = require("node:path"); const { execFile } = require("node:child_process"); +const { createServer } = require("node:http"); +const { attach } = require("engine.io"); const { Socket } = require("../"); const { repeat } = require("./util"); const expect = require("expect.js"); @@ -109,6 +111,31 @@ describe("node.js", () => { }); }); + it("should not close when pingInterval + pingTimeout is above the maximum timer delay", (done) => { + const httpServer = createServer(); + const engine = attach(httpServer, { + pingInterval: 25000, + pingTimeout: 2147483647, // the maximum delay of setTimeout() + }); + + httpServer.listen(0, () => { + const socket = new Socket( + `http://localhost:${httpServer.address().port}`, + { transports: ["websocket"] }, + ); + + socket.on("open", () => { + setTimeout(() => { + expect(socket.readyState).to.eql("open"); + socket.close(); + engine.close(); + httpServer.close(); + done(); + }, 100); + }); + }); + }); + it("should not modify the extraHeaders option with withCredentials: true", (done) => { const extraHeaders = { foo: "bar" }; const socket = new Socket("http://localhost:3000", {