Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081import { config } from "./config";import { get, partsOf, until, xrpc } from "./net";
// listRecordVersions 400s on anything outside knot_cobs::social_collectionsconst COBS = new Set([ "org.tangled.track.ticket", "org.tangled.feed.comment", "org.tangled.feed.reaction", "org.tangled.label.definition", "org.tangled.review.patch", "org.tangled.repo.manifest"]);
export const isCob = (uri: string) => COBS.has(partsOf(uri).collection);
export interface Version { cid?: string; changeId: string; editor: string;}
export const versionsOf = async (uri: string) => { const { status, body } = await get( xrpc(config.knotUrl, "org.tangled.repo.listRecordVersions", { uri, limit: "500" }) ); return { status, body, versions: (status === 200 ? body.versions : []) as Version[] };};
const listed = (versions: Version[]) => versions.map((version) => `${version.cid ?? "erased"} by ${version.editor}`).join(", ") || "no versions";
// no uri before a create, and a chosen rkey like a label slug 404sexport const versionsBefore = async (uri?: string): Promise<Version[]> => uri && isCob(uri) ? until( `the knot should list the versions of ${uri} before the write`, 10_000, async () => { const { status, body, versions } = await versionsOf(uri); return [ status === 200 || status === 404, `it answered ${status} ${JSON.stringify(body)}`, versions ]; } ) : [];
interface Expected { // left out for records the knot wrote itself, so the check skips it editor?: string; // without a read from before the write, only the newest version gets checked had?: Version[];}
// resolves with the versions so the git check reads the same chainexport const checkVersions = ( { uri, cid }: { uri: string; cid?: string }, { editor, had }: Expected) => { const count = had ? `${had.length + 1} versions` : "versions"; const by = editor ? ` by ${editor}` : ""; return until<Version[]>( `the knot should list ${count} of ${uri}, the newest ${cid ?? "erased"}${by}`, 10_000, async () => { const { status, body, versions } = await versionsOf(uri); if (status !== 200) return [false, `it answered ${status} ${JSON.stringify(body)}`]; const last = versions.at(-1); const grew = !had || (versions.length === had.length + 1 && had.every((version, at) => versions[at].changeId === version.changeId)); const newest = last?.cid === cid && (!editor || last?.editor === editor); const before = had?.length ? `, before the write it listed ${listed(had)}` : ""; return [grew && newest, `it lists ${listed(versions)}${before}`, versions]; } );};