Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
delete opts.body;
delete opts.json;
delete opts.timeout;
if (json) {
opts.headers.accept = opts.headers.accept || 'application/json';
}
if (body) {
if (typeof body !== 'string' && !Buffer.isBuffer(body) && !isStream.readable(body)) {
throw new GotError('options.body must be a ReadableStream, string or Buffer');
}
opts.method = opts.method || 'POST';
if (!opts.headers['content-length'] && !opts.headers['transfer-encoding'] && !isStream.readable(body)) {
var length = typeof body === 'string' ? Buffer.byteLength(body) : body.length;
opts.headers['content-length'] = length;
}
}
opts.method = opts.method || 'GET';
// returns a proxy stream to the response
// if no callback has been provided
if (!cb) {
proxy = duplexify();
// forward errors on the stream
cb = function (err, data, response) {
proxy.emit('error', err, data, response);
};
const isBufferOrStream = obj => Buffer.isBuffer(obj) || isStream.readable(obj) || isSource(obj)
// An object like { content?, path? }, where content isBufferOrStream and path isString
return content.map((data) => {
// Buffer input
if (Buffer.isBuffer(data)) {
data = { path: '', content: pull.values([data]) }
}
// Readable stream input
if (isStream.readable(data)) {
data = { path: '', content: toPull.source(data) }
}
if (isSource(data)) {
data = { path: '', content: data }
}
if (data && data.content && typeof data.content !== 'function') {
if (Buffer.isBuffer(data.content)) {
data.content = pull.values([data.content])
}
if (isStream.readable(data.content)) {
data.content = toPull.source(data.content)
}
}
function fileToJson (file, opts) {
opts = opts || {}
if (isBuffer(file)) { // Buffer
if (opts.onProgressIncrement) opts.onProgressIncrement(file.length)
return bufferToJson(file)
} else if (isStream.readable(file)) { // Node stream
return pullStreamToJson(toPull.source(file), opts)
} else if (isSource(file)) { // Pull stream
return pullStreamToJson(file, opts)
} else if (file && file.content) { // Object { path?, content }
return Object.assign({}, file, { content: fileToJson(file.content, opts) })
}
return file // Object { path } maybe, but could be anything
}
err = new GotError('Parsing ' + url + ' response failed', e);
}
}
cb(err, data, response);
});
}).once('error', function (err) {
cb(new GotError('Request to ' + url + ' failed', err));
});
if (timeout) {
timedOut(req, timeout);
}
if (!proxy) {
if (isStream.readable(body)) {
body.pipe(req);
} else {
req.end(body);
}
return;
}
if (body) {
proxy.write = function () {
throw new Error('got\'s stream is not writable when options.body is used');
};
if (isStream.readable(body)) {
body.pipe(req);
} else {
err = new GotError('Parsing ' + url + ' response failed', e);
}
}
cb(err, data, response);
});
}).once('error', function (err) {
cb(new GotError('Request to ' + url + ' failed', err));
});
if (timeout) {
timedOut(req, timeout);
}
if (!proxy) {
if (isStream.readable(body)) {
body.pipe(req);
} else {
req.end(body);
}
return;
}
if (body) {
proxy.write = function () {
throw new Error('got\'s stream is not writable when options.body is used');
};
if (isStream.readable(body)) {
body.pipe(req);
} else {
if (obj === null || obj === undefined) {
res.end()
return
}
if (Buffer.isBuffer(obj)) {
if (!res.getHeader('Content-Type')) {
res.setHeader('Content-Type', 'application/octet-stream')
}
res.setHeader('Content-Length', obj.length)
res.end(obj)
return
}
if (obj instanceof Stream || readable(obj)) {
if (!res.getHeader('Content-Type')) {
res.setHeader('Content-Type', 'application/octet-stream')
}
obj.pipe(res)
return
}
let jsonify = false
let str = obj
switch (typeof str) {
case 'object':
jsonify = true
break
case 'number':
function isReadable(stream) {
return isStream.readable(stream)
}
function isWritable(stream) {
const add = promisify((_files, options, _callback) => {
if (typeof options === 'function') {
_callback = options
options = null
}
const callback = once(_callback)
if (!options) {
options = {}
}
options.converter = FileResultStreamConverter
const ok = Buffer.isBuffer(_files) ||
isStream.readable(_files) ||
Array.isArray(_files) ||
OtherBuffer.isBuffer(_files) ||
typeof _files === 'object' ||
isSource(_files)
if (!ok) {
return callback(new Error('first arg must be a buffer, readable stream, pull stream, an object or array of objects'))
}
const files = [].concat(_files)
const stream = createAddStream({ qs: options })
const concat = ConcatStream((result) => callback(null, result))
stream.once('error', callback)
stream.pipe(concat)