fix: forward -- args through devbox scripts, fail serve loudly (#84)
devbox 0.17.2 runs .devbox/gen/scripts/<name>.sh with the args as positional parameters but does not append "$@", so tools/serve.sh (and seed, bench, mutation, import-scale, fogbench) saw an empty argv and a documented `serve -- --image=...` silently served the default 4096 scene. End every argument-taking script body with "$@". serve: --dry-run parses and prints the options without building; unknown options, non-numeric ports/tile sizes and --tile-size without --image fail loudly; a no-image start prints the current map read from state.db; NixOS gets security.pki.certificateFiles instead of the Debian trust steps. Guard the faults import in worldmap/vision/markers with `when defined(leakfault)` (it is only used inside those blocks), removing the three UnusedImport warnings; leakfault builds still compile. tools/devboxargs.test.mjs goes through `devbox run <script> -- <args>` for serve (dry-run, unknown flag, tile-size without image) and seed (--help, unknown flag), the seam the direct-tool tests missed.
This commit is contained in:
+87
-4
@@ -16,11 +16,12 @@
|
||||
import { spawnSync } from 'node:child_process'
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import { DatabaseSync } from 'node:sqlite'
|
||||
import { DEFAULT_DIST, DEFAULT_LAN_PORT, DEFAULT_SERVER_BIN, DEFAULT_STATE_DIR,
|
||||
detectLanIp, startLanServe } from './lanserve.mjs'
|
||||
|
||||
const USAGE = `usage: devbox run serve [-- --host=<ip>] [-- --port=<n>] [-- --state-dir=<path>]
|
||||
[-- --image=<file> [-- --tile-size=<px>]]
|
||||
[-- --image=<file> [-- --tile-size=<px>]] [-- --dry-run]
|
||||
|
||||
Starts the FFF_vtt server behind a TLS/HTTP2 nginx on the LAN. State, GM key and certificate
|
||||
persist under the state dir (default ${DEFAULT_STATE_DIR}), so a session (and a trusted phone)
|
||||
@@ -29,6 +30,10 @@ FFF_VTT_STATE_DIR.
|
||||
|
||||
--image imports a PNG/WebP map before starting (a fresh state dir is created and migrated first)
|
||||
and then serves the imported scene; a server already holding the state-dir lock is refused (exit 3).
|
||||
--dry-run parses and prints the effective options, then exits without building or starting anything.
|
||||
|
||||
Unknown options, a non-numeric port/tile size, and --tile-size without --image fail loudly (exit 1)
|
||||
instead of being ignored: a silently dropped --image used to serve the default 4096 scene (#84).
|
||||
`
|
||||
|
||||
const VALUE_OPTIONS = new Set(
|
||||
@@ -43,6 +48,11 @@ function parseArgs(argv) {
|
||||
const m = /^--([a-z-]+)(?:=(.*))?$/.exec(arg)
|
||||
if (!m) throw new Error(`unexpected argument ${JSON.stringify(arg)} (see --help)`)
|
||||
const key = m[1]
|
||||
if (key === 'dry-run') {
|
||||
if (m[2] !== undefined) throw new Error('--dry-run takes no value (see --help)')
|
||||
opts.dryRun = true
|
||||
continue
|
||||
}
|
||||
if (!VALUE_OPTIONS.has(key)) throw new Error(`unknown option --${key} (see --help)`)
|
||||
let value = m[2]
|
||||
if (value === undefined) {
|
||||
@@ -61,6 +71,25 @@ function parseArgs(argv) {
|
||||
return opts
|
||||
}
|
||||
|
||||
/** Flags that only make sense with an image, or that need numbers, fail instead of being ignored. */
|
||||
function validate(opts) {
|
||||
for (const [key, env] of [['port', 'FFF_VTT_LAN_PORT'], ['serverPort', 'FFF_VTT_SERVER_PORT'],
|
||||
['tileSize', 'FFF_VTT_LAN_TILE']]) {
|
||||
if (opts[key] === undefined) continue
|
||||
if (!Number.isInteger(opts[key]) || opts[key] < 1) {
|
||||
throw new Error(`${key}: expected a whole number >= 1, got ${JSON.stringify(opts[key])} ` +
|
||||
`(--${key === 'serverPort' ? 'server-port' : key === 'tileSize' ? 'tile-size' : key} / ${env})`)
|
||||
}
|
||||
}
|
||||
if (opts.tileSize !== undefined && !opts.image) {
|
||||
throw new Error('--tile-size requires --image (it is the import tile size); ' +
|
||||
'drop --tile-size or pass --image=<file>')
|
||||
}
|
||||
if (opts.image !== undefined && opts.image.trim() === '') {
|
||||
throw new Error('--image: empty path (see --help)')
|
||||
}
|
||||
}
|
||||
|
||||
function envOptions() {
|
||||
const env = process.env
|
||||
const opts = {}
|
||||
@@ -98,6 +127,40 @@ function importImage(opts, stateDir) {
|
||||
}
|
||||
}
|
||||
|
||||
/** The map a no-image start will serve, read from the state db without starting anything. */
|
||||
function describeScene(stateDir) {
|
||||
const dbFile = path.join(stateDir, 'state.db')
|
||||
if (!fs.existsSync(dbFile)) return 'fresh state: default 4096x4096, tile 32, art v1'
|
||||
let db
|
||||
try {
|
||||
db = new DatabaseSync(dbFile, { readOnly: true })
|
||||
const row = db.prepare('SELECT w, h, tile_size, art_version FROM scene WHERE id = 1').get()
|
||||
if (!row) return `no scene row in ${dbFile}`
|
||||
return `${row.w}x${row.h}, tile ${row.tile_size}, art v${row.art_version}`
|
||||
} catch (e) {
|
||||
return `unreadable ${dbFile} (${e.message})`
|
||||
} finally {
|
||||
db?.close()
|
||||
}
|
||||
}
|
||||
|
||||
/** `--dry-run`: prove the `--` arguments reached this script, then exit without side effects. */
|
||||
function printDryRun(opts, stateDir) {
|
||||
const lines = [
|
||||
'serve: dry-run (nothing built, imported or started)',
|
||||
` host: ${opts.host ?? '<detected>'}${opts.host ? '' : ` (${detectLanIp() ?? 'none'})`}`,
|
||||
` port: ${opts.port ?? DEFAULT_LAN_PORT}`,
|
||||
` state-dir: ${stateDir}`,
|
||||
` dist: ${path.resolve(opts.distDir ?? DEFAULT_DIST)}`,
|
||||
` server-bin: ${path.resolve(opts.bin ?? DEFAULT_SERVER_BIN)}`,
|
||||
` server-port: ${opts.serverPort ?? '<auto>'}`,
|
||||
` image: ${opts.image ? path.resolve(opts.image) : '<none, keeping current map>'}`,
|
||||
` tile-size: ${opts.tileSize ?? '<none>'}`,
|
||||
]
|
||||
if (!opts.image) lines.push(` current map: ${describeScene(stateDir)}`)
|
||||
process.stdout.write(lines.join('\n') + '\n')
|
||||
}
|
||||
|
||||
function printStartup(handle) {
|
||||
const { origin, gmKey, stateDir, certPath, distDir, serverPort, certRegenerated } = handle
|
||||
const ip = handle.host
|
||||
@@ -121,14 +184,29 @@ function printStartup(handle) {
|
||||
' Only the "Proceed" tap is required; installing the CA just silences the warning.',
|
||||
'',
|
||||
'Laptop, one-time per certificate:',
|
||||
` Linux: sudo cp ${certPath} /usr/local/share/ca-certificates/fff-vtt-lan.crt`,
|
||||
' sudo update-ca-certificates',
|
||||
]
|
||||
// The Debian steps are wrong on NixOS (no /usr/local/share/ca-certificates, no imperative
|
||||
// update-ca-certificates): point at the declarative option instead. #84.
|
||||
if (fs.existsSync('/etc/NIXOS')) {
|
||||
lines.push(
|
||||
` NixOS: security.pki.certificateFiles = [ "${certPath}" ];`,
|
||||
' sudo nixos-rebuild switch (or home-manager switch)',
|
||||
` Chrome: "Advanced" -> "Proceed to ${ip} (unsafe)" is enough for the playtest;`,
|
||||
' the option above only silences the warning for every app.',
|
||||
)
|
||||
} else {
|
||||
lines.push(
|
||||
` Linux: sudo cp ${certPath} /usr/local/share/ca-certificates/fff-vtt-lan.crt`,
|
||||
' sudo update-ca-certificates',
|
||||
)
|
||||
}
|
||||
lines.push(
|
||||
` macOS: sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ${certPath}`,
|
||||
' Firefox: Settings > Privacy & Security > Certificates > View Certificates >',
|
||||
' Authorities > Import > select cert.pem > "Trust this CA to identify websites".',
|
||||
' (Chrome/Edge on the laptop read the system store; Firefox keeps its own.)',
|
||||
'',
|
||||
]
|
||||
)
|
||||
if (certRegenerated) {
|
||||
lines.push(`NOTE: the LAN address is new (${ip}); the certificate was regenerated. Trust it again.`, '')
|
||||
}
|
||||
@@ -142,12 +220,17 @@ function printStartup(handle) {
|
||||
|
||||
async function main() {
|
||||
const opts = { ...envOptions(), ...parseArgs(process.argv.slice(2)) }
|
||||
validate(opts)
|
||||
if (!opts.host) {
|
||||
const detected = detectLanIp()
|
||||
if (detected) opts.host = detected
|
||||
}
|
||||
const stateDir = path.resolve(opts.stateDir ?? DEFAULT_STATE_DIR)
|
||||
if (opts.dryRun) { printDryRun(opts, stateDir); return }
|
||||
if (opts.image) importImage(opts, stateDir)
|
||||
else {
|
||||
process.stdout.write(`serve: no --image given, keeping current map (${describeScene(stateDir)})\n`)
|
||||
}
|
||||
const handle = await startLanServe({
|
||||
stateDir,
|
||||
host: opts.host,
|
||||
|
||||
Reference in New Issue
Block a user