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:
parent
4cc0ff2bed
commit
1f0c7bd099
|
|
@ -1,21 +1,17 @@
|
|||
import color from 'ansis'
|
||||
import { get, handleError, post } from '../http'
|
||||
import { get, post } from '../http'
|
||||
|
||||
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 && onOff !== 'status' ? { on: onOff === 'on' } : {}
|
||||
const res = onOff === 'status'
|
||||
? await get<{ perfTiming: boolean }>('/api/system/perf')
|
||||
: await post<{ perfTiming: boolean }>('/api/system/perf',
|
||||
onOff === 'on' ? { on: true } : onOff === 'off' ? { on: false } : {})
|
||||
: await post<{ perfTiming: boolean }>('/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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import type { Server, ServerWebSocket } from 'bun'
|
||||
import { getAppBySubdomain } from '$apps'
|
||||
|
||||
export type { WsData }
|
||||
|
||||
export const perf = { timing: false }
|
||||
|
||||
export type { WsData }
|
||||
|
||||
const pendingMessages = new Map<ServerWebSocket<WsData>, (string | ArrayBuffer | Uint8Array)[]>()
|
||||
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')
|
||||
|
||||
try {
|
||||
const start = perf.timing ? performance.now() : 0
|
||||
const shouldTime = perf.timing
|
||||
const start = shouldTime ? performance.now() : 0
|
||||
const res = await fetch(target, {
|
||||
method: req.method,
|
||||
headers,
|
||||
body,
|
||||
redirect: 'manual',
|
||||
})
|
||||
if (perf.timing) {
|
||||
if (shouldTime) {
|
||||
const ms = (performance.now() - start).toFixed(1)
|
||||
console.log(`[perf] ${req.method} ${subdomain}${url.pathname} → ${res.status} in ${ms}ms`)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user