forked from sindresorhus/got
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timed-out.ts
193 lines (156 loc) · 5.12 KB
/
timed-out.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import net from 'node:net';
import type {ClientRequest, IncomingMessage} from 'node:http';
import unhandler from './utils/unhandle.js';
const reentry: unique symbol = Symbol('reentry');
const noop = (): void => {};
type TimedOutOptions = {
host?: string;
hostname?: string;
protocol?: string;
};
export type Delays = {
lookup?: number;
socket?: number;
connect?: number;
secureConnect?: number;
send?: number;
response?: number;
read?: number;
request?: number;
};
export type ErrorCode =
| 'ETIMEDOUT'
| 'ECONNRESET'
| 'EADDRINUSE'
| 'ECONNREFUSED'
| 'EPIPE'
| 'ENOTFOUND'
| 'ENETUNREACH'
| 'EAI_AGAIN';
export class TimeoutError extends Error {
code: ErrorCode;
constructor(threshold: number, public event: string) {
super(`Timeout awaiting '${event}' for ${threshold}ms`);
this.name = 'TimeoutError';
this.code = 'ETIMEDOUT';
}
}
export default function timedOut(request: ClientRequest, delays: Delays, options: TimedOutOptions): () => void {
if (reentry in request) {
return noop;
}
request[reentry] = true;
const cancelers: Array<typeof noop> = [];
const {once, unhandleAll} = unhandler();
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 => {
clearTimeout(timeout);
};
cancelers.push(cancel);
return cancel;
};
const {host, hostname} = options;
const timeoutHandler = (delay: number, event: string): void => {
request.destroy(new TimeoutError(delay, event));
};
const cancelTimeouts = (): void => {
for (const cancel of cancelers) {
cancel();
}
unhandleAll();
};
request.once('error', error => {
cancelTimeouts();
// Save original behavior
/* istanbul ignore next */
if (request.listenerCount('error') === 0) {
throw error;
}
});
if (typeof delays.request !== 'undefined') {
const cancelTimeout = addTimeout(delays.request, timeoutHandler, 'request');
once(request, 'response', (response: IncomingMessage): void => {
once(response, 'end', cancelTimeout);
});
}
if (typeof delays.socket !== 'undefined') {
const {socket} = delays;
const socketTimeoutHandler = (): void => {
timeoutHandler(socket, 'socket');
};
request.setTimeout(socket, socketTimeoutHandler);
// `request.setTimeout(0)` causes a memory leak.
// We can just remove the listener and forget about the timer - it's unreffed.
// See https://github.com/sindresorhus/got/issues/690
cancelers.push(() => {
request.removeListener('timeout', socketTimeoutHandler);
});
}
const hasLookup = typeof delays.lookup !== 'undefined';
const hasConnect = typeof delays.connect !== 'undefined';
const hasSecureConnect = typeof delays.secureConnect !== 'undefined';
const hasSend = typeof delays.send !== 'undefined';
if (hasLookup || hasConnect || hasSecureConnect || hasSend) {
once(request, 'socket', (socket: net.Socket): void => {
const {socketPath} = request as ClientRequest & {socketPath?: string};
/* istanbul ignore next: hard to test */
if (socket.connecting) {
const hasPath = Boolean(socketPath ?? net.isIP(hostname ?? host ?? '') !== 0);
if (hasLookup && !hasPath && typeof (socket.address() as net.AddressInfo).address === 'undefined') {
const cancelTimeout = addTimeout(delays.lookup!, timeoutHandler, 'lookup');
once(socket, 'lookup', cancelTimeout);
}
if (hasConnect) {
const timeConnect = (): (() => void) => addTimeout(delays.connect!, timeoutHandler, 'connect');
if (hasPath) {
once(socket, 'connect', timeConnect());
} else {
once(socket, 'lookup', (error: Error): void => {
if (error === null) {
once(socket, 'connect', timeConnect());
}
});
}
}
if (hasSecureConnect && options.protocol === 'https:') {
once(socket, 'connect', (): void => {
const cancelTimeout = addTimeout(delays.secureConnect!, timeoutHandler, 'secureConnect');
once(socket, 'secureConnect', cancelTimeout);
});
}
}
if (hasSend) {
const timeRequest = (): (() => void) => addTimeout(delays.send!, timeoutHandler, 'send');
/* istanbul ignore next: hard to test */
if (socket.connecting) {
once(socket, 'connect', (): void => {
once(request, 'upload-complete', timeRequest());
});
} else {
once(request, 'upload-complete', timeRequest());
}
}
});
}
if (typeof delays.response !== 'undefined') {
once(request, 'upload-complete', (): void => {
const cancelTimeout = addTimeout(delays.response!, timeoutHandler, 'response');
once(request, 'response', cancelTimeout);
});
}
if (typeof delays.read !== 'undefined') {
once(request, 'response', (response: IncomingMessage): void => {
const cancelTimeout = addTimeout(delays.read!, timeoutHandler, 'read');
once(response, 'end', cancelTimeout);
});
}
return cancelTimeouts;
}
declare module 'http' {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions -- This has to be an `interface` to be able to be merged.
interface ClientRequest {
[reentry]?: boolean;
}
}