Skip to content

Commit

Permalink
fix(node): implement fetch hook in all locations
Browse files Browse the repository at this point in the history
  • Loading branch information
ScriptedAlchemy committed Jun 27, 2024
1 parent 58638ba commit b121e85
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-cups-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/runtime': patch
---

fix fetch hook types on runtime plugin interfaces
5 changes: 5 additions & 0 deletions .changeset/warm-forks-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/node': minor
---

fetch hook supported in node runtime plugin
5 changes: 5 additions & 0 deletions .changeset/wild-pillows-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/sdk': patch
---

refactor node fetch functions to call fetch hook from runtime plugin
21 changes: 14 additions & 7 deletions packages/node/src/runtimePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ declare const __non_webpack_require__: (id: string) => any;
export default function (): FederationRuntimePlugin {
return {
name: 'node-federation-plugin',
beforeInit: function (args: any) {
beforeInit(args) {
//patch webpack chunk loading handlers
(() => {
function resolveFile(rootOutputDir: string, chunkId: string): string {
const path = __non_webpack_require__('path');
Expand Down Expand Up @@ -182,11 +183,17 @@ export default function (): FederationRuntimePlugin {

args.origin.loaderHook.lifecycle.fetch
.emit(url.href, {})
.then(function (res: Response) {
return res.text();
.then(function (res: Response | false | void | Promise<Response>) {
if (res instanceof Response) {
return res.text();
} else if (typeof res === 'string') {
return res;
} else {
throw new Error('Invalid response type:' + res);
}
})
.then(function (data: string) {
const chunk: any = {};
const chunk: Record<string, unknown> = {};
try {
const urlDirname = url.pathname
.split('/')
Expand All @@ -201,9 +208,9 @@ export default function (): FederationRuntimePlugin {
} catch (e) {
callback(e as Error, null);
}
});
})
.catch((err: Error) => callback(err, null));
}

function httpVmStrategy(
chunkName: string,
remoteName: string,
Expand All @@ -227,7 +234,7 @@ export default function (): FederationRuntimePlugin {
);

fetchHook
.then((res: Response) => res.text())
.then((res: any) => res.text())
.then((data: string) => {
try {
const chunk: any = {};
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class FederationHost {
fetch: new AsyncHook<
[string, RequestInit],
Promise<Response> | void | false
>('fetch'),
>(),
});

constructor(userOptions: UserOptions) {
Expand Down
56 changes: 42 additions & 14 deletions packages/sdk/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,49 @@ export function createScriptNode(
cb(new Error(`Invalid URL: ${e}`));
return;
}
const getFetch = async () => {
//@ts-ignore
if (typeof globalThis['webpackChunkLoad'] !== 'undefined') {
//@ts-ignore
return globalThis['webpackChunkLoad'];
} else if (typeof fetch === 'undefined') {
const fetchModule = await importNodeModule('node-fetch');
//@ts-ignore
return fetchModule?.default || fetchModule;
} else {
return fetch;
}
const getFetch = (): Promise<typeof fetch> => {
return new Promise((resolve, reject) => {
// @ts-expect-error
if (typeof __webpack_require__ !== 'undefined') {
try {
if (
// @ts-expect-error
__webpack_require__.federation.instance.loaderHook.lifecycle.fetch
.emit
) {
return resolve(function (
input: RequestInfo | URL,
init?: RequestInit,
): Promise<Response> {
// @ts-expect-error
return __webpack_require__.federation.instance.loaderHook.lifecycle.fetch.emit(
input,
init || {},
);
});
}
} catch (e) {
console.warn(
'federation.instance.loaderHook.lifecycle.fetch failed:',
e,
);
// fall through
}
}

if (typeof fetch === 'undefined') {
importNodeModule<typeof import('node-fetch')>('node-fetch')
.then((fetchModule) => {
const nodeFetch = fetchModule.default || fetchModule;
resolve(nodeFetch as unknown as typeof fetch); // Ensure compatibility
})
.catch(reject);
} else {
resolve(fetch);
}
});
};
console.log('fetching', urlObj.href);

getFetch().then((f) => {
f(urlObj.href)
.then((res: Response) => res.text())
Expand Down Expand Up @@ -115,7 +144,6 @@ export function loadScriptNode(
`__FEDERATION_${info?.attrs?.['name']}:custom__`;
const entryExports = ((globalThis as any)[remoteEntryKey] =
scriptContext);
debugger;
resolve(entryExports);
}
},
Expand Down
4 changes: 1 addition & 3 deletions packages/webpack-bundler-runtime/src/initializeSharing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ export function initializeSharing({
typeof console !== 'undefined' && console.warn && console.warn(msg);

var initExternal = (id: string | number) => {
var handleError = (err: any) => {
var handleError = (err: any) =>
warn('Initialization of sharing external failed: ' + err);
debugger;
};
try {
var module = webpackRequire(id);
if (!module) return;
Expand Down

0 comments on commit b121e85

Please sign in to comment.