Remove node-fetch fallback (#4417)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Gyeongjae Choi 2024-01-28 13:04:54 +09:00 committed by GitHub
parent 1b21d725ef
commit 54b8df132c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 11 additions and 20 deletions

View File

@ -29,6 +29,10 @@ myst:
functions.
{pr}`4392`
- {{ Breaking }} Pyodide will not fallback to `node-fetch` anymore when `fetch`
is not available in the Node.js < 18 environment.
{pr}`4417`
- {{ Enhancement }} Updated `pyimport` to support `pyimport("module.attribute")`.
{pr}`4395`

View File

@ -8,7 +8,6 @@ import {
} from "./environments";
let nodeUrlMod: any;
let nodeFetch: any;
let nodePath: any;
let nodeVmMod: any;
/** @private */
@ -20,13 +19,6 @@ declare var globalThis: {
fetch?: any;
};
const FETCH_NOT_FOUND_MSG = `\
"fetch" is not defined, maybe you're using node < 18? \
From Pyodide >= 0.25.0, node >= 18 is required. \
Older versions of Node.js may work, but it is not guaranteed or supported. \
Falling back to "node-fetch".\
`;
/**
* If we're in node, it's most convenient to import various node modules on
* initialization. Otherwise, this does nothing.
@ -39,14 +31,7 @@ export async function initNodeModules() {
// @ts-ignore
nodeUrlMod = (await import("url")).default;
nodeFsPromisesMod = await import("fs/promises");
if (globalThis.fetch) {
nodeFetch = fetch;
} else {
// @ts-ignore
console.warn(FETCH_NOT_FOUND_MSG);
// @ts-ignore
nodeFetch = (await import("node-fetch")).default;
}
// @ts-ignore
nodeVmMod = (await import("vm")).default;
nodePath = await import("path");
@ -131,7 +116,7 @@ function node_getBinaryResponse(
}
if (path.includes("://")) {
// If it has a protocol, make a fetch request
return { response: nodeFetch(path) };
return { response: fetch(path) };
} else {
// Otherwise get it from the file system
return {
@ -235,7 +220,7 @@ async function nodeLoadScript(url: string) {
}
if (url.includes("://")) {
// If it's a url, load it with fetch then eval it.
nodeVmMod.runInThisContext(await (await nodeFetch(url)).text());
nodeVmMod.runInThisContext(await (await fetch(url)).text());
} else {
// Otherwise, hopefully it is a relative path we can load from the file
// system.

View File

@ -59,7 +59,6 @@ const config = ({ input, output, format, name: globalName }) => ({
"crypto",
"fs",
"fs/promises",
"node-fetch",
"path",
"tty",
"url",

View File

@ -130,5 +130,8 @@
"base-64": "^1.0.0",
"ws": "^8.5.0"
},
"types": "./pyodide.d.ts"
"types": "./pyodide.d.ts",
"engines": {
"node": ">=18.0.0"
}
}