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 4 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,8 @@ describe('hostPlugin', () => {
abortOnError: true,
consumeAPITypes: false,
runtimePkgs: [],
ipVersion:
'ipv4' as moduleFederationPlugin.DtsHostOptions['ipVersion'],
});

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

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: [],
ipVersion: 'ipv4',
} 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();
});
24 changes: 22 additions & 2 deletions packages/dts-plugin/src/core/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,28 @@ export function cloneDeepOptions(options: DTSManagerOptions) {
});
}

/**
* Extracts IP version from Module Federation plugin configuration
* Defaults to 'ipv4' if not specified
*/
export function getIpFamilyFromConfig(
config: moduleFederationPlugin.ModuleFederationPluginOptions,
): AxiosRequestConfig['family'] {
if (!config) return 4;

const { dts } = config;
if (dts && typeof dts === 'object' && dts !== null) {
const { consumeTypes } = dts;
if (typeof consumeTypes === 'object' && consumeTypes !== null) {
return consumeTypes.ipVersion === 'ipv6' ? 6 : 4;
}
}

return 4;
}

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[];
ipVersion?: 'ipv4' | 'ipv6';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you set the field as family , so no need to add transform helper

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can, the name "family" felt a little ambiguous at first and the types are a bit weird since multiple layers of the config can be a boolean or the type so there is a lot of casting involved - which the helper/util method sort of abstracted away

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@2heal1 - updated, let me know if you want me to just inline the type handling and remove the util method

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine . Thanks for contributing! Approved~

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Let me test this a bit further

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sure , when you ready to merge just pin me

}

export interface DtsRemoteOptions {
Expand Down