Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dts-plugin): add IPV6 property to DTS Plugin #3421

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/odd-toys-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@module-federation/dts-plugin': minor
'@module-federation/sdk': minor
---

Added ipv6 property to DTS consume types options
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';

import { retrieveHostConfig } from './hostPlugin';
import { retrieveTypesArchiveDestinationPath } from '../lib/archiveHandler';
import { moduleFederationPlugin } from '@module-federation/sdk/.';

describe('hostPlugin', () => {
const moduleFederationConfig = {
Expand Down Expand Up @@ -41,6 +42,7 @@ describe('hostPlugin', () => {
abortOnError: true,
consumeAPITypes: false,
runtimePkgs: [],
family: 4 as moduleFederationPlugin.DtsHostOptions['family'],
});

expect(mapRemotesToDownload).toStrictEqual({
Expand All @@ -66,6 +68,7 @@ describe('hostPlugin', () => {
abortOnError: true,
consumeAPITypes: false,
runtimePkgs: [],
family: 4 as moduleFederationPlugin.DtsHostOptions['family'],
};

const { hostOptions, mapRemotesToDownload } =
Expand Down
1 change: 1 addition & 0 deletions packages/dts-plugin/src/core/configurations/hostPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const defaultOptions = {
abortOnError: true,
consumeAPITypes: false,
runtimePkgs: [],
family: 4,
} satisfies Partial<HostOptions>;

const buildZipUrl = (hostOptions: Required<HostOptions>, url: string) => {
Expand Down
21 changes: 18 additions & 3 deletions packages/dts-plugin/src/core/lib/DTSManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ import {
HOST_API_TYPES_FILE_NAME,
} from '../constant';
import { fileLog, logger } from '../../server';
import { axiosGet, cloneDeepOptions, isDebugMode } from './utils';
import {
axiosGet,
cloneDeepOptions,
isDebugMode,
getIpFamilyFromConfig,
} from './utils';
import { UpdateMode } from '../../server/constant';
import { ModuleFederationPluginOptions } from '@module-federation/sdk/dist/src/types/plugins/ModuleFederationPlugin';

export const MODULE_DTS_MANAGER_IDENTIFIER = 'MF DTS Manager';

Expand Down Expand Up @@ -180,8 +186,13 @@ class DTSManager {
if (!remoteInfo.url.includes(MANIFEST_EXT)) {
return remoteInfo as Required<RemoteInfo>;
}

const url = remoteInfo.url;
const res = await axiosGet(url);
const res = await axiosGet(url, {
family: getIpFamilyFromConfig(
this.options.host?.moduleFederationConfig,
),
});
const manifestJson = res.data as unknown as Manifest;
if (!manifestJson.metaData.types.zip) {
throw new Error(`Can not get ${remoteInfo.name}'s types archive url!`);
Expand Down Expand Up @@ -254,7 +265,11 @@ class DTSManager {
}
try {
const url = apiTypeUrl;
const res = await axiosGet(url);
const res = await axiosGet(url, {
family: getIpFamilyFromConfig(
this.options.host?.moduleFederationConfig,
),
});
let apiTypeFile = res.data as string;
apiTypeFile = apiTypeFile.replaceAll(
REMOTE_ALIAS_IDENTIFIER,
Expand Down
3 changes: 2 additions & 1 deletion packages/dts-plugin/src/core/lib/archiveHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { HostOptions } from '../interfaces/HostOptions';
import { RemoteOptions } from '../interfaces/RemoteOptions';
import { retrieveMfTypesPath } from './typeScriptCompiler';
import { fileLog } from '../../server';
import { axiosGet } from './utils';
import { axiosGet, getIpFamilyFromConfig } from './utils';
import { TsConfigJson } from '../interfaces/TsConfigJson';

export const retrieveTypesZipPath = (
Expand Down Expand Up @@ -62,6 +62,7 @@ export const downloadTypesArchive = (hostOptions: Required<HostOptions>) => {
const url = fileToDownload;
const response = await axiosGet(url, {
responseType: 'arraybuffer',
family: getIpFamilyFromConfig(hostOptions.moduleFederationConfig),
}).catch(downloadErrorLogger(destinationFolder, url));

try {
Expand Down
10 changes: 10 additions & 0 deletions packages/dts-plugin/src/core/lib/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ it('axiosGet should use agents with family set to 4', async () => {

httpSpy.mockRestore();
});

it('axiosGet should use agents with family set to 6', async () => {
const httpSpy = vi.spyOn(http, 'Agent');

await axiosGet('http://localhost', { family: 6 });

expect(httpSpy).toHaveBeenCalledWith({ family: 6 });

httpSpy.mockRestore();
});
19 changes: 17 additions & 2 deletions packages/dts-plugin/src/core/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import {
} from './typeScriptCompiler';
import cloneDeepWith from 'lodash.clonedeepwith';
import { DTSManagerOptions } from '../interfaces/DTSManagerOptions';
import {
DtsHostOptions,
PluginDtsOptions,
} from '@module-federation/sdk/dist/src/types/plugins/ModuleFederationPlugin';

export function getDTSManagerConstructor(
implementation?: string,
Expand Down Expand Up @@ -132,8 +136,19 @@ export function cloneDeepOptions(options: DTSManagerOptions) {
});
}

/**
* Extracts IP Family from Module Federation plugin configuration
* Defaults to 4 if not specified
*/
export function getIpFamilyFromConfig(
config: moduleFederationPlugin.ModuleFederationPluginOptions,
): AxiosRequestConfig['family'] {
const dtsPlugin = config?.dts as PluginDtsOptions;
return (dtsPlugin?.consumeTypes as DtsHostOptions)?.family;
}

export async function axiosGet(url: string, config?: AxiosRequestConfig) {
const httpAgent = new http.Agent({ family: 4 });
const httpsAgent = new https.Agent({ family: 4 });
const httpAgent = new http.Agent({ family: config?.family ?? 4 });
const httpsAgent = new https.Agent({ family: config?.family ?? 4 });
return axios.get(url, { httpAgent, httpsAgent, ...config });
}
1 change: 1 addition & 0 deletions packages/sdk/src/types/plugins/ModuleFederationPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export interface DtsHostOptions {
maxRetries?: number;
consumeAPITypes?: boolean;
runtimePkgs?: string[];
family?: 4 | 6;
}

export interface DtsRemoteOptions {
Expand Down
Loading