chadtmiller.com/group-host
atmosphere group host
0
0
just dev runs the console to use, over a world of its own
.knotless/workflows/e2e.yml+1−1
| @@ -18,7 +18,7 @@ on: | ||
push: {refs: [refs/heads/main]} | ||
pull: | ||
base: [refs/heads/main, refs/heads/feature/*] | ||
paths: [web/**, lexicons/**, crates/**, Cargo.toml, Cargo.lock, harness/e2e/**, harness/pds.mjs, harness/tls-proxy.mjs, harness/package.json, harness/package-lock.json, .knotless/workflows/e2e.yml, .knotless/uses.lock] | ||
paths: [web/**, lexicons/**, crates/**, Cargo.toml, Cargo.lock, harness/e2e/**, harness/world.mjs, harness/pds.mjs, harness/tls-proxy.mjs, harness/package.json, harness/package-lock.json, .knotless/workflows/e2e.yml, .knotless/uses.lock] | ||
cache: [target, /usr/local/cargo/registry, /usr/local/rustup, /opt/node, /opt/browsers, /opt/chromium-debs, web/node_modules, harness/node_modules] | ||
env: | ||
PLAYWRIGHT_BROWSERS_PATH: /opt/browsers | ||
docs/reference/cli.md+1−0
| @@ -49,6 +49,7 @@ usage. | ||
| `cov`, `cov-html` | the tests under the 100% gate, or their coverage as a page | | ||
| `lint` | formatting, clippy, the console's layers and svelte-check | | ||
| `lex` | the lexicons' checks | | ||
| `dev` | the console to use, over a world of its own: the reference PDS with alice, bob and harbor, the host, and the console at https://group-host.localtest.me ([development](../development.md#using-it)) | | ||
| `serve` | the host | | ||
| `web`, `web-dev` | the console built, or served as its source changes | | ||
| `e2e <args>` | the console's flows in Chromium over the reference PDS, as the `e2e` check runs them | | ||
docs/development.md+21−2
| @@ -6,10 +6,29 @@ for the lexicons. | ||
| ||
```sh | ||
just setup # the console's packages, and cargo-llvm-cov | ||
just serve # the host, on http://localhost:2600 | ||
just web-dev # the console, asking that host | ||
just dev # all of it, to use: see below | ||
just serve # the host alone, on http://localhost:2600 | ||
just web-dev # the console alone, asking that host | ||
``` | ||
| ||
## Using it | ||
| ||
`just dev` starts a world of its own (`harness/dev.mjs`): the reference | ||
PDS and a PLC directory with alice, bob and harbor on them, the host over | ||
them, and the console at https://group-host.localtest.me, with a group of | ||
alice's. It prints each one's password, where the codes the PDS emails | ||
are read, and how to run the command against it. ctrl-c stops it all, and | ||
each start is empty again. | ||
| ||
Signing in to the console takes https at a name: the host is the | ||
console's OAuth client, and a PDS fetches a client's metadata only so. | ||
portless (a harness package) serves it: its proxy on port 443 sends every | ||
name under localtest.me (which public DNS resolves to 127.0.0.1) to the | ||
port registered for it, with a certificate from a local CA in the system's | ||
trust store, which the PDS is told to trust too. Start its proxy once a | ||
boot, as `just dev` says if it is not up; it asks for your password to | ||
take port 443. | ||
| ||
## Tests | ||
| ||
The tests that reach the network run against the reference PDS and a PLC | ||
harness/e2e/setup.mjs+19−106
| @@ -1,113 +1,26 @@ | ||
// Before the flows: the reference PDS and PLC (harness/pds.mjs), the host | ||
// over them, and the console's build behind the https front the PDS sends | ||
// sign-ins back to (harness/tls-proxy.mjs); and a group of alice's, made | ||
// with service auth from her PDS, as a member's own client would. Where | ||
// everything is goes to the flows in E2E_WORLD. Returns the teardown. | ||
// Before the flows: a world (../world.mjs) whose console is behind the | ||
// https front the PDS sends sign-ins back to (harness/tls-proxy.mjs), at | ||
// the name harness/pds.mjs resolves for itself. Where everything is goes | ||
// to the flows in E2E_WORLD. Returns the teardown. | ||
| ||
import { spawn } from 'node:child_process' | ||
import { mkdtempSync } from 'node:fs' | ||
import { createServer } from 'node:net' | ||
import { tmpdir } from 'node:os' | ||
import { join, resolve } from 'node:path' | ||
import { createInterface } from 'node:readline' | ||
import { fileURLToPath } from 'node:url' | ||
| ||
const root = resolve(fileURLToPath(new URL('.', import.meta.url)), '../..') | ||
| ||
/// The first line `child` prints that `wanted` takes. | ||
const line = async (child, wanted) => { | ||
for await (const l of createInterface({ input: child.stdout })) if (wanted(l)) return l | ||
throw new Error(`${child.spawnfile} stopped before it was ready`) | ||
} | ||
| ||
/// A port nothing listens on now. | ||
const free = () => | ||
new Promise((done) => { | ||
const server = createServer().listen(0, '127.0.0.1', () => { | ||
const { port } = server.address() | ||
server.close(() => done(port)) | ||
}) | ||
import { join } from 'node:path' | ||
import { line, root, start } from '../world.mjs' | ||
| ||
/// The console's front: harness/tls-proxy.mjs, with the certificate the | ||
/// PDS trusts, for the name only it resolves. | ||
const front = async (consolePort, net, children) => { | ||
const proxy = spawn('node', [join(root, 'harness/tls-proxy.mjs'), net.console.cert, net.console.key, String(consolePort)], { | ||
stdio: ['pipe', 'pipe', 'inherit'], | ||
}) | ||
| ||
/// Until `url` answers. | ||
const up = async (url) => { | ||
for (let i = 0; i < 100; i++) { | ||
if (await fetch(url).then(() => true, () => false)) return | ||
await new Promise((r) => setTimeout(r, 100)) | ||
} | ||
throw new Error(`${url} never answered`) | ||
} | ||
| ||
/// `name`'s service auth for `aud` and `lxm`, from their own PDS. | ||
const serviceAuth = async (pds, account, aud, lxm) => { | ||
const session = await fetch(`${pds}/xrpc/com.atproto.server.createSession`, { | ||
method: 'POST', | ||
headers: { 'content-type': 'application/json' }, | ||
body: JSON.stringify({ identifier: account.handle, password: account.password }), | ||
}).then((r) => r.json()) | ||
const asked = `${pds}/xrpc/com.atproto.server.getServiceAuth?aud=${encodeURIComponent(aud)}&lxm=${lxm}` | ||
const { token } = await fetch(asked, { headers: { authorization: `Bearer ${session.accessJwt}` } }).then((r) => r.json()) | ||
return token | ||
children.push(proxy) | ||
return `https://${net.console.host}:${(await line(proxy, (l) => /^\d+$/.test(l.trim()))).trim()}` | ||
} | ||
| ||
export default async function setup() { | ||
const children = [] | ||
const stop = () => children.reverse().forEach((c) => c.kill('SIGTERM')) | ||
try { | ||
// harbor is an account of its own, which alice moves here to become a group. | ||
const pds = spawn('node', [join(root, 'harness/pds.mjs'), '--accounts', 'alice,bob,harbor'], { stdio: ['pipe', 'pipe', 'inherit'] }) | ||
children.push(pds) | ||
const net = JSON.parse(await line(pds, (l) => l.startsWith('{"pds"'))) | ||
| ||
const [hostPort, consolePort] = [await free(), await free()] | ||
const proxy = spawn('node', [join(root, 'harness/tls-proxy.mjs'), net.console.cert, net.console.key, String(consolePort)], { | ||
stdio: ['pipe', 'pipe', 'inherit'], | ||
}) | ||
children.push(proxy) | ||
const base = `https://${net.console.host}:${(await line(proxy, (l) => /^\d+$/.test(l.trim()))).trim()}` | ||
const host = `http://localhost:${hostPort}` | ||
const did = `did:web:localhost%3A${hostPort}` | ||
| ||
const server = spawn(join(root, 'target/debug/group-host'), [], { | ||
stdio: ['ignore', 'pipe', 'inherit'], | ||
env: { | ||
...process.env, | ||
PORT: String(hostPort), | ||
PUBLIC_URL: host, | ||
CONSOLE_URL: base, | ||
DATA_DIR: mkdtempSync(join(tmpdir(), 'group-host-e2e-')), | ||
PLC_URL: net.plc, | ||
DID_WEB_SCHEME: 'http', | ||
HANDLE_DOMAIN: '.groups.test', | ||
HANDLE_RESOLVERS: net.pds, | ||
}, | ||
}) | ||
children.push(server) | ||
await line(server, (l) => l.startsWith('listening on')) | ||
| ||
const web = spawn('node', ['build'], { | ||
cwd: join(root, 'web'), | ||
stdio: ['ignore', 'inherit', 'inherit'], | ||
env: { ...process.env, PORT: String(consolePort), HOST: '127.0.0.1', GROUP_HOST: `http://127.0.0.1:${hostPort}`, ORIGIN: base }, | ||
}) | ||
children.push(web) | ||
await up(`http://127.0.0.1:${consolePort}/`) | ||
| ||
// A group of alice's, as her own client would make one. | ||
const alice = net.accounts.find((a) => a.handle === 'alice.test') | ||
const lxm = 'fyi.opensocial.createGroup' | ||
const made = await fetch(`${host}/xrpc/${lxm}`, { | ||
method: 'POST', | ||
headers: { 'content-type': 'application/json', authorization: `Bearer ${await serviceAuth(net.pds, alice, did, lxm)}` }, | ||
body: JSON.stringify({ handle: 'riders.groups.test', displayName: 'Riders' }), | ||
}) | ||
if (!made.ok) throw new Error(`createGroup: ${await made.text()}`) | ||
const riders = await made.json() | ||
| ||
process.env.E2E_WORLD = JSON.stringify({ base, host, pds: net.pds, mail: net.mail, accounts: net.accounts, riders: riders.did }) | ||
return stop | ||
} catch (e) { | ||
stop() | ||
throw e | ||
} | ||
// harbor is an account of its own, which alice moves here to become a group. | ||
const w = await start({ accounts: ['alice', 'bob', 'harbor'], front }) | ||
const { net } = w | ||
process.env.E2E_WORLD = JSON.stringify({ base: w.base, host: w.host, pds: net.pds, mail: net.mail, accounts: net.accounts, riders: w.riders }) | ||
return w.stop | ||
} | ||
harness/dev.mjsnew+82−0
| @@ -0,0 +1,82 @@ | ||
// `just dev`: a world to use the console in (world.mjs), for a person to | ||
// look at: the reference PDS with alice, bob and harbor on it, the host, the | ||
// console at https://group-host.localtest.me, and a group of alice's. | ||
// Stops on ctrl-c, and forgets everything: the PDS, the directory and the | ||
// host start empty each time. | ||
// | ||
// The console is the host's OAuth client, so the PDS fetches its metadata | ||
// from an https name, and the browser trusts that name's certificate: | ||
// portless gives both (https://github.com/vercel-labs/portless), its proxy | ||
// on port 443 for every name under localtest.me, which public DNS resolves | ||
// to 127.0.0.1, with a local CA in the system's trust store. The PDS is | ||
// told to trust that CA too. | ||
| ||
import { execFileSync } from 'node:child_process' | ||
import { readFileSync } from 'node:fs' | ||
import { homedir } from 'node:os' | ||
import { join } from 'node:path' | ||
import { root, start } from './world.mjs' | ||
| ||
const NAME = 'group-host' | ||
const DOMAIN = 'localtest.me' | ||
const portless = join(root, 'harness/node_modules/.bin/portless') | ||
const state = process.env.PORTLESS_STATE_DIR ?? join(homedir(), '.portless') | ||
| ||
// portless's proxy, on 443 for both TLDs: started once a boot, with sudo. | ||
const port = (() => { | ||
try { | ||
return readFileSync(join(state, 'proxy.port'), 'utf8').trim() | ||
} catch { | ||
return '' | ||
} | ||
})() | ||
if (port !== '443') { | ||
console.error(`portless's https proxy is not on port 443. Start it, once a boot (it asks for your password): | ||
| ||
cd harness && PORTLESS_TLD=localhost,${DOMAIN} PORTLESS_WILDCARD=1 PORTLESS_SYNC_HOSTS=0 npx portless proxy start -p 443 | ||
| ||
and \`npx portless trust\` once, if your browser does not trust its certificate.`) | ||
process.exit(1) | ||
} | ||
| ||
/// The console behind portless, at https://group-host.localtest.me. | ||
const front = async (consolePort) => { | ||
execFileSync(portless, ['alias', NAME, String(consolePort), '--force'], { stdio: 'ignore' }) | ||
return `https://${NAME}.${DOMAIN}` | ||
} | ||
| ||
const w = await start({ | ||
accounts: ['alice', 'bob', 'harbor'], | ||
front, | ||
pdsEnv: { NODE_EXTRA_CA_CERTS: join(state, 'ca.pem') }, | ||
}) | ||
| ||
const stop = () => { | ||
try { | ||
execFileSync(portless, ['alias', '--remove', NAME], { stdio: 'ignore' }) | ||
} finally { | ||
w.stop() | ||
process.exit(0) | ||
} | ||
} | ||
process.on('SIGINT', stop) | ||
process.on('SIGTERM', stop) | ||
| ||
const people = w.net.accounts.map((a) => ` ${a.handle.padEnd(14)} ${a.password}`).join('\n') | ||
console.log(` | ||
The console ${w.base} | ||
The host ${w.host} (${w.did}) | ||
The PDS ${w.net.pds} | ||
The directory ${w.net.plc} | ||
| ||
Sign in as any of them, with their password at the PDS's own page: | ||
${people} | ||
| ||
alice runs Riders (riders.groups.test). harbor is an account of its own, to | ||
move here from ${w.base}/move; the code its server emails is at | ||
${w.net.mail}?to=harbor@test.com | ||
| ||
The command, as alice: | ||
OPENSOCIAL_HOST=${w.host} PDS_URL=${w.net.pds} OPENSOCIAL_AS=alice.test OPENSOCIAL_PASSWORD=alice-password just opensocial inspect riders.groups.test | ||
| ||
ctrl-c stops it all.`) | ||
harness/package-lock.json+20−1
| @@ -9,7 +9,8 @@ | ||
"@atproto/dev-env": "0.6.26" | ||
}, | ||
"devDependencies": { | ||
"@playwright/test": "1.58.2" | ||
"@playwright/test": "1.58.2", | ||
"portless": "0.15.7" | ||
} | ||
}, | ||
"node_modules/@atproto-labs/did-resolver": { | ||
| @@ -8013,6 +8014,24 @@ | ||
"node": ">=18" | ||
} | ||
}, | ||
"node_modules/portless": { | ||
"version": "0.15.7", | ||
"resolved": "https://registry.npmjs.org/portless/-/portless-0.15.7.tgz", | ||
"integrity": "sha512-fffMnzfwU/0chJmOMmeAOjFouRsZUmdJagxdqE4DATLw83ssAnHX4BzvNqOI5vBgsuvWLX5ZLg63bzs7zdVFNw==", | ||
"dev": true, | ||
"license": "Apache-2.0", | ||
"os": [ | ||
"darwin", | ||
"linux", | ||
"win32" | ||
], | ||
"bin": { | ||
"portless": "dist/cli.js" | ||
}, | ||
"engines": { | ||
"node": ">=24" | ||
} | ||
}, | ||
"node_modules/postgres-array": { | ||
"version": "2.0.0", | ||
"resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", | ||
harness/package.json+2−1
| @@ -7,6 +7,7 @@ | ||
"@atproto/dev-env": "0.6.26" | ||
}, | ||
"devDependencies": { | ||
"@playwright/test": "1.58.2" | ||
"@playwright/test": "1.58.2", | ||
"portless": "0.15.7" | ||
} | ||
} | ||
harness/world.mjsnew+115−0
| @@ -0,0 +1,115 @@ | ||
// A world to use the console in: the reference PDS and PLC | ||
// (harness/pds.mjs), the host over them, the console's build in front of | ||
// the host, and a group of alice's, made with service auth from her PDS, as | ||
// a member's own client would. The e2e flows run over one | ||
// (e2e/setup.mjs), behind a front of their own; `just dev` runs one for a | ||
// person to look at (dev.mjs), behind portless. | ||
| ||
import { spawn } from 'node:child_process' | ||
import { mkdtempSync } from 'node:fs' | ||
import { createServer } from 'node:net' | ||
import { tmpdir } from 'node:os' | ||
import { join, resolve } from 'node:path' | ||
import { createInterface } from 'node:readline' | ||
import { fileURLToPath } from 'node:url' | ||
| ||
export const root = resolve(fileURLToPath(new URL('.', import.meta.url)), '..') | ||
| ||
/// The first line `child` prints that `wanted` takes. | ||
export const line = async (child, wanted) => { | ||
for await (const l of createInterface({ input: child.stdout })) if (wanted(l)) return l | ||
throw new Error(`${child.spawnfile} stopped before it was ready`) | ||
} | ||
| ||
/// A port nothing listens on now. | ||
export const free = () => | ||
new Promise((done) => { | ||
const server = createServer().listen(0, '127.0.0.1', () => { | ||
const { port } = server.address() | ||
server.close(() => done(port)) | ||
}) | ||
}) | ||
| ||
/// Until `url` answers. | ||
const up = async (url) => { | ||
for (let i = 0; i < 100; i++) { | ||
if (await fetch(url).then(() => true, () => false)) return | ||
await new Promise((r) => setTimeout(r, 100)) | ||
} | ||
throw new Error(`${url} never answered`) | ||
} | ||
| ||
/// `account`'s service auth for `aud` and `lxm`, from their own PDS. | ||
const serviceAuth = async (pds, account, aud, lxm) => { | ||
const session = await fetch(`${pds}/xrpc/com.atproto.server.createSession`, { | ||
method: 'POST', | ||
headers: { 'content-type': 'application/json' }, | ||
body: JSON.stringify({ identifier: account.handle, password: account.password }), | ||
}).then((r) => r.json()) | ||
const asked = `${pds}/xrpc/com.atproto.server.getServiceAuth?aud=${encodeURIComponent(aud)}&lxm=${lxm}` | ||
const { token } = await fetch(asked, { headers: { authorization: `Bearer ${session.accessJwt}` } }).then((r) => r.json()) | ||
return token | ||
} | ||
| ||
/// Starts a world with `accounts` on the reference PDS, its console | ||
/// served at the port `front` is given and reached at the https address | ||
/// `front` answers with (`front(consolePort, net, children)`). `pdsEnv` is | ||
/// the PDS's environment beyond this one's. Its parts, and `stop`. | ||
export async function start({ accounts, front, pdsEnv = {} }) { | ||
const children = [] | ||
const stop = () => children.reverse().forEach((c) => c.kill('SIGTERM')) | ||
try { | ||
const pds = spawn('node', [join(root, 'harness/pds.mjs'), '--accounts', accounts.join(',')], { | ||
stdio: ['pipe', 'pipe', 'inherit'], | ||
env: { ...process.env, ...pdsEnv }, | ||
}) | ||
children.push(pds) | ||
const net = JSON.parse(await line(pds, (l) => l.startsWith('{"pds"'))) | ||
| ||
const [hostPort, consolePort] = [await free(), await free()] | ||
const base = await front(consolePort, net, children) | ||
const host = `http://localhost:${hostPort}` | ||
const did = `did:web:localhost%3A${hostPort}` | ||
| ||
const server = spawn(join(root, 'target/debug/group-host'), [], { | ||
stdio: ['ignore', 'pipe', 'inherit'], | ||
env: { | ||
...process.env, | ||
PORT: String(hostPort), | ||
PUBLIC_URL: host, | ||
CONSOLE_URL: base, | ||
DATA_DIR: mkdtempSync(join(tmpdir(), 'group-host-world-')), | ||
PLC_URL: net.plc, | ||
DID_WEB_SCHEME: 'http', | ||
HANDLE_DOMAIN: '.groups.test', | ||
HANDLE_RESOLVERS: net.pds, | ||
}, | ||
}) | ||
children.push(server) | ||
await line(server, (l) => l.startsWith('listening on')) | ||
| ||
const web = spawn('node', ['build'], { | ||
cwd: join(root, 'web'), | ||
stdio: ['ignore', 'inherit', 'inherit'], | ||
env: { ...process.env, PORT: String(consolePort), HOST: '127.0.0.1', GROUP_HOST: `http://127.0.0.1:${hostPort}`, ORIGIN: base }, | ||
}) | ||
children.push(web) | ||
await up(`http://127.0.0.1:${consolePort}/`) | ||
| ||
// A group of alice's, as her own client would make one. | ||
const alice = net.accounts.find((a) => a.handle === 'alice.test') | ||
const lxm = 'fyi.opensocial.createGroup' | ||
const made = await fetch(`${host}/xrpc/${lxm}`, { | ||
method: 'POST', | ||
headers: { 'content-type': 'application/json', authorization: `Bearer ${await serviceAuth(net.pds, alice, did, lxm)}` }, | ||
body: JSON.stringify({ handle: 'riders.groups.test', displayName: 'Riders' }), | ||
}) | ||
if (!made.ok) throw new Error(`createGroup: ${await made.text()}`) | ||
const riders = await made.json() | ||
| ||
return { base, host, did, net, riders: riders.did, stop } | ||
} catch (e) { | ||
stop() | ||
throw e | ||
} | ||
} | ||
Justfile+7−0
| @@ -51,6 +51,13 @@ web: | ||
web-dev: | ||
cd web && npm run dev | ||
| ||
# The console to use yourself, over a world of its own (harness/dev.mjs): | ||
# the reference PDS with alice, bob and harbor, the host, and the console | ||
# at https://group-host.localtest.me, behind portless. Empty each time. | ||
dev: web | ||
{{cargo}} build -p group-host | ||
node harness/dev.mjs | ||
| ||
# The console used as people use it (harness/e2e/flows.spec.mjs): in | ||
# Chromium, signed in to through the reference PDS's own approval page, | ||
# and an app signed in as a group through it, over a world of its own. | ||