Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
opts.path = `${opts.path.split('?')[0]}?${opts.query}`;
delete opts.query;
}
if (opts.json && is.undefined(opts.headers.accept)) {
opts.headers.accept = 'application/json';
}
const body = opts.body;
if (is.nullOrUndefined(body)) {
opts.method = (opts.method || 'GET').toUpperCase();
} else {
const headers = opts.headers;
if (!is.nodeStream(body) && !is.string(body) && !is.buffer(body) && !(opts.form || opts.json)) {
throw new TypeError('The `body` option must be a stream.Readable, string, Buffer or plain Object');
}
const canBodyBeStringified = is.plainObject(body) || is.array(body);
if ((opts.form || opts.json) && !canBodyBeStringified) {
throw new TypeError('The `body` option must be a plain Object or Array when the `form` or `json` option is used');
}
if (isFormData(body)) {
// Special case for https://github.com/form-data/form-data
headers['content-type'] = headers['content-type'] || `multipart/form-data; boundary=${body.getBoundary()}`;
} else if (opts.form && canBodyBeStringified) {
headers['content-type'] = headers['content-type'] || 'application/x-www-form-urlencoded';
opts.body = querystring.stringify(body);
} else if (opts.json && canBodyBeStringified) {
headers['content-type'] = headers['content-type'] || 'application/json';
}
opts.path = `${opts.path.split('?')[0]}?${opts.query}`;
delete opts.query;
}
if (opts.json && is.undefined(opts.headers.accept)) {
opts.headers.accept = 'application/json';
}
const body = opts.body;
if (is.nullOrUndefined(body)) {
opts.method = (opts.method || 'GET').toUpperCase();
} else {
const headers = opts.headers;
if (!is.nodeStream(body) && !is.string(body) && !is.buffer(body) && !(opts.form || opts.json)) {
throw new TypeError('The `body` option must be a stream.Readable, string, Buffer or plain Object');
}
const canBodyBeStringified = is.plainObject(body) || is.array(body);
if ((opts.form || opts.json) && !canBodyBeStringified) {
throw new TypeError('The `body` option must be a plain Object or Array when the `form` or `json` option is used');
}
if (isFormData(body)) {
// Special case for https://github.com/form-data/form-data
headers['content-type'] = headers['content-type'] || `multipart/form-data; boundary=${body.getBoundary()}`;
} else if (opts.form && canBodyBeStringified) {
headers['content-type'] = headers['content-type'] || 'application/x-www-form-urlencoded';
opts.body = querystring.stringify(body);
} else if (opts.json && canBodyBeStringified) {
headers['content-type'] = headers['content-type'] || 'application/json';
} else if (opts.form && canBodyBeStringified) {
headers['content-type'] = headers['content-type'] || 'application/x-www-form-urlencoded';
opts.body = querystring.stringify(body);
} else if (opts.json && canBodyBeStringified) {
headers['content-type'] = headers['content-type'] || 'application/json';
opts.body = JSON.stringify(body);
}
if (is.undefined(headers['content-length']) && is.undefined(headers['transfer-encoding']) && !is.nodeStream(body)) {
const length = is.string(opts.body) ? Buffer.byteLength(opts.body) : opts.body.length;
headers['content-length'] = length;
}
// Convert buffer to stream to receive upload progress events
// see https://github.com/sindresorhus/got/pull/322
if (is.buffer(body)) {
opts.body = intoStream(body);
opts.body._buffer = body;
}
opts.method = (opts.method || 'POST').toUpperCase();
}
if (opts.hostname === 'unix') {
const matches = /(.+?):(.+)/.exec(opts.path);
if (matches) {
opts.socketPath = matches[1];
opts.path = matches[2];
opts.host = null;
}
}
test('reuses a TLS session', wrapper, async (t, server) => {
const agent = new Agent();
const session = await agent.getSession(server.url);
await setImmediateAsync();
const tlsSession = agent.tlsSessionCache.get(`${Agent.normalizeAuthority(server.url)}:`).session;
session.close();
await pEvent(session, 'close');
const secondSession = await agent.getSession(server.url);
await setImmediateAsync();
t.deepEqual(secondSession.socket.getSession(), tlsSession);
t.true(is.buffer(tlsSession));
agent.destroy();
});
export default async (options: Options): Promise => {
const {body, headers} = options;
if (headers && 'content-length' in headers) {
return Number(headers['content-length']);
}
if (!body) {
return 0;
}
if (is.string(body)) {
return Buffer.byteLength(body);
}
if (is.buffer(body)) {
return body.length;
}
if (isFormData(body)) {
return promisify(body.getLength.bind(body))();
}
if (body instanceof ReadStream) {
const {size} = await statAsync(body.path);
return size;
}
return undefined;
};
return 0;
}
if (is.string(body)) {
return Buffer.byteLength(body);
}
if (isFormData(body)) {
return pify(body.getLength.bind(body))();
}
if (body instanceof fs.ReadStream) {
return pify(fs.stat)(body.path).then(stat => stat.size);
}
if (is.nodeStream(body) && is.buffer(body._buffer)) {
return body._buffer.length;
}
return null;
};