Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const { body } = opts;
if (this._agent) {
opts.agent = this._agent;
}
if (body && typeof body === 'object' && typeof body.pipe !== 'function') {
opts.headers['Content-Type'] = 'application/json';
opts.body = new JsonBody(body);
}
if (
body &&
typeof body === 'object' &&
typeof body.pipe === 'function'
) {
opts.body = new StreamBody(body);
}
const handleCompleted = async res => {
currentContext.ongoingFetches--;
if (
currentContext !== this._currContext &&
currentContext.ongoingFetches <= 0
) {
// We've completely moved on to a new socket
// close the old one
// TODO: Fix race condition:
// If the response is a stream, and the server is still streaming data
// we should check if the stream has closed before disconnecting
// hasCompleted CAN technically be called before the res body stream is closed
debug('Closing old socket');
const { body } = opts
if (this._agent) {
opts.agent = this._agent
}
if (body && typeof body === 'object' && typeof body.pipe !== 'function') {
opts.headers['Content-Type'] = 'application/json'
if (shouldUseHttp2(this)) {
opts.body = new JsonBody(body)
} else {
opts.body = JSON.stringify(body)
}
}
if(shouldUseHttp2(this) && body && typeof body === 'object' && typeof body.pipe === 'function') {
opts.body = new StreamBody(body)
}
if (!shouldUseHttp2(this) && opts.body && typeof body.pipe !== 'function') {
opts.headers['Content-Length'] = Buffer.byteLength(opts.body)
}
const handleCompleted = async (res) => {
currentContext.ongoingFetches--;
if(currentContext !== this._currContext && currentContext.ongoingFetches <= 0) {
// We've completely moved on to a new socket
// close the old one
// TODO: Fix race condition:
// If the response is a stream, and the server is still streaming data
// we should check if the stream has closed before disconnecting
// hasCompleted CAN technically be called before the res body stream is closed