forked from sindresorhus/got
-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalize-arguments.js
206 lines (172 loc) · 6.52 KB
/
normalize-arguments.js
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
'use strict';
const URLSearchParamsGlobal = typeof URLSearchParams === 'undefined' ? require('url').URLSearchParams : URLSearchParams; // TODO: Use the `URL` global when targeting Node.js 10
const is = require('@sindresorhus/is');
const toReadableStream = require('to-readable-stream');
const urlParseLax = require('url-parse-lax');
const isRetryOnNetworkErrorAllowed = require('./is-retry-on-network-error-allowed');
const urlToOptions = require('./url-to-options');
const isFormData = require('./is-form-data');
const retryAfterStatusCodes = new Set([413, 429, 503]);
module.exports = (url, options, defaults) => {
if (Reflect.has(options, 'url') || (is.object(url) && Reflect.has(url, 'url'))) {
throw new TypeError('Parameter `url` is not an option. Use got(url, options)');
}
if (!is.string(url) && !is.object(url)) {
throw new TypeError(`Parameter \`url\` must be a string or object, not ${is(url)}`);
} else if (is.string(url)) {
url = url.replace(/^unix:/, 'http://$&');
try {
decodeURI(url);
} catch (_) {
throw new Error('Parameter `url` must contain valid UTF-8 character sequences');
}
url = urlParseLax(url);
if (url.auth) {
throw new Error('Basic authentication must be done with the `auth` option');
}
} else if (is(url) === 'URL') {
url = urlToOptions(url);
}
options = {
path: '',
...url,
protocol: url.protocol || 'http:', // Override both null/undefined with default protocol
...options
};
if (options.decompress && is.undefined(options.headers['accept-encoding'])) {
options.headers['accept-encoding'] = 'gzip, deflate';
}
const {query} = options;
if (query) {
if (!is.string(query)) {
options.query = (new URLSearchParamsGlobal(query)).toString();
}
options.path = `${options.path.split('?')[0]}?${options.query}`;
delete options.query;
}
if (options.json && is.undefined(options.headers.accept)) {
options.headers.accept = 'application/json';
}
const {body} = options;
if (is.nullOrUndefined(body)) {
options.method = (options.method || 'GET').toUpperCase();
} else {
const {headers} = options;
const isObject = is.object(body) && !Buffer.isBuffer(body) && !is.nodeStream(body);
if (!is.nodeStream(body) && !is.string(body) && !is.buffer(body) && !(options.form || options.json)) {
throw new TypeError('The `body` option must be a stream.Readable, string or Buffer');
}
if (options.json && !(isObject || is.array(body))) {
throw new TypeError('The `body` option must be an Object or Array when the `json` option is used');
}
if (options.form && !isObject) {
throw new TypeError('The `body` option must be an Object when the `form` option is used');
}
if (isFormData(body)) {
// Special case for https://github.com/form-data/form-data
headers['content-type'] = headers['content-type'] || `multipart/form-data; boundary=${body.getBoundary()}`;
} else if (options.form) {
headers['content-type'] = headers['content-type'] || 'application/x-www-form-urlencoded';
options.body = (new URLSearchParamsGlobal(body)).toString();
} else if (options.json) {
headers['content-type'] = headers['content-type'] || 'application/json';
options.body = JSON.stringify(body);
}
if (is.undefined(headers['content-length']) && is.undefined(headers['transfer-encoding']) && !is.nodeStream(body)) {
const length = is.string(options.body) ? Buffer.byteLength(options.body) : options.body.length;
headers['content-length'] = length;
}
// Convert buffer to stream to receive upload progress events (#322)
if (is.buffer(body)) {
options.body = toReadableStream(body);
options.body._buffer = body;
}
options.method = (options.method || 'POST').toUpperCase();
}
if (options.hostname === 'unix') {
const matches = /(.+?):(.+)/.exec(options.path);
if (matches) {
const [, socketPath, path] = matches;
options = {
...options,
socketPath,
path,
host: null
};
}
}
options.gotRetry = {retries: 0, methods: [], statusCodes: []};
if (options.retry !== false) {
if (is.number(options.retry)) {
if (is.object(defaults.options.retry)) {
options.gotRetry = {...defaults.options.retry, retries: options.retry};
} else {
options.gotRetry.retries = options.retry;
}
} else {
options.gotRetry = {...options.gotRetry, ...options.retry};
}
delete options.retry;
}
options.gotRetry.methods = new Set(options.gotRetry.methods.map(method => method.toUpperCase()));
options.gotRetry.statusCodes = new Set(options.gotRetry.statusCodes);
if (!options.gotRetry.maxRetryAfter && Reflect.has(options, 'timeout')) {
if (is.number(options.timeout)) {
options.gotRetry.maxRetryAfter = options.timeout;
} else {
options.gotRetry.maxRetryAfter = Math.min(...[options.timeout.request, options.timeout.connection].filter(n => !is.nullOrUndefined(n)));
}
}
if (!is.function(options.gotRetry.retries)) {
const {retries} = options.gotRetry;
options.gotRetry.retries = (iteration, error) => {
if (iteration > retries || (!isRetryOnNetworkErrorAllowed(error) && (!options.gotRetry.methods.has(error.method) || !options.gotRetry.statusCodes.has(error.statusCode)))) {
return 0;
}
if (Reflect.has(error, 'headers') && Reflect.has(error.headers, 'retry-after') && retryAfterStatusCodes.has(error.statusCode)) {
let after = Number(error.headers['retry-after']);
if (is.number(after)) {
after *= 1000;
} else {
after = Math.max(Date.parse(error.headers['retry-after']) - Date.now(), 0);
}
if (after > options.gotRetry.maxRetryAfter) {
return 0;
}
return after;
}
if (error.statusCode === 413) {
return 0;
}
const noise = Math.random() * 100;
return ((1 << iteration) * 1000) + noise;
};
}
if (is.undefined(options.followRedirect)) {
options.followRedirect = true;
}
if (is.number(options.timeout) || is.object(options.timeout)) {
if (is.number(options.timeout)) {
options.gotTimeout = {request: options.timeout};
} else {
options.gotTimeout = options.timeout;
}
delete options.timeout;
}
if (is.object(options.hooks)) {
for (const [hookEvent, hooks] of Object.entries(options.hooks)) {
if (is.array(hooks)) {
for (const [index, hook] of Object.entries(hooks)) {
if (!is.function(hook)) {
throw new TypeError(`Parameter \`hooks.${hookEvent}[${index}]\` must be a function, not ${is(hook)}`);
}
}
} else {
throw new TypeError(`Parameter \`hooks.${hookEvent}\` must be an array, not ${is(hooks)}`);
}
}
} else {
throw new TypeError(`Parameter \`hooks\` must be an object, not ${is(options.hooks)}`);
}
return options;
};