forked from sindresorhus/got
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathas-promise.ts
182 lines (144 loc) · 5.09 KB
/
as-promise.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
import EventEmitter = require('events');
import getStream = require('get-stream');
import PCancelable = require('p-cancelable');
import is from '@sindresorhus/is';
import {ParseError, ReadError, HTTPError} from './errors';
import {normalizeArguments, mergeOptions} from './normalize-arguments';
import requestAsEventEmitter, {proxyEvents} from './request-as-event-emitter';
import {CancelableRequest, GeneralError, NormalizedOptions, Response} from './types';
const parseBody = (body: Buffer, responseType: NormalizedOptions['responseType'], encoding: NormalizedOptions['encoding']): unknown => {
if (responseType === 'json') {
return body.length === 0 ? '' : JSON.parse(body.toString());
}
if (responseType === 'buffer') {
return Buffer.from(body);
}
if (responseType === 'text') {
return body.toString(encoding);
}
throw new TypeError(`Unknown body type '${responseType as string}'`);
};
export function createRejection(error: Error): CancelableRequest<never> {
const promise = Promise.reject(error) as CancelableRequest<never>;
const returnPromise = (): CancelableRequest<never> => promise;
promise.json = returnPromise;
promise.text = returnPromise;
promise.buffer = returnPromise;
promise.on = returnPromise;
return promise;
}
export default function asPromise<T>(options: NormalizedOptions): CancelableRequest<T> {
const proxy = new EventEmitter();
let body: Buffer;
const promise = new PCancelable<Response | Response['body']>((resolve, reject, onCancel) => {
const emitter = requestAsEventEmitter(options);
onCancel(emitter.abort);
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);
}
reject(error);
} catch (error_) {
reject(error_);
}
};
emitter.on('response', async (response: Response) => {
proxy.emit('response', response);
// Download body
try {
body = await getStream.buffer(response, {encoding: 'binary'});
} catch (error) {
emitError(new ReadError(error, options));
return;
}
if (response.req?.aborted) {
// Canceled while downloading - will throw a `CancelError` or `TimeoutError` error
return;
}
const isOk = (): boolean => {
const {statusCode} = response;
const limitStatusCode = options.followRedirect ? 299 : 399;
return (statusCode >= 200 && statusCode <= limitStatusCode) || statusCode === 304;
};
// Parse body
try {
response.body = parseBody(body, options.responseType, options.encoding);
} catch (error) {
// Fall back to `utf8`
response.body = body.toString();
if (isOk()) {
const parseError = new ParseError(error, response, options);
emitError(parseError);
return;
}
}
try {
for (const [index, hook] of options.hooks.afterResponse.entries()) {
// @ts-ignore TS doesn't notice that CancelableRequest is a Promise
// eslint-disable-next-line no-await-in-loop
response = await hook(response, async (updatedOptions): CancelableRequest<Response> => {
const typedOptions = normalizeArguments(mergeOptions(options, {
...updatedOptions,
retry: {
calculateDelay: () => 0
},
throwHttpErrors: false,
resolveBodyOnly: false
}));
// Remove any further hooks for that request, because we'll call them anyway.
// The loop continues. We don't want duplicates (asPromise recursion).
typedOptions.hooks.afterResponse = options.hooks.afterResponse.slice(0, index);
for (const hook of options.hooks.beforeRetry) {
// eslint-disable-next-line no-await-in-loop
await hook(typedOptions);
}
const promise = asPromise(typedOptions);
onCancel(() => {
promise.catch(() => {});
promise.cancel();
});
return promise as unknown as CancelableRequest<Response>;
});
}
} catch (error) {
emitError(error);
return;
}
// Check for HTTP error codes
if (!isOk()) {
const error = new HTTPError(response, options);
if (emitter.retry(error)) {
return;
}
if (options.throwHttpErrors) {
emitError(error);
return;
}
}
resolve(options.resolveBodyOnly ? response.body : response);
});
emitter.once('error', reject);
proxyEvents(proxy, emitter);
}) as CancelableRequest<T>;
promise.on = (name: string, fn: (...args: any[]) => void) => {
proxy.on(name, fn);
return promise;
};
const shortcut = <T>(responseType: NormalizedOptions['responseType']): CancelableRequest<T> => {
// eslint-disable-next-line promise/prefer-await-to-then
const newPromise = promise.then(() => parseBody(body, responseType, options.encoding));
Object.defineProperties(newPromise, Object.getOwnPropertyDescriptors(promise));
return newPromise as CancelableRequest<T>;
};
promise.json = () => {
if (is.undefined(body) && is.undefined(options.headers.accept)) {
options.headers.accept = 'application/json';
}
return shortcut('json');
};
promise.buffer = () => shortcut('buffer');
promise.text = () => shortcut('text');
return promise;
}