WIP: initial prototype

- Added asStream option
- Better handling of request object
This commit is contained in:
delvedor
2018-11-15 17:50:56 +01:00
parent e15546541e
commit 11b0d50751
2 changed files with 15 additions and 9 deletions

View File

@ -11,6 +11,7 @@ class Connection {
constructor (opts = {}) {
this.url = opts.url
this.ssl = opts.ssl || null
// TODO: clean user:password from id
this.id = opts.id || opts.url.href
this.headers = opts.headers || null
this.deadCount = 0
@ -25,7 +26,7 @@ class Connection {
keepAliveMsecs: 1000,
maxSockets: Infinity,
maxFreeSockets: 256
}, opts.agent || opts.agent)
}, opts.agent)
this._agent = this.url.protocol === 'http:'
? new http.Agent(agentOptions)
: new https.Agent(Object.assign({}, agentOptions, this.ssl))
@ -38,7 +39,6 @@ class Connection {
request (params, callback) {
this._openRequests++
var ended = false
params.agent = this._agent
debug('Starting a new request', params)
const request = this.makeRequest(this.buildRequestObject(params))
@ -49,7 +49,12 @@ class Connection {
if (ended === false) {
ended = true
this._openRequests--
callback(null, decompressResponse(response))
if (params.asStream === true) {
callback(null, response)
} else {
callback(null, decompressResponse(response))
}
}
})
@ -131,7 +136,8 @@ class Connection {
headers: this.headers,
auth: !!url.username === true || !!url.password === true
? `${url.username}:${url.password}`
: undefined
: undefined,
agent: this._agent
}
const paramsKeys = Object.keys(params)

View File

@ -80,7 +80,8 @@ class Transport {
// handles request timeout
params.timeout = toMs(params.requestTimeout || this.requestTimeout)
this.emit('request', params)
// TODO: expose nicely the node metadata (also in response an error)
this.emit('request', params, connection)
// perform the actual http request
const request = connection.request(params, (err, response) => {
@ -116,10 +117,9 @@ class Transport {
result.warnings = headers['warning'].split(/(?!\B"[^"]*),(?![^"]*"\B)/)
}
// TODO: expose `asStream` option for returning the
// body already parsed?
if (params.asHttpResponse === true) {
callback(null, response)
if (params.asStream === true) {
result.body = response
callback(null, result)
return
}