Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(platform): fixed emulated browser detection in node.js environment; #6055

Merged
merged 33 commits into from Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
3f2f496
chore(ci): Add release-it script;
DigitalBrainJS Dec 10, 2022
1bfc41b
Merge branch 'chore/release-it' into v1.x
DigitalBrainJS Dec 10, 2022
acaf0f4
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 15, 2022
8a5de86
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 15, 2022
cff9271
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 17, 2022
67afe20
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 19, 2022
dc7b66d
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 22, 2022
716eb59
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 23, 2022
10216bf
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 29, 2022
3b199f4
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Jan 7, 2023
f3d444f
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Jan 19, 2023
077c381
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Jan 26, 2023
f598657
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Jan 31, 2023
7bf5713
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Feb 5, 2023
81a8bd6
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Mar 8, 2023
f8bb158
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Apr 5, 2023
3f1c768
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Apr 18, 2023
a9b0418
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Apr 25, 2023
1a16f4e
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Apr 27, 2023
5e22e2f
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS May 10, 2023
b7c77b0
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Aug 25, 2023
2200038
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Aug 25, 2023
df38e0c
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Aug 26, 2023
878548a
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Aug 26, 2023
7a33bcd
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Aug 28, 2023
5cafdeb
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Sep 13, 2023
f0e6b28
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Sep 26, 2023
98371b0
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Sep 28, 2023
bee6e45
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Oct 4, 2023
c97df04
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Oct 26, 2023
62274b7
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Oct 28, 2023
f4d12c1
chore: draft;
DigitalBrainJS Nov 7, 2023
421343f
fix(platform): fixed emulated browser detection in node.js environment;
DigitalBrainJS Nov 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/adapters/xhr.js
Expand Up @@ -64,7 +64,7 @@ export default isXHRAdapterSupported && function (config) {
let contentType;

if (utils.isFormData(requestData)) {
if (platform.isStandardBrowserEnv || platform.isStandardBrowserWebWorkerEnv) {
if (platform.hasStandardBrowserEnv || platform.hasStandardBrowserWebWorkerEnv) {
requestHeaders.setContentType(false); // Let the browser set it
} else if(!requestHeaders.getContentType(/^\s*multipart\/form-data/)){
requestHeaders.setContentType('multipart/form-data'); // mobile/desktop app frameworks
Expand Down Expand Up @@ -186,7 +186,7 @@ export default isXHRAdapterSupported && function (config) {
// Add xsrf header
// This is only done if running in a standard browser environment.
// Specifically not if we're in a web worker, or react-native.
if (platform.isStandardBrowserEnv) {
if (platform.hasStandardBrowserEnv) {
// Add xsrf header
// regarding CVE-2023-45857 config.withCredentials condition was removed temporarily
const xsrfValue = isURLSameOrigin(fullPath) && config.xsrfCookieName && cookies.read(config.xsrfCookieName);
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/cookies.js
Expand Up @@ -3,7 +3,7 @@
import utils from './../utils.js';
import platform from '../platform/index.js';

export default platform.isStandardBrowserEnv ?
export default platform.hasStandardBrowserEnv ?

// Standard browser envs support document.cookie
(function standardBrowserEnv() {
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/isURLSameOrigin.js
Expand Up @@ -3,7 +3,7 @@
import utils from './../utils.js';
import platform from '../platform/index.js';

export default platform.isStandardBrowserEnv ?
export default platform.hasStandardBrowserEnv ?

// Standard browser envs have full support of the APIs needed to test
// whether the request URL is of the same origin as current location.
Expand Down
51 changes: 0 additions & 51 deletions lib/platform/browser/index.js
Expand Up @@ -2,63 +2,12 @@ import URLSearchParams from './classes/URLSearchParams.js'
import FormData from './classes/FormData.js'
import Blob from './classes/Blob.js'

/**
* Determine if we're running in a standard browser environment
*
* This allows axios to run in a web worker, and react-native.
* Both environments support XMLHttpRequest, but not fully standard globals.
*
* web workers:
* typeof window -> undefined
* typeof document -> undefined
*
* react-native:
* navigator.product -> 'ReactNative'
* nativescript
* navigator.product -> 'NativeScript' or 'NS'
*
* @returns {boolean}
*/
const isStandardBrowserEnv = (() => {
let product;
if (typeof navigator !== 'undefined' && (
(product = navigator.product) === 'ReactNative' ||
product === 'NativeScript' ||
product === 'NS')
) {
return false;
}

return typeof window !== 'undefined' && typeof document !== 'undefined';
})();

/**
* Determine if we're running in a standard browser webWorker environment
*
* Although the `isStandardBrowserEnv` method indicates that
* `allows axios to run in a web worker`, the WebWorker will still be
* filtered out due to its judgment standard
* `typeof window !== 'undefined' && typeof document !== 'undefined'`.
* This leads to a problem when axios post `FormData` in webWorker
*/
const isStandardBrowserWebWorkerEnv = (() => {
return (
typeof WorkerGlobalScope !== 'undefined' &&
// eslint-disable-next-line no-undef
self instanceof WorkerGlobalScope &&
typeof self.importScripts === 'function'
);
})();


export default {
isBrowser: true,
classes: {
URLSearchParams,
FormData,
Blob
},
isStandardBrowserEnv,
isStandardBrowserWebWorkerEnv,
protocols: ['http', 'https', 'file', 'blob', 'url', 'data']
};
47 changes: 47 additions & 0 deletions lib/platform/common/utils.js
@@ -0,0 +1,47 @@
const hasBrowserEnv = typeof window !== 'undefined' && typeof document !== 'undefined';

/**
* Determine if we're running in a standard browser environment
*
* This allows axios to run in a web worker, and react-native.
* Both environments support XMLHttpRequest, but not fully standard globals.
*
* web workers:
* typeof window -> undefined
* typeof document -> undefined
*
* react-native:
* navigator.product -> 'ReactNative'
* nativescript
* navigator.product -> 'NativeScript' or 'NS'
*
* @returns {boolean}
*/
const hasStandardBrowserEnv = (
(product) => {
return hasBrowserEnv && ['ReactNative', 'NativeScript', 'NS'].indexOf(product) < 0
})(typeof navigator !== 'undefined' && navigator.product);

/**
* Determine if we're running in a standard browser webWorker environment
*
* Although the `isStandardBrowserEnv` method indicates that
* `allows axios to run in a web worker`, the WebWorker will still be
* filtered out due to its judgment standard
* `typeof window !== 'undefined' && typeof document !== 'undefined'`.
* This leads to a problem when axios post `FormData` in webWorker
*/
const hasStandardBrowserWebWorkerEnv = (() => {
return (
typeof WorkerGlobalScope !== 'undefined' &&
// eslint-disable-next-line no-undef
self instanceof WorkerGlobalScope &&
typeof self.importScripts === 'function'
);
})();

export {
hasBrowserEnv,
hasStandardBrowserWebWorkerEnv,
hasStandardBrowserEnv
}
6 changes: 5 additions & 1 deletion lib/platform/index.js
@@ -1,3 +1,7 @@
import platform from './node/index.js';
import * as utils from './common/utils.js';

export {platform as default}
export default {
...utils,
...platform
}