-
-
Notifications
You must be signed in to change notification settings - Fork 956
/
Copy pathrequest-as-event-emitter.ts
339 lines (277 loc) · 9.84 KB
/
request-as-event-emitter.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import CacheableRequest = require('cacheable-request');
import EventEmitter = require('events');
import http = require('http');
import stream = require('stream');
import {URL} from 'url';
import {promisify} from 'util';
import is from '@sindresorhus/is';
import timer from '@szmarczak/http-timer';
import {ProxyStream} from './as-stream';
import calculateRetryDelay from './calculate-retry-delay';
import {CacheError, GotError, MaxRedirectsError, RequestError, TimeoutError} from './errors';
import getResponse from './get-response';
import {normalizeRequestArguments} from './normalize-arguments';
import {createProgressStream} from './progress';
import timedOut, {TimeoutError as TimedOutTimeoutError} from './utils/timed-out';
import {GeneralError, NormalizedOptions, Response, ResponseObject} from './utils/types';
import urlToOptions from './utils/url-to-options';
const setImmediateAsync = async (): Promise<void> => new Promise(resolve => setImmediate(resolve));
const pipeline = promisify(stream.pipeline);
const redirectCodes: ReadonlySet<number> = new Set([300, 301, 302, 303, 304, 307, 308]);
export interface RequestAsEventEmitter extends EventEmitter {
retry: <T extends GotError>(error: T) => boolean;
abort: () => void;
}
export default (options: NormalizedOptions): RequestAsEventEmitter => {
const emitter = new EventEmitter() as RequestAsEventEmitter;
const requestURL = options.url.toString();
const redirects: string[] = [];
let retryCount = 0;
let currentRequest: http.ClientRequest;
// `request.aborted` is a boolean since v11.0.0: https://github.com/nodejs/node/commit/4b00c4fafaa2ae8c41c1f78823c0feb810ae4723#diff-e3bc37430eb078ccbafe3aa3b570c91a
const isAborted = (): boolean => typeof currentRequest.aborted === 'number' || (currentRequest.aborted as unknown as boolean);
const emitError = async (error: GeneralError): Promise<void> => {
try {
for (const hook of options.hooks.beforeError) {
// eslint-disable-next-line no-await-in-loop
error = await hook(error);
}
emitter.emit('error', error);
} catch (error_) {
emitter.emit('error', error_);
}
};
const get = async (): Promise<void> => {
let httpOptions = await normalizeRequestArguments(options);
const handleResponse = async (response: http.ServerResponse | ResponseObject): Promise<void> => {
try {
/* istanbul ignore next: fixes https://github.com/electron/electron/blob/cbb460d47628a7a146adf4419ed48550a98b2923/lib/browser/api/net.js#L59-L65 */
if (options.useElectronNet) {
response = new Proxy(response, {
get: (target, name) => {
if (name === 'trailers' || name === 'rawTrailers') {
return [];
}
const value = (target as any)[name];
return is.function_(value) ? value.bind(target) : value;
}
});
}
const typedResponse = response as Response;
const {statusCode} = typedResponse;
typedResponse.statusMessage = is.nonEmptyString(typedResponse.statusMessage) ? typedResponse.statusMessage : http.STATUS_CODES[statusCode];
typedResponse.url = options.url.toString();
typedResponse.requestUrl = requestURL;
typedResponse.retryCount = retryCount;
typedResponse.redirectUrls = redirects;
typedResponse.request = {options};
typedResponse.isFromCache = typedResponse.fromCache ?? false;
delete typedResponse.fromCache;
if (!typedResponse.isFromCache) {
// @ts-ignore Node.js typings haven't been updated yet
typedResponse.ip = response.socket.remoteAddress;
}
const rawCookies = typedResponse.headers['set-cookie'];
if (Reflect.has(options, 'cookieJar') && rawCookies) {
let promises: Array<Promise<unknown>> = rawCookies.map(async (rawCookie: string) => options.cookieJar.setCookie(rawCookie, typedResponse.url));
if (options.ignoreInvalidCookies) {
promises = promises.map(async p => p.catch(() => {}));
}
await Promise.all(promises);
}
if (options.followRedirect && Reflect.has(typedResponse.headers, 'location') && redirectCodes.has(statusCode)) {
typedResponse.resume(); // We're being redirected, we don't care about the response.
if (statusCode === 303 || options.methodRewriting === false) {
if (options.method !== 'GET' && options.method !== 'HEAD') {
// Server responded with "see other", indicating that the resource exists at another location,
// and the client should request it from that location via GET or HEAD.
options.method = 'GET';
}
delete options.body;
delete options.json;
delete options.form;
}
if (redirects.length >= options.maxRedirects) {
throw new MaxRedirectsError(typedResponse, options.maxRedirects, options);
}
// Handles invalid URLs. See https://github.com/sindresorhus/got/issues/604
const redirectBuffer = Buffer.from(typedResponse.headers.location, 'binary').toString();
const redirectURL = new URL(redirectBuffer, options.url);
// Redirecting to a different site, clear cookies.
if (redirectURL.hostname !== options.url.hostname) {
delete options.headers.cookie;
}
redirects.push(redirectURL.toString());
options.url = redirectURL;
for (const hook of options.hooks.beforeRedirect) {
// eslint-disable-next-line no-await-in-loop
await hook(options, typedResponse);
}
emitter.emit('redirect', response, options);
await get();
return;
}
try {
await getResponse(typedResponse, options, emitter);
} catch (error) {
// Don't throw `Premature close` if the request has been aborted
if (!(isAborted() && error.message === 'Premature close')) {
throw error;
}
}
} catch (error) {
emitError(error);
}
};
const handleRequest = async (request: http.ClientRequest): Promise<void> => {
let isPiped = false;
let isFinished = false;
// `request.finished` doesn't indicate whether this has been emitted or not
request.once('finish', () => {
isFinished = true;
});
currentRequest = request;
const onError = (error: GeneralError): void => {
if (error instanceof TimedOutTimeoutError) {
error = new TimeoutError(error, request.timings!, options);
} else {
error = new RequestError(error, options);
}
if (!emitter.retry(error as GotError)) {
emitError(error);
}
};
request.on('error', error => {
if (isPiped) {
// Check if it's caught by `stream.pipeline(...)`
if (!isFinished) {
return;
}
// We need to let `TimedOutTimeoutError` through, because `stream.pipeline(…)` aborts the request automatically.
if (isAborted() && !(error instanceof TimedOutTimeoutError)) {
return;
}
}
onError(error);
});
try {
timer(request);
timedOut(request, options.timeout, options.url);
emitter.emit('request', request);
const uploadStream = createProgressStream('uploadProgress', emitter, httpOptions.headers!['content-length'] as string);
isPiped = true;
await pipeline(
httpOptions.body!,
uploadStream,
request
);
request.emit('upload-complete');
} catch (error) {
if (isAborted() && error.message === 'Premature close') {
// The request was aborted on purpose
return;
}
onError(error);
}
};
if (options.cache) {
// `cacheable-request` doesn't support Node 10 API, fallback.
httpOptions = {
...httpOptions,
...urlToOptions(options.url)
};
// @ts-ignore ResponseLike missing socket field, should be fixed upstream
const cacheRequest = options.cacheableRequest!(httpOptions, handleResponse);
cacheRequest.once('error', (error: GeneralError) => {
if (error instanceof CacheableRequest.RequestError) {
emitError(new RequestError(error, options));
} else {
emitError(new CacheError(error, options));
}
});
cacheRequest.once('request', handleRequest);
} else {
// Catches errors thrown by calling `requestFn(…)`
try {
// @ts-ignore ResponseObject does not equal IncomingMessage
handleRequest(httpOptions.request(options.url, httpOptions, handleResponse));
} catch (error) {
emitError(new RequestError(error, options));
}
}
};
emitter.retry = error => {
let backoff: number;
retryCount++;
try {
backoff = options.retry.calculateDelay({
attemptCount: retryCount,
retryOptions: options.retry,
error,
computedValue: calculateRetryDelay({
attemptCount: retryCount,
retryOptions: options.retry,
error,
computedValue: 0
})
});
} catch (error_) {
emitError(error_);
return false;
}
if (backoff) {
const retry = async (options: NormalizedOptions): Promise<void> => {
try {
for (const hook of options.hooks.beforeRetry) {
// eslint-disable-next-line no-await-in-loop
await hook(options, error, retryCount);
}
await get();
} catch (error_) {
emitError(error_);
}
};
setTimeout(retry, backoff, {...options, forceRefresh: true});
return true;
}
return false;
};
emitter.abort = () => {
emitter.prependListener('request', (request: http.ClientRequest) => {
request.abort();
});
if (currentRequest) {
currentRequest.abort();
}
};
(async () => {
// Promises are executed immediately.
// If there were no `setImmediate` here,
// `promise.json()` would have no effect
// as the request would be sent already.
await setImmediateAsync();
try {
for (const hook of options.hooks.beforeRequest) {
// eslint-disable-next-line no-await-in-loop
await hook(options);
}
await get();
} catch (error) {
emitError(error);
}
})();
return emitter;
};
export const proxyEvents = (proxy: EventEmitter | ProxyStream, emitter: RequestAsEventEmitter): void => {
const events = [
'request',
'redirect',
'uploadProgress',
'downloadProgress'
];
for (const event of events) {
emitter.on(event, (...args: unknown[]) => {
proxy.emit(event, ...args);
});
}
};