refactor(ide): replace dynamic undici import with static fetch import (#23268)

This commit is contained in:
Coco Sheng
2026-03-20 17:14:25 -04:00
committed by GitHub
parent 4e80f01fda
commit 8eb419a47a
2 changed files with 12 additions and 7 deletions

View File

@@ -696,4 +696,14 @@ describe('ide-connection-utils', () => {
); // Short-circuiting
});
});
describe('createProxyAwareFetch', () => {
it('should return a proxy-aware fetcher function', async () => {
const { createProxyAwareFetch } = await import(
'./ide-connection-utils.js'
);
const fetcher = await createProxyAwareFetch('127.0.0.1');
expect(typeof fetcher).toBe('function');
});
});
});

View File

@@ -7,7 +7,7 @@
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
import { EnvHttpProxyAgent } from 'undici';
import { EnvHttpProxyAgent, fetch as undiciFetch } from 'undici';
import { debugLogger } from '../utils/debugLogger.js';
import { isSubpath, resolveToRealPath } from '../utils/paths.js';
import { isNodeError } from '../utils/errors.js';
@@ -286,12 +286,7 @@ export async function createProxyAwareFetch(ideServerHost: string) {
const agent = new EnvHttpProxyAgent({
noProxy: [existingNoProxy, ideServerHost].filter(Boolean).join(','),
});
const undiciPromise = import('undici');
// Suppress unhandled rejection if the promise is not awaited immediately.
// If the import fails, the error will be thrown when awaiting undiciPromise below.
undiciPromise.catch(() => {});
return async (url: string | URL, init?: RequestInit): Promise<Response> => {
const { fetch: fetchFn } = await undiciPromise;
const fetchOptions: RequestInit & { dispatcher?: unknown } = {
...init,
dispatcher: agent,
@@ -299,7 +294,7 @@ export async function createProxyAwareFetch(ideServerHost: string) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
const options = fetchOptions as unknown as import('undici').RequestInit;
try {
const response = await fetchFn(url, options);
const response = await undiciFetch(url, options);
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
return new Response(response.body as ReadableStream<unknown> | null, {
status: response.status,