| 1 | const hasBrowserEnv = typeof window !== 'undefined' && typeof document !== 'undefined';
|
|---|
| 2 |
|
|---|
| 3 | const _navigator = (typeof navigator === 'object' && navigator) || undefined;
|
|---|
| 4 |
|
|---|
| 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 hasStandardBrowserEnv =
|
|---|
| 23 | hasBrowserEnv &&
|
|---|
| 24 | (!_navigator || ['ReactNative', 'NativeScript', 'NS'].indexOf(_navigator.product) < 0);
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * Determine if we're running in a standard browser webWorker environment
|
|---|
| 28 | *
|
|---|
| 29 | * Although the `isStandardBrowserEnv` method indicates that
|
|---|
| 30 | * `allows axios to run in a web worker`, the WebWorker will still be
|
|---|
| 31 | * filtered out due to its judgment standard
|
|---|
| 32 | * `typeof window !== 'undefined' && typeof document !== 'undefined'`.
|
|---|
| 33 | * This leads to a problem when axios post `FormData` in webWorker
|
|---|
| 34 | */
|
|---|
| 35 | const hasStandardBrowserWebWorkerEnv = (() => {
|
|---|
| 36 | return (
|
|---|
| 37 | typeof WorkerGlobalScope !== 'undefined' &&
|
|---|
| 38 | // eslint-disable-next-line no-undef
|
|---|
| 39 | self instanceof WorkerGlobalScope &&
|
|---|
| 40 | typeof self.importScripts === 'function'
|
|---|
| 41 | );
|
|---|
| 42 | })();
|
|---|
| 43 |
|
|---|
| 44 | const origin = (hasBrowserEnv && window.location.href) || 'http://localhost';
|
|---|
| 45 |
|
|---|
| 46 | export {
|
|---|
| 47 | hasBrowserEnv,
|
|---|
| 48 | hasStandardBrowserWebWorkerEnv,
|
|---|
| 49 | hasStandardBrowserEnv,
|
|---|
| 50 | _navigator as navigator,
|
|---|
| 51 | origin,
|
|---|
| 52 | };
|
|---|