Skip to content

Commit

Permalink
feat: add followRedirects, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan7g committed Apr 10, 2024
1 parent d26c192 commit 88028b3
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 24 deletions.
2 changes: 1 addition & 1 deletion createRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ const CentraRequest = require('./model/CentraRequest.js')

module.exports = (url, method) => {
return new CentraRequest(url, method)
}
}
42 changes: 34 additions & 8 deletions model/CentraRequest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require('path')
const http = require('http')
const https = require('https')
const followRedirects = require('follow-redirects')
const qs = require('querystring')
const zlib = require('zlib')
const {URL} = require('url')
Expand All @@ -9,6 +10,27 @@ const CentraResponse = require('./CentraResponse.js')

const supportedCompressions = ['gzip', 'deflate', 'br']

const useRequest = (protocol, maxRedirects) => {
let httpr
let httpsr
if (maxRedirects <= 0) {
httpr = http.request
httpsr = https.request
}
else {
httpr = followRedirects.http.request
httpsr = followRedirects.https.request
}

if (protocol === 'http:') {
return httpr
}
else if (protocol === 'https:') {
return httpsr
}
else throw new Error('Bad URL protocol: ' + protocol)
}

module.exports = class CentraRequest {
constructor (url, method = 'GET') {
this.url = typeof url === 'string' ? new URL(url) : url
Expand All @@ -20,6 +42,7 @@ module.exports = class CentraRequest {
this.compressionEnabled = false
this.timeoutTime = null
this.coreOptions = {}
this.maxRedirects = 0

this.resOptions = {
'maxBuffer': 50 * 1000000 // 50 MB
Expand All @@ -28,6 +51,12 @@ module.exports = class CentraRequest {
return this
}

followRedirects(n) {
this.maxRedirects = n

return this
}

query (a1, a2) {
if (typeof a1 === 'object') {
Object.keys(a1).forEach((queryKey) => {
Expand Down Expand Up @@ -112,7 +141,8 @@ module.exports = class CentraRequest {
'port': this.url.port,
'path': this.url.pathname + (this.url.search === null ? '' : this.url.search),
'method': this.method,
'headers': this.reqHeaders
'headers': this.reqHeaders,
'maxRedirects': this.maxRedirects
}, this.coreOptions)

let req
Expand Down Expand Up @@ -164,13 +194,9 @@ module.exports = class CentraRequest {
}
}

if (this.url.protocol === 'http:') {
req = http.request(options, resHandler)
}
else if (this.url.protocol === 'https:') {
req = https.request(options, resHandler)
}
else throw new Error('Bad URL protocol: ' + this.url.protocol)
const request = useRequest(this.url.protocol, this.maxRedirects)

req = request(options, resHandler)

if (this.timeoutTime) {
req.setTimeout(this.timeoutTime, () => {
Expand Down
2 changes: 1 addition & 1 deletion model/CentraResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ module.exports = class CentraResponse {
async text () {
return this.body.toString()
}
}
}
Loading

0 comments on commit 88028b3

Please sign in to comment.