From c42c73fe7086526f8adfdff59c00e8764c1acfd5 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Thu, 19 Mar 2026 11:02:06 -0700 Subject: [PATCH] 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 --- src/cli/commands/perf.ts | 23 +++++++++-------------- src/cli/setup.ts | 4 ++-- src/server/api/system.ts | 2 +- src/server/proxy.ts | 3 ++- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/cli/commands/perf.ts b/src/cli/commands/perf.ts index 2ac389b..581158b 100644 --- a/src/cli/commands/perf.ts +++ b/src/cli/commands/perf.ts @@ -7,23 +7,18 @@ interface PerfState { export async function perfToggle(onOff?: string) { try { - if (onOff === undefined) { - // Toggle - const res = await post('/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('/api/system/perf', { on: true }) - if (res) console.log(`Perf timing ${color.green('on')}`) - } else if (onOff === 'off') { - const res = await post('/api/system/perf', { on: false }) - if (res) console.log(`Perf timing ${color.red('off')}`) - } else if (onOff === 'status') { - const res = await get('/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('/api/system/perf') + : await post('/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) } diff --git a/src/cli/setup.ts b/src/cli/setup.ts index fd167f7..777c2a8 100644 --- a/src/cli/setup.ts +++ b/src/cli/setup.ts @@ -16,15 +16,15 @@ import { infoApp, listApps, logApp, + metricsApp, newApp, openApp, + perfToggle, renameApp, restartApp, rmApp, shareApp, startApp, - metricsApp, - perfToggle, stopApp, unshareApp, } from './commands' diff --git a/src/server/api/system.ts b/src/server/api/system.ts index b156fc0..80db653 100644 --- a/src/server/api/system.ts +++ b/src/server/api/system.ts @@ -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'}`) diff --git a/src/server/proxy.ts b/src/server/proxy.ts index 9c50f0b..a05d7fb 100644 --- a/src/server/proxy.ts +++ b/src/server/proxy.ts @@ -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, (string | ArrayBuffer | Uint8Array)[]>() const upstreams = new Map, 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+$/, '')