Simplify perf toggle by deduplicating branching logic

Early-return on invalid input, then unify the GET/POST and display
paths so each concern is handled once instead of per-subcommand.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Chris Wanstrath 2026-03-19 11:02:06 -07:00
parent c3ad78f1be
commit c42c73fe70
4 changed files with 14 additions and 18 deletions

View File

@ -7,23 +7,18 @@ interface PerfState {
export async function perfToggle(onOff?: string) {
try {
if (onOff === undefined) {
// Toggle
const res = await post<PerfState>('/api/system/perf')
if (res) console.log(`Perf timing ${res.perfTiming ? color.green('on') : color.red('off')}`)
} else if (onOff === 'on') {
const res = await post<PerfState>('/api/system/perf', { on: true })
if (res) console.log(`Perf timing ${color.green('on')}`)
} else if (onOff === 'off') {
const res = await post<PerfState>('/api/system/perf', { on: false })
if (res) console.log(`Perf timing ${color.red('off')}`)
} else if (onOff === 'status') {
const res = await get<PerfState>('/api/system/perf')
if (res) console.log(`Perf timing is ${res.perfTiming ? color.green('on') : color.red('off')}`)
} else {
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 ?? {})
if (res) {
const label = res.perfTiming ? color.green('on') : color.red('off')
console.log(`Perf timing ${onOff === 'status' ? 'is ' : ''}${label}`)
}
} catch (error) {
handleError(error)
}

View File

@ -16,15 +16,15 @@ import {
infoApp,
listApps,
logApp,
metricsApp,
newApp,
openApp,
perfToggle,
renameApp,
restartApp,
rmApp,
shareApp,
startApp,
metricsApp,
perfToggle,
stopApp,
unshareApp,
} from './commands'

View File

@ -287,7 +287,7 @@ onHostLog(pushHostLog)
// Perf timing toggle
router.get('/perf', c => c.json({ perfTiming }))
router.post('/perf', async c => {
const body = await c.req.json<{ on?: boolean }>()
const body = await c.req.json<{ on?: boolean }>().catch(() => ({}))
const on = body.on ?? !perfTiming
setPerfTiming(on)
console.log(`[perf] timing ${on ? 'enabled' : 'disabled'}`)

View File

@ -4,7 +4,6 @@ import { getAppBySubdomain } from '$apps'
export type { WsData }
export let perfTiming = false
export const setPerfTiming = (on: boolean) => { perfTiming = on }
const pendingMessages = new Map<ServerWebSocket<WsData>, (string | ArrayBuffer | Uint8Array)[]>()
const upstreams = new Map<ServerWebSocket<WsData>, WebSocket>()
@ -15,6 +14,8 @@ interface WsData {
protocols: string[]
}
export const setPerfTiming = (on: boolean) => { perfTiming = on }
export function extractSubdomain(host: string): string | null {
// Strip port
const hostname = host.replace(/:\d+$/, '')