Add workround for deno can not handle error in streams

This commit is contained in:
2023-07-22 21:52:17 +08:00
parent 26a0d045e0
commit ea41cb0081
2 changed files with 23 additions and 11 deletions

View File

@@ -343,6 +343,9 @@ export async function download_task(
signal: force_abort,
preventClose: true,
});
if (pr.error) {
throw (pr.error);
}
} finally {
try {
f.close();

View File

@@ -6,29 +6,38 @@ type EventMap = {
export class ProgressReadable extends EventTarget {
readable: ReadableStream<Uint8Array>;
readed: number;
error?: unknown;
constructor(readable: ReadableStream<Uint8Array>) {
super();
this.readed = 0;
const reader = readable.getReader();
this.readable = new ReadableStream({
pull: async (c) => {
pull: (c) => {
if (c.byobRequest) {
throw Error("Unimplemented.");
} else {
const v = await reader.read();
if (v.done) {
this.dispatchEvent("finished", this.readed);
reader.read().then((v) => {
if (v.done) {
this.dispatchEvent("finished", this.readed);
c.close();
return;
} else {
this.readed += v.value.byteLength;
this.dispatchEvent("progress", this.readed);
c.enqueue(v.value);
}
}).catch((e) => {
c.close();
return;
} else {
this.readed += v.value.byteLength;
this.dispatchEvent("progress", this.readed);
c.enqueue(v.value);
}
this.error = e;
});
}
},
cancel: (reason) => {
readable.cancel(reason);
try {
readable.cancel(reason);
} catch (_) {
null;
}
},
type: "bytes",
});