(filePath: string, maxBytes: number)
| 170 | } |
| 171 | |
| 172 | async function readFilePrefix(filePath: string, maxBytes: number): Promise<string> { |
| 173 | const handle = await fs.open(filePath, 'r'); |
| 174 | |
| 175 | try { |
| 176 | const buffer = Buffer.alloc(maxBytes); |
| 177 | const { bytesRead } = await handle.read(buffer, 0, maxBytes, 0); |
| 178 | // Decode only the bounded prefix and drop any incomplete trailing code point. |
| 179 | const decoder = new StringDecoder('utf8'); |
| 180 | return decoder.write(buffer.subarray(0, bytesRead)); |
| 181 | } finally { |
| 182 | await handle.close(); |
| 183 | } |
| 184 | } |