Inline PerfState interface and move subscriptions before routes

The PerfState interface was only used twice, so inline it. Move
onChange/onHostLog subscriptions above the route definitions to keep
side-effects grouped. Skip perf.now() in proxy when timing is off.
This commit is contained in:
Chris Wanstrath 2026-03-19 11:29:17 -07:00
parent 9a19c0a861
commit 4cc0ff2bed
3 changed files with 10 additions and 14 deletions

View File

@ -1,20 +1,16 @@
import color from 'ansis'
import { get, handleError, post } from '../http'
interface PerfState {
perfTiming: boolean
}
export async function perfToggle(onOff?: string) {
try {
if (onOff && !['on', 'off', 'status'].includes(onOff)) {
console.error('Usage: toes perf [on|off|status]')
process.exit(1)
}
const body = onOff === 'on' ? { on: true } : onOff === 'off' ? { on: false } : undefined
const res = onOff === 'status'
? await get<PerfState>('/api/system/perf')
: await post<PerfState>('/api/system/perf', body ?? {})
? await get<{ perfTiming: boolean }>('/api/system/perf')
: await post<{ perfTiming: boolean }>('/api/system/perf',
onOff === 'on' ? { on: true } : onOff === 'off' ? { on: false } : {})
if (res) {
const label = res.perfTiming ? color.green('on') : color.red('off')
console.log(`Perf timing ${onOff === 'status' ? 'is ' : ''}${label}`)

View File

@ -278,6 +278,12 @@ function pushHostLog(text: string) {
for (const listener of unifiedLogListeners) listener(line)
}
// Subscribe to app changes to collect logs
onChange(collectLogs)
// Subscribe to host-level log messages
onHostLog(pushHostLog)
// Perf timing toggle
router.get('/perf', c => c.json({ perfTiming: perf.timing }))
router.post('/perf', async c => {
@ -288,12 +294,6 @@ router.post('/perf', async c => {
return c.json({ perfTiming: on })
})
// Subscribe to app changes to collect logs
onChange(collectLogs)
// Subscribe to host-level log messages
onHostLog(pushHostLog)
// Restart server (systemd brings it back)
router.post('/restart', c => {
setTimeout(() => process.exit(0), 100)

View File

@ -55,7 +55,7 @@ export async function proxySubdomain(subdomain: string, req: Request): Promise<R
headers.delete('transfer-encoding')
try {
const start = performance.now()
const start = perf.timing ? performance.now() : 0
const res = await fetch(target, {
method: req.method,
headers,