chadtmiller.com/monolith
An atproto PDS
0
0
// What the harness knows of the accounts and their repositories, as the
// steps change them.
/// The accounts, in the order they were made. Each is `{n, handle, email,
/// password, sides, ...}`: `n` names its DID on both sides alike (`<did:n>`);
/// an account without a password, or with an empty one, keeps no email, so
/// another account may have it; and `sides`, by side name, holds its `did`,
/// `token`, the `refresh` token of the session it came from, and signing
/// `key` on that side. `handles`, by side name, is every handle the side
/// gave it, in order, one for each `#identity` event. The rest is its
/// repository: the records that exist, by
/// `collection/rkey`, and their CIDs; each side's latest commit CID, and
/// those before; the last record each key was given, to write the same
/// again; the keys and record CIDs written since the last repository check;
/// the server's rev at the last check, and each side's, with the tree then,
/// for `getRepo since`; and the last TID it gave a record.
export const accounts = []
/// Every CID seen, for links and wrong swaps.
export const cids = []
export function addAccount({ handle, email, password }, sides) {
const acct = {
n: accounts.length + 1,
handle,
email,
password,
sides,
handles: { reference: [handle], server: [handle] },
records: new Map(),
heads: { reference: [], server: [] },
lastValues: new Map(),
written: { keys: new Set(), records: new Set() },
lastRev: '',
revs: { reference: null, server: null },
lastTid: '',
}
accounts.push(acct)
return acct
}
export const didOf = (side, acct) => acct.sides[side.name].did
export const tokenOf = (side, acct) => acct.sides[side.name].token
/// The account a `repo` or `did` names on `side`, by its DID or handle.
export const accountNamed = (side, repo) => accounts.find((a) => a.handle === repo || didOf(side, a) === repo)
/// The account whose token on `side` this is.
export const accountWithToken = (side, token) => (token ? accounts.find((a) => tokenOf(side, a) === token) : undefined)
export const recordCount = () => accounts.reduce((n, a) => n + a.records.size, 0)
export const exists = (acct, c, k) => acct.records.has(`${c}/${k}`)
export const noteCommit = (side, acct, res) => {
const c = res.body?.commit?.cid
if (c) acct.heads[side.name].push(c)
}
export function noteWrite(acct, collection, key, res) {
if (res.status >= 400) return
if (res.body?.cid) {
acct.records.set(`${collection}/${key}`, res.body.cid)
cids.push(res.body.cid)
}
}
let batchSerial = 0
/// A number no record value has had yet, to keep values apart.
export const nextBatch = () => ++batchSerial
const WRITES = ['com.atproto.repo.createRecord', 'com.atproto.repo.putRecord', 'com.atproto.repo.deleteRecord', 'com.atproto.repo.applyWrites']
/// Note, in the account the write names, the keys, as `collection/rkey`,
/// that it named, whether or not it landed, and the record CIDs it answered:
/// what `getRepo since` may have taken out of the tree and put back, or
/// written again, since the last repository check.
export function noteKeys(side, nsid, body, res) {
if (!WRITES.includes(nsid) || !body || typeof body !== 'object') return
const acct = accountNamed(side, body.repo)
if (!acct) return
const { written } = acct
const add = (c, k) => typeof c === 'string' && typeof k === 'string' && written.keys.add(`${c}/${k}`)
const tail = (uri) => (typeof uri === 'string' ? uri.split('/').pop() : undefined)
if (nsid.endsWith('applyWrites')) (Array.isArray(body.writes) ? body.writes : []).forEach((w, i) => add(w?.collection, w?.rkey ?? tail(res?.body?.results?.[i]?.uri)))
else add(body.collection, body.rkey ?? tail(res?.body?.uri))
for (const r of [res?.body, ...(Array.isArray(res?.body?.results) ? res.body.results : [])]) if (typeof r?.cid === 'string') written.records.add(r.cid)
}