Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157// The record set carried between runs. A full walk seeds it; Jetstream commits// update it; the dataset is rebuilt from it every day. It holds only records// that can affect a publication: publications, documents whose `site` is an// at-uri, and the subscription and recommend edges.//// Documents are stored after text analysis, not as raw records. A run never// needs the prose again, only the measurements the scoring tree reads.
import { gzipSync, gunzipSync } from 'node:zlib'
export const STATE_VERSION = 1
export function emptyState() { return { cursor: 0, methodVersion: undefined, fullAt: undefined, updatedAt: undefined, looseDocuments: 0, looseByDid: new Map(), repoCollections: new Map(), publications: new Map(), documents: new Map(), subscriptions: new Map(), recommends: new Map(), retry: new Map(), unavailable: new Map(), truncated: new Set(), }}
export function serializeState(state) { return { version: STATE_VERSION, methodVersion: state.methodVersion, cursor: state.cursor ?? 0, fullAt: state.fullAt, updatedAt: state.updatedAt, looseDocuments: state.looseDocuments ?? 0, loose: [...(state.looseByDid ?? new Map())], repositories: [...state.repoCollections].map(([did, kinds]) => [did, [...kinds]]), publications: [...state.publications.values()], documents: [...state.documents.values()], subscriptions: edgesToArray(state.subscriptions), recommends: edgesToArray(state.recommends), retry: [...state.retry].map(([did, kinds]) => [did, [...kinds]]), unavailable: [...state.unavailable], truncated: [...state.truncated], }}
export function deserializeState(raw) { if (!raw || raw.version !== STATE_VERSION) { throw new Error(`unsupported panoptiblog state version: ${raw?.version ?? 'missing'}`) } const state = emptyState() state.cursor = Number.isFinite(raw.cursor) ? raw.cursor : 0 state.methodVersion = raw.methodVersion state.fullAt = raw.fullAt state.updatedAt = raw.updatedAt state.looseDocuments = Number.isFinite(raw.looseDocuments) ? raw.looseDocuments : 0 for (const [did, count] of raw.loose ?? []) { if (typeof did === 'string' && Number.isFinite(count)) state.looseByDid.set(did, count) } for (const [did, kinds] of raw.repositories ?? []) { if (typeof did === 'string' && Array.isArray(kinds)) state.repoCollections.set(did, new Set(kinds)) } for (const publication of raw.publications ?? []) { if (typeof publication?.uri === 'string') state.publications.set(publication.uri, publication) } for (const document of raw.documents ?? []) { if (typeof document?.uri === 'string') state.documents.set(document.uri, document) } state.subscriptions = edgesFromArray(raw.subscriptions) state.recommends = edgesFromArray(raw.recommends) for (const [did, kinds] of raw.retry ?? []) { if (typeof did === 'string' && Array.isArray(kinds)) state.retry.set(did, new Set(kinds)) } for (const [did, message] of raw.unavailable ?? []) { if (typeof did === 'string') state.unavailable.set(did, String(message ?? 'skipped')) } for (const did of raw.truncated ?? []) { if (typeof did === 'string') state.truncated.add(did) } return state}
export function gzipState(state) { return gzipSync(Buffer.from(JSON.stringify(serializeState(state))))}
export function gunzipState(body) { return deserializeState(JSON.parse(gunzipSync(body).toString('utf8')))}
function edgesToArray(edges) { const rows = [] for (const [target, linkers] of edges ?? []) { for (const [linker, createdAt] of linkers) rows.push([target, linker, createdAt ?? null]) } return rows}
function edgesFromArray(rows) { const edges = new Map() for (const [target, linker, createdAt] of rows ?? []) { if (typeof target !== 'string' || typeof linker !== 'string') continue let linkers = edges.get(target) if (!linkers) edges.set(target, (linkers = new Map())) linkers.set(linker, createdAt ?? undefined) } return edges}
/** Documents grouped by the publication their `site` points at. */export function groupDocuments(publications, documents) { const byPublication = new Map() const documentOwners = new Map() let linked = 0 let orphans = 0 for (const document of documents.values()) { documentOwners.set(document.uri, document.did) if (!document.siteUri) continue if (!publications.has(document.siteUri)) { orphans++ continue } linked++ let list = byPublication.get(document.siteUri) if (!list) byPublication.set(document.siteUri, (list = [])) list.push(document) } return { byPublication, documentOwners, linked, orphans }}
/** The repositories that hold each collection, as the public dataset reports it. */export function discoveryCounts(repoCollections) { const counts = { publication: 0, document: 0, subscription: 0, recommend: 0 } for (const kinds of repoCollections.values()) { for (const kind of kinds) if (kind in counts) counts[kind]++ } return counts}
/** Set membership for one repository, without dropping other collections it holds. */export function setRepoKinds(repoCollections, did, kind, present) { let kinds = repoCollections.get(did) if (present) { if (!kinds) repoCollections.set(did, (kinds = new Set())) kinds.add(kind) return } if (!kinds) return kinds.delete(kind) if (kinds.size === 0) repoCollections.delete(did)}