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

Function readBoundedSourceSync

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

Source from the content-addressed store, hash-verified

99
100/** Synchronous {@link readBoundedSource}. */
101export function readBoundedSourceSync(file: string): BoundedSource {
102 const initial = fs.statSync(file);
103 assertRegularFile(initial);
104 if (initial.size > MAX_SOURCE_FILE_SIZE_BYTES) return { stats: initial, bytes: null };
105 const fd = fs.openSync(file, 'r');
106 try {
107 let stats = fs.fstatSync(fd);
108 assertRegularFile(stats);
109 if (stats.size > MAX_SOURCE_FILE_SIZE_BYTES) return { stats, bytes: null };
110 const chunks: Buffer[] = [];
111 let size = 0;
112 while (size <= MAX_SOURCE_FILE_SIZE_BYTES) {
113 const chunk = Buffer.allocUnsafe(Math.min(READ_CHUNK_BYTES, MAX_SOURCE_FILE_SIZE_BYTES + 1 - size));
114 const bytesRead = fs.readSync(fd, chunk, 0, chunk.length, size);
115 if (!bytesRead) break;
116 chunks.push(chunk.subarray(0, bytesRead));
117 size += bytesRead;
118 }
119 stats = fs.fstatSync(fd);
120 if (size > MAX_SOURCE_FILE_SIZE_BYTES || stats.size > MAX_SOURCE_FILE_SIZE_BYTES) {
121 stats.size = Math.max(size, stats.size);
122 return { stats, bytes: null };
123 }
124 return { stats, bytes: Buffer.concat(chunks, size) };
125 } finally {
126 fs.closeSync(fd);
127 }
128}

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