// Proves the official packages end to end against the registry app running locally: starts `apps/registry`, makes a // publish token and a signing key for the owner `weft`, publishes each package signed, then in a fresh app runs // `weft add`, checks it, runs it on the VM and builds it for the browser. Nothing leaves this machine. // // node packages/scripts/verify-local.mjs [markdown] [stripe] [email] import { spawn, spawnSync } from "node:child_process" import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs" import { dirname, join } from "node:path" import { fileURLToPath } from "node:url" const repo = join(dirname(fileURLToPath(import.meta.url)), "../..") const weft = process.env.WEFT_BIN ?? join(repo, "core/target/release/weft") const work = join(repo, "core/target/tools/verify") const admin = "local-operator-token" const port = 4873 + (process.pid % 1000) const registry = `http://127.0.0.1:${port}` // What a fresh app does with each package, and the line it must print. No network: these run offline. const apps = { markdown: { main: `import { toHtml } from "@weft/markdown" import { log } from "std/log" log(toHtml("# Hello\\n\\n- [x] **done** ")) `, expect: "
Welcome
`, }, expect: "memory 1 true", capabilities: "capabilities http.fetch", }, } rmSync(work, { recursive: true, force: true }) mkdirSync(work, { recursive: true }) const base = { ...process.env, WEFT_REGISTRY: registry, WEFT_CACHE: join(work, "cache") } delete base.WEFT_TOKEN delete base.WEFT_SIGNING_KEY function run(args, cwd, env = {}) { const r = spawnSync(weft, args, { cwd, env: { ...base, ...env }, encoding: "utf8" }) const out = `${r.stdout}${r.stderr}` if (r.status !== 0) throw new Error(`weft ${args.join(" ")} failed in ${cwd}:\n${out}`) return out } async function api(method, path, token, body) { const res = await fetch(`${registry}${path}`, { method, headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, body: JSON.stringify(body), }) const text = await res.text() if (!res.ok) throw new Error(`${method} ${path}: ${res.status} ${text}`) return JSON.parse(text) } const server = spawn(weft, ["run", "."], { cwd: join(repo, "apps/registry"), env: { ...base, PORT: String(port), HOST: "127.0.0.1", REGISTRY_DB: join(work, "registry.db"), REGISTRY_ADMIN_TOKEN: admin }, stdio: ["ignore", "ignore", "inherit"], }) try { for (let i = 0; ; i++) { const up = await fetch(`${registry}/config.json`).then((r) => r.ok, () => false) if (up) break if (i > 600) throw new Error("the registry didn't start") await new Promise((r) => setTimeout(r, 100)) } const { token } = await api("POST", "/v1/tokens", admin, { owners: ["weft"], label: "official packages" }) const keys = run(["publish", "--new-key"], work) const field = (label) => keys.split("\n").find((l) => l.startsWith(label)).split(" ").pop() const [secret, key] = [field("secret"), field("public")] // The operator's request, from this machine: a first key sent with an owner's token waits a day. const proof = run(["publish", "--endorse-key", key, "--owner", "weft"], work, { WEFT_SIGNING_KEY: secret }).trim() await api("PUT", "/v1/owners/weft/key", admin, { key, proof }) console.log(`registry ${registry}: owner \`weft\` holds signing key ${key.slice(0, 16)}…`) for (const name of process.argv.slice(2).length ? process.argv.slice(2) : Object.keys(apps)) { const dir = join(repo, "packages", name) const signing = { WEFT_TOKEN: token, WEFT_SIGNING_KEY: secret, WEFT_COMMIT: "local" } console.log(`\n== weft/${name}`) console.log(run(["publish", "--dry-run"], dir).trim().split("\n").filter((l) => /capabilit|dry run/.test(l)).join("\n")) console.log(run(["publish", "--sign"], dir, signing).trim()) // Without the key the registry refuses a new version: signing is what's protecting the owner. const app = join(work, `app-${name}`) mkdirSync(app, { recursive: true }) writeFileSync(join(app, "weft.toml"), 'main = "main.weft"\n\n[dependencies]\n') writeFileSync(join(app, "main.weft"), apps[name].main) for (const [file, source] of Object.entries(apps[name].files ?? {})) writeFileSync(join(app, file), source) const added = run(["add", `weft/${name}`], app) console.log(added.trim()) if (!added.includes(apps[name].capabilities)) throw new Error(`expected "${apps[name].capabilities}" in: ${added}`) run(["check", "."], app) const printed = run(["run", "."], app).trim() if (printed !== apps[name].expect) throw new Error(`weft run printed:\n${printed}\nexpected:\n${apps[name].expect}`) console.log("weft run: the expected output") run(["build", "--target", "js", "."], app) if (!existsSync(join(app, "build"))) throw new Error("weft build wrote nothing") console.log("weft build --target js: ok") run(["install", "--frozen"], app) console.log("weft install --frozen: ok") const page = await fetch(`${registry}/packages/weft/${name}`).then((r) => r.text()) if (!page.includes(`weft/${name}`)) throw new Error("the registry has no page for it") console.log(`generated docs: ${registry}/packages/weft/${name} (${page.length} bytes)`) } console.log("\nverified") } finally { server.kill() }