MCPcopy Create free account
hub / github.com/agegr/pi-web / readSessionHeader

Function readSessionHeader

lib/session-reader.ts:156–187  ·  view source on GitHub ↗
(filePath: string)

Source from the content-addressed store, hash-verified

154}
155
156export function readSessionHeader(filePath: string): SessionHeader | null {
157 const fd = openSync(filePath, "r");
158 try {
159 const chunks: Buffer[] = [];
160 const maxHeaderBytes = 64 * 1024;
161 let position = 0;
162 let foundNewline = false;
163
164 while (position < maxHeaderBytes && !foundNewline) {
165 const buffer = Buffer.allocUnsafe(Math.min(4096, maxHeaderBytes - position));
166 const bytesRead = readSync(fd, buffer, 0, buffer.length, position);
167 if (bytesRead === 0) break;
168 const data = buffer.subarray(0, bytesRead);
169 const newlineIndex = data.indexOf(0x0a);
170 chunks.push(newlineIndex === -1 ? data : data.subarray(0, newlineIndex));
171 position += bytesRead;
172 foundNewline = newlineIndex !== -1;
173 }
174
175 if (!foundNewline && position >= maxHeaderBytes) return null;
176 const firstLine = Buffer.concat(chunks).toString("utf8").trimEnd();
177 if (!firstLine) return null;
178 try {
179 const header = JSON.parse(firstLine) as SessionHeader;
180 return header.type === "session" ? header : null;
181 } catch {
182 return null;
183 }
184 } finally {
185 closeSync(fd);
186 }
187}
188
189export function getSessionEntries(filePath: string): SessionEntry[] {
190 const entries = SessionManager.open(filePath).getEntries();

Callers 2

DELETEFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected