Skip to content

Commit 3dc8369

Browse files
fix(platform): fixed emulated browser detection in node.js environment; (#6055)
1 parent f7adacd commit 3dc8369

File tree

6 files changed

+56
-56
lines changed

6 files changed

+56
-56
lines changed

lib/adapters/xhr.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export default isXHRAdapterSupported && function (config) {
6464
let contentType;
6565

6666
if (utils.isFormData(requestData)) {
67-
if (platform.isStandardBrowserEnv || platform.isStandardBrowserWebWorkerEnv) {
67+
if (platform.hasStandardBrowserEnv || platform.hasStandardBrowserWebWorkerEnv) {
6868
requestHeaders.setContentType(false); // Let the browser set it
6969
} else if(!requestHeaders.getContentType(/^\s*multipart\/form-data/)){
7070
requestHeaders.setContentType('multipart/form-data'); // mobile/desktop app frameworks
@@ -186,7 +186,7 @@ export default isXHRAdapterSupported && function (config) {
186186
// Add xsrf header
187187
// This is only done if running in a standard browser environment.
188188
// Specifically not if we're in a web worker, or react-native.
189-
if (platform.isStandardBrowserEnv) {
189+
if (platform.hasStandardBrowserEnv) {
190190
// Add xsrf header
191191
// regarding CVE-2023-45857 config.withCredentials condition was removed temporarily
192192
const xsrfValue = isURLSameOrigin(fullPath) && config.xsrfCookieName && cookies.read(config.xsrfCookieName);

lib/helpers/cookies.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import utils from './../utils.js';
44
import platform from '../platform/index.js';
55

6-
export default platform.isStandardBrowserEnv ?
6+
export default platform.hasStandardBrowserEnv ?
77

88
// Standard browser envs support document.cookie
99
(function standardBrowserEnv() {

lib/helpers/isURLSameOrigin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import utils from './../utils.js';
44
import platform from '../platform/index.js';
55

6-
export default platform.isStandardBrowserEnv ?
6+
export default platform.hasStandardBrowserEnv ?
77

88
// Standard browser envs have full support of the APIs needed to test
99
// whether the request URL is of the same origin as current location.

lib/platform/browser/index.js

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,12 @@ import URLSearchParams from './classes/URLSearchParams.js'
22
import FormData from './classes/FormData.js'
33
import Blob from './classes/Blob.js'
44

5-
/**
6-
* Determine if we're running in a standard browser environment
7-
*
8-
* This allows axios to run in a web worker, and react-native.
9-
* Both environments support XMLHttpRequest, but not fully standard globals.
10-
*
11-
* web workers:
12-
* typeof window -> undefined
13-
* typeof document -> undefined
14-
*
15-
* react-native:
16-
* navigator.product -> 'ReactNative'
17-
* nativescript
18-
* navigator.product -> 'NativeScript' or 'NS'
19-
*
20-
* @returns {boolean}
21-
*/
22-
const isStandardBrowserEnv = (() => {
23-
let product;
24-
if (typeof navigator !== 'undefined' && (
25-
(product = navigator.product) === 'ReactNative' ||
26-
product === 'NativeScript' ||
27-
product === 'NS')
28-
) {
29-
return false;
30-
}
31-
32-
return typeof window !== 'undefined' && typeof document !== 'undefined';
33-
})();
34-
35-
/**
36-
* Determine if we're running in a standard browser webWorker environment
37-
*
38-
* Although the `isStandardBrowserEnv` method indicates that
39-
* `allows axios to run in a web worker`, the WebWorker will still be
40-
* filtered out due to its judgment standard
41-
* `typeof window !== 'undefined' && typeof document !== 'undefined'`.
42-
* This leads to a problem when axios post `FormData` in webWorker
43-
*/
44-
const isStandardBrowserWebWorkerEnv = (() => {
45-
return (
46-
typeof WorkerGlobalScope !== 'undefined' &&
47-
// eslint-disable-next-line no-undef
48-
self instanceof WorkerGlobalScope &&
49-
typeof self.importScripts === 'function'
50-
);
51-
})();
52-
53-
545
export default {
556
isBrowser: true,
567
classes: {
578
URLSearchParams,
589
FormData,
5910
Blob
6011
},
61-
isStandardBrowserEnv,
62-
isStandardBrowserWebWorkerEnv,
6312
protocols: ['http', 'https', 'file', 'blob', 'url', 'data']
6413
};

lib/platform/common/utils.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const hasBrowserEnv = typeof window !== 'undefined' && typeof document !== 'undefined';
2+
3+
/**
4+
* Determine if we're running in a standard browser environment
5+
*
6+
* This allows axios to run in a web worker, and react-native.
7+
* Both environments support XMLHttpRequest, but not fully standard globals.
8+
*
9+
* web workers:
10+
* typeof window -> undefined
11+
* typeof document -> undefined
12+
*
13+
* react-native:
14+
* navigator.product -> 'ReactNative'
15+
* nativescript
16+
* navigator.product -> 'NativeScript' or 'NS'
17+
*
18+
* @returns {boolean}
19+
*/
20+
const hasStandardBrowserEnv = (
21+
(product) => {
22+
return hasBrowserEnv && ['ReactNative', 'NativeScript', 'NS'].indexOf(product) < 0
23+
})(typeof navigator !== 'undefined' && navigator.product);
24+
25+
/**
26+
* Determine if we're running in a standard browser webWorker environment
27+
*
28+
* Although the `isStandardBrowserEnv` method indicates that
29+
* `allows axios to run in a web worker`, the WebWorker will still be
30+
* filtered out due to its judgment standard
31+
* `typeof window !== 'undefined' && typeof document !== 'undefined'`.
32+
* This leads to a problem when axios post `FormData` in webWorker
33+
*/
34+
const hasStandardBrowserWebWorkerEnv = (() => {
35+
return (
36+
typeof WorkerGlobalScope !== 'undefined' &&
37+
// eslint-disable-next-line no-undef
38+
self instanceof WorkerGlobalScope &&
39+
typeof self.importScripts === 'function'
40+
);
41+
})();
42+
43+
export {
44+
hasBrowserEnv,
45+
hasStandardBrowserWebWorkerEnv,
46+
hasStandardBrowserEnv
47+
}

lib/platform/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import platform from './node/index.js';
2+
import * as utils from './common/utils.js';
23

3-
export {platform as default}
4+
export default {
5+
...utils,
6+
...platform
7+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy