A snapshot of packages/ in the Weft repository; the official packages are published from these commits. Markup-Kit-Commit: dd1e6eb5270e62e0c8b6c202b3d01f0da6e00ddc
143 lines
6.7 KiB
JavaScript
143 lines
6.7 KiB
JavaScript
// 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** <script>bad()</script>"))
|
|
`,
|
|
expect: "<h1>Hello</h1>\n<ul>\n<li><input checked=\"\" disabled=\"\" type=\"checkbox\"> <strong>done</strong> <script>bad()</script></li>\n</ul>",
|
|
capabilities: "capabilities none",
|
|
},
|
|
stripe: {
|
|
main: `import { client } from "@weft/stripe"
|
|
import { signature, verify } from "@weft/stripe/webhooks"
|
|
import { log } from "std/log"
|
|
let payload = "{\\"id\\":\\"evt_1\\",\\"type\\":\\"checkout.session.completed\\",\\"created\\":1,\\"data\\":{\\"object\\":{}}}"
|
|
let event = try verify(payload, signature(payload, "whsec_local", 1700000000), "whsec_local", { now: 1700000000 })
|
|
log(event.type, client("sk_test_local").baseUrl)
|
|
`,
|
|
expect: "checkout.session.completed https://api.stripe.com",
|
|
capabilities: "capabilities http.fetch",
|
|
},
|
|
email: {
|
|
main: `import { memory, outbox, send } from "@weft/email"
|
|
import { toMime } from "@weft/email/mime"
|
|
import { document } from "@weft/email/template"
|
|
import { render } from "std/app"
|
|
import { log } from "std/log"
|
|
import Welcome from "./Welcome"
|
|
let body = document(render(Welcome), { title: "Hi" })
|
|
let sent = try await send(memory(), { from: "a@example.com", to: ["b@example.com"], subject: "Hi", html: body.html, text: body.text })
|
|
log(sent.provider, outbox().length, toMime(outbox()[0], { id: "x" }).includes("multipart/alternative"))
|
|
`,
|
|
files: {
|
|
"Welcome.weft": `import Button from "@weft/email/components/Button"
|
|
import H1 from "@weft/email/components/H1"
|
|
import P from "@weft/email/components/P"
|
|
---
|
|
<H1>Hi</H1>
|
|
<P>Welcome</P>
|
|
<Button url="https://example.com" label="Open" />
|
|
`,
|
|
},
|
|
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()
|
|
}
|