-
-
Notifications
You must be signed in to change notification settings - Fork 916
/
normalize-arguments.ts
289 lines (238 loc) 路 8.6 KB
/
normalize-arguments.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
import https = require('https');
import {format, URL, URLSearchParams} from 'url';
import CacheableLookup from 'cacheable-lookup';
import is from '@sindresorhus/is';
import lowercaseKeys = require('lowercase-keys');
import Keyv = require('keyv');
import urlToOptions, {URLOptions} from './utils/url-to-options';
import validateSearchParams from './utils/validate-search-params';
import supportsBrotli from './utils/supports-brotli';
import merge from './merge';
import knownHookEvents from './known-hook-events';
import {
Options,
Defaults,
NormalizedOptions,
NormalizedRetryOptions,
RetryOption,
Method,
Delays,
ErrorCode,
StatusCode,
URLArgument,
URLOrOptions
} from './utils/types';
import {HTTPError, ParseError, MaxRedirectsError, GotError} from './errors';
const retryAfterStatusCodes: ReadonlySet<StatusCode> = new Set([413, 429, 503]);
let hasShownDeprecation = false;
// `preNormalize` handles static options (e.g. headers).
// For example, when you create a custom instance and make a request
// with no static changes, they won't be normalized again.
//
// `normalize` operates on dynamic options - they cannot be saved.
// For example, `body` is every time different per request.
// When it's done normalizing the new options, it performs merge()
// on the prenormalized options and the normalized ones.
export const preNormalizeArguments = (options: Options, defaults?: Options): NormalizedOptions => {
if (is.nullOrUndefined(options.headers)) {
options.headers = {};
} else {
options.headers = lowercaseKeys(options.headers);
}
if (options.prefixUrl) {
options.prefixUrl = options.prefixUrl.toString();
if (!options.prefixUrl.toString().endsWith('/')) {
options.prefixUrl += '/';
}
}
if (is.nullOrUndefined(options.hooks)) {
options.hooks = {};
} else if (!is.object(options.hooks)) {
throw new TypeError(`Parameter \`hooks\` must be an object, not ${is(options.hooks)}`);
}
for (const event of knownHookEvents) {
if (is.nullOrUndefined(options.hooks[event])) {
if (defaults && defaults.hooks) {
if (event in defaults.hooks) {
options.hooks[event] = defaults.hooks[event]!.slice();
}
} else {
options.hooks[event] = [];
}
}
}
if (is.number(options.timeout)) {
options.gotTimeout = {request: options.timeout};
} else if (is.object(options.timeout)) {
options.gotTimeout = options.timeout;
}
delete options.timeout;
const {retry} = options;
options.retry = {
retries: () => 0,
methods: new Set(),
statusCodes: new Set(),
errorCodes: new Set(),
maxRetryAfter: undefined
};
if (is.nonEmptyObject(defaults) && retry !== false) {
options.retry = {...(defaults.retry as RetryOption)};
}
if (retry !== false) {
if (is.number(retry)) {
options.retry.retries = retry;
} else {
// eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion
options.retry = {...options.retry, ...retry} as NormalizedRetryOptions;
}
}
if (!options.retry.maxRetryAfter && options.gotTimeout) {
options.retry.maxRetryAfter = Math.min(...[(options.gotTimeout as Delays).request!, (options.gotTimeout as Delays).connect!].filter(n => !is.nullOrUndefined(n)));
}
if (is.array(options.retry.methods)) {
options.retry.methods = new Set(options.retry.methods.map(method => method.toUpperCase())) as ReadonlySet<Method>;
}
if (is.array(options.retry.statusCodes)) {
options.retry.statusCodes = new Set(options.retry.statusCodes);
}
if (is.array(options.retry.errorCodes)) {
options.retry.errorCodes = new Set(options.retry.errorCodes);
}
if (options.dnsCache) {
const cacheableLookup = new CacheableLookup({cacheAdapter: options.dnsCache as Keyv | undefined});
(options as NormalizedOptions).lookup = cacheableLookup.lookup;
delete options.dnsCache;
}
return options as NormalizedOptions;
};
export const normalizeArguments = (url: URLOrOptions, options: NormalizedOptions, defaults?: Defaults): NormalizedOptions => {
let urlArgument: URLArgument;
if (is.plainObject(url)) {
options = {...url, ...options};
urlArgument = options.url || {};
delete options.url;
} else {
urlArgument = url;
}
if (defaults) {
options = merge<NormalizedOptions, Options>({} as NormalizedOptions, defaults.options!, options ? preNormalizeArguments(options, defaults.options) : {});
} else {
options = merge({}, preNormalizeArguments(options));
}
if (!is.string(urlArgument) && !is.object(urlArgument)) {
throw new TypeError(`Parameter \`url\` must be a string or object, not ${is(urlArgument)}`);
}
let urlObj: https.RequestOptions | URLOptions;
if (is.string(urlArgument)) {
if (options.prefixUrl && urlArgument.startsWith('/')) {
throw new Error('`url` must not begin with a slash when using `prefixUrl`');
}
if (options.prefixUrl) {
urlArgument = options.prefixUrl + urlArgument;
}
urlArgument = urlArgument.replace(/^unix:/, 'http://$&');
urlObj = urlToOptions(new URL(urlArgument));
} else if (is.urlInstance(urlArgument)) {
urlObj = urlToOptions(urlArgument);
} else if (options.prefixUrl) {
urlObj = {
...urlToOptions(new URL(options.prefixUrl)),
...urlArgument
};
} else {
urlObj = urlArgument;
}
// Override both null/undefined with default protocol
options = merge<NormalizedOptions, Partial<URL | URLOptions | NormalizedOptions | Options>>({path: ''} as NormalizedOptions, urlObj, {protocol: urlObj.protocol || 'https:'}, options);
for (const hook of options.hooks.init) {
const isCalled = hook(options);
if (is.promise(isCalled)) {
throw new TypeError('The `init` hook must be a synchronous function');
}
}
const {prefixUrl} = options;
Object.defineProperty(options, 'prefixUrl', {
set: () => {
throw new Error('Failed to set prefixUrl. Options are normalized already.');
},
get: () => prefixUrl
});
let {searchParams} = options;
delete options.searchParams;
// TODO: Remove this before Got v11
if (options.query) {
if (!hasShownDeprecation) {
console.warn('`options.query` is deprecated. We support it solely for compatibility - it will be removed in Got 11. Use `options.searchParams` instead.');
hasShownDeprecation = true;
}
searchParams = options.query;
delete options.query;
}
if (is.nonEmptyString(searchParams) || is.nonEmptyObject(searchParams) || (searchParams && searchParams! instanceof URLSearchParams)) {
if (!is.string(searchParams)) {
if (!(searchParams instanceof URLSearchParams)) {
validateSearchParams(searchParams);
}
searchParams = (new URLSearchParams(searchParams as Record<string, string>)).toString();
}
options.path = `${options.path.split('?')[0]}?${searchParams}`;
}
if (options.hostname === 'unix') {
const matches = /(.+?):(.+)/.exec(options.path);
if (matches) {
const [, socketPath, path] = matches;
options = {
...options,
socketPath,
path,
host: ''
};
}
}
const {headers} = options;
for (const [key, value] of Object.entries(headers)) {
if (is.nullOrUndefined(value)) {
delete headers[key];
}
}
if (options.decompress && is.undefined(headers['accept-encoding'])) {
headers['accept-encoding'] = supportsBrotli ? 'gzip, deflate, br' : 'gzip, deflate';
}
if (options.method) {
options.method = options.method.toUpperCase() as Method;
}
if (!is.function_(options.retry.retries)) {
const {retries} = options.retry;
options.retry.retries = (iteration, error) => {
if (iteration > retries) {
return 0;
}
const hasMethod = options.retry.methods.has((error as GotError).options.method as Method);
const hasErrorCode = Reflect.has(error, 'code') && options.retry.errorCodes.has((error as GotError).code as ErrorCode);
const hasStatusCode = Reflect.has(error, 'response') && options.retry.statusCodes.has((error as HTTPError | ParseError | MaxRedirectsError).response.statusCode as StatusCode);
if (!hasMethod || (!hasErrorCode && !hasStatusCode)) {
return 0;
}
const {response} = error as HTTPError | ParseError | MaxRedirectsError;
if (response && Reflect.has(response.headers, 'retry-after') && retryAfterStatusCodes.has(response.statusCode as StatusCode)) {
let after = Number(response.headers['retry-after']);
if (is.nan(after)) {
after = Date.parse(response.headers['retry-after']!) - Date.now();
} else {
after *= 1000;
}
if (after > options.retry.maxRetryAfter) {
return 0;
}
return after;
}
if (response && response.statusCode === 413) {
return 0;
}
const noise = Math.random() * 100;
return ((2 ** (iteration - 1)) * 1000) + noise;
};
}
return options;
};
export const reNormalizeArguments = (options: NormalizedOptions): NormalizedOptions => normalizeArguments(format(options as unknown as URL | URLOptions), options);