@geekmidas/telescope 0.0.1

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.
Files changed (103) hide show
  1. package/README.md +521 -0
  2. package/dist/Telescope-B3Wd82yk.cjs +602 -0
  3. package/dist/Telescope-B3Wd82yk.cjs.map +1 -0
  4. package/dist/Telescope-C5dyDYYB.d.cts +133 -0
  5. package/dist/Telescope-D-uoZB6b.mjs +596 -0
  6. package/dist/Telescope-D-uoZB6b.mjs.map +1 -0
  7. package/dist/Telescope-DyIWgh9-.d.mts +133 -0
  8. package/dist/Telescope.cjs +3 -0
  9. package/dist/Telescope.d.cts +3 -0
  10. package/dist/Telescope.d.mts +3 -0
  11. package/dist/Telescope.mjs +3 -0
  12. package/dist/chunk-CUT6urMc.cjs +30 -0
  13. package/dist/index.cjs +5 -0
  14. package/dist/index.d.cts +4 -0
  15. package/dist/index.d.mts +4 -0
  16. package/dist/index.mjs +4 -0
  17. package/dist/logger/console.cjs +161 -0
  18. package/dist/logger/console.cjs.map +1 -0
  19. package/dist/logger/console.d.cts +109 -0
  20. package/dist/logger/console.d.mts +109 -0
  21. package/dist/logger/console.mjs +159 -0
  22. package/dist/logger/console.mjs.map +1 -0
  23. package/dist/logger/pino.cjs +118 -0
  24. package/dist/logger/pino.cjs.map +1 -0
  25. package/dist/logger/pino.d.cts +89 -0
  26. package/dist/logger/pino.d.mts +89 -0
  27. package/dist/logger/pino.mjs +116 -0
  28. package/dist/logger/pino.mjs.map +1 -0
  29. package/dist/memory-9-B9WACq.cjs +110 -0
  30. package/dist/memory-9-B9WACq.cjs.map +1 -0
  31. package/dist/memory-Cm0eevCS.d.mts +38 -0
  32. package/dist/memory-DiP1a-pp.d.cts +38 -0
  33. package/dist/memory-SdN5vtG9.mjs +104 -0
  34. package/dist/memory-SdN5vtG9.mjs.map +1 -0
  35. package/dist/server/hono.cjs +180 -0
  36. package/dist/server/hono.cjs.map +1 -0
  37. package/dist/server/hono.d.cts +26 -0
  38. package/dist/server/hono.d.mts +26 -0
  39. package/dist/server/hono.mjs +176 -0
  40. package/dist/server/hono.mjs.map +1 -0
  41. package/dist/storage/kysely.cjs +336 -0
  42. package/dist/storage/kysely.cjs.map +1 -0
  43. package/dist/storage/kysely.d.cts +161 -0
  44. package/dist/storage/kysely.d.mts +161 -0
  45. package/dist/storage/kysely.mjs +334 -0
  46. package/dist/storage/kysely.mjs.map +1 -0
  47. package/dist/storage/memory.cjs +3 -0
  48. package/dist/storage/memory.d.cts +3 -0
  49. package/dist/storage/memory.d.mts +3 -0
  50. package/dist/storage/memory.mjs +3 -0
  51. package/dist/types-BGDhFv4R.d.cts +170 -0
  52. package/dist/types-CZbzz8kx.d.mts +170 -0
  53. package/dist/types.cjs +0 -0
  54. package/dist/types.d.cts +2 -0
  55. package/dist/types.d.mts +2 -0
  56. package/dist/types.mjs +0 -0
  57. package/dist/ui-assets-D6-8TAr_.mjs +30 -0
  58. package/dist/ui-assets-D6-8TAr_.mjs.map +1 -0
  59. package/dist/ui-assets-ulevVble.cjs +48 -0
  60. package/dist/ui-assets-ulevVble.cjs.map +1 -0
  61. package/dist/ui-assets.cjs +5 -0
  62. package/dist/ui-assets.d.cts +12 -0
  63. package/dist/ui-assets.d.mts +12 -0
  64. package/dist/ui-assets.mjs +3 -0
  65. package/package.json +83 -0
  66. package/scripts/embed-ui.ts +90 -0
  67. package/src/Telescope.ts +714 -0
  68. package/src/__tests__/Telescope.spec.ts +356 -0
  69. package/src/index.ts +23 -0
  70. package/src/logger/__tests__/console.spec.ts +266 -0
  71. package/src/logger/__tests__/pino.spec.ts +217 -0
  72. package/src/logger/console.ts +230 -0
  73. package/src/logger/pino.ts +191 -0
  74. package/src/server/__tests__/hono.spec.ts +340 -0
  75. package/src/server/hono.ts +247 -0
  76. package/src/storage/__tests__/kysely.spec.ts +715 -0
  77. package/src/storage/__tests__/memory.spec.ts +411 -0
  78. package/src/storage/kysely.ts +572 -0
  79. package/src/storage/memory.ts +168 -0
  80. package/src/types.ts +188 -0
  81. package/src/ui-assets.ts +40 -0
  82. package/ui/index.html +12 -0
  83. package/ui/node_modules/.bin/browserslist +21 -0
  84. package/ui/node_modules/.bin/jiti +21 -0
  85. package/ui/node_modules/.bin/terser +21 -0
  86. package/ui/node_modules/.bin/tsc +21 -0
  87. package/ui/node_modules/.bin/tsserver +21 -0
  88. package/ui/node_modules/.bin/tsx +21 -0
  89. package/ui/node_modules/.bin/vite +21 -0
  90. package/ui/package.json +24 -0
  91. package/ui/src/App.tsx +342 -0
  92. package/ui/src/api.ts +75 -0
  93. package/ui/src/components/ExceptionDetail.tsx +100 -0
  94. package/ui/src/components/LogDetail.tsx +91 -0
  95. package/ui/src/components/RequestDetail.tsx +143 -0
  96. package/ui/src/main.tsx +10 -0
  97. package/ui/src/styles.css +10 -0
  98. package/ui/src/types.ts +63 -0
  99. package/ui/src/vite-env.d.ts +1 -0
  100. package/ui/src/vite-plugin-gkm-config.ts +54 -0
  101. package/ui/tsconfig.json +20 -0
  102. package/ui/tsconfig.tsbuildinfo +14 -0
  103. package/ui/vite.config.ts +13 -0
@@ -0,0 +1,714 @@
1
+ import { nanoid } from 'nanoid';
2
+ import type {
3
+ ExceptionEntry,
4
+ LogEntry,
5
+ NormalizedTelescopeOptions,
6
+ QueryOptions,
7
+ RequestEntry,
8
+ StackFrame,
9
+ TelescopeEvent,
10
+ TelescopeOptions,
11
+ TelescopeStorage,
12
+ } from './types';
13
+
14
+ /**
15
+ * Framework-agnostic Telescope class for debugging and monitoring applications.
16
+ * Use framework-specific adapters (e.g., @geekmidas/telescope/hono) for integration.
17
+ */
18
+ export class Telescope {
19
+ private storage: TelescopeStorage;
20
+ private options: NormalizedTelescopeOptions;
21
+ private wsClients = new Set<WebSocket>();
22
+ private pruneInterval?: ReturnType<typeof setInterval>;
23
+
24
+ constructor(options: TelescopeOptions) {
25
+ this.storage = options.storage;
26
+ this.options = this.normalizeOptions(options);
27
+
28
+ // Set up auto-pruning if configured
29
+ if (this.options.pruneAfterHours) {
30
+ const intervalMs = 60 * 60 * 1000; // 1 hour
31
+ this.pruneInterval = setInterval(() => {
32
+ this.autoPrune().catch(console.error);
33
+ }, intervalMs);
34
+ }
35
+ }
36
+
37
+ // ============================================
38
+ // Public API - Recording
39
+ // ============================================
40
+
41
+ /**
42
+ * Record a request entry
43
+ */
44
+ async recordRequest(
45
+ entry: Omit<RequestEntry, 'id' | 'timestamp'>,
46
+ ): Promise<string> {
47
+ if (!this.options.enabled) return '';
48
+
49
+ const id = nanoid();
50
+ const fullEntry: RequestEntry = {
51
+ ...entry,
52
+ id,
53
+ timestamp: new Date(),
54
+ };
55
+
56
+ await this.storage.saveRequest(fullEntry);
57
+ this.broadcast({
58
+ type: 'request',
59
+ payload: fullEntry,
60
+ timestamp: Date.now(),
61
+ });
62
+ return id;
63
+ }
64
+
65
+ /**
66
+ * Log entry input for batch operations
67
+ */
68
+ private async saveLogEntries(entries: LogEntry[]): Promise<void> {
69
+ if (this.storage.saveLogs) {
70
+ await this.storage.saveLogs(entries);
71
+ } else {
72
+ await Promise.all(entries.map((entry) => this.storage.saveLog(entry)));
73
+ }
74
+
75
+ for (const entry of entries) {
76
+ this.broadcast({ type: 'log', payload: entry, timestamp: Date.now() });
77
+ }
78
+ }
79
+
80
+ /**
81
+ * Record log entries in batch.
82
+ * More efficient than individual calls for database storage.
83
+ *
84
+ * @example
85
+ * await telescope.log([
86
+ * { level: 'info', message: 'Request started' },
87
+ * { level: 'debug', message: 'Processing...', context: { step: 1 } },
88
+ * ]);
89
+ */
90
+ async log(
91
+ entries: Array<{
92
+ level: 'debug' | 'info' | 'warn' | 'error';
93
+ message: string;
94
+ context?: Record<string, unknown>;
95
+ requestId?: string;
96
+ }>,
97
+ ): Promise<void> {
98
+ if (!this.options.enabled || entries.length === 0) return;
99
+
100
+ const timestamp = new Date();
101
+ const logEntries: LogEntry[] = entries.map((e) => ({
102
+ id: nanoid(),
103
+ level: e.level,
104
+ message: e.message,
105
+ context: e.context,
106
+ requestId: e.requestId,
107
+ timestamp,
108
+ }));
109
+
110
+ await this.saveLogEntries(logEntries);
111
+ }
112
+
113
+ /**
114
+ * Log a debug message
115
+ */
116
+ async debug(
117
+ message: string,
118
+ context?: Record<string, unknown>,
119
+ requestId?: string,
120
+ ): Promise<void> {
121
+ if (!this.options.enabled) return;
122
+
123
+ const entry: LogEntry = {
124
+ id: nanoid(),
125
+ level: 'debug',
126
+ message,
127
+ context,
128
+ requestId,
129
+ timestamp: new Date(),
130
+ };
131
+
132
+ await this.storage.saveLog(entry);
133
+ this.broadcast({ type: 'log', payload: entry, timestamp: Date.now() });
134
+ }
135
+
136
+ /**
137
+ * Log an info message
138
+ */
139
+ async info(
140
+ message: string,
141
+ context?: Record<string, unknown>,
142
+ requestId?: string,
143
+ ): Promise<void> {
144
+ if (!this.options.enabled) return;
145
+
146
+ const entry: LogEntry = {
147
+ id: nanoid(),
148
+ level: 'info',
149
+ message,
150
+ context,
151
+ requestId,
152
+ timestamp: new Date(),
153
+ };
154
+
155
+ await this.storage.saveLog(entry);
156
+ this.broadcast({ type: 'log', payload: entry, timestamp: Date.now() });
157
+ }
158
+
159
+ /**
160
+ * Log a warning message
161
+ */
162
+ async warn(
163
+ message: string,
164
+ context?: Record<string, unknown>,
165
+ requestId?: string,
166
+ ): Promise<void> {
167
+ if (!this.options.enabled) return;
168
+
169
+ const entry: LogEntry = {
170
+ id: nanoid(),
171
+ level: 'warn',
172
+ message,
173
+ context,
174
+ requestId,
175
+ timestamp: new Date(),
176
+ };
177
+
178
+ await this.storage.saveLog(entry);
179
+ this.broadcast({ type: 'log', payload: entry, timestamp: Date.now() });
180
+ }
181
+
182
+ /**
183
+ * Log an error message
184
+ */
185
+ async error(
186
+ message: string,
187
+ context?: Record<string, unknown>,
188
+ requestId?: string,
189
+ ): Promise<void> {
190
+ if (!this.options.enabled) return;
191
+
192
+ const entry: LogEntry = {
193
+ id: nanoid(),
194
+ level: 'error',
195
+ message,
196
+ context,
197
+ requestId,
198
+ timestamp: new Date(),
199
+ };
200
+
201
+ await this.storage.saveLog(entry);
202
+ this.broadcast({ type: 'log', payload: entry, timestamp: Date.now() });
203
+ }
204
+
205
+ /**
206
+ * Record an exception
207
+ */
208
+ async exception(error: Error, requestId?: string): Promise<void> {
209
+ if (!this.options.enabled) return;
210
+
211
+ const stack = this.parseStack(error.stack || '');
212
+
213
+ const entry: ExceptionEntry = {
214
+ id: nanoid(),
215
+ name: error.name,
216
+ message: error.message,
217
+ stack,
218
+ requestId,
219
+ timestamp: new Date(),
220
+ handled: false,
221
+ };
222
+
223
+ await this.storage.saveException(entry);
224
+ this.broadcast({
225
+ type: 'exception',
226
+ payload: entry,
227
+ timestamp: Date.now(),
228
+ });
229
+ }
230
+
231
+ // ============================================
232
+ // Public API - Data Access
233
+ // ============================================
234
+
235
+ /**
236
+ * Get requests from storage
237
+ */
238
+ async getRequests(options?: QueryOptions): Promise<RequestEntry[]> {
239
+ return this.storage.getRequests(options);
240
+ }
241
+
242
+ /**
243
+ * Get a single request by ID
244
+ */
245
+ async getRequest(id: string): Promise<RequestEntry | null> {
246
+ return this.storage.getRequest(id);
247
+ }
248
+
249
+ /**
250
+ * Get exceptions from storage
251
+ */
252
+ async getExceptions(options?: QueryOptions): Promise<ExceptionEntry[]> {
253
+ return this.storage.getExceptions(options);
254
+ }
255
+
256
+ /**
257
+ * Get a single exception by ID
258
+ */
259
+ async getException(id: string): Promise<ExceptionEntry | null> {
260
+ return this.storage.getException(id);
261
+ }
262
+
263
+ /**
264
+ * Get logs from storage
265
+ */
266
+ async getLogs(options?: QueryOptions): Promise<LogEntry[]> {
267
+ return this.storage.getLogs(options);
268
+ }
269
+
270
+ /**
271
+ * Get storage statistics
272
+ */
273
+ async getStats() {
274
+ return this.storage.getStats();
275
+ }
276
+
277
+ // ============================================
278
+ // Public API - WebSocket
279
+ // ============================================
280
+
281
+ /**
282
+ * Add a WebSocket client for real-time updates
283
+ */
284
+ addWsClient(ws: WebSocket): void {
285
+ this.wsClients.add(ws);
286
+ this.broadcast({
287
+ type: 'connected',
288
+ payload: { clientCount: this.wsClients.size },
289
+ timestamp: Date.now(),
290
+ });
291
+ }
292
+
293
+ /**
294
+ * Remove a WebSocket client
295
+ */
296
+ removeWsClient(ws: WebSocket): void {
297
+ this.wsClients.delete(ws);
298
+ }
299
+
300
+ /**
301
+ * Broadcast an event to all connected WebSocket clients
302
+ */
303
+ broadcast(event: TelescopeEvent): void {
304
+ const data = JSON.stringify(event);
305
+ for (const client of this.wsClients) {
306
+ try {
307
+ client.send(data);
308
+ } catch {
309
+ this.wsClients.delete(client);
310
+ }
311
+ }
312
+ }
313
+
314
+ // ============================================
315
+ // Public API - Lifecycle
316
+ // ============================================
317
+
318
+ /**
319
+ * Manually prune old entries
320
+ */
321
+ async prune(olderThan: Date): Promise<number> {
322
+ return this.storage.prune(olderThan);
323
+ }
324
+
325
+ /**
326
+ * Clean up resources
327
+ */
328
+ destroy(): void {
329
+ if (this.pruneInterval) {
330
+ clearInterval(this.pruneInterval);
331
+ }
332
+ this.wsClients.clear();
333
+ }
334
+
335
+ // ============================================
336
+ // Public API - Configuration
337
+ // ============================================
338
+
339
+ /**
340
+ * Get the telescope path
341
+ */
342
+ get path(): string {
343
+ return this.options.path;
344
+ }
345
+
346
+ /**
347
+ * Check if telescope is enabled
348
+ */
349
+ get enabled(): boolean {
350
+ return this.options.enabled;
351
+ }
352
+
353
+ /**
354
+ * Check if body recording is enabled
355
+ */
356
+ get recordBody(): boolean {
357
+ return this.options.recordBody;
358
+ }
359
+
360
+ /**
361
+ * Get max body size
362
+ */
363
+ get maxBodySize(): number {
364
+ return this.options.maxBodySize;
365
+ }
366
+
367
+ /**
368
+ * Check if a path should be ignored
369
+ */
370
+ shouldIgnore(path: string): boolean {
371
+ // Always ignore telescope's own routes
372
+ if (path.startsWith(this.options.path)) {
373
+ return true;
374
+ }
375
+
376
+ return this.options.ignorePatterns.some((pattern) => {
377
+ if (pattern.includes('*')) {
378
+ const regex = new RegExp(
379
+ '^' + pattern.replace(/\*/g, '.*').replace(/\?/g, '.') + '$',
380
+ );
381
+ return regex.test(path);
382
+ }
383
+ return path.startsWith(pattern);
384
+ });
385
+ }
386
+
387
+ // ============================================
388
+ // Public API - Dashboard
389
+ // ============================================
390
+
391
+ /**
392
+ * Get the dashboard HTML
393
+ */
394
+ getDashboardHtml(): string {
395
+ return `<!DOCTYPE html>
396
+ <html lang="en">
397
+ <head>
398
+ <meta charset="UTF-8">
399
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
400
+ <title>Telescope</title>
401
+ <style>
402
+ * { box-sizing: border-box; margin: 0; padding: 0; }
403
+ body {
404
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
405
+ background: #0f0f23;
406
+ color: #e0e0e0;
407
+ min-height: 100vh;
408
+ }
409
+ .container {
410
+ max-width: 1400px;
411
+ margin: 0 auto;
412
+ padding: 24px;
413
+ }
414
+ header {
415
+ display: flex;
416
+ justify-content: space-between;
417
+ align-items: center;
418
+ margin-bottom: 24px;
419
+ padding-bottom: 16px;
420
+ border-bottom: 1px solid #333;
421
+ }
422
+ h1 {
423
+ font-size: 24px;
424
+ font-weight: 600;
425
+ display: flex;
426
+ align-items: center;
427
+ gap: 8px;
428
+ }
429
+ h1::before {
430
+ content: '';
431
+ width: 8px;
432
+ height: 8px;
433
+ background: #10b981;
434
+ border-radius: 50%;
435
+ }
436
+ .stats {
437
+ display: flex;
438
+ gap: 24px;
439
+ font-size: 14px;
440
+ color: #888;
441
+ }
442
+ .stat-value {
443
+ color: #fff;
444
+ font-weight: 500;
445
+ }
446
+ nav {
447
+ display: flex;
448
+ gap: 8px;
449
+ margin-bottom: 24px;
450
+ }
451
+ nav a {
452
+ padding: 8px 16px;
453
+ background: #1a1a3e;
454
+ border-radius: 6px;
455
+ color: #e0e0e0;
456
+ text-decoration: none;
457
+ font-size: 14px;
458
+ transition: background 0.2s;
459
+ }
460
+ nav a:hover, nav a.active { background: #2a2a5e; }
461
+ .panel {
462
+ background: #1a1a3e;
463
+ border-radius: 8px;
464
+ overflow: hidden;
465
+ }
466
+ .entry {
467
+ display: grid;
468
+ grid-template-columns: 70px 1fr 100px 80px;
469
+ gap: 16px;
470
+ padding: 12px 16px;
471
+ border-bottom: 1px solid #252550;
472
+ align-items: center;
473
+ cursor: pointer;
474
+ transition: background 0.2s;
475
+ }
476
+ .entry:hover { background: #252550; }
477
+ .method {
478
+ font-weight: 600;
479
+ font-size: 12px;
480
+ padding: 4px 8px;
481
+ border-radius: 4px;
482
+ text-align: center;
483
+ }
484
+ .GET { background: #10b981; color: #fff; }
485
+ .POST { background: #3b82f6; color: #fff; }
486
+ .PUT { background: #f59e0b; color: #fff; }
487
+ .PATCH { background: #8b5cf6; color: #fff; }
488
+ .DELETE { background: #ef4444; color: #fff; }
489
+ .path { font-family: monospace; font-size: 13px; }
490
+ .status { font-family: monospace; }
491
+ .status-2xx { color: #10b981; }
492
+ .status-3xx { color: #3b82f6; }
493
+ .status-4xx { color: #f59e0b; }
494
+ .status-5xx { color: #ef4444; }
495
+ .duration { color: #888; font-size: 13px; }
496
+ .empty {
497
+ padding: 48px;
498
+ text-align: center;
499
+ color: #666;
500
+ }
501
+ #entries { max-height: calc(100vh - 200px); overflow-y: auto; }
502
+ </style>
503
+ </head>
504
+ <body>
505
+ <div class="container">
506
+ <header>
507
+ <h1>Telescope</h1>
508
+ <div class="stats">
509
+ <span>Requests: <span class="stat-value" id="request-count">-</span></span>
510
+ <span>Exceptions: <span class="stat-value" id="exception-count">-</span></span>
511
+ <span>Logs: <span class="stat-value" id="log-count">-</span></span>
512
+ </div>
513
+ </header>
514
+
515
+ <nav>
516
+ <a href="#" class="active" data-view="requests">Requests</a>
517
+ <a href="#" data-view="exceptions">Exceptions</a>
518
+ <a href="#" data-view="logs">Logs</a>
519
+ </nav>
520
+
521
+ <div class="panel">
522
+ <div id="entries"></div>
523
+ </div>
524
+ </div>
525
+
526
+ <script>
527
+ let currentView = 'requests';
528
+ const basePath = window.location.pathname.replace(/\\/$/, '');
529
+
530
+ async function fetchStats() {
531
+ try {
532
+ const res = await fetch(basePath + '/api/stats');
533
+ const stats = await res.json();
534
+ document.getElementById('request-count').textContent = stats.requests;
535
+ document.getElementById('exception-count').textContent = stats.exceptions;
536
+ document.getElementById('log-count').textContent = stats.logs;
537
+ } catch (e) {
538
+ console.error('Failed to fetch stats:', e);
539
+ }
540
+ }
541
+
542
+ async function fetchData(type) {
543
+ try {
544
+ const res = await fetch(basePath + '/api/' + type);
545
+ return await res.json();
546
+ } catch (e) {
547
+ console.error('Failed to fetch ' + type + ':', e);
548
+ return [];
549
+ }
550
+ }
551
+
552
+ function renderRequests(requests) {
553
+ const container = document.getElementById('entries');
554
+ if (requests.length === 0) {
555
+ container.innerHTML = '<div class="empty">No requests recorded yet</div>';
556
+ return;
557
+ }
558
+ container.innerHTML = requests.map(r => \`
559
+ <div class="entry">
560
+ <span class="method \${r.method}">\${r.method}</span>
561
+ <span class="path">\${r.path}</span>
562
+ <span class="status status-\${Math.floor(r.status/100)}xx">\${r.status}</span>
563
+ <span class="duration">\${r.duration.toFixed(1)}ms</span>
564
+ </div>
565
+ \`).join('');
566
+ }
567
+
568
+ function renderExceptions(exceptions) {
569
+ const container = document.getElementById('entries');
570
+ if (exceptions.length === 0) {
571
+ container.innerHTML = '<div class="empty">No exceptions recorded yet</div>';
572
+ return;
573
+ }
574
+ container.innerHTML = exceptions.map(e => \`
575
+ <div class="entry" style="grid-template-columns: 1fr 200px;">
576
+ <div>
577
+ <div style="color: #ef4444; font-weight: 500;">\${e.name}</div>
578
+ <div style="font-size: 13px; color: #888; margin-top: 4px;">\${e.message}</div>
579
+ </div>
580
+ <span class="duration">\${new Date(e.timestamp).toLocaleTimeString()}</span>
581
+ </div>
582
+ \`).join('');
583
+ }
584
+
585
+ function renderLogs(logs) {
586
+ const container = document.getElementById('entries');
587
+ if (logs.length === 0) {
588
+ container.innerHTML = '<div class="empty">No logs recorded yet</div>';
589
+ return;
590
+ }
591
+ const levelColors = { debug: '#888', info: '#3b82f6', warn: '#f59e0b', error: '#ef4444' };
592
+ container.innerHTML = logs.map(l => \`
593
+ <div class="entry" style="grid-template-columns: 60px 1fr 100px;">
594
+ <span style="color: \${levelColors[l.level]}; font-size: 12px; text-transform: uppercase;">\${l.level}</span>
595
+ <span style="font-family: monospace; font-size: 13px;">\${l.message}</span>
596
+ <span class="duration">\${new Date(l.timestamp).toLocaleTimeString()}</span>
597
+ </div>
598
+ \`).join('');
599
+ }
600
+
601
+ async function loadView(view) {
602
+ currentView = view;
603
+ document.querySelectorAll('nav a').forEach(a => {
604
+ a.classList.toggle('active', a.dataset.view === view);
605
+ });
606
+
607
+ const data = await fetchData(view);
608
+ if (view === 'requests') renderRequests(data);
609
+ else if (view === 'exceptions') renderExceptions(data);
610
+ else if (view === 'logs') renderLogs(data);
611
+ }
612
+
613
+ // Navigation
614
+ document.querySelectorAll('nav a').forEach(a => {
615
+ a.addEventListener('click', (e) => {
616
+ e.preventDefault();
617
+ loadView(a.dataset.view);
618
+ });
619
+ });
620
+
621
+ // WebSocket for real-time updates
622
+ function connectWs() {
623
+ const protocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
624
+ const ws = new WebSocket(protocol + '//' + location.host + basePath + '/ws');
625
+
626
+ ws.onmessage = (event) => {
627
+ const msg = JSON.parse(event.data);
628
+ if (msg.type === currentView.slice(0, -1) ||
629
+ (msg.type === 'request' && currentView === 'requests') ||
630
+ (msg.type === 'exception' && currentView === 'exceptions') ||
631
+ (msg.type === 'log' && currentView === 'logs')) {
632
+ loadView(currentView);
633
+ }
634
+ fetchStats();
635
+ };
636
+
637
+ ws.onclose = () => {
638
+ setTimeout(connectWs, 1000);
639
+ };
640
+ }
641
+
642
+ // Initial load
643
+ fetchStats();
644
+ loadView('requests');
645
+ connectWs();
646
+ </script>
647
+ </body>
648
+ </html>`;
649
+ }
650
+
651
+ // ============================================
652
+ // Private Methods
653
+ // ============================================
654
+
655
+ private normalizeOptions(
656
+ options: TelescopeOptions,
657
+ ): NormalizedTelescopeOptions {
658
+ return {
659
+ storage: options.storage,
660
+ enabled: options.enabled ?? true,
661
+ path: options.path ?? '/__telescope',
662
+ recordBody: options.recordBody ?? true,
663
+ maxBodySize: options.maxBodySize ?? 64 * 1024, // 64KB
664
+ ignorePatterns: options.ignorePatterns ?? [],
665
+ pruneAfterHours: options.pruneAfterHours,
666
+ };
667
+ }
668
+
669
+ private parseStack(stack: string): StackFrame[] {
670
+ const lines = stack.split('\n').slice(1);
671
+ const frames: StackFrame[] = [];
672
+
673
+ for (const line of lines) {
674
+ // Match: " at functionName (file:line:column)"
675
+ // or: " at file:line:column"
676
+ const match =
677
+ line.match(/at\s+(.+?)\s+\((.+):(\d+):(\d+)\)/) ||
678
+ line.match(/at\s+(.+):(\d+):(\d+)/);
679
+
680
+ if (match) {
681
+ if (match.length === 5) {
682
+ // Has function name
683
+ frames.push({
684
+ function: match[1],
685
+ file: match[2],
686
+ line: parseInt(match[3], 10),
687
+ column: parseInt(match[4], 10),
688
+ isApp: !match[2].includes('node_modules'),
689
+ });
690
+ } else if (match.length === 4) {
691
+ // No function name
692
+ frames.push({
693
+ function: '<anonymous>',
694
+ file: match[1],
695
+ line: parseInt(match[2], 10),
696
+ column: parseInt(match[3], 10),
697
+ isApp: !match[1].includes('node_modules'),
698
+ });
699
+ }
700
+ }
701
+ }
702
+
703
+ return frames;
704
+ }
705
+
706
+ private async autoPrune(): Promise<void> {
707
+ if (!this.options.pruneAfterHours) return;
708
+
709
+ const olderThan = new Date(
710
+ Date.now() - this.options.pruneAfterHours * 60 * 60 * 1000,
711
+ );
712
+ await this.storage.prune(olderThan);
713
+ }
714
+ }