~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
203 lines · 8.3 KB Raw History
// The services both sides forward requests to (spec/PROXY.md): HTTP servers
// on localhost that record every request they get and answer as the step
// running says, with the DID documents that name them.

import { createServer } from 'node:http'
import { Secp256k1Keypair } from '@atproto/crypto'
import * as plc from '@did-plc/lib'
import { noteAuth } from './observe.mjs'

/// The CDN pattern both sides make image URLs with.
export const CDN_PATTERN = 'http://cdn.pds-harness.test/%s/%s/%s'

/// The services by name, each `{name, port, url, did}`; `did` is set for
/// those that have one. Set by `startAppView` and `startServices`:
///
/// - `appview`, a did:web with `#bsky_appview` and `#bsky_notif`, both it;
/// - `chat`, a did:plc with `#bsky_chat`; its own URL serves no DID document;
/// - `elsewhere`, a did:web with services that are it, by other URLs, and
///   not it (`ELSEWHERE` below);
/// - `feedgen` and `notif`, did:plcs with `#bsky_fg` and `#bsky_notif` at
///   elsewhere's URL, and `notifOther`, with `#bsky_notif` of another type;
/// - `idMismatch` and `untyped`, did:webs whose documents are not theirs or
///   not well formed.
export const services = {}

/// Every request the services got and have not been taken, in order:
/// `{service, method, path, headers, body}`, `body` a Buffer.
const got = []
/// How the services answer a request: `(request) => {status, headers, body}`,
/// or `{respond}`, a function given the response to answer as and when it
/// likes; by default `{}`, as between proxy steps, so the AppView the
/// reference falls back to (as for resolveHandle) answers no step's answer.
const ANSWER_EMPTY = () => ({ status: 200, headers: { 'content-type': 'application/json' }, body: '{}' })
let answerer = ANSWER_EMPTY
export const answerWith = (fn = ANSWER_EMPTY) => {
  answerer = fn
}
/// The requests the services got since the last time they were taken.
export const takeRequests = () => got.splice(0)

/// The services of `elsewhere`'s DID document, by id, as functions of its
/// URL and DID.
const ELSEWHERE = {
  atproto_labeler: (url) => url,
  bsky_x: (url) => url,
  BSKY_X: () => 'http://localhost:1',
  ip: (url) => url.replace('localhost', '127.0.0.1'),
  path: (url) => `${url}/base`,
  query: (url) => `${url}/?a=b`,
  fragment: (url) => `${url}#f`,
  ftp: (url) => url.replace('http:', 'ftp:'),
  upper: (url) => url.replace('http:', 'HTTP:'),
  object: (url) => ({ uri: url }),
  down: () => 'http://localhost:1',
}

const listen = (handler) =>
  new Promise((ok, fail) => {
    const s = createServer(handler)
    // Both IPv6 and IPv4, as `localhost` may be either.
    s.once('error', () => {
      const v4 = createServer(handler)
      v4.once('error', fail)
      v4.listen(0, '127.0.0.1', () => ok(v4))
    })
    s.listen({ port: 0, host: '::', ipv6Only: false }, () => ok(s))
  })

/// A service named `name` whose DID document, if `doc` is given, is
/// `doc(url, did)` at `/.well-known/did.json`, and which is a did:web.
async function service(name, doc) {
  const record = { name }
  const server = await listen((req, res) => {
    const chunks = []
    req.on('data', (d) => chunks.push(d))
    req.on('end', () => {
      if (req.url === '/.well-known/did.json') {
        if (!doc) {
          res.writeHead(404, { 'content-type': 'application/json' })
          return res.end('{"error":"NotFound"}')
        }
        res.writeHead(200, { 'content-type': 'application/json' })
        return res.end(JSON.stringify(doc(record.url, record.did)))
      }
      const request = { service: name, method: req.method, path: req.url, headers: req.headers, body: Buffer.concat(chunks) }
      got.push(request)
      // What a side forwards is not the harness's request, but the token it
      // signs for the service is a secret all the same (spec/OPERATIONS.md).
      noteAuth(req.headers.authorization)
      let a
      try {
        a = answerer(request)
      } catch (err) {
        a = { status: 599, headers: {}, body: String(err) }
      }
      if (typeof a.respond === 'function') return a.respond(res)
      res.writeHead(a.status, a.headers ?? {})
      res.end(req.method === 'HEAD' ? undefined : a.body ?? '')
    })
  })
  server.unref()
  record.port = server.address().port
  record.url = `http://localhost:${record.port}`
  if (doc) record.did = `did:web:localhost%3A${record.port}`
  services[name] = record
  return record
}

const document = (did, entries) => ({
  '@context': ['https://www.w3.org/ns/did/v1'],
  id: did,
  service: entries.map(([id, type, serviceEndpoint]) => ({ id, type, serviceEndpoint })),
})

/// Start the AppView, which the reference is configured with before it
/// starts. Answers it.
export const startAppView = () =>
  service('appview', (url, did) =>
    document(did, [
      ['#bsky_appview', 'BskyAppView', url],
      ['#bsky_notif', 'BskyNotificationService', url],
    ]),
  )

/// A did:plc registered with the directory at `plcUrl` with `entries`, as
/// `{id: {type, endpoint}}`.
async function plcService(plcUrl, entries) {
  const key = await Secp256k1Keypair.create()
  const op = await plc.signOperation({ type: 'plc_operation', rotationKeys: [key.did()], verificationMethods: {}, alsoKnownAs: [], services: entries, prev: null }, key)
  const did = await plc.didForCreateOp(op)
  await new plc.Client(plcUrl).sendOperation(did, op)
  return did
}

/// Start the other services, and register those with a did:plc with the
/// directory at `plcUrl`.
export async function startServices(plcUrl) {
  const chat = await service('chat')
  chat.did = await plcService(plcUrl, { bsky_chat: { type: 'BskyChatService', endpoint: chat.url } })
  const elsewhere = await service('elsewhere', (url, did) =>
    document(did, [...Object.entries(ELSEWHERE).map(([id, f]) => [`#${id}`, 'Elsewhere', f(url)]), [`${did}#full`, 'Elsewhere', url]]),
  )
  services.feedgen = { name: 'feedgen', did: await plcService(plcUrl, { bsky_fg: { type: 'BskyFeedGenerator', endpoint: elsewhere.url } }) }
  services.notif = { name: 'notif', did: await plcService(plcUrl, { bsky_notif: { type: 'BskyNotificationService', endpoint: elsewhere.url } }) }
  services.notifOther = { name: 'notifOther', did: await plcService(plcUrl, { bsky_notif: { type: 'Elsewhere', endpoint: elsewhere.url } }) }
  await service('idMismatch', (url) => document('did:web:example.com', [['#x', 'Elsewhere', url]]))
  await service('untyped', (url, did) => ({ id: did, service: [{ id: '#x', serviceEndpoint: url }] }))
}

/// A host of a check's own on localhost, apart from the services above: a
/// did:web, `did:web:localhost%3A<port>`, whose requests it counts and
/// answers as `answer(req, res)` does; by default its DID document at
/// `/.well-known/did.json`, naming itself as `#x` (type `X`), and `{}`
/// otherwise. Answers `{port, did, url, requests, close}`; `requests` is
/// every request it got, `{method, path}`.
export async function ownHost(answer) {
  const requests = []
  let h
  const server = createServer((req, res) => {
    requests.push({ method: req.method, path: req.url })
    req.resume()
    req.on('end', () => {
      if (answer) return answer(req, res, h)
      res.writeHead(200, { 'content-type': 'application/json' })
      res.end(JSON.stringify(req.url === '/.well-known/did.json' ? h.document() : {}))
    })
  })
  await new Promise((ok) => server.listen(0, '127.0.0.1', ok))
  const port = server.address().port
  h = {
    port,
    did: `did:web:localhost%3A${port}`,
    url: `http://localhost:${port}`,
    requests,
    document: () => document(`did:web:localhost%3A${port}`, [['#x', 'X', `http://localhost:${port}`]]),
    close: () => new Promise((ok) => { server.closeAllConnections?.(); server.close(() => ok()) }),
  }
  return h
}

/// A listener on 127.0.0.1 that takes connections and never answers:
/// `{port, did, connections, close}`, counting the connections it took.
export async function silentHost() {
  const { createServer: tcp } = await import('node:net')
  const sockets = new Set()
  let connections = 0
  const server = tcp((s) => {
    connections += 1
    sockets.add(s)
    s.on('error', () => {})
    s.on('close', () => sockets.delete(s))
  })
  await new Promise((ok) => server.listen(0, '127.0.0.1', ok))
  const port = server.address().port
  return {
    port,
    did: `did:web:localhost%3A${port}`,
    get connections() {
      return connections
    },
    close: () => new Promise((ok) => { for (const s of sockets) s.destroy(); server.close(() => ok()) }),
  }
}