Skip to content

Commit

Permalink
refactor: 移除web worker
Browse files Browse the repository at this point in the history
  • Loading branch information
share121 committed Feb 7, 2025
1 parent f7fa5f5 commit 1ee36c9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 54 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: build
on:
push:
tags:
- "v*"
- v*

permissions:
packages: write
Expand All @@ -22,11 +22,11 @@ jobs:

- name: Build
run: |
deno compile --target x86_64-unknown-linux-gnu -o fast-down-x86_64-unknown-linux-gnu --allow-write --allow-net --allow-read --include worker.ts main.ts
deno compile --target aarch64-unknown-linux-gnu -o fast-down-aarch64-unknown-linux-gnu --allow-write --allow-net --allow-read --include worker.ts main.ts
deno compile --target x86_64-pc-windows-msvc -o fast-down-x86_64-pc-windows-msvc.exe --allow-write --allow-net --allow-read --include worker.ts main.ts
deno compile --target x86_64-apple-darwin -o fast-down-x86_64-apple-darwin --allow-write --allow-net --allow-read --include worker.ts main.ts
deno compile --target aarch64-apple-darwin -o fast-down-aarch64-apple-darwin --allow-write --allow-net --allow-read --include worker.ts main.ts
deno compile --target x86_64-unknown-linux-gnu -o fast-down-x86_64-unknown-linux-gnu --allow-write --allow-net --allow-read main.ts
deno compile --target aarch64-unknown-linux-gnu -o fast-down-aarch64-unknown-linux-gnu --allow-write --allow-net --allow-read main.ts
deno compile --target x86_64-pc-windows-msvc -o fast-down-x86_64-pc-windows-msvc.exe --allow-write --allow-net --allow-read main.ts
deno compile --target x86_64-apple-darwin -o fast-down-x86_64-apple-darwin --allow-write --allow-net --allow-read main.ts
deno compile --target aarch64-apple-darwin -o fast-down-aarch64-apple-darwin --allow-write --allow-net --allow-read main.ts
- name: Create Release and Upload Release Asset
uses: softprops/action-gh-release@v1
Expand Down
60 changes: 12 additions & 48 deletions worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,17 @@ export interface FetchChunksOptions {
export async function* fetchChunks(
data: FetchChunksOptions
): AsyncGenerator<Uint8Array, void, unknown> {
const r = await fetch(data.url, {
headers: {
...data.headers,
Range: `bytes=${data.chunks[0].start}-${data.chunks.at(-1)!.end}`,
},
signal: data.signal,
});
if (r.status !== 206) throw new Error(`Invalid status code ${r.status}"}`);
if (!r.body) throw new Error("No body");
const reader = r.body.getReader();
let currentChunkIndex = 0;
let currentBuffer: Uint8Array | null = null;
let bufferOffset = 0;
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
let bytesProcessed = 0;
while (bytesProcessed < value.length) {
if (!currentBuffer && currentChunkIndex < data.chunks.length) {
const chunk = data.chunks[currentChunkIndex];
currentBuffer = new Uint8Array(chunk.end - chunk.start + 1);
bufferOffset = 0;
}
if (!currentBuffer) throw new Error("Unexpected extra data received");
const remainingBytes = currentBuffer.length - bufferOffset;
const bytesToCopy = Math.min(
remainingBytes,
value.length - bytesProcessed
);
currentBuffer.set(
value.subarray(bytesProcessed, bytesProcessed + bytesToCopy),
bufferOffset
);
bufferOffset += bytesToCopy;
bytesProcessed += bytesToCopy;
if (bufferOffset === currentBuffer.length) {
yield currentBuffer;
currentChunkIndex++;
currentBuffer = null;
}
}
}
if (currentChunkIndex !== data.chunks.length || currentBuffer) {
throw new Error("Incomplete data received");
}
} finally {
reader.releaseLock();
for (const chunk of data.chunks) {
const client = Deno.createHttpClient({});
const r = await fetch(data.url, {
headers: {
...data.headers,
Range: `bytes=${chunk.start}-${chunk.end}`,
},
signal: data.signal,
client,
});
if (r.status !== 206) throw new Error(`Invalid status code ${r.status}"}`);
yield new Uint8Array(await r.arrayBuffer());
}
}

0 comments on commit 1ee36c9

Please sign in to comment.