MCPcopy Create free account
hub / github.com/cortexkit/magic-context / parseRpcPortFile

Function parseRpcPortFile

packages/plugin/src/shared/rpc-utils.ts:53–78  ·  view source on GitHub ↗
(content: string, fallbackPid = 0)

Source from the content-addressed store, hash-verified

51}
52
53export function parseRpcPortFile(content: string, fallbackPid = 0): RpcPortFileRecord | null {
54 const trimmed = content.trim();
55 if (!trimmed) return null;
56
57 if (trimmed.startsWith("{")) {
58 try {
59 const parsed = JSON.parse(trimmed) as Partial<RpcPortFileRecord>;
60 const port = Number(parsed.port);
61 const pid = Number(parsed.pid);
62 const startedAt = Number(parsed.started_at);
63 if (!isValidPort(port) || !Number.isInteger(pid) || pid <= 0) return null;
64 return {
65 port,
66 pid,
67 started_at: Number.isFinite(startedAt) ? startedAt : 0,
68 token: typeof parsed.token === "string" ? parsed.token : undefined,
69 };
70 } catch {
71 return null;
72 }
73 }
74
75 const port = Number.parseInt(trimmed, 10);
76 if (!isValidPort(port)) return null;
77 return { port, pid: fallbackPid, started_at: 0 };
78}
79
80function isValidPort(port: number): boolean {
81 return Number.isInteger(port) && port > 0 && port <= 65535;

Callers 3

readPortFileMethod · 0.90
rpc-client.test.tsFile · 0.90

Calls 1

isValidPortFunction · 0.85

Tested by

no test coverage detected