MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / readBoundedSourceSync

Function readBoundedSourceSync

src/file-limits.ts:204–231  ·  view source on GitHub ↗
(file: string)

Source from the content-addressed store, hash-verified

202
203/** Synchronous {@link readBoundedSource}. */
204export function readBoundedSourceSync(file: string): BoundedSource {
205 const initial = fs.statSync(file);
206 assertRegularFile(initial);
207 if (initial.size > MAX_SOURCE_FILE_SIZE_BYTES) return { stats: initial, bytes: null };
208 const fd = fs.openSync(file, 'r');
209 try {
210 let stats = fs.fstatSync(fd);
211 assertRegularFile(stats);
212 if (stats.size > MAX_SOURCE_FILE_SIZE_BYTES) return { stats, bytes: null };
213 const chunks: Buffer[] = [];
214 let size = 0;
215 while (size <= MAX_SOURCE_FILE_SIZE_BYTES) {
216 const chunk = Buffer.allocUnsafe(Math.min(READ_CHUNK_BYTES, MAX_SOURCE_FILE_SIZE_BYTES + 1 - size));
217 const bytesRead = fs.readSync(fd, chunk, 0, chunk.length, size);
218 if (!bytesRead) break;
219 chunks.push(chunk.subarray(0, bytesRead));
220 size += bytesRead;
221 }
222 stats = fs.fstatSync(fd);
223 if (size > MAX_SOURCE_FILE_SIZE_BYTES || stats.size > MAX_SOURCE_FILE_SIZE_BYTES) {
224 stats.size = Math.max(size, stats.size);
225 return { stats, bytes: null };
226 }
227 return { stats, bytes: Buffer.concat(chunks, size) };
228 } finally {
229 fs.closeSync(fd);
230 }
231}

Callers 4

readSourceOrStampFunction · 0.90
buildDetectionContextMethod · 0.90
manifestNameFunction · 0.90

Calls 3

assertRegularFileFunction · 0.85
pushMethod · 0.80
openSyncMethod · 0.45

Tested by

no test coverage detected