diff --git a/lib/phin.js b/lib/phin.js index 59c165d..ae9cbe3 100644 --- a/lib/phin.js +++ b/lib/phin.js @@ -2,6 +2,8 @@ const {URL} = require('url') const centra = require('centra') +const unspecifiedFollowRedirectsDefault = 20 + /** * phin options object. phin also supports all options from http.request(options, callback) by passing them on to this method (or similar). * @typedef {Object} phinOptions @@ -49,6 +51,14 @@ const phin = async (opts) => { if (opts.form) req.body(opts.form, 'form') if (opts.compression) req.compress() + if (opts.followRedirects) { + if (opts.followRedirects === true) { + req.followRedirects(unspecifiedFollowRedirectsDefault) + } else if (typeof opts.followRedirects === 'number') { + req.followRedirects(opts.followRedirects) + } + } + if (typeof opts.core === 'object') { Object.keys(opts.core).forEach((optName) => { req.option(optName, opts.core[optName]) @@ -57,12 +67,6 @@ const phin = async (opts) => { const res = await req.send() - if (res.headers.hasOwnProperty('location') && opts.followRedirects) { - opts.url = (new URL(res.headers['location'], opts.url)).toString() - - return await phin(opts) - } - if (opts.stream) { res.stream = res diff --git a/package-lock.json b/package-lock.json index 3d1fc6a..9c72574 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,16 +9,38 @@ "version": "3.7.0", "license": "MIT", "dependencies": { - "centra": "^2.6.0" + "centra": "^2.7.0" }, "engines": { "node": ">= 8" } }, "node_modules/centra": { - "version": "2.6.0", - "resolved": "https://registry.npmjs.org/centra/-/centra-2.6.0.tgz", - "integrity": "sha512-dgh+YleemrT8u85QL11Z6tYhegAs3MMxsaWAq/oXeAmYJ7VxL3SI9TZtnfaEvNDMAPolj25FXIb3S+HCI4wQaQ==" + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/centra/-/centra-2.7.0.tgz", + "integrity": "sha512-PbFMgMSrmgx6uxCdm57RUos9Tc3fclMvhLSATYN39XsDV29B89zZ3KA89jmY0vwSGazyU+uerqwa6t+KaodPcg==", + "dependencies": { + "follow-redirects": "^1.15.6" + } + }, + "node_modules/follow-redirects": { + "version": "1.15.6", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", + "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } } } } diff --git a/package.json b/package.json index cfc54a0..bc1852e 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,6 @@ "node": ">= 8" }, "dependencies": { - "centra": "^2.6.0" + "centra": "^2.7.0" } }