// KEYSTONE SPIKE — prove atcute's Client({handler}) seam can be backed by OUR OWN transport,
// against a REAL hyper-proto-peer repo (wj62). This is the single biggest architectural bet in
// DESIGN.md §4: if a custom handler drives an atcute client, the whole favela client thesis holds.
//
// The handler contract (verified from @atcute/client dist): `(pathname, init) => Promise<Response>`.
// So the favela "P2P XRPC handler" just parses `/xrpc/<method>?<params>` and returns a Response.
//
// >>> In favela the handler does a Hyperswarm + atproto-store read-view read of the DID's repo —
// EXACTLY what atproto-bridge PeerFront already does live every day. For THIS spike we satisfy
// the read from the live membrane over HTTP, purely to prove atcute parses real wj62 records
// through the pluggable seam. Swapping HTTP -> P2P is a change to the marked SWAP POINT only. <<<
import { Client, ok } from '@atcute/client'
const WJ62 = 'did:plc:wj62ebbusln6gxo5azo6ogzo'
const RKEY = '3mqkrr6bnh222' // a real wj62 post authored this session
let handlerCalls = 0
const p2pHandler = async (pathname, init) => {
handlerCalls++
const method = pathname.replace(/^\/xrpc\//, '').split('?')[0]
console.log(` [our handler] XRPC ${init?.method || 'GET'} ${method}`)
// ================= SWAP POINT =================
// favela: read the DID's repo over Hyperswarm via atproto-store read-view (PeerFront pattern).
// spike: read from the live membrane over HTTP to prove the seam with real data.
return fetch('https://pds.7rnx.net' + pathname, init)
// =============================================
}
const client = new Client({ handler: p2pHandler })
console.log('atcute Client built with a CUSTOM handler. Driving real XRPC reads of wj62...\n')
const desc = await ok(client.get('com.atproto.repo.describeRepo', { params: { repo: WJ62 } }))
console.log('describeRepo ->', { did: desc.did, handle: desc.handle, collections: desc.collections })
const rec = await ok(client.get('com.atproto.repo.getRecord', {
params: { repo: WJ62, collection: 'app.bsky.feed.post', rkey: RKEY }
}))
console.log('getRecord ->', JSON.stringify(rec.value?.text || rec).slice(0, 70))
const list = await ok(client.get('com.atproto.repo.listRecords', {
params: { repo: WJ62, collection: 'app.bsky.feed.post', limit: 3 }
}))
console.log('listRecords ->', list.records.length, 'records; newest:', JSON.stringify(list.records[0]?.value?.text || '').slice(0, 50))
console.log(`\nKEYSTONE OK: atcute drove ${handlerCalls} XRPC calls through OUR handler against real wj62 data.`)
console.log('Next: replace the SWAP POINT with a Hyperswarm/atproto-store read-view read (PeerFront pattern).')