Skip to content

Commit a50c459

Browse files
authored
fix: use ipv4 first while load local remote (#2623)
1 parent 2d234ea commit a50c459

File tree

6 files changed

+44
-20
lines changed

6 files changed

+44
-20
lines changed

.changeset/clean-wombats-kiss.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/dts-plugin': patch
3+
---
4+
5+
fix: use ipv4 first while load local remote

packages/dts-plugin/src/core/lib/DTSManager.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import {
2929
REMOTE_ALIAS_IDENTIFIER,
3030
HOST_API_TYPES_FILE_NAME,
3131
} from '../constant';
32-
import axios from 'axios';
3332
import { fileLog } from '../../server';
33+
import { axiosGet } from './utils';
3434

3535
export const MODULE_DTS_MANAGER_IDENTIFIER = 'MF DTS Manager';
3636

@@ -183,10 +183,7 @@ class DTSManager {
183183
return remoteInfo as Required<RemoteInfo>;
184184
}
185185
const url = remoteInfo.url;
186-
const res = await axios({
187-
method: 'get',
188-
url,
189-
});
186+
const res = await axiosGet(url);
190187
const manifestJson = res.data as unknown as Manifest;
191188
if (!manifestJson.metaData.types.zip) {
192189
throw new Error(`Can not get ${remoteInfo.name}'s types archive url!`);
@@ -249,7 +246,7 @@ class DTSManager {
249246
}
250247
try {
251248
const url = apiTypeUrl;
252-
const res = await axios.get(url);
249+
const res = await axiosGet(url);
253250
let apiTypeFile = res.data as string;
254251
apiTypeFile = apiTypeFile.replaceAll(
255252
REMOTE_ALIAS_IDENTIFIER,

packages/dts-plugin/src/core/lib/archiveHandler.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ describe('archiveHandler', () => {
7676
]);
7777
expect(existsSync(archivePath)).toBeTruthy();
7878
expect(axios.get).toHaveBeenCalledTimes(1);
79-
expect(axios.get).toHaveBeenCalledWith(fileToDownload, {
80-
responseType: 'arraybuffer',
81-
});
8279
});
8380

8481
it('correctly handles exception', async () => {
@@ -92,9 +89,6 @@ describe('archiveHandler', () => {
9289
`Network error: Unable to download federated mocks for '${destinationFolder}' from '${fileToDownload}' because '${message}'`,
9390
);
9491
expect(axios.get).toHaveBeenCalledTimes(hostOptions.maxRetries);
95-
expect(axios.get).toHaveBeenCalledWith(fileToDownload, {
96-
responseType: 'arraybuffer',
97-
});
9892
});
9993

10094
it('not throw error while set abortOnError: false ', async () => {

packages/dts-plugin/src/core/lib/archiveHandler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import AdmZip from 'adm-zip';
2-
import axios from 'axios';
32
import { resolve, join } from 'path';
43
import typescript from 'typescript';
54

65
import { HostOptions } from '../interfaces/HostOptions';
76
import { RemoteOptions } from '../interfaces/RemoteOptions';
87
import { retrieveMfTypesPath } from './typeScriptCompiler';
98
import { fileLog } from '../../server';
9+
import { axiosGet } from './utils';
1010

1111
export const retrieveTypesZipPath = (
1212
mfTypesPath: string,
@@ -59,9 +59,9 @@ export const downloadTypesArchive = (hostOptions: Required<HostOptions>) => {
5959
while (retries++ < hostOptions.maxRetries) {
6060
try {
6161
const url = fileToDownload;
62-
const response = await axios
63-
.get(url, { responseType: 'arraybuffer' })
64-
.catch(downloadErrorLogger(destinationFolder, url));
62+
const response = await axiosGet(url, {
63+
responseType: 'arraybuffer',
64+
}).catch(downloadErrorLogger(destinationFolder, url));
6565

6666
const zip = new AdmZip(Buffer.from(response.data));
6767
zip.extractAllTo(destinationPath, true);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { it, expect, vi } from 'vitest';
2+
import http from 'http';
3+
import { axiosGet } from './utils';
4+
5+
vi.mock('axios', () => ({
6+
default: {
7+
get: vi.fn(() => Promise.resolve({ data: 'mocked response' })),
8+
},
9+
}));
10+
11+
it('axiosGet should use agents with family set to 4', async () => {
12+
const httpSpy = vi.spyOn(http, 'Agent');
13+
14+
await axiosGet('http://localhost');
15+
16+
expect(httpSpy).toHaveBeenCalledWith({ family: 4 });
17+
18+
httpSpy.mockRestore();
19+
});

packages/dts-plugin/src/core/lib/utils.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
import fs from 'fs';
12
import path from 'path';
3+
import axios, { type AxiosRequestConfig } from 'axios';
4+
import http from 'http';
5+
import https from 'https';
6+
import { moduleFederationPlugin } from '@module-federation/sdk';
7+
import ansiColors from 'ansi-colors';
8+
29
import { retrieveRemoteConfig } from '../configurations/remotePlugin';
310
import { HostOptions } from '../interfaces/HostOptions';
411
import { RemoteOptions } from '../interfaces/RemoteOptions';
512
import { DTSManager } from './DTSManager';
613
import { retrieveTypesZipPath } from './archiveHandler';
7-
import fs from 'fs';
814
import {
915
retrieveMfAPITypesPath,
1016
retrieveMfTypesPath,
1117
} from './typeScriptCompiler';
12-
import { moduleFederationPlugin } from '@module-federation/sdk';
13-
import ansiColors from 'ansi-colors';
14-
import tsconfigPaths from 'vite-tsconfig-paths';
1518

1619
export function getDTSManagerConstructor(
1720
implementation?: string,
@@ -93,3 +96,9 @@ export const isTSProject = (
9396
return false;
9497
}
9598
};
99+
100+
export async function axiosGet(url: string, config?: AxiosRequestConfig) {
101+
const httpAgent = new http.Agent({ family: 4 });
102+
const httpsAgent = new https.Agent({ family: 4 });
103+
return axios.get(url, { httpAgent, httpsAgent, ...config });
104+
}

0 commit comments

Comments
 (0)