Expose Module.FS (#1692)

This commit is contained in:
Nicholas Bollweg 2021-07-10 17:57:01 -04:00 committed by GitHub
parent be980d503c
commit 60acd0089f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 70 additions and 5 deletions

View File

@ -87,10 +87,7 @@ node_modules/.installed : src/js/package.json
touch node_modules/.installed
build/pyodide.js: src/js/*.js src/js/pyproxy.gen.js node_modules/.installed
npx typescript src/js/pyodide.js \
--lib ES2018 --allowJs \
--declaration --emitDeclarationOnly \
--outDir build
npx typescript --project src/js
npx rollup -c src/js/rollup.config.js
src/js/pyproxy.gen.js : src/core/pyproxy.* src/core/*.h

View File

@ -47,6 +47,9 @@ substitutions:
- {{ Enhancement }} Pyodide can experimentally be used in Node.js {pr}`1689`
- {{ Enhancement }} Pyodide now exposes the emscripten `FS` module as `fileSystem`,
allowing for direct manipulation of the in-memory filesystem {pr}`1692`
## Standard library
- The following standard library modules are now available as standalone packages

View File

@ -7,6 +7,7 @@ export { loadPackage, loadedPackages, isPyProxy };
* @typedef {import('./pyproxy.gen').Py2JsResult} Py2JsResult
* @typedef {import('./pyproxy.gen').PyProxy} PyProxy
* @typedef {import('./pyproxy.gen').TypedArray} TypedArray
* @typedef {import('emscripten')} Emscripten
*/
/**
@ -329,8 +330,23 @@ setInterruptBuffer = Module.setInterruptBuffer;
export { setInterruptBuffer };
export function makePublicAPI() {
/**
* An alias to the `Emscripten File System API
* <https://emscripten.org/docs/api_reference/Filesystem-API.html>`_.
*
* This provides a wide range of POSIX-`like` file/device operations, including
* `mount
* <https://emscripten.org/docs/api_reference/Filesystem-API.html#FS.mount>`_
* which can be used to extend the in-memory filesystem with features like `persistence
* <https://emscripten.org/docs/api_reference/Filesystem-API.html#persistent-data>`_.
*
* @type {FS} The Emscripten File System API.
*/
const fileSystem = Module.FS;
let namespace = {
globals,
fileSystem,
pyodide_py,
version,
loadPackage,
@ -348,6 +364,7 @@ export function makePublicAPI() {
PythonError,
PyBuffer,
};
namespace._module = Module; // @private
Module.public_api = namespace;
return namespace;

View File

@ -1,3 +1,8 @@
/**
* The Emscripten Module.
*
* @private @type {import('emscripten').Module}
*/
export let Module = {};
Module.noImageDecoding = true;
Module.noAudioDecoding = true;

View File

@ -129,6 +129,12 @@
"defer-to-connect": "^1.0.1"
}
},
"@types/emscripten": {
"version": "1.39.5",
"resolved": "https://registry.npmjs.org/@types/emscripten/-/emscripten-1.39.5.tgz",
"integrity": "sha512-DIOOg+POSrYl+OlNRHQuIEqCd8DCtynG57H862UCce16nXJX7J8eWxNGgOcf8Eyge8zXeSs27mz1UcFu8L/L7g==",
"dev": true
},
"@types/eslint": {
"version": "7.2.14",
"resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-7.2.14.tgz",

View File

@ -8,12 +8,21 @@
"rollup-plugin-terser": "^7.0.2",
"tsd": "^0.15.1",
"typescript": "^4.2.4",
"mocha": "^9.0.2"
"mocha": "^9.0.2",
"@types/emscripten": "^1.39.5"
},
"type": "module",
"scripts": {
"test": "mocha --timeout 15000"
},
"tsd": {
"compilerOptions": {
"lib": [
"ES2018",
"DOM"
]
}
},
"dependencies": {
"node-fetch": "^2.6.1"
}

View File

@ -0,0 +1,15 @@
import assert from "assert";
import { loadPyodide } from "../pyodide.js";
describe("fileSystem", () => {
let pyodide;
it("loadPyodide", async () => {
pyodide = await loadPyodide({ indexURL: "../../build/" });
});
const exists = () => {
return pyodide.runPython("import os; os.path.exists('/tmp/js-test')");
};
it("no dir", async () => assert.equal(exists(), false));
it("mkdir", async () => pyodide.fileSystem.mkdir("/tmp/js-test"));
it("made dir", async () => assert.equal(exists(), true));
});

13
src/js/tsconfig.json Normal file
View File

@ -0,0 +1,13 @@
{
"$schema": "http://json.schemastore.org/tsconfig",
"compilerOptions": {
"allowJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"lib": ["ES2018", "DOM"],
"outDir": "../../build",
"rootDir": "."
},
"include": ["./*.js"],
"exclude": ["rollup.config.js"]
}