teemux 1.0.1 → 1.2.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 CHANGED
@@ -5,7 +5,7 @@ Aggregate logs from multiple processes in a single view – in browser or termin
5
5
  ## Motivation
6
6
 
7
7
  * Needed a simple way to browse logs aggregated across multiple processes.
8
- * Needed a simple way to give agents a unified view of all the logs.
8
+ * Needed a simple way to [give agents a unified view of all the logs](#agentsmd).
9
9
 
10
10
  ## Install
11
11
 
@@ -140,6 +140,18 @@ GET /health 200
140
140
 
141
141
  ## FAQ
142
142
 
143
+ ### How does teemux work?
144
+
145
+ teemux uses automatic leader discovery to coordinate log aggregation across multiple processes:
146
+
147
+ 1. **Leader Discovery**: When the first teemux process starts, it attempts to bind to the configured port (default 8336). If successful, it becomes the **leader** and starts the log aggregation server.
148
+
149
+ 2. **Client Registration**: When subsequent teemux processes start, they detect the port is already in use, verify a server is responding, and automatically become **clients** that forward their logs to the leader.
150
+
151
+ 3. **Leader Election**: If the leader process exits, clients detect this through periodic health checks (every 2 seconds). When a client detects the leader is gone, it attempts to become the new leader. Random jitter prevents multiple clients from racing to claim leadership simultaneously.
152
+
153
+ This design requires no configuration – just run multiple `teemux` commands and they automatically coordinate.
154
+
143
155
  ### Docker output appears corrupted with strange spacing
144
156
 
145
157
  When running Docker with the `-t` flag, output may appear corrupted:
@@ -164,4 +176,4 @@ teemux --name db -- docker run --rm my-database
164
176
 
165
177
  The flags:
166
178
  - `-i` = keep stdin open (for interactive input) ✅
167
- - `-t` = allocate pseudo-TTY (adds terminal formatting) ❌
179
+ - `-t` = allocate pseudo-TTY (adds terminal formatting) ❌
package/dist/teemux.js CHANGED
@@ -162,7 +162,7 @@ const COLORS = [
162
162
  "\x1B[93m"
163
163
  ];
164
164
  const RESET$1 = "\x1B[0m";
165
- const DIM = "\x1B[90m";
165
+ const DIM$1 = "\x1B[90m";
166
166
  const RED$1 = "\x1B[91m";
167
167
  const HOST = "0.0.0.0";
168
168
  var LogServer = class {
@@ -270,7 +270,7 @@ var LogServer = class {
270
270
  reject(error);
271
271
  });
272
272
  this.server.listen(this.port, "0.0.0.0", () => {
273
- console.log(`${DIM}[teemux] aggregating logs on http://${HOST}:${this.port}${RESET$1}`);
273
+ console.log(`${DIM$1}[teemux] aggregating logs on http://${HOST}:${this.port}${RESET$1}`);
274
274
  resolve();
275
275
  });
276
276
  });
@@ -287,7 +287,7 @@ var LogServer = class {
287
287
  });
288
288
  }
289
289
  broadcastEvent(name, message, timestamp) {
290
- const forWeb = `${DIM}${this.getColor(name)}[${name}]${RESET$1} ${DIM}${message}${RESET$1}`;
290
+ const forWeb = `${DIM$1}${this.getColor(name)}[${name}]${RESET$1} ${DIM$1}${message}${RESET$1}`;
291
291
  this.sendToClients(forWeb, timestamp);
292
292
  }
293
293
  broadcastLog(name, line, type, timestamp) {
@@ -403,6 +403,30 @@ var LogServer = class {
403
403
  .json-number { color: #b5cea8; }
404
404
  .json-bool { color: #569cd6; }
405
405
  .json-null { color: #569cd6; }
406
+ #tail-btn {
407
+ position: fixed;
408
+ bottom: 20px;
409
+ right: 20px;
410
+ background: #007acc;
411
+ color: #fff;
412
+ border: none;
413
+ border-radius: 4px;
414
+ padding: 8px 16px;
415
+ font-family: inherit;
416
+ font-size: 12px;
417
+ cursor: pointer;
418
+ display: none;
419
+ align-items: center;
420
+ gap: 6px;
421
+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
422
+ transition: background 0.15s;
423
+ }
424
+ #tail-btn:hover {
425
+ background: #0098ff;
426
+ }
427
+ #tail-btn svg {
428
+ flex-shrink: 0;
429
+ }
406
430
  </style>
407
431
  </head>
408
432
  <body>
@@ -412,11 +436,16 @@ var LogServer = class {
412
436
  <label>Highlight: <input type="text" id="highlight" placeholder="term1,term2"></label>
413
437
  </div>
414
438
  <div id="container"></div>
439
+ <button id="tail-btn" title="Jump to bottom and follow new logs">
440
+ <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>
441
+ Tail
442
+ </button>
415
443
  <script>
416
444
  const container = document.getElementById('container');
417
445
  const includeInput = document.getElementById('include');
418
446
  const excludeInput = document.getElementById('exclude');
419
447
  const highlightInput = document.getElementById('highlight');
448
+ const tailBtn = document.getElementById('tail-btn');
420
449
  const params = new URLSearchParams(window.location.search);
421
450
  const tailSize = ${this.tailSize};
422
451
 
@@ -427,6 +456,10 @@ var LogServer = class {
427
456
  let tailing = true;
428
457
  let pinnedIds = new Set();
429
458
 
459
+ const updateTailButton = () => {
460
+ tailBtn.style.display = tailing ? 'none' : 'flex';
461
+ };
462
+
430
463
  // Lucide pin icon SVG
431
464
  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>';
432
465
 
@@ -556,6 +589,13 @@ var LogServer = class {
556
589
  container.addEventListener('scroll', () => {
557
590
  const atBottom = container.scrollHeight - container.scrollTop - container.clientHeight < 50;
558
591
  tailing = atBottom;
592
+ updateTailButton();
593
+ });
594
+
595
+ tailBtn.addEventListener('click', () => {
596
+ container.scrollTop = container.scrollHeight;
597
+ tailing = true;
598
+ updateTailButton();
559
599
  });
560
600
 
561
601
  let debounceTimer;
@@ -594,7 +634,10 @@ var LogServer = class {
594
634
  //#region src/teemux.ts
595
635
  const getTimestamp = () => performance.timeOrigin + performance.now();
596
636
  const RESET = "\x1B[0m";
637
+ const DIM = "\x1B[90m";
597
638
  const RED = "\x1B[91m";
639
+ const LEADER_CHECK_INTERVAL = 2e3;
640
+ const MAX_PROMOTION_RETRIES = 3;
598
641
  var LogClient = class {
599
642
  name;
600
643
  port;
@@ -721,6 +764,44 @@ const waitForServer = async (port, maxAttempts = 50) => {
721
764
  }
722
765
  return false;
723
766
  };
767
+ const tryBecomeLeader = async (server) => {
768
+ for (let attempt = 0; attempt < MAX_PROMOTION_RETRIES; attempt++) try {
769
+ await server.start();
770
+ return true;
771
+ } catch (error) {
772
+ if (error.code !== "EADDRINUSE") throw error;
773
+ if (await checkServerReady(server.getPort())) return false;
774
+ await sleep(50 + Math.random() * 100);
775
+ }
776
+ return false;
777
+ };
778
+ const startLeaderMonitoring = (server, port) => {
779
+ let isRunning = true;
780
+ let timeoutId = null;
781
+ const checkAndPromote = async () => {
782
+ if (!isRunning) return;
783
+ if (!await checkServerReady(port) && isRunning) {
784
+ await sleep(Math.random() * 500);
785
+ if (isRunning && !await checkServerReady(port)) {
786
+ if (await tryBecomeLeader(server)) {
787
+ console.log(`${DIM}[teemux] promoted to leader, now aggregating logs${RESET}`);
788
+ isRunning = false;
789
+ return;
790
+ }
791
+ }
792
+ }
793
+ if (isRunning) timeoutId = setTimeout(() => {
794
+ checkAndPromote();
795
+ }, LEADER_CHECK_INTERVAL);
796
+ };
797
+ timeoutId = setTimeout(() => {
798
+ checkAndPromote();
799
+ }, LEADER_CHECK_INTERVAL);
800
+ return { stop: () => {
801
+ isRunning = false;
802
+ if (timeoutId) clearTimeout(timeoutId);
803
+ } };
804
+ };
724
805
  const main = async () => {
725
806
  const argv = await yargs(hideBin(process.argv)).env("TEEMUX").usage("Usage: $0 --name <name> -- <command> [args...]").option("name", {
726
807
  alias: "n",
@@ -757,10 +838,13 @@ const main = async () => {
757
838
  if (await checkServerReady(port)) break;
758
839
  await sleep(50 + Math.random() * 100);
759
840
  }
841
+ let leaderMonitor = null;
760
842
  if (!isServer) {
761
843
  if (!await waitForServer(port)) console.error("[teemux] Could not connect to server. Is another instance running?");
844
+ leaderMonitor = startLeaderMonitoring(server, port);
762
845
  }
763
846
  const exitCode = await runProcess(name, command, new LogClient(name, port));
847
+ leaderMonitor?.stop();
764
848
  process.exit(exitCode);
765
849
  };
766
850
  main().catch((error) => {
@@ -1 +1 @@
1
- {"version":3,"file":"teemux.js","names":["RESET","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('&quot;', '\"')\n .replaceAll('&amp;', '&')\n .replaceAll('&lt;', '<')\n .replaceAll('&gt;', '>')\n .replaceAll('&#x27;', \"'\")\n .replaceAll('&#39;', \"'\");\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 (&quot;).\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 /&quot;((?:(?!&quot;).)*)&quot;/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\">&quot;${content}&quot;</span>${colon}`;\n }\n\n // This is a value\n return `<span class=\"json-string\">&quot;${content}&quot;</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 (&quot; &amp; 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('&', '&amp;')\n .replaceAll('\"', '&quot;');\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 </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 <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 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 // 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 });\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 RED = '\\u001B[91m';\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 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\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\n const client = new LogClient(name, port);\n\n // Run the process\n const exitCode = await runProcess(name, command, client);\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,SAA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AM,MAAM,KAFG,KAAK,QAAQ,GAAG,IAEP;;AAK5B,KAAI,CAAC,UAGH;MAAI,CAFgB,MAAM,cAAc,KAAK,CAI3C,SAAQ,MACN,qEACD;;CAOL,MAAM,WAAW,MAAM,WAAW,MAAM,SAHzB,IAAI,UAAU,MAAM,KAAK,CAGgB;AAExD,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('&quot;', '\"')\n .replaceAll('&amp;', '&')\n .replaceAll('&lt;', '<')\n .replaceAll('&gt;', '>')\n .replaceAll('&#x27;', \"'\")\n .replaceAll('&#39;', \"'\");\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 (&quot;).\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 /&quot;((?:(?!&quot;).)*)&quot;/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\">&quot;${content}&quot;</span>${colon}`;\n }\n\n // This is a value\n return `<span class=\"json-string\">&quot;${content}&quot;</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 (&quot; &amp; 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('&', '&amp;')\n .replaceAll('\"', '&quot;');\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,sC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EACP,CAAC,CACD,OAAO,QAAQ;EACd,OAAO;EACP,SAAS;EACT,aAAa;EACb,MAAM;EACP,CAAC,CACD,OAAO,QAAQ;EACd,OAAO;EACP,SAAS;EACT,aAAa;EACb,MAAM;EACP,CAAC,CACD,MAAM,CACN,OAAO;CAEV,MAAM,UAAU,KAAK;AAErB,KAAI,QAAQ,WAAW,GAAG;AAExB,UAAQ,MAAM,uBAAuB;AAErC,UAAQ,MAAM,qDAAqD;AACnE,UAAQ,KAAK,EAAE;;CAGjB,MAAM,OAAO,KAAK,QAAQ,QAAQ,MAAM;CACxC,MAAM,OAAO,KAAK;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"}
package/package.json CHANGED
@@ -86,5 +86,5 @@
86
86
  },
87
87
  "type": "module",
88
88
  "types": "./dist/teemux.d.ts",
89
- "version": "1.0.1"
89
+ "version": "1.2.0"
90
90
  }
package/src/LogServer.ts CHANGED
@@ -407,6 +407,30 @@ export class LogServer {
407
407
  .json-number { color: #b5cea8; }
408
408
  .json-bool { color: #569cd6; }
409
409
  .json-null { color: #569cd6; }
410
+ #tail-btn {
411
+ position: fixed;
412
+ bottom: 20px;
413
+ right: 20px;
414
+ background: #007acc;
415
+ color: #fff;
416
+ border: none;
417
+ border-radius: 4px;
418
+ padding: 8px 16px;
419
+ font-family: inherit;
420
+ font-size: 12px;
421
+ cursor: pointer;
422
+ display: none;
423
+ align-items: center;
424
+ gap: 6px;
425
+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
426
+ transition: background 0.15s;
427
+ }
428
+ #tail-btn:hover {
429
+ background: #0098ff;
430
+ }
431
+ #tail-btn svg {
432
+ flex-shrink: 0;
433
+ }
410
434
  </style>
411
435
  </head>
412
436
  <body>
@@ -416,11 +440,16 @@ export class LogServer {
416
440
  <label>Highlight: <input type="text" id="highlight" placeholder="term1,term2"></label>
417
441
  </div>
418
442
  <div id="container"></div>
443
+ <button id="tail-btn" title="Jump to bottom and follow new logs">
444
+ <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>
445
+ Tail
446
+ </button>
419
447
  <script>
420
448
  const container = document.getElementById('container');
421
449
  const includeInput = document.getElementById('include');
422
450
  const excludeInput = document.getElementById('exclude');
423
451
  const highlightInput = document.getElementById('highlight');
452
+ const tailBtn = document.getElementById('tail-btn');
424
453
  const params = new URLSearchParams(window.location.search);
425
454
  const tailSize = ${this.tailSize};
426
455
 
@@ -431,6 +460,10 @@ export class LogServer {
431
460
  let tailing = true;
432
461
  let pinnedIds = new Set();
433
462
 
463
+ const updateTailButton = () => {
464
+ tailBtn.style.display = tailing ? 'none' : 'flex';
465
+ };
466
+
434
467
  // Lucide pin icon SVG
435
468
  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>';
436
469
 
@@ -560,6 +593,13 @@ export class LogServer {
560
593
  container.addEventListener('scroll', () => {
561
594
  const atBottom = container.scrollHeight - container.scrollTop - container.clientHeight < 50;
562
595
  tailing = atBottom;
596
+ updateTailButton();
597
+ });
598
+
599
+ tailBtn.addEventListener('click', () => {
600
+ container.scrollTop = container.scrollHeight;
601
+ tailing = true;
602
+ updateTailButton();
563
603
  });
564
604
 
565
605
  let debounceTimer;
package/src/teemux.ts CHANGED
@@ -12,8 +12,13 @@ import { hideBin } from 'yargs/helpers';
12
12
  const getTimestamp = (): number => performance.timeOrigin + performance.now();
13
13
 
14
14
  const RESET = '\u001B[0m';
15
+ const DIM = '\u001B[90m';
15
16
  const RED = '\u001B[91m';
16
17
 
18
+ // Leader monitoring configuration
19
+ const LEADER_CHECK_INTERVAL = 2_000; // Check every 2 seconds
20
+ const MAX_PROMOTION_RETRIES = 3;
21
+
17
22
  type LogType = 'stderr' | 'stdout';
18
23
 
19
24
  class LogClient {
@@ -208,6 +213,95 @@ const waitForServer = async (
208
213
  return false;
209
214
  };
210
215
 
216
+ const tryBecomeLeader = async (server: LogServer): Promise<boolean> => {
217
+ for (let attempt = 0; attempt < MAX_PROMOTION_RETRIES; attempt++) {
218
+ try {
219
+ await server.start();
220
+ return true;
221
+ } catch (error) {
222
+ if ((error as NodeJS.ErrnoException).code !== 'EADDRINUSE') {
223
+ throw error;
224
+ }
225
+
226
+ // Check if another server took over
227
+ if (await checkServerReady(server.getPort())) {
228
+ // Another process became leader
229
+ return false;
230
+ }
231
+
232
+ // Port in use but server not responding - might be starting up
233
+ // Add random jitter to avoid thundering herd
234
+ const jitter = Math.random() * 100;
235
+
236
+ await sleep(50 + jitter);
237
+ }
238
+ }
239
+
240
+ return false;
241
+ };
242
+
243
+ const startLeaderMonitoring = (
244
+ server: LogServer,
245
+ port: number,
246
+ ): { stop: () => void } => {
247
+ let isRunning = true;
248
+ let timeoutId: null | ReturnType<typeof setTimeout> = null;
249
+
250
+ const checkAndPromote = async (): Promise<void> => {
251
+ if (!isRunning) {
252
+ return;
253
+ }
254
+
255
+ const serverAlive = await checkServerReady(port);
256
+
257
+ if (!serverAlive && isRunning) {
258
+ // Leader might be dead, try to become leader
259
+ // Add random jitter to prevent all clients from trying simultaneously
260
+ const jitter = Math.random() * 500;
261
+
262
+ await sleep(jitter);
263
+
264
+ // Double-check server is still down after jitter
265
+ if (isRunning && !(await checkServerReady(port))) {
266
+ const promoted = await tryBecomeLeader(server);
267
+
268
+ if (promoted) {
269
+ // eslint-disable-next-line no-console
270
+ console.log(
271
+ `${DIM}[teemux] promoted to leader, now aggregating logs${RESET}`,
272
+ );
273
+ // Stop monitoring - we're now the leader
274
+ // eslint-disable-next-line require-atomic-updates -- safe: only modified here or in stop()
275
+ isRunning = false;
276
+ return;
277
+ }
278
+ }
279
+ }
280
+
281
+ // Schedule next check
282
+ if (isRunning) {
283
+ timeoutId = setTimeout(() => {
284
+ void checkAndPromote();
285
+ }, LEADER_CHECK_INTERVAL);
286
+ }
287
+ };
288
+
289
+ // Start monitoring after initial delay
290
+ timeoutId = setTimeout(() => {
291
+ void checkAndPromote();
292
+ }, LEADER_CHECK_INTERVAL);
293
+
294
+ return {
295
+ stop: () => {
296
+ isRunning = false;
297
+
298
+ if (timeoutId) {
299
+ clearTimeout(timeoutId);
300
+ }
301
+ },
302
+ };
303
+ };
304
+
211
305
  const main = async (): Promise<void> => {
212
306
  const argv = await yargs(hideBin(process.argv))
213
307
  .env('TEEMUX')
@@ -276,7 +370,9 @@ const main = async (): Promise<void> => {
276
370
  }
277
371
  }
278
372
 
279
- // If we're not the server, wait for it to be ready
373
+ // If we're not the server, wait for it to be ready and start monitoring
374
+ let leaderMonitor: null | { stop: () => void } = null;
375
+
280
376
  if (!isServer) {
281
377
  const serverReady = await waitForServer(port);
282
378
 
@@ -286,6 +382,9 @@ const main = async (): Promise<void> => {
286
382
  '[teemux] Could not connect to server. Is another instance running?',
287
383
  );
288
384
  }
385
+
386
+ // Start monitoring for leader failover
387
+ leaderMonitor = startLeaderMonitoring(server, port);
289
388
  }
290
389
 
291
390
  const client = new LogClient(name, port);
@@ -293,6 +392,9 @@ const main = async (): Promise<void> => {
293
392
  // Run the process
294
393
  const exitCode = await runProcess(name, command, client);
295
394
 
395
+ // Stop leader monitoring if running
396
+ leaderMonitor?.stop();
397
+
296
398
  process.exit(exitCode);
297
399
  };
298
400