got 7.0.0 → 7.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +25 -28
- package/package.json +8 -7
- package/readme.md +32 -15
package/index.js
CHANGED
|
@@ -10,6 +10,7 @@ const isStream = require('is-stream');
|
|
|
10
10
|
const getStream = require('get-stream');
|
|
11
11
|
const timedOut = require('timed-out');
|
|
12
12
|
const urlParseLax = require('url-parse-lax');
|
|
13
|
+
const urlToOptions = require('url-to-options');
|
|
13
14
|
const lowercaseKeys = require('lowercase-keys');
|
|
14
15
|
const decompressResponse = require('decompress-response');
|
|
15
16
|
const isRetryAllowed = require('is-retry-allowed');
|
|
@@ -85,9 +86,14 @@ function requestAsEventEmitter(opts) {
|
|
|
85
86
|
}
|
|
86
87
|
|
|
87
88
|
setImmediate(() => {
|
|
88
|
-
const response =
|
|
89
|
+
const response = opts.decompress === true &&
|
|
90
|
+
typeof decompressResponse === 'function' &&
|
|
89
91
|
req.method !== 'HEAD' ? decompressResponse(res) : res;
|
|
90
92
|
|
|
93
|
+
if (!opts.decompress && ['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) {
|
|
94
|
+
opts.encoding = null;
|
|
95
|
+
}
|
|
96
|
+
|
|
91
97
|
response.redirectUrls = redirects;
|
|
92
98
|
|
|
93
99
|
ee.emit('response', response);
|
|
@@ -257,29 +263,22 @@ function asStream(opts) {
|
|
|
257
263
|
function normalizeArguments(url, opts) {
|
|
258
264
|
if (typeof url !== 'string' && typeof url !== 'object') {
|
|
259
265
|
throw new TypeError(`Parameter \`url\` must be a string or object, not ${typeof url}`);
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
if (typeof url === 'string') {
|
|
266
|
+
} else if (typeof url === 'string') {
|
|
263
267
|
url = url.replace(/^unix:/, 'http://$&');
|
|
264
268
|
url = urlParseLax(url);
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
throw new Error('Basic authentication must be done with auth option');
|
|
268
|
-
}
|
|
269
|
+
} else if (isURL.lenient(url)) {
|
|
270
|
+
url = urlToOptions(url);
|
|
269
271
|
}
|
|
270
272
|
|
|
271
|
-
if (
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
if (url.auth) {
|
|
275
|
-
throw new Error('Basic authentication must be done with auth option');
|
|
276
|
-
}
|
|
273
|
+
if (url.auth) {
|
|
274
|
+
throw new Error('Basic authentication must be done with auth option');
|
|
277
275
|
}
|
|
278
276
|
|
|
279
277
|
opts = Object.assign(
|
|
280
278
|
{
|
|
281
279
|
path: '',
|
|
282
280
|
retries: 2,
|
|
281
|
+
decompress: true,
|
|
283
282
|
useElectronNet: true
|
|
284
283
|
},
|
|
285
284
|
url,
|
|
@@ -316,17 +315,18 @@ function normalizeArguments(url, opts) {
|
|
|
316
315
|
throw new TypeError('options.body must be a ReadableStream, string, Buffer or plain Object');
|
|
317
316
|
}
|
|
318
317
|
|
|
319
|
-
|
|
320
|
-
|
|
318
|
+
const canBodyBeStringified = isPlainObj(body) || Array.isArray(body);
|
|
319
|
+
if ((opts.form || opts.json) && !canBodyBeStringified) {
|
|
320
|
+
throw new TypeError('options.body must be a plain Object or Array when options.form or options.json is used');
|
|
321
321
|
}
|
|
322
322
|
|
|
323
323
|
if (isStream(body) && typeof body.getBoundary === 'function') {
|
|
324
324
|
// Special case for https://github.com/form-data/form-data
|
|
325
325
|
headers['content-type'] = headers['content-type'] || `multipart/form-data; boundary=${body.getBoundary()}`;
|
|
326
|
-
} else if (opts.form &&
|
|
326
|
+
} else if (opts.form && canBodyBeStringified) {
|
|
327
327
|
headers['content-type'] = headers['content-type'] || 'application/x-www-form-urlencoded';
|
|
328
328
|
opts.body = querystring.stringify(body);
|
|
329
|
-
} else if (opts.json &&
|
|
329
|
+
} else if (opts.json && canBodyBeStringified) {
|
|
330
330
|
headers['content-type'] = headers['content-type'] || 'application/json';
|
|
331
331
|
opts.body = JSON.stringify(body);
|
|
332
332
|
}
|
|
@@ -342,7 +342,7 @@ function normalizeArguments(url, opts) {
|
|
|
342
342
|
}
|
|
343
343
|
|
|
344
344
|
if (opts.hostname === 'unix') {
|
|
345
|
-
const matches = /(
|
|
345
|
+
const matches = /(.+?):(.+)/.exec(opts.path);
|
|
346
346
|
|
|
347
347
|
if (matches) {
|
|
348
348
|
opts.socketPath = matches[1];
|
|
@@ -389,7 +389,9 @@ function got(url, opts) {
|
|
|
389
389
|
}
|
|
390
390
|
}
|
|
391
391
|
|
|
392
|
-
|
|
392
|
+
got.stream = (url, opts) => asStream(normalizeArguments(url, opts));
|
|
393
|
+
|
|
394
|
+
const methods = [
|
|
393
395
|
'get',
|
|
394
396
|
'post',
|
|
395
397
|
'put',
|
|
@@ -398,14 +400,9 @@ const helpers = [
|
|
|
398
400
|
'delete'
|
|
399
401
|
];
|
|
400
402
|
|
|
401
|
-
|
|
402
|
-
got[
|
|
403
|
-
});
|
|
404
|
-
|
|
405
|
-
got.stream = (url, opts) => asStream(normalizeArguments(url, opts));
|
|
406
|
-
|
|
407
|
-
for (const el of helpers) {
|
|
408
|
-
got.stream[el] = (url, opts) => got.stream(url, Object.assign({}, opts, {method: el}));
|
|
403
|
+
for (const method of methods) {
|
|
404
|
+
got[method] = (url, opts) => got(url, Object.assign({}, opts, {method}));
|
|
405
|
+
got.stream[method] = (url, opts) => got.stream(url, Object.assign({}, opts, {method}));
|
|
409
406
|
}
|
|
410
407
|
|
|
411
408
|
class StdError extends Error {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "got",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.1.0",
|
|
4
4
|
"description": "Simplified HTTP requests",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/got",
|
|
@@ -58,22 +58,23 @@
|
|
|
58
58
|
"is-stream": "^1.0.0",
|
|
59
59
|
"isurl": "^1.0.0-alpha5",
|
|
60
60
|
"lowercase-keys": "^1.0.0",
|
|
61
|
-
"p-cancelable": "^0.
|
|
61
|
+
"p-cancelable": "^0.3.0",
|
|
62
62
|
"p-timeout": "^1.1.1",
|
|
63
63
|
"safe-buffer": "^5.0.1",
|
|
64
64
|
"timed-out": "^4.0.0",
|
|
65
|
-
"url-parse-lax": "^1.0.0"
|
|
65
|
+
"url-parse-lax": "^1.0.0",
|
|
66
|
+
"url-to-options": "^1.0.1"
|
|
66
67
|
},
|
|
67
68
|
"devDependencies": {
|
|
68
|
-
"ava": "^0.
|
|
69
|
+
"ava": "^0.20.0",
|
|
69
70
|
"coveralls": "^2.11.4",
|
|
70
71
|
"form-data": "^2.1.1",
|
|
71
72
|
"get-port": "^3.0.0",
|
|
72
73
|
"into-stream": "^3.0.0",
|
|
73
|
-
"nyc": "^
|
|
74
|
+
"nyc": "^11.0.2",
|
|
74
75
|
"pem": "^1.4.4",
|
|
75
|
-
"pify": "^
|
|
76
|
-
"tempfile": "^
|
|
76
|
+
"pify": "^3.0.0",
|
|
77
|
+
"tempfile": "^2.0.0",
|
|
77
78
|
"tempy": "^0.1.0",
|
|
78
79
|
"universal-url": "^1.0.0-alpha",
|
|
79
80
|
"xo": "^0.18.0"
|
package/readme.md
CHANGED
|
@@ -12,16 +12,24 @@
|
|
|
12
12
|
|
|
13
13
|
A nicer interface to the built-in [`http`](http://nodejs.org/api/http.html) module.
|
|
14
14
|
|
|
15
|
-
It supports following redirects, promises, streams, retries, automagically handling gzip/deflate, canceling of requests, and some convenience options.
|
|
16
|
-
|
|
17
15
|
Created because [`request`](https://github.com/request/request) is bloated *(several megabytes!)*.
|
|
18
16
|
|
|
19
|
-
When used with Electron, it takes advantage of [`electron.net`](https://electron.atom.io/docs/api/net/).
|
|
20
17
|
|
|
18
|
+
## Highlights
|
|
21
19
|
|
|
22
|
-
|
|
20
|
+
- [Promise & stream API](#api)
|
|
21
|
+
- [Request cancelation](#aborting-the-request)
|
|
22
|
+
- [Follows redirects](#followredirect)
|
|
23
|
+
- [Retries on network failure](#retries)
|
|
24
|
+
- [Handles gzip/deflate](#decompress)
|
|
25
|
+
- [Timeout handling](#timeout)
|
|
26
|
+
- [Errors with metadata](#errors)
|
|
27
|
+
- [JSON mode](#json)
|
|
28
|
+
- [WHATWG URL support](#url)
|
|
29
|
+
- [Electron support](#useelectronnet)
|
|
23
30
|
|
|
24
|
-
|
|
31
|
+
|
|
32
|
+
## Install
|
|
25
33
|
|
|
26
34
|
```
|
|
27
35
|
$ npm install --save got
|
|
@@ -62,7 +70,7 @@ Returns a Promise for a `response` object with a `body` property, a `url` proper
|
|
|
62
70
|
|
|
63
71
|
##### url
|
|
64
72
|
|
|
65
|
-
Type: `string
|
|
73
|
+
Type: `string` `Object`
|
|
66
74
|
|
|
67
75
|
The URL to request as simple string, a [`http.request` options](https://nodejs.org/api/http.html#http_http_request_options_callback), or a [WHATWG `URL`](https://nodejs.org/api/url.html#url_class_url).
|
|
68
76
|
|
|
@@ -70,13 +78,13 @@ Properties from `options` will override properties in the parsed `url`.
|
|
|
70
78
|
|
|
71
79
|
##### options
|
|
72
80
|
|
|
73
|
-
Type: `
|
|
81
|
+
Type: `Object`
|
|
74
82
|
|
|
75
83
|
Any of the [`http.request`](http://nodejs.org/api/http.html#http_http_request_options_callback) options.
|
|
76
84
|
|
|
77
85
|
###### body
|
|
78
86
|
|
|
79
|
-
Type: `string
|
|
87
|
+
Type: `string` `Buffer` `stream.Readable`
|
|
80
88
|
|
|
81
89
|
*This is mutually exclusive with stream mode.*
|
|
82
90
|
|
|
@@ -88,10 +96,10 @@ If `content-length` or `transfer-encoding` is not set in `options.headers` and `
|
|
|
88
96
|
|
|
89
97
|
###### encoding
|
|
90
98
|
|
|
91
|
-
Type: `string
|
|
99
|
+
Type: `string` `null`<br>
|
|
92
100
|
Default: `'utf8'`
|
|
93
101
|
|
|
94
|
-
Encoding to be used on `setEncoding` of the response data. If `null`, the body is returned as a Buffer.
|
|
102
|
+
[Encoding](https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings) to be used on `setEncoding` of the response data. If `null`, the body is returned as a Buffer.
|
|
95
103
|
|
|
96
104
|
###### form
|
|
97
105
|
|
|
@@ -102,7 +110,7 @@ Default: `false`
|
|
|
102
110
|
|
|
103
111
|
If set to `true` and `Content-Type` header is not set, it will be set to `application/x-www-form-urlencoded`.
|
|
104
112
|
|
|
105
|
-
`body` must be a plain object and will be stringified.
|
|
113
|
+
`body` must be a plain object or array and will be stringified.
|
|
106
114
|
|
|
107
115
|
###### json
|
|
108
116
|
|
|
@@ -115,17 +123,17 @@ If set to `true` and `Content-Type` header is not set, it will be set to `applic
|
|
|
115
123
|
|
|
116
124
|
Parse response body with `JSON.parse` and set `accept` header to `application/json`. If used in conjunction with the `form` option, the `body` will the stringified as querystring and the response parsed as JSON.
|
|
117
125
|
|
|
118
|
-
`body` must be a plain object and will be stringified.
|
|
126
|
+
`body` must be a plain object or array and will be stringified.
|
|
119
127
|
|
|
120
128
|
###### query
|
|
121
129
|
|
|
122
|
-
Type: `string
|
|
130
|
+
Type: `string` `Object`<br>
|
|
123
131
|
|
|
124
132
|
Query string object that will be added to the request URL. This will override the query string in `url`.
|
|
125
133
|
|
|
126
134
|
###### timeout
|
|
127
135
|
|
|
128
|
-
Type: `number
|
|
136
|
+
Type: `number` `Object`
|
|
129
137
|
|
|
130
138
|
Milliseconds to wait for the server to end the response before aborting request with `ETIMEDOUT` error.
|
|
131
139
|
|
|
@@ -133,7 +141,7 @@ This also accepts an object with separate `connect`, `socket`, and `request` fie
|
|
|
133
141
|
|
|
134
142
|
###### retries
|
|
135
143
|
|
|
136
|
-
Type: `number
|
|
144
|
+
Type: `number` `Function`<br>
|
|
137
145
|
Default: `2`
|
|
138
146
|
|
|
139
147
|
Number of request retries when network errors happens. Delays between retries counts with function `1000 * Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 0).
|
|
@@ -152,6 +160,15 @@ Defines if redirect responses should be followed automatically.
|
|
|
152
160
|
Note that if a `303` is sent by the server in response to any request type (`POST`, `DELETE`, etc.), got will automatically
|
|
153
161
|
request the resource pointed to in the location header via `GET`. This is in accordance with [the spec](https://tools.ietf.org/html/rfc7231#section-6.4.4).
|
|
154
162
|
|
|
163
|
+
###### decompress
|
|
164
|
+
|
|
165
|
+
Type: `boolean`<br>
|
|
166
|
+
Default: `true`
|
|
167
|
+
|
|
168
|
+
Decompress the response automatically.
|
|
169
|
+
|
|
170
|
+
If this is disabled, a compressed response is returned as a `Buffer`. This may be useful if you want to handle decompression yourself or stream the raw compressed data.
|
|
171
|
+
|
|
155
172
|
###### useElectronNet
|
|
156
173
|
|
|
157
174
|
Type: `boolean`<br>
|