Remove try-catch from perfToggle and fix perf timing flag read

Let errors propagate to the caller instead of catching locally,
simplify the request body construction, and snapshot perf.timing
before the fetch to avoid a TOCTOU race.
This commit is contained in:
Chris Wanstrath 2026-03-19 11:50:10 -07:00
parent 4cc0ff2bed
commit 1f0c7bd099
2 changed files with 17 additions and 20 deletions

View File

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

View File

@ -1,10 +1,10 @@
import type { Server, ServerWebSocket } from 'bun' import type { Server, ServerWebSocket } from 'bun'
import { getAppBySubdomain } from '$apps' import { getAppBySubdomain } from '$apps'
export type { WsData }
export const perf = { timing: false } export const perf = { timing: false }
export type { WsData }
const pendingMessages = new Map<ServerWebSocket<WsData>, (string | ArrayBuffer | Uint8Array)[]>() const pendingMessages = new Map<ServerWebSocket<WsData>, (string | ArrayBuffer | Uint8Array)[]>()
const upstreams = new Map<ServerWebSocket<WsData>, WebSocket>() const upstreams = new Map<ServerWebSocket<WsData>, WebSocket>()
@ -55,14 +55,15 @@ export async function proxySubdomain(subdomain: string, req: Request): Promise<R
headers.delete('transfer-encoding') headers.delete('transfer-encoding')
try { try {
const start = perf.timing ? performance.now() : 0 const shouldTime = perf.timing
const start = shouldTime ? performance.now() : 0
const res = await fetch(target, { const res = await fetch(target, {
method: req.method, method: req.method,
headers, headers,
body, body,
redirect: 'manual', redirect: 'manual',
}) })
if (perf.timing) { if (shouldTime) {
const ms = (performance.now() - start).toFixed(1) const ms = (performance.now() - start).toFixed(1)
console.log(`[perf] ${req.method} ${subdomain}${url.pathname}${res.status} in ${ms}ms`) console.log(`[perf] ${req.method} ${subdomain}${url.pathname}${res.status} in ${ms}ms`)
} }