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

fix(node): allow fetch override on runtime plugin #2603

Merged
merged 14 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/beige-impalas-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/sdk': patch
---

allow global fetch override for node
5 changes: 5 additions & 0 deletions .changeset/seven-moons-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/node': patch
---

allow fetch override on runtime plugin with globalThis.webpackChunkLoad
71 changes: 49 additions & 22 deletions packages/node/src/runtimePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ export default function () {
};
return callback(null, emptyChunk);
}
fetch(url)
const fetchFunction = globalThis.webpackChunkLoad || fetch;
Copy link
Collaborator

Choose a reason for hiding this comment

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

fetch: new AsyncHook<

Maybe we can set fetch through fetch hook instead of using global variables

Copy link
Member Author

Choose a reason for hiding this comment

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

@zhoushaw do you have a sample of how to use the fetch hook?
I do not think it is bound to script loader, only json manifest loader

This provides backward compat for user, but i am happy to look at better solution for future and deprecate this usage

fetchFunction(url)
.then(function (res) {
return res.text();
})
Expand All @@ -150,48 +151,74 @@ export default function () {
}
});
}
function httpVmStrategy(
chunkName: string,
remoteName: string,
callback: (err: Error | null, chunk: any) => void,
): void {
const http = __non_webpack_require__('http');
const https = __non_webpack_require__('https');
ScriptedAlchemy marked this conversation as resolved.
Show resolved Hide resolved
const vm = __non_webpack_require__('vm');

function httpVmStrategy(chunkName, remoteName, callback) {
var http = __non_webpack_require__('http');
var https = __non_webpack_require__('https');
var vm = __non_webpack_require__('vm');

var url = resolveUrl(remoteName, chunkName);
const url = resolveUrl(remoteName, chunkName);
if (!url) {
var emptyChunk = {
const emptyChunk = {
modules: {}, // No modules
ids: [], // No chunk IDs
runtime: null, // No runtime function
};
return callback(null, emptyChunk);
}
var protocol = url.protocol === 'https:' ? https : http;
protocol.get(url.href, function (res) {
var data = '';
res.on('data', function (chunk) {

const fetchMethod =
globalThis.webpackChunkLoad ||
(url.protocol === 'https:' ? https : http).get;
ScriptedAlchemy marked this conversation as resolved.
Show resolved Hide resolved

const handleResponse = (res: any) => {
let data = '';
res.on('data', (chunk: Buffer) => {
data += chunk.toString();
});
res.on('end', function () {
res.on('end', () => {
try {
var chunk = {};
var urlDirname = url.pathname.split('/').slice(0, -1).join('/');
const chunk = {};
const urlDirname = url.pathname
.split('/')
.slice(0, -1)
.join('/');
vm.runInThisContext(
'(function(exports, require, __dirname, __filename) {' +
data +
'\n})',
chunkName,
`(function(exports, require, __dirname, __filename) {${data}\n})`,
{ filename: chunkName },
)(chunk, __non_webpack_require__, urlDirname, chunkName);
callback(null, chunk);
} catch (e) {
callback(e, null);
}
});
res.on('error', function (err) {
res.on('error', (err: Error) => {
callback(err, null);
});
});
}
};

if (fetchMethod === globalThis.webpackChunkLoad) {
fetchMethod(url.href)
.then((res: Response) => res.text())
.then((data: string) => {
handleResponse({
on: (event: string, handler: (data?: string) => void) => {
if (event === 'data') {
handler(data);
} else if (event === 'end') {
handler();
}
},
});
})
.catch((err: Error) => callback(err, null));
} else {
fetchMethod(url.href, handleResponse);
}
}
function loadChunkStrategy(
strategyType,
chunkId,
Expand Down
6 changes: 5 additions & 1 deletion packages/sdk/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ export function createScriptNode(
return;
}
const getFetch = async () => {
if (typeof fetch === 'undefined') {
//@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;
Expand Down
Loading