Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
{
// TODO: these checks should be moved to `preNormalizeArguments`
const isForm = !is.undefined(options.form);
const isJSON = !is.undefined(options.json);
const isBody = !is.undefined(options.body);
if ((isBody || isForm || isJSON) && withoutBody.has(options.method)) {
throw new TypeError(`The \`${options.method}\` method cannot be used with a body`);
}
if ([isBody, isForm, isJSON].filter(isTrue => isTrue).length > 1) {
throw new TypeError('The `body`, `json` and `form` options are mutually exclusive');
}
if (
isBody &&
!is.nodeStream(options.body) &&
!is.string(options.body) &&
!is.buffer(options.body) &&
!(is.object(options.body) && isFormData(options.body))
) {
throw new TypeError('The `body` option must be a stream.Readable, string or Buffer');
}
if (isForm && !is.object(options.form)) {
throw new TypeError('The `form` option must be an Object');
}
}
if (options.body) {
// Special case for https://github.com/form-data/form-data
if (is.object(options.body) && isFormData(options.body) && noContentType) {
headers['content-type'] = `multipart/form-data; boundary=${options.body.getBoundary()}`;
ee.on('request', req => {
proxy.emit('request', req);
if (is.nodeStream(opts.body)) {
opts.body.pipe(req);
return;
}
if (opts.body) {
req.end(opts.body);
return;
}
if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') {
input.pipe(req);
return;
}
req.end();
});
timings = timer(request);
progress.upload(request, emitter, uploadBodySize);
if (options.gotTimeout) {
timedOut(request, options.gotTimeout, options);
}
emitter.emit('request', request);
const uploadComplete = () => {
request.emit('upload-complete');
};
try {
if (is.nodeStream(options.body)) {
options.body.once('end', uploadComplete);
options.body.pipe(request);
options.body = undefined;
} else if (options.body) {
request.end(options.body, uploadComplete);
} else if (input && (options.method === 'POST' || options.method === 'PUT' || options.method === 'PATCH')) {
input.once('end', uploadComplete);
input.pipe(request);
} else {
request.end(uploadComplete);
}
} catch (error) {
emitError(new RequestError(error, options));
}
};
ee.on('request', req => {
proxy.emit('request', req);
if (is.nodeStream(opts.body)) {
opts.body.pipe(req);
return;
}
if (opts.body) {
req.end(opts.body);
return;
}
if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') {
input.pipe(req);
return;
}
req.end();
});
module.exports = body => is.nodeStream(body) && is.function(body.getBoundary);