rock-req 5.0.0 → 5.0.2
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/README.md +8 -5
- package/index.js +15 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,11 +7,14 @@
|
|
|
7
7
|
|
|
8
8
|
<p align="center">Ensure your HTTP requests always reach their destination!</p>
|
|
9
9
|
|
|
10
|
-
[![javascript style guide][standard-image]][standard-url]
|
|
10
|
+
[![npm][npm-image]][npm-url] [![ci][ci-image]][ci-url] [![javascript style guide][standard-image]][standard-url]
|
|
11
11
|
|
|
12
12
|
[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg
|
|
13
13
|
[standard-url]: https://standardjs.com
|
|
14
|
-
|
|
14
|
+
[npm-image]: https://img.shields.io/npm/v/rock-req.svg
|
|
15
|
+
[npm-url]: https://npmjs.org/package/rock-req
|
|
16
|
+
[ci-url]: https://github.com/carboneio/rock-req/actions
|
|
17
|
+
[ci-image]: https://img.shields.io/github/actions/workflow/status/carboneio/rock-req/ci.yml?branch=master
|
|
15
18
|
|
|
16
19
|
## 🔥 Why should you need this?
|
|
17
20
|
|
|
@@ -230,7 +233,7 @@ rock(opts, function (err, res) {})
|
|
|
230
233
|
|
|
231
234
|
By default, rock-req retries with the following errors if `maxRetry > 1`.
|
|
232
235
|
|
|
233
|
-
The callback is called when
|
|
236
|
+
The callback is called when the request succeed or all retries are done
|
|
234
237
|
|
|
235
238
|
```js
|
|
236
239
|
const rock = require('rock-req');
|
|
@@ -316,7 +319,7 @@ Headers are merged. Then only the first level of the options object is merged (n
|
|
|
316
319
|
Here is a basic example of `beforeRequest` interceptor to use [HAProxy as a forward proxy](https://www.haproxy.com/user-spotlight-series/haproxy-as-egress-controller/).
|
|
317
320
|
|
|
318
321
|
`beforeRequest` is always called on each redirect/retry.
|
|
319
|
-
- on redirect, `opts.url` (and `hostname`, `port`, `protocol`, `path`) is updated to the new location
|
|
322
|
+
- on redirect, `opts.url` (and `hostname`, `port`, `protocol`, `path`) is updated to the new location. `opts.url` is null if it is a relative redirect.
|
|
320
323
|
- on retry, `opts.url` (and `hostname`, `port`, `protocol`, `path`) have the same value as they did
|
|
321
324
|
when the rock-req was initially called.
|
|
322
325
|
|
|
@@ -328,7 +331,7 @@ const myInstance = rock.extend({
|
|
|
328
331
|
opts.protocol = 'http:';
|
|
329
332
|
opts.hostname = '10.0.0.1';
|
|
330
333
|
opts.port = 80;
|
|
331
|
-
opts.path = `${
|
|
334
|
+
opts.path = `${hostname}/${port}${path}`;
|
|
332
335
|
return opts;
|
|
333
336
|
},
|
|
334
337
|
headers: {
|
package/index.js
CHANGED
|
@@ -36,9 +36,10 @@ function extend (defaultOptions = {}) {
|
|
|
36
36
|
|
|
37
37
|
if (opts.url) {
|
|
38
38
|
const { hostname, port, protocol, auth, path } = url.parse(opts.url) // eslint-disable-line node/no-deprecated-api
|
|
39
|
-
if (!hostname && !port && !protocol && !auth) opts.path = path // Relative
|
|
39
|
+
if (!hostname && !port && !protocol && !auth) opts.path = path // Relative path with hostname set
|
|
40
40
|
else Object.assign(opts, { hostname, port, protocol, auth, path }) // Absolute redirect
|
|
41
41
|
}
|
|
42
|
+
const originalRequest = { hostname: opts.hostname, port: opts.port, protocol: opts.protocol, auth: opts.auth, path: opts.path }
|
|
42
43
|
opts = opts.beforeRequest(opts)
|
|
43
44
|
|
|
44
45
|
let body
|
|
@@ -58,7 +59,6 @@ function extend (defaultOptions = {}) {
|
|
|
58
59
|
if (opts.json) opts.headers.accept = 'application/json'
|
|
59
60
|
if (opts.method) opts.method = opts.method.toUpperCase()
|
|
60
61
|
|
|
61
|
-
const originalHost = opts.hostname // hostname before potential redirect
|
|
62
62
|
const protocol = opts.protocol === 'https:' ? https : http // Support http/https urls
|
|
63
63
|
const chunks = []
|
|
64
64
|
let requestAbortedOrEnded = false
|
|
@@ -85,16 +85,18 @@ function extend (defaultOptions = {}) {
|
|
|
85
85
|
res.resume() // Discard response, consume data until the end to free up memory. Mandatory!
|
|
86
86
|
return setTimeout(rock, opts.retryDelay, opts, cb) // retry later
|
|
87
87
|
}
|
|
88
|
-
|
|
89
88
|
// or redirect and leave
|
|
90
89
|
if (opts.followRedirects !== false && res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
|
91
90
|
requestAbortedOrEnded = true // discard all new events which could come after for this request to avoid calling the callback
|
|
92
91
|
res.resume() // Discard response, consume data until the end to free up memory. Mandatory!
|
|
93
|
-
opts.url = res.headers.location // Follow 3xx redirects
|
|
94
92
|
delete opts.headers.host // Discard `host` header on redirect (see #32)
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
if (
|
|
93
|
+
opts.url = res.headers.location
|
|
94
|
+
const redirectTo = url.parse(opts.url) // eslint-disable-line node/no-deprecated-api
|
|
95
|
+
if (redirectTo.hostname === null) { // relative redirect
|
|
96
|
+
opts.url = null
|
|
97
|
+
Object.assign(opts, originalRequest)
|
|
98
|
+
opts.path = redirectTo.path
|
|
99
|
+
} else if (redirectTo.hostname !== originalRequest.hostname) { // If redirected host is different than original host, drop headers to prevent cookie leak (#73)
|
|
98
100
|
delete opts.headers.cookie
|
|
99
101
|
delete opts.headers.authorization
|
|
100
102
|
}
|
|
@@ -108,7 +110,6 @@ function extend (defaultOptions = {}) {
|
|
|
108
110
|
}
|
|
109
111
|
return rock(opts, cb)
|
|
110
112
|
}
|
|
111
|
-
|
|
112
113
|
// or read response and leave at the end
|
|
113
114
|
response = res
|
|
114
115
|
const contentEncoding = opts.method !== 'HEAD' ? (res.headers['content-encoding'] || '').toLowerCase() : ''
|
|
@@ -124,16 +125,18 @@ function extend (defaultOptions = {}) {
|
|
|
124
125
|
}
|
|
125
126
|
})
|
|
126
127
|
req.once('timeout', () => {
|
|
128
|
+
// This timeout event can come after the input pipeline is finished (ex. timeout with no body)
|
|
127
129
|
const _error = new Error('TimeoutError'); _error.code = 'ETIMEDOUT'
|
|
128
|
-
req.destroy() // we must destroy manually
|
|
129
|
-
|
|
130
|
+
req.destroy(_error) // we must destroy manually and send the error to the error listener to call onRequestEnd
|
|
131
|
+
})
|
|
132
|
+
req.once('error', (e) => {
|
|
133
|
+
onRequestEnd(e) // error can happen before pipeline is executed when some interceptor are used such as nock
|
|
134
|
+
req.destroy()
|
|
130
135
|
})
|
|
131
|
-
|
|
132
136
|
const _inputStream = isFnStream(body) ? body(opts) : Readable.from([body], { objectMode: false })
|
|
133
137
|
pipeline(_inputStream, req, (e) => {
|
|
134
138
|
if (e) onRequestEnd(e)
|
|
135
139
|
})
|
|
136
|
-
|
|
137
140
|
return req
|
|
138
141
|
}
|
|
139
142
|
|
|
@@ -150,6 +153,5 @@ function extend (defaultOptions = {}) {
|
|
|
150
153
|
rock.concat = rock
|
|
151
154
|
rock.defaults = _default
|
|
152
155
|
rock.extend = extend
|
|
153
|
-
|
|
154
156
|
return rock
|
|
155
157
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rock-req",
|
|
3
3
|
"description": "Zero dependencies (160 LOC) & rock-solid request library: http/https, reliable retry on failure, redirects, gzip/deflate/brotli, extensible, proxy, streams, JSON mode, forms, timeout",
|
|
4
|
-
"version": "5.0.
|
|
4
|
+
"version": "5.0.2",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "David Grelaud & Feross Aboukhadijeh"
|
|
7
7
|
},
|