http.js 27 KB

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