fix(process): honor cancellation at bounded file read completion (#8908)

This commit is contained in:
Afloat authored and GitHub committed 2026-11-06 10:11:56 +08:00
1 parent b28b1e0ae2
commit b153ba8ddd
2 files changed
+46 -1

No files matched your search

+3
View File
@@ -41,6 +41,7 @@ export const readBoundedFileBytes = async (
expectedSize > maxBytes)
)
throw new RangeError("Expected file size must fit the byte limit");
assertActive();
if (expectedSize === undefined) {
const chunks: Buffer[] = [];
const buffer = Buffer.allocUnsafe(Math.min(64 * 1_024, maxBytes + 1));
@@ -52,6 +53,7 @@ export const readBoundedFileBytes = async (
0,
Math.min(buffer.length, maxBytes - total + 1),
);
assertActive();
if (bytesRead === 0) return Buffer.concat(chunks, total);
total += bytesRead;
if (total > maxBytes) return undefined;
@@ -64,6 +66,7 @@ export const readBoundedFileBytes = async (
for (;;) {
assertActive();
const { bytesRead } = await read(buffer, total, expectedSize - total);
assertActive();
if (bytesRead === 0) return buffer.subarray(0, total);
total += bytesRead;
if (total > maxBytes) return undefined;