// OPFS contract shared by every fcweb consumer. // // The browser keeps exported geometry in real OPFS under the directory // `freecad-exports` (web/src/storage.ts). Node scripts cannot reach // navigator.storage, so this module mirrors the same directory layout on disk // and addresses files by the bare export name, exactly like the UI does. // // Unlike the case-local opfs-store.mjs this root is configurable, so the // toolkit does not depend on any teaching case directory. The default lives // inside the toolkit's own out/, which keeps selftest hermetic. import fs from "node:fs"; import path from "node:path"; import { createHash } from "node:crypto"; import { fileURLToPath } from "node:url"; const here = path.dirname(fileURLToPath(import.meta.url)); export const EXPORT_DIRECTORY = "freecad-exports"; export const DEFAULT_ROOT = path.join(here, "out", "opfs", EXPORT_DIRECTORY); let root = DEFAULT_ROOT; /** Point the store at another directory (absolute path). Returns the new root. */ export function configureOpfs({ root: next }) { if (!next) throw new Error("configureOpfs requires { root }"); root = path.resolve(next); return root; } export function storeRoot() { return root; } /** * A bare export name, no directory part, with an extension. * Rejecting separators is what keeps `../x.brep` from escaping the store. */ export function safeName(name) { if (typeof name !== "string") throw new Error(`invalid export name: ${String(name)}`); if (name.includes("/") || name.includes("\\") || name.includes("\0")) throw new Error(`invalid export name: ${name}`); const base = name.split(/[\\/]/).pop() || ""; if (base === "." || base === "..") throw new Error(`invalid export name: ${name}`); if (!/^[A-Za-z0-9._-]+$/.test(base)) throw new Error(`invalid export name: ${name}`); if (!/\.[A-Za-z0-9]+$/.test(base)) throw new Error(`invalid export name: ${name}`); return base; } export function writeExportFile(name, bytes) { const file = safeName(name); fs.mkdirSync(root, { recursive: true }); const target = path.join(root, file); fs.writeFileSync(target, Buffer.from(bytes)); return { name: file, path: target, bytes: fs.statSync(target).size }; } export function readExportFile(name) { const file = safeName(name); const target = path.join(root, file); if (!fs.existsSync(target)) throw new Error(`export missing from OPFS store: ${file}`); return new Uint8Array(fs.readFileSync(target)); } export function hasExportFile(name) { return fs.existsSync(path.join(root, safeName(name))); } export function listExportFiles() { if (!fs.existsSync(root)) return []; return fs.readdirSync(root).sort(); } export function sha256(bytes) { return createHash("sha256").update(Buffer.from(bytes)).digest("hex"); } /** Write bytes and report size + digest in one call. */ export function storeBytes(name, bytes) { const stored = writeExportFile(name, bytes); return { name: stored.name, path: stored.path, bytes: stored.bytes, sha256: sha256(bytes) }; } /** * Persist a JSON evidence record into the store and next to it on disk, so a * report can be read without knowing the OPFS mirror location. */ export function evidence(name, value) { const json = JSON.stringify(value, null, 2); const stored = storeBytes(name, json); const local = path.join(path.dirname(root), `evidence-${safeName(name)}`); fs.mkdirSync(path.dirname(local), { recursive: true }); fs.writeFileSync(local, json); return { ...stored, local }; } /** Directory the OPFS mirror sits in, for evidence and trace side files. */ export function outRoot() { return path.dirname(root); }