(connections: ConnectionInfo[])
| 106 | } |
| 107 | |
| 108 | function formatTable(connections: ConnectionInfo[]): string { |
| 109 | if (connections.length === 0) { |
| 110 | return 'No active clients.\n'; |
| 111 | } |
| 112 | |
| 113 | const header = ['ID', 'CONNECTED', 'REMOTE', 'USER_AGENT', 'SESSIONS', 'HELLO']; |
| 114 | const rows = connections.map((c) => [ |
| 115 | c.id, |
| 116 | formatAge(c.connected_at), |
| 117 | c.remote_address ?? '-', |
| 118 | truncate(c.user_agent ?? '-', USER_AGENT_MAX_WIDTH), |
| 119 | String(c.subscriptions.length), |
| 120 | c.has_client_hello ? 'yes' : 'no', |
| 121 | ]); |
| 122 | |
| 123 | const widths = header.map((h, i) => Math.max(h.length, ...rows.map((r) => r[i]!.length))); |
| 124 | const formatRow = (cells: string[]): string => |
| 125 | cells.map((cell, i) => cell + ' '.repeat(Math.max(0, widths[i]! - cell.length))).join(' '); |
| 126 | |
| 127 | const lines = [chalk.bold(formatRow(header)), ...rows.map(formatRow)]; |
| 128 | return `${lines.join('\n')}\n`; |
| 129 | } |
| 130 | |
| 131 | function formatAge(iso: string): string { |
| 132 | const ms = Date.now() - Date.parse(iso); |
no test coverage detected