Skip to content

Commit

Permalink
fix: switch to Centra followRedirects
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanent committed Apr 11, 2024
1 parent abcf851 commit c071f95
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
16 changes: 10 additions & 6 deletions lib/phin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const {URL} = require('url')

const centra = require('centra')

const unspecifiedFollowRedirectsDefault = 20

/**
* 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).
* @typedef {Object} phinOptions
Expand Down Expand Up @@ -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])
Expand All @@ -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

Expand Down
30 changes: 26 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
"node": ">= 8"
},
"dependencies": {
"centra": "^2.6.0"
"centra": "^2.7.0"
}
}

0 comments on commit c071f95

Please sign in to comment.