2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-23 20:40:40 +03:00

fix(fetch): defer global access in fetch adapter (#7260)

This commit is contained in:
Yueming Gao
2026-05-02 00:20:18 +08:00
committed by GitHub
parent ad68e1a484
commit e57349992f
2 changed files with 26 additions and 8 deletions
+7 -8
View File
@@ -18,13 +18,6 @@ const DEFAULT_CHUNK_SIZE = 64 * 1024;
const { isFunction } = utils; const { isFunction } = utils;
const globalFetchAPI = (({ Request, Response }) => ({
Request,
Response,
}))(utils.global);
const { ReadableStream, TextEncoder } = utils.global;
const test = (fn, ...args) => { const test = (fn, ...args) => {
try { try {
return !!fn(...args); return !!fn(...args);
@@ -34,11 +27,17 @@ const test = (fn, ...args) => {
}; };
const factory = (env) => { const factory = (env) => {
const globalObject = utils.global ?? globalThis;
const { ReadableStream, TextEncoder } = globalObject;
env = utils.merge.call( env = utils.merge.call(
{ {
skipUndefined: true, skipUndefined: true,
}, },
globalFetchAPI, {
Request: globalObject.Request,
Response: globalObject.Response,
},
env env
); );
+19
View File
@@ -9,6 +9,7 @@ import {
makeEchoStream, makeEchoStream,
} from '../../setup/server.js'; } from '../../setup/server.js';
import axios from '../../../index.js'; import axios from '../../../index.js';
import utils from '../../../lib/utils.js';
import { getFetch } from '../../../lib/adapters/fetch.js'; import { getFetch } from '../../../lib/adapters/fetch.js';
import stream from 'stream'; import stream from 'stream';
import { AbortController } from 'abortcontroller-polyfill/dist/cjs-ponyfill.js'; import { AbortController } from 'abortcontroller-polyfill/dist/cjs-ponyfill.js';
@@ -814,6 +815,24 @@ describe.runIf(typeof fetch === 'function')('supports fetch with nodejs', () =>
}); });
describe('env config', () => { describe('env config', () => {
it('should fallback to globalThis when utils.global is temporarily undefined', () => {
const originalGlobal = utils.global;
try {
utils.global = undefined;
assert.doesNotThrow(() =>
getFetch({
env: {
fetch() {},
},
})
);
} finally {
utils.global = originalGlobal;
}
});
it('should respect env fetch API configuration', async () => { it('should respect env fetch API configuration', async () => {
const { data, headers } = await fetchAxios.get('/', { const { data, headers } = await fetchAxios.get('/', {
env: { env: {