-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild.js
More file actions
54 lines (46 loc) · 1.93 KB
/
build.js
File metadata and controls
54 lines (46 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { readFile, writeFile } from "node:fs/promises";
import { resolve, dirname } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
// We bypass ComponentizeJS's stubWasi step which strips WASI filesystem methods
// (openAt, readViaStream, etc.) from the output component. ComponentizeJS has
// no "filesystem" feature, so it stubs those out by default.
//
// Strategy: copy componentize.js next to the original (so relative imports
// resolve), patch the stubWasi call to a no-op, then import the patched copy.
const __dirname = dirname(fileURLToPath(import.meta.url));
const componentizeDir = resolve(
__dirname,
"node_modules/@bytecodealliance/componentize-js/src"
);
const originalPath = resolve(componentizeDir, "componentize.js");
const patchedPath = resolve(componentizeDir, "componentize-patched.js");
let src = await readFile(originalPath, "utf-8");
src = src.replace(
/const finalBin = stubWasi\(\s*bin,\s*features,\s*witWorld,[\s\S]*?worldName\s*\);/,
"const finalBin = bin; // PATCHED: skip stubWasi to keep all WASI filesystem methods"
);
await writeFile(patchedPath, src);
const { componentize } = await import(pathToFileURL(patchedPath));
// Use the shared WIT from the wasm_sandbox crate
const witPath = "../../wit/hyperlight-sandbox.wit";
const sourcePath = "sandbox_executor.js";
const outputPath = "js-sandbox.wasm";
async function build() {
console.log("Reading source...");
const source = await readFile(sourcePath, "utf-8");
console.log("Componentizing...");
const { component, imports } = await componentize(source, {
witPath,
worldName: "hyperlight:sandbox/sandbox",
});
console.log("Writing component...");
await writeFile(outputPath, component);
console.log(`Built ${outputPath} (${component.byteLength} bytes)`);
if (imports && imports.length > 0) {
console.log("Imports:", imports);
}
}
build().catch((err) => {
console.error("Build failed:", err);
process.exit(1);
});