You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

345 lines
8.0 KiB

'use strict';
const EventEmitter = require('events').EventEmitter;
const http = require('http');
const https = require('https');
const PassThrough = require('stream').PassThrough;
const duplexer2 = require('duplexer2');
const urlLib = require('url');
const querystring = require('querystring');
const objectAssign = require('object-assign');
const isStream = require('is-stream');
const getStream = require('get-stream');
const timedOut = require('timed-out');
const urlParseLax = require('url-parse-lax');
const lowercaseKeys = require('lowercase-keys');
const isRedirect = require('is-redirect');
const unzipResponse = require('unzip-response');
const createErrorClass = require('create-error-class');
const nodeStatusCodes = require('node-status-codes');
const isPlainObj = require('is-plain-obj');
function requestAsEventEmitter(opts) {
opts = opts || {};
const ee = new EventEmitter();
let redirectCount = 0;
let retryCount = 0;
10 years ago
const get = opts => {
const fn = opts.protocol === 'https:' ? https : http;
const req = fn.request(opts, res => {
const statusCode = res.statusCode;
if (isRedirect(statusCode) && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
res.resume();
11 years ago
if (++redirectCount > 10) {
ee.emit('error', new got.MaxRedirectsError(statusCode, opts), null, res);
11 years ago
return;
}
const redirectUrl = urlLib.resolve(urlLib.format(opts), res.headers.location);
const redirectOpts = objectAssign({}, opts, urlLib.parse(redirectUrl));
ee.emit('redirect', res, redirectOpts);
get(redirectOpts);
11 years ago
return;
}
setImmediate(function () {
ee.emit('response', typeof unzipResponse === 'function' && req.method !== 'HEAD' ? unzipResponse(res) : res);
});
});
req.once('error', err => {
const backoff = opts.retries(++retryCount, err);
if (backoff) {
setTimeout(get, backoff, opts);
return;
}
ee.emit('error', new got.RequestError(err, opts));
});
if (opts.timeout) {
timedOut(req, opts.timeout);
}
setImmediate(() => ee.emit('request', req));
};
get(opts);
return ee;
}
function asPromise(opts) {
return new Promise((resolve, reject) => {
const ee = requestAsEventEmitter(opts);
10 years ago
ee.on('request', req => {
if (isStream(opts.body)) {
opts.body.pipe(req);
opts.body = undefined;
return;
}
req.end(opts.body);
});
ee.on('response', res => {
const stream = opts.encoding === null ? getStream.buffer(res) : getStream(res, opts);
stream
.then(data => {
let err;
const statusCode = res.statusCode;
res.body = data;
if (statusCode < 200 || statusCode > 299) {
err = new got.HTTPError(statusCode, opts);
}
if (opts.json && statusCode !== 204) {
try {
res.body = JSON.parse(res.body);
} catch (e) {
err = new got.ParseError(e, opts, data);
}
}
if (err) {
Object.defineProperty(err, 'response', {value: res});
reject(err);
return;
}
resolve(res);
})
.catch(err => {
err = new got.ReadError(err, opts);
Object.defineProperty(err, 'response', {value: res});
reject(err);
});
});
ee.on('error', reject);
});
}
function asStream(opts) {
const input = new PassThrough();
const output = new PassThrough();
const proxy = duplexer2(input, output);
if (opts.json) {
throw new Error('got can not be used as stream when options.json is used');
}
if (opts.body) {
proxy.write = () => {
throw new Error('got\'s stream is not writable when options.body is used');
};
}
const ee = requestAsEventEmitter(opts);
ee.on('request', req => {
proxy.emit('request', req);
if (isStream.readable(opts.body)) {
opts.body.pipe(req);
return;
}
10 years ago
if (opts.body) {
req.end(opts.body);
return;
}
if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') {
input.pipe(req);
return;
}
req.end();
});
ee.on('response', res => {
const statusCode = res.statusCode;
res.pipe(output);
if (statusCode < 200 || statusCode > 299) {
proxy.emit('error', new got.HTTPError(statusCode, opts), null, res);
return;
}
proxy.emit('response', res);
});
11 years ago
ee.on('redirect', proxy.emit.bind(proxy, 'redirect'));
ee.on('error', proxy.emit.bind(proxy, 'error'));
return proxy;
}
function normalizeArguments(url, opts) {
if (typeof url !== 'string' && typeof url !== 'object') {
throw new Error(`Parameter \`url\` must be a string or object, not ${typeof url}`);
}
if (typeof url === 'string') {
url = urlParseLax(url);
if (url.auth) {
throw new Error('Basic authentication must be done with auth option');
}
}
opts = objectAssign(
{protocol: 'http:', path: '', retries: 5},
url,
opts
);
opts.headers = objectAssign({
'user-agent': 'https://github.com/sindresorhus/got',
'accept-encoding': 'gzip,deflate'
}, lowercaseKeys(opts.headers));
const query = opts.query;
9 years ago
if (query) {
if (typeof query !== 'string') {
opts.query = querystring.stringify(query);
}
opts.path = `${opts.path.split('?')[0]}?${opts.query}`;
delete opts.query;
}
if (opts.json && opts.headers.accept === undefined) {
opts.headers.accept = 'application/json';
}
let body = opts.body;
9 years ago
if (body) {
if (typeof body !== 'string' && !Buffer.isBuffer(body) && !isStream.readable(body) && !isPlainObj(body)) {
throw new Error('options.body must be a ReadableStream, string, Buffer or plain Object');
}
opts.method = opts.method || 'POST';
if (isPlainObj(body)) {
opts.headers['content-type'] = opts.headers['content-type'] || 'application/x-www-form-urlencoded';
body = opts.body = querystring.stringify(body);
}
if (opts.headers['content-length'] === undefined && opts.headers['transfer-encoding'] === undefined && !isStream.readable(body)) {
const length = typeof body === 'string' ? Buffer.byteLength(body) : body.length;
opts.headers['content-length'] = length;
}
}
opts.method = opts.method || 'GET';
if (opts.hostname === 'unix') {
const matches = /(.+)\:(.+)/.exec(opts.path);
if (matches) {
opts.socketPath = matches[1];
opts.path = matches[2];
opts.host = null;
}
}
if (typeof opts.retries !== 'function') {
const retries = opts.retries;
opts.retries = function backoff(iter) {
if (iter > retries) {
return 0;
}
const noise = Math.random() * 100;
return (1 << iter) * 1000 + noise;
};
}
return opts;
}
function got(url, opts) {
try {
return asPromise(normalizeArguments(url, opts));
} catch (error) {
return Promise.reject(error);
}
}
const helpers = [
'get',
'post',
'put',
'patch',
'head',
'delete'
];
helpers.forEach(el => {
got[el] = (url, opts) => got(url, objectAssign({}, opts, {method: el.toUpperCase()}));
});
got.stream = function (url, opts) {
return asStream(normalizeArguments(url, opts));
};
helpers.forEach(el => {
got.stream[el] = function (url, opts) {
return got.stream(url, objectAssign({}, opts, {method: el.toUpperCase()}));
};
});
function stdError(error, opts) {
if (error.code !== undefined) {
this.code = error.code;
}
objectAssign(this, {
message: error.message,
host: opts.host,
hostname: opts.hostname,
method: opts.method,
path: opts.path
});
}
got.RequestError = createErrorClass('RequestError', stdError);
got.ReadError = createErrorClass('ReadError', stdError);
got.ParseError = createErrorClass('ParseError', function (e, opts, data) {
stdError.call(this, e, opts);
this.message = `${e.message} in "${urlLib.format(opts)}": \n${data.slice(0, 77)}...`;
});
got.HTTPError = createErrorClass('HTTPError', function (statusCode, opts) {
stdError.call(this, {}, opts);
this.statusCode = statusCode;
this.statusMessage = nodeStatusCodes[this.statusCode];
this.message = `Response code ${this.statusCode} (${this.statusMessage})`;
});
got.MaxRedirectsError = createErrorClass('MaxRedirectsError', function (statusCode, opts) {
stdError.call(this, {}, opts);
this.statusCode = statusCode;
this.statusMessage = nodeStatusCodes[this.statusCode];
this.message = 'Redirected 10 times. Aborting.';
});
module.exports = got;