rock-req 5.3.0 → 5.3.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.
- package/index.js +11 -6
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -43,6 +43,7 @@ function extend (defaultOptions = {}) {
|
|
|
43
43
|
defaultOptions.headers = applyDefault(cloneLowerCase(defaultOptions.headers), _default.headers)
|
|
44
44
|
_default = applyDefault(defaultOptions, _default) // inherits of parent options
|
|
45
45
|
const idleTimeout = Number(_default.keepAliveTimeout ?? 5000) // ms, like Node's globalAgent. Like decompress (true), not in defaults: not copied in each request (faster)
|
|
46
|
+
const sensitiveHeaders = _default.sensitiveHeaders ?? ['authorization', 'cookie', 'proxy-authorization', 'x-auth-token'] // dropped on redirect to another host or from https to http
|
|
46
47
|
const agents = [http, https].map(h => {
|
|
47
48
|
if (!(_default.keepAliveDuration > 0)) return undefined
|
|
48
49
|
class RockAgent extends h.Agent {
|
|
@@ -65,7 +66,11 @@ function extend (defaultOptions = {}) {
|
|
|
65
66
|
|
|
66
67
|
function retry (opts, cb, wait = 0) { // failure only: retryDelay * 2^n + jitter, capped by retryMaxDelay, at least Retry-After
|
|
67
68
|
const d = opts.retryDelay * 2 ** (opts.maxRetry - opts.remainingRetry - 1)
|
|
68
|
-
setTimeout(
|
|
69
|
+
setTimeout(next, Math.max(wait, Math.min(opts.retryMaxDelay ?? 2000, d + Math.random() * d)), opts, cb)
|
|
70
|
+
}
|
|
71
|
+
function next (opts, cb) { // retry or redirect: called from an event handler, so an invalid request (bad location, port...) must not throw
|
|
72
|
+
let called = false
|
|
73
|
+
try { rock(opts, (err, res, data) => { called = true; return cb(err, res, data) }) } catch (e) { if (called) throw e; cb(e) } // an exception of the caller's callback is not swallowed, never a second call
|
|
69
74
|
}
|
|
70
75
|
|
|
71
76
|
function rock (opts, directBody, cb) {
|
|
@@ -151,14 +156,14 @@ function extend (defaultOptions = {}) {
|
|
|
151
156
|
res.resume() // Discard response, consume data until the end to free up memory. Mandatory!
|
|
152
157
|
delete opts.headers.host // Discard `host` header on redirect (see #32)
|
|
153
158
|
opts.url = res.headers.location
|
|
154
|
-
|
|
159
|
+
let redirectTo
|
|
160
|
+
try { redirectTo = url.parse(opts.url) } catch (e) { return cb(e) } // eslint-disable-line node/no-deprecated-api
|
|
155
161
|
if (redirectTo.hostname === null) { // relative redirect
|
|
156
162
|
opts.url = null
|
|
157
163
|
Object.assign(opts, originalRequest)
|
|
158
164
|
opts.path = redirectTo.path
|
|
159
|
-
} else if (redirectTo.hostname !== originalRequest.hostname
|
|
160
|
-
delete opts.headers.
|
|
161
|
-
delete opts.headers.authorization
|
|
165
|
+
} else if (redirectTo.hostname !== originalRequest.hostname || (redirectTo.protocol === 'http:' && originalRequest.protocol === 'https:')) {
|
|
166
|
+
for (const h of sensitiveHeaders) delete opts.headers[h] // other host or https to http: drop credentials (#73). The body of a 307/308 is still sent
|
|
162
167
|
}
|
|
163
168
|
if ((opts.method === 'POST' && [301, 302].includes(res.statusCode)) || (res.statusCode === 303 && opts.method && opts.method !== 'GET' && opts.method !== 'HEAD')) {
|
|
164
169
|
opts.method = 'GET' // On 301/302 redirect, change POST to GET (see #35). On 303, change all methods except HEAD to GET (RFC 9110)
|
|
@@ -168,7 +173,7 @@ function extend (defaultOptions = {}) {
|
|
|
168
173
|
requestAbortedOrEnded = false
|
|
169
174
|
return onRequestEnd(new Error('too many redirects'))
|
|
170
175
|
}
|
|
171
|
-
return
|
|
176
|
+
return next(opts, cb)
|
|
172
177
|
}
|
|
173
178
|
// or read response and leave at the end
|
|
174
179
|
response = res
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rock-req",
|
|
3
3
|
"description": "Ultra-light (150 LOC, No dependencies) & Ultra-fast request library with reliable retry on failure, http/https, redirects, gzip/deflate/brotli, extensible, proxy, streams, JSON mode, forms, timeout",
|
|
4
|
-
"version": "5.3.
|
|
4
|
+
"version": "5.3.1",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "David Grelaud & Feross Aboukhadijeh"
|
|
7
7
|
},
|