diff --git a/packages/engine.io-client/lib/transport.ts b/packages/engine.io-client/lib/transport.ts index 9bcd30d90..76b9412a7 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 7d85b66c6..53e9655f3 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 2196d2d22..12308cece 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 1f756a13b..29d15ff10 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", () => {