| 578 | }, |
| 579 | |
| 580 | async readFileBytes(fsPath: string, maxBytes?: number) { |
| 581 | if (maxBytes === undefined) { |
| 582 | return readFilePromise(fsPath) |
| 583 | } |
| 584 | const handle = await open(fsPath, 'r') |
| 585 | try { |
| 586 | const { size } = await handle.stat() |
| 587 | const readSize = Math.min(size, maxBytes) |
| 588 | const buffer = Buffer.allocUnsafe(readSize) |
| 589 | let offset = 0 |
| 590 | while (offset < readSize) { |
| 591 | const { bytesRead } = await handle.read( |
| 592 | buffer, |
| 593 | offset, |
| 594 | readSize - offset, |
| 595 | offset, |
| 596 | ) |
| 597 | if (bytesRead === 0) break |
| 598 | offset += bytesRead |
| 599 | } |
| 600 | return offset < readSize ? buffer.subarray(0, offset) : buffer |
| 601 | } finally { |
| 602 | await handle.close() |
| 603 | } |
| 604 | }, |
| 605 | } |
| 606 | |
| 607 | // The currently active filesystem implementation |