mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-08-29 22:57:14 +02:00
Run the desktop's model layer in the browser
The browser host now stores data through yaak-models compiled to wasm — the same queries, migrations, cascade rules, duplicate naming and first-run bootstrap the desktop and CLI use — instead of a TypeScript port of them. - crates/yaak-web: the wasm crate. boot() registers an IndexedDB-backed VFS and calls init_standalone; rpc(cmd, payload, label) answers the models_* commands via ClientDb and returns the model_writes it caused; blob get/put through blob_manager. Built like yaak-templates (pkg/ committed); the build script keeps pkg/ and says so when no wasm-capable clang is present, so a desktop bootstrap never depends on one. - models_ops moves from crates/yaak into yaak-models so the wasm crate can use it without the send engine. - The database lives in a SharedWorker (packages/platform/src/web/worker.ts): one process holds the data and pushes writes to every tab, as on the desktop. Where SharedWorker is missing or its script cannot be fetched, a dedicated worker guarded by a Web Lock takes over and a second tab is told so. The worker imports the wasm lazily so a tab's connect is answered instantly; the tab reconnects if it isn't. - packages/platform/src/web loses models.ts, schema.ts, db.ts and the BroadcastChannel; commands.ts forwards model commands to the worker and keeps the fixed answers and refusals. Verified in Chrome, dev and production builds: bootstrap, CRUD across every model type, serde defaults, engine-format ids, Rust copy naming, folder cascade with per-descendant events, single-event workspace delete, reload persistence, two tabs coherent, Send declined with a toast, 8/8 reloads rendering in ~100 ms.
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
const { execSync, spawnSync } = require("node:child_process");
|
||||
const fs = require("node:fs");
|
||||
const os = require("node:os");
|
||||
const path = require("node:path");
|
||||
|
||||
// Same shape as crates/yaak-templates/build-wasm.cjs, plus one wrinkle: this
|
||||
// crate links SQLite, and sqlite-wasm-rs compiles sqlite3.c to wasm at build
|
||||
// time. That needs a C compiler with a WebAssembly backend, which Apple's
|
||||
// clang is not. So the build looks for one, and when it finds none it keeps
|
||||
// the committed pkg/ and says so — desktop developers never need this crate
|
||||
// rebuilt, and failing their `npm run bootstrap` over it would be wrong.
|
||||
|
||||
if (process.env.SKIP_WASM_BUILD === "1") {
|
||||
console.log("Skipping wasm-pack build (SKIP_WASM_BUILD=1)");
|
||||
return;
|
||||
}
|
||||
|
||||
/** A clang that can emit wasm32, or null. */
|
||||
function findWasmClang() {
|
||||
const candidates = [
|
||||
process.env.CC_wasm32_unknown_unknown,
|
||||
"/opt/homebrew/opt/llvm/bin/clang", // Homebrew LLVM, Apple Silicon
|
||||
"/usr/local/opt/llvm/bin/clang", // Homebrew LLVM, Intel
|
||||
"clang", // Linux distros' clang usually has the backend built in
|
||||
].filter(Boolean);
|
||||
|
||||
for (const clang of candidates) {
|
||||
const probe = spawnSync(clang, ["--print-targets"], { encoding: "utf8" });
|
||||
if (probe.status === 0 && /\bwasm32\b/.test(probe.stdout)) return clang;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const clang = findWasmClang();
|
||||
if (clang == null) {
|
||||
console.log(
|
||||
[
|
||||
"yaak-web: no C compiler with a WebAssembly backend found; keeping the committed pkg/.",
|
||||
" To rebuild: install LLVM (macOS: `brew install llvm`) or point CC_wasm32_unknown_unknown at one.",
|
||||
].join("\n"),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// llvm-ar lives next to clang in every LLVM distribution
|
||||
const ar = path.join(path.dirname(clang), "llvm-ar");
|
||||
|
||||
// Remap machine-specific paths that rustc embeds into the binary (panic
|
||||
// location strings), so builds are reproducible across machines
|
||||
const sysroot = execSync("rustc --print sysroot").toString().trim();
|
||||
const cargoHome = process.env.CARGO_HOME ?? path.join(os.homedir(), ".cargo");
|
||||
|
||||
execSync("wasm-pack build --target bundler", {
|
||||
stdio: "inherit",
|
||||
cwd: __dirname,
|
||||
env: {
|
||||
...process.env,
|
||||
CC_wasm32_unknown_unknown: clang,
|
||||
AR_wasm32_unknown_unknown: fs.existsSync(ar) ? ar : (process.env.AR_wasm32_unknown_unknown ?? ""),
|
||||
RUSTFLAGS: `--remap-path-prefix=${cargoHome}=/cargo --remap-path-prefix=${sysroot}=/rustc`,
|
||||
},
|
||||
});
|
||||
|
||||
// Rewrite the generated entry to use Vite's ?init import style instead of
|
||||
// the ES Module Integration style that wasm-pack generates, which Vite/rolldown
|
||||
// does not support in production builds.
|
||||
const entry = path.join(__dirname, "pkg", "yaak_web.js");
|
||||
fs.writeFileSync(
|
||||
entry,
|
||||
[
|
||||
'import init from "./yaak_web_bg.wasm?init";',
|
||||
'export * from "./yaak_web_bg.js";',
|
||||
'import * as bg from "./yaak_web_bg.js";',
|
||||
'const instance = await init({ "./yaak_web_bg.js": bg });',
|
||||
"bg.__wbg_set_wasm(instance.exports);",
|
||||
"instance.exports.__wbindgen_start();",
|
||||
"",
|
||||
].join("\n"),
|
||||
);
|
||||
Reference in New Issue
Block a user