Skip to content

Commit

Permalink
feat: 自动构建
Browse files Browse the repository at this point in the history
  • Loading branch information
share121 committed Feb 6, 2025
1 parent 7508d49 commit 4863d6f
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 28 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: build

on:
push:
tags:
- "v*"

permissions:
packages: write
contents: write

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
targets:
- x86_64-unknown-linux-gnu
- aarch64-unknown-linux-gnu
- x86_64-pc-windows-msvc
- x86_64-apple-darwin
- aarch64-apple-darwin
steps:
- uses: actions/checkout@v4

- name: Setup Deno
uses: denoland/setup-deno@v2
with:
deno-version: v2.x

- name: Build
run: deno compile --target ${{matrix.targets}} --allow-write --allow-net --allow-read --include worker.ts main.ts

- name: Create Release and Upload Release Asset
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ github.ref }}
name: Fast Down ${{ github.ref }}
body: 修复了一些已知问题
draft: false
prerelease: false
files: |
fast-down
fast-down.exe
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: build

on:
push:
branches:
- main
paths-ignore:
- "docs/**"
- "README.md"
pull_request:
paths-ignore:
- "docs/**"
- "README.md"
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup Deno
uses: denoland/setup-deno@v2
with:
deno-version: v2.x

- name: Test
run: deno test --allow-write --allow-net --allow-read
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"tasks": {
"build": "deno compile --allow-write --allow-net --allow-read --include worker.ts main.ts "
"build": "deno compile --allow-write --allow-net --allow-read --include worker.ts main.ts"
},
"compilerOptions": {
"lib": ["deno.window", "deno.worker"]
Expand Down
13 changes: 5 additions & 8 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { dirname, join, basename } from "jsr:@std/path";
import { Mutex } from "npm:async-mutex";
import { exec } from "./workerpool.ts";
import { downloadChunk } from "./workerpool.ts";

export async function download(
url: string,
filename: string,
threads: number,
chunkSize = 10 * 1024 * 1024,
chunkSize = 5 * 1024 * 1024,
headers: HeadersInit = {}
) {
async function writeFile(buf: ArrayBuffer, pos: number) {
Expand Down Expand Up @@ -44,14 +44,13 @@ export async function download(
const contentLength = await getContentLength();
const chunkCount = Math.ceil(contentLength / chunkSize);
const chunks: Chunk[] = Array.from({ length: chunkCount }, (_, i) => ({
url,
start: i * chunkSize,
end: Math.min((i + 1) * chunkSize, contentLength) - 1,
headers,
}));
await exec<Chunk, ArrayBuffer>(
await downloadChunk<Chunk, ArrayBuffer>(
threads,
() => new Worker(import.meta.resolve("./worker.ts"), { type: "module" }),
url,
headers,
chunks,
async (i) => {
await writeFile(i.data, i.origin.start);
Expand All @@ -66,10 +65,8 @@ export async function download(
}

export interface Chunk {
url: string;
start: number;
end: number;
headers: HeadersInit;
}

if (import.meta.main) {
Expand Down
5 changes: 2 additions & 3 deletions main_test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { crypto } from "jsr:@std/crypto";
import { download } from "./main.ts";
import { join } from "jsr:@std/path/join";
import { basename } from "jsr:@std/path/basename";
import { join, basename } from "jsr:@std/path";
import { encodeHex } from "jsr:@std/encoding/hex";
import { assertEquals } from "jsr:@std/assert";
import { download } from "./main.ts";

Deno.test("Download ISO file test", async () => {
const url =
Expand Down
21 changes: 14 additions & 7 deletions worker.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import type { Chunk } from "./main.ts";

addEventListener("message", async ({ data }: MessageEvent<Chunk[]>) => {
for (const item of data) {
const r = await fetch(item.url, {
headers: { Range: `bytes=${item.start}-${item.end}` },
interface EventData {
url: string;
headers: HeadersInit;
chunks: Chunk[];
}

addEventListener("message", async ({ data }: MessageEvent<EventData>) => {
for (const item of data.chunks) {
const r = await fetch(data.url, {
headers: {
...data.headers,
Range: `bytes=${item.start}-${item.end}`,
},
});
if (r.status !== 206) {
throw new Error(`Invalid status code ${r.status}"}`);
}
if (r.status !== 206) throw new Error(`Invalid status code ${r.status}"}`);
const buf = await r.arrayBuffer();
self.postMessage(buf, [buf]);
}
Expand Down
29 changes: 20 additions & 9 deletions workerpool.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// deno-lint-ignore-file ban-ts-comment
export function exec<T, R>(
export function downloadChunk<T, R>(
threadCount: number,
workerFactory: () => Worker,
url: string,
headers: HeadersInit,
data: T[],
onProgress: (data: { origin: T; data: R; index: number }) => void,
maxRetries: number = 3
) {
if (data.length < 1) return Promise.resolve([]);
if (threadCount < 1) throw new Error("threadCount must be greater than 0");
const workerFactory = () =>
new Worker(import.meta.resolve("./worker.ts"), { type: "module" });
return new Promise<void>((resolve, reject) => {
const baseChunkCount = Math.floor(data.length / threadCount);
const remainingChunks = data.length % threadCount;
Expand Down Expand Up @@ -83,9 +86,11 @@ export function exec<T, R>(
splitPoint;
printWorkerData(workerData, i);
printWorkerData(targetWorker, targetWorkerIndex);
workerData.worker.postMessage(
data.slice(splitPoint, workerData.endChunk)
);
workerData.worker.postMessage({
url,
headers,
chunks: data.slice(splitPoint, workerData.endChunk),
});
};
const errorHandel = (err: ErrorEvent) => {
if (workerData.retryCount >= maxRetries) {
Expand All @@ -97,13 +102,19 @@ export function exec<T, R>(
workerData.retryCount++;
workerData.stolen = false;
printWorkerData(workerData, i, "try: ");
workerData.worker.postMessage(
data.slice(workerData.currentChunk, workerData.endChunk)
);
workerData.worker.postMessage({
url,
headers,
chunks: data.slice(workerData.currentChunk, workerData.endChunk),
});
};
workerData.worker.addEventListener("message", messageHandle);
workerData.worker.addEventListener("error", errorHandel);
workerData.worker.postMessage(data.slice(startChunk, endChunk));
workerData.worker.postMessage({
url,
headers,
chunks: data.slice(startChunk, endChunk),
});
}
});
}
Expand Down

0 comments on commit 4863d6f

Please sign in to comment.