teemux 1.2.0 → 1.4.0
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/README.md +10 -6
- package/dist/teemux.js +35 -4
- package/dist/teemux.js.map +1 -1
- package/package.json +1 -1
- package/src/LogServer.ts +5 -0
- package/src/teemux.ts +40 -3
package/README.md
CHANGED
|
@@ -69,13 +69,13 @@ All process logs are aggregated at http://127.0.0.1:8336/
|
|
|
69
69
|
curl http://127.0.0.1:8336/
|
|
70
70
|
|
|
71
71
|
# View logs from a specific process
|
|
72
|
-
curl "http://127.0.0.1:8336/?include=
|
|
72
|
+
curl "http://127.0.0.1:8336/?include=api"
|
|
73
73
|
|
|
74
74
|
# View only errors (using wildcard for case variations)
|
|
75
75
|
curl "http://127.0.0.1:8336/?include=*error*,*Error*,*ERROR*"
|
|
76
76
|
|
|
77
77
|
# View logs from api OR worker
|
|
78
|
-
curl "http://127.0.0.1:8336/?include=
|
|
78
|
+
curl "http://127.0.0.1:8336/?include=api,worker"
|
|
79
79
|
|
|
80
80
|
# Exclude noisy logs (using wildcard)
|
|
81
81
|
curl "http://127.0.0.1:8336/?exclude=health*,DEBUG"
|
|
@@ -95,13 +95,13 @@ Patterns support `*` as a wildcard (matches any characters):
|
|
|
95
95
|
|
|
96
96
|
```bash
|
|
97
97
|
# Show only logs from the api process
|
|
98
|
-
curl "http://127.0.0.1:8336/?include=
|
|
98
|
+
curl "http://127.0.0.1:8336/?include=api"
|
|
99
99
|
|
|
100
100
|
# Show only error logs (using wildcard)
|
|
101
101
|
curl "http://127.0.0.1:8336/?include=*error*"
|
|
102
102
|
|
|
103
103
|
# Show logs from api OR worker
|
|
104
|
-
curl "http://127.0.0.1:8336/?include=
|
|
104
|
+
curl "http://127.0.0.1:8336/?include=api,worker"
|
|
105
105
|
|
|
106
106
|
# Hide healthcheck and ping logs
|
|
107
107
|
curl "http://127.0.0.1:8336/?exclude=health*,ping"
|
|
@@ -110,10 +110,10 @@ curl "http://127.0.0.1:8336/?exclude=health*,ping"
|
|
|
110
110
|
curl "http://127.0.0.1:8336/?include=*GET*/api*"
|
|
111
111
|
|
|
112
112
|
# Show api logs but exclude verbose debug output
|
|
113
|
-
curl "http://127.0.0.1:8336/?include=
|
|
113
|
+
curl "http://127.0.0.1:8336/?include=api&exclude=DEBUG,TRACE"
|
|
114
114
|
|
|
115
115
|
# In browser
|
|
116
|
-
open "http://127.0.0.1:8336/?include=
|
|
116
|
+
open "http://127.0.0.1:8336/?include=api&exclude=health*"
|
|
117
117
|
```
|
|
118
118
|
|
|
119
119
|
Filters apply to both buffered logs and new incoming logs in real-time.
|
|
@@ -140,6 +140,10 @@ GET /health 200
|
|
|
140
140
|
|
|
141
141
|
## FAQ
|
|
142
142
|
|
|
143
|
+
### What's the origin of the name?
|
|
144
|
+
|
|
145
|
+
The name combines **tee** (the Unix command that duplicates output) and **mux** (multiplexer) – it multiplexes multiple log streams into one.
|
|
146
|
+
|
|
143
147
|
### How does teemux work?
|
|
144
148
|
|
|
145
149
|
teemux uses automatic leader discovery to coordinate log aggregation across multiple processes:
|
package/dist/teemux.js
CHANGED
|
@@ -533,6 +533,11 @@ var LogServer = class {
|
|
|
533
533
|
if (highlightInput.value) newParams.set('highlight', highlightInput.value);
|
|
534
534
|
const newUrl = newParams.toString() ? '?' + newParams.toString() : window.location.pathname;
|
|
535
535
|
history.replaceState(null, '', newUrl);
|
|
536
|
+
|
|
537
|
+
// Jump to bottom and resume tailing after filter change
|
|
538
|
+
container.scrollTop = container.scrollHeight;
|
|
539
|
+
tailing = true;
|
|
540
|
+
updateTailButton();
|
|
536
541
|
};
|
|
537
542
|
|
|
538
543
|
const trimBuffer = () => {
|
|
@@ -724,10 +729,31 @@ const runProcess = async (name, command, client) => {
|
|
|
724
729
|
});
|
|
725
730
|
const pid = child.pid ?? 0;
|
|
726
731
|
await client.event("start", pid);
|
|
727
|
-
|
|
728
|
-
|
|
732
|
+
let rlStdout = null;
|
|
733
|
+
let rlStderr = null;
|
|
734
|
+
if (child.stdout) {
|
|
735
|
+
rlStdout = readline.createInterface({ input: child.stdout });
|
|
736
|
+
rlStdout.on("line", (line) => client.log(line, "stdout"));
|
|
737
|
+
}
|
|
738
|
+
if (child.stderr) {
|
|
739
|
+
rlStderr = readline.createInterface({ input: child.stderr });
|
|
740
|
+
rlStderr.on("line", (line) => client.log(line, "stderr"));
|
|
741
|
+
}
|
|
742
|
+
let signalCount = 0;
|
|
743
|
+
const onSignal = () => {
|
|
744
|
+
signalCount++;
|
|
745
|
+
if (signalCount >= 2 && child.pid && !child.killed) child.kill("SIGKILL");
|
|
746
|
+
};
|
|
747
|
+
process.on("SIGINT", onSignal);
|
|
748
|
+
process.on("SIGTERM", onSignal);
|
|
749
|
+
process.on("SIGHUP", onSignal);
|
|
729
750
|
return new Promise((resolve) => {
|
|
730
751
|
child.on("close", async (code) => {
|
|
752
|
+
rlStdout?.close();
|
|
753
|
+
rlStderr?.close();
|
|
754
|
+
process.off("SIGINT", onSignal);
|
|
755
|
+
process.off("SIGTERM", onSignal);
|
|
756
|
+
process.off("SIGHUP", onSignal);
|
|
731
757
|
await client.flush();
|
|
732
758
|
await client.event("exit", pid, code ?? 0);
|
|
733
759
|
resolve(code ?? 0);
|
|
@@ -843,8 +869,13 @@ const main = async () => {
|
|
|
843
869
|
if (!await waitForServer(port)) console.error("[teemux] Could not connect to server. Is another instance running?");
|
|
844
870
|
leaderMonitor = startLeaderMonitoring(server, port);
|
|
845
871
|
}
|
|
846
|
-
const
|
|
847
|
-
|
|
872
|
+
const client = new LogClient(name, port);
|
|
873
|
+
const cleanup = async () => {
|
|
874
|
+
leaderMonitor?.stop();
|
|
875
|
+
if (isServer) await server.stop();
|
|
876
|
+
};
|
|
877
|
+
const exitCode = await runProcess(name, command, client);
|
|
878
|
+
await cleanup();
|
|
848
879
|
process.exit(exitCode);
|
|
849
880
|
};
|
|
850
881
|
main().catch((error) => {
|
package/dist/teemux.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"teemux.js","names":["RESET","DIM","RED"],"sources":["../src/utils/stripHtmlTags.ts","../src/utils/unescapeHtml.ts","../src/utils/highlightJson.ts","../src/utils/linkifyUrls.ts","../src/utils/stripAnsi.ts","../src/utils/matchesFilters.ts","../src/LogServer.ts","../src/teemux.ts"],"sourcesContent":["/**\n * Strip HTML tags from a string, leaving only text content.\n */\nexport const stripHtmlTags = (html: string): string => {\n return html.replaceAll(/<[^>]*>/gu, '');\n};\n","/**\n * Unescape HTML entities back to their original characters.\n */\nexport const unescapeHtml = (text: string): string => {\n return text\n .replaceAll('"', '\"')\n .replaceAll('&', '&')\n .replaceAll('<', '<')\n .replaceAll('>', '>')\n .replaceAll(''', \"'\")\n .replaceAll(''', \"'\");\n};\n","import { stripHtmlTags } from './stripHtmlTags.js';\nimport { unescapeHtml } from './unescapeHtml.js';\n\n/**\n * Apply syntax highlighting to JSON text that uses HTML-escaped quotes (").\n * Uses placeholder technique to avoid double-wrapping strings.\n */\nexport const highlightJsonText = (text: string): string => {\n // First, extract and mark all JSON strings with placeholders\n const strings: string[] = [];\n let result = text.replaceAll(\n /"((?:(?!").)*)"/gu,\n (_match, content) => {\n strings.push(content as string);\n return `\\u0000STR${strings.length - 1}\\u0000`;\n },\n );\n\n // Booleans and null\n result = result.replaceAll(\n /\\b(true|false|null)\\b/gu,\n '<span class=\"json-bool\">$1</span>',\n );\n\n // Numbers\n result = result.replaceAll(\n /(?<!\\w)(-?\\d+(?:\\.\\d+)?(?:[eE][+-]?\\d+)?)\\b/gu,\n '<span class=\"json-number\">$1</span>',\n );\n\n // Restore strings with appropriate highlighting\n result = result.replaceAll(\n /\\0STR(\\d+)\\0(\\s*:)?/gu,\n (_match, index, colon) => {\n const content = strings[Number.parseInt(index as string, 10)];\n if (colon) {\n // This is a key\n return `<span class=\"json-key\">"${content}"</span>${colon}`;\n }\n\n // This is a value\n return `<span class=\"json-string\">"${content}"</span>`;\n },\n );\n\n return result;\n};\n\n/**\n * Process HTML text, applying JSON highlighting only to text outside of HTML tags.\n */\nexport const syntaxHighlightJson = (html: string): string => {\n let result = '';\n let index = 0;\n\n while (index < html.length) {\n if (html[index] === '<') {\n // Find end of tag\n const tagEnd = html.indexOf('>', index);\n if (tagEnd === -1) {\n result += html.slice(index);\n break;\n }\n\n result += html.slice(index, tagEnd + 1);\n index = tagEnd + 1;\n } else {\n // Find next tag or end of string\n const nextTag = html.indexOf('<', index);\n const textEnd = nextTag === -1 ? html.length : nextTag;\n const text = html.slice(index, textEnd);\n\n // Highlight JSON syntax in this text segment\n result += highlightJsonText(text);\n index = textEnd;\n }\n }\n\n return result;\n};\n\n/**\n * Detect if the content (after prefix) is valid JSON and apply syntax highlighting.\n * Returns the original HTML if not valid JSON.\n */\nexport const highlightJson = (html: string): string => {\n // Extract the text content (strip HTML tags) to check if it's JSON\n const textContent = stripHtmlTags(html);\n\n // Unescape HTML entities for JSON parsing\n const unescaped = unescapeHtml(textContent);\n\n // Find where the actual log content starts (after the prefix like [name])\n const prefixMatch = /^\\[[\\w-]+\\]\\s*/u.exec(unescaped);\n const prefix = prefixMatch?.[0] ?? '';\n const content = unescaped.slice(prefix.length).trim();\n\n // Check if the content is valid JSON\n if (!content.startsWith('{') && !content.startsWith('[')) {\n return html;\n }\n\n try {\n JSON.parse(content);\n } catch {\n return html;\n }\n\n // It's valid JSON - now highlight it\n // Find the position after the prefix span in the HTML\n const prefixHtmlMatch = /^<span[^>]*>\\[[^\\]]+\\]<\\/span>\\s*/u.exec(html);\n const htmlPrefix = prefixHtmlMatch?.[0] ?? '';\n const jsonHtml = html.slice(htmlPrefix.length);\n\n // Apply syntax highlighting to the JSON portion\n const highlighted = syntaxHighlightJson(jsonHtml);\n\n return htmlPrefix + highlighted;\n};\n","/**\n * Convert URLs in HTML text to clickable anchor tags.\n * Supports http://, https://, and file:// URLs.\n * Avoids double-linking URLs that are already in href attributes.\n */\nexport const linkifyUrls = (html: string): string => {\n // Match URLs that are not already inside href attributes\n // Supports http://, https://, and file:// URLs\n // Exclude common delimiters and HTML entities (" & etc)\n const urlRegex = /(?<!href=[\"'])(?:https?|file):\\/\\/[^\\s<>\"'{}&]+/gu;\n\n return html.replaceAll(urlRegex, (url) => {\n // Remove trailing punctuation that's likely not part of the URL\n const cleanUrl = url.replace(/[.,;:!?)\\]]+$/u, '');\n const trailing = url.slice(cleanUrl.length);\n\n // Escape HTML entities in the URL for the href attribute\n const escapedHref = cleanUrl\n .replaceAll('&', '&')\n .replaceAll('\"', '"');\n\n return `<a href=\"${escapedHref}\" target=\"_blank\" rel=\"noopener\">${cleanUrl}</a>${trailing}`;\n });\n};\n","/**\n * Strip ANSI escape codes from text.\n * Removes color codes and other terminal formatting sequences.\n */\nexport const stripAnsi = (text: string): string => {\n // eslint-disable-next-line no-control-regex\n return text.replaceAll(/\\u001B\\[[\\d;]*m/gu, '');\n};\n","import { stripAnsi } from './stripAnsi.js';\n\n/**\n * Convert a glob pattern (with * wildcards) to a RegExp.\n * - `*` matches any characters (zero or more)\n * - All other characters are escaped for literal matching\n */\nconst globToRegex = (pattern: string): RegExp => {\n // Escape regex special characters except *\n const escaped = pattern.replaceAll(/[$()+.?[\\\\\\]^{|}]/gu, '\\\\$&');\n // Convert * to .*\n const regexPattern = escaped.replaceAll('*', '.*');\n return new RegExp(regexPattern, 'iu');\n};\n\n/**\n * Check if text matches a pattern (supports * glob wildcards).\n * If no wildcards, does a simple substring match for better performance.\n */\nconst matchesPattern = (text: string, pattern: string): boolean => {\n if (pattern.includes('*')) {\n return globToRegex(pattern).test(text);\n }\n\n return text.includes(pattern.toLowerCase());\n};\n\n/**\n * Check if a line matches the given filter criteria.\n * @param line - The line to check (may contain ANSI codes)\n * @param includes - Patterns where ANY match includes the line (OR logic), case-insensitive. Supports * wildcards.\n * @param excludes - Patterns where ANY match excludes the line (OR logic), case-insensitive. Supports * wildcards.\n * @returns true if the line should be included, false if filtered out\n */\nexport const matchesFilters = (\n line: string,\n includes: string[],\n excludes: string[],\n): boolean => {\n const plainText = stripAnsi(line).toLowerCase();\n\n // Any include must match (OR logic) - case insensitive\n if (includes.length > 0) {\n const anyIncludeMatches = includes.some((pattern) =>\n matchesPattern(plainText, pattern),\n );\n\n if (!anyIncludeMatches) {\n return false;\n }\n }\n\n // None of the excludes should match (OR logic for exclusion) - case insensitive\n if (excludes.length > 0) {\n const anyExcludeMatches = excludes.some((pattern) =>\n matchesPattern(plainText, pattern),\n );\n\n if (anyExcludeMatches) {\n return false;\n }\n }\n\n return true;\n};\n","import { highlightJson } from './utils/highlightJson.js';\nimport { linkifyUrls } from './utils/linkifyUrls.js';\nimport { matchesFilters } from './utils/matchesFilters.js';\nimport { stripAnsi } from './utils/stripAnsi.js';\nimport Convert from 'ansi-to-html';\nimport http from 'node:http';\nimport { performance } from 'node:perf_hooks';\nimport { URL } from 'node:url';\n\nconst COLORS = [\n '\\u001B[36m',\n '\\u001B[33m',\n '\\u001B[32m',\n '\\u001B[35m',\n '\\u001B[34m',\n '\\u001B[91m',\n '\\u001B[92m',\n '\\u001B[93m',\n];\nconst RESET = '\\u001B[0m';\nconst DIM = '\\u001B[90m';\nconst RED = '\\u001B[91m';\nconst HOST = '0.0.0.0';\n\ntype BufferedLog = {\n line: string;\n timestamp: number;\n};\n\ntype EventPayload = {\n code?: number;\n event: 'exit' | 'start';\n name: string;\n pid: number;\n timestamp: number;\n};\n\ntype LogPayload = {\n line: string;\n name: string;\n timestamp: number;\n type: LogType;\n};\n\ntype LogType = 'stderr' | 'stdout';\n\ntype StreamClient = {\n excludes: string[];\n includes: string[];\n isBrowser: boolean;\n response: http.ServerResponse;\n};\n\nexport class LogServer {\n private ansiConverter = new Convert({ escapeXML: true, newline: true });\n\n private buffer: BufferedLog[] = [];\n\n private clients = new Set<StreamClient>();\n\n private colorIndex = 0;\n\n private colorMap = new Map<string, string>();\n\n private port: number;\n\n private server: http.Server | null = null;\n\n private tailSize: number;\n\n constructor(port: number, tailSize: number = 1_000) {\n this.port = port;\n this.tailSize = tailSize;\n }\n\n getPort(): number {\n if (this.server) {\n const address = this.server.address();\n if (address && typeof address === 'object') {\n return address.port;\n }\n }\n\n return this.port;\n }\n\n start(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.server = http.createServer((request, response) => {\n // Handle streaming GET request\n if (request.method === 'GET' && request.url?.startsWith('/')) {\n const url = new URL(request.url, `http://${request.headers.host}`);\n const includeParameter = url.searchParams.get('include');\n const includes = includeParameter\n ? includeParameter\n .split(',')\n .map((term) => term.trim())\n .filter(Boolean)\n : [];\n const excludeParameter = url.searchParams.get('exclude');\n const excludes = excludeParameter\n ? excludeParameter\n .split(',')\n .map((pattern) => pattern.trim())\n .filter(Boolean)\n : [];\n\n const userAgent = request.headers['user-agent'] ?? '';\n const isBrowser = userAgent.includes('Mozilla');\n\n // Sort buffer by timestamp\n const sortedBuffer = this.buffer.toSorted(\n (a, b) => a.timestamp - b.timestamp,\n );\n\n if (isBrowser) {\n // Browser: send all logs, filtering is done client-side\n response.writeHead(200, {\n 'Cache-Control': 'no-cache',\n Connection: 'keep-alive',\n 'Content-Type': 'text/html; charset=utf-8',\n 'X-Content-Type-Options': 'nosniff',\n });\n\n // Send HTML header with styling\n response.write(this.getHtmlHeader());\n\n // Send all buffered logs as HTML\n for (const entry of sortedBuffer) {\n response.write(this.getHtmlLine(entry.line));\n }\n } else {\n // Non-browser (curl, etc): apply server-side filtering\n const filteredBuffer = sortedBuffer.filter((entry) =>\n matchesFilters(entry.line, includes, excludes),\n );\n\n response.writeHead(200, {\n 'Cache-Control': 'no-cache',\n Connection: 'keep-alive',\n 'Content-Type': 'text/plain; charset=utf-8',\n 'X-Content-Type-Options': 'nosniff',\n });\n\n // Send filtered logs as plain text (strip ANSI)\n for (const entry of filteredBuffer) {\n response.write(stripAnsi(entry.line) + '\\n');\n }\n }\n\n // Add to clients for streaming\n const client: StreamClient = {\n excludes,\n includes,\n isBrowser,\n response,\n };\n\n this.clients.add(client);\n\n request.on('close', () => {\n this.clients.delete(client);\n });\n\n return;\n }\n\n let body = '';\n\n request.on('data', (chunk: Buffer) => {\n body += chunk.toString();\n });\n request.on('end', () => {\n if (request.method === 'POST' && request.url === '/log') {\n try {\n const { line, name, timestamp, type } = JSON.parse(\n body,\n ) as LogPayload;\n\n this.broadcastLog(name, line, type, timestamp);\n } catch {\n // Ignore parse errors\n }\n\n response.writeHead(200);\n response.end();\n } else if (request.method === 'POST' && request.url === '/event') {\n try {\n const { code, event, name, pid, timestamp } = JSON.parse(\n body,\n ) as EventPayload;\n\n if (event === 'start') {\n this.broadcastEvent(name, `● started (pid ${pid})`, timestamp);\n } else if (event === 'exit') {\n this.broadcastEvent(name, `○ exited (code ${code})`, timestamp);\n }\n } catch {\n // Ignore parse errors\n }\n\n response.writeHead(200);\n response.end();\n } else if (request.method === 'POST' && request.url === '/inject') {\n // Test injection endpoint\n try {\n const data = JSON.parse(body) as {\n event?: 'exit' | 'start';\n message: string;\n name: string;\n pid?: number;\n };\n const timestamp = performance.timeOrigin + performance.now();\n\n if (data.event === 'start') {\n this.broadcastEvent(\n data.name,\n `● started (pid ${data.pid ?? 0})`,\n timestamp,\n );\n } else if (data.event === 'exit') {\n this.broadcastEvent(data.name, `○ exited (code 0)`, timestamp);\n } else {\n this.broadcastLog(data.name, data.message, 'stdout', timestamp);\n }\n } catch {\n // Ignore parse errors\n }\n\n response.writeHead(200);\n response.end();\n } else {\n response.writeHead(200);\n response.end();\n }\n });\n });\n\n this.server.once('error', (error: NodeJS.ErrnoException) => {\n reject(error);\n });\n\n this.server.listen(this.port, '0.0.0.0', () => {\n // eslint-disable-next-line no-console\n console.log(\n `${DIM}[teemux] aggregating logs on http://${HOST}:${this.port}${RESET}`,\n );\n resolve();\n });\n });\n }\n\n stop(): Promise<void> {\n return new Promise((resolve) => {\n // Close all client connections\n for (const client of this.clients) {\n client.response.end();\n }\n\n this.clients.clear();\n\n if (this.server) {\n this.server.close(() => {\n this.server = null;\n resolve();\n });\n } else {\n resolve();\n }\n });\n }\n\n private broadcastEvent(\n name: string,\n message: string,\n timestamp: number,\n ): void {\n const color = this.getColor(name);\n const forWeb = `${DIM}${color}[${name}]${RESET} ${DIM}${message}${RESET}`;\n\n this.sendToClients(forWeb, timestamp);\n }\n\n private broadcastLog(\n name: string,\n line: string,\n type: LogType,\n timestamp: number,\n ): void {\n const color = this.getColor(name);\n const errorPrefix = type === 'stderr' ? `${RED}[ERR]${RESET} ` : '';\n const forWeb = `${color}[${name}]${RESET} ${errorPrefix}${line}`;\n\n this.sendToClients(forWeb, timestamp);\n }\n\n private getColor(name: string): string {\n if (!this.colorMap.has(name)) {\n this.colorMap.set(name, COLORS[this.colorIndex++ % COLORS.length]);\n }\n\n return this.colorMap.get(name) ?? COLORS[0];\n }\n\n private getHtmlHeader(): string {\n return `<!DOCTYPE html>\n<html>\n<head>\n <meta charset=\"utf-8\">\n <title>teemux</title>\n <style>\n * { box-sizing: border-box; }\n html, body {\n height: 100%;\n margin: 0;\n overflow: hidden;\n }\n body {\n background: #1e1e1e;\n color: #d4d4d4;\n font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Fira Code', monospace;\n font-size: 12px;\n line-height: 1.3;\n display: flex;\n flex-direction: column;\n }\n #filter-bar {\n flex-shrink: 0;\n display: flex;\n gap: 8px;\n padding: 8px 12px;\n background: #252526;\n border-bottom: 1px solid #3c3c3c;\n }\n #filter-bar label {\n display: flex;\n align-items: center;\n gap: 6px;\n color: #888;\n }\n #filter-bar input {\n background: #1e1e1e;\n border: 1px solid #3c3c3c;\n border-radius: 3px;\n color: #d4d4d4;\n font-family: inherit;\n font-size: 12px;\n padding: 4px 8px;\n width: 200px;\n }\n #filter-bar input:focus {\n outline: none;\n border-color: #007acc;\n }\n #container {\n flex: 1;\n overflow-y: auto;\n padding: 8px 12px;\n }\n .line {\n white-space: pre-wrap;\n word-break: break-all;\n padding: 1px 4px;\n margin: 0 -4px;\n border-radius: 2px;\n position: relative;\n display: flex;\n align-items: flex-start;\n }\n .line:hover {\n background: rgba(255, 255, 255, 0.05);\n }\n .line.pinned {\n background: rgba(255, 204, 0, 0.1);\n border-left: 2px solid #fc0;\n margin-left: -6px;\n padding-left: 6px;\n }\n .line-content {\n flex: 1;\n }\n .pin-btn {\n opacity: 0;\n cursor: pointer;\n padding: 0 4px;\n color: #888;\n flex-shrink: 0;\n transition: opacity 0.15s;\n }\n .line:hover .pin-btn {\n opacity: 0.5;\n }\n .pin-btn:hover {\n opacity: 1 !important;\n color: #fc0;\n }\n .line.pinned .pin-btn {\n opacity: 1;\n color: #fc0;\n }\n a { color: #4fc1ff; text-decoration: underline; }\n a:hover { text-decoration: none; }\n mark { background: #623800; color: inherit; border-radius: 2px; }\n mark.filter { background: #264f00; }\n .json-key { color: #9cdcfe; }\n .json-string { color: #ce9178; }\n .json-number { color: #b5cea8; }\n .json-bool { color: #569cd6; }\n .json-null { color: #569cd6; }\n #tail-btn {\n position: fixed;\n bottom: 20px;\n right: 20px;\n background: #007acc;\n color: #fff;\n border: none;\n border-radius: 4px;\n padding: 8px 16px;\n font-family: inherit;\n font-size: 12px;\n cursor: pointer;\n display: none;\n align-items: center;\n gap: 6px;\n box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);\n transition: background 0.15s;\n }\n #tail-btn:hover {\n background: #0098ff;\n }\n #tail-btn svg {\n flex-shrink: 0;\n }\n </style>\n</head>\n<body>\n <div id=\"filter-bar\">\n <label>Include: <input type=\"text\" id=\"include\" placeholder=\"error*,warn* (OR, * = wildcard)\"></label>\n <label>Exclude: <input type=\"text\" id=\"exclude\" placeholder=\"health*,debug (OR, * = wildcard)\"></label>\n <label>Highlight: <input type=\"text\" id=\"highlight\" placeholder=\"term1,term2\"></label>\n </div>\n <div id=\"container\"></div>\n <button id=\"tail-btn\" title=\"Jump to bottom and follow new logs\">\n <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"14\" height=\"14\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path d=\"M12 5v14\"/><path d=\"m19 12-7 7-7-7\"/></svg>\n Tail\n </button>\n <script>\n const container = document.getElementById('container');\n const includeInput = document.getElementById('include');\n const excludeInput = document.getElementById('exclude');\n const highlightInput = document.getElementById('highlight');\n const tailBtn = document.getElementById('tail-btn');\n const params = new URLSearchParams(window.location.search);\n const tailSize = ${this.tailSize};\n \n includeInput.value = params.get('include') || '';\n excludeInput.value = params.get('exclude') || '';\n highlightInput.value = params.get('highlight') || '';\n \n let tailing = true;\n let pinnedIds = new Set();\n \n const updateTailButton = () => {\n tailBtn.style.display = tailing ? 'none' : 'flex';\n };\n \n // Lucide pin icon SVG\n const pinIcon = '<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"14\" height=\"14\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path d=\"M12 17v5\"/><path d=\"M9 10.76a2 2 0 0 1-1.11 1.79l-1.78.9A2 2 0 0 0 5 15.24V16a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-.76a2 2 0 0 0-1.11-1.79l-1.78-.9A2 2 0 0 1 15 10.76V7a1 1 0 0 1 1-1 2 2 0 0 0 0-4H8a2 2 0 0 0 0 4 1 1 0 0 1 1 1z\"/></svg>';\n \n const stripAnsi = (str) => str.replace(/\\\\u001B\\\\[[\\\\d;]*m/g, '');\n \n const globToRegex = (pattern) => {\n const escaped = pattern.replace(/([.+?^\\${}()|[\\\\]\\\\\\\\])/g, '\\\\\\\\$1');\n const regexPattern = escaped.replace(/\\\\*/g, '.*');\n return new RegExp(regexPattern, 'i');\n };\n \n const matchesPattern = (text, pattern) => {\n if (pattern.includes('*')) {\n return globToRegex(pattern).test(text);\n }\n return text.includes(pattern.toLowerCase());\n };\n \n const matchesFilters = (text, includes, excludes) => {\n const plain = stripAnsi(text).toLowerCase();\n if (includes.length > 0) {\n const anyMatch = includes.some(p => matchesPattern(plain, p));\n if (!anyMatch) return false;\n }\n if (excludes.length > 0) {\n const anyMatch = excludes.some(p => matchesPattern(plain, p));\n if (anyMatch) return false;\n }\n return true;\n };\n \n const highlightTerms = (html, terms, className = '') => {\n if (!terms.length) return html;\n let result = html;\n for (const term of terms) {\n if (!term) continue;\n const escaped = term.replace(/([.*+?^\\${}()|[\\\\]\\\\\\\\])/g, '\\\\\\\\$1');\n const regex = new RegExp('(?![^<]*>)(' + escaped + ')', 'gi');\n const cls = className ? ' class=\"' + className + '\"' : '';\n result = result.replace(regex, '<mark' + cls + '>$1</mark>');\n }\n return result;\n };\n \n const applyFilters = () => {\n const includes = includeInput.value.split(',').map(s => s.trim()).filter(Boolean);\n const excludes = excludeInput.value.split(',').map(s => s.trim()).filter(Boolean);\n const highlights = highlightInput.value.split(',').map(s => s.trim()).filter(Boolean);\n \n document.querySelectorAll('.line').forEach(line => {\n const id = line.dataset.id;\n const isPinned = pinnedIds.has(id);\n const text = line.dataset.raw;\n const matches = matchesFilters(text, includes, excludes);\n line.style.display = (matches || isPinned) ? '' : 'none';\n \n // Re-apply highlighting\n const contentEl = line.querySelector('.line-content');\n if (contentEl) {\n let html = line.dataset.html;\n html = highlightTerms(html, includes, 'filter');\n html = highlightTerms(html, highlights);\n contentEl.innerHTML = html;\n }\n });\n \n // Update URL without reload\n const newParams = new URLSearchParams();\n if (includeInput.value) newParams.set('include', includeInput.value);\n if (excludeInput.value) newParams.set('exclude', excludeInput.value);\n if (highlightInput.value) newParams.set('highlight', highlightInput.value);\n const newUrl = newParams.toString() ? '?' + newParams.toString() : window.location.pathname;\n history.replaceState(null, '', newUrl);\n };\n \n const trimBuffer = () => {\n const lines = container.querySelectorAll('.line');\n const unpinnedLines = Array.from(lines).filter(l => !pinnedIds.has(l.dataset.id));\n const excess = unpinnedLines.length - tailSize;\n if (excess > 0) {\n for (let i = 0; i < excess; i++) {\n unpinnedLines[i].remove();\n }\n }\n };\n \n let lineCounter = 0;\n const addLine = (html, raw) => {\n const id = 'line-' + (lineCounter++);\n const includes = includeInput.value.split(',').map(s => s.trim()).filter(Boolean);\n const excludes = excludeInput.value.split(',').map(s => s.trim()).filter(Boolean);\n const highlights = highlightInput.value.split(',').map(s => s.trim()).filter(Boolean);\n \n const div = document.createElement('div');\n div.className = 'line';\n div.dataset.id = id;\n div.dataset.raw = raw;\n div.dataset.html = html;\n \n let displayHtml = html;\n displayHtml = highlightTerms(displayHtml, includes, 'filter');\n displayHtml = highlightTerms(displayHtml, highlights);\n \n div.innerHTML = '<span class=\"line-content\">' + displayHtml + '</span><span class=\"pin-btn\" title=\"Pin\">' + pinIcon + '</span>';\n \n // Pin button handler\n div.querySelector('.pin-btn').addEventListener('click', (e) => {\n e.stopPropagation();\n if (pinnedIds.has(id)) {\n pinnedIds.delete(id);\n div.classList.remove('pinned');\n } else {\n pinnedIds.add(id);\n div.classList.add('pinned');\n }\n applyFilters();\n });\n \n const matches = matchesFilters(raw, includes, excludes);\n div.style.display = matches ? '' : 'none';\n \n container.appendChild(div);\n trimBuffer();\n if (tailing) container.scrollTop = container.scrollHeight;\n };\n \n container.addEventListener('scroll', () => {\n const atBottom = container.scrollHeight - container.scrollTop - container.clientHeight < 50;\n tailing = atBottom;\n updateTailButton();\n });\n \n tailBtn.addEventListener('click', () => {\n container.scrollTop = container.scrollHeight;\n tailing = true;\n updateTailButton();\n });\n \n let debounceTimer;\n const debounce = (fn, delay) => {\n clearTimeout(debounceTimer);\n debounceTimer = setTimeout(fn, delay);\n };\n \n includeInput.addEventListener('input', () => debounce(applyFilters, 50));\n excludeInput.addEventListener('input', () => debounce(applyFilters, 50));\n highlightInput.addEventListener('input', () => debounce(applyFilters, 50));\n </script>\n`;\n }\n\n private getHtmlLine(line: string): string {\n let html = this.ansiConverter.toHtml(line);\n html = highlightJson(html);\n html = linkifyUrls(html);\n const escaped = html.replaceAll('\\\\', '\\\\\\\\').replaceAll(\"'\", \"\\\\'\");\n const raw = stripAnsi(line).replaceAll('\\\\', '\\\\\\\\').replaceAll(\"'\", \"\\\\'\");\n return `<script>addLine('${escaped}', '${raw}')</script>\\n`;\n }\n\n private sendToClients(forWeb: string, timestamp: number): void {\n // Add to buffer\n this.buffer.push({ line: forWeb, timestamp });\n\n // Trim buffer to tail size\n if (this.buffer.length > this.tailSize) {\n this.buffer.shift();\n }\n\n // Send to all connected clients\n for (const client of this.clients) {\n if (client.isBrowser) {\n client.response.write(this.getHtmlLine(forWeb));\n } else {\n // Server-side filtering for non-browser clients\n if (!matchesFilters(forWeb, client.includes, client.excludes)) {\n continue;\n }\n\n client.response.write(stripAnsi(forWeb) + '\\n');\n }\n }\n\n // Note: Each client prints its own logs locally, so server doesn't need to\n }\n}\n","#!/usr/bin/env node\n\nimport { LogServer } from './LogServer.js';\nimport { spawn } from 'node:child_process';\nimport http from 'node:http';\nimport { performance } from 'node:perf_hooks';\nimport readline from 'node:readline';\nimport yargs from 'yargs';\nimport { hideBin } from 'yargs/helpers';\n\n// High-precision timestamp (milliseconds with microsecond precision)\nconst getTimestamp = (): number => performance.timeOrigin + performance.now();\n\nconst RESET = '\\u001B[0m';\nconst DIM = '\\u001B[90m';\nconst RED = '\\u001B[91m';\n\n// Leader monitoring configuration\nconst LEADER_CHECK_INTERVAL = 2_000; // Check every 2 seconds\nconst MAX_PROMOTION_RETRIES = 3;\n\ntype LogType = 'stderr' | 'stdout';\n\nclass LogClient {\n private name: string;\n\n private port: number;\n\n private queue: Array<{ line: string; timestamp: number; type: LogType }> = [];\n\n private sending = false;\n\n constructor(name: string, port: number) {\n this.name = name;\n this.port = port;\n }\n\n async event(\n event: 'exit' | 'start',\n pid: number,\n code?: number,\n ): Promise<void> {\n await this.send('/event', {\n code,\n event,\n name: this.name,\n pid,\n timestamp: getTimestamp(),\n });\n }\n\n async flush(): Promise<void> {\n if (this.sending || this.queue.length === 0) {\n return;\n }\n\n this.sending = true;\n\n while (this.queue.length > 0) {\n const item = this.queue.shift();\n\n if (!item) {\n continue;\n }\n\n const success = await this.send('/log', {\n line: item.line,\n name: this.name,\n timestamp: item.timestamp,\n type: item.type,\n });\n\n if (!success) {\n // Fallback to local output if server unreachable\n // eslint-disable-next-line no-console\n console.log(`[${this.name}] ${item.line}`);\n }\n }\n\n this.sending = false;\n }\n\n log(line: string, type: LogType = 'stdout'): void {\n // Always output locally\n const errorPrefix = type === 'stderr' ? `${RED}[ERR]${RESET} ` : '';\n\n // eslint-disable-next-line no-console\n console.log(`${errorPrefix}${line}`);\n\n // Capture timestamp immediately when log is received\n this.queue.push({ line, timestamp: getTimestamp(), type });\n void this.flush();\n }\n\n private async send(endpoint: string, data: object): Promise<boolean> {\n return new Promise((resolve) => {\n const postData = JSON.stringify(data);\n const request = http.request(\n {\n headers: {\n 'Content-Length': Buffer.byteLength(postData),\n 'Content-Type': 'application/json',\n },\n hostname: '127.0.0.1',\n method: 'POST',\n path: endpoint,\n port: this.port,\n timeout: 1_000,\n },\n (response) => {\n response.resume();\n response.on('end', () => resolve(true));\n },\n );\n\n request.on('error', () => resolve(false));\n request.on('timeout', () => {\n request.destroy();\n resolve(false);\n });\n request.write(postData);\n request.end();\n });\n }\n}\n\nconst runProcess = async (\n name: string,\n command: string[],\n client: LogClient,\n): Promise<number> => {\n const [cmd, ...args] = command;\n\n const child = spawn(cmd, args, {\n env: {\n ...process.env,\n FORCE_COLOR: '1',\n },\n shell: process.platform === 'win32',\n stdio: ['inherit', 'pipe', 'pipe'],\n });\n\n const pid = child.pid ?? 0;\n\n await client.event('start', pid);\n\n if (child.stdout) {\n const rlStdout = readline.createInterface({ input: child.stdout });\n\n rlStdout.on('line', (line) => client.log(line, 'stdout'));\n }\n\n if (child.stderr) {\n const rlStderr = readline.createInterface({ input: child.stderr });\n\n rlStderr.on('line', (line) => client.log(line, 'stderr'));\n }\n\n return new Promise((resolve) => {\n child.on('close', async (code) => {\n await client.flush();\n await client.event('exit', pid, code ?? 0);\n resolve(code ?? 0);\n });\n });\n};\n\nconst sleep = (ms: number): Promise<void> =>\n new Promise((resolve) => {\n setTimeout(resolve, ms);\n });\n\nconst checkServerReady = async (port: number): Promise<boolean> => {\n return new Promise((resolve) => {\n const request = http.request(\n {\n hostname: '127.0.0.1',\n method: 'GET',\n path: '/',\n port,\n timeout: 200,\n },\n (response) => {\n response.resume();\n resolve(true);\n },\n );\n\n request.on('error', () => resolve(false));\n request.on('timeout', () => {\n request.destroy();\n resolve(false);\n });\n request.end();\n });\n};\n\nconst waitForServer = async (\n port: number,\n maxAttempts = 50,\n): Promise<boolean> => {\n for (let index = 0; index < maxAttempts; index++) {\n if (await checkServerReady(port)) {\n return true;\n }\n\n // Exponential backoff: 10ms, 20ms, 40ms, ... capped at 200ms\n const delay = Math.min(10 * 2 ** index, 200);\n\n await sleep(delay);\n }\n\n return false;\n};\n\nconst tryBecomeLeader = async (server: LogServer): Promise<boolean> => {\n for (let attempt = 0; attempt < MAX_PROMOTION_RETRIES; attempt++) {\n try {\n await server.start();\n return true;\n } catch (error) {\n if ((error as NodeJS.ErrnoException).code !== 'EADDRINUSE') {\n throw error;\n }\n\n // Check if another server took over\n if (await checkServerReady(server.getPort())) {\n // Another process became leader\n return false;\n }\n\n // Port in use but server not responding - might be starting up\n // Add random jitter to avoid thundering herd\n const jitter = Math.random() * 100;\n\n await sleep(50 + jitter);\n }\n }\n\n return false;\n};\n\nconst startLeaderMonitoring = (\n server: LogServer,\n port: number,\n): { stop: () => void } => {\n let isRunning = true;\n let timeoutId: null | ReturnType<typeof setTimeout> = null;\n\n const checkAndPromote = async (): Promise<void> => {\n if (!isRunning) {\n return;\n }\n\n const serverAlive = await checkServerReady(port);\n\n if (!serverAlive && isRunning) {\n // Leader might be dead, try to become leader\n // Add random jitter to prevent all clients from trying simultaneously\n const jitter = Math.random() * 500;\n\n await sleep(jitter);\n\n // Double-check server is still down after jitter\n if (isRunning && !(await checkServerReady(port))) {\n const promoted = await tryBecomeLeader(server);\n\n if (promoted) {\n // eslint-disable-next-line no-console\n console.log(\n `${DIM}[teemux] promoted to leader, now aggregating logs${RESET}`,\n );\n // Stop monitoring - we're now the leader\n // eslint-disable-next-line require-atomic-updates -- safe: only modified here or in stop()\n isRunning = false;\n return;\n }\n }\n }\n\n // Schedule next check\n if (isRunning) {\n timeoutId = setTimeout(() => {\n void checkAndPromote();\n }, LEADER_CHECK_INTERVAL);\n }\n };\n\n // Start monitoring after initial delay\n timeoutId = setTimeout(() => {\n void checkAndPromote();\n }, LEADER_CHECK_INTERVAL);\n\n return {\n stop: () => {\n isRunning = false;\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n },\n };\n};\n\nconst main = async (): Promise<void> => {\n const argv = await yargs(hideBin(process.argv))\n .env('TEEMUX')\n .usage('Usage: $0 --name <name> -- <command> [args...]')\n .option('name', {\n alias: 'n',\n description:\n 'Name to identify this process in logs (defaults to command)',\n type: 'string',\n })\n .option('port', {\n alias: 'p',\n default: 8_336,\n description: 'Port for the log aggregation server',\n type: 'number',\n })\n .option('tail', {\n alias: 't',\n default: 1_000,\n description: 'Number of log lines to keep in buffer',\n type: 'number',\n })\n .help()\n .parse();\n\n const command = argv._ as string[];\n\n if (command.length === 0) {\n // eslint-disable-next-line no-console\n console.error('No command specified');\n // eslint-disable-next-line no-console\n console.error('Usage: teemux --name <name> -- <command> [args...]');\n process.exit(1);\n }\n\n const name = argv.name ?? command[0] ?? 'unknown';\n const port = argv.port;\n\n const server = new LogServer(port, argv.tail);\n\n // Try to become server with retries - if port is taken, become client\n let isServer = false;\n const maxRetries = 3;\n\n for (let attempt = 0; attempt < maxRetries; attempt++) {\n try {\n await server.start();\n isServer = true;\n break;\n } catch (error) {\n if ((error as NodeJS.ErrnoException).code !== 'EADDRINUSE') {\n throw error;\n }\n\n // Check if another server is actually running\n if (await checkServerReady(port)) {\n // Server exists, we're a client\n break;\n }\n\n // Port in use but server not responding - might be starting up\n // Add random jitter to avoid thundering herd\n const jitter = Math.random() * 100;\n\n await sleep(50 + jitter);\n }\n }\n\n // If we're not the server, wait for it to be ready and start monitoring\n let leaderMonitor: null | { stop: () => void } = null;\n\n if (!isServer) {\n const serverReady = await waitForServer(port);\n\n if (!serverReady) {\n // eslint-disable-next-line no-console\n console.error(\n '[teemux] Could not connect to server. Is another instance running?',\n );\n }\n\n // Start monitoring for leader failover\n leaderMonitor = startLeaderMonitoring(server, port);\n }\n\n const client = new LogClient(name, port);\n\n // Run the process\n const exitCode = await runProcess(name, command, client);\n\n // Stop leader monitoring if running\n leaderMonitor?.stop();\n\n process.exit(exitCode);\n};\n\nmain().catch((error: unknown) => {\n // eslint-disable-next-line no-console\n console.error('Fatal error:', error);\n process.exit(1);\n});\n"],"mappings":";;;;;;;;;;;;;;AAGA,MAAa,iBAAiB,SAAyB;AACrD,QAAO,KAAK,WAAW,aAAa,GAAG;;;;;;;;ACDzC,MAAa,gBAAgB,SAAyB;AACpD,QAAO,KACJ,WAAW,UAAU,KAAI,CACzB,WAAW,SAAS,IAAI,CACxB,WAAW,QAAQ,IAAI,CACvB,WAAW,QAAQ,IAAI,CACvB,WAAW,UAAU,IAAI,CACzB,WAAW,SAAS,IAAI;;;;;;;;;ACH7B,MAAa,qBAAqB,SAAyB;CAEzD,MAAM,UAAoB,EAAE;CAC5B,IAAI,SAAS,KAAK,WAChB,qCACC,QAAQ,YAAY;AACnB,UAAQ,KAAK,QAAkB;AAC/B,SAAO,YAAY,QAAQ,SAAS,EAAE;GAEzC;AAGD,UAAS,OAAO,WACd,2BACA,sCACD;AAGD,UAAS,OAAO,WACd,iDACA,wCACD;AAGD,UAAS,OAAO,WACd,0BACC,QAAQ,OAAO,UAAU;EACxB,MAAM,UAAU,QAAQ,OAAO,SAAS,OAAiB,GAAG;AAC5D,MAAI,MAEF,QAAO,gCAAgC,QAAQ,eAAe;AAIhE,SAAO,mCAAmC,QAAQ;GAErD;AAED,QAAO;;;;;AAMT,MAAa,uBAAuB,SAAyB;CAC3D,IAAI,SAAS;CACb,IAAI,QAAQ;AAEZ,QAAO,QAAQ,KAAK,OAClB,KAAI,KAAK,WAAW,KAAK;EAEvB,MAAM,SAAS,KAAK,QA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CAElB,MAAM,SAAS,IAAI,UAAU,MAAM,KAAK,KAAK;CAG7C,IAAI,WAAW;CACf,MAAM,aAAa;AAEnB,MAAK,IAAI,UAAU,GAAG,UAAU,YAAY,UAC1C,KAAI;AACF,QAAM,OAAO,OAAO;AACpB,aAAW;AACX;UACO,OAAO;AACd,MAAK,MAAgC,SAAS,aAC5C,OAAM;AAIR,MAAI,MAAM,iBAAiB,KAAK,CAE9B;AAOF,QAAM,MAAM,KAFG,KAAK,QAAQ,GAAG,IAEP;;CAK5B,IAAI,gBAA6C;AAEjD,KAAI,CAAC,UAAU;AAGb,MAAI,CAFgB,MAAM,cAAc,KAAK,CAI3C,SAAQ,MACN,qEACD;AAIH,kBAAgB,sBAAsB,QAAQ,KAAK;;CAMrD,MAAM,WAAW,MAAM,WAAW,MAAM,SAHzB,IAAI,UAAU,MAAM,KAAK,CAGgB;AAGxD,gBAAe,MAAM;AAErB,SAAQ,KAAK,SAAS;;AAGxB,MAAM,CAAC,OAAO,UAAmB;AAE/B,SAAQ,MAAM,gBAAgB,MAAM;AACpC,SAAQ,KAAK,EAAE;EACf"}
|
|
1
|
+
{"version":3,"file":"teemux.js","names":["RESET","DIM","RED"],"sources":["../src/utils/stripHtmlTags.ts","../src/utils/unescapeHtml.ts","../src/utils/highlightJson.ts","../src/utils/linkifyUrls.ts","../src/utils/stripAnsi.ts","../src/utils/matchesFilters.ts","../src/LogServer.ts","../src/teemux.ts"],"sourcesContent":["/**\n * Strip HTML tags from a string, leaving only text content.\n */\nexport const stripHtmlTags = (html: string): string => {\n return html.replaceAll(/<[^>]*>/gu, '');\n};\n","/**\n * Unescape HTML entities back to their original characters.\n */\nexport const unescapeHtml = (text: string): string => {\n return text\n .replaceAll('"', '\"')\n .replaceAll('&', '&')\n .replaceAll('<', '<')\n .replaceAll('>', '>')\n .replaceAll(''', \"'\")\n .replaceAll(''', \"'\");\n};\n","import { stripHtmlTags } from './stripHtmlTags.js';\nimport { unescapeHtml } from './unescapeHtml.js';\n\n/**\n * Apply syntax highlighting to JSON text that uses HTML-escaped quotes (").\n * Uses placeholder technique to avoid double-wrapping strings.\n */\nexport const highlightJsonText = (text: string): string => {\n // First, extract and mark all JSON strings with placeholders\n const strings: string[] = [];\n let result = text.replaceAll(\n /"((?:(?!").)*)"/gu,\n (_match, content) => {\n strings.push(content as string);\n return `\\u0000STR${strings.length - 1}\\u0000`;\n },\n );\n\n // Booleans and null\n result = result.replaceAll(\n /\\b(true|false|null)\\b/gu,\n '<span class=\"json-bool\">$1</span>',\n );\n\n // Numbers\n result = result.replaceAll(\n /(?<!\\w)(-?\\d+(?:\\.\\d+)?(?:[eE][+-]?\\d+)?)\\b/gu,\n '<span class=\"json-number\">$1</span>',\n );\n\n // Restore strings with appropriate highlighting\n result = result.replaceAll(\n /\\0STR(\\d+)\\0(\\s*:)?/gu,\n (_match, index, colon) => {\n const content = strings[Number.parseInt(index as string, 10)];\n if (colon) {\n // This is a key\n return `<span class=\"json-key\">"${content}"</span>${colon}`;\n }\n\n // This is a value\n return `<span class=\"json-string\">"${content}"</span>`;\n },\n );\n\n return result;\n};\n\n/**\n * Process HTML text, applying JSON highlighting only to text outside of HTML tags.\n */\nexport const syntaxHighlightJson = (html: string): string => {\n let result = '';\n let index = 0;\n\n while (index < html.length) {\n if (html[index] === '<') {\n // Find end of tag\n const tagEnd = html.indexOf('>', index);\n if (tagEnd === -1) {\n result += html.slice(index);\n break;\n }\n\n result += html.slice(index, tagEnd + 1);\n index = tagEnd + 1;\n } else {\n // Find next tag or end of string\n const nextTag = html.indexOf('<', index);\n const textEnd = nextTag === -1 ? html.length : nextTag;\n const text = html.slice(index, textEnd);\n\n // Highlight JSON syntax in this text segment\n result += highlightJsonText(text);\n index = textEnd;\n }\n }\n\n return result;\n};\n\n/**\n * Detect if the content (after prefix) is valid JSON and apply syntax highlighting.\n * Returns the original HTML if not valid JSON.\n */\nexport const highlightJson = (html: string): string => {\n // Extract the text content (strip HTML tags) to check if it's JSON\n const textContent = stripHtmlTags(html);\n\n // Unescape HTML entities for JSON parsing\n const unescaped = unescapeHtml(textContent);\n\n // Find where the actual log content starts (after the prefix like [name])\n const prefixMatch = /^\\[[\\w-]+\\]\\s*/u.exec(unescaped);\n const prefix = prefixMatch?.[0] ?? '';\n const content = unescaped.slice(prefix.length).trim();\n\n // Check if the content is valid JSON\n if (!content.startsWith('{') && !content.startsWith('[')) {\n return html;\n }\n\n try {\n JSON.parse(content);\n } catch {\n return html;\n }\n\n // It's valid JSON - now highlight it\n // Find the position after the prefix span in the HTML\n const prefixHtmlMatch = /^<span[^>]*>\\[[^\\]]+\\]<\\/span>\\s*/u.exec(html);\n const htmlPrefix = prefixHtmlMatch?.[0] ?? '';\n const jsonHtml = html.slice(htmlPrefix.length);\n\n // Apply syntax highlighting to the JSON portion\n const highlighted = syntaxHighlightJson(jsonHtml);\n\n return htmlPrefix + highlighted;\n};\n","/**\n * Convert URLs in HTML text to clickable anchor tags.\n * Supports http://, https://, and file:// URLs.\n * Avoids double-linking URLs that are already in href attributes.\n */\nexport const linkifyUrls = (html: string): string => {\n // Match URLs that are not already inside href attributes\n // Supports http://, https://, and file:// URLs\n // Exclude common delimiters and HTML entities (" & etc)\n const urlRegex = /(?<!href=[\"'])(?:https?|file):\\/\\/[^\\s<>\"'{}&]+/gu;\n\n return html.replaceAll(urlRegex, (url) => {\n // Remove trailing punctuation that's likely not part of the URL\n const cleanUrl = url.replace(/[.,;:!?)\\]]+$/u, '');\n const trailing = url.slice(cleanUrl.length);\n\n // Escape HTML entities in the URL for the href attribute\n const escapedHref = cleanUrl\n .replaceAll('&', '&')\n .replaceAll('\"', '"');\n\n return `<a href=\"${escapedHref}\" target=\"_blank\" rel=\"noopener\">${cleanUrl}</a>${trailing}`;\n });\n};\n","/**\n * Strip ANSI escape codes from text.\n * Removes color codes and other terminal formatting sequences.\n */\nexport const stripAnsi = (text: string): string => {\n // eslint-disable-next-line no-control-regex\n return text.replaceAll(/\\u001B\\[[\\d;]*m/gu, '');\n};\n","import { stripAnsi } from './stripAnsi.js';\n\n/**\n * Convert a glob pattern (with * wildcards) to a RegExp.\n * - `*` matches any characters (zero or more)\n * - All other characters are escaped for literal matching\n */\nconst globToRegex = (pattern: string): RegExp => {\n // Escape regex special characters except *\n const escaped = pattern.replaceAll(/[$()+.?[\\\\\\]^{|}]/gu, '\\\\$&');\n // Convert * to .*\n const regexPattern = escaped.replaceAll('*', '.*');\n return new RegExp(regexPattern, 'iu');\n};\n\n/**\n * Check if text matches a pattern (supports * glob wildcards).\n * If no wildcards, does a simple substring match for better performance.\n */\nconst matchesPattern = (text: string, pattern: string): boolean => {\n if (pattern.includes('*')) {\n return globToRegex(pattern).test(text);\n }\n\n return text.includes(pattern.toLowerCase());\n};\n\n/**\n * Check if a line matches the given filter criteria.\n * @param line - The line to check (may contain ANSI codes)\n * @param includes - Patterns where ANY match includes the line (OR logic), case-insensitive. Supports * wildcards.\n * @param excludes - Patterns where ANY match excludes the line (OR logic), case-insensitive. Supports * wildcards.\n * @returns true if the line should be included, false if filtered out\n */\nexport const matchesFilters = (\n line: string,\n includes: string[],\n excludes: string[],\n): boolean => {\n const plainText = stripAnsi(line).toLowerCase();\n\n // Any include must match (OR logic) - case insensitive\n if (includes.length > 0) {\n const anyIncludeMatches = includes.some((pattern) =>\n matchesPattern(plainText, pattern),\n );\n\n if (!anyIncludeMatches) {\n return false;\n }\n }\n\n // None of the excludes should match (OR logic for exclusion) - case insensitive\n if (excludes.length > 0) {\n const anyExcludeMatches = excludes.some((pattern) =>\n matchesPattern(plainText, pattern),\n );\n\n if (anyExcludeMatches) {\n return false;\n }\n }\n\n return true;\n};\n","import { highlightJson } from './utils/highlightJson.js';\nimport { linkifyUrls } from './utils/linkifyUrls.js';\nimport { matchesFilters } from './utils/matchesFilters.js';\nimport { stripAnsi } from './utils/stripAnsi.js';\nimport Convert from 'ansi-to-html';\nimport http from 'node:http';\nimport { performance } from 'node:perf_hooks';\nimport { URL } from 'node:url';\n\nconst COLORS = [\n '\\u001B[36m',\n '\\u001B[33m',\n '\\u001B[32m',\n '\\u001B[35m',\n '\\u001B[34m',\n '\\u001B[91m',\n '\\u001B[92m',\n '\\u001B[93m',\n];\nconst RESET = '\\u001B[0m';\nconst DIM = '\\u001B[90m';\nconst RED = '\\u001B[91m';\nconst HOST = '0.0.0.0';\n\ntype BufferedLog = {\n line: string;\n timestamp: number;\n};\n\ntype EventPayload = {\n code?: number;\n event: 'exit' | 'start';\n name: string;\n pid: number;\n timestamp: number;\n};\n\ntype LogPayload = {\n line: string;\n name: string;\n timestamp: number;\n type: LogType;\n};\n\ntype LogType = 'stderr' | 'stdout';\n\ntype StreamClient = {\n excludes: string[];\n includes: string[];\n isBrowser: boolean;\n response: http.ServerResponse;\n};\n\nexport class LogServer {\n private ansiConverter = new Convert({ escapeXML: true, newline: true });\n\n private buffer: BufferedLog[] = [];\n\n private clients = new Set<StreamClient>();\n\n private colorIndex = 0;\n\n private colorMap = new Map<string, string>();\n\n private port: number;\n\n private server: http.Server | null = null;\n\n private tailSize: number;\n\n constructor(port: number, tailSize: number = 1_000) {\n this.port = port;\n this.tailSize = tailSize;\n }\n\n getPort(): number {\n if (this.server) {\n const address = this.server.address();\n if (address && typeof address === 'object') {\n return address.port;\n }\n }\n\n return this.port;\n }\n\n start(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.server = http.createServer((request, response) => {\n // Handle streaming GET request\n if (request.method === 'GET' && request.url?.startsWith('/')) {\n const url = new URL(request.url, `http://${request.headers.host}`);\n const includeParameter = url.searchParams.get('include');\n const includes = includeParameter\n ? includeParameter\n .split(',')\n .map((term) => term.trim())\n .filter(Boolean)\n : [];\n const excludeParameter = url.searchParams.get('exclude');\n const excludes = excludeParameter\n ? excludeParameter\n .split(',')\n .map((pattern) => pattern.trim())\n .filter(Boolean)\n : [];\n\n const userAgent = request.headers['user-agent'] ?? '';\n const isBrowser = userAgent.includes('Mozilla');\n\n // Sort buffer by timestamp\n const sortedBuffer = this.buffer.toSorted(\n (a, b) => a.timestamp - b.timestamp,\n );\n\n if (isBrowser) {\n // Browser: send all logs, filtering is done client-side\n response.writeHead(200, {\n 'Cache-Control': 'no-cache',\n Connection: 'keep-alive',\n 'Content-Type': 'text/html; charset=utf-8',\n 'X-Content-Type-Options': 'nosniff',\n });\n\n // Send HTML header with styling\n response.write(this.getHtmlHeader());\n\n // Send all buffered logs as HTML\n for (const entry of sortedBuffer) {\n response.write(this.getHtmlLine(entry.line));\n }\n } else {\n // Non-browser (curl, etc): apply server-side filtering\n const filteredBuffer = sortedBuffer.filter((entry) =>\n matchesFilters(entry.line, includes, excludes),\n );\n\n response.writeHead(200, {\n 'Cache-Control': 'no-cache',\n Connection: 'keep-alive',\n 'Content-Type': 'text/plain; charset=utf-8',\n 'X-Content-Type-Options': 'nosniff',\n });\n\n // Send filtered logs as plain text (strip ANSI)\n for (const entry of filteredBuffer) {\n response.write(stripAnsi(entry.line) + '\\n');\n }\n }\n\n // Add to clients for streaming\n const client: StreamClient = {\n excludes,\n includes,\n isBrowser,\n response,\n };\n\n this.clients.add(client);\n\n request.on('close', () => {\n this.clients.delete(client);\n });\n\n return;\n }\n\n let body = '';\n\n request.on('data', (chunk: Buffer) => {\n body += chunk.toString();\n });\n request.on('end', () => {\n if (request.method === 'POST' && request.url === '/log') {\n try {\n const { line, name, timestamp, type } = JSON.parse(\n body,\n ) as LogPayload;\n\n this.broadcastLog(name, line, type, timestamp);\n } catch {\n // Ignore parse errors\n }\n\n response.writeHead(200);\n response.end();\n } else if (request.method === 'POST' && request.url === '/event') {\n try {\n const { code, event, name, pid, timestamp } = JSON.parse(\n body,\n ) as EventPayload;\n\n if (event === 'start') {\n this.broadcastEvent(name, `● started (pid ${pid})`, timestamp);\n } else if (event === 'exit') {\n this.broadcastEvent(name, `○ exited (code ${code})`, timestamp);\n }\n } catch {\n // Ignore parse errors\n }\n\n response.writeHead(200);\n response.end();\n } else if (request.method === 'POST' && request.url === '/inject') {\n // Test injection endpoint\n try {\n const data = JSON.parse(body) as {\n event?: 'exit' | 'start';\n message: string;\n name: string;\n pid?: number;\n };\n const timestamp = performance.timeOrigin + performance.now();\n\n if (data.event === 'start') {\n this.broadcastEvent(\n data.name,\n `● started (pid ${data.pid ?? 0})`,\n timestamp,\n );\n } else if (data.event === 'exit') {\n this.broadcastEvent(data.name, `○ exited (code 0)`, timestamp);\n } else {\n this.broadcastLog(data.name, data.message, 'stdout', timestamp);\n }\n } catch {\n // Ignore parse errors\n }\n\n response.writeHead(200);\n response.end();\n } else {\n response.writeHead(200);\n response.end();\n }\n });\n });\n\n this.server.once('error', (error: NodeJS.ErrnoException) => {\n reject(error);\n });\n\n this.server.listen(this.port, '0.0.0.0', () => {\n // eslint-disable-next-line no-console\n console.log(\n `${DIM}[teemux] aggregating logs on http://${HOST}:${this.port}${RESET}`,\n );\n resolve();\n });\n });\n }\n\n stop(): Promise<void> {\n return new Promise((resolve) => {\n // Close all client connections\n for (const client of this.clients) {\n client.response.end();\n }\n\n this.clients.clear();\n\n if (this.server) {\n this.server.close(() => {\n this.server = null;\n resolve();\n });\n } else {\n resolve();\n }\n });\n }\n\n private broadcastEvent(\n name: string,\n message: string,\n timestamp: number,\n ): void {\n const color = this.getColor(name);\n const forWeb = `${DIM}${color}[${name}]${RESET} ${DIM}${message}${RESET}`;\n\n this.sendToClients(forWeb, timestamp);\n }\n\n private broadcastLog(\n name: string,\n line: string,\n type: LogType,\n timestamp: number,\n ): void {\n const color = this.getColor(name);\n const errorPrefix = type === 'stderr' ? `${RED}[ERR]${RESET} ` : '';\n const forWeb = `${color}[${name}]${RESET} ${errorPrefix}${line}`;\n\n this.sendToClients(forWeb, timestamp);\n }\n\n private getColor(name: string): string {\n if (!this.colorMap.has(name)) {\n this.colorMap.set(name, COLORS[this.colorIndex++ % COLORS.length]);\n }\n\n return this.colorMap.get(name) ?? COLORS[0];\n }\n\n private getHtmlHeader(): string {\n return `<!DOCTYPE html>\n<html>\n<head>\n <meta charset=\"utf-8\">\n <title>teemux</title>\n <style>\n * { box-sizing: border-box; }\n html, body {\n height: 100%;\n margin: 0;\n overflow: hidden;\n }\n body {\n background: #1e1e1e;\n color: #d4d4d4;\n font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Fira Code', monospace;\n font-size: 12px;\n line-height: 1.3;\n display: flex;\n flex-direction: column;\n }\n #filter-bar {\n flex-shrink: 0;\n display: flex;\n gap: 8px;\n padding: 8px 12px;\n background: #252526;\n border-bottom: 1px solid #3c3c3c;\n }\n #filter-bar label {\n display: flex;\n align-items: center;\n gap: 6px;\n color: #888;\n }\n #filter-bar input {\n background: #1e1e1e;\n border: 1px solid #3c3c3c;\n border-radius: 3px;\n color: #d4d4d4;\n font-family: inherit;\n font-size: 12px;\n padding: 4px 8px;\n width: 200px;\n }\n #filter-bar input:focus {\n outline: none;\n border-color: #007acc;\n }\n #container {\n flex: 1;\n overflow-y: auto;\n padding: 8px 12px;\n }\n .line {\n white-space: pre-wrap;\n word-break: break-all;\n padding: 1px 4px;\n margin: 0 -4px;\n border-radius: 2px;\n position: relative;\n display: flex;\n align-items: flex-start;\n }\n .line:hover {\n background: rgba(255, 255, 255, 0.05);\n }\n .line.pinned {\n background: rgba(255, 204, 0, 0.1);\n border-left: 2px solid #fc0;\n margin-left: -6px;\n padding-left: 6px;\n }\n .line-content {\n flex: 1;\n }\n .pin-btn {\n opacity: 0;\n cursor: pointer;\n padding: 0 4px;\n color: #888;\n flex-shrink: 0;\n transition: opacity 0.15s;\n }\n .line:hover .pin-btn {\n opacity: 0.5;\n }\n .pin-btn:hover {\n opacity: 1 !important;\n color: #fc0;\n }\n .line.pinned .pin-btn {\n opacity: 1;\n color: #fc0;\n }\n a { color: #4fc1ff; text-decoration: underline; }\n a:hover { text-decoration: none; }\n mark { background: #623800; color: inherit; border-radius: 2px; }\n mark.filter { background: #264f00; }\n .json-key { color: #9cdcfe; }\n .json-string { color: #ce9178; }\n .json-number { color: #b5cea8; }\n .json-bool { color: #569cd6; }\n .json-null { color: #569cd6; }\n #tail-btn {\n position: fixed;\n bottom: 20px;\n right: 20px;\n background: #007acc;\n color: #fff;\n border: none;\n border-radius: 4px;\n padding: 8px 16px;\n font-family: inherit;\n font-size: 12px;\n cursor: pointer;\n display: none;\n align-items: center;\n gap: 6px;\n box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);\n transition: background 0.15s;\n }\n #tail-btn:hover {\n background: #0098ff;\n }\n #tail-btn svg {\n flex-shrink: 0;\n }\n </style>\n</head>\n<body>\n <div id=\"filter-bar\">\n <label>Include: <input type=\"text\" id=\"include\" placeholder=\"error*,warn* (OR, * = wildcard)\"></label>\n <label>Exclude: <input type=\"text\" id=\"exclude\" placeholder=\"health*,debug (OR, * = wildcard)\"></label>\n <label>Highlight: <input type=\"text\" id=\"highlight\" placeholder=\"term1,term2\"></label>\n </div>\n <div id=\"container\"></div>\n <button id=\"tail-btn\" title=\"Jump to bottom and follow new logs\">\n <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"14\" height=\"14\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path d=\"M12 5v14\"/><path d=\"m19 12-7 7-7-7\"/></svg>\n Tail\n </button>\n <script>\n const container = document.getElementById('container');\n const includeInput = document.getElementById('include');\n const excludeInput = document.getElementById('exclude');\n const highlightInput = document.getElementById('highlight');\n const tailBtn = document.getElementById('tail-btn');\n const params = new URLSearchParams(window.location.search);\n const tailSize = ${this.tailSize};\n \n includeInput.value = params.get('include') || '';\n excludeInput.value = params.get('exclude') || '';\n highlightInput.value = params.get('highlight') || '';\n \n let tailing = true;\n let pinnedIds = new Set();\n \n const updateTailButton = () => {\n tailBtn.style.display = tailing ? 'none' : 'flex';\n };\n \n // Lucide pin icon SVG\n const pinIcon = '<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"14\" height=\"14\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path d=\"M12 17v5\"/><path d=\"M9 10.76a2 2 0 0 1-1.11 1.79l-1.78.9A2 2 0 0 0 5 15.24V16a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-.76a2 2 0 0 0-1.11-1.79l-1.78-.9A2 2 0 0 1 15 10.76V7a1 1 0 0 1 1-1 2 2 0 0 0 0-4H8a2 2 0 0 0 0 4 1 1 0 0 1 1 1z\"/></svg>';\n \n const stripAnsi = (str) => str.replace(/\\\\u001B\\\\[[\\\\d;]*m/g, '');\n \n const globToRegex = (pattern) => {\n const escaped = pattern.replace(/([.+?^\\${}()|[\\\\]\\\\\\\\])/g, '\\\\\\\\$1');\n const regexPattern = escaped.replace(/\\\\*/g, '.*');\n return new RegExp(regexPattern, 'i');\n };\n \n const matchesPattern = (text, pattern) => {\n if (pattern.includes('*')) {\n return globToRegex(pattern).test(text);\n }\n return text.includes(pattern.toLowerCase());\n };\n \n const matchesFilters = (text, includes, excludes) => {\n const plain = stripAnsi(text).toLowerCase();\n if (includes.length > 0) {\n const anyMatch = includes.some(p => matchesPattern(plain, p));\n if (!anyMatch) return false;\n }\n if (excludes.length > 0) {\n const anyMatch = excludes.some(p => matchesPattern(plain, p));\n if (anyMatch) return false;\n }\n return true;\n };\n \n const highlightTerms = (html, terms, className = '') => {\n if (!terms.length) return html;\n let result = html;\n for (const term of terms) {\n if (!term) continue;\n const escaped = term.replace(/([.*+?^\\${}()|[\\\\]\\\\\\\\])/g, '\\\\\\\\$1');\n const regex = new RegExp('(?![^<]*>)(' + escaped + ')', 'gi');\n const cls = className ? ' class=\"' + className + '\"' : '';\n result = result.replace(regex, '<mark' + cls + '>$1</mark>');\n }\n return result;\n };\n \n const applyFilters = () => {\n const includes = includeInput.value.split(',').map(s => s.trim()).filter(Boolean);\n const excludes = excludeInput.value.split(',').map(s => s.trim()).filter(Boolean);\n const highlights = highlightInput.value.split(',').map(s => s.trim()).filter(Boolean);\n \n document.querySelectorAll('.line').forEach(line => {\n const id = line.dataset.id;\n const isPinned = pinnedIds.has(id);\n const text = line.dataset.raw;\n const matches = matchesFilters(text, includes, excludes);\n line.style.display = (matches || isPinned) ? '' : 'none';\n \n // Re-apply highlighting\n const contentEl = line.querySelector('.line-content');\n if (contentEl) {\n let html = line.dataset.html;\n html = highlightTerms(html, includes, 'filter');\n html = highlightTerms(html, highlights);\n contentEl.innerHTML = html;\n }\n });\n \n // Update URL without reload\n const newParams = new URLSearchParams();\n if (includeInput.value) newParams.set('include', includeInput.value);\n if (excludeInput.value) newParams.set('exclude', excludeInput.value);\n if (highlightInput.value) newParams.set('highlight', highlightInput.value);\n const newUrl = newParams.toString() ? '?' + newParams.toString() : window.location.pathname;\n history.replaceState(null, '', newUrl);\n \n // Jump to bottom and resume tailing after filter change\n container.scrollTop = container.scrollHeight;\n tailing = true;\n updateTailButton();\n };\n \n const trimBuffer = () => {\n const lines = container.querySelectorAll('.line');\n const unpinnedLines = Array.from(lines).filter(l => !pinnedIds.has(l.dataset.id));\n const excess = unpinnedLines.length - tailSize;\n if (excess > 0) {\n for (let i = 0; i < excess; i++) {\n unpinnedLines[i].remove();\n }\n }\n };\n \n let lineCounter = 0;\n const addLine = (html, raw) => {\n const id = 'line-' + (lineCounter++);\n const includes = includeInput.value.split(',').map(s => s.trim()).filter(Boolean);\n const excludes = excludeInput.value.split(',').map(s => s.trim()).filter(Boolean);\n const highlights = highlightInput.value.split(',').map(s => s.trim()).filter(Boolean);\n \n const div = document.createElement('div');\n div.className = 'line';\n div.dataset.id = id;\n div.dataset.raw = raw;\n div.dataset.html = html;\n \n let displayHtml = html;\n displayHtml = highlightTerms(displayHtml, includes, 'filter');\n displayHtml = highlightTerms(displayHtml, highlights);\n \n div.innerHTML = '<span class=\"line-content\">' + displayHtml + '</span><span class=\"pin-btn\" title=\"Pin\">' + pinIcon + '</span>';\n \n // Pin button handler\n div.querySelector('.pin-btn').addEventListener('click', (e) => {\n e.stopPropagation();\n if (pinnedIds.has(id)) {\n pinnedIds.delete(id);\n div.classList.remove('pinned');\n } else {\n pinnedIds.add(id);\n div.classList.add('pinned');\n }\n applyFilters();\n });\n \n const matches = matchesFilters(raw, includes, excludes);\n div.style.display = matches ? '' : 'none';\n \n container.appendChild(div);\n trimBuffer();\n if (tailing) container.scrollTop = container.scrollHeight;\n };\n \n container.addEventListener('scroll', () => {\n const atBottom = container.scrollHeight - container.scrollTop - container.clientHeight < 50;\n tailing = atBottom;\n updateTailButton();\n });\n \n tailBtn.addEventListener('click', () => {\n container.scrollTop = container.scrollHeight;\n tailing = true;\n updateTailButton();\n });\n \n let debounceTimer;\n const debounce = (fn, delay) => {\n clearTimeout(debounceTimer);\n debounceTimer = setTimeout(fn, delay);\n };\n \n includeInput.addEventListener('input', () => debounce(applyFilters, 50));\n excludeInput.addEventListener('input', () => debounce(applyFilters, 50));\n highlightInput.addEventListener('input', () => debounce(applyFilters, 50));\n </script>\n`;\n }\n\n private getHtmlLine(line: string): string {\n let html = this.ansiConverter.toHtml(line);\n html = highlightJson(html);\n html = linkifyUrls(html);\n const escaped = html.replaceAll('\\\\', '\\\\\\\\').replaceAll(\"'\", \"\\\\'\");\n const raw = stripAnsi(line).replaceAll('\\\\', '\\\\\\\\').replaceAll(\"'\", \"\\\\'\");\n return `<script>addLine('${escaped}', '${raw}')</script>\\n`;\n }\n\n private sendToClients(forWeb: string, timestamp: number): void {\n // Add to buffer\n this.buffer.push({ line: forWeb, timestamp });\n\n // Trim buffer to tail size\n if (this.buffer.length > this.tailSize) {\n this.buffer.shift();\n }\n\n // Send to all connected clients\n for (const client of this.clients) {\n if (client.isBrowser) {\n client.response.write(this.getHtmlLine(forWeb));\n } else {\n // Server-side filtering for non-browser clients\n if (!matchesFilters(forWeb, client.includes, client.excludes)) {\n continue;\n }\n\n client.response.write(stripAnsi(forWeb) + '\\n');\n }\n }\n\n // Note: Each client prints its own logs locally, so server doesn't need to\n }\n}\n","#!/usr/bin/env node\n\nimport { LogServer } from './LogServer.js';\nimport { spawn } from 'node:child_process';\nimport http from 'node:http';\nimport { performance } from 'node:perf_hooks';\nimport readline from 'node:readline';\nimport yargs from 'yargs';\nimport { hideBin } from 'yargs/helpers';\n\n// High-precision timestamp (milliseconds with microsecond precision)\nconst getTimestamp = (): number => performance.timeOrigin + performance.now();\n\nconst RESET = '\\u001B[0m';\nconst DIM = '\\u001B[90m';\nconst RED = '\\u001B[91m';\n\n// Leader monitoring configuration\nconst LEADER_CHECK_INTERVAL = 2_000; // Check every 2 seconds\nconst MAX_PROMOTION_RETRIES = 3;\n\ntype LogType = 'stderr' | 'stdout';\n\nclass LogClient {\n private name: string;\n\n private port: number;\n\n private queue: Array<{ line: string; timestamp: number; type: LogType }> = [];\n\n private sending = false;\n\n constructor(name: string, port: number) {\n this.name = name;\n this.port = port;\n }\n\n async event(\n event: 'exit' | 'start',\n pid: number,\n code?: number,\n ): Promise<void> {\n await this.send('/event', {\n code,\n event,\n name: this.name,\n pid,\n timestamp: getTimestamp(),\n });\n }\n\n async flush(): Promise<void> {\n if (this.sending || this.queue.length === 0) {\n return;\n }\n\n this.sending = true;\n\n while (this.queue.length > 0) {\n const item = this.queue.shift();\n\n if (!item) {\n continue;\n }\n\n const success = await this.send('/log', {\n line: item.line,\n name: this.name,\n timestamp: item.timestamp,\n type: item.type,\n });\n\n if (!success) {\n // Fallback to local output if server unreachable\n // eslint-disable-next-line no-console\n console.log(`[${this.name}] ${item.line}`);\n }\n }\n\n this.sending = false;\n }\n\n log(line: string, type: LogType = 'stdout'): void {\n // Always output locally\n const errorPrefix = type === 'stderr' ? `${RED}[ERR]${RESET} ` : '';\n\n // eslint-disable-next-line no-console\n console.log(`${errorPrefix}${line}`);\n\n // Capture timestamp immediately when log is received\n this.queue.push({ line, timestamp: getTimestamp(), type });\n void this.flush();\n }\n\n private async send(endpoint: string, data: object): Promise<boolean> {\n return new Promise((resolve) => {\n const postData = JSON.stringify(data);\n const request = http.request(\n {\n headers: {\n 'Content-Length': Buffer.byteLength(postData),\n 'Content-Type': 'application/json',\n },\n hostname: '127.0.0.1',\n method: 'POST',\n path: endpoint,\n port: this.port,\n timeout: 1_000,\n },\n (response) => {\n response.resume();\n response.on('end', () => resolve(true));\n },\n );\n\n request.on('error', () => resolve(false));\n request.on('timeout', () => {\n request.destroy();\n resolve(false);\n });\n request.write(postData);\n request.end();\n });\n }\n}\n\nconst runProcess = async (\n name: string,\n command: string[],\n client: LogClient,\n): Promise<number> => {\n const [cmd, ...args] = command;\n\n const child = spawn(cmd, args, {\n env: {\n ...process.env,\n FORCE_COLOR: '1',\n },\n shell: process.platform === 'win32',\n stdio: ['inherit', 'pipe', 'pipe'],\n });\n\n const pid = child.pid ?? 0;\n\n await client.event('start', pid);\n\n let rlStdout: null | readline.Interface = null;\n let rlStderr: null | readline.Interface = null;\n\n if (child.stdout) {\n rlStdout = readline.createInterface({ input: child.stdout });\n\n rlStdout.on('line', (line) => client.log(line, 'stdout'));\n }\n\n if (child.stderr) {\n rlStderr = readline.createInterface({ input: child.stderr });\n\n rlStderr.on('line', (line) => client.log(line, 'stderr'));\n }\n\n // Track signal count for force-kill on second signal\n let signalCount = 0;\n\n const onSignal = (): void => {\n signalCount++;\n\n if (signalCount >= 2 && child.pid && !child.killed) {\n // Second signal: force kill\n child.kill('SIGKILL');\n }\n };\n\n process.on('SIGINT', onSignal);\n process.on('SIGTERM', onSignal);\n process.on('SIGHUP', onSignal);\n\n return new Promise((resolve) => {\n child.on('close', async (code) => {\n // Clean up readline interfaces\n rlStdout?.close();\n rlStderr?.close();\n\n // Remove signal handlers\n process.off('SIGINT', onSignal);\n process.off('SIGTERM', onSignal);\n process.off('SIGHUP', onSignal);\n\n await client.flush();\n await client.event('exit', pid, code ?? 0);\n resolve(code ?? 0);\n });\n });\n};\n\nconst sleep = (ms: number): Promise<void> =>\n new Promise((resolve) => {\n setTimeout(resolve, ms);\n });\n\nconst checkServerReady = async (port: number): Promise<boolean> => {\n return new Promise((resolve) => {\n const request = http.request(\n {\n hostname: '127.0.0.1',\n method: 'GET',\n path: '/',\n port,\n timeout: 200,\n },\n (response) => {\n response.resume();\n resolve(true);\n },\n );\n\n request.on('error', () => resolve(false));\n request.on('timeout', () => {\n request.destroy();\n resolve(false);\n });\n request.end();\n });\n};\n\nconst waitForServer = async (\n port: number,\n maxAttempts = 50,\n): Promise<boolean> => {\n for (let index = 0; index < maxAttempts; index++) {\n if (await checkServerReady(port)) {\n return true;\n }\n\n // Exponential backoff: 10ms, 20ms, 40ms, ... capped at 200ms\n const delay = Math.min(10 * 2 ** index, 200);\n\n await sleep(delay);\n }\n\n return false;\n};\n\nconst tryBecomeLeader = async (server: LogServer): Promise<boolean> => {\n for (let attempt = 0; attempt < MAX_PROMOTION_RETRIES; attempt++) {\n try {\n await server.start();\n return true;\n } catch (error) {\n if ((error as NodeJS.ErrnoException).code !== 'EADDRINUSE') {\n throw error;\n }\n\n // Check if another server took over\n if (await checkServerReady(server.getPort())) {\n // Another process became leader\n return false;\n }\n\n // Port in use but server not responding - might be starting up\n // Add random jitter to avoid thundering herd\n const jitter = Math.random() * 100;\n\n await sleep(50 + jitter);\n }\n }\n\n return false;\n};\n\nconst startLeaderMonitoring = (\n server: LogServer,\n port: number,\n): { stop: () => void } => {\n let isRunning = true;\n let timeoutId: null | ReturnType<typeof setTimeout> = null;\n\n const checkAndPromote = async (): Promise<void> => {\n if (!isRunning) {\n return;\n }\n\n const serverAlive = await checkServerReady(port);\n\n if (!serverAlive && isRunning) {\n // Leader might be dead, try to become leader\n // Add random jitter to prevent all clients from trying simultaneously\n const jitter = Math.random() * 500;\n\n await sleep(jitter);\n\n // Double-check server is still down after jitter\n if (isRunning && !(await checkServerReady(port))) {\n const promoted = await tryBecomeLeader(server);\n\n if (promoted) {\n // eslint-disable-next-line no-console\n console.log(\n `${DIM}[teemux] promoted to leader, now aggregating logs${RESET}`,\n );\n // Stop monitoring - we're now the leader\n // eslint-disable-next-line require-atomic-updates -- safe: only modified here or in stop()\n isRunning = false;\n return;\n }\n }\n }\n\n // Schedule next check\n if (isRunning) {\n timeoutId = setTimeout(() => {\n void checkAndPromote();\n }, LEADER_CHECK_INTERVAL);\n }\n };\n\n // Start monitoring after initial delay\n timeoutId = setTimeout(() => {\n void checkAndPromote();\n }, LEADER_CHECK_INTERVAL);\n\n return {\n stop: () => {\n isRunning = false;\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n },\n };\n};\n\nconst main = async (): Promise<void> => {\n const argv = await yargs(hideBin(process.argv))\n .env('TEEMUX')\n .usage('Usage: $0 --name <name> -- <command> [args...]')\n .option('name', {\n alias: 'n',\n description:\n 'Name to identify this process in logs (defaults to command)',\n type: 'string',\n })\n .option('port', {\n alias: 'p',\n default: 8_336,\n description: 'Port for the log aggregation server',\n type: 'number',\n })\n .option('tail', {\n alias: 't',\n default: 1_000,\n description: 'Number of log lines to keep in buffer',\n type: 'number',\n })\n .help()\n .parse();\n\n const command = argv._ as string[];\n\n if (command.length === 0) {\n // eslint-disable-next-line no-console\n console.error('No command specified');\n // eslint-disable-next-line no-console\n console.error('Usage: teemux --name <name> -- <command> [args...]');\n process.exit(1);\n }\n\n const name = argv.name ?? command[0] ?? 'unknown';\n const port = argv.port;\n\n const server = new LogServer(port, argv.tail);\n\n // Try to become server with retries - if port is taken, become client\n let isServer = false;\n const maxRetries = 3;\n\n for (let attempt = 0; attempt < maxRetries; attempt++) {\n try {\n await server.start();\n isServer = true;\n break;\n } catch (error) {\n if ((error as NodeJS.ErrnoException).code !== 'EADDRINUSE') {\n throw error;\n }\n\n // Check if another server is actually running\n if (await checkServerReady(port)) {\n // Server exists, we're a client\n break;\n }\n\n // Port in use but server not responding - might be starting up\n // Add random jitter to avoid thundering herd\n const jitter = Math.random() * 100;\n\n await sleep(50 + jitter);\n }\n }\n\n // If we're not the server, wait for it to be ready and start monitoring\n let leaderMonitor: null | { stop: () => void } = null;\n\n if (!isServer) {\n const serverReady = await waitForServer(port);\n\n if (!serverReady) {\n // eslint-disable-next-line no-console\n console.error(\n '[teemux] Could not connect to server. Is another instance running?',\n );\n }\n\n // Start monitoring for leader failover\n leaderMonitor = startLeaderMonitoring(server, port);\n }\n\n const client = new LogClient(name, port);\n\n // Cleanup function for graceful shutdown\n const cleanup = async (): Promise<void> => {\n leaderMonitor?.stop();\n\n if (isServer) {\n await server.stop();\n }\n };\n\n // Run the process\n const exitCode = await runProcess(name, command, client);\n\n // Stop leader monitoring if running\n await cleanup();\n\n process.exit(exitCode);\n};\n\nmain().catch((error: unknown) => {\n // eslint-disable-next-line no-console\n console.error('Fatal error:', error);\n process.exit(1);\n});\n"],"mappings":";;;;;;;;;;;;;;AAGA,MAAa,iBAAiB,SAAyB;AACrD,QAAO,KAAK,WAAW,aAAa,GAAG;;;;;;;;ACDzC,MAAa,gBAAgB,SAAyB;AACpD,QAAO,KACJ,WAAW,UAAU,KAAI,CACzB,WAAW,SAAS,IAAI,CACxB,WAAW,QAAQ,IAAI,CACvB,WAAW,QAAQ,IAAI,CACvB,WAAW,UAAU,IAAI,CACzB,WAAW,SAAS,IAAI;;;;;;;;;ACH7B,MAAa,qBAAqB,SAAyB;CAEzD,MAAM,UAAoB,EAAE;CAC5B,IAAI,SAAS,KAAK,WAChB,qCACC,QAAQ,YAAY;AACnB,UAAQ,KAAK,QAAkB;AAC/B,SAAO,YAAY,QAAQ,SAAS,EAAE;GAEzC;AAGD,UAAS,OAAO,WACd,2BACA,sCACD;AAGD,UAAS,OAAO,WACd,iDACA,wCACD;AAGD,UAAS,OAAO,WACd,0BACC,QAAQ,OAAO,UAAU;EACxB,MAAM,UAAU,QAAQ,OAAO,SAAS,OAAiB,GAAG;AAC5D,MAAI,MAEF,QAAO,gCAAgC,QAAQ,eAAe;AAIhE,SAAO,mCAAmC,QAAQ;GAErD;AAED,QAAO;;;;;AAMT,MAAa,uBAAuB,SAAyB;CAC3D,IAAI,SAAS;CACb,IAAI,QAAQ;AAEZ,QAAO,QAAQ,KAAK,OAClB,KAAI,KAAK,WAAW,KAAK;EAEvB,MAAM,SAAS,KAAK,QAAQ,KAAK,MAAM;AAC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|
@@ -537,6 +537,11 @@ export class LogServer {
|
|
|
537
537
|
if (highlightInput.value) newParams.set('highlight', highlightInput.value);
|
|
538
538
|
const newUrl = newParams.toString() ? '?' + newParams.toString() : window.location.pathname;
|
|
539
539
|
history.replaceState(null, '', newUrl);
|
|
540
|
+
|
|
541
|
+
// Jump to bottom and resume tailing after filter change
|
|
542
|
+
container.scrollTop = container.scrollHeight;
|
|
543
|
+
tailing = true;
|
|
544
|
+
updateTailButton();
|
|
540
545
|
};
|
|
541
546
|
|
|
542
547
|
const trimBuffer = () => {
|
package/src/teemux.ts
CHANGED
|
@@ -144,20 +144,48 @@ const runProcess = async (
|
|
|
144
144
|
|
|
145
145
|
await client.event('start', pid);
|
|
146
146
|
|
|
147
|
+
let rlStdout: null | readline.Interface = null;
|
|
148
|
+
let rlStderr: null | readline.Interface = null;
|
|
149
|
+
|
|
147
150
|
if (child.stdout) {
|
|
148
|
-
|
|
151
|
+
rlStdout = readline.createInterface({ input: child.stdout });
|
|
149
152
|
|
|
150
153
|
rlStdout.on('line', (line) => client.log(line, 'stdout'));
|
|
151
154
|
}
|
|
152
155
|
|
|
153
156
|
if (child.stderr) {
|
|
154
|
-
|
|
157
|
+
rlStderr = readline.createInterface({ input: child.stderr });
|
|
155
158
|
|
|
156
159
|
rlStderr.on('line', (line) => client.log(line, 'stderr'));
|
|
157
160
|
}
|
|
158
161
|
|
|
162
|
+
// Track signal count for force-kill on second signal
|
|
163
|
+
let signalCount = 0;
|
|
164
|
+
|
|
165
|
+
const onSignal = (): void => {
|
|
166
|
+
signalCount++;
|
|
167
|
+
|
|
168
|
+
if (signalCount >= 2 && child.pid && !child.killed) {
|
|
169
|
+
// Second signal: force kill
|
|
170
|
+
child.kill('SIGKILL');
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
process.on('SIGINT', onSignal);
|
|
175
|
+
process.on('SIGTERM', onSignal);
|
|
176
|
+
process.on('SIGHUP', onSignal);
|
|
177
|
+
|
|
159
178
|
return new Promise((resolve) => {
|
|
160
179
|
child.on('close', async (code) => {
|
|
180
|
+
// Clean up readline interfaces
|
|
181
|
+
rlStdout?.close();
|
|
182
|
+
rlStderr?.close();
|
|
183
|
+
|
|
184
|
+
// Remove signal handlers
|
|
185
|
+
process.off('SIGINT', onSignal);
|
|
186
|
+
process.off('SIGTERM', onSignal);
|
|
187
|
+
process.off('SIGHUP', onSignal);
|
|
188
|
+
|
|
161
189
|
await client.flush();
|
|
162
190
|
await client.event('exit', pid, code ?? 0);
|
|
163
191
|
resolve(code ?? 0);
|
|
@@ -389,11 +417,20 @@ const main = async (): Promise<void> => {
|
|
|
389
417
|
|
|
390
418
|
const client = new LogClient(name, port);
|
|
391
419
|
|
|
420
|
+
// Cleanup function for graceful shutdown
|
|
421
|
+
const cleanup = async (): Promise<void> => {
|
|
422
|
+
leaderMonitor?.stop();
|
|
423
|
+
|
|
424
|
+
if (isServer) {
|
|
425
|
+
await server.stop();
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
|
|
392
429
|
// Run the process
|
|
393
430
|
const exitCode = await runProcess(name, command, client);
|
|
394
431
|
|
|
395
432
|
// Stop leader monitoring if running
|
|
396
|
-
|
|
433
|
+
await cleanup();
|
|
397
434
|
|
|
398
435
|
process.exit(exitCode);
|
|
399
436
|
};
|