chadtmiller.com/monolith
An atproto PDS
0
0
// Monolith: the harness as it always ran it, a process given Monolith's
// `PDS_*` environment (compare.mjs's header), its mail read from
// PDS_DEV_MAIL_DIR and sent to the harness's SMTP server (lib/smtp.mjs),
// its traces and metrics where the harness reads them, and its log kept in
// a directory of its own too, for the admin's reads.
import { mkdtempSync, readdirSync, readFileSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { Secp256k1Keypair } from '@atproto/crypto'
import { ADMIN_PASSWORD, serverEnv } from '../env.mjs'
import { noteSecret } from '../observe.mjs'
import { root, sh } from '../impls.mjs'
import { basic, child } from './common.mjs'
import { startSmtp } from '../smtp.mjs'
/// The server's PDS_DEV_MAIL_DIR: empty, the harness's own.
const MAIL_DIR = mkdtempSync(join(tmpdir(), 'pds-mail-'))
/// The emails Monolith wrote to MAIL_DIR since they were last taken, oldest
/// first, each as it wrote it: `{to, kind, token}` if it is right; a name
/// starting with `.` is not yet in place, and names sort in the order
/// written (spec/EMAIL.md, "The development sink"). Each address and token
/// is a secret its log must not hold.
function takeMailDir() {
const names = readdirSync(MAIL_DIR).filter((n) => n.endsWith('.json') && !n.startsWith('.')).sort()
return names.map((n) => {
const path = join(MAIL_DIR, n)
const text = readFileSync(path, 'utf8')
rmSync(path)
try {
const m = JSON.parse(text)
if (m && typeof m === 'object') for (const k of ['to', 'token']) noteSecret(m[k])
return m
} catch {
return { unparsed: text.slice(0, 200) }
}
})
}
/// What the server's traces go to, and how often: often, so that a run
/// need not wait long for them, with room for every span of a run, and an
/// export timeout of 3 s, so that a server waiting on its exports would
/// show it in every request while the receiver hangs.
export const OTEL_ENV = {
OTEL_SERVICE_NAME: 'pds-under-test',
OTEL_BSP_SCHEDULE_DELAY: '200',
OTEL_BSP_MAX_QUEUE_SIZE: '200000',
OTEL_BSP_EXPORT_TIMEOUT: '3000',
}
/// A value the receiver must be sent with each export, and the log must
/// never hold.
export const OTLP_KEY = 'otlp-key-5f1d0c3a9e7b4d2c'
/// The bytes the server's kept log may take: little, so that a run lets
/// lines go (spec/OPERATIONS.md, "How it is checked").
export const LOG_KEEP_BYTES = 262144
export default {
about: 'Monolith, this repository: the command after `--`, given the `PDS_*` environment compare.mjs\'s header lists',
pageApi: true,
own: true,
unsupported: {},
port: null,
async info() {
return { version: (await sh(`${root}target/release/pds`, ['--version'])) || '0.0.0 (workspace)', source: `this repository at ${await sh('git', ['rev-parse', '--short', 'HEAD'], root)}`, config: 'compare.mjs\'s header: every setting the harness has' }
},
async start(o) {
// The SMTP server the server sends each email through, signing in with
// a user and password that must be percent-encoded in its URL
// (spec/EMAIL.md, "SMTP").
const smtp = await startSmtp({ user: 'pds harness@mail', password: 'p:ss/w%rd#1', from: 'mailer@pds-harness.test' })
const { env, rotationKey } = await serverEnv({ port: o.port, dataDir: o.dataDir, plc: o.plc, recoveryKey: o.recoveryKey, crawlers: o.crawlers, appView: o.appView, clientFetch: o.clientFetch, handles: o.handles, limits: o.limits, invites: o.invites, backfillMs: o.backfillMs, cacheBytes: o.cacheBytes, lexiconAuthority: o.lexiconAuthority, blobstore: o.blobstore, smtp })
env.PDS_DEV_MAIL_DIR = MAIL_DIR
Object.assign(env, OTEL_ENV, { PDS_METRICS_PORT: String(o.metricsPort), OTEL_EXPORTER_OTLP_ENDPOINT: o.otlp.url, OTEL_EXPORTER_OTLP_HEADERS: `x-otlp-key=${OTLP_KEY}` })
Object.assign(env, { PDS_LOG_DIR: o.keptLogDir, PDS_LOG_KEEP_BYTES: String(LOG_KEEP_BYTES) })
return {
server: child('server', o.command, env, join(o.logDir, 'server.log'), join(o.logDir, 'server.err')),
url: `http://127.0.0.1:${o.port}`,
publicUrl: env.PDS_PUBLIC_URL,
recoveryKey: o.recoveryKey,
rotationKey,
plcSigner: await Secp256k1Keypair.import(env.PDS_PLC_ROTATION_KEY_HEX),
admin: basic('admin', ADMIN_PASSWORD),
takeMail: takeMailDir,
}
},
}