-
-
Notifications
You must be signed in to change notification settings - Fork 958
/
Copy pathtimed-out.ts
178 lines (142 loc) · 4.49 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
import * as net from 'net';
import {ClientRequest, IncomingMessage} from 'http';
import unhandler from './unhandle';
const reentry: unique symbol = Symbol('reentry');
const noop = (): void => {};
interface TimedOutOptions {
host?: string;
hostname?: string;
protocol?: string;
}
export interface Delays {
lookup?: number;
connect?: number;
secureConnect?: number;
socket?: number;
response?: number;
send?: 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 (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;
}
});
request.once('close', cancelTimeouts);
once(request, 'response', (response: IncomingMessage): void => {
once(response, 'end', cancelTimeouts);
});
if (typeof delays.request !== 'undefined') {
addTimeout(delays.request, timeoutHandler, 'request');
}
if (typeof delays.socket !== 'undefined') {
const socketTimeoutHandler = (): void => {
timeoutHandler(delays.socket!, 'socket');
};
request.setTimeout(delays.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);
});
}
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 (typeof delays.lookup !== 'undefined' && !hasPath && typeof (socket.address() as net.AddressInfo).address === 'undefined') {
const cancelTimeout = addTimeout(delays.lookup, timeoutHandler, 'lookup');
once(socket, 'lookup', cancelTimeout);
}
if (typeof delays.connect !== 'undefined') {
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 (typeof delays.secureConnect !== 'undefined' && options.protocol === 'https:') {
once(socket, 'connect', (): void => {
const cancelTimeout = addTimeout(delays.secureConnect!, timeoutHandler, 'secureConnect');
once(socket, 'secureConnect', cancelTimeout);
});
}
}
if (typeof delays.send !== 'undefined') {
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);
});
}
return cancelTimeouts;
};
declare module 'http' {
interface ClientRequest {
[reentry]: boolean;
}
}