(handle: FileHandle)
| 121 | * chunks + concat. Reads directly into the right offset; no intermediate copies. |
| 122 | */ |
| 123 | export async function readCapped(handle: FileHandle): Promise<string | null> { |
| 124 | let buf = Buffer.allocUnsafe(CHUNK_SIZE) |
| 125 | let total = 0 |
| 126 | for (;;) { |
| 127 | if (total === buf.length) { |
| 128 | const grown = Buffer.allocUnsafe( |
| 129 | Math.min(buf.length * 2, MAX_SCAN_BYTES + CHUNK_SIZE), |
| 130 | ) |
| 131 | buf.copy(grown, 0, 0, total) |
| 132 | buf = grown |
| 133 | } |
| 134 | const { bytesRead } = await handle.read( |
| 135 | buf, |
| 136 | total, |
| 137 | buf.length - total, |
| 138 | total, |
| 139 | ) |
| 140 | if (bytesRead === 0) break |
| 141 | total += bytesRead |
| 142 | if (total > MAX_SCAN_BYTES) return null |
| 143 | } |
| 144 | return normalizeCRLF(buf, total) |
| 145 | } |
| 146 | |
| 147 | /** buf.indexOf bounded to [0, end) without allocating a view. */ |
| 148 | function indexOfWithin(buf: Buffer, needle: Buffer, end: number): number { |
no test coverage detected