feat(core): support AbortSignal in readFile (#10943)

This commit is contained in:
Benjamin Gruenbaum 2021-06-22 18:45:26 +03:00 committed by GitHub
parent 9c0b41e24b
commit 20b0a5125a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 9 deletions

View file

@ -110,9 +110,12 @@
const READ_PER_ITER = 32 * 1024;
async function readAll(r) {
return await readAllInner(r);
}
async function readAllInner(r, options) {
const buffers = [];
while (true) {
const signal = options?.signal ?? null;
while (!signal?.aborted) {
const buf = new Uint8Array(READ_PER_ITER);
const read = await r.read(buf);
if (typeof read == "number") {
@ -121,6 +124,9 @@
break;
}
}
if (signal?.aborted) {
throw new DOMException("The read operation was aborted.", "AbortError");
}
let totalLen = 0;
for (const buf of buffers) {
@ -177,6 +183,7 @@
write,
writeSync,
readAll,
readAllInner,
readAllSync,
};
})(this);