~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
87 lines · 6.0 KB Raw History
// pds.js, in Docker, from the image `node bench.mjs --setup` builds from
// its pinned commit (bench/impls.json): a server that hosts one account
// (spec/COMPAT.md, "One account"), made by `claim`.

import { spawnSync } from 'node:child_process'
import { randomBytes } from 'node:crypto'
import { join } from 'node:path'
import { Browser } from '../oauth.mjs'
import { dockerPort, fromDocker, imageId, pdsjsCreateArgs, pdsjsEnv, pins, shOk } from '../impls.mjs'
import { DOCKER_NEEDS, container, hidden, throughForms } from './common.mjs'

const PDSJS = pins.implementations['pds.js']
export default {
  about: `pds.js ${PDSJS.release} (${PDSJS.repo} at ${PDSJS.commit.slice(0, 7)}), its image built by \`node bench.mjs --setup\` with its own Dockerfile, in Docker`,
  pageApi: false,
  own: false,
  unsupported: {
    accounts: 'pds.js hosts one account (README.md, "one server, one identity"): createAccount only adopts a DID made elsewhere, for a migration (packages/core/src/handlers/xrpc-server.js:222-340), and a new one is made once, by POST /account/onboarding/create on an empty server (xrpc-server.js:453-615), which the adapter\'s claim calls',
    rotationKey: 'pds.js lists the account\'s signing key alone as its rotation key, held by the server (xrpc-server.js:540-551): the harness has no key to sign an operation of its own with',
    recoveryKey: 'the genesis operation\'s rotation keys are the account\'s signing key alone (xrpc-server.js:547): the reference\'s recovery key cannot be one',
    lexiconAuthority: 'the harness knows no setting of pds.js naming one DID every NSID\'s lexicon is resolved from, as PDS_LEXICON_AUTHORITY_DID does: it would look the harness\'s permission sets up in DNS, which the harness does not serve',
    hostname: 'pds.js tells one relay of itself (RELAY_URL), at most every 20 minutes (packages/core/src/pds.js:149, 1934-1950), not each crawler it is given; and it names its endpoint https://<PDS_HOSTNAME> (xrpc-server.js:546), so the hostname cannot be the URL the harness calls',
    handleDomains: 'pds.js\'s one handle domain is its hostname (xrpc-server.js:127): describeServer\'s availableUserDomains is [".localhost"], and `.test` and `.example` cannot be given',
    clientFetch: 'pds.js fetches a client document from the client_id\'s own URL (packages/core/src/oauth.js:635-646); it has no mapping of origins to the harness\'s web, as PDS_OAUTH_CLIENT_FETCH is',
    narrowScope: 'pds.js\'s consent form approves or denies the scope asked (packages/core/src/handlers/oauth.js:884-956): a narrower one cannot be chosen',
    adminPassword: 'pds.js has no com.atproto.admin.* methods (docs/endpoint-comparison.md)',
    mail: 'its container has no mail transport (docker/server.js), so it sends none',
    limits: 'pds.js has no rate limits to turn on',
    restart: 'a container keeps the environment it was made with: no restart with another',
  },
  port: dockerPort,
  readyMs: 120_000,
  async info() {
    return { version: PDSJS.release, source: `${PDSJS.repo} at ${PDSJS.commit}, image ${PDSJS.tag} (${await imageId(PDSJS.tag)}) built with its own ${PDSJS.dockerfile}`, config: 'PDS_HOSTNAME localhost; the rest its container\'s defaults (docs/configuration.md)' }
  },
  async start(o) {
    const image = await imageId(PDSJS.tag)
    if (!image) throw new Error(`pds.js: no Docker image ${PDSJS.tag}; ${DOCKER_NEEDS}`)
    const name = `compat-pdsjs-${process.pid}-${Date.now()}`
    const password = `pdsjs-${randomBytes(12).toString('hex')}`
    // The harness's setup, as far as pds.js takes it (docs/configuration.md):
    // the reference's hostname, the harness's PLC directory and AppView.
    // Its one account is made by `claim`, not here.
    const env = {
      ...pdsjsEnv({ hostname: 'localhost', plcUrl: o.plc, password }),
      ...(o.appView ? { APPVIEW_URL: fromDocker(o.appView.url), APPVIEW_DID: o.appView.did } : {}),
    }
    await shOk('docker', pdsjsCreateArgs({ name, port: o.port, env, image }))
    const url = `http://127.0.0.1:${o.port}`
    const json = (r) => r.json().catch(() => null)
    return {
      server: container(name, env, join(o.logDir, 'server.log'), join(o.logDir, 'server.err'), () => spawnSync('docker', ['rm', '-f', '-v', name])),
      url,
      // Its DID documents name https://<PDS_HOSTNAME>; its OAuth issuer is
      // the URL it is called at.
      publicUrl: 'https://localhost',
      issuer: url,
      recoveryKey: null,
      // The account's own, read from its genesis operation once it is made.
      rotationKey: null,
      plcSigner: null,
      admin: null,
      takeMail: () => [],
      password,
      /// The account, made as its onboarding page makes it (a same-origin
      /// POST of the server's password, the handle and the email), then a
      /// session of it by createSession.
      async claim({ handle, email }) {
        const made = await fetch(`${url}/account/onboarding/create`, { method: 'POST', headers: { 'content-type': 'application/json', origin: url }, body: JSON.stringify({ password, handle, email }) })
        const body = await json(made)
        if (made.status !== 200) return { status: made.status, body }
        const session = await fetch(`${url}/xrpc/com.atproto.server.createSession`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ identifier: body.did, password }) })
        return { status: session.status, body: await json(session) }
      },
    }
  },
  /// Its consent page (packages/core/src/oauth.js, the form at
  /// /oauth/authorize): `request_uri`, `client_id`, the account's password,
  /// and `action=approve` or `deny`, which answers a 302 to the client. It
  /// asks no identifier: the server has one account.
  async signIn(account, authorizeUrl, consent) {
    const b = new Browser()
    return throughForms(b, authorizeUrl, [
      (page) => ({ action: '/oauth/authorize', fields: { request_uri: hidden(page.text, 'request_uri'), client_id: hidden(page.text, 'client_id'), password: account.password, action: consent.approve ? 'approve' : 'deny' } }),
    ])
  },
}