Something went wrong. Try again.
Local-first knowledge base made for Typst and the Atmosphere. typbase.at
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138// Writes the `place.wisp.settings` record for a site deployed with wispctl.//// CI deploys files only. `wispctl deploy --spa`/`--directory` replaces the// whole settings record, which would drop the COOP/COEP headers the app needs// for cross-origin isolation, so this script writes that record instead. It// runs after the deploy and talks to the PDS directly with an app password.// The wisp firehose picks the change up like any other settings update.//// Usage: WISPCTL_APP_PASSWORD=... node scripts/ci/wisp-settings.mjs <handle> <site>//// `WISP_PDS_URL` skips handle and DID resolution against a known PDS.
const [, , handle, site] = process.argv;if (!handle || !site) { console.error("usage: wisp-settings.mjs <handle> <site>"); process.exit(1);}
const password = process.env.WISPCTL_APP_PASSWORD;if (!password) { console.error("WISPCTL_APP_PASSWORD is not set"); process.exit(1);}
// The three isolation headers apply site-wide. Cache-Control entries are// path-scoped and non-overlapping: a path that matches none of them keeps the// host default (10 minutes, revalidate), which is what the entry document// wants. Hashed build output never changes under its URL, so it can be// immutable. `sw.js` must revalidate or a deploy would not replace the worker.const headers = [ { name: "Cross-Origin-Opener-Policy", value: "same-origin" }, { name: "Cross-Origin-Embedder-Policy", value: "credentialless" }, { name: "X-Frame-Options", value: "SAMEORIGIN" }, { name: "Cache-Control", value: "public, max-age=31536000, immutable", path: "/_nuxt/*" }, { name: "Cache-Control", value: "public, max-age=604800, stale-while-revalidate=86400", path: "/fonts/*", }, { name: "Cache-Control", value: "public, max-age=604800, stale-while-revalidate=86400", path: "/icons/*", }, { name: "Cache-Control", value: "no-cache", path: "/sw.js" }, { name: "Cache-Control", value: "no-cache", path: "/site.webmanifest" },];
const record = { $type: "place.wisp.settings", cleanUrls: true, spaMode: "200.html", headers,};
const timeout = () => AbortSignal.timeout(15_000);
async function getJson(url) { const response = await fetch(url, { signal: timeout() }); if (!response.ok) { throw new Error(`${url} returned ${response.status}`); }
return response.json();}
/** handle -> DID through the public AppView. */async function resolveDid(input) { if (input.startsWith("did:")) return input;
const body = await getJson( `https://public.api.bsky.app/xrpc/com.atproto.identity.resolveHandle?handle=${encodeURIComponent(input)}`, );
return body.did;}
/** DID document -> the account's PDS endpoint. */async function resolvePds(did) { const url = did.startsWith("did:web:") ? `https://${did.slice("did:web:".length).replace(/:/g, "%3A")}/.well-known/did.json` : `https://plc.directory/${encodeURIComponent(did)}`; const doc = await getJson(url); const service = (doc.service ?? []).find( (entry) => entry.type === "AtprotoPersonalDataServer" || String(entry.id).endsWith("#atproto_pds"), ); if (!service?.serviceEndpoint) { throw new Error(`no atproto_pds service in the DID document for ${did}`); }
return String(service.serviceEndpoint).replace(/\/$/, "");}
async function callPds(pds, method, body, token) { const response = await fetch(`${pds}/xrpc/${method}`, { method: "POST", headers: { "content-type": "application/json", ...(token ? { authorization: `Bearer ${token}` } : {}), }, body: JSON.stringify(body), signal: timeout(), }); const json = await response.json().catch(() => null); if (!response.ok) { const detail = json?.message ?? json?.error ?? response.status; throw new Error(`${method} failed: ${detail}`); }
return json;}
const did = await resolveDid(handle).catch((cause) => { throw new Error(`could not resolve ${handle}: ${cause.message}`);});const pds = process.env.WISP_PDS_URL?.replace(/\/$/, "") || (await resolvePds(did));const session = await callPds(pds, "com.atproto.server.createSession", { identifier: handle, password,});if (!session?.accessJwt) { throw new Error("createSession returned no access token; check the app password");}const result = await callPds( pds, "com.atproto.repo.putRecord", { repo: did, collection: "place.wisp.settings", rkey: site, record, }, session.accessJwt,);
console.log(`wrote place.wisp.settings/${site} to ${pds} (${result.uri})`);