~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
147 lines · 6.7 KB Raw History
// What the adapters share (lib/adapters/index.mjs): the server as a
// process or a Docker container, a fresh rotation key, Basic
// authorization, and a browser's way through a sign-in form.

import { spawn, spawnSync } from 'node:child_process'
import { openSync, statSync } from 'node:fs'
import { Secp256k1Keypair } from '@atproto/crypto'
import { now } from '../observe.mjs'
import { sh } from '../impls.mjs'

const sizeOf = (path) => {
  try {
    return statSync(path).size
  } catch {
    return 0
  }
}

export const basic = (user, password) => `Basic ${Buffer.from(`${user}:${password}`).toString('base64')}`

/// A process the harness starts, kills and starts again, writing its
/// standard output to `logPath` and its standard error to `errPath`.
/// `p.env` is its environment, which a start may change; `p.ulimit`, if
/// set, the file descriptors it is started with at most. `p.lives` is each
/// start, `{started, env, outFrom, errFrom, ended, how, signalled, exited,
/// outTo, errTo}`: when, with what environment, where in each file its
/// output starts and ends, how it ended (`SIGKILL`, `SIGTERM`, or `exit` of
/// its own accord), and when it was sent the signal it ended by.
export function child(name, cmd, env, logPath, errPath) {
  const out = openSync(logPath, 'a')
  const err = openSync(errPath, 'a')
  const p = { name, logPath, errPath, proc: null, exited: null, env, ulimit: null, lives: [] }
  p.start = () => {
    p.exited = null
    const argv = p.ulimit ? ['sh', '-c', `ulimit -n ${p.ulimit} && exec "$@"`, 'sh', ...cmd] : cmd
    const life = { started: now(), env: { ...p.env }, outFrom: sizeOf(logPath), errFrom: sizeOf(errPath), ended: null, how: null, exited: null }
    p.lives.push(life)
    const self = spawn(argv[0], argv.slice(1), { env: { ...process.env, ...p.env }, stdio: ['ignore', out, err] })
    p.proc = self
    self.on('exit', (code, signal) => {
      Object.assign(life, { ended: now(), exited: { code, signal }, outTo: sizeOf(logPath), errTo: sizeOf(errPath) })
      life.how ??= 'exit'
      if (p.proc === self) p.exited = { code, signal }
    })
  }
  /// Send the process `signal`, and wait at most `ms` for it to exit.
  /// Answers how it exited, or null if it did not.
  p.stop = async (signal = 'SIGKILL', ms = Infinity) => {
    const dying = p.proc
    if (!dying || p.exited) return p.exited
    const life = p.lives.at(-1)
    life.how ??= signal
    life.signalled ??= now()
    const gone = new Promise((ok) => dying.once('exit', (code, sig) => ok({ code, signal: sig })))
    dying.kill(signal)
    return Promise.race([gone, ...(ms === Infinity ? [] : [new Promise((ok) => setTimeout(() => ok(null), ms))])])
  }
  p.kill = async () => void (await p.stop('SIGKILL'))
  /// At the end of the run.
  p.close = () => p.proc?.kill()
  return p
}

/// A server in the Docker container `name`, made already, as `child` is a
/// process: `start` starts the container, `stop` sends its process a
/// signal, and its log goes to `logPath` and `errPath` (`docker logs`).
/// `teardown` removes it, and whatever it needs beside it, at the end. Its
/// environment is fixed when it is made: `env` is what it was made with.
export function container(name, env, logPath, errPath, teardown) {
  const p = { name: 'server', container: name, logPath, errPath, proc: null, exited: null, env, ulimit: null, lives: [] }
  let ended = null
  p.start = () => {
    p.exited = null
    const life = { started: now(), env: { ...p.env }, outFrom: sizeOf(logPath), errFrom: sizeOf(errPath), ended: null, how: null, exited: null }
    p.lives.push(life)
    const since = new Date(Date.now() - 1000).toISOString()
    const r = spawnSync('docker', ['start', name], { encoding: 'utf8' })
    if (r.status !== 0) {
      Object.assign(life, { ended: now(), how: 'exit', exited: { code: r.status, signal: null } })
      p.exited = life.exited
      return
    }
    const logs = spawn('docker', ['logs', '--follow', '--since', since, name], { stdio: ['ignore', openSync(logPath, 'a'), openSync(errPath, 'a')] })
    const waiter = spawn('docker', ['wait', name], { stdio: ['ignore', 'pipe', 'ignore'] })
    let out = ''
    waiter.stdout.on('data', (d) => (out += d))
    ended = new Promise((ok) => waiter.on('exit', () => {
      const code = Number(out.trim())
      Object.assign(life, { ended: now(), exited: { code: Number.isNaN(code) ? null : code, signal: null }, outTo: sizeOf(logPath), errTo: sizeOf(errPath) })
      life.how ??= 'exit'
      p.exited = life.exited
      logs.kill()
      ok(life.exited)
    }))
    p.proc = { pid: null, kill: () => spawnSync('docker', ['kill', name]) }
  }
  p.stop = async (signal = 'SIGKILL', ms = Infinity) => {
    if (!ended || p.exited) return p.exited
    p.lives.at(-1).how ??= signal
    p.lives.at(-1).signalled ??= now()
    await sh('docker', ['kill', '--signal', signal, name])
    return Promise.race([ended, ...(ms === Infinity ? [] : [new Promise((ok) => setTimeout(() => ok(null), ms))])])
  }
  p.kill = async () => void (await p.stop('SIGKILL'))
  p.close = () => teardown()
  return p
}

/// A new secp256k1 rotation key, and its private key as hex.
export const signer = async () => {
  const rotation = await Secp256k1Keypair.create({ exportable: true })
  return { rotation, hex: Buffer.from(await rotation.export()).toString('hex') }
}

/// A browser's way through a sign-in form: GET `url` with `browser`, then
/// POST each form in turn, `fields(page)` answering the next form's action
/// and fields, until an answer is not a page to post from. Answers the last
/// answer. Each POST is a same-origin form submission.
export async function throughForms(browser, url, steps) {
  let page = await browser.navigate(url)
  for (const step of steps) {
    if (page.status !== 200) return page
    const next = step(page)
    if (!next) return page
    const origin = new URL(url).origin
    const target = new URL(next.action, url).href
    page = await browser.request(target, {
      method: 'POST',
      headers: { 'content-type': 'application/x-www-form-urlencoded', origin, referer: url, 'sec-fetch-mode': 'navigate', 'sec-fetch-dest': 'document', 'sec-fetch-site': 'same-origin' },
      body: new URLSearchParams(next.fields).toString(),
    })
  }
  return page
}

/// The value of the hidden input `name` of a page, or null.
export const hidden = (html, name) => {
  const unescape = (s) => s.replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/&#x27;/g, "'").replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&')
  for (const m of html.matchAll(/<input\b[^>]*>/gi)) {
    const tag = m[0]
    const n = /\bname="([^"]*)"/i.exec(tag)?.[1]
    if (n !== undefined && unescape(n) === name) return unescape(/\bvalue="([^"]*)"/i.exec(tag)?.[1] ?? '')
  }
  return null
}

export const DOCKER_NEEDS = 'Docker, and the image `node bench.mjs --setup` builds or pulls'