request-as-event-emitter.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. 'use strict';
  2. const {URL} = require('url'); // TODO: Use the `URL` global when targeting Node.js 10
  3. const util = require('util');
  4. const EventEmitter = require('events');
  5. const http = require('http');
  6. const https = require('https');
  7. const urlLib = require('url');
  8. const CacheableRequest = require('cacheable-request');
  9. const toReadableStream = require('to-readable-stream');
  10. const is = require('@sindresorhus/is');
  11. const timer = require('@szmarczak/http-timer');
  12. const timedOut = require('./utils/timed-out');
  13. const getBodySize = require('./utils/get-body-size');
  14. const getResponse = require('./get-response');
  15. const progress = require('./progress');
  16. const {CacheError, UnsupportedProtocolError, MaxRedirectsError, RequestError, TimeoutError} = require('./errors');
  17. const urlToOptions = require('./utils/url-to-options');
  18. const getMethodRedirectCodes = new Set([300, 301, 302, 303, 304, 305, 307, 308]);
  19. const allMethodRedirectCodes = new Set([300, 303, 307, 308]);
  20. module.exports = (options, input) => {
  21. const emitter = new EventEmitter();
  22. const redirects = [];
  23. let currentRequest;
  24. let requestUrl;
  25. let redirectString;
  26. let uploadBodySize;
  27. let retryCount = 0;
  28. let shouldAbort = false;
  29. const setCookie = options.cookieJar ? util.promisify(options.cookieJar.setCookie.bind(options.cookieJar)) : null;
  30. const getCookieString = options.cookieJar ? util.promisify(options.cookieJar.getCookieString.bind(options.cookieJar)) : null;
  31. const agents = is.object(options.agent) ? options.agent : null;
  32. const emitError = async error => {
  33. try {
  34. for (const hook of options.hooks.beforeError) {
  35. // eslint-disable-next-line no-await-in-loop
  36. error = await hook(error);
  37. }
  38. emitter.emit('error', error);
  39. } catch (error2) {
  40. emitter.emit('error', error2);
  41. }
  42. };
  43. const get = async options => {
  44. const currentUrl = redirectString || requestUrl;
  45. if (options.protocol !== 'http:' && options.protocol !== 'https:') {
  46. throw new UnsupportedProtocolError(options);
  47. }
  48. decodeURI(currentUrl);
  49. let fn;
  50. if (is.function(options.request)) {
  51. fn = {request: options.request};
  52. } else {
  53. fn = options.protocol === 'https:' ? https : http;
  54. }
  55. if (agents) {
  56. const protocolName = options.protocol === 'https:' ? 'https' : 'http';
  57. options.agent = agents[protocolName] || options.agent;
  58. }
  59. /* istanbul ignore next: electron.net is broken */
  60. if (options.useElectronNet && process.versions.electron) {
  61. const r = ({x: require})['yx'.slice(1)]; // Trick webpack
  62. const electron = r('electron');
  63. fn = electron.net || electron.remote.net;
  64. }
  65. if (options.cookieJar) {
  66. const cookieString = await getCookieString(currentUrl, {});
  67. if (is.nonEmptyString(cookieString)) {
  68. options.headers.cookie = cookieString;
  69. }
  70. }
  71. let timings;
  72. const handleResponse = async response => {
  73. try {
  74. /* istanbul ignore next: fixes https://github.com/electron/electron/blob/cbb460d47628a7a146adf4419ed48550a98b2923/lib/browser/api/net.js#L59-L65 */
  75. if (options.useElectronNet) {
  76. response = new Proxy(response, {
  77. get: (target, name) => {
  78. if (name === 'trailers' || name === 'rawTrailers') {
  79. return [];
  80. }
  81. const value = target[name];
  82. return is.function(value) ? value.bind(target) : value;
  83. }
  84. });
  85. }
  86. const {statusCode} = response;
  87. response.url = currentUrl;
  88. response.requestUrl = requestUrl;
  89. response.retryCount = retryCount;
  90. response.timings = timings;
  91. response.redirectUrls = redirects;
  92. response.request = {
  93. gotOptions: options
  94. };
  95. const rawCookies = response.headers['set-cookie'];
  96. if (options.cookieJar && rawCookies) {
  97. await Promise.all(rawCookies.map(rawCookie => setCookie(rawCookie, response.url)));
  98. }
  99. if (options.followRedirect && 'location' in response.headers) {
  100. if (allMethodRedirectCodes.has(statusCode) || (getMethodRedirectCodes.has(statusCode) && (options.method === 'GET' || options.method === 'HEAD'))) {
  101. response.resume(); // We're being redirected, we don't care about the response.
  102. if (statusCode === 303) {
  103. // Server responded with "see other", indicating that the resource exists at another location,
  104. // and the client should request it from that location via GET or HEAD.
  105. options.method = 'GET';
  106. }
  107. if (redirects.length >= 10) {
  108. throw new MaxRedirectsError(statusCode, redirects, options);
  109. }
  110. // Handles invalid URLs. See https://github.com/sindresorhus/got/issues/604
  111. const redirectBuffer = Buffer.from(response.headers.location, 'binary').toString();
  112. const redirectURL = new URL(redirectBuffer, currentUrl);
  113. redirectString = redirectURL.toString();
  114. redirects.push(redirectString);
  115. const redirectOptions = {
  116. ...options,
  117. ...urlToOptions(redirectURL)
  118. };
  119. for (const hook of options.hooks.beforeRedirect) {
  120. // eslint-disable-next-line no-await-in-loop
  121. await hook(redirectOptions);
  122. }
  123. emitter.emit('redirect', response, redirectOptions);
  124. await get(redirectOptions);
  125. return;
  126. }
  127. }
  128. getResponse(response, options, emitter);
  129. } catch (error) {
  130. emitError(error);
  131. }
  132. };
  133. const handleRequest = request => {
  134. if (shouldAbort) {
  135. request.once('error', () => {});
  136. request.abort();
  137. return;
  138. }
  139. currentRequest = request;
  140. request.once('error', error => {
  141. if (request.aborted) {
  142. return;
  143. }
  144. if (error instanceof timedOut.TimeoutError) {
  145. error = new TimeoutError(error, options);
  146. } else {
  147. error = new RequestError(error, options);
  148. }
  149. if (emitter.retry(error) === false) {
  150. emitError(error);
  151. }
  152. });
  153. timings = timer(request);
  154. progress.upload(request, emitter, uploadBodySize);
  155. if (options.gotTimeout) {
  156. timedOut(request, options.gotTimeout, options);
  157. }
  158. emitter.emit('request', request);
  159. const uploadComplete = () => {
  160. request.emit('upload-complete');
  161. };
  162. try {
  163. if (is.nodeStream(options.body)) {
  164. options.body.once('end', uploadComplete);
  165. options.body.pipe(request);
  166. options.body = undefined;
  167. } else if (options.body) {
  168. request.end(options.body, uploadComplete);
  169. } else if (input && (options.method === 'POST' || options.method === 'PUT' || options.method === 'PATCH')) {
  170. input.once('end', uploadComplete);
  171. input.pipe(request);
  172. } else {
  173. request.end(uploadComplete);
  174. }
  175. } catch (error) {
  176. emitError(new RequestError(error, options));
  177. }
  178. };
  179. if (options.cache) {
  180. const cacheableRequest = new CacheableRequest(fn.request, options.cache);
  181. const cacheRequest = cacheableRequest(options, handleResponse);
  182. cacheRequest.once('error', error => {
  183. if (error instanceof CacheableRequest.RequestError) {
  184. emitError(new RequestError(error, options));
  185. } else {
  186. emitError(new CacheError(error, options));
  187. }
  188. });
  189. cacheRequest.once('request', handleRequest);
  190. } else {
  191. // Catches errors thrown by calling fn.request(...)
  192. try {
  193. handleRequest(fn.request(options, handleResponse));
  194. } catch (error) {
  195. emitError(new RequestError(error, options));
  196. }
  197. }
  198. };
  199. emitter.retry = error => {
  200. let backoff;
  201. try {
  202. backoff = options.retry.retries(++retryCount, error);
  203. } catch (error2) {
  204. emitError(error2);
  205. return;
  206. }
  207. if (backoff) {
  208. const retry = async options => {
  209. try {
  210. for (const hook of options.hooks.beforeRetry) {
  211. // eslint-disable-next-line no-await-in-loop
  212. await hook(options, error, retryCount);
  213. }
  214. await get(options);
  215. } catch (error) {
  216. emitError(error);
  217. }
  218. };
  219. setTimeout(retry, backoff, {...options, forceRefresh: true});
  220. return true;
  221. }
  222. return false;
  223. };
  224. emitter.abort = () => {
  225. if (currentRequest) {
  226. currentRequest.once('error', () => {});
  227. currentRequest.abort();
  228. } else {
  229. shouldAbort = true;
  230. }
  231. };
  232. setImmediate(async () => {
  233. try {
  234. // Convert buffer to stream to receive upload progress events (#322)
  235. const {body} = options;
  236. if (is.buffer(body)) {
  237. options.body = toReadableStream(body);
  238. uploadBodySize = body.length;
  239. } else {
  240. uploadBodySize = await getBodySize(options);
  241. }
  242. if (is.undefined(options.headers['content-length']) && is.undefined(options.headers['transfer-encoding'])) {
  243. if ((uploadBodySize > 0 || options.method === 'PUT') && !is.null(uploadBodySize)) {
  244. options.headers['content-length'] = uploadBodySize;
  245. }
  246. }
  247. for (const hook of options.hooks.beforeRequest) {
  248. // eslint-disable-next-line no-await-in-loop
  249. await hook(options);
  250. }
  251. requestUrl = options.href || (new URL(options.path, urlLib.format(options))).toString();
  252. await get(options);
  253. } catch (error) {
  254. emitError(error);
  255. }
  256. });
  257. return emitter;
  258. };