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

View File

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