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:
parent
9a19c0a861
commit
4cc0ff2bed
|
|
@ -1,20 +1,16 @@
|
||||||
import color from 'ansis'
|
import color from 'ansis'
|
||||||
import { get, handleError, post } from '../http'
|
import { get, handleError, post } from '../http'
|
||||||
|
|
||||||
interface PerfState {
|
|
||||||
perfTiming: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function perfToggle(onOff?: string) {
|
export async function perfToggle(onOff?: string) {
|
||||||
try {
|
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 === 'on' ? { on: true } : onOff === 'off' ? { on: false } : undefined
|
|
||||||
const res = onOff === 'status'
|
const res = onOff === 'status'
|
||||||
? await get<PerfState>('/api/system/perf')
|
? await get<{ perfTiming: boolean }>('/api/system/perf')
|
||||||
: await post<PerfState>('/api/system/perf', body ?? {})
|
: await post<{ perfTiming: boolean }>('/api/system/perf',
|
||||||
|
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}`)
|
||||||
|
|
|
||||||
|
|
@ -278,6 +278,12 @@ function pushHostLog(text: string) {
|
||||||
for (const listener of unifiedLogListeners) listener(line)
|
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
|
// Perf timing toggle
|
||||||
router.get('/perf', c => c.json({ perfTiming: perf.timing }))
|
router.get('/perf', c => c.json({ perfTiming: perf.timing }))
|
||||||
router.post('/perf', async c => {
|
router.post('/perf', async c => {
|
||||||
|
|
@ -288,12 +294,6 @@ router.post('/perf', async c => {
|
||||||
return c.json({ perfTiming: on })
|
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)
|
// Restart server (systemd brings it back)
|
||||||
router.post('/restart', c => {
|
router.post('/restart', c => {
|
||||||
setTimeout(() => process.exit(0), 100)
|
setTimeout(() => process.exit(0), 100)
|
||||||
|
|
|
||||||
|
|
@ -55,7 +55,7 @@ export async function proxySubdomain(subdomain: string, req: Request): Promise<R
|
||||||
headers.delete('transfer-encoding')
|
headers.delete('transfer-encoding')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const start = performance.now()
|
const start = perf.timing ? performance.now() : 0
|
||||||
const res = await fetch(target, {
|
const res = await fetch(target, {
|
||||||
method: req.method,
|
method: req.method,
|
||||||
headers,
|
headers,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user