2 const EventEmitter = require('events');
3 const http = require('http');
4 const https = require('https');
5 const PassThrough = require('stream').PassThrough;
6 const urlLib = require('url');
7 const querystring = require('querystring');
8 const duplexer3 = require('duplexer3');
9 const isStream = require('is-stream');
10 const getStream = require('get-stream');
11 const timedOut = require('timed-out');
12 const urlParseLax = require('url-parse-lax');
13 const lowercaseKeys = require('lowercase-keys');
14 const isRedirect = require('is-redirect');
15 const unzipResponse = require('unzip-response');
16 const createErrorClass = require('create-error-class');
17 const isRetryAllowed = require('is-retry-allowed');
18 const Buffer = require('safe-buffer').Buffer;
19 const pkg = require('./package');
21 function requestAsEventEmitter(opts) {
24 const ee = new EventEmitter();
25 const requestUrl = opts.href || urlLib.resolve(urlLib.format(opts), opts.path);
26 let redirectCount = 0;
31 const fn = opts.protocol === 'https:' ? https : http;
33 const req = fn.request(opts, res => {
34 const statusCode = res.statusCode;
36 if (isRedirect(statusCode) && opts.followRedirect && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
39 if (++redirectCount > 10) {
40 ee.emit('error', new got.MaxRedirectsError(statusCode, opts), null, res);
44 const bufferString = Buffer.from(res.headers.location, 'binary').toString();
46 redirectUrl = urlLib.resolve(urlLib.format(opts), bufferString);
47 const redirectOpts = Object.assign({}, opts, urlLib.parse(redirectUrl));
49 ee.emit('redirect', res, redirectOpts);
57 const response = typeof unzipResponse === 'function' && req.method !== 'HEAD' ? unzipResponse(res) : res;
58 response.url = redirectUrl || requestUrl;
59 response.requestUrl = requestUrl;
61 ee.emit('response', response);
65 req.once('error', err => {
66 const backoff = opts.retries(++retryCount, err);
69 setTimeout(get, backoff, opts);
73 ee.emit('error', new got.RequestError(err, opts));
76 if (opts.gotTimeout) {
77 timedOut(req, opts.gotTimeout);
81 ee.emit('request', req);
89 function asPromise(opts) {
90 return new Promise((resolve, reject) => {
91 const ee = requestAsEventEmitter(opts);
93 ee.on('request', req => {
94 if (isStream(opts.body)) {
96 opts.body = undefined;
103 ee.on('response', res => {
104 const stream = opts.encoding === null ? getStream.buffer(res) : getStream(res, opts);
107 .catch(err => reject(new got.ReadError(err, opts)))
109 const statusCode = res.statusCode;
110 const limitStatusCode = opts.followRedirect ? 299 : 399;
114 if (opts.json && res.body) {
116 res.body = JSON.parse(res.body);
118 throw new got.ParseError(e, statusCode, opts, data);
122 if (statusCode < 200 || statusCode > limitStatusCode) {
123 throw new got.HTTPError(statusCode, opts);
129 Object.defineProperty(err, 'response', {value: res});
134 ee.on('error', reject);
138 function asStream(opts) {
139 const input = new PassThrough();
140 const output = new PassThrough();
141 const proxy = duplexer3(input, output);
144 throw new Error('got can not be used as stream when options.json is used');
148 proxy.write = () => {
149 throw new Error('got\'s stream is not writable when options.body is used');
153 const ee = requestAsEventEmitter(opts);
155 ee.on('request', req => {
156 proxy.emit('request', req);
158 if (isStream(opts.body)) {
168 if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') {
176 ee.on('response', res => {
177 const statusCode = res.statusCode;
181 if (statusCode < 200 || statusCode > 299) {
182 proxy.emit('error', new got.HTTPError(statusCode, opts), null, res);
186 proxy.emit('response', res);
189 ee.on('redirect', proxy.emit.bind(proxy, 'redirect'));
190 ee.on('error', proxy.emit.bind(proxy, 'error'));
195 function normalizeArguments(url, opts) {
196 if (typeof url !== 'string' && typeof url !== 'object') {
197 throw new Error(`Parameter \`url\` must be a string or object, not ${typeof url}`);
200 if (typeof url === 'string') {
201 url = url.replace(/^unix:/, 'http://$&');
202 url = urlParseLax(url);
205 throw new Error('Basic authentication must be done with auth option');
209 opts = Object.assign(
219 opts.headers = Object.assign({
220 'user-agent': `${pkg.name}/${pkg.version} (https://github.com/sindresorhus/got)`,
221 'accept-encoding': 'gzip,deflate'
222 }, lowercaseKeys(opts.headers));
224 const query = opts.query;
227 if (typeof query !== 'string') {
228 opts.query = querystring.stringify(query);
231 opts.path = `${opts.path.split('?')[0]}?${opts.query}`;
235 if (opts.json && opts.headers.accept === undefined) {
236 opts.headers.accept = 'application/json';
239 let body = opts.body;
242 if (typeof body !== 'string' && !(body !== null && typeof body === 'object')) {
243 throw new Error('options.body must be a ReadableStream, string, Buffer or plain Object');
246 opts.method = opts.method || 'POST';
248 if (isStream(body) && typeof body.getBoundary === 'function') {
249 // Special case for https://github.com/form-data/form-data
250 opts.headers['content-type'] = opts.headers['content-type'] || `multipart/form-data; boundary=${body.getBoundary()}`;
251 } else if (body !== null && typeof body === 'object' && !Buffer.isBuffer(body) && !isStream(body)) {
252 opts.headers['content-type'] = opts.headers['content-type'] || 'application/x-www-form-urlencoded';
253 body = opts.body = querystring.stringify(body);
256 if (opts.headers['content-length'] === undefined && opts.headers['transfer-encoding'] === undefined && !isStream(body)) {
257 const length = typeof body === 'string' ? Buffer.byteLength(body) : body.length;
258 opts.headers['content-length'] = length;
262 opts.method = (opts.method || 'GET').toUpperCase();
264 if (opts.hostname === 'unix') {
265 const matches = /(.+):(.+)/.exec(opts.path);
268 opts.socketPath = matches[1];
269 opts.path = matches[2];
274 if (typeof opts.retries !== 'function') {
275 const retries = opts.retries;
277 opts.retries = (iter, err) => {
278 if (iter > retries || !isRetryAllowed(err)) {
282 const noise = Math.random() * 100;
284 return ((1 << iter) * 1000) + noise;
288 if (opts.followRedirect === undefined) {
289 opts.followRedirect = true;
293 opts.gotTimeout = opts.timeout;
300 function got(url, opts) {
302 return asPromise(normalizeArguments(url, opts));
304 return Promise.reject(err);
317 helpers.forEach(el => {
318 got[el] = (url, opts) => got(url, Object.assign({}, opts, {method: el}));
321 got.stream = (url, opts) => asStream(normalizeArguments(url, opts));
323 for (const el of helpers) {
324 got.stream[el] = (url, opts) => got.stream(url, Object.assign({}, opts, {method: el}));
327 function stdError(error, opts) {
328 if (error.code !== undefined) {
329 this.code = error.code;
332 Object.assign(this, {
333 message: error.message,
335 hostname: opts.hostname,
341 got.RequestError = createErrorClass('RequestError', stdError);
342 got.ReadError = createErrorClass('ReadError', stdError);
343 got.ParseError = createErrorClass('ParseError', function (e, statusCode, opts, data) {
344 stdError.call(this, e, opts);
345 this.statusCode = statusCode;
346 this.statusMessage = http.STATUS_CODES[this.statusCode];
347 this.message = `${e.message} in "${urlLib.format(opts)}": \n${data.slice(0, 77)}...`;
350 got.HTTPError = createErrorClass('HTTPError', function (statusCode, opts) {
351 stdError.call(this, {}, opts);
352 this.statusCode = statusCode;
353 this.statusMessage = http.STATUS_CODES[this.statusCode];
354 this.message = `Response code ${this.statusCode} (${this.statusMessage})`;
357 got.MaxRedirectsError = createErrorClass('MaxRedirectsError', function (statusCode, opts) {
358 stdError.call(this, {}, opts);
359 this.statusCode = statusCode;
360 this.statusMessage = http.STATUS_CODES[this.statusCode];
361 this.message = 'Redirected 10 times. Aborting.';
364 module.exports = got;