Skip to content

Commit c071f95

Browse files
committed
fix: switch to Centra followRedirects
1 parent abcf851 commit c071f95

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

lib/phin.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const {URL} = require('url')
22

33
const centra = require('centra')
44

5+
const unspecifiedFollowRedirectsDefault = 20
6+
57
/**
68
* phin options object. phin also supports all options from <a href="https://nodejs.org/api/http.html#http_http_request_options_callback">http.request(options, callback)</a> by passing them on to this method (or similar).
79
* @typedef {Object} phinOptions
@@ -49,6 +51,14 @@ const phin = async (opts) => {
4951
if (opts.form) req.body(opts.form, 'form')
5052
if (opts.compression) req.compress()
5153

54+
if (opts.followRedirects) {
55+
if (opts.followRedirects === true) {
56+
req.followRedirects(unspecifiedFollowRedirectsDefault)
57+
} else if (typeof opts.followRedirects === 'number') {
58+
req.followRedirects(opts.followRedirects)
59+
}
60+
}
61+
5262
if (typeof opts.core === 'object') {
5363
Object.keys(opts.core).forEach((optName) => {
5464
req.option(optName, opts.core[optName])
@@ -57,12 +67,6 @@ const phin = async (opts) => {
5767

5868
const res = await req.send()
5969

60-
if (res.headers.hasOwnProperty('location') && opts.followRedirects) {
61-
opts.url = (new URL(res.headers['location'], opts.url)).toString()
62-
63-
return await phin(opts)
64-
}
65-
6670
if (opts.stream) {
6771
res.stream = res
6872

package-lock.json

+26-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@
3535
"node": ">= 8"
3636
},
3737
"dependencies": {
38-
"centra": "^2.6.0"
38+
"centra": "^2.7.0"
3939
}
4040
}

0 commit comments

Comments
 (0)