rock-req 5.0.0 → 5.0.1

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.
Files changed (3) hide show
  1. package/README.md +8 -5
  2. package/index.js +9 -6
  3. 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 all the request succeed or all retries are done
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 = `${encodeURIComponent(hostname)}/${port}${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 redirect
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
@@ -90,11 +90,14 @@ function extend (defaultOptions = {}) {
90
90
  if (opts.followRedirects !== false && res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
91
91
  requestAbortedOrEnded = true // discard all new events which could come after for this request to avoid calling the callback
92
92
  res.resume() // Discard response, consume data until the end to free up memory. Mandatory!
93
- opts.url = res.headers.location // Follow 3xx redirects
94
93
  delete opts.headers.host // Discard `host` header on redirect (see #32)
95
- const redirectHost = url.parse(opts.url).hostname // eslint-disable-line node/no-deprecated-api
96
- // If redirected host is different than original host, drop headers to prevent cookie leak (#73)
97
- if (redirectHost !== null && redirectHost !== originalHost) {
94
+ opts.url = res.headers.location
95
+ const redirectTo = url.parse(opts.url) // eslint-disable-line node/no-deprecated-api
96
+ if (redirectTo.hostname === null) { // relative redirect
97
+ opts.url = null
98
+ Object.assign(opts, originalRequest)
99
+ opts.path = redirectTo.path
100
+ } else if (redirectTo.hostname !== originalRequest.hostname) { // If redirected host is different than original host, drop headers to prevent cookie leak (#73)
98
101
  delete opts.headers.cookie
99
102
  delete opts.headers.authorization
100
103
  }
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.0",
4
+ "version": "5.0.1",
5
5
  "author": {
6
6
  "name": "David Grelaud & Feross Aboukhadijeh"
7
7
  },