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
1 change: 1 addition & 0 deletions packages/engine.io-client/lib/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
31 changes: 26 additions & 5 deletions packages/engine.io-client/lib/transports/polling-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
);
}
}
19 changes: 19 additions & 0 deletions packages/engine.io-client/lib/transports/polling-xhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
33 changes: 33 additions & 0 deletions packages/engine.io-client/test/node.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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", () => {
Expand Down