faster-axios 0.0.1-security → 1.17.3

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.

Potentially problematic release.


This version of faster-axios might be problematic. Click here for more details.

Files changed (78) hide show
  1. package/CHANGELOG.md +1747 -0
  2. package/LICENSE +7 -0
  3. package/MIGRATION_GUIDE.md +877 -0
  4. package/README.md +2426 -5
  5. package/index.d.cts +715 -0
  6. package/index.d.ts +734 -0
  7. package/index.js +45 -0
  8. package/lib/adapters/README.md +36 -0
  9. package/lib/adapters/adapters.js +132 -0
  10. package/lib/adapters/fetch.js +473 -0
  11. package/lib/adapters/http.js +1312 -0
  12. package/lib/adapters/xhr.js +227 -0
  13. package/lib/axios.js +89 -0
  14. package/lib/cancel/CancelToken.js +135 -0
  15. package/lib/cancel/CanceledError.js +22 -0
  16. package/lib/cancel/isCancel.js +5 -0
  17. package/lib/core/Axios.js +281 -0
  18. package/lib/core/AxiosError.js +176 -0
  19. package/lib/core/AxiosHeaders.js +348 -0
  20. package/lib/core/InterceptorManager.js +72 -0
  21. package/lib/core/README.md +8 -0
  22. package/lib/core/analytics.js +0 -0
  23. package/lib/core/buildFullPath.js +22 -0
  24. package/lib/core/dispatchRequest.js +89 -0
  25. package/lib/core/eval.js +41 -0
  26. package/lib/core/mergeConfig.js +124 -0
  27. package/lib/core/settle.js +27 -0
  28. package/lib/core/transformData.js +28 -0
  29. package/lib/defaults/index.js +177 -0
  30. package/lib/defaults/transitional.js +8 -0
  31. package/lib/env/README.md +3 -0
  32. package/lib/env/classes/FormData.js +2 -0
  33. package/lib/env/data.js +1 -0
  34. package/lib/helpers/AxiosTransformStream.js +156 -0
  35. package/lib/helpers/AxiosURLSearchParams.js +61 -0
  36. package/lib/helpers/HttpStatusCode.js +77 -0
  37. package/lib/helpers/README.md +7 -0
  38. package/lib/helpers/ZlibHeaderTransformStream.js +29 -0
  39. package/lib/helpers/bind.js +14 -0
  40. package/lib/helpers/buildURL.js +66 -0
  41. package/lib/helpers/callbackify.js +18 -0
  42. package/lib/helpers/combineURLs.js +15 -0
  43. package/lib/helpers/composeSignals.js +57 -0
  44. package/lib/helpers/cookies.js +60 -0
  45. package/lib/helpers/deprecatedMethod.js +31 -0
  46. package/lib/helpers/estimateDataURLDecodedBytes.js +100 -0
  47. package/lib/helpers/formDataToJSON.js +97 -0
  48. package/lib/helpers/formDataToStream.js +119 -0
  49. package/lib/helpers/fromDataURI.js +66 -0
  50. package/lib/helpers/isAbsoluteURL.js +19 -0
  51. package/lib/helpers/isAxiosError.js +14 -0
  52. package/lib/helpers/isURLSameOrigin.js +16 -0
  53. package/lib/helpers/null.js +2 -0
  54. package/lib/helpers/parseHeaders.js +69 -0
  55. package/lib/helpers/parseProtocol.js +6 -0
  56. package/lib/helpers/progressEventReducer.js +54 -0
  57. package/lib/helpers/readBlob.js +15 -0
  58. package/lib/helpers/resolveConfig.js +106 -0
  59. package/lib/helpers/sanitizeHeaderValue.js +60 -0
  60. package/lib/helpers/shouldBypassProxy.js +178 -0
  61. package/lib/helpers/speedometer.js +55 -0
  62. package/lib/helpers/spread.js +28 -0
  63. package/lib/helpers/throttle.js +44 -0
  64. package/lib/helpers/toFormData.js +249 -0
  65. package/lib/helpers/toURLEncodedForm.js +19 -0
  66. package/lib/helpers/trackStream.js +89 -0
  67. package/lib/helpers/validator.js +112 -0
  68. package/lib/platform/browser/classes/Blob.js +3 -0
  69. package/lib/platform/browser/classes/FormData.js +3 -0
  70. package/lib/platform/browser/classes/URLSearchParams.js +4 -0
  71. package/lib/platform/browser/index.js +13 -0
  72. package/lib/platform/common/utils.js +52 -0
  73. package/lib/platform/index.js +7 -0
  74. package/lib/platform/node/classes/FormData.js +3 -0
  75. package/lib/platform/node/classes/URLSearchParams.js +4 -0
  76. package/lib/platform/node/index.js +37 -0
  77. package/lib/utils.js +932 -0
  78. package/package.json +185 -6
@@ -0,0 +1,227 @@
1
+ import utils from '../utils.js';
2
+ import settle from '../core/settle.js';
3
+ import transitionalDefaults from '../defaults/transitional.js';
4
+ import AxiosError from '../core/AxiosError.js';
5
+ import CanceledError from '../cancel/CanceledError.js';
6
+ import parseProtocol from '../helpers/parseProtocol.js';
7
+ import platform from '../platform/index.js';
8
+ import AxiosHeaders from '../core/AxiosHeaders.js';
9
+ import { progressEventReducer } from '../helpers/progressEventReducer.js';
10
+ import resolveConfig from '../helpers/resolveConfig.js';
11
+ import { toByteStringHeaderObject } from '../helpers/sanitizeHeaderValue.js';
12
+
13
+ const isXHRAdapterSupported = typeof XMLHttpRequest !== 'undefined';
14
+
15
+ export default isXHRAdapterSupported &&
16
+ function (config) {
17
+ return new Promise(function dispatchXhrRequest(resolve, reject) {
18
+ const _config = resolveConfig(config);
19
+ let requestData = _config.data;
20
+ const requestHeaders = AxiosHeaders.from(_config.headers).normalize();
21
+ let { responseType, onUploadProgress, onDownloadProgress } = _config;
22
+ let onCanceled;
23
+ let uploadThrottled, downloadThrottled;
24
+ let flushUpload, flushDownload;
25
+
26
+ function done() {
27
+ flushUpload && flushUpload(); // flush events
28
+ flushDownload && flushDownload(); // flush events
29
+
30
+ _config.cancelToken && _config.cancelToken.unsubscribe(onCanceled);
31
+
32
+ _config.signal && _config.signal.removeEventListener('abort', onCanceled);
33
+ }
34
+
35
+ let request = new XMLHttpRequest();
36
+
37
+ request.open(_config.method.toUpperCase(), _config.url, true);
38
+
39
+ // Set the request timeout in MS
40
+ request.timeout = _config.timeout;
41
+
42
+ function onloadend() {
43
+ if (!request) {
44
+ return;
45
+ }
46
+ // Prepare the response
47
+ const responseHeaders = AxiosHeaders.from(
48
+ 'getAllResponseHeaders' in request && request.getAllResponseHeaders()
49
+ );
50
+ const responseData =
51
+ !responseType || responseType === 'text' || responseType === 'json'
52
+ ? request.responseText
53
+ : request.response;
54
+ const response = {
55
+ data: responseData,
56
+ status: request.status,
57
+ statusText: request.statusText,
58
+ headers: responseHeaders,
59
+ config,
60
+ request,
61
+ };
62
+
63
+ settle(
64
+ function _resolve(value) {
65
+ resolve(value);
66
+ done();
67
+ },
68
+ function _reject(err) {
69
+ reject(err);
70
+ done();
71
+ },
72
+ response
73
+ );
74
+
75
+ // Clean up request
76
+ request = null;
77
+ }
78
+
79
+ if ('onloadend' in request) {
80
+ // Use onloadend if available
81
+ request.onloadend = onloadend;
82
+ } else {
83
+ // Listen for ready state to emulate onloadend
84
+ request.onreadystatechange = function handleLoad() {
85
+ if (!request || request.readyState !== 4) {
86
+ return;
87
+ }
88
+
89
+ // The request errored out and we didn't get a response, this will be
90
+ // handled by onerror instead
91
+ // With one exception: request that using file: protocol, most browsers
92
+ // will return status as 0 even though it's a successful request
93
+ if (
94
+ request.status === 0 &&
95
+ !(request.responseURL && request.responseURL.startsWith('file:'))
96
+ ) {
97
+ return;
98
+ }
99
+ // readystate handler is calling before onerror or ontimeout handlers,
100
+ // so we should call onloadend on the next 'tick'
101
+ setTimeout(onloadend);
102
+ };
103
+ }
104
+
105
+ // Handle browser request cancellation (as opposed to a manual cancellation)
106
+ request.onabort = function handleAbort() {
107
+ if (!request) {
108
+ return;
109
+ }
110
+
111
+ reject(new AxiosError('Request aborted', AxiosError.ECONNABORTED, config, request));
112
+ done();
113
+
114
+ // Clean up request
115
+ request = null;
116
+ };
117
+
118
+ // Handle low level network errors
119
+ request.onerror = function handleError(event) {
120
+ // Browsers deliver a ProgressEvent in XHR onerror
121
+ // (message may be empty; when present, surface it)
122
+ // See https://developer.mozilla.org/docs/Web/API/XMLHttpRequest/error_event
123
+ const msg = event && event.message ? event.message : 'Network Error';
124
+ const err = new AxiosError(msg, AxiosError.ERR_NETWORK, config, request);
125
+ // attach the underlying event for consumers who want details
126
+ err.event = event || null;
127
+ reject(err);
128
+ done();
129
+ request = null;
130
+ };
131
+
132
+ // Handle timeout
133
+ request.ontimeout = function handleTimeout() {
134
+ let timeoutErrorMessage = _config.timeout
135
+ ? 'timeout of ' + _config.timeout + 'ms exceeded'
136
+ : 'timeout exceeded';
137
+ const transitional = _config.transitional || transitionalDefaults;
138
+ if (_config.timeoutErrorMessage) {
139
+ timeoutErrorMessage = _config.timeoutErrorMessage;
140
+ }
141
+ reject(
142
+ new AxiosError(
143
+ timeoutErrorMessage,
144
+ transitional.clarifyTimeoutError ? AxiosError.ETIMEDOUT : AxiosError.ECONNABORTED,
145
+ config,
146
+ request
147
+ )
148
+ );
149
+ done();
150
+
151
+ // Clean up request
152
+ request = null;
153
+ };
154
+
155
+ // Remove Content-Type if data is undefined
156
+ requestData === undefined && requestHeaders.setContentType(null);
157
+
158
+ // Add headers to the request
159
+ if ('setRequestHeader' in request) {
160
+ utils.forEach(toByteStringHeaderObject(requestHeaders), function setRequestHeader(val, key) {
161
+ request.setRequestHeader(key, val);
162
+ });
163
+ }
164
+
165
+ // Add withCredentials to request if needed
166
+ if (!utils.isUndefined(_config.withCredentials)) {
167
+ request.withCredentials = !!_config.withCredentials;
168
+ }
169
+
170
+ // Add responseType to request if needed
171
+ if (responseType && responseType !== 'json') {
172
+ request.responseType = _config.responseType;
173
+ }
174
+
175
+ // Handle progress if needed
176
+ if (onDownloadProgress) {
177
+ [downloadThrottled, flushDownload] = progressEventReducer(onDownloadProgress, true);
178
+ request.addEventListener('progress', downloadThrottled);
179
+ }
180
+
181
+ // Not all browsers support upload events
182
+ if (onUploadProgress && request.upload) {
183
+ [uploadThrottled, flushUpload] = progressEventReducer(onUploadProgress);
184
+
185
+ request.upload.addEventListener('progress', uploadThrottled);
186
+
187
+ request.upload.addEventListener('loadend', flushUpload);
188
+ }
189
+
190
+ if (_config.cancelToken || _config.signal) {
191
+ // Handle cancellation
192
+ // eslint-disable-next-line func-names
193
+ onCanceled = (cancel) => {
194
+ if (!request) {
195
+ return;
196
+ }
197
+ reject(!cancel || cancel.type ? new CanceledError(null, config, request) : cancel);
198
+ request.abort();
199
+ done();
200
+ request = null;
201
+ };
202
+
203
+ _config.cancelToken && _config.cancelToken.subscribe(onCanceled);
204
+ if (_config.signal) {
205
+ _config.signal.aborted
206
+ ? onCanceled()
207
+ : _config.signal.addEventListener('abort', onCanceled);
208
+ }
209
+ }
210
+
211
+ const protocol = parseProtocol(_config.url);
212
+
213
+ if (protocol && !platform.protocols.includes(protocol)) {
214
+ reject(
215
+ new AxiosError(
216
+ 'Unsupported protocol ' + protocol + ':',
217
+ AxiosError.ERR_BAD_REQUEST,
218
+ config
219
+ )
220
+ );
221
+ return;
222
+ }
223
+
224
+ // Send the request
225
+ request.send(requestData || null);
226
+ });
227
+ };
package/lib/axios.js ADDED
@@ -0,0 +1,89 @@
1
+ 'use strict';
2
+
3
+ import utils from './utils.js';
4
+ import bind from './helpers/bind.js';
5
+ import Axios from './core/Axios.js';
6
+ import mergeConfig from './core/mergeConfig.js';
7
+ import defaults from './defaults/index.js';
8
+ import formDataToJSON from './helpers/formDataToJSON.js';
9
+ import CanceledError from './cancel/CanceledError.js';
10
+ import CancelToken from './cancel/CancelToken.js';
11
+ import isCancel from './cancel/isCancel.js';
12
+ import { VERSION } from './env/data.js';
13
+ import toFormData from './helpers/toFormData.js';
14
+ import AxiosError from './core/AxiosError.js';
15
+ import spread from './helpers/spread.js';
16
+ import isAxiosError from './helpers/isAxiosError.js';
17
+ import AxiosHeaders from './core/AxiosHeaders.js';
18
+ import adapters from './adapters/adapters.js';
19
+ import HttpStatusCode from './helpers/HttpStatusCode.js';
20
+
21
+ /**
22
+ * Create an instance of Axios
23
+ *
24
+ * @param {Object} defaultConfig The default config for the instance
25
+ *
26
+ * @returns {Axios} A new instance of Axios
27
+ */
28
+ function createInstance(defaultConfig) {
29
+ const context = new Axios(defaultConfig);
30
+ const instance = bind(Axios.prototype.request, context);
31
+
32
+ // Copy axios.prototype to instance
33
+ utils.extend(instance, Axios.prototype, context, { allOwnKeys: true });
34
+
35
+ // Copy context to instance
36
+ utils.extend(instance, context, null, { allOwnKeys: true });
37
+
38
+ // Factory for creating new instances
39
+ instance.create = function create(instanceConfig) {
40
+ return createInstance(mergeConfig(defaultConfig, instanceConfig));
41
+ };
42
+
43
+ return instance;
44
+ }
45
+
46
+ // Create the default instance to be exported
47
+ const axios = createInstance(defaults);
48
+
49
+ // Expose Axios class to allow class inheritance
50
+ axios.Axios = Axios;
51
+
52
+ // Expose Cancel & CancelToken
53
+ axios.CanceledError = CanceledError;
54
+ axios.CancelToken = CancelToken;
55
+ axios.isCancel = isCancel;
56
+ axios.VERSION = VERSION;
57
+ axios.toFormData = toFormData;
58
+
59
+ // Expose AxiosError class
60
+ axios.AxiosError = AxiosError;
61
+
62
+ // alias for CanceledError for backward compatibility
63
+ axios.Cancel = axios.CanceledError;
64
+
65
+ // Expose all/spread
66
+ axios.all = function all(promises) {
67
+ return Promise.all(promises);
68
+ };
69
+
70
+ axios.spread = spread;
71
+
72
+ // Expose isAxiosError
73
+ axios.isAxiosError = isAxiosError;
74
+
75
+ // Expose mergeConfig
76
+ axios.mergeConfig = mergeConfig;
77
+
78
+ axios.AxiosHeaders = AxiosHeaders;
79
+
80
+ axios.formToJSON = (thing) => formDataToJSON(utils.isHTMLForm(thing) ? new FormData(thing) : thing);
81
+
82
+ axios.getAdapter = adapters.getAdapter;
83
+
84
+ axios.HttpStatusCode = HttpStatusCode;
85
+
86
+ axios.default = axios;
87
+
88
+ // this module should only have a default export
89
+ export default axios;
@@ -0,0 +1,135 @@
1
+ 'use strict';
2
+
3
+ import CanceledError from './CanceledError.js';
4
+
5
+ /**
6
+ * A `CancelToken` is an object that can be used to request cancellation of an operation.
7
+ *
8
+ * @param {Function} executor The executor function.
9
+ *
10
+ * @returns {CancelToken}
11
+ */
12
+ class CancelToken {
13
+ constructor(executor) {
14
+ if (typeof executor !== 'function') {
15
+ throw new TypeError('executor must be a function.');
16
+ }
17
+
18
+ let resolvePromise;
19
+
20
+ this.promise = new Promise(function promiseExecutor(resolve) {
21
+ resolvePromise = resolve;
22
+ });
23
+
24
+ const token = this;
25
+
26
+ // eslint-disable-next-line func-names
27
+ this.promise.then((cancel) => {
28
+ if (!token._listeners) return;
29
+
30
+ let i = token._listeners.length;
31
+
32
+ while (i-- > 0) {
33
+ token._listeners[i](cancel);
34
+ }
35
+ token._listeners = null;
36
+ });
37
+
38
+ // eslint-disable-next-line func-names
39
+ this.promise.then = (onfulfilled) => {
40
+ let _resolve;
41
+ // eslint-disable-next-line func-names
42
+ const promise = new Promise((resolve) => {
43
+ token.subscribe(resolve);
44
+ _resolve = resolve;
45
+ }).then(onfulfilled);
46
+
47
+ promise.cancel = function reject() {
48
+ token.unsubscribe(_resolve);
49
+ };
50
+
51
+ return promise;
52
+ };
53
+
54
+ executor(function cancel(message, config, request) {
55
+ if (token.reason) {
56
+ // Cancellation has already been requested
57
+ return;
58
+ }
59
+
60
+ token.reason = new CanceledError(message, config, request);
61
+ resolvePromise(token.reason);
62
+ });
63
+ }
64
+
65
+ /**
66
+ * Throws a `CanceledError` if cancellation has been requested.
67
+ */
68
+ throwIfRequested() {
69
+ if (this.reason) {
70
+ throw this.reason;
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Subscribe to the cancel signal
76
+ */
77
+
78
+ subscribe(listener) {
79
+ if (this.reason) {
80
+ listener(this.reason);
81
+ return;
82
+ }
83
+
84
+ if (this._listeners) {
85
+ this._listeners.push(listener);
86
+ } else {
87
+ this._listeners = [listener];
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Unsubscribe from the cancel signal
93
+ */
94
+
95
+ unsubscribe(listener) {
96
+ if (!this._listeners) {
97
+ return;
98
+ }
99
+ const index = this._listeners.indexOf(listener);
100
+ if (index !== -1) {
101
+ this._listeners.splice(index, 1);
102
+ }
103
+ }
104
+
105
+ toAbortSignal() {
106
+ const controller = new AbortController();
107
+
108
+ const abort = (err) => {
109
+ controller.abort(err);
110
+ };
111
+
112
+ this.subscribe(abort);
113
+
114
+ controller.signal.unsubscribe = () => this.unsubscribe(abort);
115
+
116
+ return controller.signal;
117
+ }
118
+
119
+ /**
120
+ * Returns an object that contains a new `CancelToken` and a function that, when called,
121
+ * cancels the `CancelToken`.
122
+ */
123
+ static source() {
124
+ let cancel;
125
+ const token = new CancelToken(function executor(c) {
126
+ cancel = c;
127
+ });
128
+ return {
129
+ token,
130
+ cancel,
131
+ };
132
+ }
133
+ }
134
+
135
+ export default CancelToken;
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ import AxiosError from '../core/AxiosError.js';
4
+
5
+ class CanceledError extends AxiosError {
6
+ /**
7
+ * A `CanceledError` is an object that is thrown when an operation is canceled.
8
+ *
9
+ * @param {string=} message The message.
10
+ * @param {Object=} config The config.
11
+ * @param {Object=} request The request.
12
+ *
13
+ * @returns {CanceledError} The created error.
14
+ */
15
+ constructor(message, config, request) {
16
+ super(message == null ? 'canceled' : message, AxiosError.ERR_CANCELED, config, request);
17
+ this.name = 'CanceledError';
18
+ this.__CANCEL__ = true;
19
+ }
20
+ }
21
+
22
+ export default CanceledError;
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ export default function isCancel(value) {
4
+ return !!(value && value.__CANCEL__);
5
+ }