Store typescript on PDS, run it on kasta
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192import type { DatabaseSync } from "node:sqlite";import { upsertUser, upsertKast, deleteKastsNotIn, type KastFile,} from "./db.ts";import { listRecords, getRecord } from "./atproto.ts";import { log } from "./log.ts";
export interface SyncResult { did: string; handle: string; synced: Array<{ name: string; url: string }>;}
/** * Sync all kasts for a user into the local DB. * Caller is responsible for resolving DID, handle, and pdsUrl beforehand. */export async function syncDid( db: DatabaseSync, did: string, handle: string, pdsUrl: string, baseUrl: string,): Promise<SyncResult> { log("INFO", "Sync: listing pointer records", { did, handle, pdsUrl }); const pointers = await listRecords(pdsUrl, did, "me.wilb.kasta.pointer"); log("INFO", "Sync: found pointers", { did, handle, count: pointers.length });
const synced: Array<{ name: string; url: string }> = []; const syncedNames: string[] = [];
for (const pointer of pointers) { const value = pointer.value as { name: string; active: { uri: string; cid: string }; };
log("INFO", "Sync: fetching code record", { did, kast: value.name, uri: value.active.uri, }); const codeRecord = await getRecord(pdsUrl, value.active.uri); const codeValue = codeRecord.value as { name: string; files: KastFile[]; entrypoint: string; };
log("INFO", "Sync: upserting kast", { did, handle, kast: value.name, entrypoint: codeValue.entrypoint, fileCount: codeValue.files.length, versionCid: codeRecord.cid, }); upsertKast(db, { did, handle, name: value.name, entrypoint: codeValue.entrypoint, files: codeValue.files, versionCid: codeRecord.cid, pointerCid: pointer.cid, });
syncedNames.push(value.name); const { protocol, host } = new URL(baseUrl); const encodedHandle = handle.replaceAll(".", "-"); synced.push({ name: value.name, url: `${protocol}//${encodedHandle}.${host}/${value.name}`, }); }
log("INFO", "Sync: pruning removed kasts", { did, handle, keeping: syncedNames, }); deleteKastsNotIn(db, did, syncedNames);
upsertUser(db, { did, handle, pdsUrl }); log("INFO", "Sync: upserted user record", { did, handle });
return { did, handle, synced };}