Skip to content

Commit

Permalink
Fix for sindresorhus#2248, as supplied by thegedge
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrutter committed Jan 25, 2025
1 parent f997bcb commit f23f4fc
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions source/core/timed-out.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ export default function timedOut(request: ClientRequest, delays: Delays, options
request[reentry] = true;
const cancelers: Array<typeof noop> = [];
const {once, unhandleAll} = unhandler();

const handled: Record<string, boolean> = {};

const addTimeout = (delay: number, callback: (delay: number, event: string) => void, event: string): (typeof noop) => {
const timeout = setTimeout(callback, delay, delay, event) as unknown as NodeJS.Timeout;

timeout.unref?.();

const cancel = (): void => {
handled[event] = true;
clearTimeout(timeout);
};

Expand All @@ -69,7 +71,11 @@ export default function timedOut(request: ClientRequest, delays: Delays, options
const {host, hostname} = options;

const timeoutHandler = (delay: number, event: string): void => {
request.destroy(new TimeoutError(delay, event));
setTimeout(() => {
if (!handled[event]) {
request.destroy(new TimeoutError(delay, event));
}
}, 0);
};

const cancelTimeouts = (): void => {
Expand Down

0 comments on commit f23f4fc

Please sign in to comment.