Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
module.exports = function getHttpResponseData (httpContentBuffer) {
const parser = new HTTPParser(HTTPParser.RESPONSE);
let httpData = {};
parser[HTTPParser.kOnMessageComplete] = noop;
parser[HTTPParser.kOnHeaders] = noop;
// Get headers and parse them to an object format for easier use
parser[HTTPParser.kOnHeadersComplete] = function (meta) {
const headerObject = {};
for (let i = 0; i < meta.headers.length; i += 2) {
headerObject[meta.headers[i]] = meta.headers[i + 1];
}
httpData = xtend(httpData, meta, {
headers: headerObject
});
};
// The below function can fire multiple times for "transfer-encoding: chunked"
this.paused = false
}, 1000)
}
this.timeoutTicker = retimer(handleTimeout, this.timeout)
this.parser[HTTPParser.kOnHeaders] = () => {}
this.parser[HTTPParser.kOnHeadersComplete] = (opts) => {
this.emit('headers', opts)
this.resData[this.cer].headers = opts
}
this.parser[HTTPParser.kOnBody] = (body) => {
this.emit('body', body)
}
this.parser[HTTPParser.kOnMessageComplete] = () => {
const end = process.hrtime(this.resData[this.cer].startTime)
const responseTime = end[0] * 1e3 + end[1] / 1e6
this.emit('response', this.resData[this.cer].headers.statusCode, this.resData[this.cer].bytes, responseTime)
this.resData[this.cer].bytes = 0
if (!this.destroyed && this.reconnectRate && this.reqsMade % this.reconnectRate === 0) {
return this._resetConnection()
}
this.cer = this.cer === opts.pipelining - 1 ? 0 : this.cer++
this._doRequest(this.cer)
}
this._connect()
}
cb(null, {
statusCode,
headers: parseHeaders(headers),
body: this._lastBody
})
if (this.closed && this[kQueue].length() === 0) {
this.destroy()
}
}
this.parser[HTTPParser.kOnBody] = (chunk, offset, length) => {
this._lastBody.push(chunk.slice(offset, offset + length))
}
this.parser[HTTPParser.kOnMessageComplete] = () => {
const body = this._lastBody
this._lastBody = null
body.push(null)
}
this[kReadCb] = () => {
this[kIsWaiting] = false
this[kRead]()
}
}