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