mirror of https://github.com/pyodide/pyodide.git
Minimal Node.js support (#1691)
This commit is contained in:
parent
601e474e7d
commit
e97d3bb2cf
|
@ -220,7 +220,7 @@ jobs:
|
|||
command: |
|
||||
cd src/js
|
||||
npx tsd
|
||||
npm i
|
||||
npm install
|
||||
npm test
|
||||
|
||||
|
||||
|
|
|
@ -45,6 +45,8 @@ substitutions:
|
|||
using {any}`pyodide.registerComlink`
|
||||
{pr}`1642`
|
||||
|
||||
- {{ Enhancement }} Pyodide can experimentally be used in Node.js {pr}`1689`
|
||||
|
||||
## Standard library
|
||||
|
||||
- The following standard library modules are now available as standalone packages
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
commit fb4b3c3a955e73fe20f930fb73588c0ad7a446e5
|
||||
Author: Sam Clegg <sbc@chromium.org>
|
||||
Date: Thu Jun 3 09:42:51 2021 -0700
|
||||
|
||||
Add support for `--preload-file` in Node.js (#11785)
|
||||
|
||||
Note: this is included in emscripten 2.O.24
|
||||
|
||||
diff --git a/emsdk/upstream/emscripten/tools/file_packager.py b/emsdk/upstream/emscripten/tools/file_packager.py
|
||||
index cd70b4b7d..ace6d6b2c 100755
|
||||
--- a/emsdk/upstream/emscripten/tools/file_packager.py
|
||||
+++ b/emsdk/upstream/emscripten/tools/file_packager.py
|
||||
@@ -525,14 +525,12 @@ def main():
|
||||
remote_package_size = os.path.getsize(package_name)
|
||||
remote_package_name = os.path.basename(package_name)
|
||||
ret += r'''
|
||||
- var PACKAGE_PATH;
|
||||
+ var PACKAGE_PATH = '';
|
||||
if (typeof window === 'object') {
|
||||
PACKAGE_PATH = window['encodeURIComponent'](window.location.pathname.toString().substring(0, window.location.pathname.toString().lastIndexOf('/')) + '/');
|
||||
- } else if (typeof location !== 'undefined') {
|
||||
- // worker
|
||||
+ } else if (typeof process === 'undefined' && typeof location !== 'undefined') {
|
||||
+ // web worker
|
||||
PACKAGE_PATH = encodeURIComponent(location.pathname.toString().substring(0, location.pathname.toString().lastIndexOf('/')) + '/');
|
||||
- } else {
|
||||
- throw 'using preloaded data can only be done on a web page or in a web worker';
|
||||
}
|
||||
var PACKAGE_NAME = '%s';
|
||||
var REMOTE_PACKAGE_BASE = '%s';
|
||||
@@ -716,6 +714,16 @@ def main():
|
||||
|
||||
ret += r'''
|
||||
function fetchRemotePackage(packageName, packageSize, callback, errback) {
|
||||
+ if (typeof process === 'object') {
|
||||
+ require('fs').readFile(packageName, function(err, contents) {
|
||||
+ if (err) {
|
||||
+ errback(err);
|
||||
+ } else {
|
||||
+ callback(contents.buffer);
|
||||
+ }
|
||||
+ });
|
||||
+ return;
|
||||
+ }
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', packageName, true);
|
||||
xhr.responseType = 'arraybuffer';
|
|
@ -9,8 +9,17 @@ let baseURL;
|
|||
*/
|
||||
export async function initializePackageIndex(indexURL) {
|
||||
baseURL = indexURL;
|
||||
let response = await fetch(`${indexURL}packages.json`);
|
||||
Module.packages = await response.json();
|
||||
if (typeof process !== "undefined" && process.release.name !== "undefined") {
|
||||
const fs = await import("fs");
|
||||
fs.readFile(`${indexURL}packages.json`, (err, data) => {
|
||||
if (err) throw err;
|
||||
let response = JSON.parse(data);
|
||||
Module.packages = response;
|
||||
});
|
||||
} else {
|
||||
let response = await fetch(`${indexURL}packages.json`);
|
||||
Module.packages = await response.json();
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -12,6 +12,9 @@
|
|||
},
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "mocha"
|
||||
"test": "mocha --timeout 15000"
|
||||
},
|
||||
"dependencies": {
|
||||
"node-fetch": "^2.6.1"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
import assert from "assert";
|
||||
import { loadPyodide } from "../pyodide.js";
|
||||
|
||||
import fetch from "node-fetch";
|
||||
|
||||
describe("Pyodide", () => {
|
||||
let pyodide;
|
||||
it("loadPyodide", async () => {
|
||||
pyodide = await loadPyodide({ indexURL: "../../build/" });
|
||||
});
|
||||
it("runPython", async () => {
|
||||
let res = pyodide.runPython("1+1");
|
||||
assert.equal(res, 2);
|
||||
});
|
||||
it("loadPackage", async () => {
|
||||
await pyodide.loadPackage(["micropip"]);
|
||||
});
|
||||
it("micropip.install", async () => {
|
||||
// TODO: micropip currently requires a globally defined fetch function
|
||||
global.fetch = fetch;
|
||||
await pyodide.runPythonAsync(
|
||||
'import micropip; await micropip.install("snowballstemmer")'
|
||||
);
|
||||
let res = pyodide.runPython(`
|
||||
import snowballstemmer
|
||||
len(snowballstemmer.stemmer('english').stemWords(['A', 'node', 'test']))
|
||||
`);
|
||||
assert.equal(res, 3);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue