( raw: string, terminalByProcessId: ReadonlyMap<number, TerminalProcessOwner> = new Map(), )
| 80 | }): string => `${owner.threadId}\u0000${owner.terminalId}`; |
| 81 | |
| 82 | const parseLsofOutput = ( |
| 83 | raw: string, |
| 84 | terminalByProcessId: ReadonlyMap<number, TerminalProcessOwner> = new Map(), |
| 85 | ): ReadonlyArray<DiscoveredLocalServer> => { |
| 86 | const seen = new Map<string, DiscoveredLocalServer>(); |
| 87 | let pid: number | null = null; |
| 88 | let processName: string | null = null; |
| 89 | |
| 90 | for (const line of raw.split("\n")) { |
| 91 | if (line.length === 0) continue; |
| 92 | const tag = line.charAt(0); |
| 93 | const value = line.slice(1); |
| 94 | if (tag === "p") { |
| 95 | const parsed = Number.parseInt(value, 10); |
| 96 | pid = Number.isFinite(parsed) && parsed > 0 ? parsed : null; |
| 97 | processName = null; |
| 98 | continue; |
| 99 | } |
| 100 | if (tag === "c") { |
| 101 | processName = value.trim() || null; |
| 102 | continue; |
| 103 | } |
| 104 | if (tag === "n") { |
| 105 | const portMatch = parsePortFromLsofName(value); |
| 106 | if (portMatch == null) continue; |
| 107 | const url = `http://localhost:${portMatch}`; |
| 108 | const key = `localhost:${portMatch}`; |
| 109 | if (seen.has(key)) continue; |
| 110 | seen.set(key, { |
| 111 | host: "localhost", |
| 112 | port: portMatch, |
| 113 | url, |
| 114 | processName, |
| 115 | pid, |
| 116 | terminal: pid === null ? null : (terminalByProcessId.get(pid) ?? null), |
| 117 | }); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return Array.from(seen.values()).toSorted((a, b) => a.port - b.port); |
| 122 | }; |
| 123 | |
| 124 | const parsePortFromLsofName = (name: string): number | null => { |
| 125 | // Examples: "*:5173", "127.0.0.1:5173", "[::1]:5173", "localhost:5173", |
no test coverage detected