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

Function readBoundedSourceSync

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

Source from the content-addressed store, hash-verified

154
155/** Synchronous {@link readBoundedSource}. */
156export function readBoundedSourceSync(file: string): BoundedSource {
157 const initial = fs.statSync(file);
158 assertRegularFile(initial);
159 if (initial.size > MAX_SOURCE_FILE_SIZE_BYTES) return { stats: initial, bytes: null };
160 const fd = fs.openSync(file, 'r');
161 try {
162 let stats = fs.fstatSync(fd);
163 assertRegularFile(stats);
164 if (stats.size > MAX_SOURCE_FILE_SIZE_BYTES) return { stats, bytes: null };
165 const chunks: Buffer[] = [];
166 let size = 0;
167 while (size <= MAX_SOURCE_FILE_SIZE_BYTES) {
168 const chunk = Buffer.allocUnsafe(Math.min(READ_CHUNK_BYTES, MAX_SOURCE_FILE_SIZE_BYTES + 1 - size));
169 const bytesRead = fs.readSync(fd, chunk, 0, chunk.length, size);
170 if (!bytesRead) break;
171 chunks.push(chunk.subarray(0, bytesRead));
172 size += bytesRead;
173 }
174 stats = fs.fstatSync(fd);
175 if (size > MAX_SOURCE_FILE_SIZE_BYTES || stats.size > MAX_SOURCE_FILE_SIZE_BYTES) {
176 stats.size = Math.max(size, stats.size);
177 return { stats, bytes: null };
178 }
179 return { stats, bytes: Buffer.concat(chunks, size) };
180 } finally {
181 fs.closeSync(fd);
182 }
183}

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