(file: string)
| 154 | |
| 155 | /** Synchronous {@link readBoundedSource}. */ |
| 156 | export 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 | } |
no test coverage detected