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