@mayflowergmbh/wasmsh-pyodide 0.5.2 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
Binary file
@@ -1,10 +1,18 @@
1
1
  import { execFileSync } from "node:child_process";
2
2
  import { existsSync, readFileSync } from "node:fs";
3
+ import { createRequire } from "node:module";
3
4
  import { resolve } from "node:path";
4
5
  import { pathToFileURL } from "node:url";
5
6
 
6
7
  import { createRuntimeBridge } from "./runtime-bridge.mjs";
7
8
 
9
+ // Deno's Node compat layer exposes `process` (so Emscripten detects
10
+ // ENVIRONMENT_IS_NODE) but does not provide CJS globals in ESM context.
11
+ // pyodide.asm.js uses `require("fs")` in its Node path.
12
+ if (typeof globalThis.require === "undefined") {
13
+ globalThis.require = createRequire(import.meta.url);
14
+ }
15
+
8
16
  const fetchHelperPath = resolve(
9
17
  new URL(".", import.meta.url).pathname,
10
18
  "fetch-helper.mjs",
@@ -15,6 +23,8 @@ const fetchHelperPath = resolve(
15
23
  * fetch-helper.mjs. Request data is passed via stdin (no argv size limits).
16
24
  * Returns a JSON object with {status, headers, body_base64}.
17
25
  */
26
+ const IS_DENO = typeof globalThis.Deno !== "undefined";
27
+
18
28
  export function syncHttpFetchNode(url, method, headersJson, bodyBase64, followRedirects) {
19
29
  const input = JSON.stringify({
20
30
  url,
@@ -24,9 +34,15 @@ export function syncHttpFetchNode(url, method, headersJson, bodyBase64, followRe
24
34
  follow_redirects: Boolean(followRedirects),
25
35
  });
26
36
  try {
37
+ // Under Deno, the fetch-helper subprocess needs --allow-net to make
38
+ // outbound requests. Extract the hostname and grant only that host.
39
+ const host = new URL(url).hostname;
40
+ const args = IS_DENO
41
+ ? ["run", `--allow-net=${host}`, fetchHelperPath]
42
+ : [fetchHelperPath];
27
43
  const out = execFileSync(
28
44
  process.execPath,
29
- [fetchHelperPath],
45
+ args,
30
46
  { timeout: 30000, encoding: "utf-8", input, stdio: ["pipe", "pipe", "ignore"] },
31
47
  );
32
48
  return JSON.parse(out);
@@ -66,6 +82,22 @@ let _nodeModuleRef = null;
66
82
  * stubs and sentinel imports, then restore it after boot.
67
83
  */
68
84
  export async function createFullModule(distDir) {
85
+ // Polyfill __dirname/__filename for Deno — pyodide.asm.js needs them to
86
+ // resolve its own location. Must point to the assets dir, not this module.
87
+ const savedDirname = globalThis.__dirname;
88
+ const savedFilename = globalThis.__filename;
89
+ globalThis.__dirname = distDir;
90
+ globalThis.__filename = resolve(distDir, "pyodide.asm.js");
91
+
92
+ // Under Deno, pyodide.mjs's nodeLoadScript uses import() which loads
93
+ // pyodide.asm.js as ESM — but the glue is CJS and calls require("fs").
94
+ // Pre-load it via createRequire so _createPyodideModule is already
95
+ // defined as a global, skipping nodeLoadScript entirely.
96
+ if (IS_DENO && typeof globalThis._createPyodideModule === "undefined") {
97
+ const denoRequire = createRequire(resolve(distDir, "package.json"));
98
+ denoRequire("./pyodide.asm.js");
99
+ }
100
+
69
101
  // Import loadPyodide from the packaged pyodide.mjs
70
102
  const pyodideMjs = resolve(distDir, "pyodide.mjs");
71
103
  const { loadPyodide } = await import(pathToFileURL(pyodideMjs).href);
@@ -107,8 +139,10 @@ export async function createFullModule(distDir) {
107
139
  stderr: () => {},
108
140
  });
109
141
  } finally {
110
- // Restore original WebAssembly.instantiate
142
+ // Restore original WebAssembly.instantiate and CJS globals
111
143
  WebAssembly.instantiate = origInstantiate;
144
+ globalThis.__dirname = savedDirname;
145
+ globalThis.__filename = savedFilename;
112
146
  }
113
147
 
114
148
  // The underlying Emscripten module is accessible via pyodide._module
package/node-host.mjs CHANGED
@@ -1,7 +1,13 @@
1
+ import { createRequire } from "node:module";
1
2
  import { dirname, resolve } from "node:path";
2
3
  import readline from "node:readline";
3
4
  import { fileURLToPath } from "node:url";
4
5
 
6
+ // Polyfill CJS globals for Deno — Emscripten's pyodide.asm.js expects them.
7
+ if (typeof globalThis.require === "undefined") {
8
+ globalThis.require = createRequire(import.meta.url);
9
+ }
10
+
5
11
  import { isHostAllowed } from "./lib/allowlist.mjs";
6
12
  import { createFullModule } from "./lib/node-module.mjs";
7
13
  import {
@@ -42,6 +48,12 @@ class WasmshNodeHost {
42
48
  if (this.module && this.runtimeBridge) {
43
49
  return;
44
50
  }
51
+ // Polyfill __dirname/__filename for Deno — pyodide.asm.js needs them
52
+ // to resolve its own location within the asset directory.
53
+ if (typeof globalThis.__dirname === "undefined") {
54
+ globalThis.__dirname = this.assetDir;
55
+ globalThis.__filename = resolve(this.assetDir, "pyodide.asm.js");
56
+ }
45
57
  this.module = await createFullModule(this.assetDir);
46
58
  this.runtimeBridge = createRuntimeBridge(this.module);
47
59
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mayflowergmbh/wasmsh-pyodide",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "description": "Pyodide-backed wasmsh runtime package for Node.js and browser workers",
5
5
  "type": "module",
6
6
  "main": "./index.js",