diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2ec33eb..7a8c295 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -3,7 +3,7 @@ name: build on: push: tags: - - "v*" + - v* permissions: packages: write @@ -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 diff --git a/worker.ts b/worker.ts index 9dcbdec..5c2693b 100644 --- a/worker.ts +++ b/worker.ts @@ -10,53 +10,17 @@ export interface FetchChunksOptions { export async function* fetchChunks( data: FetchChunksOptions ): AsyncGenerator { - 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()); } }