From e44d6d5b1cb4514bb57fc28f1f749a2ca2a8b204 Mon Sep 17 00:00:00 2001 From: Mark Russell Date: Fri, 13 Nov 2026 00:45:35 -0500 Subject: [PATCH] fix(eio-client): honor the requestTimeout option in Node.js and with fetch() The requestTimeout option was only applied through the "timeout" attribute of the XMLHttpRequest object, which is not supported by the "xmlhttprequest-ssl" package (the default polling implementation in Node.js), and was ignored by the Fetch transport. A request that never received a response stayed pending forever. The XHR request now handles the timeout itself when the XMLHttpRequest implementation does not support it, and the Fetch transport aborts the request with an AbortController. Co-Authored-By: Claude Opus 5.5 --- packages/engine.io-client/lib/transport.ts | 1 + .../lib/transports/polling-fetch.ts | 31 ++++++++++++++--- .../lib/transports/polling-xhr.ts | 19 +++++++++++ packages/engine.io-client/test/node.js | 33 +++++++++++++++++++ 4 files changed, 79 insertions(+), 5 deletions(-) diff --git a/packages/engine.io-client/lib/transport.ts b/packages/engine.io-client/lib/transport.ts index 9bcd30d90c..76b9412a7d 100644 --- a/packages/engine.io-client/lib/transport.ts +++ b/packages/engine.io-client/lib/transport.ts @@ -50,6 +50,7 @@ export abstract class Transport extends Emitter< protected readyState: TransportState; protected socket: Socket; protected setTimeoutFn: typeof setTimeout; + protected clearTimeoutFn: typeof clearTimeout; /** * Transport abstract constructor. diff --git a/packages/engine.io-client/lib/transports/polling-fetch.ts b/packages/engine.io-client/lib/transports/polling-fetch.ts index 7d85b66c62..53e9655f37 100644 --- a/packages/engine.io-client/lib/transports/polling-fetch.ts +++ b/packages/engine.io-client/lib/transports/polling-fetch.ts @@ -48,16 +48,37 @@ export class Fetch extends Polling { this.socket._cookieJar?.appendCookies(headers); + let signal: AbortSignal; + let timeoutTimer: NodeJS.Timeout; + + if (this.opts.requestTimeout) { + const controller = new AbortController(); + signal = controller.signal; + timeoutTimer = this.setTimeoutFn( + () => controller.abort(), + this.opts.requestTimeout, + ); + } + return fetch(this.uri(), { method: isPost ? "POST" : "GET", body: isPost ? data : null, headers, credentials: this.opts.withCredentials ? "include" : "omit", - }).then((res) => { - // @ts-ignore getSetCookie() was added in Node.js v19.7.0 - this.socket._cookieJar?.parseCookies(res.headers.getSetCookie()); + signal, + }).then( + (res) => { + this.clearTimeoutFn(timeoutTimer); + + // @ts-ignore getSetCookie() was added in Node.js v19.7.0 + this.socket._cookieJar?.parseCookies(res.headers.getSetCookie()); - return res; - }); + return res; + }, + (err) => { + this.clearTimeoutFn(timeoutTimer); + throw err; + }, + ); } } diff --git a/packages/engine.io-client/lib/transports/polling-xhr.ts b/packages/engine.io-client/lib/transports/polling-xhr.ts index 2196d2d220..12308cece7 100644 --- a/packages/engine.io-client/lib/transports/polling-xhr.ts +++ b/packages/engine.io-client/lib/transports/polling-xhr.ts @@ -108,7 +108,9 @@ export class Request extends Emitter< private _xhr: any; private setTimeoutFn: typeof setTimeout; + private clearTimeoutFn: typeof clearTimeout; private _index: number; + private _timeoutTimer: NodeJS.Timeout; static requestsCount = 0; static requests = {}; @@ -215,6 +217,22 @@ export class Request extends Emitter< debug("xhr data %s", this._data); xhr.send(this._data); + + if (this._opts.requestTimeout && !("ontimeout" in xhr)) { + // the XMLHttpRequest implementation does not support the "timeout" attribute (e.g. the "xmlhttprequest-ssl" + // package in Node.js), so we handle the timeout ourselves + this._timeoutTimer = this.setTimeoutFn(() => { + debug("xhr timeout after %d ms", this._opts.requestTimeout); + this.emitReserved("error", 0, this._xhr); + // note: xhr.abort() is not called here, because with the "xmlhttprequest-ssl" package, aborting a pending + // request throws an uncaught exception (see https://github.com/mjwwit/node-XMLHttpRequest/issues/37) + this._cleanup(); + }, this._opts.requestTimeout); + + if (this._opts.autoUnref) { + this._timeoutTimer.unref(); + } + } } catch (e) { // Need to defer since .create() is called directly from the constructor // and thus the 'error' event can only be only bound *after* this exception @@ -250,6 +268,7 @@ export class Request extends Emitter< if ("undefined" === typeof this._xhr || null === this._xhr) { return; } + this.clearTimeoutFn(this._timeoutTimer); this._xhr.onreadystatechange = empty; if (fromError) { diff --git a/packages/engine.io-client/test/node.js b/packages/engine.io-client/test/node.js index 1f756a13bd..29d15ff100 100644 --- a/packages/engine.io-client/test/node.js +++ b/packages/engine.io-client/test/node.js @@ -1,7 +1,9 @@ const path = require("node:path"); const { execFile } = require("node:child_process"); +const { createServer } = require("node:http"); const { Socket } = require("../"); const { repeat } = require("./util"); +const { useFetch } = require("./support/env"); const expect = require("expect.js"); const { parse } = require("../build/cjs/globals.node.js"); const { CookieJar } = require("../build/cjs/globals.node.js"); @@ -123,6 +125,37 @@ describe("node.js", () => { done(); }); }); + + it("should abort the request after requestTimeout ms (polling)", (done) => { + const httpServer = createServer(() => { + // never answered + }); + + httpServer.listen(0, () => { + const socket = new Socket( + `http://localhost:${httpServer.address().port}`, + { + transports: ["polling"], + requestTimeout: 100, + }, + ); + + socket.on("error", (err) => { + expect(err.type).to.eql("TransportError"); + expect(err.message).to.eql( + useFetch ? "fetch read error" : "xhr poll error", + ); + }); + + socket.on("close", (reason) => { + expect(reason).to.eql("transport error"); + + httpServer.closeAllConnections(); + httpServer.close(); + done(); + }); + }); + }); }); describe("cookie parsing", () => {