Skip to content

Commit 6c7be8d

Browse files
Remove code related to nodejs usages
1 parent 7e7cadb commit 6c7be8d

11 files changed

+36
-664
lines changed

src/js/api.ts

+4-34
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { CanvasInterface, canvas } from "./canvas";
33

44
import { PackageData, loadPackage, loadedPackages } from "./load-package";
55
import { type PyProxy, type PyDict } from "generated/pyproxy";
6-
import { loadBinaryFile, nodeFSMod } from "./compat";
6+
import { loadBinaryFile } from "./compat";
77
import { version } from "./version";
88
import { setStdin, setStdout, setStderr } from "./streams";
99
import { scheduleCallback } from "./scheduler";
1010
import { TypedArray } from "./types";
11-
import { IN_NODE, detectEnvironment } from "./environments";
11+
import { detectEnvironment } from "./environments";
1212
import "./literal-map.js";
1313
import {
1414
makeGlobalsProxy,
@@ -195,8 +195,8 @@ export class PyodideAPI {
195195
errorCallback?: (message: string) => void;
196196
checkIntegrity?: boolean;
197197
} = {
198-
checkIntegrity: true,
199-
},
198+
checkIntegrity: true,
199+
},
200200
): Promise<Array<PackageData>> {
201201
let pyimports = API.pyodide_code.find_imports(code);
202202
let imports;
@@ -552,36 +552,6 @@ export class PyodideAPI {
552552
};
553553
}
554554

555-
/**
556-
* Mounts a host directory into Pyodide file system. Only works in node.
557-
*
558-
* @param emscriptenPath The absolute path in the Emscripten file system to
559-
* mount the native directory. If the directory does not exist, it will be
560-
* created. If it does exist, it must be empty.
561-
* @param hostPath The host path to mount. It must be a directory that exists.
562-
*/
563-
static mountNodeFS(emscriptenPath: string, hostPath: string): void {
564-
if (!IN_NODE) {
565-
throw new Error("mountNodeFS only works in Node");
566-
}
567-
ensureMountPathExists(emscriptenPath);
568-
let stat;
569-
try {
570-
stat = nodeFSMod.lstatSync(hostPath);
571-
} catch (e) {
572-
throw new Error(`hostPath '${hostPath}' does not exist`);
573-
}
574-
if (!stat.isDirectory()) {
575-
throw new Error(`hostPath '${hostPath}' is not a directory`);
576-
}
577-
578-
Module.FS.mount(
579-
Module.FS.filesystems.NODEFS,
580-
{ root: hostPath },
581-
emscriptenPath,
582-
);
583-
}
584-
585555
/**
586556
* Tell Pyodide about Comlink.
587557
* Necessary to enable importing Comlink proxies into Python.

src/js/compat.ts

+4-158
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,16 @@
11
import ErrorStackParser from "./vendor/stackframe/error-stack-parser";
22
import {
3-
IN_NODE,
4-
IN_NODE_ESM,
53
IN_BROWSER_MAIN_THREAD,
64
IN_BROWSER_WEB_WORKER,
7-
IN_NODE_COMMONJS,
85
} from "./environments";
96
import { Lockfile } from "./types";
107

11-
let nodeUrlMod: typeof import("node:url");
12-
let nodePath: typeof import("node:path");
13-
let nodeVmMod: typeof import("node:vm");
14-
/** @private */
15-
export let nodeFSMod: typeof import("node:fs");
16-
/** @private */
17-
export let nodeFsPromisesMod: typeof import("node:fs/promises");
18-
198
declare var globalThis: {
209
importScripts: (url: string) => void;
2110
document?: typeof document;
2211
fetch?: typeof fetch;
2312
};
2413

25-
/**
26-
* If we're in node, it's most convenient to import various node modules on
27-
* initialization. Otherwise, this does nothing.
28-
* @private
29-
*/
30-
export async function initNodeModules() {
31-
if (!IN_NODE) {
32-
return;
33-
}
34-
// @ts-ignore
35-
nodeUrlMod = (await import("node:url")).default;
36-
nodeFSMod = await import("node:fs");
37-
nodeFsPromisesMod = await import("node:fs/promises");
38-
39-
// @ts-ignore
40-
nodeVmMod = (await import("node:vm")).default;
41-
nodePath = await import("node:path");
42-
pathSep = nodePath.sep;
43-
44-
// Emscripten uses `require`, so if it's missing (because we were imported as
45-
// an ES6 module) we need to polyfill `require` with `import`. `import` is
46-
// async and `require` is synchronous, so we import all packages that might be
47-
// required up front and define require to look them up in this table.
48-
49-
if (typeof require !== "undefined") {
50-
return;
51-
}
52-
// These are all the packages required in pyodide.asm.js. You can get this
53-
// list with:
54-
// $ grep -o 'require("[a-z]*")' pyodide.asm.js | sort -u
55-
const fs = nodeFSMod;
56-
const crypto = await import("node:crypto");
57-
const ws = await import("ws");
58-
const child_process = await import("node:child_process");
59-
const node_modules: { [mode: string]: any } = {
60-
fs,
61-
crypto,
62-
ws,
63-
child_process,
64-
};
65-
// Since we're in an ES6 module, this is only modifying the module namespace,
66-
// it's still private to Pyodide.
67-
(globalThis as any).require = function (mod: string): any {
68-
return node_modules[mod];
69-
};
70-
}
71-
72-
function node_resolvePath(path: string, base?: string): string {
73-
return nodePath.resolve(base || ".", path);
74-
}
75-
7614
function browser_resolvePath(path: string, base?: string): string {
7715
if (base === undefined) {
7816
// @ts-ignore
@@ -82,11 +20,7 @@ function browser_resolvePath(path: string, base?: string): string {
8220
}
8321

8422
export let resolvePath: (rest: string, base?: string) => string;
85-
if (IN_NODE) {
86-
resolvePath = node_resolvePath;
87-
} else {
88-
resolvePath = browser_resolvePath;
89-
}
23+
resolvePath = browser_resolvePath;
9024

9125
/**
9226
* Get the path separator. If we are on Linux or in the browser, it's /.
@@ -95,45 +29,6 @@ if (IN_NODE) {
9529
*/
9630
export let pathSep: string;
9731

98-
if (!IN_NODE) {
99-
pathSep = "/";
100-
}
101-
102-
/**
103-
* Load a binary file, only for use in Node. If the path explicitly is a URL,
104-
* then fetch from a URL, else load from the file system.
105-
* @param indexURL base path to resolve relative paths
106-
* @param path the path to load
107-
* @param checksum sha-256 checksum of the package
108-
* @returns An ArrayBuffer containing the binary data
109-
* @private
110-
*/
111-
function node_getBinaryResponse(
112-
path: string,
113-
_file_sub_resource_hash?: string | undefined, // Ignoring sub resource hash. See issue-2431.
114-
):
115-
| { response: Promise<Response>; binary?: undefined }
116-
| { binary: Promise<Uint8Array> } {
117-
if (path.startsWith("file://")) {
118-
// handle file:// with filesystem operations rather than with fetch.
119-
path = path.slice("file://".length);
120-
}
121-
if (path.includes("://")) {
122-
// If it has a protocol, make a fetch request
123-
return { response: fetch(path) };
124-
} else {
125-
// Otherwise get it from the file system
126-
return {
127-
binary: nodeFsPromisesMod
128-
.readFile(path)
129-
.then(
130-
(data: Buffer) =>
131-
new Uint8Array(data.buffer, data.byteOffset, data.byteLength),
132-
),
133-
};
134-
}
135-
}
136-
13732
/**
13833
* Load a binary file, only for use in browser. Resolves relative paths against
13934
* indexURL.
@@ -159,11 +54,7 @@ export let getBinaryResponse: (
15954
) =>
16055
| { response: Promise<Response>; binary?: undefined }
16156
| { response?: undefined; binary: Promise<Uint8Array> };
162-
if (IN_NODE) {
163-
getBinaryResponse = node_getBinaryResponse;
164-
} else {
165-
getBinaryResponse = browser_getBinaryResponse;
166-
}
57+
getBinaryResponse = browser_getBinaryResponse;
16758

16859
export async function loadBinaryFile(
16960
path: string,
@@ -206,53 +97,21 @@ if (IN_BROWSER_MAIN_THREAD) {
20697
}
20798
}
20899
};
209-
} else if (IN_NODE) {
210-
loadScript = nodeLoadScript;
211100
} else {
212101
throw new Error("Cannot determine runtime environment");
213102
}
214103

215-
/**
216-
* Load a text file and executes it as Javascript
217-
* @param url The path to load. May be a url or a relative file system path.
218-
* @private
219-
*/
220-
async function nodeLoadScript(url: string) {
221-
if (url.startsWith("file://")) {
222-
// handle file:// with filesystem operations rather than with fetch.
223-
url = url.slice("file://".length);
224-
}
225-
if (url.includes("://")) {
226-
// If it's a url, load it with fetch then eval it.
227-
nodeVmMod.runInThisContext(await (await fetch(url)).text());
228-
} else {
229-
// Otherwise, hopefully it is a relative path we can load from the file
230-
// system.
231-
await import(/* webpackIgnore: true */ nodeUrlMod.pathToFileURL(url).href);
232-
}
233-
}
234104

235105
export async function loadLockFile(lockFileURL: string): Promise<Lockfile> {
236-
if (IN_NODE) {
237-
await initNodeModules();
238-
const package_string = await nodeFsPromisesMod.readFile(lockFileURL, {
239-
encoding: "utf8",
240-
});
241-
return JSON.parse(package_string);
242-
} else {
243-
let response = await fetch(lockFileURL);
244-
return await response.json();
245-
}
106+
let response = await fetch(lockFileURL);
107+
return await response.json();
246108
}
247109

248110
/**
249111
* Calculate the directory name of the current module.
250112
* This is used to guess the indexURL when it is not provided.
251113
*/
252114
export async function calculateDirname(): Promise<string> {
253-
if (IN_NODE_COMMONJS) {
254-
return __dirname;
255-
}
256115

257116
let err: Error;
258117
try {
@@ -262,19 +121,6 @@ export async function calculateDirname(): Promise<string> {
262121
}
263122
let fileName = ErrorStackParser.parse(err)[0].fileName!;
264123

265-
if (IN_NODE && !fileName.startsWith("file://")) {
266-
fileName = `file://${fileName}`; // Error stack filenames are not starting with `file://` in `Bun`
267-
}
268-
269-
if (IN_NODE_ESM) {
270-
const nodePath = await import("node:path");
271-
const nodeUrl = await import("node:url");
272-
273-
// FIXME: We would like to use import.meta.url here,
274-
// but mocha seems to mess with compiling typescript files to ES6.
275-
return nodeUrl.fileURLToPath(nodePath.dirname(fileName));
276-
}
277-
278124
const indexOfLastSlash = fileName.lastIndexOf(pathSep);
279125
if (indexOfLastSlash === -1) {
280126
throw new Error(

src/js/emscripten-settings.ts

-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/** @private */
22

33
import { ConfigType } from "./pyodide";
4-
import { initializeNativeFS } from "./nativefs";
54
import { loadBinaryFile, getBinaryResponse } from "./compat";
65
import { API, PreRunFunc } from "./types";
76

@@ -100,19 +99,6 @@ function setEnvironment(env: { [key: string]: string }): PreRunFunc {
10099
};
101100
}
102101

103-
/**
104-
* Mount local directories to the virtual file system. Only for Node.js.
105-
* @param mounts The list of paths to mount.
106-
*/
107-
function mountLocalDirectories(mounts: string[]): PreRunFunc {
108-
return (Module) => {
109-
for (const mount of mounts) {
110-
Module.FS.mkdirTree(mount);
111-
Module.FS.mount(Module.FS.filesystems.NODEFS, { root: mount }, mount);
112-
}
113-
};
114-
}
115-
116102
/**
117103
* Install the Python standard library to the virtual file system.
118104
*
@@ -169,8 +155,6 @@ function getFileSystemInitializationFuncs(config: ConfigType): PreRunFunc[] {
169155
installStdlib(stdLibURL),
170156
createHomeDirectory(config.env.HOME),
171157
setEnvironment(config.env),
172-
mountLocalDirectories(config._node_mounts),
173-
initializeNativeFS,
174158
];
175159
}
176160

src/js/environments.ts

+1-30
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,7 @@
11
// @ts-nocheck
22

33
/** @private */
4-
export const IN_NODE =
5-
typeof process === "object" &&
6-
typeof process.versions === "object" &&
7-
typeof process.versions.node === "string" &&
8-
!process.browser; /* This last condition checks if we run the browser shim of process */
9-
10-
/** @private */
11-
export const IN_NODE_COMMONJS =
12-
IN_NODE &&
13-
typeof module !== "undefined" &&
14-
typeof module.exports !== "undefined" &&
15-
typeof require !== "undefined" &&
16-
typeof __dirname !== "undefined";
17-
18-
/** @private */
19-
export const IN_NODE_ESM = IN_NODE && !IN_NODE_COMMONJS;
20-
21-
/** @private */
22-
export const IN_BUN = typeof globalThis.Bun !== "undefined";
23-
24-
/** @private */
25-
export const IN_DENO = typeof Deno !== "undefined"; // just in case...
26-
27-
/** @private */
28-
export const IN_BROWSER = !IN_NODE && !IN_DENO;
4+
export const IN_BROWSER = true;
295

306
/** @private */
317
export const IN_BROWSER_MAIN_THREAD =
@@ -54,11 +30,6 @@ export const IN_SAFARI =
5430
*/
5531
export function detectEnvironment(): Record<string, boolean> {
5632
return {
57-
IN_NODE: IN_NODE,
58-
IN_NODE_COMMONJS: IN_NODE_COMMONJS,
59-
IN_NODE_ESM: IN_NODE_ESM,
60-
IN_BUN: IN_BUN,
61-
IN_DENO: IN_DENO,
6233
IN_BROWSER: IN_BROWSER,
6334
IN_BROWSER_MAIN_THREAD: IN_BROWSER_MAIN_THREAD,
6435
IN_BROWSER_WEB_WORKER: IN_BROWSER_WEB_WORKER,

0 commit comments

Comments
 (0)