index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. 'use strict';
  2. const EventEmitter = require('events');
  3. const http = require('http');
  4. const https = require('https');
  5. const PassThrough = require('stream').PassThrough;
  6. const Transform = require('stream').Transform;
  7. const urlLib = require('url');
  8. const fs = require('fs');
  9. const querystring = require('querystring');
  10. const CacheableRequest = require('cacheable-request');
  11. const duplexer3 = require('duplexer3');
  12. const intoStream = require('into-stream');
  13. const is = require('@sindresorhus/is');
  14. const getStream = require('get-stream');
  15. const timedOut = require('timed-out');
  16. const urlParseLax = require('url-parse-lax');
  17. const urlToOptions = require('url-to-options');
  18. const lowercaseKeys = require('lowercase-keys');
  19. const decompressResponse = require('decompress-response');
  20. const mimicResponse = require('mimic-response');
  21. const isRetryAllowed = require('is-retry-allowed');
  22. const isURL = require('isurl');
  23. const PCancelable = require('p-cancelable');
  24. const pTimeout = require('p-timeout');
  25. const pify = require('pify');
  26. const Buffer = require('safe-buffer').Buffer;
  27. const pkg = require('./package.json');
  28. const errors = require('./errors');
  29. const getMethodRedirectCodes = new Set([300, 301, 302, 303, 304, 305, 307, 308]);
  30. const allMethodRedirectCodes = new Set([300, 303, 307, 308]);
  31. const isFormData = body => is.nodeStream(body) && is.function(body.getBoundary);
  32. const getBodySize = opts => {
  33. const body = opts.body;
  34. if (opts.headers['content-length']) {
  35. return Number(opts.headers['content-length']);
  36. }
  37. if (!body && !opts.stream) {
  38. return 0;
  39. }
  40. if (is.string(body)) {
  41. return Buffer.byteLength(body);
  42. }
  43. if (isFormData(body)) {
  44. return pify(body.getLength.bind(body))();
  45. }
  46. if (body instanceof fs.ReadStream) {
  47. return pify(fs.stat)(body.path).then(stat => stat.size);
  48. }
  49. if (is.nodeStream(body) && is.buffer(body._buffer)) {
  50. return body._buffer.length;
  51. }
  52. return null;
  53. };
  54. function requestAsEventEmitter(opts) {
  55. opts = opts || {};
  56. const ee = new EventEmitter();
  57. const requestUrl = opts.href || urlLib.resolve(urlLib.format(opts), opts.path);
  58. const redirects = [];
  59. const agents = is.object(opts.agent) ? opts.agent : null;
  60. let retryCount = 0;
  61. let redirectUrl;
  62. let uploadBodySize;
  63. let uploaded = 0;
  64. const get = opts => {
  65. if (opts.protocol !== 'http:' && opts.protocol !== 'https:') {
  66. ee.emit('error', new got.UnsupportedProtocolError(opts));
  67. return;
  68. }
  69. let fn = opts.protocol === 'https:' ? https : http;
  70. if (agents) {
  71. const protocolName = opts.protocol === 'https:' ? 'https' : 'http';
  72. opts.agent = agents[protocolName] || opts.agent;
  73. }
  74. if (opts.useElectronNet && process.versions.electron) {
  75. const electron = require('electron');
  76. fn = electron.net || electron.remote.net;
  77. }
  78. let progressInterval;
  79. const cacheableRequest = new CacheableRequest(fn.request, opts.cache);
  80. const cacheReq = cacheableRequest(opts, res => {
  81. clearInterval(progressInterval);
  82. ee.emit('uploadProgress', {
  83. percent: 1,
  84. transferred: uploaded,
  85. total: uploadBodySize
  86. });
  87. const statusCode = res.statusCode;
  88. res.url = redirectUrl || requestUrl;
  89. res.requestUrl = requestUrl;
  90. const followRedirect = opts.followRedirect && 'location' in res.headers;
  91. const redirectGet = followRedirect && getMethodRedirectCodes.has(statusCode);
  92. const redirectAll = followRedirect && allMethodRedirectCodes.has(statusCode);
  93. if (redirectAll || (redirectGet && (opts.method === 'GET' || opts.method === 'HEAD'))) {
  94. res.resume();
  95. if (statusCode === 303) {
  96. // Server responded with "see other", indicating that the resource exists at another location,
  97. // and the client should request it from that location via GET or HEAD.
  98. opts.method = 'GET';
  99. }
  100. if (redirects.length >= 10) {
  101. ee.emit('error', new got.MaxRedirectsError(statusCode, redirects, opts), null, res);
  102. return;
  103. }
  104. const bufferString = Buffer.from(res.headers.location, 'binary').toString();
  105. redirectUrl = urlLib.resolve(urlLib.format(opts), bufferString);
  106. redirects.push(redirectUrl);
  107. const redirectOpts = Object.assign({}, opts, urlLib.parse(redirectUrl));
  108. ee.emit('redirect', res, redirectOpts);
  109. get(redirectOpts);
  110. return;
  111. }
  112. setImmediate(() => {
  113. try {
  114. getResponse(res, opts, ee, redirects);
  115. } catch (e) {
  116. ee.emit('error', e);
  117. }
  118. });
  119. });
  120. cacheReq.on('error', err => {
  121. if (err instanceof CacheableRequest.RequestError) {
  122. ee.emit('error', new got.RequestError(err, opts));
  123. } else {
  124. ee.emit('error', new got.CacheError(err, opts));
  125. }
  126. });
  127. cacheReq.once('request', req => {
  128. let aborted = false;
  129. req.once('abort', _ => {
  130. aborted = true;
  131. });
  132. req.once('error', err => {
  133. clearInterval(progressInterval);
  134. if (aborted) {
  135. return;
  136. }
  137. const backoff = opts.retries(++retryCount, err);
  138. if (backoff) {
  139. setTimeout(get, backoff, opts);
  140. return;
  141. }
  142. ee.emit('error', new got.RequestError(err, opts));
  143. });
  144. ee.once('request', req => {
  145. ee.emit('uploadProgress', {
  146. percent: 0,
  147. transferred: 0,
  148. total: uploadBodySize
  149. });
  150. const socket = req.connection;
  151. if (socket) {
  152. // `._connecting` was the old property which was made public in node v6.1.0
  153. const isConnecting = socket.connecting === undefined ? socket._connecting : socket.connecting;
  154. const onSocketConnect = () => {
  155. const uploadEventFrequency = 150;
  156. progressInterval = setInterval(() => {
  157. if (socket.destroyed) {
  158. clearInterval(progressInterval);
  159. return;
  160. }
  161. const lastUploaded = uploaded;
  162. const headersSize = req._header ? Buffer.byteLength(req._header) : 0;
  163. uploaded = socket.bytesWritten - headersSize;
  164. // Prevent the known issue of `bytesWritten` being larger than body size
  165. if (uploadBodySize && uploaded > uploadBodySize) {
  166. uploaded = uploadBodySize;
  167. }
  168. // Don't emit events with unchanged progress and
  169. // prevent last event from being emitted, because
  170. // it's emitted when `response` is emitted
  171. if (uploaded === lastUploaded || uploaded === uploadBodySize) {
  172. return;
  173. }
  174. ee.emit('uploadProgress', {
  175. percent: uploadBodySize ? uploaded / uploadBodySize : 0,
  176. transferred: uploaded,
  177. total: uploadBodySize
  178. });
  179. }, uploadEventFrequency);
  180. };
  181. // Only subscribe to 'connect' event if we're actually connecting a new
  182. // socket, otherwise if we're already connected (because this is a
  183. // keep-alive connection) do not bother. This is important since we won't
  184. // get a 'connect' event for an already connected socket.
  185. if (isConnecting) {
  186. socket.once('connect', onSocketConnect);
  187. } else {
  188. onSocketConnect();
  189. }
  190. }
  191. });
  192. if (opts.gotTimeout) {
  193. clearInterval(progressInterval);
  194. timedOut(req, opts.gotTimeout);
  195. }
  196. setImmediate(() => {
  197. ee.emit('request', req);
  198. });
  199. });
  200. };
  201. setImmediate(() => {
  202. Promise.resolve(getBodySize(opts))
  203. .then(size => {
  204. uploadBodySize = size;
  205. if (
  206. is.undefined(opts.headers['content-length']) &&
  207. is.undefined(opts.headers['transfer-encoding']) &&
  208. isFormData(opts.body)
  209. ) {
  210. opts.headers['content-length'] = size;
  211. }
  212. get(opts);
  213. })
  214. .catch(err => {
  215. ee.emit('error', err);
  216. });
  217. });
  218. return ee;
  219. }
  220. function getResponse(res, opts, ee, redirects) {
  221. const downloadBodySize = Number(res.headers['content-length']) || null;
  222. let downloaded = 0;
  223. const progressStream = new Transform({
  224. transform(chunk, encoding, callback) {
  225. downloaded += chunk.length;
  226. const percent = downloadBodySize ? downloaded / downloadBodySize : 0;
  227. // Let flush() be responsible for emitting the last event
  228. if (percent < 1) {
  229. ee.emit('downloadProgress', {
  230. percent,
  231. transferred: downloaded,
  232. total: downloadBodySize
  233. });
  234. }
  235. callback(null, chunk);
  236. },
  237. flush(callback) {
  238. ee.emit('downloadProgress', {
  239. percent: 1,
  240. transferred: downloaded,
  241. total: downloadBodySize
  242. });
  243. callback();
  244. }
  245. });
  246. mimicResponse(res, progressStream);
  247. progressStream.redirectUrls = redirects;
  248. const response = opts.decompress === true &&
  249. is.function(decompressResponse) &&
  250. opts.method !== 'HEAD' ? decompressResponse(progressStream) : progressStream;
  251. if (!opts.decompress && ['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) {
  252. opts.encoding = null;
  253. }
  254. ee.emit('response', response);
  255. ee.emit('downloadProgress', {
  256. percent: 0,
  257. transferred: 0,
  258. total: downloadBodySize
  259. });
  260. res.pipe(progressStream);
  261. }
  262. function asPromise(opts) {
  263. const timeoutFn = requestPromise => opts.gotTimeout && opts.gotTimeout.request ?
  264. pTimeout(requestPromise, opts.gotTimeout.request, new got.RequestError({message: 'Request timed out', code: 'ETIMEDOUT'}, opts)) :
  265. requestPromise;
  266. const proxy = new EventEmitter();
  267. const cancelable = new PCancelable((resolve, reject, onCancel) => {
  268. const ee = requestAsEventEmitter(opts);
  269. let cancelOnRequest = false;
  270. onCancel(() => {
  271. cancelOnRequest = true;
  272. });
  273. ee.on('request', req => {
  274. if (cancelOnRequest) {
  275. req.abort();
  276. }
  277. onCancel(() => {
  278. req.abort();
  279. });
  280. if (is.nodeStream(opts.body)) {
  281. opts.body.pipe(req);
  282. opts.body = undefined;
  283. return;
  284. }
  285. req.end(opts.body);
  286. });
  287. ee.on('response', res => {
  288. const stream = is.null(opts.encoding) ? getStream.buffer(res) : getStream(res, opts);
  289. stream
  290. .catch(err => reject(new got.ReadError(err, opts)))
  291. .then(data => {
  292. const statusCode = res.statusCode;
  293. const limitStatusCode = opts.followRedirect ? 299 : 399;
  294. res.body = data;
  295. if (opts.json && res.body) {
  296. try {
  297. res.body = JSON.parse(res.body);
  298. } catch (err) {
  299. if (statusCode >= 200 && statusCode < 300) {
  300. throw new got.ParseError(err, statusCode, opts, data);
  301. }
  302. }
  303. }
  304. if (opts.throwHttpErrors && statusCode !== 304 && (statusCode < 200 || statusCode > limitStatusCode)) {
  305. throw new got.HTTPError(statusCode, res.statusMessage, res.headers, opts);
  306. }
  307. resolve(res);
  308. })
  309. .catch(err => {
  310. Object.defineProperty(err, 'response', {value: res});
  311. reject(err);
  312. });
  313. });
  314. ee.once('error', reject);
  315. ee.on('redirect', proxy.emit.bind(proxy, 'redirect'));
  316. ee.on('uploadProgress', proxy.emit.bind(proxy, 'uploadProgress'));
  317. ee.on('downloadProgress', proxy.emit.bind(proxy, 'downloadProgress'));
  318. });
  319. // Preserve backwards-compatibility
  320. // TODO: Remove this in the next major version
  321. Object.defineProperty(cancelable, 'canceled', {
  322. get() {
  323. return cancelable.isCanceled;
  324. }
  325. });
  326. const promise = timeoutFn(cancelable);
  327. promise.cancel = cancelable.cancel.bind(cancelable);
  328. promise.on = (name, fn) => {
  329. proxy.on(name, fn);
  330. return promise;
  331. };
  332. return promise;
  333. }
  334. function asStream(opts) {
  335. opts.stream = true;
  336. const input = new PassThrough();
  337. const output = new PassThrough();
  338. const proxy = duplexer3(input, output);
  339. let timeout;
  340. if (opts.gotTimeout && opts.gotTimeout.request) {
  341. timeout = setTimeout(() => {
  342. proxy.emit('error', new got.RequestError({message: 'Request timed out', code: 'ETIMEDOUT'}, opts));
  343. }, opts.gotTimeout.request);
  344. }
  345. if (opts.json) {
  346. throw new Error('Got can not be used as a stream when the `json` option is used');
  347. }
  348. if (opts.body) {
  349. proxy.write = () => {
  350. throw new Error('Got\'s stream is not writable when the `body` option is used');
  351. };
  352. }
  353. const ee = requestAsEventEmitter(opts);
  354. ee.on('request', req => {
  355. proxy.emit('request', req);
  356. if (is.nodeStream(opts.body)) {
  357. opts.body.pipe(req);
  358. return;
  359. }
  360. if (opts.body) {
  361. req.end(opts.body);
  362. return;
  363. }
  364. if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') {
  365. input.pipe(req);
  366. return;
  367. }
  368. req.end();
  369. });
  370. ee.on('response', res => {
  371. clearTimeout(timeout);
  372. const statusCode = res.statusCode;
  373. res.on('error', err => {
  374. proxy.emit('error', new got.ReadError(err, opts));
  375. });
  376. res.pipe(output);
  377. if (opts.throwHttpErrors && statusCode !== 304 && (statusCode < 200 || statusCode > 299)) {
  378. proxy.emit('error', new got.HTTPError(statusCode, res.statusMessage, res.headers, opts), null, res);
  379. return;
  380. }
  381. proxy.emit('response', res);
  382. });
  383. ee.on('error', proxy.emit.bind(proxy, 'error'));
  384. ee.on('redirect', proxy.emit.bind(proxy, 'redirect'));
  385. ee.on('uploadProgress', proxy.emit.bind(proxy, 'uploadProgress'));
  386. ee.on('downloadProgress', proxy.emit.bind(proxy, 'downloadProgress'));
  387. return proxy;
  388. }
  389. function normalizeArguments(url, opts) {
  390. if (!is.string(url) && !is.object(url)) {
  391. throw new TypeError(`Parameter \`url\` must be a string or object, not ${is(url)}`);
  392. } else if (is.string(url)) {
  393. url = url.replace(/^unix:/, 'http://$&');
  394. try {
  395. decodeURI(url);
  396. } catch (err) {
  397. throw new Error('Parameter `url` must contain valid UTF-8 character sequences');
  398. }
  399. url = urlParseLax(url);
  400. if (url.auth) {
  401. throw new Error('Basic authentication must be done with the `auth` option');
  402. }
  403. } else if (isURL.lenient(url)) {
  404. url = urlToOptions(url);
  405. }
  406. opts = Object.assign(
  407. {
  408. path: '',
  409. retries: 2,
  410. cache: false,
  411. decompress: true,
  412. useElectronNet: false,
  413. throwHttpErrors: true
  414. },
  415. url,
  416. {
  417. protocol: url.protocol || 'http:' // Override both null/undefined with default protocol
  418. },
  419. opts
  420. );
  421. const headers = lowercaseKeys(opts.headers);
  422. for (const key of Object.keys(headers)) {
  423. if (is.nullOrUndefined(headers[key])) {
  424. delete headers[key];
  425. }
  426. }
  427. opts.headers = Object.assign({
  428. 'user-agent': `${pkg.name}/${pkg.version} (https://github.com/sindresorhus/got)`
  429. }, headers);
  430. if (opts.decompress && is.undefined(opts.headers['accept-encoding'])) {
  431. opts.headers['accept-encoding'] = 'gzip, deflate';
  432. }
  433. const query = opts.query;
  434. if (query) {
  435. if (!is.string(query)) {
  436. opts.query = querystring.stringify(query);
  437. }
  438. opts.path = `${opts.path.split('?')[0]}?${opts.query}`;
  439. delete opts.query;
  440. }
  441. if (opts.json && is.undefined(opts.headers.accept)) {
  442. opts.headers.accept = 'application/json';
  443. }
  444. const body = opts.body;
  445. if (is.nullOrUndefined(body)) {
  446. opts.method = (opts.method || 'GET').toUpperCase();
  447. } else {
  448. const headers = opts.headers;
  449. if (!is.nodeStream(body) && !is.string(body) && !is.buffer(body) && !(opts.form || opts.json)) {
  450. throw new TypeError('The `body` option must be a stream.Readable, string, Buffer or plain Object');
  451. }
  452. const canBodyBeStringified = is.plainObject(body) || is.array(body);
  453. if ((opts.form || opts.json) && !canBodyBeStringified) {
  454. throw new TypeError('The `body` option must be a plain Object or Array when the `form` or `json` option is used');
  455. }
  456. if (isFormData(body)) {
  457. // Special case for https://github.com/form-data/form-data
  458. headers['content-type'] = headers['content-type'] || `multipart/form-data; boundary=${body.getBoundary()}`;
  459. } else if (opts.form && canBodyBeStringified) {
  460. headers['content-type'] = headers['content-type'] || 'application/x-www-form-urlencoded';
  461. opts.body = querystring.stringify(body);
  462. } else if (opts.json && canBodyBeStringified) {
  463. headers['content-type'] = headers['content-type'] || 'application/json';
  464. opts.body = JSON.stringify(body);
  465. }
  466. if (is.undefined(headers['content-length']) && is.undefined(headers['transfer-encoding']) && !is.nodeStream(body)) {
  467. const length = is.string(opts.body) ? Buffer.byteLength(opts.body) : opts.body.length;
  468. headers['content-length'] = length;
  469. }
  470. // Convert buffer to stream to receive upload progress events
  471. // see https://github.com/sindresorhus/got/pull/322
  472. if (is.buffer(body)) {
  473. opts.body = intoStream(body);
  474. opts.body._buffer = body;
  475. }
  476. opts.method = (opts.method || 'POST').toUpperCase();
  477. }
  478. if (opts.hostname === 'unix') {
  479. const matches = /(.+?):(.+)/.exec(opts.path);
  480. if (matches) {
  481. opts.socketPath = matches[1];
  482. opts.path = matches[2];
  483. opts.host = null;
  484. }
  485. }
  486. if (!is.function(opts.retries)) {
  487. const retries = opts.retries;
  488. opts.retries = (iter, err) => {
  489. if (iter > retries || !isRetryAllowed(err)) {
  490. return 0;
  491. }
  492. const noise = Math.random() * 100;
  493. return ((1 << iter) * 1000) + noise;
  494. };
  495. }
  496. if (is.undefined(opts.followRedirect)) {
  497. opts.followRedirect = true;
  498. }
  499. if (opts.timeout) {
  500. if (is.number(opts.timeout)) {
  501. opts.gotTimeout = {request: opts.timeout};
  502. } else {
  503. opts.gotTimeout = opts.timeout;
  504. }
  505. delete opts.timeout;
  506. }
  507. return opts;
  508. }
  509. function got(url, opts) {
  510. try {
  511. const normalizedArgs = normalizeArguments(url, opts);
  512. if (normalizedArgs.stream) {
  513. return asStream(normalizedArgs);
  514. }
  515. return asPromise(normalizedArgs);
  516. } catch (err) {
  517. return Promise.reject(err);
  518. }
  519. }
  520. got.stream = (url, opts) => asStream(normalizeArguments(url, opts));
  521. const methods = [
  522. 'get',
  523. 'post',
  524. 'put',
  525. 'patch',
  526. 'head',
  527. 'delete'
  528. ];
  529. for (const method of methods) {
  530. got[method] = (url, opts) => got(url, Object.assign({}, opts, {method}));
  531. got.stream[method] = (url, opts) => got.stream(url, Object.assign({}, opts, {method}));
  532. }
  533. Object.assign(got, errors);
  534. module.exports = got;