@hamur/devtools 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +24 -2
- package/dist/src/client.js +780 -0
- package/dist/src/index.js +1711 -0
- package/dist/types/src/apps/locator.d.ts +3 -0
- package/dist/types/src/bridge.d.ts +8 -0
- package/dist/types/src/client.d.ts +3 -0
- package/dist/types/src/dev/analyze.d.ts +31 -0
- package/dist/types/src/dev/assets.d.ts +33 -0
- package/dist/types/src/dev/authz.d.ts +49 -0
- package/dist/types/src/dev/bundle.d.ts +22 -0
- package/dist/types/src/dev/config.d.ts +24 -0
- package/dist/types/src/dev/handler.d.ts +20 -0
- package/dist/types/src/dev/modules.d.ts +28 -0
- package/dist/types/src/dev/overview.d.ts +20 -0
- package/dist/types/src/dev/processes.d.ts +4 -0
- package/dist/types/src/dev/server-routes.d.ts +7 -0
- package/dist/types/src/dev/tasks.d.ts +26 -0
- package/dist/types/src/dev/timeline.d.ts +20 -0
- package/dist/types/src/dev/virtual.d.ts +9 -0
- package/dist/types/src/icons.d.ts +24 -0
- package/dist/types/src/index.d.ts +8 -0
- package/dist/types/src/locator.d.ts +17 -0
- package/dist/types/src/mask.d.ts +8 -0
- package/dist/types/src/module.d.ts +27 -0
- package/dist/types/src/open-in-editor.d.ts +4 -0
- package/dist/types/src/options.d.ts +14 -0
- package/dist/types/src/panel/document.d.ts +16 -0
- package/dist/types/src/panel/groups.d.ts +14 -0
- package/dist/types/src/panel/index.d.ts +1 -0
- package/dist/types/src/panel/settings.d.ts +69 -0
- package/dist/types/src/panel/style.d.ts +1 -0
- package/dist/types/src/panel/ui.d.ts +34 -0
- package/dist/types/src/server/authz.d.ts +30 -0
- package/dist/types/src/server/index.d.ts +3 -0
- package/dist/types/src/server/routes.d.ts +6 -0
- package/dist/types/src/server/storage.d.ts +56 -0
- package/dist/types/src/source-link.d.ts +2 -0
- package/dist/types/src/tabs/analyze-model.d.ts +31 -0
- package/dist/types/src/tabs/analyze.d.ts +2 -0
- package/dist/types/src/tabs/assets.d.ts +11 -0
- package/dist/types/src/tabs/audit-model.d.ts +42 -0
- package/dist/types/src/tabs/audit.d.ts +2 -0
- package/dist/types/src/tabs/authz-model.d.ts +11 -0
- package/dist/types/src/tabs/authz.d.ts +2 -0
- package/dist/types/src/tabs/config.d.ts +8 -0
- package/dist/types/src/tabs/inspect.d.ts +7 -0
- package/dist/types/src/tabs/modules.d.ts +4 -0
- package/dist/types/src/tabs/overview.d.ts +2 -0
- package/dist/types/src/tabs/pages.d.ts +2 -0
- package/dist/types/src/tabs/payload.d.ts +6 -0
- package/dist/types/src/tabs/plugins-model.d.ts +15 -0
- package/dist/types/src/tabs/plugins.d.ts +2 -0
- package/dist/types/src/tabs/seo-model.d.ts +36 -0
- package/dist/types/src/tabs/seo.d.ts +2 -0
- package/dist/types/src/tabs/server-routes.d.ts +2 -0
- package/dist/types/src/tabs/storage.d.ts +13 -0
- package/dist/types/src/tabs/tasks-model.d.ts +16 -0
- package/dist/types/src/tabs/tasks.d.ts +2 -0
- package/dist/types/src/tabs/terminals-model.d.ts +53 -0
- package/dist/types/src/tabs/terminals.d.ts +2 -0
- package/dist/types/src/tabs/timeline-model.d.ts +58 -0
- package/dist/types/src/tabs/timeline.d.ts +2 -0
- package/dist/types/src/tester.d.ts +43 -0
- package/dist/types/src/toolbar/element.d.ts +28 -0
- package/dist/types/src/toolbar/shortcut.d.ts +14 -0
- package/dist/types/src/toolbar/style.d.ts +3 -0
- package/dist/types/src/view-context.d.ts +13 -0
- package/package.json +34 -4
- package/src/apps/locator.ts +131 -0
- package/src/bridge.ts +172 -0
- package/src/client.ts +10 -0
- package/src/dev/analyze.ts +128 -0
- package/src/dev/assets.ts +208 -0
- package/src/dev/authz.ts +162 -0
- package/src/dev/bundle.ts +91 -0
- package/src/dev/config.ts +132 -0
- package/src/dev/handler.ts +161 -0
- package/src/dev/modules.ts +60 -0
- package/src/dev/overview.ts +57 -0
- package/src/dev/processes.ts +34 -0
- package/src/dev/server-routes.ts +14 -0
- package/src/dev/tasks.ts +93 -0
- package/src/dev/timeline.ts +43 -0
- package/src/dev/virtual.ts +40 -0
- package/src/env.d.ts +21 -0
- package/src/icons.ts +48 -0
- package/src/index.ts +12 -0
- package/src/locator.ts +31 -0
- package/src/mask.ts +16 -0
- package/src/module.ts +352 -0
- package/src/open-in-editor.ts +18 -0
- package/src/options.ts +36 -0
- package/src/panel/document.ts +60 -0
- package/src/panel/groups.ts +41 -0
- package/src/panel/index.ts +429 -0
- package/src/panel/settings.ts +224 -0
- package/src/panel/style.ts +321 -0
- package/src/panel/ui.ts +52 -0
- package/src/server/authz.ts +110 -0
- package/src/server/index.ts +25 -0
- package/src/server/routes.ts +51 -0
- package/src/server/storage.ts +251 -0
- package/src/source-link.ts +29 -0
- package/src/tabs/analyze-model.ts +97 -0
- package/src/tabs/analyze.ts +217 -0
- package/src/tabs/assets.ts +375 -0
- package/src/tabs/audit-model.ts +216 -0
- package/src/tabs/audit.ts +223 -0
- package/src/tabs/authz-model.ts +28 -0
- package/src/tabs/authz.ts +337 -0
- package/src/tabs/config.ts +250 -0
- package/src/tabs/inspect.ts +101 -0
- package/src/tabs/modules.ts +58 -0
- package/src/tabs/overview.ts +104 -0
- package/src/tabs/pages.ts +188 -0
- package/src/tabs/payload.ts +224 -0
- package/src/tabs/plugins-model.ts +44 -0
- package/src/tabs/plugins.ts +95 -0
- package/src/tabs/seo-model.ts +140 -0
- package/src/tabs/seo.ts +162 -0
- package/src/tabs/server-routes.ts +469 -0
- package/src/tabs/storage.ts +313 -0
- package/src/tabs/tasks-model.ts +59 -0
- package/src/tabs/tasks.ts +287 -0
- package/src/tabs/terminals-model.ts +127 -0
- package/src/tabs/terminals.ts +294 -0
- package/src/tabs/timeline-model.ts +200 -0
- package/src/tabs/timeline.ts +260 -0
- package/src/tester.ts +158 -0
- package/src/toolbar/element.ts +428 -0
- package/src/toolbar/shortcut.ts +56 -0
- package/src/toolbar/style.ts +94 -0
- package/src/view-context.ts +93 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { DevProcessInfo } from "hamur/kit"
|
|
2
|
+
|
|
3
|
+
export const LOG_LEVELS = ["trace", "debug", "info", "warn", "error", "fatal"] as const
|
|
4
|
+
export type LogLevelName = (typeof LOG_LEVELS)[number]
|
|
5
|
+
|
|
6
|
+
/** A record of `/_hamur/logs` as the runner writes it. */
|
|
7
|
+
export interface LogEntry {
|
|
8
|
+
time: number
|
|
9
|
+
level: LogLevelName
|
|
10
|
+
msg: string
|
|
11
|
+
bindings: Record<string, unknown>
|
|
12
|
+
fields?: Record<string, unknown>
|
|
13
|
+
err?: unknown
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The tab keeps this many log records and process lines, as the runner does. */
|
|
17
|
+
export const KEPT = 1000
|
|
18
|
+
|
|
19
|
+
export interface LogFilter {
|
|
20
|
+
levels: ReadonlySet<LogLevelName>
|
|
21
|
+
/** Kept when it is empty; else only the records of that request. */
|
|
22
|
+
reqId: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** One `data:` payload of the stream; `undefined` for anything that is not a record. */
|
|
26
|
+
export function parseLogEvent(data: string): LogEntry | undefined {
|
|
27
|
+
let record: unknown
|
|
28
|
+
try {
|
|
29
|
+
record = JSON.parse(data)
|
|
30
|
+
} catch {
|
|
31
|
+
return undefined
|
|
32
|
+
}
|
|
33
|
+
if (record === null || typeof record !== "object") return undefined
|
|
34
|
+
const { time, level, msg, bindings } = record as Record<string, unknown>
|
|
35
|
+
if (typeof time !== "number" || typeof msg !== "string" || !LOG_LEVELS.includes(level as LogLevelName))
|
|
36
|
+
return undefined
|
|
37
|
+
return {
|
|
38
|
+
...(record as LogEntry),
|
|
39
|
+
bindings: bindings !== null && typeof bindings === "object" ? (bindings as Record<string, unknown>) : {},
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const requestIdOf = (record: LogEntry): string | undefined =>
|
|
44
|
+
typeof record.bindings.reqId === "string" ? record.bindings.reqId : undefined
|
|
45
|
+
|
|
46
|
+
export function filterLogs(records: readonly LogEntry[], filter: LogFilter): LogEntry[] {
|
|
47
|
+
const reqId = filter.reqId.trim()
|
|
48
|
+
return records.filter((record) => filter.levels.has(record.level) && (reqId === "" || requestIdOf(record) === reqId))
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const pad = (value: number, width = 2) => String(value).padStart(width, "0")
|
|
52
|
+
|
|
53
|
+
const valueText = (value: unknown): string =>
|
|
54
|
+
typeof value === "string" ? value : (JSON.stringify(value) ?? String(value))
|
|
55
|
+
|
|
56
|
+
/** `HH:MM:SS.mmm [name] msg key=value`, the level shown apart. */
|
|
57
|
+
export function logText(record: LogEntry): string {
|
|
58
|
+
const date = new Date(record.time)
|
|
59
|
+
const time = `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}.${pad(date.getMilliseconds(), 3)}`
|
|
60
|
+
const name = typeof record.bindings.name === "string" ? `[${record.bindings.name}] ` : ""
|
|
61
|
+
const fields = Object.entries(record.fields ?? {})
|
|
62
|
+
.map(([key, value]) => ` ${key}=${valueText(value)}`)
|
|
63
|
+
.join("")
|
|
64
|
+
const err = record.err as { message?: unknown } | undefined
|
|
65
|
+
const error = err && typeof err.message === "string" ? ` ${err.message}` : ""
|
|
66
|
+
return `${time} ${name}${record.msg}${fields}${error}`
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Adds `record` and drops the oldest past `KEPT`. */
|
|
70
|
+
export function keep<T>(list: T[], item: T): T[] {
|
|
71
|
+
list.push(item)
|
|
72
|
+
if (list.length > KEPT) list.splice(0, list.length - KEPT)
|
|
73
|
+
return list
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The `devtools:process` push. */
|
|
77
|
+
export interface ProcessEvent {
|
|
78
|
+
name: string
|
|
79
|
+
line?: string
|
|
80
|
+
stderr?: true
|
|
81
|
+
status?: DevProcessInfo["status"]
|
|
82
|
+
code?: number
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** A process as the tab holds it: its lines with the stream each came from. */
|
|
86
|
+
export interface ProcessView {
|
|
87
|
+
name: string
|
|
88
|
+
module: string
|
|
89
|
+
command: string
|
|
90
|
+
args: string[]
|
|
91
|
+
status: DevProcessInfo["status"]
|
|
92
|
+
code?: number
|
|
93
|
+
lines: Array<{ text: string; stderr: boolean }>
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function processView(info: DevProcessInfo): ProcessView {
|
|
97
|
+
const stderr = new Set(info.stderr)
|
|
98
|
+
return {
|
|
99
|
+
name: info.name,
|
|
100
|
+
module: info.module,
|
|
101
|
+
command: info.command,
|
|
102
|
+
args: info.args,
|
|
103
|
+
status: info.status,
|
|
104
|
+
...(info.code === undefined ? {} : { code: info.code }),
|
|
105
|
+
lines: info.lines.map((text, index) => ({ text, stderr: stderr.has(index) })),
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Applies a push to the process it names; `false` when the tab does not know that name. */
|
|
110
|
+
export function applyProcessEvent(views: Map<string, ProcessView>, event: ProcessEvent): boolean {
|
|
111
|
+
const view = views.get(event.name)
|
|
112
|
+
if (!view) return false
|
|
113
|
+
if (typeof event.line === "string") keep(view.lines, { text: event.line, stderr: event.stderr === true })
|
|
114
|
+
if (event.status) {
|
|
115
|
+
view.status = event.status
|
|
116
|
+
if (event.code === undefined) delete view.code
|
|
117
|
+
else view.code = event.code
|
|
118
|
+
}
|
|
119
|
+
return true
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** The words next to a process's status dot. */
|
|
123
|
+
export function statusText(view: Pick<ProcessView, "status" | "code">): string {
|
|
124
|
+
if (view.status === "running") return "running"
|
|
125
|
+
if (view.status === "failed") return "could not start"
|
|
126
|
+
return view.code === undefined ? "not running" : `exited with code ${view.code}`
|
|
127
|
+
}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { defineDevtoolsApp } from "hamur/app"
|
|
2
|
+
import type { DevProcessInfo } from "hamur/kit"
|
|
3
|
+
import { element, pill, tab, UI } from "../panel/ui.ts"
|
|
4
|
+
import {
|
|
5
|
+
applyProcessEvent,
|
|
6
|
+
filterLogs,
|
|
7
|
+
keep,
|
|
8
|
+
LOG_LEVELS,
|
|
9
|
+
logText,
|
|
10
|
+
parseLogEvent,
|
|
11
|
+
processView,
|
|
12
|
+
requestIdOf,
|
|
13
|
+
statusText,
|
|
14
|
+
type LogEntry,
|
|
15
|
+
type LogLevelName,
|
|
16
|
+
type ProcessEvent,
|
|
17
|
+
type ProcessView,
|
|
18
|
+
} from "./terminals-model.ts"
|
|
19
|
+
|
|
20
|
+
const messageOf = (error: unknown): string => (error instanceof Error ? error.message : String(error))
|
|
21
|
+
|
|
22
|
+
const SERVER = "server"
|
|
23
|
+
const LOGS = "/_hamur/logs"
|
|
24
|
+
|
|
25
|
+
const DOT_COLORS: Record<string, string> = { running: "bg-green", failed: "bg-red" }
|
|
26
|
+
const LEVEL_COLORS: Record<string, string> = { warn: "text-amber", error: "text-red", fatal: "text-red" }
|
|
27
|
+
const LINES = "m-0 px-3 py-2 flex-1 min-h-0 list-none overflow-auto bg-field font-mono text-xs"
|
|
28
|
+
const BAR = "flex-none flex flex-wrap items-center gap-2 px-3 py-2"
|
|
29
|
+
const PANE = "flex flex-col flex-1 min-h-0"
|
|
30
|
+
|
|
31
|
+
function dot(status: string): HTMLElement {
|
|
32
|
+
const node = element("span", { className: `flex-none size-2 rounded-full ${DOT_COLORS[status] ?? "bg-fg-3"}` })
|
|
33
|
+
node.dataset.status = status
|
|
34
|
+
return node
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export default defineDevtoolsApp({
|
|
38
|
+
async init(canvas, ctx) {
|
|
39
|
+
canvas.className = "flex flex-col flex-1 min-h-0"
|
|
40
|
+
const strip = element("div", {
|
|
41
|
+
className: "flex-none flex gap-0.5 px-3 pt-1 border-b border-line overflow-x-auto",
|
|
42
|
+
role: "tablist",
|
|
43
|
+
})
|
|
44
|
+
strip.setAttribute("aria-label", "Processes")
|
|
45
|
+
const note = element("p", { className: `m-0 px-4 py-1 ${UI.fail} empty:hidden` })
|
|
46
|
+
note.dataset.error = ""
|
|
47
|
+
const body = element("div", { className: "flex flex-col flex-1 min-h-0" })
|
|
48
|
+
canvas.append(strip, note, body)
|
|
49
|
+
|
|
50
|
+
const records: LogEntry[] = []
|
|
51
|
+
const levels = new Set<LogLevelName>(LOG_LEVELS.filter((level) => level !== "trace" && level !== "debug"))
|
|
52
|
+
let reqId = ""
|
|
53
|
+
let serverStatus = "running"
|
|
54
|
+
const views = new Map<string, ProcessView>()
|
|
55
|
+
let selected = SERVER
|
|
56
|
+
|
|
57
|
+
const chips = element("div", { className: "flex flex-wrap gap-1", role: "group" })
|
|
58
|
+
chips.setAttribute("aria-label", "Levels")
|
|
59
|
+
for (const level of LOG_LEVELS) {
|
|
60
|
+
const chip = element("button", { type: "button", className: `${pill(levels.has(level))} font-mono` }, level)
|
|
61
|
+
chip.dataset.level = level
|
|
62
|
+
chip.setAttribute("aria-pressed", String(levels.has(level)))
|
|
63
|
+
chip.addEventListener("click", () => {
|
|
64
|
+
if (levels.has(level)) levels.delete(level)
|
|
65
|
+
else levels.add(level)
|
|
66
|
+
chip.setAttribute("aria-pressed", String(levels.has(level)))
|
|
67
|
+
chip.className = `${pill(levels.has(level))} font-mono`
|
|
68
|
+
paintLogs()
|
|
69
|
+
})
|
|
70
|
+
chips.append(chip)
|
|
71
|
+
}
|
|
72
|
+
const reqInput = element("input", {
|
|
73
|
+
type: "search",
|
|
74
|
+
name: "reqId",
|
|
75
|
+
placeholder: "Request id",
|
|
76
|
+
className: `${UI.field} flex-1 min-w-40 font-mono`,
|
|
77
|
+
})
|
|
78
|
+
reqInput.setAttribute("aria-label", "Request id")
|
|
79
|
+
reqInput.addEventListener("input", () => {
|
|
80
|
+
reqId = reqInput.value
|
|
81
|
+
paintLogs()
|
|
82
|
+
})
|
|
83
|
+
const logList = element("ol", { className: LINES })
|
|
84
|
+
const serverPane = element(
|
|
85
|
+
"section",
|
|
86
|
+
{ className: PANE },
|
|
87
|
+
element("div", { className: BAR }, chips, reqInput),
|
|
88
|
+
logList,
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
const processLines = element("ol", { className: LINES })
|
|
92
|
+
const processState = element("span", { className: "flex items-center gap-1.5 mr-auto text-sm text-fg-3 break-all" })
|
|
93
|
+
const restart = element("button", { type: "button", className: UI.button }, "Restart")
|
|
94
|
+
const kill = element("button", { type: "button", className: UI.button }, "Kill")
|
|
95
|
+
const processPane = element(
|
|
96
|
+
"section",
|
|
97
|
+
{ className: PANE },
|
|
98
|
+
element("div", { className: BAR }, processState, restart, kill),
|
|
99
|
+
processLines,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
function setReqId(id: string): void {
|
|
103
|
+
reqId = id
|
|
104
|
+
reqInput.value = id
|
|
105
|
+
paintLogs()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function logItem(record: LogEntry): HTMLElement {
|
|
109
|
+
const item = element(
|
|
110
|
+
"li",
|
|
111
|
+
{ className: "flex gap-2 whitespace-pre-wrap break-all" },
|
|
112
|
+
element(
|
|
113
|
+
"span",
|
|
114
|
+
{ className: `flex-none w-11 uppercase ${LEVEL_COLORS[record.level] ?? "text-fg-3"}` },
|
|
115
|
+
record.level,
|
|
116
|
+
),
|
|
117
|
+
element("span", { className: "flex-1 min-w-0 text-fg-2" }, logText(record)),
|
|
118
|
+
)
|
|
119
|
+
item.dataset.level = record.level
|
|
120
|
+
const id = requestIdOf(record)
|
|
121
|
+
if (id) {
|
|
122
|
+
item.dataset.reqId = id
|
|
123
|
+
const link = element(
|
|
124
|
+
"button",
|
|
125
|
+
{
|
|
126
|
+
type: "button",
|
|
127
|
+
className: "p-0 border-0 bg-transparent text-fg-3 underline decoration-dotted cursor-pointer",
|
|
128
|
+
title: "Show this request only",
|
|
129
|
+
},
|
|
130
|
+
id,
|
|
131
|
+
)
|
|
132
|
+
link.addEventListener("click", () => setReqId(id))
|
|
133
|
+
item.append(link)
|
|
134
|
+
}
|
|
135
|
+
return item
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function paintLogs(): void {
|
|
139
|
+
if (selected !== SERVER) return
|
|
140
|
+
const shown = filterLogs(records, { levels, reqId })
|
|
141
|
+
const atEnd = logList.scrollTop + logList.clientHeight >= logList.scrollHeight - 4
|
|
142
|
+
logList.replaceChildren(
|
|
143
|
+
...(shown.length
|
|
144
|
+
? shown.map(logItem)
|
|
145
|
+
: [
|
|
146
|
+
element(
|
|
147
|
+
"li",
|
|
148
|
+
{ className: UI.hint },
|
|
149
|
+
records.length ? "No line matches the filters" : "No server log yet",
|
|
150
|
+
),
|
|
151
|
+
]),
|
|
152
|
+
)
|
|
153
|
+
if (atEnd) logList.scrollTop = logList.scrollHeight
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function processItem(line: { text: string; stderr: boolean }): HTMLElement {
|
|
157
|
+
const item = element(
|
|
158
|
+
"li",
|
|
159
|
+
{ className: `flex gap-2 whitespace-pre-wrap break-all ${line.stderr ? "text-red" : "text-fg-2"}` },
|
|
160
|
+
element("span", { className: "flex-1 min-w-0" }, line.text),
|
|
161
|
+
)
|
|
162
|
+
if (line.stderr) item.dataset.stream = "stderr"
|
|
163
|
+
return item
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function paintProcess(): void {
|
|
167
|
+
const view = views.get(selected)
|
|
168
|
+
if (!view) return
|
|
169
|
+
processState.replaceChildren(
|
|
170
|
+
dot(view.status),
|
|
171
|
+
element("code", { className: "font-mono text-xs text-fg-2" }, [view.command, ...view.args].join(" ")),
|
|
172
|
+
` ${statusText(view)}`,
|
|
173
|
+
)
|
|
174
|
+
kill.disabled = view.status !== "running"
|
|
175
|
+
const atEnd = processLines.scrollTop + processLines.clientHeight >= processLines.scrollHeight - 4
|
|
176
|
+
processLines.replaceChildren(
|
|
177
|
+
...(view.lines.length ? view.lines.map(processItem) : [element("li", { className: UI.hint }, "No output yet")]),
|
|
178
|
+
)
|
|
179
|
+
if (atEnd) processLines.scrollTop = processLines.scrollHeight
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function paintStrip(): void {
|
|
183
|
+
const names = [SERVER, ...views.keys()]
|
|
184
|
+
strip.replaceChildren(
|
|
185
|
+
...names.map((name) => {
|
|
186
|
+
const status = name === SERVER ? serverStatus : views.get(name)!.status
|
|
187
|
+
const button = element(
|
|
188
|
+
"button",
|
|
189
|
+
{ type: "button", role: "tab", className: `${tab(name === selected)} gap-1.5` },
|
|
190
|
+
dot(status),
|
|
191
|
+
name,
|
|
192
|
+
)
|
|
193
|
+
button.dataset.name = name
|
|
194
|
+
button.setAttribute("aria-selected", String(name === selected))
|
|
195
|
+
button.addEventListener("click", () => select(name))
|
|
196
|
+
return button
|
|
197
|
+
}),
|
|
198
|
+
)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function select(name: string): void {
|
|
202
|
+
selected = views.has(name) ? name : SERVER
|
|
203
|
+
paintStrip()
|
|
204
|
+
if (selected === SERVER) {
|
|
205
|
+
body.replaceChildren(serverPane)
|
|
206
|
+
paintLogs()
|
|
207
|
+
} else {
|
|
208
|
+
body.replaceChildren(processPane)
|
|
209
|
+
paintProcess()
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async function act(action: "restart" | "kill"): Promise<void> {
|
|
214
|
+
const name = selected
|
|
215
|
+
restart.disabled = kill.disabled = true
|
|
216
|
+
try {
|
|
217
|
+
const response = await fetch(`api/processes/${encodeURIComponent(name)}/${action}`, { method: "POST" })
|
|
218
|
+
if (!response.ok)
|
|
219
|
+
throw new Error(
|
|
220
|
+
((await response.json().catch(() => ({}))) as { error?: string }).error ??
|
|
221
|
+
`api/processes answered ${response.status}`,
|
|
222
|
+
)
|
|
223
|
+
note.textContent = ""
|
|
224
|
+
} catch (error) {
|
|
225
|
+
note.textContent = messageOf(error)
|
|
226
|
+
} finally {
|
|
227
|
+
restart.disabled = false
|
|
228
|
+
if (selected === name) paintProcess()
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
restart.addEventListener("click", () => void act("restart"))
|
|
232
|
+
kill.addEventListener("click", () => void act("kill"))
|
|
233
|
+
|
|
234
|
+
let pending: ReturnType<typeof setTimeout> | undefined
|
|
235
|
+
const schedule = () => {
|
|
236
|
+
if (pending !== undefined) return
|
|
237
|
+
pending = setTimeout(() => {
|
|
238
|
+
pending = undefined
|
|
239
|
+
paintStrip()
|
|
240
|
+
if (selected === SERVER) paintLogs()
|
|
241
|
+
else paintProcess()
|
|
242
|
+
}, 50)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const stream = new EventSource(LOGS)
|
|
246
|
+
let dropped = false
|
|
247
|
+
stream.onopen = () => {
|
|
248
|
+
// A reconnected stream replays the newest records again.
|
|
249
|
+
if (dropped) records.length = 0
|
|
250
|
+
dropped = false
|
|
251
|
+
serverStatus = "running"
|
|
252
|
+
schedule()
|
|
253
|
+
}
|
|
254
|
+
stream.onmessage = (event) => {
|
|
255
|
+
const record = parseLogEvent(String(event.data))
|
|
256
|
+
if (!record) return
|
|
257
|
+
keep(records, record)
|
|
258
|
+
schedule()
|
|
259
|
+
}
|
|
260
|
+
stream.onerror = () => {
|
|
261
|
+
dropped = true
|
|
262
|
+
serverStatus = stream.readyState === EventSource.CLOSED ? "failed" : "exited"
|
|
263
|
+
schedule()
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const off = ctx.on("devtools:process", (data) => {
|
|
267
|
+
if (applyProcessEvent(views, data as ProcessEvent)) schedule()
|
|
268
|
+
else void loadProcesses()
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
async function loadProcesses(): Promise<void> {
|
|
272
|
+
try {
|
|
273
|
+
const response = await fetch("api/processes")
|
|
274
|
+
const rows = (await response.json()) as DevProcessInfo[] & { error?: string }
|
|
275
|
+
if (!response.ok) throw new Error(rows.error ?? `api/processes answered ${response.status}`)
|
|
276
|
+
views.clear()
|
|
277
|
+
for (const row of rows) views.set(row.name, processView(row))
|
|
278
|
+
note.textContent = ""
|
|
279
|
+
} catch (error) {
|
|
280
|
+
note.textContent = messageOf(error)
|
|
281
|
+
}
|
|
282
|
+
select(selected)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
select(SERVER)
|
|
286
|
+
await loadProcesses()
|
|
287
|
+
|
|
288
|
+
return () => {
|
|
289
|
+
off()
|
|
290
|
+
stream.close()
|
|
291
|
+
clearTimeout(pending)
|
|
292
|
+
}
|
|
293
|
+
},
|
|
294
|
+
})
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import type { DevHook } from "hamur/kit"
|
|
2
|
+
import type { SpanRow } from "../dev/timeline.ts"
|
|
3
|
+
|
|
4
|
+
export type EventKind = "navigation" | "hydration" | "update" | "middleware" | "loader" | "fetch" | "chunk"
|
|
5
|
+
|
|
6
|
+
/** One client event on the Timeline; `start` and `end` are epoch milliseconds. */
|
|
7
|
+
export interface TimelineEvent {
|
|
8
|
+
id: string
|
|
9
|
+
kind: EventKind
|
|
10
|
+
name: string
|
|
11
|
+
start: number
|
|
12
|
+
end: number
|
|
13
|
+
/** A loader's trace id and the span id it sent as `traceparent`'s parent. */
|
|
14
|
+
trace?: string
|
|
15
|
+
span?: string
|
|
16
|
+
detail: Record<string, string>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface MarkLike {
|
|
20
|
+
name: string
|
|
21
|
+
startTime: number
|
|
22
|
+
duration: number
|
|
23
|
+
detail?: unknown
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ResourceLike {
|
|
27
|
+
name: string
|
|
28
|
+
initiatorType: string
|
|
29
|
+
startTime: number
|
|
30
|
+
duration: number
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** A line of the waterfall: a client event (depth 0) or a server span below it. */
|
|
34
|
+
export interface TimelineRow {
|
|
35
|
+
id: string
|
|
36
|
+
kind: EventKind | "server"
|
|
37
|
+
label: string
|
|
38
|
+
start: number
|
|
39
|
+
end: number
|
|
40
|
+
depth: number
|
|
41
|
+
detail: Record<string, string>
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const PANEL_PATHS = /\/_hamur\/(?:devtools|reload|logs|overlay)(?:[/?]|$)/
|
|
45
|
+
const CHUNK = /\.(?:m?js|css)(?:$|\?)/
|
|
46
|
+
|
|
47
|
+
function hamurDetail(mark: MarkLike): { name?: string; phase?: string; trace?: string; span?: string } {
|
|
48
|
+
const detail = mark.detail as { hamur?: Record<string, unknown> } | null | undefined
|
|
49
|
+
const hamur = detail?.hamur ?? {}
|
|
50
|
+
const text = (value: unknown) => (typeof value === "string" ? value : undefined)
|
|
51
|
+
return { name: text(hamur.name), phase: text(hamur.phase), trace: text(hamur.trace), span: text(hamur.span) }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The client events of the page: its `hamur:*` measures and the fetches and script and style loads Resource Timing recorded. */
|
|
55
|
+
export function clientEvents(
|
|
56
|
+
marks: readonly MarkLike[],
|
|
57
|
+
resources: readonly ResourceLike[],
|
|
58
|
+
timeOrigin: number,
|
|
59
|
+
): TimelineEvent[] {
|
|
60
|
+
const events: TimelineEvent[] = []
|
|
61
|
+
const at = (entry: { startTime: number; duration: number }) => ({
|
|
62
|
+
start: timeOrigin + entry.startTime,
|
|
63
|
+
end: timeOrigin + entry.startTime + entry.duration,
|
|
64
|
+
})
|
|
65
|
+
marks.forEach((mark, index) => {
|
|
66
|
+
const { name, phase, trace, span } = hamurDetail(mark)
|
|
67
|
+
if (mark.name === "hamur:navigation") {
|
|
68
|
+
const kind: EventKind = phase === "hydrate" ? "hydration" : phase === "update" ? "update" : "navigation"
|
|
69
|
+
events.push({ id: `navigation:${index}`, kind, name: name ?? "", ...at(mark), detail: { phase: phase ?? "" } })
|
|
70
|
+
} else if (mark.name.startsWith("hamur:middleware:") || mark.name.startsWith("hamur:loader:")) {
|
|
71
|
+
const loader = mark.name.startsWith("hamur:loader:")
|
|
72
|
+
events.push({
|
|
73
|
+
id: `${loader ? "loader" : "middleware"}:${index}`,
|
|
74
|
+
kind: loader ? "loader" : "middleware",
|
|
75
|
+
name: mark.name.slice(loader ? "hamur:loader:".length : "hamur:middleware:".length),
|
|
76
|
+
...at(mark),
|
|
77
|
+
...(trace ? { trace } : {}),
|
|
78
|
+
...(span ? { span } : {}),
|
|
79
|
+
detail: trace ? { trace, span: span ?? "" } : {},
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
resources.forEach((resource, index) => {
|
|
84
|
+
let path: string
|
|
85
|
+
try {
|
|
86
|
+
const url = new URL(resource.name, "http://page.invalid")
|
|
87
|
+
path = `${url.pathname}${url.search}`
|
|
88
|
+
} catch {
|
|
89
|
+
return
|
|
90
|
+
}
|
|
91
|
+
if (PANEL_PATHS.test(path)) return
|
|
92
|
+
const kind: EventKind | undefined = CHUNK.test(path)
|
|
93
|
+
? "chunk"
|
|
94
|
+
: resource.initiatorType === "fetch" || resource.initiatorType === "xmlhttprequest"
|
|
95
|
+
? "fetch"
|
|
96
|
+
: undefined
|
|
97
|
+
if (kind)
|
|
98
|
+
events.push({
|
|
99
|
+
id: `${kind}:${index}`,
|
|
100
|
+
kind,
|
|
101
|
+
name: path,
|
|
102
|
+
...at(resource),
|
|
103
|
+
detail: { initiator: resource.initiatorType },
|
|
104
|
+
})
|
|
105
|
+
})
|
|
106
|
+
return events.sort((a, b) => a.start - b.start || a.end - b.end)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The spans of `spans` below the span `parentId`, children after their parent and by start; `depth` 1 is a child of `parentId`. */
|
|
110
|
+
export function nestSpans(spans: readonly SpanRow[], parentId: string): Array<{ span: SpanRow; depth: number }> {
|
|
111
|
+
const children = new Map<string, SpanRow[]>()
|
|
112
|
+
for (const span of spans) {
|
|
113
|
+
if (span.parentSpanId === undefined) continue
|
|
114
|
+
const list = children.get(span.parentSpanId)
|
|
115
|
+
if (list) list.push(span)
|
|
116
|
+
else children.set(span.parentSpanId, [span])
|
|
117
|
+
}
|
|
118
|
+
const seen = new Set<string>()
|
|
119
|
+
const walk = (parent: string, depth: number): Array<{ span: SpanRow; depth: number }> =>
|
|
120
|
+
(children.get(parent) ?? [])
|
|
121
|
+
.sort((a, b) => a.startTime - b.startTime)
|
|
122
|
+
.flatMap((span) => {
|
|
123
|
+
if (seen.has(span.spanId)) return []
|
|
124
|
+
seen.add(span.spanId)
|
|
125
|
+
return [{ span, depth }, ...walk(span.spanId, depth + 1)]
|
|
126
|
+
})
|
|
127
|
+
return walk(parentId, 1)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The waterfall rows: each event, then the server spans of its trace nested below it by `traceparent`. */
|
|
131
|
+
export function timelineRows(
|
|
132
|
+
events: readonly TimelineEvent[],
|
|
133
|
+
spansByTrace: ReadonlyMap<string, readonly SpanRow[]>,
|
|
134
|
+
): TimelineRow[] {
|
|
135
|
+
return events.flatMap((event): TimelineRow[] => {
|
|
136
|
+
const row: TimelineRow = {
|
|
137
|
+
id: event.id,
|
|
138
|
+
kind: event.kind,
|
|
139
|
+
label: event.name,
|
|
140
|
+
start: event.start,
|
|
141
|
+
end: event.end,
|
|
142
|
+
depth: 0,
|
|
143
|
+
detail: event.detail,
|
|
144
|
+
}
|
|
145
|
+
const spans = event.trace && event.span ? spansByTrace.get(event.trace) : undefined
|
|
146
|
+
if (!spans || !event.span) return [row]
|
|
147
|
+
const nested = nestSpans(spans, event.span).map(({ span, depth }): TimelineRow => {
|
|
148
|
+
const detail: Record<string, string> = {
|
|
149
|
+
kind: span.kind,
|
|
150
|
+
status: span.status.code,
|
|
151
|
+
...(span.status.message ? { message: span.status.message } : {}),
|
|
152
|
+
}
|
|
153
|
+
for (const [key, value] of Object.entries(span.attributes))
|
|
154
|
+
detail[key] = typeof value === "string" ? value : JSON.stringify(value)
|
|
155
|
+
return {
|
|
156
|
+
id: `${event.id}:${span.spanId}`,
|
|
157
|
+
kind: "server",
|
|
158
|
+
label: span.name,
|
|
159
|
+
start: span.startTime,
|
|
160
|
+
end: span.endTime,
|
|
161
|
+
depth,
|
|
162
|
+
detail,
|
|
163
|
+
}
|
|
164
|
+
})
|
|
165
|
+
return [row, ...nested]
|
|
166
|
+
})
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export interface HookRow {
|
|
170
|
+
hook: string
|
|
171
|
+
kind: "build" | "app" | "server"
|
|
172
|
+
calls: number
|
|
173
|
+
totalMs: number
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** The Hooks view: the runner's build and server hooks, and the app hooks the page measured (`hamur:hook:<name>`), most time first. */
|
|
177
|
+
export function hookRows(runner: readonly DevHook[], marks: readonly MarkLike[]): HookRow[] {
|
|
178
|
+
const app = new Map<string, HookRow>()
|
|
179
|
+
for (const mark of marks) {
|
|
180
|
+
if (!mark.name.startsWith("hamur:hook:")) continue
|
|
181
|
+
const hook = mark.name.slice("hamur:hook:".length)
|
|
182
|
+
const row = app.get(hook) ?? { hook, kind: "app" as const, calls: 0, totalMs: 0 }
|
|
183
|
+
row.calls++
|
|
184
|
+
row.totalMs += mark.duration
|
|
185
|
+
app.set(hook, row)
|
|
186
|
+
}
|
|
187
|
+
return [...runner, ...app.values()].sort((a, b) => b.totalMs - a.totalMs || a.hook.localeCompare(b.hook))
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** `1.234` → `1.2 ms`, `1234` → `1.23 s`. */
|
|
191
|
+
export function formatMs(ms: number): string {
|
|
192
|
+
return ms >= 1000 ? `${(ms / 1000).toFixed(2)} s` : `${ms.toFixed(1)} ms`
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** A tick distance of 1, 2 or 5 times a power of ten that puts at most `max` ticks on `total` milliseconds. */
|
|
196
|
+
export function tickStep(total: number, max = 6): number {
|
|
197
|
+
const raw = Math.max(total, 1) / max
|
|
198
|
+
const power = 10 ** Math.floor(Math.log10(raw))
|
|
199
|
+
return [1, 2, 5, 10].map((factor) => factor * power).find((step) => step >= raw) ?? power * 10
|
|
200
|
+
}
|