Something went wrong. Try again.
r/place on the atmosphere, targeting cloudflare's free tier
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788// Creates or updates a canvas record in your repo, using an app password.//// ATP_IDENTIFIER=you.bsky.social ATP_APP_PASSWORD=xxxx-xxxx-xxxx-xxxx \// pnpm create-canvas --rkey main --name "My canvas"//// Re-running with the same rkey updates the record (e.g. a new cooldown).
import { resolveTxt } from "node:dns/promises";import { parseArgs } from "node:util";import { CANVAS_NSID, type CanvasRecord, isCanvasRecord } from "../src/shared/lexicon.ts";import { AtprotoClient } from "../src/shared/atproto.ts";
// The 16-color 2017 r/place palette.const DEFAULT_PALETTE = [ "#ffffff", "#e4e4e4", "#888888", "#222222", "#ffa7d1", "#e50000", "#e59500", "#a06a42", "#e5d900", "#94e044", "#02be01", "#00d3dd", "#0083c7", "#0000ea", "#cf6ee4", "#820080",];
const { values } = parseArgs({ options: { rkey: { type: "string", default: "main" }, name: { type: "string", default: "atplace" }, width: { type: "string", default: "1000" }, height: { type: "string", default: "1000" }, cooldown: { type: "string", default: "300" }, palette: { type: "string", default: DEFAULT_PALETTE.join(",") }, "starts-at": { type: "string" }, "ends-at": { type: "string" }, },});
const identifier = requireEnv("ATP_IDENTIFIER");const password = requireEnv("ATP_APP_PASSWORD");// Log in at the account's own PDS; ATP_SERVICE overrides the lookup.const service = process.env.ATP_SERVICE ?? (await new AtprotoClient().resolvePds(await resolveDid(identifier)));
const record: CanvasRecord = { $type: CANVAS_NSID, name: values.name, width: Number(values.width), height: Number(values.height), palette: values.palette.split(","), cooldownSeconds: Number(values.cooldown), startsAt: values["starts-at"], endsAt: values["ends-at"],};if (!isCanvasRecord(record)) throw new Error("invalid canvas options");
const session = (await xrpc("com.atproto.server.createSession", { identifier, password })) as { did: string; accessJwt: string;};const { uri } = (await xrpc( "com.atproto.repo.putRecord", { repo: session.did, collection: CANVAS_NSID, rkey: values.rkey, record }, session.accessJwt,)) as { uri: string };
console.log(`Canvas record: ${uri}`);console.log("Add it to CANVAS_ALLOWLIST in wrangler.jsonc (or .dev.vars) to serve it.");
async function xrpc(method: string, body: unknown, token?: string): Promise<unknown> { const res = await fetch(`${service}/xrpc/${method}`, { method: "POST", headers: { "content-type": "application/json", ...(token && { authorization: `Bearer ${token}` }) }, body: JSON.stringify(body), }); if (!res.ok) throw new Error(`${method} failed: ${res.status} ${await res.text()}`); return res.json();}
// Accepts a DID or a handle; handles resolve via DNS, then .well-known.async function resolveDid(identifier: string): Promise<string> { if (identifier.startsWith("did:")) return identifier; const txt = await resolveTxt(`_atproto.${identifier}`).catch(() => []); const fromDns = txt.flat().find((s) => s.startsWith("did="))?.slice(4); if (fromDns) return fromDns; const res = await fetch(`https://${identifier}/.well-known/atproto-did`); if (!res.ok) throw new Error(`could not resolve handle ${identifier}`); return (await res.text()).trim();}
function requireEnv(name: string): string { const value = process.env[name]; if (!value) throw new Error(`${name} is required`); return value;}