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