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: 7 additions & 1 deletion packages/engine.io-client/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down
27 changes: 27 additions & 0 deletions packages/engine.io-client/test/node.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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", {
Expand Down