Skip to content

Commit

Permalink
fix: 修复 unhandledrejection 未被捕获的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
share121 committed Feb 6, 2025
1 parent 4a358b0 commit 5327529
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
Empty file.
39 changes: 22 additions & 17 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,28 @@ export async function download({
endChunk?: number;
}) {
async function getURLInfo() {
const r = await fetch(url, {
method: "HEAD",
headers: headers,
});
const contentLength = Math.min(
+r.headers.get("content-length")!,
endChunk - startChunk + 1
);
const disposition = r.headers.get("content-disposition");
const filename = (
disposition
?.split(";")
.map((s) => s.trim())
.find((s) => s.startsWith("filename="))
?.split("=")[1] ?? basename(url)
).replace(/[\\/:*?"<>|]/g, "_");
return { filename, contentLength };
while (true) {
const r = await fetch(url, {
method: "HEAD",
headers: headers,
});
const contentLength = Math.min(
+r.headers.get("content-length")!,
endChunk - startChunk + 1
);
const disposition = r.headers.get("content-disposition");
const filename = (
(disposition
?.split(";")
.map((s) => s.trim())
.find((s) => s.startsWith("filename="))
?.split("=")[1] ??
basename(new URL(url).pathname)) ||
"download"
).replace(/[\\/:*?"<>|]/g, "_");
if (contentLength) return { filename, contentLength };
else console.log(`Content-Length = ${contentLength}, retrying...`);
}
}

const mutex = new Mutex();
Expand Down
2 changes: 1 addition & 1 deletion workerpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function downloadChunk<T, R>(
const messageHandle = (e: MessageEvent<R>) => {
// Deno 不能在主线程中捕获错误,所以这是折中的办法 start
// @ts-ignore
if (e.data?.type === "error") {
if (e.data?.type === "error" || e.data?.type === "unhandledrejection") {
// @ts-ignore
return errorHandel(e.data);
}
Expand Down

0 comments on commit 5327529

Please sign in to comment.