// Requests: an XRPC call to one side, or the same request to both, and // their answers with what may differ between two servers taken out. import { isDeepStrictEqual } from 'node:util' import { COMMANDS } from './args.mjs' import { below, chance } from './random.mjs' import { ref, cand } from './processes.mjs' import { count, differ } from './report.mjs' import { accounts, didOf, noteKeys, tokenOf } from './state.mjs' import { viaTool, viaCommand, viaXrpc } from './via.mjs' import { withoutKem } from './kem.mjs' import { learn, noteSecret, walk } from './observe.mjs' /// A request's token on `side`: an account's, or the string given. const tokenOn = (side, token) => (token && typeof token === 'object' ? tokenOf(side, token) : token) /// An XRPC call to one side. `token` is an account, for its token on that /// side, or a string. With `--commands`, some of the server's calls go /// through the method's MCP tool, its CLI command, or `pds xrpc` instead, /// answered in the same form (spec/COMMANDS.md, "How it is checked"). export async function xrpc(side, nsid, request = {}) { request = { ...request, token: tokenOn(side, request.token) } let res if (COMMANDS && side === cand && chance(0.4)) res = await [viaTool, viaCommand, viaXrpc][below(3)](nsid, request) res ??= await xrpcHttp(side, nsid, request) noteKeys(side, nsid, request.body, res) if (side === cand) { walk(request.body) noteSecret(request.token) learn(res.body) } return res } export async function xrpcHttp(side, nsid, { params, body, token, raw, method } = {}) { token = tokenOn(side, token) const url = new URL(`${side.url}/xrpc/${nsid}`) // An array is the parameter once for each of its values. for (const [k, v] of Object.entries(params ?? {})) for (const x of Array.isArray(v) ? v : [v]) if (x !== undefined) url.searchParams.append(k, String(x)) const headers = {} if (token) headers.authorization = `Bearer ${token}` if (body !== undefined) headers['content-type'] = 'application/json' let res try { res = await fetch(url, { method: method ?? (body === undefined ? 'GET' : 'POST'), headers, body: body === undefined ? undefined : JSON.stringify(body) }) } catch (err) { return { status: 0, body: { error: `request failed: ${err.message}` } } } if (raw && res.ok) return { status: res.status, bytes: new Uint8Array(await res.arrayBuffer()) } const text = await res.text() let parsed = null try { parsed = text ? JSON.parse(text) : null } catch { parsed = { unparsed: text.slice(0, 200) } } return { status: res.status, body: parsed } } const DID_KEY = /did:key:z[1-9A-HJ-NP-Za-km-z]+/g const TOKENS = ['accessJwt', 'refreshJwt'] /// Each account's DID on `side` named alike on both sides, by the order the /// accounts were made, and `extra`'s `[did, name]` pairs too. const namesOn = (side, extra = []) => [...accounts.map((a) => [didOf(side, a), ``]), ...extra.filter(([d]) => typeof d === 'string')] /// A string with each account's DID, the side's URL and signing keys taken /// out. export function strip(side, s, extra) { for (const [did, name] of namesOn(side, extra)) s = s.split(did).join(name) for (const url of new Set([side.publicUrl, side.url])) s = s.split(url).join('') return s.replace(DID_KEY, '') } /// The answer with what may differ between the two servers taken out /// (spec/PDS.md). `extra` names DIDs not yet any account's, as /// `[did, name]`. The KEM key, which the `latest` reference has not, is /// taken out too, once held to the spec elsewhere: from a DID document /// (which checkDidDoc holds whole to the directory's) and from /// getRecommendedDidCredentials (which recommendedCredentials holds to /// the genesis operation). export function normalize(side, res, nsid, extra) { const walk = (v) => { if (typeof v === 'string') return strip(side, v, extra) if (Array.isArray(v)) return v.map((x) => walk(x)) if (v && typeof v === 'object') { if (Array.isArray(v.verificationMethod)) v = withoutKem(v) const out = {} for (const [k, x] of Object.entries(v)) { if (k === 'commit' && x && typeof x === 'object') out[k] = '' else if (k === 'cursor') out[k] = '' else if (TOKENS.includes(k) && typeof x === 'string') out[k] = '' else if (k === 'publicKeyMultibase') out[k] = '' else if (k === 'message' && 'error' in v) continue else out[k] = walk(x) } return out } return v } if (res.status >= 400 || res.status === 0) return { status: res.status, error: res.body?.error ?? null } // A method that answers nothing answers `{}` as a tool or command. let body = res.body ?? {} if (nsid === 'com.atproto.server.describeServer' && body && typeof body === 'object') { const { did, availableUserDomains, inviteCodeRequired } = body body = { did, availableUserDomains, inviteCodeRequired } } if (nsid === 'com.atproto.identity.getRecommendedDidCredentials' && body?.verificationMethods && typeof body.verificationMethods === 'object') { const { atproto_kem: _, ...verificationMethods } = body.verificationMethods body = { ...body, verificationMethods } } return { status: res.status, body: walk(body) } } /// A request as the report shows it: on the reference's side, with its DIDs /// named, and an account's token as whose it is. export function shown(request) { const r = typeof request === 'function' ? request(ref) : request const token = r.token && typeof r.token === 'object' ? `` : r.token return JSON.parse(strip(ref, JSON.stringify({ ...r, token }))) } /// Send one request to both and compare. `request` may be a function of the /// side, for what differs between them, such as a DID; its `token` an /// account, for its token on each side. Returns both raw answers. export async function both(kind, nsid, request) { const on = (side) => (typeof request === 'function' ? request(side) : request) const [a, b] = await Promise.all([xrpc(ref, nsid, on(ref)), xrpc(cand, nsid, on(cand))]) const [na, nb] = [normalize(ref, a, nsid), normalize(cand, b, nsid)] if (!isDeepStrictEqual(na, nb)) await differ(nsid, shown(request), na, nb) count(kind, a.status < 400) return [a, b] }