mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +03:00
fix(platform): fixed emulated browser detection in node.js environment; (#6055)
This commit is contained in:
+2
-2
@@ -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
|
||||
@@ -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);
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -2,55 +2,6 @@ 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: {
|
||||
@@ -58,7 +9,5 @@ export default {
|
||||
FormData,
|
||||
Blob
|
||||
},
|
||||
isStandardBrowserEnv,
|
||||
isStandardBrowserWebWorkerEnv,
|
||||
protocols: ['http', 'https', 'file', 'blob', 'url', 'data']
|
||||
};
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user