axios 1.6.7 → 1.7.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 axios might be problematic. Click here for more details.

@@ -2,8 +2,6 @@
2
2
 
3
3
  import stream from 'stream';
4
4
  import utils from '../utils.js';
5
- import throttle from './throttle.js';
6
- import speedometer from './speedometer.js';
7
5
 
8
6
  const kInternals = Symbol('internals');
9
7
 
@@ -24,12 +22,8 @@ class AxiosTransformStream extends stream.Transform{
24
22
  readableHighWaterMark: options.chunkSize
25
23
  });
26
24
 
27
- const self = this;
28
-
29
25
  const internals = this[kInternals] = {
30
- length: options.length,
31
26
  timeWindow: options.timeWindow,
32
- ticksRate: options.ticksRate,
33
27
  chunkSize: options.chunkSize,
34
28
  maxRate: options.maxRate,
35
29
  minChunkSize: options.minChunkSize,
@@ -41,8 +35,6 @@ class AxiosTransformStream extends stream.Transform{
41
35
  onReadCallback: null
42
36
  };
43
37
 
44
- const _speedometer = speedometer(internals.ticksRate * options.samplesCount, internals.timeWindow);
45
-
46
38
  this.on('newListener', event => {
47
39
  if (event === 'progress') {
48
40
  if (!internals.isCaptured) {
@@ -50,38 +42,6 @@ class AxiosTransformStream extends stream.Transform{
50
42
  }
51
43
  }
52
44
  });
53
-
54
- let bytesNotified = 0;
55
-
56
- internals.updateProgress = throttle(function throttledHandler() {
57
- const totalBytes = internals.length;
58
- const bytesTransferred = internals.bytesSeen;
59
- const progressBytes = bytesTransferred - bytesNotified;
60
- if (!progressBytes || self.destroyed) return;
61
-
62
- const rate = _speedometer(progressBytes);
63
-
64
- bytesNotified = bytesTransferred;
65
-
66
- process.nextTick(() => {
67
- self.emit('progress', {
68
- 'loaded': bytesTransferred,
69
- 'total': totalBytes,
70
- 'progress': totalBytes ? (bytesTransferred / totalBytes) : undefined,
71
- 'bytes': progressBytes,
72
- 'rate': rate ? rate : undefined,
73
- 'estimated': rate && totalBytes && bytesTransferred <= totalBytes ?
74
- (totalBytes - bytesTransferred) / rate : undefined
75
- });
76
- });
77
- }, internals.ticksRate);
78
-
79
- const onFinish = () => {
80
- internals.updateProgress(true);
81
- };
82
-
83
- this.once('end', onFinish);
84
- this.once('error', onFinish);
85
45
  }
86
46
 
87
47
  _read(size) {
@@ -95,7 +55,6 @@ class AxiosTransformStream extends stream.Transform{
95
55
  }
96
56
 
97
57
  _transform(chunk, encoding, callback) {
98
- const self = this;
99
58
  const internals = this[kInternals];
100
59
  const maxRate = internals.maxRate;
101
60
 
@@ -107,16 +66,14 @@ class AxiosTransformStream extends stream.Transform{
107
66
  const bytesThreshold = (maxRate / divider);
108
67
  const minChunkSize = internals.minChunkSize !== false ? Math.max(internals.minChunkSize, bytesThreshold * 0.01) : 0;
109
68
 
110
- function pushChunk(_chunk, _callback) {
69
+ const pushChunk = (_chunk, _callback) => {
111
70
  const bytes = Buffer.byteLength(_chunk);
112
71
  internals.bytesSeen += bytes;
113
72
  internals.bytes += bytes;
114
73
 
115
- if (internals.isCaptured) {
116
- internals.updateProgress();
117
- }
74
+ internals.isCaptured && this.emit('progress', internals.bytesSeen);
118
75
 
119
- if (self.push(_chunk)) {
76
+ if (this.push(_chunk)) {
120
77
  process.nextTick(_callback);
121
78
  } else {
122
79
  internals.onReadCallback = () => {
@@ -181,11 +138,6 @@ class AxiosTransformStream extends stream.Transform{
181
138
  }
182
139
  });
183
140
  }
184
-
185
- setLength(length) {
186
- this[kInternals].length = +length;
187
- return this;
188
- }
189
141
  }
190
142
 
191
143
  export default AxiosTransformStream;
@@ -0,0 +1,46 @@
1
+ import CanceledError from "../cancel/CanceledError.js";
2
+ import AxiosError from "../core/AxiosError.js";
3
+
4
+ const composeSignals = (signals, timeout) => {
5
+ let controller = new AbortController();
6
+
7
+ let aborted;
8
+
9
+ const onabort = function (cancel) {
10
+ if (!aborted) {
11
+ aborted = true;
12
+ unsubscribe();
13
+ const err = cancel instanceof Error ? cancel : this.reason;
14
+ controller.abort(err instanceof AxiosError ? err : new CanceledError(err instanceof Error ? err.message : err));
15
+ }
16
+ }
17
+
18
+ let timer = timeout && setTimeout(() => {
19
+ onabort(new AxiosError(`timeout ${timeout} of ms exceeded`, AxiosError.ETIMEDOUT))
20
+ }, timeout)
21
+
22
+ const unsubscribe = () => {
23
+ if (signals) {
24
+ timer && clearTimeout(timer);
25
+ timer = null;
26
+ signals.forEach(signal => {
27
+ signal &&
28
+ (signal.removeEventListener ? signal.removeEventListener('abort', onabort) : signal.unsubscribe(onabort));
29
+ });
30
+ signals = null;
31
+ }
32
+ }
33
+
34
+ signals.forEach((signal) => signal && signal.addEventListener && signal.addEventListener('abort', onabort));
35
+
36
+ const {signal} = controller;
37
+
38
+ signal.unsubscribe = unsubscribe;
39
+
40
+ return [signal, () => {
41
+ timer && clearTimeout(timer);
42
+ timer = null;
43
+ }];
44
+ }
45
+
46
+ export default composeSignals;
@@ -0,0 +1,44 @@
1
+ import speedometer from "./speedometer.js";
2
+ import throttle from "./throttle.js";
3
+ import utils from "../utils.js";
4
+
5
+ export const progressEventReducer = (listener, isDownloadStream, freq = 3) => {
6
+ let bytesNotified = 0;
7
+ const _speedometer = speedometer(50, 250);
8
+
9
+ return throttle(e => {
10
+ const loaded = e.loaded;
11
+ const total = e.lengthComputable ? e.total : undefined;
12
+ const progressBytes = loaded - bytesNotified;
13
+ const rate = _speedometer(progressBytes);
14
+ const inRange = loaded <= total;
15
+
16
+ bytesNotified = loaded;
17
+
18
+ const data = {
19
+ loaded,
20
+ total,
21
+ progress: total ? (loaded / total) : undefined,
22
+ bytes: progressBytes,
23
+ rate: rate ? rate : undefined,
24
+ estimated: rate && total && inRange ? (total - loaded) / rate : undefined,
25
+ event: e,
26
+ lengthComputable: total != null,
27
+ [isDownloadStream ? 'download' : 'upload']: true
28
+ };
29
+
30
+ listener(data);
31
+ }, freq);
32
+ }
33
+
34
+ export const progressEventDecorator = (total, throttled) => {
35
+ const lengthComputable = total != null;
36
+
37
+ return [(loaded) => throttled[0]({
38
+ lengthComputable,
39
+ total,
40
+ loaded
41
+ }), throttled[1]];
42
+ }
43
+
44
+ export const asyncDecorator = (fn) => (...args) => utils.asap(() => fn(...args));
@@ -0,0 +1,57 @@
1
+ import platform from "../platform/index.js";
2
+ import utils from "../utils.js";
3
+ import isURLSameOrigin from "./isURLSameOrigin.js";
4
+ import cookies from "./cookies.js";
5
+ import buildFullPath from "../core/buildFullPath.js";
6
+ import mergeConfig from "../core/mergeConfig.js";
7
+ import AxiosHeaders from "../core/AxiosHeaders.js";
8
+ import buildURL from "./buildURL.js";
9
+
10
+ export default (config) => {
11
+ const newConfig = mergeConfig({}, config);
12
+
13
+ let {data, withXSRFToken, xsrfHeaderName, xsrfCookieName, headers, auth} = newConfig;
14
+
15
+ newConfig.headers = headers = AxiosHeaders.from(headers);
16
+
17
+ newConfig.url = buildURL(buildFullPath(newConfig.baseURL, newConfig.url), config.params, config.paramsSerializer);
18
+
19
+ // HTTP basic authentication
20
+ if (auth) {
21
+ headers.set('Authorization', 'Basic ' +
22
+ btoa((auth.username || '') + ':' + (auth.password ? unescape(encodeURIComponent(auth.password)) : ''))
23
+ );
24
+ }
25
+
26
+ let contentType;
27
+
28
+ if (utils.isFormData(data)) {
29
+ if (platform.hasStandardBrowserEnv || platform.hasStandardBrowserWebWorkerEnv) {
30
+ headers.setContentType(undefined); // Let the browser set it
31
+ } else if ((contentType = headers.getContentType()) !== false) {
32
+ // fix semicolon duplication issue for ReactNative FormData implementation
33
+ const [type, ...tokens] = contentType ? contentType.split(';').map(token => token.trim()).filter(Boolean) : [];
34
+ headers.setContentType([type || 'multipart/form-data', ...tokens].join('; '));
35
+ }
36
+ }
37
+
38
+ // Add xsrf header
39
+ // This is only done if running in a standard browser environment.
40
+ // Specifically not if we're in a web worker, or react-native.
41
+
42
+ if (platform.hasStandardBrowserEnv) {
43
+ withXSRFToken && utils.isFunction(withXSRFToken) && (withXSRFToken = withXSRFToken(newConfig));
44
+
45
+ if (withXSRFToken || (withXSRFToken !== false && isURLSameOrigin(newConfig.url))) {
46
+ // Add xsrf header
47
+ const xsrfValue = xsrfHeaderName && xsrfCookieName && cookies.read(xsrfCookieName);
48
+
49
+ if (xsrfValue) {
50
+ headers.set(xsrfHeaderName, xsrfValue);
51
+ }
52
+ }
53
+ }
54
+
55
+ return newConfig;
56
+ }
57
+
@@ -1,5 +1,3 @@
1
- 'use strict';
2
-
3
1
  /**
4
2
  * Throttle decorator
5
3
  * @param {Function} fn
@@ -8,26 +6,39 @@
8
6
  */
9
7
  function throttle(fn, freq) {
10
8
  let timestamp = 0;
11
- const threshold = 1000 / freq;
12
- let timer = null;
13
- return function throttled(force, args) {
9
+ let threshold = 1000 / freq;
10
+ let lastArgs;
11
+ let timer;
12
+
13
+ const invoke = (args, now = Date.now()) => {
14
+ timestamp = now;
15
+ lastArgs = null;
16
+ if (timer) {
17
+ clearTimeout(timer);
18
+ timer = null;
19
+ }
20
+ fn.apply(null, args);
21
+ }
22
+
23
+ const throttled = (...args) => {
14
24
  const now = Date.now();
15
- if (force || now - timestamp > threshold) {
16
- if (timer) {
17
- clearTimeout(timer);
18
- timer = null;
25
+ const passed = now - timestamp;
26
+ if ( passed >= threshold) {
27
+ invoke(args, now);
28
+ } else {
29
+ lastArgs = args;
30
+ if (!timer) {
31
+ timer = setTimeout(() => {
32
+ timer = null;
33
+ invoke(lastArgs)
34
+ }, threshold - passed);
19
35
  }
20
- timestamp = now;
21
- return fn.apply(null, args);
22
36
  }
23
- if (!timer) {
24
- timer = setTimeout(() => {
25
- timer = null;
26
- timestamp = Date.now();
27
- return fn.apply(null, args);
28
- }, threshold - (now - timestamp));
29
- }
30
- };
37
+ }
38
+
39
+ const flush = () => lastArgs && invoke(lastArgs);
40
+
41
+ return [throttled, flush];
31
42
  }
32
43
 
33
44
  export default throttle;
@@ -0,0 +1,67 @@
1
+
2
+ export const streamChunk = function* (chunk, chunkSize) {
3
+ let len = chunk.byteLength;
4
+
5
+ if (!chunkSize || len < chunkSize) {
6
+ yield chunk;
7
+ return;
8
+ }
9
+
10
+ let pos = 0;
11
+ let end;
12
+
13
+ while (pos < len) {
14
+ end = pos + chunkSize;
15
+ yield chunk.slice(pos, end);
16
+ pos = end;
17
+ }
18
+ }
19
+
20
+ export const readBytes = async function* (iterable, chunkSize, encode) {
21
+ for await (const chunk of iterable) {
22
+ yield* streamChunk(ArrayBuffer.isView(chunk) ? chunk : (await encode(String(chunk))), chunkSize);
23
+ }
24
+ }
25
+
26
+ export const trackStream = (stream, chunkSize, onProgress, onFinish, encode) => {
27
+ const iterator = readBytes(stream, chunkSize, encode);
28
+
29
+ let bytes = 0;
30
+ let done;
31
+ let _onFinish = (e) => {
32
+ if (!done) {
33
+ done = true;
34
+ onFinish && onFinish(e);
35
+ }
36
+ }
37
+
38
+ return new ReadableStream({
39
+ async pull(controller) {
40
+ try {
41
+ const {done, value} = await iterator.next();
42
+
43
+ if (done) {
44
+ _onFinish();
45
+ controller.close();
46
+ return;
47
+ }
48
+
49
+ let len = value.byteLength;
50
+ if (onProgress) {
51
+ let loadedBytes = bytes += len;
52
+ onProgress(loadedBytes);
53
+ }
54
+ controller.enqueue(new Uint8Array(value));
55
+ } catch (err) {
56
+ _onFinish(err);
57
+ throw err;
58
+ }
59
+ },
60
+ cancel(reason) {
61
+ _onFinish(reason);
62
+ return iterator.return();
63
+ }
64
+ }, {
65
+ highWaterMark: 2
66
+ })
67
+ }
@@ -40,8 +40,11 @@ const hasStandardBrowserWebWorkerEnv = (() => {
40
40
  );
41
41
  })();
42
42
 
43
+ const origin = hasBrowserEnv && window.location.href || 'http://localhost';
44
+
43
45
  export {
44
46
  hasBrowserEnv,
45
47
  hasStandardBrowserWebWorkerEnv,
46
- hasStandardBrowserEnv
48
+ hasStandardBrowserEnv,
49
+ origin
47
50
  }
package/lib/utils.js CHANGED
@@ -209,6 +209,8 @@ const isFormData = (thing) => {
209
209
  */
210
210
  const isURLSearchParams = kindOfTest('URLSearchParams');
211
211
 
212
+ const [isReadableStream, isRequest, isResponse, isHeaders] = ['ReadableStream', 'Request', 'Response', 'Headers'].map(kindOfTest);
213
+
212
214
  /**
213
215
  * Trim excess whitespace off the beginning and end of a string
214
216
  *
@@ -597,8 +599,7 @@ const toObjectSet = (arrayOrString, delimiter) => {
597
599
  const noop = () => {}
598
600
 
599
601
  const toFiniteNumber = (value, defaultValue) => {
600
- value = +value;
601
- return Number.isFinite(value) ? value : defaultValue;
602
+ return value != null && Number.isFinite(value = +value) ? value : defaultValue;
602
603
  }
603
604
 
604
605
  const ALPHA = 'abcdefghijklmnopqrstuvwxyz'
@@ -668,6 +669,36 @@ const isAsyncFn = kindOfTest('AsyncFunction');
668
669
  const isThenable = (thing) =>
669
670
  thing && (isObject(thing) || isFunction(thing)) && isFunction(thing.then) && isFunction(thing.catch);
670
671
 
672
+ // original code
673
+ // https://github.com/DigitalBrainJS/AxiosPromise/blob/16deab13710ec09779922131f3fa5954320f83ab/lib/utils.js#L11-L34
674
+
675
+ const _setImmediate = ((setImmediateSupported, postMessageSupported) => {
676
+ if (setImmediateSupported) {
677
+ return setImmediate;
678
+ }
679
+
680
+ return postMessageSupported ? ((token, callbacks) => {
681
+ _global.addEventListener("message", ({source, data}) => {
682
+ if (source === _global && data === token) {
683
+ callbacks.length && callbacks.shift()();
684
+ }
685
+ }, false);
686
+
687
+ return (cb) => {
688
+ callbacks.push(cb);
689
+ _global.postMessage(token, "*");
690
+ }
691
+ })(`axios@${Math.random()}`, []) : (cb) => setTimeout(cb);
692
+ })(
693
+ typeof setImmediate === 'function',
694
+ isFunction(_global.postMessage)
695
+ );
696
+
697
+ const asap = typeof queueMicrotask !== 'undefined' ?
698
+ queueMicrotask.bind(_global) : ( typeof process !== 'undefined' && process.nextTick || _setImmediate);
699
+
700
+ // *********************
701
+
671
702
  export default {
672
703
  isArray,
673
704
  isArrayBuffer,
@@ -679,6 +710,10 @@ export default {
679
710
  isBoolean,
680
711
  isObject,
681
712
  isPlainObject,
713
+ isReadableStream,
714
+ isRequest,
715
+ isResponse,
716
+ isHeaders,
682
717
  isUndefined,
683
718
  isDate,
684
719
  isFile,
@@ -719,5 +754,7 @@ export default {
719
754
  isSpecCompliantForm,
720
755
  toJSONObject,
721
756
  isAsyncFn,
722
- isThenable
757
+ isThenable,
758
+ setImmediate: _setImmediate,
759
+ asap
723
760
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "axios",
3
- "version": "1.6.7",
3
+ "version": "1.7.3",
4
4
  "description": "Promise based HTTP client for the browser and node.js",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -80,41 +80,41 @@
80
80
  },
81
81
  "homepage": "https://axios-http.com",
82
82
  "devDependencies": {
83
- "@babel/core": "^7.18.2",
84
- "@babel/preset-env": "^7.18.2",
85
- "@commitlint/cli": "^17.3.0",
86
- "@commitlint/config-conventional": "^17.3.0",
83
+ "@babel/core": "^7.23.9",
84
+ "@babel/preset-env": "^7.23.9",
85
+ "@commitlint/cli": "^17.8.1",
86
+ "@commitlint/config-conventional": "^17.8.1",
87
87
  "@release-it/conventional-changelog": "^5.1.1",
88
88
  "@rollup/plugin-babel": "^5.3.1",
89
89
  "@rollup/plugin-commonjs": "^15.1.0",
90
90
  "@rollup/plugin-json": "^4.1.0",
91
- "@rollup/plugin-multi-entry": "^4.0.0",
91
+ "@rollup/plugin-multi-entry": "^4.1.0",
92
92
  "@rollup/plugin-node-resolve": "^9.0.0",
93
- "abortcontroller-polyfill": "^1.7.3",
93
+ "abortcontroller-polyfill": "^1.7.5",
94
94
  "auto-changelog": "^2.4.0",
95
- "body-parser": "^1.20.0",
96
- "chalk": "^5.2.0",
95
+ "body-parser": "^1.20.2",
96
+ "chalk": "^5.3.0",
97
97
  "coveralls": "^3.1.1",
98
98
  "cross-env": "^7.0.3",
99
99
  "dev-null": "^0.1.1",
100
100
  "dtslint": "^4.2.1",
101
101
  "es6-promise": "^4.2.8",
102
- "eslint": "^8.17.0",
103
- "express": "^4.18.1",
104
- "formdata-node": "^5.0.0",
105
- "formidable": "^2.0.1",
102
+ "eslint": "^8.56.0",
103
+ "express": "^4.18.2",
104
+ "formdata-node": "^5.0.1",
105
+ "formidable": "^2.1.2",
106
106
  "fs-extra": "^10.1.0",
107
107
  "get-stream": "^3.0.0",
108
108
  "gulp": "^4.0.2",
109
109
  "gzip-size": "^7.0.0",
110
- "handlebars": "^4.7.7",
111
- "husky": "^8.0.2",
110
+ "handlebars": "^4.7.8",
111
+ "husky": "^8.0.3",
112
112
  "istanbul-instrumenter-loader": "^3.0.1",
113
- "jasmine-core": "^2.4.1",
113
+ "jasmine-core": "^2.99.1",
114
114
  "karma": "^6.3.17",
115
- "karma-chrome-launcher": "^3.1.1",
115
+ "karma-chrome-launcher": "^3.2.0",
116
116
  "karma-firefox-launcher": "^2.1.2",
117
- "karma-jasmine": "^1.1.1",
117
+ "karma-jasmine": "^1.1.2",
118
118
  "karma-jasmine-ajax": "^0.1.13",
119
119
  "karma-rollup-preprocessor": "^7.0.8",
120
120
  "karma-safari-launcher": "^1.0.0",
@@ -122,12 +122,12 @@
122
122
  "karma-sinon": "^1.0.5",
123
123
  "karma-sourcemap-loader": "^0.3.8",
124
124
  "memoizee": "^0.4.15",
125
- "minimist": "^1.2.7",
126
- "mocha": "^10.0.0",
125
+ "minimist": "^1.2.8",
126
+ "mocha": "^10.3.0",
127
127
  "multer": "^1.4.4",
128
- "pretty-bytes": "^6.0.0",
129
- "release-it": "^15.5.1",
130
- "rollup": "^2.67.0",
128
+ "pretty-bytes": "^6.1.1",
129
+ "release-it": "^15.11.0",
130
+ "rollup": "^2.79.1",
131
131
  "rollup-plugin-auto-external": "^2.0.0",
132
132
  "rollup-plugin-bundle-size": "^1.0.3",
133
133
  "rollup-plugin-terser": "^7.0.2",
@@ -135,7 +135,8 @@
135
135
  "stream-throttle": "^0.1.3",
136
136
  "string-replace-async": "^3.0.2",
137
137
  "terser-webpack-plugin": "^4.2.3",
138
- "typescript": "^4.8.4"
138
+ "typescript": "^4.9.5",
139
+ "@rollup/plugin-alias": "^5.1.0"
139
140
  },
140
141
  "browser": {
141
142
  "./lib/adapters/http.js": "./lib/helpers/null.js",
@@ -146,7 +147,7 @@
146
147
  "unpkg": "dist/axios.min.js",
147
148
  "typings": "./index.d.ts",
148
149
  "dependencies": {
149
- "follow-redirects": "^1.15.4",
150
+ "follow-redirects": "^1.15.6",
150
151
  "form-data": "^4.0.0",
151
152
  "proxy-from-env": "^1.1.0"
152
153
  },
@@ -159,8 +160,8 @@
159
160
  "contributors": [
160
161
  "Matt Zabriskie (https://github.com/mzabriskie)",
161
162
  "Nick Uraltsev (https://github.com/nickuraltsev)",
162
- "Jay (https://github.com/jasonsaayman)",
163
163
  "Dmitriy Mozgovoy (https://github.com/DigitalBrainJS)",
164
+ "Jay (https://github.com/jasonsaayman)",
164
165
  "Emily Morehouse (https://github.com/emilyemorehouse)",
165
166
  "Rubén Norte (https://github.com/rubennorte)",
166
167
  "Justin Beckwith (https://github.com/JustinBeckwith)",