/
as-promise.ts
163 lines (134 loc) 路 4.43 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
import {IncomingMessage} from 'http';
import EventEmitter = require('events');
import getStream = require('get-stream');
import is from '@sindresorhus/is';
import PCancelable = require('p-cancelable');
import {NormalizedOptions, Response, CancelableRequest} from './utils/types';
import {mergeOptions} from './merge';
import {ParseError, ReadError, HTTPError} from './errors';
import {reNormalizeArguments} from './normalize-arguments';
import requestAsEventEmitter from './request-as-event-emitter';
type ResponseReturn = Response | Buffer | string | any;
export const isProxiedSymbol = Symbol('proxied');
export default function asPromise(options: NormalizedOptions): CancelableRequest<Response> {
const proxy = new EventEmitter();
const parseBody = (response: Response): void => {
if (options.responseType === 'json') {
response.body = JSON.parse(response.body);
} else if (options.responseType === 'buffer') {
response.body = Buffer.from(response.body);
} else if (options.responseType !== 'text' && !is.falsy(options.responseType)) {
throw new Error(`Failed to parse body of type '${options.responseType}'`);
}
};
const promise = new PCancelable<IncomingMessage>((resolve, reject, onCancel) => {
const emitter = requestAsEventEmitter(options);
onCancel(emitter.abort);
const emitError = async (error: Error): 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);
const stream = is.null_(options.encoding) ? getStream.buffer(response) : getStream(response, {encoding: options.encoding});
let data: Buffer | string;
try {
data = await stream;
} catch (error) {
emitError(new ReadError(error, options));
return;
}
if (response.req && response.req.aborted) {
// Canceled while downloading - will throw a CancelError or TimeoutError
return;
}
const limitStatusCode = options.followRedirect ? 299 : 399;
response.body = data;
try {
for (const [index, hook] of options.hooks.afterResponse.entries()) {
// eslint-disable-next-line no-await-in-loop
response = await hook(response, updatedOptions => {
updatedOptions = reNormalizeArguments(mergeOptions(options, {
...updatedOptions,
// @ts-ignore TS complaining that it's missing properties, which get merged
retry: {
calculateDelay: () => 0
},
throwHttpErrors: false,
responseType: 'text',
resolveBodyOnly: false
}));
// Remove any further hooks for that request, because we we'll call them anyway.
// The loop continues. We don't want duplicates (asPromise recursion).
updatedOptions.hooks.afterResponse = options.hooks.afterResponse.slice(0, index);
return asPromise(updatedOptions);
});
}
} catch (error) {
emitError(error);
return;
}
const {statusCode} = response;
if (response.body) {
try {
parseBody(response);
} catch (error) {
if (statusCode >= 200 && statusCode < 300) {
const parseError = new ParseError(error, response, options);
emitError(parseError);
return;
}
}
}
if (statusCode !== 304 && (statusCode < 200 || statusCode > limitStatusCode)) {
const error = new HTTPError(response, options);
if (emitter.retry(error) === false) {
if (options.throwHttpErrors) {
emitError(error);
return;
}
resolve(options.resolveBodyOnly ? response.body : response);
}
return;
}
resolve(options.resolveBodyOnly ? response.body : response);
});
emitter.once('error', reject);
[
'request',
'redirect',
'uploadProgress',
'downloadProgress'
].forEach(event => emitter.on(event, (...args: unknown[]) => {
proxy.emit(event, ...args);
}));
}) as CancelableRequest<ResponseReturn>;
promise[isProxiedSymbol] = true;
promise.on = (name, fn) => {
proxy.on(name, fn);
return promise;
};
promise.json = () => {
options.responseType = 'json';
options.resolveBodyOnly = true;
return promise;
};
promise.buffer = () => {
options.responseType = 'buffer';
options.resolveBodyOnly = true;
return promise;
};
promise.text = () => {
options.responseType = 'text';
options.resolveBodyOnly = true;
return promise;
};
return promise;
}