http.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. import utils from '../utils.js';
  2. import settle from '../core/settle.js';
  3. import buildFullPath from '../core/buildFullPath.js';
  4. import buildURL from '../helpers/buildURL.js';
  5. import proxyFromEnv from 'proxy-from-env';
  6. import http from 'http';
  7. import https from 'https';
  8. import http2 from 'http2';
  9. import util from 'util';
  10. import followRedirects from 'follow-redirects';
  11. import zlib from 'zlib';
  12. import { VERSION } from '../env/data.js';
  13. import transitionalDefaults from '../defaults/transitional.js';
  14. import AxiosError from '../core/AxiosError.js';
  15. import CanceledError from '../cancel/CanceledError.js';
  16. import platform from '../platform/index.js';
  17. import fromDataURI from '../helpers/fromDataURI.js';
  18. import stream from 'stream';
  19. import AxiosHeaders from '../core/AxiosHeaders.js';
  20. import AxiosTransformStream from '../helpers/AxiosTransformStream.js';
  21. import { EventEmitter } from 'events';
  22. import formDataToStream from '../helpers/formDataToStream.js';
  23. import readBlob from '../helpers/readBlob.js';
  24. import ZlibHeaderTransformStream from '../helpers/ZlibHeaderTransformStream.js';
  25. import callbackify from '../helpers/callbackify.js';
  26. import {
  27. progressEventReducer,
  28. progressEventDecorator,
  29. asyncDecorator,
  30. } from '../helpers/progressEventReducer.js';
  31. import estimateDataURLDecodedBytes from '../helpers/estimateDataURLDecodedBytes.js';
  32. const zlibOptions = {
  33. flush: zlib.constants.Z_SYNC_FLUSH,
  34. finishFlush: zlib.constants.Z_SYNC_FLUSH,
  35. };
  36. const brotliOptions = {
  37. flush: zlib.constants.BROTLI_OPERATION_FLUSH,
  38. finishFlush: zlib.constants.BROTLI_OPERATION_FLUSH,
  39. };
  40. const isBrotliSupported = utils.isFunction(zlib.createBrotliDecompress);
  41. const { http: httpFollow, https: httpsFollow } = followRedirects;
  42. const isHttps = /https:?/;
  43. const supportedProtocols = platform.protocols.map((protocol) => {
  44. return protocol + ':';
  45. });
  46. const flushOnFinish = (stream, [throttled, flush]) => {
  47. stream.on('end', flush).on('error', flush);
  48. return throttled;
  49. };
  50. class Http2Sessions {
  51. constructor() {
  52. this.sessions = Object.create(null);
  53. }
  54. getSession(authority, options) {
  55. options = Object.assign(
  56. {
  57. sessionTimeout: 1000,
  58. },
  59. options
  60. );
  61. let authoritySessions = this.sessions[authority];
  62. if (authoritySessions) {
  63. let len = authoritySessions.length;
  64. for (let i = 0; i < len; i++) {
  65. const [sessionHandle, sessionOptions] = authoritySessions[i];
  66. if (
  67. !sessionHandle.destroyed &&
  68. !sessionHandle.closed &&
  69. util.isDeepStrictEqual(sessionOptions, options)
  70. ) {
  71. return sessionHandle;
  72. }
  73. }
  74. }
  75. const session = http2.connect(authority, options);
  76. let removed;
  77. const removeSession = () => {
  78. if (removed) {
  79. return;
  80. }
  81. removed = true;
  82. let entries = authoritySessions,
  83. len = entries.length,
  84. i = len;
  85. while (i--) {
  86. if (entries[i][0] === session) {
  87. if (len === 1) {
  88. delete this.sessions[authority];
  89. } else {
  90. entries.splice(i, 1);
  91. }
  92. return;
  93. }
  94. }
  95. };
  96. const originalRequestFn = session.request;
  97. const { sessionTimeout } = options;
  98. if (sessionTimeout != null) {
  99. let timer;
  100. let streamsCount = 0;
  101. session.request = function () {
  102. const stream = originalRequestFn.apply(this, arguments);
  103. streamsCount++;
  104. if (timer) {
  105. clearTimeout(timer);
  106. timer = null;
  107. }
  108. stream.once('close', () => {
  109. if (!--streamsCount) {
  110. timer = setTimeout(() => {
  111. timer = null;
  112. removeSession();
  113. }, sessionTimeout);
  114. }
  115. });
  116. return stream;
  117. };
  118. }
  119. session.once('close', removeSession);
  120. let entry = [session, options];
  121. authoritySessions
  122. ? authoritySessions.push(entry)
  123. : (authoritySessions = this.sessions[authority] = [entry]);
  124. return session;
  125. }
  126. }
  127. const http2Sessions = new Http2Sessions();
  128. /**
  129. * If the proxy or config beforeRedirects functions are defined, call them with the options
  130. * object.
  131. *
  132. * @param {Object<string, any>} options - The options object that was passed to the request.
  133. *
  134. * @returns {Object<string, any>}
  135. */
  136. function dispatchBeforeRedirect(options, responseDetails) {
  137. if (options.beforeRedirects.proxy) {
  138. options.beforeRedirects.proxy(options);
  139. }
  140. if (options.beforeRedirects.config) {
  141. options.beforeRedirects.config(options, responseDetails);
  142. }
  143. }
  144. /**
  145. * If the proxy or config afterRedirects functions are defined, call them with the options
  146. *
  147. * @param {http.ClientRequestArgs} options
  148. * @param {AxiosProxyConfig} configProxy configuration from Axios options object
  149. * @param {string} location
  150. *
  151. * @returns {http.ClientRequestArgs}
  152. */
  153. function setProxy(options, configProxy, location) {
  154. let proxy = configProxy;
  155. if (!proxy && proxy !== false) {
  156. const proxyUrl = proxyFromEnv.getProxyForUrl(location);
  157. if (proxyUrl) {
  158. proxy = new URL(proxyUrl);
  159. }
  160. }
  161. if (proxy) {
  162. // Basic proxy authorization
  163. if (proxy.username) {
  164. proxy.auth = (proxy.username || '') + ':' + (proxy.password || '');
  165. }
  166. if (proxy.auth) {
  167. // Support proxy auth object form
  168. const validProxyAuth = Boolean(proxy.auth.username || proxy.auth.password);
  169. if (validProxyAuth) {
  170. proxy.auth = (proxy.auth.username || '') + ':' + (proxy.auth.password || '');
  171. } else if (typeof proxy.auth === 'object') {
  172. throw new AxiosError('Invalid proxy authorization', AxiosError.ERR_BAD_OPTION, { proxy });
  173. }
  174. const base64 = Buffer.from(proxy.auth, 'utf8').toString('base64');
  175. options.headers['Proxy-Authorization'] = 'Basic ' + base64;
  176. }
  177. options.headers.host = options.hostname + (options.port ? ':' + options.port : '');
  178. const proxyHost = proxy.hostname || proxy.host;
  179. options.hostname = proxyHost;
  180. // Replace 'host' since options is not a URL object
  181. options.host = proxyHost;
  182. options.port = proxy.port;
  183. options.path = location;
  184. if (proxy.protocol) {
  185. options.protocol = proxy.protocol.includes(':') ? proxy.protocol : `${proxy.protocol}:`;
  186. }
  187. }
  188. options.beforeRedirects.proxy = function beforeRedirect(redirectOptions) {
  189. // Configure proxy for redirected request, passing the original config proxy to apply
  190. // the exact same logic as if the redirected request was performed by axios directly.
  191. setProxy(redirectOptions, configProxy, redirectOptions.href);
  192. };
  193. }
  194. const isHttpAdapterSupported =
  195. typeof process !== 'undefined' && utils.kindOf(process) === 'process';
  196. // temporary hotfix
  197. const wrapAsync = (asyncExecutor) => {
  198. return new Promise((resolve, reject) => {
  199. let onDone;
  200. let isDone;
  201. const done = (value, isRejected) => {
  202. if (isDone) return;
  203. isDone = true;
  204. onDone && onDone(value, isRejected);
  205. };
  206. const _resolve = (value) => {
  207. done(value);
  208. resolve(value);
  209. };
  210. const _reject = (reason) => {
  211. done(reason, true);
  212. reject(reason);
  213. };
  214. asyncExecutor(_resolve, _reject, (onDoneHandler) => (onDone = onDoneHandler)).catch(_reject);
  215. });
  216. };
  217. const resolveFamily = ({ address, family }) => {
  218. if (!utils.isString(address)) {
  219. throw TypeError('address must be a string');
  220. }
  221. return {
  222. address,
  223. family: family || (address.indexOf('.') < 0 ? 6 : 4),
  224. };
  225. };
  226. const buildAddressEntry = (address, family) =>
  227. resolveFamily(utils.isObject(address) ? address : { address, family });
  228. const http2Transport = {
  229. request(options, cb) {
  230. const authority =
  231. options.protocol +
  232. '//' +
  233. options.hostname +
  234. ':' +
  235. (options.port || (options.protocol === 'https:' ? 443 : 80));
  236. const { http2Options, headers } = options;
  237. const session = http2Sessions.getSession(authority, http2Options);
  238. const { HTTP2_HEADER_SCHEME, HTTP2_HEADER_METHOD, HTTP2_HEADER_PATH, HTTP2_HEADER_STATUS } =
  239. http2.constants;
  240. const http2Headers = {
  241. [HTTP2_HEADER_SCHEME]: options.protocol.replace(':', ''),
  242. [HTTP2_HEADER_METHOD]: options.method,
  243. [HTTP2_HEADER_PATH]: options.path,
  244. };
  245. utils.forEach(headers, (header, name) => {
  246. name.charAt(0) !== ':' && (http2Headers[name] = header);
  247. });
  248. const req = session.request(http2Headers);
  249. req.once('response', (responseHeaders) => {
  250. const response = req; //duplex
  251. responseHeaders = Object.assign({}, responseHeaders);
  252. const status = responseHeaders[HTTP2_HEADER_STATUS];
  253. delete responseHeaders[HTTP2_HEADER_STATUS];
  254. response.headers = responseHeaders;
  255. response.statusCode = +status;
  256. cb(response);
  257. });
  258. return req;
  259. },
  260. };
  261. /*eslint consistent-return:0*/
  262. export default isHttpAdapterSupported &&
  263. function httpAdapter(config) {
  264. return wrapAsync(async function dispatchHttpRequest(resolve, reject, onDone) {
  265. let { data, lookup, family, httpVersion = 1, http2Options } = config;
  266. const { responseType, responseEncoding } = config;
  267. const method = config.method.toUpperCase();
  268. let isDone;
  269. let rejected = false;
  270. let req;
  271. httpVersion = +httpVersion;
  272. if (Number.isNaN(httpVersion)) {
  273. throw TypeError(`Invalid protocol version: '${config.httpVersion}' is not a number`);
  274. }
  275. if (httpVersion !== 1 && httpVersion !== 2) {
  276. throw TypeError(`Unsupported protocol version '${httpVersion}'`);
  277. }
  278. const isHttp2 = httpVersion === 2;
  279. if (lookup) {
  280. const _lookup = callbackify(lookup, (value) => (utils.isArray(value) ? value : [value]));
  281. // hotfix to support opt.all option which is required for node 20.x
  282. lookup = (hostname, opt, cb) => {
  283. _lookup(hostname, opt, (err, arg0, arg1) => {
  284. if (err) {
  285. return cb(err);
  286. }
  287. const addresses = utils.isArray(arg0)
  288. ? arg0.map((addr) => buildAddressEntry(addr))
  289. : [buildAddressEntry(arg0, arg1)];
  290. opt.all ? cb(err, addresses) : cb(err, addresses[0].address, addresses[0].family);
  291. });
  292. };
  293. }
  294. const abortEmitter = new EventEmitter();
  295. function abort(reason) {
  296. try {
  297. abortEmitter.emit(
  298. 'abort',
  299. !reason || reason.type ? new CanceledError(null, config, req) : reason
  300. );
  301. } catch (err) {
  302. console.warn('emit error', err);
  303. }
  304. }
  305. abortEmitter.once('abort', reject);
  306. const onFinished = () => {
  307. if (config.cancelToken) {
  308. config.cancelToken.unsubscribe(abort);
  309. }
  310. if (config.signal) {
  311. config.signal.removeEventListener('abort', abort);
  312. }
  313. abortEmitter.removeAllListeners();
  314. };
  315. if (config.cancelToken || config.signal) {
  316. config.cancelToken && config.cancelToken.subscribe(abort);
  317. if (config.signal) {
  318. config.signal.aborted ? abort() : config.signal.addEventListener('abort', abort);
  319. }
  320. }
  321. onDone((response, isRejected) => {
  322. isDone = true;
  323. if (isRejected) {
  324. rejected = true;
  325. onFinished();
  326. return;
  327. }
  328. const { data } = response;
  329. if (data instanceof stream.Readable || data instanceof stream.Duplex) {
  330. const offListeners = stream.finished(data, () => {
  331. offListeners();
  332. onFinished();
  333. });
  334. } else {
  335. onFinished();
  336. }
  337. });
  338. // Parse url
  339. const fullPath = buildFullPath(config.baseURL, config.url, config.allowAbsoluteUrls);
  340. const parsed = new URL(fullPath, platform.hasBrowserEnv ? platform.origin : undefined);
  341. const protocol = parsed.protocol || supportedProtocols[0];
  342. if (protocol === 'data:') {
  343. // Apply the same semantics as HTTP: only enforce if a finite, non-negative cap is set.
  344. if (config.maxContentLength > -1) {
  345. // Use the exact string passed to fromDataURI (config.url); fall back to fullPath if needed.
  346. const dataUrl = String(config.url || fullPath || '');
  347. const estimated = estimateDataURLDecodedBytes(dataUrl);
  348. if (estimated > config.maxContentLength) {
  349. return reject(
  350. new AxiosError(
  351. 'maxContentLength size of ' + config.maxContentLength + ' exceeded',
  352. AxiosError.ERR_BAD_RESPONSE,
  353. config
  354. )
  355. );
  356. }
  357. }
  358. let convertedData;
  359. if (method !== 'GET') {
  360. return settle(resolve, reject, {
  361. status: 405,
  362. statusText: 'method not allowed',
  363. headers: {},
  364. config,
  365. });
  366. }
  367. try {
  368. convertedData = fromDataURI(config.url, responseType === 'blob', {
  369. Blob: config.env && config.env.Blob,
  370. });
  371. } catch (err) {
  372. throw AxiosError.from(err, AxiosError.ERR_BAD_REQUEST, config);
  373. }
  374. if (responseType === 'text') {
  375. convertedData = convertedData.toString(responseEncoding);
  376. if (!responseEncoding || responseEncoding === 'utf8') {
  377. convertedData = utils.stripBOM(convertedData);
  378. }
  379. } else if (responseType === 'stream') {
  380. convertedData = stream.Readable.from(convertedData);
  381. }
  382. return settle(resolve, reject, {
  383. data: convertedData,
  384. status: 200,
  385. statusText: 'OK',
  386. headers: new AxiosHeaders(),
  387. config,
  388. });
  389. }
  390. if (supportedProtocols.indexOf(protocol) === -1) {
  391. return reject(
  392. new AxiosError('Unsupported protocol ' + protocol, AxiosError.ERR_BAD_REQUEST, config)
  393. );
  394. }
  395. const headers = AxiosHeaders.from(config.headers).normalize();
  396. // Set User-Agent (required by some servers)
  397. // See https://github.com/axios/axios/issues/69
  398. // User-Agent is specified; handle case where no UA header is desired
  399. // Only set header if it hasn't been set in config
  400. headers.set('User-Agent', 'axios/' + VERSION, false);
  401. const { onUploadProgress, onDownloadProgress } = config;
  402. const maxRate = config.maxRate;
  403. let maxUploadRate = undefined;
  404. let maxDownloadRate = undefined;
  405. // support for spec compliant FormData objects
  406. if (utils.isSpecCompliantForm(data)) {
  407. const userBoundary = headers.getContentType(/boundary=([-_\w\d]{10,70})/i);
  408. data = formDataToStream(
  409. data,
  410. (formHeaders) => {
  411. headers.set(formHeaders);
  412. },
  413. {
  414. tag: `axios-${VERSION}-boundary`,
  415. boundary: (userBoundary && userBoundary[1]) || undefined,
  416. }
  417. );
  418. // support for https://www.npmjs.com/package/form-data api
  419. } else if (utils.isFormData(data) && utils.isFunction(data.getHeaders)) {
  420. headers.set(data.getHeaders());
  421. if (!headers.hasContentLength()) {
  422. try {
  423. const knownLength = await util.promisify(data.getLength).call(data);
  424. Number.isFinite(knownLength) &&
  425. knownLength >= 0 &&
  426. headers.setContentLength(knownLength);
  427. /*eslint no-empty:0*/
  428. } catch (e) {}
  429. }
  430. } else if (utils.isBlob(data) || utils.isFile(data)) {
  431. data.size && headers.setContentType(data.type || 'application/octet-stream');
  432. headers.setContentLength(data.size || 0);
  433. data = stream.Readable.from(readBlob(data));
  434. } else if (data && !utils.isStream(data)) {
  435. if (Buffer.isBuffer(data)) {
  436. // Nothing to do...
  437. } else if (utils.isArrayBuffer(data)) {
  438. data = Buffer.from(new Uint8Array(data));
  439. } else if (utils.isString(data)) {
  440. data = Buffer.from(data, 'utf-8');
  441. } else {
  442. return reject(
  443. new AxiosError(
  444. 'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
  445. AxiosError.ERR_BAD_REQUEST,
  446. config
  447. )
  448. );
  449. }
  450. // Add Content-Length header if data exists
  451. headers.setContentLength(data.length, false);
  452. if (config.maxBodyLength > -1 && data.length > config.maxBodyLength) {
  453. return reject(
  454. new AxiosError(
  455. 'Request body larger than maxBodyLength limit',
  456. AxiosError.ERR_BAD_REQUEST,
  457. config
  458. )
  459. );
  460. }
  461. }
  462. const contentLength = utils.toFiniteNumber(headers.getContentLength());
  463. if (utils.isArray(maxRate)) {
  464. maxUploadRate = maxRate[0];
  465. maxDownloadRate = maxRate[1];
  466. } else {
  467. maxUploadRate = maxDownloadRate = maxRate;
  468. }
  469. if (data && (onUploadProgress || maxUploadRate)) {
  470. if (!utils.isStream(data)) {
  471. data = stream.Readable.from(data, { objectMode: false });
  472. }
  473. data = stream.pipeline(
  474. [
  475. data,
  476. new AxiosTransformStream({
  477. maxRate: utils.toFiniteNumber(maxUploadRate),
  478. }),
  479. ],
  480. utils.noop
  481. );
  482. onUploadProgress &&
  483. data.on(
  484. 'progress',
  485. flushOnFinish(
  486. data,
  487. progressEventDecorator(
  488. contentLength,
  489. progressEventReducer(asyncDecorator(onUploadProgress), false, 3)
  490. )
  491. )
  492. );
  493. }
  494. // HTTP basic authentication
  495. let auth = undefined;
  496. if (config.auth) {
  497. const username = config.auth.username || '';
  498. const password = config.auth.password || '';
  499. auth = username + ':' + password;
  500. }
  501. if (!auth && parsed.username) {
  502. const urlUsername = parsed.username;
  503. const urlPassword = parsed.password;
  504. auth = urlUsername + ':' + urlPassword;
  505. }
  506. auth && headers.delete('authorization');
  507. let path;
  508. try {
  509. path = buildURL(
  510. parsed.pathname + parsed.search,
  511. config.params,
  512. config.paramsSerializer
  513. ).replace(/^\?/, '');
  514. } catch (err) {
  515. const customErr = new Error(err.message);
  516. customErr.config = config;
  517. customErr.url = config.url;
  518. customErr.exists = true;
  519. return reject(customErr);
  520. }
  521. headers.set(
  522. 'Accept-Encoding',
  523. 'gzip, compress, deflate' + (isBrotliSupported ? ', br' : ''),
  524. false
  525. );
  526. const options = {
  527. path,
  528. method: method,
  529. headers: headers.toJSON(),
  530. agents: { http: config.httpAgent, https: config.httpsAgent },
  531. auth,
  532. protocol,
  533. family,
  534. beforeRedirect: dispatchBeforeRedirect,
  535. beforeRedirects: {},
  536. http2Options,
  537. };
  538. // cacheable-lookup integration hotfix
  539. !utils.isUndefined(lookup) && (options.lookup = lookup);
  540. if (config.socketPath) {
  541. options.socketPath = config.socketPath;
  542. } else {
  543. options.hostname = parsed.hostname.startsWith('[')
  544. ? parsed.hostname.slice(1, -1)
  545. : parsed.hostname;
  546. options.port = parsed.port;
  547. setProxy(
  548. options,
  549. config.proxy,
  550. protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path
  551. );
  552. }
  553. let transport;
  554. const isHttpsRequest = isHttps.test(options.protocol);
  555. options.agent = isHttpsRequest ? config.httpsAgent : config.httpAgent;
  556. if (isHttp2) {
  557. transport = http2Transport;
  558. } else {
  559. if (config.transport) {
  560. transport = config.transport;
  561. } else if (config.maxRedirects === 0) {
  562. transport = isHttpsRequest ? https : http;
  563. } else {
  564. if (config.maxRedirects) {
  565. options.maxRedirects = config.maxRedirects;
  566. }
  567. if (config.beforeRedirect) {
  568. options.beforeRedirects.config = config.beforeRedirect;
  569. }
  570. transport = isHttpsRequest ? httpsFollow : httpFollow;
  571. }
  572. }
  573. if (config.maxBodyLength > -1) {
  574. options.maxBodyLength = config.maxBodyLength;
  575. } else {
  576. // follow-redirects does not skip comparison, so it should always succeed for axios -1 unlimited
  577. options.maxBodyLength = Infinity;
  578. }
  579. if (config.insecureHTTPParser) {
  580. options.insecureHTTPParser = config.insecureHTTPParser;
  581. }
  582. // Create the request
  583. req = transport.request(options, function handleResponse(res) {
  584. if (req.destroyed) return;
  585. const streams = [res];
  586. const responseLength = utils.toFiniteNumber(res.headers['content-length']);
  587. if (onDownloadProgress || maxDownloadRate) {
  588. const transformStream = new AxiosTransformStream({
  589. maxRate: utils.toFiniteNumber(maxDownloadRate),
  590. });
  591. onDownloadProgress &&
  592. transformStream.on(
  593. 'progress',
  594. flushOnFinish(
  595. transformStream,
  596. progressEventDecorator(
  597. responseLength,
  598. progressEventReducer(asyncDecorator(onDownloadProgress), true, 3)
  599. )
  600. )
  601. );
  602. streams.push(transformStream);
  603. }
  604. // decompress the response body transparently if required
  605. let responseStream = res;
  606. // return the last request in case of redirects
  607. const lastRequest = res.req || req;
  608. // if decompress disabled we should not decompress
  609. if (config.decompress !== false && res.headers['content-encoding']) {
  610. // if no content, but headers still say that it is encoded,
  611. // remove the header not confuse downstream operations
  612. if (method === 'HEAD' || res.statusCode === 204) {
  613. delete res.headers['content-encoding'];
  614. }
  615. switch ((res.headers['content-encoding'] || '').toLowerCase()) {
  616. /*eslint default-case:0*/
  617. case 'gzip':
  618. case 'x-gzip':
  619. case 'compress':
  620. case 'x-compress':
  621. // add the unzipper to the body stream processing pipeline
  622. streams.push(zlib.createUnzip(zlibOptions));
  623. // remove the content-encoding in order to not confuse downstream operations
  624. delete res.headers['content-encoding'];
  625. break;
  626. case 'deflate':
  627. streams.push(new ZlibHeaderTransformStream());
  628. // add the unzipper to the body stream processing pipeline
  629. streams.push(zlib.createUnzip(zlibOptions));
  630. // remove the content-encoding in order to not confuse downstream operations
  631. delete res.headers['content-encoding'];
  632. break;
  633. case 'br':
  634. if (isBrotliSupported) {
  635. streams.push(zlib.createBrotliDecompress(brotliOptions));
  636. delete res.headers['content-encoding'];
  637. }
  638. }
  639. }
  640. responseStream = streams.length > 1 ? stream.pipeline(streams, utils.noop) : streams[0];
  641. const response = {
  642. status: res.statusCode,
  643. statusText: res.statusMessage,
  644. headers: new AxiosHeaders(res.headers),
  645. config,
  646. request: lastRequest,
  647. };
  648. if (responseType === 'stream') {
  649. response.data = responseStream;
  650. settle(resolve, reject, response);
  651. } else {
  652. const responseBuffer = [];
  653. let totalResponseBytes = 0;
  654. responseStream.on('data', function handleStreamData(chunk) {
  655. responseBuffer.push(chunk);
  656. totalResponseBytes += chunk.length;
  657. // make sure the content length is not over the maxContentLength if specified
  658. if (config.maxContentLength > -1 && totalResponseBytes > config.maxContentLength) {
  659. // stream.destroy() emit aborted event before calling reject() on Node.js v16
  660. rejected = true;
  661. responseStream.destroy();
  662. abort(
  663. new AxiosError(
  664. 'maxContentLength size of ' + config.maxContentLength + ' exceeded',
  665. AxiosError.ERR_BAD_RESPONSE,
  666. config,
  667. lastRequest
  668. )
  669. );
  670. }
  671. });
  672. responseStream.on('aborted', function handlerStreamAborted() {
  673. if (rejected) {
  674. return;
  675. }
  676. const err = new AxiosError(
  677. 'stream has been aborted',
  678. AxiosError.ERR_BAD_RESPONSE,
  679. config,
  680. lastRequest
  681. );
  682. responseStream.destroy(err);
  683. reject(err);
  684. });
  685. responseStream.on('error', function handleStreamError(err) {
  686. if (req.destroyed) return;
  687. reject(AxiosError.from(err, null, config, lastRequest));
  688. });
  689. responseStream.on('end', function handleStreamEnd() {
  690. try {
  691. let responseData =
  692. responseBuffer.length === 1 ? responseBuffer[0] : Buffer.concat(responseBuffer);
  693. if (responseType !== 'arraybuffer') {
  694. responseData = responseData.toString(responseEncoding);
  695. if (!responseEncoding || responseEncoding === 'utf8') {
  696. responseData = utils.stripBOM(responseData);
  697. }
  698. }
  699. response.data = responseData;
  700. } catch (err) {
  701. return reject(AxiosError.from(err, null, config, response.request, response));
  702. }
  703. settle(resolve, reject, response);
  704. });
  705. }
  706. abortEmitter.once('abort', (err) => {
  707. if (!responseStream.destroyed) {
  708. responseStream.emit('error', err);
  709. responseStream.destroy();
  710. }
  711. });
  712. });
  713. abortEmitter.once('abort', (err) => {
  714. if (req.close) {
  715. req.close();
  716. } else {
  717. req.destroy(err);
  718. }
  719. });
  720. // Handle errors
  721. req.on('error', function handleRequestError(err) {
  722. reject(AxiosError.from(err, null, config, req));
  723. });
  724. // set tcp keep alive to prevent drop connection by peer
  725. req.on('socket', function handleRequestSocket(socket) {
  726. // default interval of sending ack packet is 1 minute
  727. socket.setKeepAlive(true, 1000 * 60);
  728. });
  729. // Handle request timeout
  730. if (config.timeout) {
  731. // This is forcing a int timeout to avoid problems if the `req` interface doesn't handle other types.
  732. const timeout = parseInt(config.timeout, 10);
  733. if (Number.isNaN(timeout)) {
  734. abort(
  735. new AxiosError(
  736. 'error trying to parse `config.timeout` to int',
  737. AxiosError.ERR_BAD_OPTION_VALUE,
  738. config,
  739. req
  740. )
  741. );
  742. return;
  743. }
  744. // Sometime, the response will be very slow, and does not respond, the connect event will be block by event loop system.
  745. // And timer callback will be fired, and abort() will be invoked before connection, then get "socket hang up" and code ECONNRESET.
  746. // At this time, if we have a large number of request, nodejs will hang up some socket on background. and the number will up and up.
  747. // And then these socket which be hang up will devouring CPU little by little.
  748. // ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.
  749. req.setTimeout(timeout, function handleRequestTimeout() {
  750. if (isDone) return;
  751. let timeoutErrorMessage = config.timeout
  752. ? 'timeout of ' + config.timeout + 'ms exceeded'
  753. : 'timeout exceeded';
  754. const transitional = config.transitional || transitionalDefaults;
  755. if (config.timeoutErrorMessage) {
  756. timeoutErrorMessage = config.timeoutErrorMessage;
  757. }
  758. abort(
  759. new AxiosError(
  760. timeoutErrorMessage,
  761. transitional.clarifyTimeoutError ? AxiosError.ETIMEDOUT : AxiosError.ECONNABORTED,
  762. config,
  763. req
  764. )
  765. );
  766. });
  767. } else {
  768. // explicitly reset the socket timeout value for a possible `keep-alive` request
  769. req.setTimeout(0);
  770. }
  771. // Send the request
  772. if (utils.isStream(data)) {
  773. let ended = false;
  774. let errored = false;
  775. data.on('end', () => {
  776. ended = true;
  777. });
  778. data.once('error', (err) => {
  779. errored = true;
  780. req.destroy(err);
  781. });
  782. data.on('close', () => {
  783. if (!ended && !errored) {
  784. abort(new CanceledError('Request stream has been aborted', config, req));
  785. }
  786. });
  787. data.pipe(req);
  788. } else {
  789. data && req.write(data);
  790. req.end();
  791. }
  792. });
  793. };
  794. export const __setProxy = setProxy;