MCPcopy Create free account
hub / github.com/pingdotgg/t3code / parseLsofOutput

Function parseLsofOutput

apps/server/src/preview/PortScanner.ts:82–122  ·  view source on GitHub ↗
(
  raw: string,
  terminalByProcessId: ReadonlyMap<number, TerminalProcessOwner> = new Map(),
)

Source from the content-addressed store, hash-verified

80}): string => `${owner.threadId}\u0000${owner.terminalId}`;
81
82const 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
124const parsePortFromLsofName = (name: string): number | null => {
125 // Examples: "*:5173", "127.0.0.1:5173", "[::1]:5173", "localhost:5173",

Callers 1

PortScanner.tsFile · 0.85

Calls 3

parsePortFromLsofNameFunction · 0.85
setMethod · 0.80
getMethod · 0.80

Tested by

no test coverage detected