proxy-rotator-js 1.0.8 → 1.0.9

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