智慧水务管理系统 - 精河县供水工程综合管理平台

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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 { getProxyForUrl } from 'proxy-from-env';
  6. import HttpsProxyAgent from 'https-proxy-agent';
  7. import http from 'http';
  8. import https from 'https';
  9. import http2 from 'http2';
  10. import util from 'util';
  11. import { resolve as resolvePath } from 'path';
  12. import followRedirects from 'follow-redirects';
  13. import zlib from 'zlib';
  14. import { VERSION } from '../env/data.js';
  15. import transitionalDefaults from '../defaults/transitional.js';
  16. import AxiosError from '../core/AxiosError.js';
  17. import CanceledError from '../cancel/CanceledError.js';
  18. import platform from '../platform/index.js';
  19. import fromDataURI from '../helpers/fromDataURI.js';
  20. import stream from 'stream';
  21. import AxiosHeaders from '../core/AxiosHeaders.js';
  22. import AxiosTransformStream from '../helpers/AxiosTransformStream.js';
  23. import { EventEmitter } from 'events';
  24. import formDataToStream from '../helpers/formDataToStream.js';
  25. import readBlob from '../helpers/readBlob.js';
  26. import ZlibHeaderTransformStream from '../helpers/ZlibHeaderTransformStream.js';
  27. import Http2Sessions from '../helpers/Http2Sessions.js';
  28. import callbackify from '../helpers/callbackify.js';
  29. import shouldBypassProxy from '../helpers/shouldBypassProxy.js';
  30. import { toByteStringHeaderObject } from '../helpers/sanitizeHeaderValue.js';
  31. import {
  32. progressEventReducer,
  33. progressEventDecorator,
  34. asyncDecorator,
  35. } from '../helpers/progressEventReducer.js';
  36. import estimateDataURLDecodedBytes from '../helpers/estimateDataURLDecodedBytes.js';
  37. const zlibOptions = {
  38. flush: zlib.constants.Z_SYNC_FLUSH,
  39. finishFlush: zlib.constants.Z_SYNC_FLUSH,
  40. };
  41. const brotliOptions = {
  42. flush: zlib.constants.BROTLI_OPERATION_FLUSH,
  43. finishFlush: zlib.constants.BROTLI_OPERATION_FLUSH,
  44. };
  45. const zstdOptions = {
  46. flush: zlib.constants.ZSTD_e_flush,
  47. finishFlush: zlib.constants.ZSTD_e_flush,
  48. };
  49. const isBrotliSupported = utils.isFunction(zlib.createBrotliDecompress);
  50. const isZstdSupported = utils.isFunction(zlib.createZstdDecompress);
  51. const ACCEPT_ENCODING = 'gzip, compress, deflate' + (isBrotliSupported ? ', br' : '');
  52. const ACCEPT_ENCODING_WITH_ZSTD = ACCEPT_ENCODING + (isZstdSupported ? ', zstd' : '');
  53. const { http: httpFollow, https: httpsFollow } = followRedirects;
  54. const isHttps = /https:?/;
  55. const FORM_DATA_CONTENT_HEADERS = ['content-type', 'content-length'];
  56. function setFormDataHeaders(headers, formHeaders, policy) {
  57. if (policy !== 'content-only') {
  58. headers.set(formHeaders);
  59. return;
  60. }
  61. Object.entries(formHeaders).forEach(([key, val]) => {
  62. if (FORM_DATA_CONTENT_HEADERS.includes(key.toLowerCase())) {
  63. headers.set(key, val);
  64. }
  65. });
  66. }
  67. // Symbols used to bind a single 'error' listener to a pooled socket and track
  68. // the request currently owning that socket across keep-alive reuse (issue #10780).
  69. const kAxiosSocketListener = Symbol('axios.http.socketListener');
  70. const kAxiosCurrentReq = Symbol('axios.http.currentReq');
  71. // Tags HttpsProxyAgent instances installed by setProxy() so the redirect path
  72. // can strip them without clobbering a user-supplied agent that happens to be
  73. // an HttpsProxyAgent.
  74. const kAxiosInstalledTunnel = Symbol('axios.http.installedTunnel');
  75. // Cache of CONNECT-tunneling agents keyed by proxy config so repeat requests
  76. // through the same proxy reuse a single agent (and its socket pool). The
  77. // keyspace is bounded by the set of distinct proxy configs the process uses,
  78. // so unbounded growth is not a concern in practice.
  79. const tunnelingAgentCache = new Map();
  80. const tunnelingAgentCacheUser = new WeakMap();
  81. function getTunnelingAgent(agentOptions, userHttpsAgent) {
  82. const key =
  83. agentOptions.protocol +
  84. '//' +
  85. agentOptions.hostname +
  86. ':' +
  87. (agentOptions.port || '') +
  88. '#' +
  89. (agentOptions.auth || '');
  90. const cache = userHttpsAgent
  91. ? (tunnelingAgentCacheUser.get(userHttpsAgent) ||
  92. tunnelingAgentCacheUser.set(userHttpsAgent, new Map()).get(userHttpsAgent))
  93. : tunnelingAgentCache;
  94. let agent = cache.get(key);
  95. if (agent) return agent;
  96. // Forward the user's TLS options (custom CA, rejectUnauthorized, client cert,
  97. // etc.) into the tunneling agent so they apply to the origin TLS upgrade
  98. // performed after CONNECT. Our proxy fields take precedence on conflict.
  99. const merged = userHttpsAgent && userHttpsAgent.options
  100. ? { ...userHttpsAgent.options, ...agentOptions }
  101. : agentOptions;
  102. agent = new HttpsProxyAgent(merged);
  103. if (userHttpsAgent && userHttpsAgent.options) {
  104. const originTLSOptions = { ...userHttpsAgent.options };
  105. const callback = agent.callback;
  106. agent.callback = function axiosTunnelingAgentCallback(req, opts) {
  107. // HttpsProxyAgent v5 reads callback opts for the post-CONNECT origin TLS upgrade.
  108. return callback.call(this, req, { ...originTLSOptions, ...opts });
  109. };
  110. }
  111. agent[kAxiosInstalledTunnel] = true;
  112. cache.set(key, agent);
  113. return agent;
  114. }
  115. const supportedProtocols = platform.protocols.map((protocol) => {
  116. return protocol + ':';
  117. });
  118. // Node's WHATWG URL parser returns `username` and `password` percent-encoded.
  119. // Decode before composing the `auth` option so credentials such as
  120. // `my%40email.com:pass` are sent as `my@email.com:pass`. Falls back to the
  121. // original value for malformed input so a bad encoding never throws.
  122. const decodeURIComponentSafe = (value) => {
  123. if (!utils.isString(value)) {
  124. return value;
  125. }
  126. try {
  127. return decodeURIComponent(value);
  128. } catch (error) {
  129. return value;
  130. }
  131. };
  132. const flushOnFinish = (stream, [throttled, flush]) => {
  133. stream.on('end', flush).on('error', flush);
  134. return throttled;
  135. };
  136. const http2Sessions = new Http2Sessions();
  137. /**
  138. * If the proxy, auth, sensitive header, or config beforeRedirects functions are defined,
  139. * call them with the options object.
  140. *
  141. * @param {Object<string, any>} options - The options object that was passed to the request.
  142. *
  143. * @returns {Object<string, any>}
  144. */
  145. function dispatchBeforeRedirect(options, responseDetails, requestDetails) {
  146. if (options.beforeRedirects.proxy) {
  147. options.beforeRedirects.proxy(options);
  148. }
  149. if (options.beforeRedirects.auth) {
  150. options.beforeRedirects.auth(options);
  151. }
  152. if (options.beforeRedirects.sensitiveHeaders) {
  153. options.beforeRedirects.sensitiveHeaders(options, requestDetails);
  154. }
  155. if (options.beforeRedirects.config) {
  156. options.beforeRedirects.config(options, responseDetails, requestDetails);
  157. }
  158. }
  159. function stripMatchingHeaders(headers, sensitiveSet) {
  160. if (!headers) {
  161. return;
  162. }
  163. Object.keys(headers).forEach((header) => {
  164. if (sensitiveSet.has(header.toLowerCase())) {
  165. delete headers[header];
  166. }
  167. });
  168. }
  169. function isSameOriginRedirect(redirectOptions, requestDetails) {
  170. if (!requestDetails) {
  171. return false;
  172. }
  173. try {
  174. return new URL(requestDetails.url).origin === new URL(redirectOptions.href).origin;
  175. } catch (e) {
  176. // If origin comparison fails, treat the redirect as unsafe.
  177. return false;
  178. }
  179. }
  180. /**
  181. * If the proxy or config afterRedirects functions are defined, call them with the options
  182. *
  183. * @param {http.ClientRequestArgs} options
  184. * @param {AxiosProxyConfig} configProxy configuration from Axios options object
  185. * @param {string} location
  186. *
  187. * @returns {http.ClientRequestArgs}
  188. */
  189. function setProxy(options, configProxy, location, isRedirect, configHttpsAgent) {
  190. let proxy = configProxy;
  191. if (!proxy && proxy !== false) {
  192. const proxyUrl = getProxyForUrl(location);
  193. if (proxyUrl) {
  194. if (!shouldBypassProxy(location)) {
  195. proxy = new URL(proxyUrl);
  196. }
  197. }
  198. }
  199. // On redirect re-invocation, strip any stale Proxy-Authorization header carried
  200. // over from the prior request (e.g. new target no longer uses a proxy, or uses
  201. // a different proxy). Skip on the initial request so user-supplied headers are
  202. // preserved. Header names are case-insensitive, so remove every case variant.
  203. if (isRedirect && options.headers) {
  204. for (const name of Object.keys(options.headers)) {
  205. if (name.toLowerCase() === 'proxy-authorization') {
  206. delete options.headers[name];
  207. }
  208. }
  209. }
  210. // Strip any tunneling agent we installed for the previous hop so a redirect
  211. // that drops the proxy or crosses an HTTPS↔HTTP boundary doesn't reuse a
  212. // stale one. Match on our Symbol marker so a user-supplied HttpsProxyAgent
  213. // (which won't carry the marker) is left alone.
  214. if (isRedirect && options.agent && options.agent[kAxiosInstalledTunnel]) {
  215. options.agent = undefined;
  216. }
  217. if (proxy) {
  218. // Read proxy fields without traversing the prototype chain. URL instances expose
  219. // username/password/hostname/host/port/protocol via getters on URL.prototype (so
  220. // direct reads are shielded), but plain object proxies — and the `auth` field
  221. // (which URL does not expose) — must be guarded so a polluted Object.prototype
  222. // (e.g. Object.prototype.auth = { username, password }) cannot inject
  223. // attacker-controlled credentials into the Proxy-Authorization header or
  224. // redirect proxying to an attacker-controlled host.
  225. const isProxyURL = proxy instanceof URL;
  226. const readProxyField = (key) =>
  227. isProxyURL || utils.hasOwnProp(proxy, key) ? proxy[key] : undefined;
  228. const proxyUsername = readProxyField('username');
  229. const proxyPassword = readProxyField('password');
  230. let proxyAuth = utils.hasOwnProp(proxy, 'auth') ? proxy.auth : undefined;
  231. // Basic proxy authorization
  232. if (proxyUsername) {
  233. proxyAuth = (proxyUsername || '') + ':' + (proxyPassword || '');
  234. }
  235. if (proxyAuth) {
  236. // Support proxy auth object form. Read sub-fields via own-prop checks so a
  237. // plain object inheriting from polluted Object.prototype cannot leak creds.
  238. const authIsObject = typeof proxyAuth === 'object';
  239. const authUsername =
  240. authIsObject && utils.hasOwnProp(proxyAuth, 'username') ? proxyAuth.username : undefined;
  241. const authPassword =
  242. authIsObject && utils.hasOwnProp(proxyAuth, 'password') ? proxyAuth.password : undefined;
  243. const validProxyAuth = Boolean(authUsername || authPassword);
  244. if (validProxyAuth) {
  245. proxyAuth = (authUsername || '') + ':' + (authPassword || '');
  246. } else if (authIsObject) {
  247. throw new AxiosError('Invalid proxy authorization', AxiosError.ERR_BAD_OPTION, { proxy });
  248. }
  249. }
  250. const targetIsHttps = isHttps.test(options.protocol);
  251. if (targetIsHttps) {
  252. // CONNECT-tunneling path for HTTPS targets. Preserves end-to-end TLS to
  253. // the origin so the proxy cannot inspect the URL, headers, or body — the
  254. // behavior already promised by THREATMODEL.md (T-R9). HttpsProxyAgent
  255. // sends Proxy-Authorization on the CONNECT request only, never on the
  256. // wrapped TLS request, which is why we don't stamp it onto
  257. // options.headers here. If the user already supplied an HttpsProxyAgent,
  258. // they own tunneling end-to-end and we leave them alone; otherwise we
  259. // install our own tunneling agent and forward their TLS options (if any)
  260. // so a custom httpsAgent for cert pinning / rejectUnauthorized still
  261. // applies to the origin TLS upgrade.
  262. if (!(configHttpsAgent instanceof HttpsProxyAgent)) {
  263. const proxyHost = readProxyField('hostname') || readProxyField('host');
  264. const proxyPort = readProxyField('port');
  265. const rawProxyProtocol = readProxyField('protocol');
  266. const normalizedProtocol = rawProxyProtocol
  267. ? rawProxyProtocol.includes(':')
  268. ? rawProxyProtocol
  269. : `${rawProxyProtocol}:`
  270. : 'http:';
  271. // Bracket IPv6 literals for URL parsing; URL.hostname strips the
  272. // brackets again on read so the agent receives the raw form.
  273. const proxyHostForURL =
  274. proxyHost && proxyHost.includes(':') && !proxyHost.startsWith('[')
  275. ? `[${proxyHost}]`
  276. : proxyHost;
  277. const proxyURL = new URL(
  278. `${normalizedProtocol}//${proxyHostForURL}${proxyPort ? ':' + proxyPort : ''}`
  279. );
  280. const agentOptions = {
  281. protocol: proxyURL.protocol,
  282. hostname: proxyURL.hostname.replace(/^\[|\]$/g, ''),
  283. port: proxyURL.port,
  284. auth: proxyAuth && typeof proxyAuth === 'string' ? proxyAuth : undefined,
  285. };
  286. if (proxyURL.protocol === 'https:') {
  287. agentOptions.ALPNProtocols = ['http/1.1'];
  288. }
  289. const tunnelingAgent = getTunnelingAgent(agentOptions, configHttpsAgent);
  290. // Set both: `options.agent` is consumed by the native https.request path
  291. // (maxRedirects === 0); `options.agents.https` is consumed by
  292. // follow-redirects, which ignores `options.agent` when `options.agents`
  293. // is present.
  294. options.agent = tunnelingAgent;
  295. if (options.agents) {
  296. options.agents.https = tunnelingAgent;
  297. }
  298. }
  299. } else {
  300. // Forward-proxy mode for plaintext HTTP targets. The request line carries
  301. // the absolute URL and the proxy sees everything — acceptable for plain
  302. // HTTP since the wire was already plaintext.
  303. if (proxyAuth) {
  304. const base64 = Buffer.from(proxyAuth, 'utf8').toString('base64');
  305. options.headers['Proxy-Authorization'] = 'Basic ' + base64;
  306. }
  307. // Preserve a user-supplied Host header (case-insensitive) so callers can override
  308. // the value forwarded to the proxy; otherwise default to the request URL's host.
  309. let hasUserHostHeader = false;
  310. for (const name of Object.keys(options.headers)) {
  311. if (name.toLowerCase() === 'host') {
  312. hasUserHostHeader = true;
  313. break;
  314. }
  315. }
  316. if (!hasUserHostHeader) {
  317. options.headers.host = options.hostname + (options.port ? ':' + options.port : '');
  318. }
  319. const proxyHost = readProxyField('hostname') || readProxyField('host');
  320. options.hostname = proxyHost;
  321. // Replace 'host' since options is not a URL object
  322. options.host = proxyHost;
  323. options.port = readProxyField('port');
  324. options.path = location;
  325. const proxyProtocol = readProxyField('protocol');
  326. if (proxyProtocol) {
  327. options.protocol = proxyProtocol.includes(':') ? proxyProtocol : `${proxyProtocol}:`;
  328. }
  329. }
  330. }
  331. options.beforeRedirects.proxy = function beforeRedirect(redirectOptions) {
  332. // Configure proxy for redirected request, passing the original config proxy to apply
  333. // the exact same logic as if the redirected request was performed by axios directly.
  334. setProxy(redirectOptions, configProxy, redirectOptions.href, true, configHttpsAgent);
  335. };
  336. }
  337. const isHttpAdapterSupported =
  338. typeof process !== 'undefined' && utils.kindOf(process) === 'process';
  339. // temporary hotfix
  340. const wrapAsync = (asyncExecutor) => {
  341. return new Promise((resolve, reject) => {
  342. let onDone;
  343. let isDone;
  344. const done = (value, isRejected) => {
  345. if (isDone) return;
  346. isDone = true;
  347. onDone && onDone(value, isRejected);
  348. };
  349. const _resolve = (value) => {
  350. done(value);
  351. resolve(value);
  352. };
  353. const _reject = (reason) => {
  354. done(reason, true);
  355. reject(reason);
  356. };
  357. asyncExecutor(_resolve, _reject, (onDoneHandler) => (onDone = onDoneHandler)).catch(_reject);
  358. });
  359. };
  360. const resolveFamily = ({ address, family }) => {
  361. if (!utils.isString(address)) {
  362. throw TypeError('address must be a string');
  363. }
  364. return {
  365. address,
  366. family: family || (address.indexOf('.') < 0 ? 6 : 4),
  367. };
  368. };
  369. const buildAddressEntry = (address, family) =>
  370. resolveFamily(utils.isObject(address) ? address : { address, family });
  371. const http2Transport = {
  372. request(options, cb) {
  373. const authority =
  374. options.protocol +
  375. '//' +
  376. options.hostname +
  377. ':' +
  378. (options.port || (options.protocol === 'https:' ? 443 : 80));
  379. const { http2Options, headers } = options;
  380. const session = http2Sessions.getSession(authority, http2Options);
  381. const { HTTP2_HEADER_SCHEME, HTTP2_HEADER_METHOD, HTTP2_HEADER_PATH, HTTP2_HEADER_STATUS } =
  382. http2.constants;
  383. const http2Headers = {
  384. [HTTP2_HEADER_SCHEME]: options.protocol.replace(':', ''),
  385. [HTTP2_HEADER_METHOD]: options.method,
  386. [HTTP2_HEADER_PATH]: options.path,
  387. };
  388. utils.forEach(headers, (header, name) => {
  389. name.charAt(0) !== ':' && (http2Headers[name] = header);
  390. });
  391. const req = session.request(http2Headers);
  392. req.once('response', (responseHeaders) => {
  393. const response = req; //duplex
  394. responseHeaders = Object.assign({}, responseHeaders);
  395. const status = responseHeaders[HTTP2_HEADER_STATUS];
  396. delete responseHeaders[HTTP2_HEADER_STATUS];
  397. response.headers = responseHeaders;
  398. response.statusCode = +status;
  399. cb(response);
  400. });
  401. return req;
  402. },
  403. };
  404. /*eslint consistent-return:0*/
  405. export default isHttpAdapterSupported &&
  406. function httpAdapter(config) {
  407. return wrapAsync(async function dispatchHttpRequest(resolve, reject, onDone) {
  408. // Read config pollution-safely: own properties and members inherited from
  409. // a non-Object.prototype source (e.g. an Object.create(defaults) template)
  410. // are honored, but values injected onto a polluted Object.prototype are
  411. // ignored. All behavior-affecting reads in this adapter go through own()
  412. // so the protection boundary stays consistent.
  413. const own = (key) => utils.getSafeProp(config, key);
  414. const transitional = own('transitional') || transitionalDefaults;
  415. let data = own('data');
  416. let lookup = own('lookup');
  417. let family = own('family');
  418. let httpVersion = own('httpVersion');
  419. if (httpVersion === undefined) httpVersion = 1;
  420. let http2Options = own('http2Options');
  421. const responseType = own('responseType');
  422. const responseEncoding = own('responseEncoding');
  423. const httpAgent = own('httpAgent');
  424. const httpsAgent = own('httpsAgent');
  425. const method = own('method').toUpperCase();
  426. const maxRedirects = own('maxRedirects');
  427. const maxBodyLength = own('maxBodyLength');
  428. const maxContentLength = own('maxContentLength');
  429. const decompress = own('decompress');
  430. let isDone;
  431. let rejected = false;
  432. let req;
  433. let connectPhaseTimer;
  434. httpVersion = +httpVersion;
  435. if (Number.isNaN(httpVersion)) {
  436. throw TypeError(`Invalid protocol version: '${config.httpVersion}' is not a number`);
  437. }
  438. if (httpVersion !== 1 && httpVersion !== 2) {
  439. throw TypeError(`Unsupported protocol version '${httpVersion}'`);
  440. }
  441. const isHttp2 = httpVersion === 2;
  442. if (lookup) {
  443. const _lookup = callbackify(lookup, (value) => (utils.isArray(value) ? value : [value]));
  444. // hotfix to support opt.all option which is required for node 20.x
  445. lookup = (hostname, opt, cb) => {
  446. _lookup(hostname, opt, (err, arg0, arg1) => {
  447. if (err) {
  448. return cb(err);
  449. }
  450. const addresses = utils.isArray(arg0)
  451. ? arg0.map((addr) => buildAddressEntry(addr))
  452. : [buildAddressEntry(arg0, arg1)];
  453. opt.all ? cb(err, addresses) : cb(err, addresses[0].address, addresses[0].family);
  454. });
  455. };
  456. }
  457. const abortEmitter = new EventEmitter();
  458. function abort(reason) {
  459. try {
  460. abortEmitter.emit(
  461. 'abort',
  462. !reason || reason.type ? new CanceledError(null, config, req) : reason
  463. );
  464. } catch (err) {
  465. // ignore emit errors
  466. }
  467. }
  468. function clearConnectPhaseTimer() {
  469. if (connectPhaseTimer) {
  470. clearTimeout(connectPhaseTimer);
  471. connectPhaseTimer = null;
  472. }
  473. }
  474. function createTimeoutError() {
  475. const configTimeout = own('timeout');
  476. let timeoutErrorMessage = configTimeout
  477. ? 'timeout of ' + configTimeout + 'ms exceeded'
  478. : 'timeout exceeded';
  479. const configTimeoutErrorMessage = own('timeoutErrorMessage');
  480. if (configTimeoutErrorMessage) {
  481. timeoutErrorMessage = configTimeoutErrorMessage;
  482. }
  483. return new AxiosError(
  484. timeoutErrorMessage,
  485. transitional.clarifyTimeoutError ? AxiosError.ETIMEDOUT : AxiosError.ECONNABORTED,
  486. config,
  487. req
  488. );
  489. }
  490. abortEmitter.once('abort', reject);
  491. const onFinished = () => {
  492. clearConnectPhaseTimer();
  493. if (config.cancelToken) {
  494. config.cancelToken.unsubscribe(abort);
  495. }
  496. if (config.signal) {
  497. config.signal.removeEventListener('abort', abort);
  498. }
  499. abortEmitter.removeAllListeners();
  500. };
  501. if (config.cancelToken || config.signal) {
  502. config.cancelToken && config.cancelToken.subscribe(abort);
  503. if (config.signal) {
  504. config.signal.aborted ? abort() : config.signal.addEventListener('abort', abort);
  505. }
  506. }
  507. onDone((response, isRejected) => {
  508. isDone = true;
  509. clearConnectPhaseTimer();
  510. if (isRejected) {
  511. rejected = true;
  512. onFinished();
  513. return;
  514. }
  515. const { data } = response;
  516. if (data instanceof stream.Readable || data instanceof stream.Duplex) {
  517. const offListeners = stream.finished(data, () => {
  518. offListeners();
  519. onFinished();
  520. });
  521. } else {
  522. onFinished();
  523. }
  524. });
  525. // Parse url
  526. const fullPath = buildFullPath(own('baseURL'), own('url'), own('allowAbsoluteUrls'), config);
  527. const parsed = new URL(fullPath, platform.hasBrowserEnv ? platform.origin : undefined);
  528. const protocol = parsed.protocol || supportedProtocols[0];
  529. if (protocol === 'data:') {
  530. // Apply the same semantics as HTTP: only enforce if a finite, non-negative cap is set.
  531. if (maxContentLength > -1) {
  532. // Use the exact string passed to fromDataURI (the configured url); fall back to fullPath if needed.
  533. const dataUrl = String(own('url') || fullPath || '');
  534. const estimated = estimateDataURLDecodedBytes(dataUrl);
  535. if (estimated > maxContentLength) {
  536. return reject(
  537. new AxiosError(
  538. 'maxContentLength size of ' + maxContentLength + ' exceeded',
  539. AxiosError.ERR_BAD_RESPONSE,
  540. config
  541. )
  542. );
  543. }
  544. }
  545. let convertedData;
  546. if (method !== 'GET') {
  547. return settle(resolve, reject, {
  548. status: 405,
  549. statusText: 'method not allowed',
  550. headers: {},
  551. config,
  552. });
  553. }
  554. try {
  555. convertedData = fromDataURI(own('url'), responseType === 'blob', {
  556. Blob: config.env && config.env.Blob,
  557. });
  558. } catch (err) {
  559. throw AxiosError.from(err, AxiosError.ERR_BAD_REQUEST, config);
  560. }
  561. if (responseType === 'text') {
  562. convertedData = convertedData.toString(responseEncoding);
  563. if (!responseEncoding || responseEncoding === 'utf8') {
  564. convertedData = utils.stripBOM(convertedData);
  565. }
  566. } else if (responseType === 'stream') {
  567. convertedData = stream.Readable.from(convertedData);
  568. }
  569. return settle(resolve, reject, {
  570. data: convertedData,
  571. status: 200,
  572. statusText: 'OK',
  573. headers: new AxiosHeaders(),
  574. config,
  575. });
  576. }
  577. if (supportedProtocols.indexOf(protocol) === -1) {
  578. return reject(
  579. new AxiosError('Unsupported protocol ' + protocol, AxiosError.ERR_BAD_REQUEST, config)
  580. );
  581. }
  582. const headers = AxiosHeaders.from(config.headers).normalize();
  583. // Set User-Agent (required by some servers)
  584. // See https://github.com/axios/axios/issues/69
  585. // User-Agent is specified; handle case where no UA header is desired
  586. // Only set header if it hasn't been set in config
  587. headers.set('User-Agent', 'axios/' + VERSION, false);
  588. const { onUploadProgress, onDownloadProgress } = config;
  589. const maxRate = config.maxRate;
  590. let maxUploadRate = undefined;
  591. let maxDownloadRate = undefined;
  592. // support for spec compliant FormData objects
  593. if (utils.isSpecCompliantForm(data)) {
  594. const userBoundary = headers.getContentType(/boundary=([-_\w\d]{10,70})/i);
  595. data = formDataToStream(
  596. data,
  597. (formHeaders) => {
  598. headers.set(formHeaders);
  599. },
  600. {
  601. tag: `axios-${VERSION}-boundary`,
  602. boundary: (userBoundary && userBoundary[1]) || undefined,
  603. }
  604. );
  605. // support for https://www.npmjs.com/package/form-data api
  606. } else if (
  607. utils.isFormData(data) &&
  608. utils.isFunction(data.getHeaders) &&
  609. data.getHeaders !== Object.prototype.getHeaders
  610. ) {
  611. setFormDataHeaders(headers, data.getHeaders(), own('formDataHeaderPolicy'));
  612. if (!headers.hasContentLength()) {
  613. try {
  614. const knownLength = await util.promisify(data.getLength).call(data);
  615. Number.isFinite(knownLength) &&
  616. knownLength >= 0 &&
  617. headers.setContentLength(knownLength);
  618. /*eslint no-empty:0*/
  619. } catch (e) {}
  620. }
  621. } else if (utils.isBlob(data) || utils.isFile(data)) {
  622. data.size && headers.setContentType(data.type || 'application/octet-stream');
  623. headers.setContentLength(data.size || 0);
  624. data = stream.Readable.from(readBlob(data));
  625. } else if (data && !utils.isStream(data)) {
  626. if (Buffer.isBuffer(data)) {
  627. // Nothing to do...
  628. } else if (utils.isArrayBuffer(data)) {
  629. data = Buffer.from(new Uint8Array(data));
  630. } else if (utils.isString(data)) {
  631. data = Buffer.from(data, 'utf-8');
  632. } else {
  633. return reject(
  634. new AxiosError(
  635. 'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
  636. AxiosError.ERR_BAD_REQUEST,
  637. config
  638. )
  639. );
  640. }
  641. // Add Content-Length header if data exists
  642. headers.setContentLength(data.length, false);
  643. if (maxBodyLength > -1 && data.length > maxBodyLength) {
  644. return reject(
  645. new AxiosError(
  646. 'Request body larger than maxBodyLength limit',
  647. AxiosError.ERR_BAD_REQUEST,
  648. config
  649. )
  650. );
  651. }
  652. }
  653. const contentLength = utils.toFiniteNumber(headers.getContentLength());
  654. if (utils.isArray(maxRate)) {
  655. maxUploadRate = maxRate[0];
  656. maxDownloadRate = maxRate[1];
  657. } else {
  658. maxUploadRate = maxDownloadRate = maxRate;
  659. }
  660. if (data && (onUploadProgress || maxUploadRate)) {
  661. if (!utils.isStream(data)) {
  662. data = stream.Readable.from(data, { objectMode: false });
  663. }
  664. data = stream.pipeline(
  665. [
  666. data,
  667. new AxiosTransformStream({
  668. maxRate: utils.toFiniteNumber(maxUploadRate),
  669. }),
  670. ],
  671. utils.noop
  672. );
  673. onUploadProgress &&
  674. data.on(
  675. 'progress',
  676. flushOnFinish(
  677. data,
  678. progressEventDecorator(
  679. contentLength,
  680. progressEventReducer(asyncDecorator(onUploadProgress), false, 3)
  681. )
  682. )
  683. );
  684. }
  685. // HTTP basic authentication
  686. let auth = undefined;
  687. const configAuth = own('auth');
  688. if (configAuth) {
  689. const username = utils.getSafeProp(configAuth, 'username') || '';
  690. const password = utils.getSafeProp(configAuth, 'password') || '';
  691. auth = username + ':' + password;
  692. }
  693. if (!auth && (parsed.username || parsed.password)) {
  694. const urlUsername = decodeURIComponentSafe(parsed.username);
  695. const urlPassword = decodeURIComponentSafe(parsed.password);
  696. auth = urlUsername + ':' + urlPassword;
  697. }
  698. auth && headers.delete('authorization');
  699. let path;
  700. try {
  701. path = buildURL(
  702. parsed.pathname + parsed.search,
  703. own('params'),
  704. own('paramsSerializer')
  705. ).replace(/^\?/, '');
  706. } catch (err) {
  707. const customErr = new Error(err.message);
  708. customErr.config = config;
  709. customErr.url = own('url');
  710. customErr.exists = true;
  711. return reject(customErr);
  712. }
  713. headers.set(
  714. 'Accept-Encoding',
  715. utils.hasOwnProp(transitional, 'advertiseZstdAcceptEncoding') &&
  716. transitional.advertiseZstdAcceptEncoding === true ? ACCEPT_ENCODING_WITH_ZSTD : ACCEPT_ENCODING,
  717. false
  718. );
  719. // Null-prototype to block prototype pollution gadgets on properties read
  720. // directly by Node's http.request (e.g. insecureHTTPParser, lookup).
  721. const options = Object.assign(Object.create(null), {
  722. path,
  723. method: method,
  724. headers: toByteStringHeaderObject(headers),
  725. agents: { http: httpAgent, https: httpsAgent },
  726. auth,
  727. protocol,
  728. family,
  729. beforeRedirect: dispatchBeforeRedirect,
  730. beforeRedirects: Object.create(null),
  731. http2Options,
  732. });
  733. // cacheable-lookup integration hotfix
  734. !utils.isUndefined(lookup) && (options.lookup = lookup);
  735. const socketPath = own('socketPath');
  736. if (socketPath) {
  737. if (typeof socketPath !== 'string') {
  738. return reject(
  739. new AxiosError('socketPath must be a string', AxiosError.ERR_BAD_OPTION_VALUE, config)
  740. );
  741. }
  742. const allowedSocketPaths = own('allowedSocketPaths');
  743. if (allowedSocketPaths != null) {
  744. const allowed = Array.isArray(allowedSocketPaths)
  745. ? allowedSocketPaths
  746. : [allowedSocketPaths];
  747. const resolvedSocket = resolvePath(socketPath);
  748. const isAllowed = allowed.some(
  749. (entry) => typeof entry === 'string' && resolvePath(entry) === resolvedSocket
  750. );
  751. if (!isAllowed) {
  752. return reject(
  753. new AxiosError(
  754. `socketPath "${socketPath}" is not permitted by allowedSocketPaths`,
  755. AxiosError.ERR_BAD_OPTION_VALUE,
  756. config
  757. )
  758. );
  759. }
  760. }
  761. options.socketPath = socketPath;
  762. } else {
  763. options.hostname = parsed.hostname.startsWith('[')
  764. ? parsed.hostname.slice(1, -1)
  765. : parsed.hostname;
  766. options.port = parsed.port;
  767. setProxy(
  768. options,
  769. own('proxy'),
  770. protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path,
  771. false,
  772. httpsAgent
  773. );
  774. }
  775. let transport;
  776. let isNativeTransport = false;
  777. // True only for the follow-redirects transport, which applies
  778. // options.maxBodyLength itself. Every other transport (http2, native
  779. // http/https, a user-supplied custom transport) needs the explicit
  780. // byte-counting pipeline below to enforce maxBodyLength on streamed uploads.
  781. let transportEnforcesMaxBodyLength = false;
  782. const isHttpsRequest = isHttps.test(options.protocol);
  783. // Don't clobber a CONNECT-tunneling agent installed by setProxy() for an
  784. // HTTPS target.
  785. if (options.agent == null) {
  786. options.agent = isHttpsRequest ? httpsAgent : httpAgent;
  787. }
  788. if (isHttp2) {
  789. transport = http2Transport;
  790. } else {
  791. const configTransport = own('transport');
  792. if (configTransport) {
  793. transport = configTransport;
  794. } else if (maxRedirects === 0) {
  795. transport = isHttpsRequest ? https : http;
  796. isNativeTransport = true;
  797. } else {
  798. transportEnforcesMaxBodyLength = true;
  799. options.sensitiveHeaders = [];
  800. if (maxRedirects) {
  801. options.maxRedirects = maxRedirects;
  802. }
  803. const configBeforeRedirect = own('beforeRedirect');
  804. if (configBeforeRedirect) {
  805. options.beforeRedirects.config = configBeforeRedirect;
  806. }
  807. if (auth) {
  808. // Restore HTTP Basic credentials on same-origin redirects only.
  809. // follow-redirects >= 1.15.8 strips Authorization on every redirect (see #6929);
  810. // cross-origin stripping is the documented mitigation for T-R2 in THREATMODEL.md
  811. // and is preserved by deliberately not restoring on origin change.
  812. const requestOrigin = parsed.origin;
  813. const authToRestore = auth;
  814. options.beforeRedirects.auth = function beforeRedirectAuth(redirectOptions) {
  815. try {
  816. if (new URL(redirectOptions.href).origin === requestOrigin) {
  817. redirectOptions.auth = authToRestore;
  818. }
  819. } catch (e) {
  820. // ignore malformed URL: leaving auth stripped is fail-safe
  821. }
  822. };
  823. }
  824. const sensitiveHeaders = own('sensitiveHeaders');
  825. if (sensitiveHeaders != null) {
  826. if (!utils.isArray(sensitiveHeaders)) {
  827. return reject(
  828. new AxiosError(
  829. 'sensitiveHeaders must be an array of strings',
  830. AxiosError.ERR_BAD_OPTION_VALUE,
  831. config
  832. )
  833. );
  834. }
  835. const sensitiveSet = new Set();
  836. for (const header of sensitiveHeaders) {
  837. if (!utils.isString(header)) {
  838. return reject(
  839. new AxiosError(
  840. 'sensitiveHeaders must be an array of strings',
  841. AxiosError.ERR_BAD_OPTION_VALUE,
  842. config
  843. )
  844. );
  845. }
  846. sensitiveSet.add(header.toLowerCase());
  847. }
  848. if (sensitiveSet.size) {
  849. options.sensitiveHeaders = Array.from(sensitiveSet);
  850. options.beforeRedirects.sensitiveHeaders = function beforeRedirectSensitiveHeaders(
  851. redirectOptions,
  852. requestDetails
  853. ) {
  854. if (!isSameOriginRedirect(redirectOptions, requestDetails)) {
  855. stripMatchingHeaders(redirectOptions.headers, sensitiveSet);
  856. }
  857. };
  858. }
  859. }
  860. transport = isHttpsRequest ? httpsFollow : httpFollow;
  861. }
  862. }
  863. if (maxBodyLength > -1) {
  864. options.maxBodyLength = maxBodyLength;
  865. } else {
  866. // follow-redirects does not skip comparison, so it should always succeed for axios -1 unlimited
  867. options.maxBodyLength = Infinity;
  868. }
  869. // Always set an explicit own value so a polluted
  870. // Object.prototype.insecureHTTPParser cannot enable the lenient parser
  871. // through Node's internal options copy
  872. options.insecureHTTPParser = Boolean(own('insecureHTTPParser'));
  873. // Create the request
  874. req = transport.request(options, function handleResponse(res) {
  875. clearConnectPhaseTimer();
  876. if (req.destroyed) return;
  877. const streams = [res];
  878. const responseLength = utils.toFiniteNumber(res.headers['content-length']);
  879. if (onDownloadProgress || maxDownloadRate) {
  880. const transformStream = new AxiosTransformStream({
  881. maxRate: utils.toFiniteNumber(maxDownloadRate),
  882. });
  883. onDownloadProgress &&
  884. transformStream.on(
  885. 'progress',
  886. flushOnFinish(
  887. transformStream,
  888. progressEventDecorator(
  889. responseLength,
  890. progressEventReducer(asyncDecorator(onDownloadProgress), true, 3)
  891. )
  892. )
  893. );
  894. streams.push(transformStream);
  895. }
  896. // decompress the response body transparently if required
  897. let responseStream = res;
  898. // return the last request in case of redirects
  899. const lastRequest = res.req || req;
  900. // if decompress disabled we should not decompress
  901. if (decompress !== false && res.headers['content-encoding']) {
  902. // if no content, but headers still say that it is encoded,
  903. // remove the header not confuse downstream operations
  904. if (method === 'HEAD' || res.statusCode === 204) {
  905. delete res.headers['content-encoding'];
  906. }
  907. switch ((res.headers['content-encoding'] || '').toLowerCase()) {
  908. /*eslint default-case:0*/
  909. case 'gzip':
  910. case 'x-gzip':
  911. case 'compress':
  912. case 'x-compress':
  913. // add the unzipper to the body stream processing pipeline
  914. streams.push(zlib.createUnzip(zlibOptions));
  915. // remove the content-encoding in order to not confuse downstream operations
  916. delete res.headers['content-encoding'];
  917. break;
  918. case 'deflate':
  919. streams.push(new ZlibHeaderTransformStream());
  920. // add the unzipper to the body stream processing pipeline
  921. streams.push(zlib.createUnzip(zlibOptions));
  922. // remove the content-encoding in order to not confuse downstream operations
  923. delete res.headers['content-encoding'];
  924. break;
  925. case 'br':
  926. if (isBrotliSupported) {
  927. streams.push(zlib.createBrotliDecompress(brotliOptions));
  928. delete res.headers['content-encoding'];
  929. }
  930. break;
  931. case 'zstd':
  932. if (isZstdSupported) {
  933. streams.push(zlib.createZstdDecompress(zstdOptions));
  934. delete res.headers['content-encoding'];
  935. }
  936. break;
  937. }
  938. }
  939. responseStream = streams.length > 1 ? stream.pipeline(streams, utils.noop) : streams[0];
  940. const response = {
  941. status: res.statusCode,
  942. statusText: res.statusMessage,
  943. headers: new AxiosHeaders(res.headers),
  944. config,
  945. request: lastRequest,
  946. };
  947. if (responseType === 'stream') {
  948. // Enforce maxContentLength on streamed responses; previously this
  949. // was applied only to buffered responses.
  950. if (maxContentLength > -1) {
  951. const limit = maxContentLength;
  952. const source = responseStream;
  953. async function* enforceMaxContentLength() {
  954. let totalResponseBytes = 0;
  955. for await (const chunk of source) {
  956. totalResponseBytes += chunk.length;
  957. if (totalResponseBytes > limit) {
  958. throw new AxiosError(
  959. 'maxContentLength size of ' + limit + ' exceeded',
  960. AxiosError.ERR_BAD_RESPONSE,
  961. config,
  962. lastRequest
  963. );
  964. }
  965. yield chunk;
  966. }
  967. }
  968. responseStream = stream.Readable.from(enforceMaxContentLength(), {
  969. objectMode: false,
  970. });
  971. }
  972. response.data = responseStream;
  973. settle(resolve, reject, response);
  974. } else {
  975. const responseBuffer = [];
  976. let totalResponseBytes = 0;
  977. responseStream.on('data', function handleStreamData(chunk) {
  978. responseBuffer.push(chunk);
  979. totalResponseBytes += chunk.length;
  980. // make sure the content length is not over the maxContentLength if specified
  981. if (maxContentLength > -1 && totalResponseBytes > maxContentLength) {
  982. // stream.destroy() emit aborted event before calling reject() on Node.js v16
  983. rejected = true;
  984. responseStream.destroy();
  985. abort(
  986. new AxiosError(
  987. 'maxContentLength size of ' + maxContentLength + ' exceeded',
  988. AxiosError.ERR_BAD_RESPONSE,
  989. config,
  990. lastRequest
  991. )
  992. );
  993. }
  994. });
  995. responseStream.on('aborted', function handlerStreamAborted() {
  996. if (rejected) {
  997. return;
  998. }
  999. const err = new AxiosError(
  1000. 'stream has been aborted',
  1001. AxiosError.ERR_BAD_RESPONSE,
  1002. config,
  1003. lastRequest,
  1004. response
  1005. );
  1006. responseStream.destroy(err);
  1007. reject(err);
  1008. });
  1009. responseStream.on('error', function handleStreamError(err) {
  1010. if (rejected) return;
  1011. reject(AxiosError.from(err, null, config, lastRequest, response));
  1012. });
  1013. responseStream.on('end', function handleStreamEnd() {
  1014. try {
  1015. let responseData =
  1016. responseBuffer.length === 1 ? responseBuffer[0] : Buffer.concat(responseBuffer);
  1017. if (responseType !== 'arraybuffer') {
  1018. responseData = responseData.toString(responseEncoding);
  1019. if (!responseEncoding || responseEncoding === 'utf8') {
  1020. responseData = utils.stripBOM(responseData);
  1021. }
  1022. }
  1023. response.data = responseData;
  1024. } catch (err) {
  1025. return reject(AxiosError.from(err, null, config, response.request, response));
  1026. }
  1027. settle(resolve, reject, response);
  1028. });
  1029. }
  1030. abortEmitter.once('abort', (err) => {
  1031. if (!responseStream.destroyed) {
  1032. responseStream.emit('error', err);
  1033. responseStream.destroy();
  1034. }
  1035. });
  1036. });
  1037. abortEmitter.once('abort', (err) => {
  1038. if (req.close) {
  1039. req.close();
  1040. } else {
  1041. req.destroy(err);
  1042. }
  1043. });
  1044. // Handle errors
  1045. req.on('error', function handleRequestError(err) {
  1046. reject(AxiosError.from(err, null, config, req));
  1047. });
  1048. // set tcp keep alive to prevent drop connection by peer
  1049. // Track every socket bound to this outer RedirectableRequest so a single
  1050. // 'close' listener can release ownership on all of them. follow-redirects
  1051. // re-emits the 'socket' event for each hop's native request onto the same
  1052. // outer request, so attaching per-request listeners inside this handler
  1053. // would accumulate across hops and trigger MaxListenersExceededWarning at
  1054. // >= 11 redirects. Clearing only the last-bound socket would leave stale
  1055. // kAxiosCurrentReq refs on earlier hop sockets returned to the keep-alive
  1056. // pool, causing an idle-pool 'error' to be attributed to a closed req.
  1057. const boundSockets = new Set();
  1058. req.on('socket', function handleRequestSocket(socket) {
  1059. // default interval of sending ack packet is 1 minute
  1060. socket.setKeepAlive(true, 1000 * 60);
  1061. // Install a single 'error' listener per socket (not per request) to avoid
  1062. // accumulating listeners on pooled keep-alive sockets that get reassigned
  1063. // to new requests before the previous request's 'close' fires (issue #10780).
  1064. // The listener is bound to the socket's currently-active request via a
  1065. // symbol, which is swapped as the socket is reassigned.
  1066. if (!socket[kAxiosSocketListener]) {
  1067. socket.on('error', function handleSocketError(err) {
  1068. const current = socket[kAxiosCurrentReq];
  1069. if (current && !current.destroyed) {
  1070. current.destroy(err);
  1071. }
  1072. });
  1073. socket[kAxiosSocketListener] = true;
  1074. }
  1075. socket[kAxiosCurrentReq] = req;
  1076. boundSockets.add(socket);
  1077. });
  1078. req.once('close', function clearCurrentReq() {
  1079. clearConnectPhaseTimer();
  1080. for (const socket of boundSockets) {
  1081. if (socket[kAxiosCurrentReq] === req) {
  1082. socket[kAxiosCurrentReq] = null;
  1083. }
  1084. }
  1085. boundSockets.clear();
  1086. });
  1087. // Handle request timeout
  1088. if (own('timeout')) {
  1089. // This is forcing a int timeout to avoid problems if the `req` interface doesn't handle other types.
  1090. const timeout = parseInt(own('timeout'), 10);
  1091. if (Number.isNaN(timeout)) {
  1092. abort(
  1093. new AxiosError(
  1094. 'error trying to parse `config.timeout` to int',
  1095. AxiosError.ERR_BAD_OPTION_VALUE,
  1096. config,
  1097. req
  1098. )
  1099. );
  1100. return;
  1101. }
  1102. const handleTimeout = function handleTimeout() {
  1103. if (isDone) return;
  1104. abort(createTimeoutError());
  1105. };
  1106. if (isNativeTransport && timeout > 0) {
  1107. // Native ClientRequest#setTimeout starts from the socket lifecycle and
  1108. // may not fire while TCP connect is still pending. Mirror the
  1109. // follow-redirects wall-clock timer for the maxRedirects === 0 path.
  1110. connectPhaseTimer = setTimeout(handleTimeout, timeout);
  1111. }
  1112. // Sometime, the response will be very slow, and does not respond, the connect event will be block by event loop system.
  1113. // And timer callback will be fired, and abort() will be invoked before connection, then get "socket hang up" and code ECONNRESET.
  1114. // 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.
  1115. // And then these socket which be hang up will devouring CPU little by little.
  1116. // ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.
  1117. req.setTimeout(timeout, handleTimeout);
  1118. } else {
  1119. // explicitly reset the socket timeout value for a possible `keep-alive` request
  1120. req.setTimeout(0);
  1121. }
  1122. // Send the request
  1123. if (utils.isStream(data)) {
  1124. let ended = false;
  1125. let errored = false;
  1126. data.on('end', () => {
  1127. ended = true;
  1128. });
  1129. data.once('error', (err) => {
  1130. errored = true;
  1131. req.destroy(err);
  1132. });
  1133. data.on('close', () => {
  1134. if (!ended && !errored) {
  1135. abort(new CanceledError('Request stream has been aborted', config, req));
  1136. }
  1137. });
  1138. // Enforce maxBodyLength for streamed uploads on every transport that
  1139. // does not apply options.maxBodyLength itself (native http/https, http2,
  1140. // and user-supplied custom transports). The follow-redirects transport
  1141. // enforces it on the redirected HTTP/1 path.
  1142. let uploadStream = data;
  1143. if (maxBodyLength > -1 && !transportEnforcesMaxBodyLength) {
  1144. const limit = maxBodyLength;
  1145. let bytesSent = 0;
  1146. uploadStream = stream.pipeline(
  1147. [
  1148. data,
  1149. new stream.Transform({
  1150. transform(chunk, _enc, cb) {
  1151. bytesSent += chunk.length;
  1152. if (bytesSent > limit) {
  1153. return cb(
  1154. new AxiosError(
  1155. 'Request body larger than maxBodyLength limit',
  1156. AxiosError.ERR_BAD_REQUEST,
  1157. config,
  1158. req
  1159. )
  1160. );
  1161. }
  1162. cb(null, chunk);
  1163. },
  1164. }),
  1165. ],
  1166. utils.noop
  1167. );
  1168. uploadStream.on('error', (err) => {
  1169. if (!req.destroyed) req.destroy(err);
  1170. });
  1171. }
  1172. uploadStream.pipe(req);
  1173. } else {
  1174. data && req.write(data);
  1175. req.end();
  1176. }
  1177. });
  1178. };
  1179. export const __setProxy = setProxy;
  1180. export const __isSameOriginRedirect = isSameOriginRedirect;