Fix download_timout not works correctly

This commit is contained in:
2023-12-25 18:23:56 +08:00
parent cf38e9e981
commit 1f326bcf0a
2 changed files with 20 additions and 15 deletions

View File

@@ -171,7 +171,7 @@ export class Config {
return this._return_number("fetch_timeout") || 10000;
}
get download_timeout() {
return this._return_number("download_timeout") || 10000;
return this._return_number("download_timeout") || 2000;
}
get ffprobe_path() {
return this._return_string("ffprobe_path") || "ffprobe";

View File

@@ -17,6 +17,7 @@ export class ProgressReadable extends EventTarget {
#controller: AbortController;
#is_timeout: boolean;
#timeout?: number;
#last_readed: number;
constructor(
readable: ReadableStream<Uint8Array>,
timeout: number,
@@ -26,11 +27,12 @@ export class ProgressReadable extends EventTarget {
this.readed = 0;
this.timeout = timeout;
this.#is_timeout = false;
this.#last_readed = Date.now();
const reader = readable.getReader();
this.#controller = new AbortController();
originalSignal?.addEventListener("abort", () => {
this.#controller.abort();
this.#clearTimeout();
this.#clearInterval();
});
this.readable = new ReadableStream({
pull: (c) => {
@@ -41,15 +43,14 @@ export class ProgressReadable extends EventTarget {
if (v.done) {
this.dispatchEvent("finished", this.readed);
c.close();
this.#clearTimeout();
this.#clearInterval();
return;
} else {
this.readed += v.value.byteLength;
this.dispatchEvent("progress", this.readed);
c.enqueue(v.value);
if (v.value.byteLength != 0) {
this.#clearTimeout();
this.#setTimeout();
this.#last_readed = Date.now();
}
}
}).catch((e) => {
@@ -59,33 +60,37 @@ export class ProgressReadable extends EventTarget {
null;
}
this.error = e;
this.#clearTimeout();
this.#clearInterval();
});
}
},
cancel: (reason) => {
try {
if (!readable.locked) readable.cancel(reason);
this.#clearTimeout();
this.#clearInterval();
} catch (_) {
null;
}
},
type: "bytes",
});
this.#setTimeout();
this.#setInterval();
}
#clearTimeout() {
#clearInterval() {
if (this.#timeout) {
clearTimeout(this.#timeout);
clearInterval(this.#timeout);
this.#timeout = undefined;
}
}
#setTimeout() {
#setInterval() {
this.#timeout = setTimeout(() => {
this.#is_timeout = true;
this.#controller.abort();
console.log("aborted");
}, this.timeout);
const now = Date.now();
if (now - this.#last_readed > this.timeout) {
this.#is_timeout = true;
this.#controller.abort();
this.#clearInterval();
}
}, 1);
}
// @ts-ignore Checked type
addEventListener<T extends keyof EventMap>(