rock-req 5.0.1 → 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.
Files changed (2) hide show
  1. package/index.js +6 -7
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -85,7 +85,6 @@ 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
@@ -111,7 +110,6 @@ function extend (defaultOptions = {}) {
111
110
  }
112
111
  return rock(opts, cb)
113
112
  }
114
-
115
113
  // or read response and leave at the end
116
114
  response = res
117
115
  const contentEncoding = opts.method !== 'HEAD' ? (res.headers['content-encoding'] || '').toLowerCase() : ''
@@ -127,16 +125,18 @@ function extend (defaultOptions = {}) {
127
125
  }
128
126
  })
129
127
  req.once('timeout', () => {
128
+ // This timeout event can come after the input pipeline is finished (ex. timeout with no body)
130
129
  const _error = new Error('TimeoutError'); _error.code = 'ETIMEDOUT'
131
- req.destroy() // we must destroy manually
132
- onRequestEnd(_error) // This timeout event can come after the input pipeline is finished (ex. timeout with no body)
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()
133
135
  })
134
-
135
136
  const _inputStream = isFnStream(body) ? body(opts) : Readable.from([body], { objectMode: false })
136
137
  pipeline(_inputStream, req, (e) => {
137
138
  if (e) onRequestEnd(e)
138
139
  })
139
-
140
140
  return req
141
141
  }
142
142
 
@@ -153,6 +153,5 @@ function extend (defaultOptions = {}) {
153
153
  rock.concat = rock
154
154
  rock.defaults = _default
155
155
  rock.extend = extend
156
-
157
156
  return rock
158
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.1",
4
+ "version": "5.0.2",
5
5
  "author": {
6
6
  "name": "David Grelaud & Feross Aboukhadijeh"
7
7
  },