Skip to content

Commit f42d61d

Browse files
committed
Only polyfill fetch for file: URLs on Node.
There are some cases where one might want to load a bundle from network on Node, like in the VS Code extension host (at a future point, when VS Code updates the Node version to v20.x or later and the extension switches to using proper async imports instead of the awful hack it is currently doing).
1 parent 66ac805 commit f42d61d

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

lib/fetch.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
// Node v18.x doesn't have a usable `fetch()`.
2-
let fetch = globalThis.fetch;
1+
let fetch;
32
if (typeof process === 'object' && process.release?.name === 'node') {
4-
fetch = async function(url) {
5-
const { readFile } = await import('fs/promises');
6-
let contentType = 'application/octet-stream';
7-
if (url.pathname.endsWith('.wasm'))
8-
contentType = 'application/wasm';
9-
return new Response(await readFile(url), { headers: { "Content-Type": contentType } });
3+
// Node doesn't have a usable `fetch()`.
4+
fetch = async function(url, options) {
5+
if (url.protocol === 'file:') {
6+
const { readFile } = await import('fs/promises');
7+
let contentType = 'application/octet-stream';
8+
if (url.pathname.endsWith('.wasm'))
9+
contentType = 'application/wasm';
10+
return new Response(await readFile(url), { headers: { "Content-Type": contentType } });
11+
} else {
12+
return globalThis.fetch(url, options);
13+
}
1014
};
15+
} else {
16+
fetch = globalThis.fetch;
1117
}
1218

1319
export default fetch;

0 commit comments

Comments
 (0)