mirror of
https://github.com/lifegpc/eh-downloader.git
synced 2026-06-06 05:38:44 +08:00
Add workround for deno can not handle error in streams
This commit is contained in:
@@ -343,6 +343,9 @@ export async function download_task(
|
||||
signal: force_abort,
|
||||
preventClose: true,
|
||||
});
|
||||
if (pr.error) {
|
||||
throw (pr.error);
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
f.close();
|
||||
|
||||
@@ -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",
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user