axios 1.7.4 → 1.7.6

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.

@@ -69,7 +69,11 @@ const getBodyLength = async (body) => {
69
69
  }
70
70
 
71
71
  if(utils.isSpecCompliantForm(body)) {
72
- return (await new Request(body).arrayBuffer()).byteLength;
72
+ const _request = new Request(platform.origin, {
73
+ method: 'POST',
74
+ body,
75
+ });
76
+ return (await _request.arrayBuffer()).byteLength;
73
77
  }
74
78
 
75
79
  if(utils.isArrayBufferView(body) || utils.isArrayBuffer(body)) {
@@ -109,18 +113,13 @@ export default isFetchSupported && (async (config) => {
109
113
 
110
114
  responseType = responseType ? (responseType + '').toLowerCase() : 'text';
111
115
 
112
- let [composedSignal, stopTimeout] = (signal || cancelToken || timeout) ?
113
- composeSignals([signal, cancelToken], timeout) : [];
114
-
115
- let finished, request;
116
+ let composedSignal = composeSignals([signal, cancelToken && cancelToken.toAbortSignal()], timeout);
116
117
 
117
- const onFinish = () => {
118
- !finished && setTimeout(() => {
119
- composedSignal && composedSignal.unsubscribe();
120
- });
118
+ let request;
121
119
 
122
- finished = true;
123
- }
120
+ const unsubscribe = composedSignal && composedSignal.unsubscribe && (() => {
121
+ composedSignal.unsubscribe();
122
+ });
124
123
 
125
124
  let requestContentLength;
126
125
 
@@ -155,6 +154,9 @@ export default isFetchSupported && (async (config) => {
155
154
  withCredentials = withCredentials ? 'include' : 'omit';
156
155
  }
157
156
 
157
+ // Cloudflare Workers throws when credentials are defined
158
+ // see https://github.com/cloudflare/workerd/issues/902
159
+ const isCredentialsSupported = "credentials" in Request.prototype;
158
160
  request = new Request(url, {
159
161
  ...fetchOptions,
160
162
  signal: composedSignal,
@@ -162,14 +164,14 @@ export default isFetchSupported && (async (config) => {
162
164
  headers: headers.normalize().toJSON(),
163
165
  body: data,
164
166
  duplex: "half",
165
- credentials: withCredentials
167
+ credentials: isCredentialsSupported ? withCredentials : undefined
166
168
  });
167
169
 
168
170
  let response = await fetch(request);
169
171
 
170
172
  const isStreamResponse = supportsResponseStream && (responseType === 'stream' || responseType === 'response');
171
173
 
172
- if (supportsResponseStream && (onDownloadProgress || isStreamResponse)) {
174
+ if (supportsResponseStream && (onDownloadProgress || (isStreamResponse && unsubscribe))) {
173
175
  const options = {};
174
176
 
175
177
  ['status', 'statusText', 'headers'].forEach(prop => {
@@ -186,7 +188,7 @@ export default isFetchSupported && (async (config) => {
186
188
  response = new Response(
187
189
  trackStream(response.body, DEFAULT_CHUNK_SIZE, onProgress, () => {
188
190
  flush && flush();
189
- isStreamResponse && onFinish();
191
+ unsubscribe && unsubscribe();
190
192
  }, encodeText),
191
193
  options
192
194
  );
@@ -196,9 +198,7 @@ export default isFetchSupported && (async (config) => {
196
198
 
197
199
  let responseData = await resolvers[utils.findKey(resolvers, responseType) || 'text'](response, config);
198
200
 
199
- !isStreamResponse && onFinish();
200
-
201
- stopTimeout && stopTimeout();
201
+ !isStreamResponse && unsubscribe && unsubscribe();
202
202
 
203
203
  return await new Promise((resolve, reject) => {
204
204
  settle(resolve, reject, {
@@ -211,7 +211,7 @@ export default isFetchSupported && (async (config) => {
211
211
  })
212
212
  })
213
213
  } catch (err) {
214
- onFinish();
214
+ unsubscribe && unsubscribe();
215
215
 
216
216
  if (err && err.name === 'TypeError' && /fetch/i.test(err.message)) {
217
217
  throw Object.assign(
@@ -229,7 +229,7 @@ export default isHttpAdapterSupported && function httpAdapter(config) {
229
229
 
230
230
  // Parse url
231
231
  const fullPath = buildFullPath(config.baseURL, config.url);
232
- const parsed = new URL(fullPath, utils.hasBrowserEnv ? platform.origin : undefined);
232
+ const parsed = new URL(fullPath, platform.hasBrowserEnv ? platform.origin : undefined);
233
233
  const protocol = parsed.protocol || supportedProtocols[0];
234
234
 
235
235
  if (protocol === 'data:') {
@@ -102,6 +102,20 @@ class CancelToken {
102
102
  }
103
103
  }
104
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
+
105
119
  /**
106
120
  * Returns an object that contains a new `CancelToken` and a function that, when called,
107
121
  * cancels the `CancelToken`.
@@ -27,7 +27,10 @@ function AxiosError(message, code, config, request, response) {
27
27
  code && (this.code = code);
28
28
  config && (this.config = config);
29
29
  request && (this.request = request);
30
- response && (this.response = response);
30
+ if (response) {
31
+ this.response = response;
32
+ this.status = response.status ? response.status : null;
33
+ }
31
34
  }
32
35
 
33
36
  utils.inherits(AxiosError, Error, {
@@ -47,7 +50,7 @@ utils.inherits(AxiosError, Error, {
47
50
  // Axios
48
51
  config: utils.toJSONObject(this.config),
49
52
  code: this.code,
50
- status: this.response && this.response.status ? this.response.status : null
53
+ status: this.status
51
54
  };
52
55
  }
53
56
  });
package/lib/env/data.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "1.7.4";
1
+ export const VERSION = "1.7.6";
@@ -1,46 +1,48 @@
1
1
  import CanceledError from "../cancel/CanceledError.js";
2
2
  import AxiosError from "../core/AxiosError.js";
3
+ import utils from '../utils.js';
3
4
 
4
5
  const composeSignals = (signals, timeout) => {
5
- let controller = new AbortController();
6
+ const {length} = (signals = signals ? signals.filter(Boolean) : []);
6
7
 
7
- let aborted;
8
+ if (timeout || length) {
9
+ let controller = new AbortController();
8
10
 
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
- }
11
+ let aborted;
17
12
 
18
- let timer = timeout && setTimeout(() => {
19
- onabort(new AxiosError(`timeout ${timeout} of ms exceeded`, AxiosError.ETIMEDOUT))
20
- }, timeout)
13
+ const onabort = function (reason) {
14
+ if (!aborted) {
15
+ aborted = true;
16
+ unsubscribe();
17
+ const err = reason instanceof Error ? reason : this.reason;
18
+ controller.abort(err instanceof AxiosError ? err : new CanceledError(err instanceof Error ? err.message : err));
19
+ }
20
+ }
21
21
 
22
- const unsubscribe = () => {
23
- if (signals) {
24
- timer && clearTimeout(timer);
22
+ let timer = timeout && setTimeout(() => {
25
23
  timer = null;
26
- signals.forEach(signal => {
27
- signal &&
28
- (signal.removeEventListener ? signal.removeEventListener('abort', onabort) : signal.unsubscribe(onabort));
29
- });
30
- signals = null;
24
+ onabort(new AxiosError(`timeout ${timeout} of ms exceeded`, AxiosError.ETIMEDOUT))
25
+ }, timeout)
26
+
27
+ const unsubscribe = () => {
28
+ if (signals) {
29
+ timer && clearTimeout(timer);
30
+ timer = null;
31
+ signals.forEach(signal => {
32
+ signal.unsubscribe ? signal.unsubscribe(onabort) : signal.removeEventListener('abort', onabort);
33
+ });
34
+ signals = null;
35
+ }
31
36
  }
32
- }
33
37
 
34
- signals.forEach((signal) => signal && signal.addEventListener && signal.addEventListener('abort', onabort));
38
+ signals.forEach((signal) => signal.addEventListener('abort', onabort));
35
39
 
36
- const {signal} = controller;
40
+ const {signal} = controller;
37
41
 
38
- signal.unsubscribe = unsubscribe;
42
+ signal.unsubscribe = () => utils.asap(unsubscribe);
39
43
 
40
- return [signal, () => {
41
- timer && clearTimeout(timer);
42
- timer = null;
43
- }];
44
+ return signal;
45
+ }
44
46
  }
45
47
 
46
48
  export default composeSignals;
@@ -8,7 +8,7 @@ export default platform.hasStandardBrowserEnv ?
8
8
  // Standard browser envs have full support of the APIs needed to test
9
9
  // whether the request URL is of the same origin as current location.
10
10
  (function standardBrowserEnv() {
11
- const msie = /(msie|trident)/i.test(navigator.userAgent);
11
+ const msie = platform.navigator && /(msie|trident)/i.test(platform.navigator.userAgent);
12
12
  const urlParsingNode = document.createElement('a');
13
13
  let originURL;
14
14
 
@@ -1,5 +1,7 @@
1
1
  const hasBrowserEnv = typeof window !== 'undefined' && typeof document !== 'undefined';
2
2
 
3
+ const _navigator = typeof navigator === 'object' && navigator || undefined;
4
+
3
5
  /**
4
6
  * Determine if we're running in a standard browser environment
5
7
  *
@@ -17,10 +19,8 @@ const hasBrowserEnv = typeof window !== 'undefined' && typeof document !== 'unde
17
19
  *
18
20
  * @returns {boolean}
19
21
  */
20
- const hasStandardBrowserEnv = (
21
- (product) => {
22
- return hasBrowserEnv && ['ReactNative', 'NativeScript', 'NS'].indexOf(product) < 0
23
- })(typeof navigator !== 'undefined' && navigator.product);
22
+ const hasStandardBrowserEnv = hasBrowserEnv &&
23
+ (!_navigator || ['ReactNative', 'NativeScript', 'NS'].indexOf(_navigator.product) < 0);
24
24
 
25
25
  /**
26
26
  * Determine if we're running in a standard browser webWorker environment
@@ -46,5 +46,6 @@ export {
46
46
  hasBrowserEnv,
47
47
  hasStandardBrowserWebWorkerEnv,
48
48
  hasStandardBrowserEnv,
49
+ _navigator as navigator,
49
50
  origin
50
51
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "axios",
3
- "version": "1.7.4",
3
+ "version": "1.7.6",
4
4
  "description": "Promise based HTTP client for the browser and node.js",
5
5
  "main": "index.js",
6
6
  "exports": {