justget 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +263 -0
- package/dist/chunks/strategy.d.ts +39 -0
- package/dist/chunks/strategy.d.ts.map +1 -0
- package/dist/chunks/strategy.js +97 -0
- package/dist/chunks/strategy.js.map +1 -0
- package/dist/cli.d.ts +7 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +117 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +71 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +158 -0
- package/dist/config.js.map +1 -0
- package/dist/core/downloader.d.ts +22 -0
- package/dist/core/downloader.d.ts.map +1 -0
- package/dist/core/downloader.js +555 -0
- package/dist/core/downloader.js.map +1 -0
- package/dist/core/http.d.ts +51 -0
- package/dist/core/http.d.ts.map +1 -0
- package/dist/core/http.js +186 -0
- package/dist/core/http.js.map +1 -0
- package/dist/core/temp.d.ts +47 -0
- package/dist/core/temp.d.ts.map +1 -0
- package/dist/core/temp.js +137 -0
- package/dist/core/temp.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/speed/ewma.d.ts +38 -0
- package/dist/speed/ewma.d.ts.map +1 -0
- package/dist/speed/ewma.js +87 -0
- package/dist/speed/ewma.js.map +1 -0
- package/dist/speed/speed-monitor.d.ts +56 -0
- package/dist/speed/speed-monitor.d.ts.map +1 -0
- package/dist/speed/speed-monitor.js +94 -0
- package/dist/speed/speed-monitor.js.map +1 -0
- package/dist/types.d.ts +236 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/hash.d.ts +38 -0
- package/dist/utils/hash.d.ts.map +1 -0
- package/dist/utils/hash.js +89 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/validator.d.ts +21 -0
- package/dist/utils/validator.d.ts.map +1 -0
- package/dist/utils/validator.js +72 -0
- package/dist/utils/validator.js.map +1 -0
- package/package.json +70 -0
|
@@ -0,0 +1,555 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 核心下载器:多源竞速分块下载
|
|
3
|
+
* Core downloader: multi-source racing chunked download
|
|
4
|
+
*
|
|
5
|
+
* 流程 / Flow:
|
|
6
|
+
* 探测(probe) → 分块(chunks) → 主源预热(warmup) → 决策(decide mirrors)
|
|
7
|
+
* → 竞速(race) → 慢源替换(replace) → 合并(merge) → 校验(verify) → 提交(commit)
|
|
8
|
+
*/
|
|
9
|
+
import * as fs from 'node:fs';
|
|
10
|
+
import * as path from 'node:path';
|
|
11
|
+
import { createWriteStream } from 'node:fs';
|
|
12
|
+
import { mergeConfig, validateConfig, isImmediateFailure, REPLACEMENT_CONFIG, } from '../config.js';
|
|
13
|
+
import { calculateChunkSize, calculateChunkCount, createChunkRanges, } from '../chunks/strategy.js';
|
|
14
|
+
import { SpeedMonitor } from '../speed/speed-monitor.js';
|
|
15
|
+
import { probe, openRangeStream, consumeBody, HttpError } from './http.js';
|
|
16
|
+
import { cleanupOldTempFiles, buildChunkTempPaths, findResumableMerged, mergeChunks, commitMerged, removeAllTempFiles, } from './temp.js';
|
|
17
|
+
import { validateFile } from '../utils/validator.js';
|
|
18
|
+
import { getTempFileName, getMergedTempFileName } from '../utils/hash.js';
|
|
19
|
+
function sleep(ms) {
|
|
20
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 多源竞速分块下载
|
|
24
|
+
* Multi-source racing chunked download
|
|
25
|
+
*/
|
|
26
|
+
export async function download(options) {
|
|
27
|
+
const startedAt = Date.now();
|
|
28
|
+
const { url, output, mirrors = [] } = options;
|
|
29
|
+
if (!url)
|
|
30
|
+
throw new Error('url is required / url 必填');
|
|
31
|
+
if (!output)
|
|
32
|
+
throw new Error('output is required / output 必填');
|
|
33
|
+
const cfg = mergeConfig(options.options);
|
|
34
|
+
validateConfig(cfg);
|
|
35
|
+
const result = await runDownload(url, output, mirrors, cfg);
|
|
36
|
+
const final = { ...result, duration: Date.now() - startedAt };
|
|
37
|
+
if (cfg.onComplete)
|
|
38
|
+
cfg.onComplete(final);
|
|
39
|
+
return final;
|
|
40
|
+
}
|
|
41
|
+
async function runDownload(url, output, mirrors, cfg) {
|
|
42
|
+
const startedAt = Date.now();
|
|
43
|
+
const outPath = path.resolve(output);
|
|
44
|
+
const dir = path.dirname(outPath);
|
|
45
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
46
|
+
// 清理过期临时文件 / clean stale temp files
|
|
47
|
+
cleanupOldTempFiles(outPath);
|
|
48
|
+
// 1) 探测主站 / probe primary
|
|
49
|
+
const primaryProbe = await probe(url, {
|
|
50
|
+
connectTimeout: cfg.connectTimeout,
|
|
51
|
+
timeout: cfg.timeout,
|
|
52
|
+
});
|
|
53
|
+
const contentLength = primaryProbe.contentLength;
|
|
54
|
+
const supportsRange = primaryProbe.supportsRange;
|
|
55
|
+
// 断点续传 / resume: 输出已完整 或 merged 临时文件可复用
|
|
56
|
+
if (contentLength !== undefined) {
|
|
57
|
+
try {
|
|
58
|
+
if (fs.statSync(outPath).size === contentLength) {
|
|
59
|
+
return summarize(outPath, contentLength, [], startedAt, []);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
// 不存在则忽略
|
|
64
|
+
}
|
|
65
|
+
const merged = findResumableMerged(outPath, contentLength);
|
|
66
|
+
if (merged && (await tryResumeMerged(merged, outPath, cfg))) {
|
|
67
|
+
return summarize(outPath, contentLength, [], startedAt, []);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// 2) 分块规划 / chunk planning
|
|
71
|
+
const chunks = [];
|
|
72
|
+
if (supportsRange && contentLength !== undefined && contentLength > 0) {
|
|
73
|
+
const chunkSize = calculateChunkSize(contentLength, cfg.chunks ?? 'auto', cfg.minChunkSize);
|
|
74
|
+
const count = calculateChunkCount(contentLength, chunkSize);
|
|
75
|
+
const ranges = createChunkRanges(contentLength, count);
|
|
76
|
+
const tempPaths = buildChunkTempPaths(outPath, ranges.length);
|
|
77
|
+
for (let i = 0; i < ranges.length; i++) {
|
|
78
|
+
const [start, end] = ranges[i];
|
|
79
|
+
chunks.push({
|
|
80
|
+
index: i,
|
|
81
|
+
start,
|
|
82
|
+
end,
|
|
83
|
+
size: end - start + 1,
|
|
84
|
+
tempPath: tempPaths[i].tempPath,
|
|
85
|
+
status: 'pending',
|
|
86
|
+
failures: 0,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const ctx = {
|
|
91
|
+
output: outPath,
|
|
92
|
+
contentLength,
|
|
93
|
+
chunks,
|
|
94
|
+
workers: [],
|
|
95
|
+
cfg,
|
|
96
|
+
done: false,
|
|
97
|
+
mirrorsStarted: false,
|
|
98
|
+
primaryFailedImmediately: false,
|
|
99
|
+
};
|
|
100
|
+
const emitProgress = () => {
|
|
101
|
+
if (!cfg.onProgress)
|
|
102
|
+
return;
|
|
103
|
+
const sources = ctx.workers.map((w) => ({
|
|
104
|
+
id: w.id,
|
|
105
|
+
url: w.url,
|
|
106
|
+
downloadedBytes: w.bytesWritten,
|
|
107
|
+
speed: w.monitor.getCurrentSpeed(),
|
|
108
|
+
status: w.status,
|
|
109
|
+
}));
|
|
110
|
+
const downloadedBytes = ctx.chunks.length > 0
|
|
111
|
+
? ctx.chunks.reduce((sum, c) => sum + (c.status === 'done' ? c.size : 0), 0)
|
|
112
|
+
: ctx.workers.reduce((sum, w) => sum + w.bytesWritten, 0);
|
|
113
|
+
const total = ctx.contentLength ?? downloadedBytes;
|
|
114
|
+
const speed = ctx.workers.reduce((sum, w) => sum + w.monitor.getCurrentSpeed(), 0);
|
|
115
|
+
const progress = {
|
|
116
|
+
downloadedBytes,
|
|
117
|
+
totalBytes: total,
|
|
118
|
+
percentage: total > 0 ? (downloadedBytes / total) * 100 : 0,
|
|
119
|
+
speed,
|
|
120
|
+
eta: speed > 0 ? ((total - downloadedBytes) / speed) * 1000 : 0,
|
|
121
|
+
sources,
|
|
122
|
+
};
|
|
123
|
+
cfg.onProgress(progress);
|
|
124
|
+
};
|
|
125
|
+
// 3) 镜像启动决策 / mirror-start decision
|
|
126
|
+
const shouldStartMirrors = () => {
|
|
127
|
+
const primarySpeed = primaryWorker.monitor.getCurrentSpeed();
|
|
128
|
+
const size = ctx.contentLength ?? 0;
|
|
129
|
+
if (primarySpeed >= (cfg.fastPrimaryThreshold ?? 512 * 1024 * 4))
|
|
130
|
+
return false;
|
|
131
|
+
if (size < (cfg.sizeThreshold ?? 50 * 1024 * 1024))
|
|
132
|
+
return false;
|
|
133
|
+
if (primarySpeed < (cfg.speedThreshold ?? 512 * 1024))
|
|
134
|
+
return true;
|
|
135
|
+
const downloaded = ctx.chunks.reduce((s, c) => s + (c.status === 'done' ? c.size : 0), 0);
|
|
136
|
+
if (size > 0 && downloaded / size < 0.3 && primarySpeed < (cfg.speedThreshold ?? 512 * 1024) * 1.5)
|
|
137
|
+
return true;
|
|
138
|
+
return false;
|
|
139
|
+
};
|
|
140
|
+
const startMirrors = () => {
|
|
141
|
+
if (ctx.mirrorsStarted)
|
|
142
|
+
return;
|
|
143
|
+
ctx.mirrorsStarted = true;
|
|
144
|
+
mirrors.forEach((mirrorUrl, i) => {
|
|
145
|
+
const w = {
|
|
146
|
+
id: `mirror-${i}`,
|
|
147
|
+
url: mirrorUrl,
|
|
148
|
+
isPrimary: false,
|
|
149
|
+
supportsRange,
|
|
150
|
+
monitor: new SpeedMonitor(),
|
|
151
|
+
controller: new AbortController(),
|
|
152
|
+
aborted: false,
|
|
153
|
+
slowChecks: 0,
|
|
154
|
+
bytesWritten: 0,
|
|
155
|
+
startedAt: Date.now(),
|
|
156
|
+
status: 'active',
|
|
157
|
+
};
|
|
158
|
+
startWorker(w);
|
|
159
|
+
});
|
|
160
|
+
};
|
|
161
|
+
const maybeStartMirrors = () => {
|
|
162
|
+
if (ctx.done || ctx.mirrorsStarted || mirrors.length === 0)
|
|
163
|
+
return;
|
|
164
|
+
// 主源立即失败 → 无条件启动镜像 / immediate primary failure → start mirrors regardless of size
|
|
165
|
+
if (ctx.primaryFailedImmediately) {
|
|
166
|
+
startMirrors();
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (shouldStartMirrors())
|
|
170
|
+
startMirrors();
|
|
171
|
+
};
|
|
172
|
+
const startWorker = (w) => {
|
|
173
|
+
ctx.workers.push(w);
|
|
174
|
+
w.monitor.startSampling(() => w.bytesWritten);
|
|
175
|
+
void runWorker(w, ctx, emitProgress).catch((err) => {
|
|
176
|
+
w.error = err;
|
|
177
|
+
w.status = 'failed';
|
|
178
|
+
ctx.lastError = ctx.lastError ?? err;
|
|
179
|
+
// 任何 HTTP 4xx/5xx 或网络级失败 → 立即失败,提前启动镜像
|
|
180
|
+
if (w.isPrimary &&
|
|
181
|
+
(err instanceof HttpError ||
|
|
182
|
+
isImmediateFailure(err, err.status))) {
|
|
183
|
+
ctx.primaryFailedImmediately = true;
|
|
184
|
+
maybeStartMirrors();
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
};
|
|
188
|
+
// 4) 启动主源 / start primary
|
|
189
|
+
const primaryWorker = {
|
|
190
|
+
id: 'primary',
|
|
191
|
+
url,
|
|
192
|
+
isPrimary: true,
|
|
193
|
+
supportsRange,
|
|
194
|
+
monitor: new SpeedMonitor(),
|
|
195
|
+
controller: new AbortController(),
|
|
196
|
+
aborted: false,
|
|
197
|
+
slowChecks: 0,
|
|
198
|
+
bytesWritten: 0,
|
|
199
|
+
startedAt: Date.now(),
|
|
200
|
+
status: 'active',
|
|
201
|
+
};
|
|
202
|
+
startWorker(primaryWorker);
|
|
203
|
+
// 5) 预热后决策 / decide after warmup
|
|
204
|
+
const warmupTimer = setTimeout(() => maybeStartMirrors(), cfg.primaryWarmupTime ?? 5000);
|
|
205
|
+
// 6) 慢源替换检查 / slow-source replacement
|
|
206
|
+
const replaceCheck = setInterval(() => {
|
|
207
|
+
if (ctx.done)
|
|
208
|
+
return;
|
|
209
|
+
const active = ctx.workers.filter((w) => w.status === 'active' && !w.aborted);
|
|
210
|
+
if (active.length < 2)
|
|
211
|
+
return;
|
|
212
|
+
const elapsedMs = Date.now() - startedAt;
|
|
213
|
+
if (elapsedMs < (cfg.minReplaceTime ?? 30_000))
|
|
214
|
+
return;
|
|
215
|
+
const fastest = active.reduce((a, b) => a.monitor.getCurrentSpeed() > b.monitor.getCurrentSpeed() ? a : b);
|
|
216
|
+
const fastestSpeed = fastest.monitor.getCurrentSpeed();
|
|
217
|
+
if (fastestSpeed <= 0)
|
|
218
|
+
return;
|
|
219
|
+
for (const w of active) {
|
|
220
|
+
if (w === fastest)
|
|
221
|
+
continue;
|
|
222
|
+
const ratio = w.monitor.getCurrentSpeed() / fastestSpeed;
|
|
223
|
+
if (ratio < REPLACEMENT_CONFIG.slowThresholdRatio) {
|
|
224
|
+
w.slowChecks += 1;
|
|
225
|
+
if (w.slowChecks >= REPLACEMENT_CONFIG.maxSlowChecks) {
|
|
226
|
+
// 替换:中止慢源,其未完成分块交还池子 / replace: abort slow source, release its chunk
|
|
227
|
+
w.aborted = true;
|
|
228
|
+
w.status = 'failed';
|
|
229
|
+
w.controller.abort();
|
|
230
|
+
emitProgress();
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
w.slowChecks = 0;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}, cfg.replaceCheckInterval ?? 10_000);
|
|
238
|
+
// 7) 进度回调 / progress tick
|
|
239
|
+
const progressTimer = setInterval(emitProgress, 1000);
|
|
240
|
+
try {
|
|
241
|
+
// 8) 等待完成 / wait for completion
|
|
242
|
+
await waitForCompletion(ctx, cfg);
|
|
243
|
+
if (!ctx.done) {
|
|
244
|
+
throw ctx.lastError ?? new Error('Download failed: no source could complete / 所有源均失败');
|
|
245
|
+
}
|
|
246
|
+
// 分块模式下 worker 正常退出时不改状态,这里统一标记 / chunked-mode
|
|
247
|
+
// workers exit without updating status; mark them completed here
|
|
248
|
+
for (const w of ctx.workers) {
|
|
249
|
+
if (w.status === 'active')
|
|
250
|
+
w.status = 'completed';
|
|
251
|
+
}
|
|
252
|
+
// 9) 合并与提交 / merge & commit
|
|
253
|
+
const mergedPath = path.join(dir, getMergedTempFileName(outPath));
|
|
254
|
+
if (ctx.chunks.length > 0) {
|
|
255
|
+
const doneChunks = ctx.chunks
|
|
256
|
+
.filter((c) => c.status === 'done')
|
|
257
|
+
.map((c) => ({ index: c.index, tempPath: c.tempPath }));
|
|
258
|
+
if (doneChunks.length !== ctx.chunks.length) {
|
|
259
|
+
throw new Error(`Incomplete chunks: ${doneChunks.length}/${ctx.chunks.length} / 分块未全部完成`);
|
|
260
|
+
}
|
|
261
|
+
await mergeChunks(doneChunks, mergedPath);
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
// 单流模式:胜者临时文件即结果 / single-stream: winner temp file is the result
|
|
265
|
+
const winner = ctx.singleWinnerPath;
|
|
266
|
+
if (!winner || !fs.existsSync(winner)) {
|
|
267
|
+
throw new Error('No download produced output / 未产生输出文件');
|
|
268
|
+
}
|
|
269
|
+
fs.renameSync(winner, mergedPath);
|
|
270
|
+
}
|
|
271
|
+
// 10) 校验 / verify checksum
|
|
272
|
+
if (cfg.checksum) {
|
|
273
|
+
const validation = await validateFile(mergedPath, cfg.checksum, cfg.checksumAlgorithm ?? 'sha256');
|
|
274
|
+
if (!validation.valid) {
|
|
275
|
+
throw new Error(`Checksum mismatch / 校验和不匹配: expected ${cfg.checksum}, got ${validation.actual ?? 'n/a'}`);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
// 11) 原子提交 / atomic commit
|
|
279
|
+
commitMerged(mergedPath, outPath);
|
|
280
|
+
// 12) 清理 / cleanup
|
|
281
|
+
removeAllTempFiles(outPath);
|
|
282
|
+
const finalSize = contentLength ?? fs.statSync(outPath).size;
|
|
283
|
+
return summarize(outPath, finalSize, ctx.workers, startedAt, ctx.chunks);
|
|
284
|
+
}
|
|
285
|
+
finally {
|
|
286
|
+
clearTimeout(warmupTimer);
|
|
287
|
+
clearInterval(replaceCheck);
|
|
288
|
+
clearInterval(progressTimer);
|
|
289
|
+
for (const w of ctx.workers) {
|
|
290
|
+
w.controller.abort();
|
|
291
|
+
w.monitor.stopSampling(); // 停止测速定时器,防止泄漏 / stop speed-sampling timers (leak fix)
|
|
292
|
+
}
|
|
293
|
+
// 失败时保留临时文件以便续传 / keep temp files on failure for resume
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
/** 尝试复用已存在的 merged 临时文件 / try to resume from an existing merged temp file */
|
|
297
|
+
async function tryResumeMerged(mergedPath, outPath, cfg) {
|
|
298
|
+
try {
|
|
299
|
+
if (cfg.checksum) {
|
|
300
|
+
const validation = await validateFile(mergedPath, cfg.checksum, cfg.checksumAlgorithm ?? 'sha256');
|
|
301
|
+
if (!validation.valid)
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
commitMerged(mergedPath, outPath);
|
|
305
|
+
return true;
|
|
306
|
+
}
|
|
307
|
+
catch {
|
|
308
|
+
return false;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
/** 等待所有分块完成或全部 worker 失败 / wait until completion or all workers failed */
|
|
312
|
+
async function waitForCompletion(ctx, cfg) {
|
|
313
|
+
const deadline = Date.now() + (cfg.timeout ?? 300_000) * 3;
|
|
314
|
+
while (Date.now() < deadline) {
|
|
315
|
+
if (ctx.done)
|
|
316
|
+
return;
|
|
317
|
+
const active = ctx.workers.some((w) => w.status === 'active' && !w.aborted);
|
|
318
|
+
if (ctx.chunks.length === 0) {
|
|
319
|
+
// 单流模式:等某个 worker 完成(downloadSingle 会置 ctx.done)
|
|
320
|
+
if (!active && ctx.workers.length > 0) {
|
|
321
|
+
throw ctx.lastError ?? new Error('All sources failed / 所有源均失败');
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
if (ctx.chunks.every((c) => c.status === 'done')) {
|
|
326
|
+
ctx.done = true;
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
if (!active && ctx.chunks.some((c) => c.status !== 'done')) {
|
|
330
|
+
throw ctx.lastError ?? new Error('No active workers and chunks pending / 无可用源且仍有分块未完成');
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
await sleep(100);
|
|
334
|
+
}
|
|
335
|
+
throw new Error(`Download timeout / 下载超时 (${(cfg.timeout ?? 300_000) * 3}ms)`);
|
|
336
|
+
}
|
|
337
|
+
/** 单个 worker 循环:不断取 chunk 下载 / worker loop: acquire and download chunks */
|
|
338
|
+
async function runWorker(w, ctx, emit) {
|
|
339
|
+
const cfg = ctx.cfg;
|
|
340
|
+
// 单流模式(无 Range):整文件竞速,先完成者胜出
|
|
341
|
+
if (ctx.chunks.length === 0) {
|
|
342
|
+
await downloadSingle(w, ctx, emit);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
for (;;) {
|
|
346
|
+
if (w.aborted || ctx.done)
|
|
347
|
+
return;
|
|
348
|
+
const chunk = acquireChunk(ctx, w);
|
|
349
|
+
if (!chunk) {
|
|
350
|
+
if (ctx.chunks.every((c) => c.status === 'done')) {
|
|
351
|
+
w.status = 'completed';
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
await sleep(150);
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
chunk.status = 'assigned';
|
|
358
|
+
let succeeded = false;
|
|
359
|
+
for (let attempt = 0; attempt <= (cfg.retries ?? 0) && !w.aborted; attempt++) {
|
|
360
|
+
if (attempt > 0)
|
|
361
|
+
await sleep(cfg.retryDelay ?? 0);
|
|
362
|
+
try {
|
|
363
|
+
await downloadChunkToFile(w, chunk, ctx);
|
|
364
|
+
succeeded = true;
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
catch (err) {
|
|
368
|
+
const e = err;
|
|
369
|
+
if (isImmediateFailure(e, e.status))
|
|
370
|
+
break; // 立即失败:不再重试该源
|
|
371
|
+
if (attempt === (cfg.retries ?? 0))
|
|
372
|
+
w.error = e;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (succeeded) {
|
|
376
|
+
chunk.status = 'done';
|
|
377
|
+
w.bytesWritten += chunk.size;
|
|
378
|
+
emit();
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
// 释放分块交还池子,其他 worker 可接手 / release chunk back to pool
|
|
382
|
+
chunk.status = 'pending';
|
|
383
|
+
chunk.failures += 1;
|
|
384
|
+
if (w.aborted) {
|
|
385
|
+
emit();
|
|
386
|
+
return; // 被替换/中止 / replaced or aborted
|
|
387
|
+
}
|
|
388
|
+
w.status = 'failed';
|
|
389
|
+
w.aborted = true;
|
|
390
|
+
w.controller.abort();
|
|
391
|
+
emit();
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
/** 从池中取 chunk:主源从头部,镜像从尾部 / acquire chunk: primary head, mirror tail */
|
|
397
|
+
function acquireChunk(ctx, w) {
|
|
398
|
+
if (w.isPrimary) {
|
|
399
|
+
for (const c of ctx.chunks) {
|
|
400
|
+
if (c.status === 'pending')
|
|
401
|
+
return c;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
for (let i = ctx.chunks.length - 1; i >= 0; i--) {
|
|
406
|
+
if (ctx.chunks[i].status === 'pending')
|
|
407
|
+
return ctx.chunks[i];
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return undefined;
|
|
411
|
+
}
|
|
412
|
+
/** 下载单个 chunk 到临时文件 / download one chunk to its temp file */
|
|
413
|
+
async function downloadChunkToFile(w, chunk, ctx) {
|
|
414
|
+
const stream = await openRangeStream(w.url, chunk.start, chunk.end, {
|
|
415
|
+
connectTimeout: ctx.cfg.connectTimeout,
|
|
416
|
+
timeout: ctx.cfg.timeout,
|
|
417
|
+
signal: w.controller.signal,
|
|
418
|
+
});
|
|
419
|
+
const ws = createWriteStream(chunk.tempPath, { flags: 'w' });
|
|
420
|
+
let written = 0;
|
|
421
|
+
let skipped = 0;
|
|
422
|
+
const skipBytes = stream.supportsRange ? 0 : chunk.start;
|
|
423
|
+
try {
|
|
424
|
+
await consumeBody(stream.body, async (value) => {
|
|
425
|
+
if (w.aborted)
|
|
426
|
+
throw new Error('Aborted');
|
|
427
|
+
let buffer;
|
|
428
|
+
if (skipBytes > skipped) {
|
|
429
|
+
// 服务器忽略 Range 返回全文:跳过前置字节 / server ignored Range: skip leading bytes
|
|
430
|
+
const remain = skipBytes - skipped;
|
|
431
|
+
if (value.byteLength <= remain) {
|
|
432
|
+
skipped += value.byteLength;
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
buffer = Buffer.from(value.subarray(remain));
|
|
436
|
+
skipped += remain;
|
|
437
|
+
}
|
|
438
|
+
else {
|
|
439
|
+
buffer = Buffer.from(value);
|
|
440
|
+
}
|
|
441
|
+
if (buffer.length > 0) {
|
|
442
|
+
if (!ws.write(buffer)) {
|
|
443
|
+
await new Promise((resolve) => ws.once('drain', resolve));
|
|
444
|
+
}
|
|
445
|
+
written += buffer.length;
|
|
446
|
+
}
|
|
447
|
+
}, w.controller.signal);
|
|
448
|
+
ws.end();
|
|
449
|
+
await new Promise((resolve, reject) => {
|
|
450
|
+
ws.once('finish', resolve);
|
|
451
|
+
ws.once('error', reject);
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
catch (err) {
|
|
455
|
+
ws.destroy();
|
|
456
|
+
stream.cancel();
|
|
457
|
+
try {
|
|
458
|
+
fs.unlinkSync(chunk.tempPath);
|
|
459
|
+
}
|
|
460
|
+
catch {
|
|
461
|
+
// 忽略
|
|
462
|
+
}
|
|
463
|
+
throw err;
|
|
464
|
+
}
|
|
465
|
+
// 校验 chunk 长度 / verify chunk size
|
|
466
|
+
const expected = chunk.end - chunk.start + 1;
|
|
467
|
+
if (written < expected) {
|
|
468
|
+
try {
|
|
469
|
+
fs.unlinkSync(chunk.tempPath);
|
|
470
|
+
}
|
|
471
|
+
catch {
|
|
472
|
+
// 忽略
|
|
473
|
+
}
|
|
474
|
+
throw new Error(`Chunk ${chunk.index} incomplete: ${written}/${expected} bytes / 分块不完整`);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
/** 无 Range 模式:整文件下载竞速,先完成者胜出 / no-Range mode: full-file racing */
|
|
478
|
+
async function downloadSingle(w, ctx, emit) {
|
|
479
|
+
const tempPath = path.join(path.dirname(ctx.output), getTempFileName(ctx.output, w.id, 0));
|
|
480
|
+
const stream = await openRangeStream(w.url, undefined, undefined, {
|
|
481
|
+
connectTimeout: ctx.cfg.connectTimeout,
|
|
482
|
+
timeout: ctx.cfg.timeout,
|
|
483
|
+
signal: w.controller.signal,
|
|
484
|
+
});
|
|
485
|
+
const ws = createWriteStream(tempPath, { flags: 'w' });
|
|
486
|
+
let written = 0;
|
|
487
|
+
try {
|
|
488
|
+
await consumeBody(stream.body, async (value) => {
|
|
489
|
+
if (w.aborted)
|
|
490
|
+
throw new Error('Aborted');
|
|
491
|
+
const buffer = Buffer.from(value);
|
|
492
|
+
if (!ws.write(buffer)) {
|
|
493
|
+
await new Promise((resolve) => ws.once('drain', resolve));
|
|
494
|
+
}
|
|
495
|
+
written += buffer.length;
|
|
496
|
+
}, w.controller.signal);
|
|
497
|
+
ws.end();
|
|
498
|
+
await new Promise((resolve, reject) => {
|
|
499
|
+
ws.once('finish', resolve);
|
|
500
|
+
ws.once('error', reject);
|
|
501
|
+
});
|
|
502
|
+
w.bytesWritten = written;
|
|
503
|
+
w.status = 'completed';
|
|
504
|
+
if (!ctx.done) {
|
|
505
|
+
// 竞速:最先完成者胜出,其余中止 / first completed worker wins, abort others
|
|
506
|
+
ctx.done = true;
|
|
507
|
+
ctx.singleWinnerPath = tempPath;
|
|
508
|
+
for (const other of ctx.workers) {
|
|
509
|
+
if (other !== w && !other.aborted) {
|
|
510
|
+
other.aborted = true;
|
|
511
|
+
other.status = 'failed';
|
|
512
|
+
other.controller.abort();
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
emit();
|
|
517
|
+
}
|
|
518
|
+
catch (err) {
|
|
519
|
+
ws.destroy();
|
|
520
|
+
stream.cancel();
|
|
521
|
+
throw err;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
/** 下载器类(download() 的面向对象封装)/ OO wrapper over download() */
|
|
525
|
+
export class Downloader {
|
|
526
|
+
options;
|
|
527
|
+
constructor(options) {
|
|
528
|
+
this.options = options;
|
|
529
|
+
}
|
|
530
|
+
/** 开始下载 / start download */
|
|
531
|
+
start() {
|
|
532
|
+
return download(this.options);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
/** 汇总结果 / summarize result */
|
|
536
|
+
function summarize(outPath, bytes, workers, startedAt, chunks) {
|
|
537
|
+
const sources = workers.map((w) => ({
|
|
538
|
+
id: w.id,
|
|
539
|
+
url: w.url,
|
|
540
|
+
bytes: w.bytesWritten,
|
|
541
|
+
duration: Date.now() - w.startedAt,
|
|
542
|
+
averageSpeed: (w.bytesWritten / Math.max(1, Date.now() - w.startedAt)) * 1000,
|
|
543
|
+
status: w.status,
|
|
544
|
+
}));
|
|
545
|
+
const totalBytes = chunks.reduce((s, c) => s + (c.status === 'done' ? c.size : 0), 0) || bytes;
|
|
546
|
+
const duration = Date.now() - startedAt;
|
|
547
|
+
return {
|
|
548
|
+
path: outPath,
|
|
549
|
+
bytes: totalBytes,
|
|
550
|
+
duration,
|
|
551
|
+
averageSpeed: duration > 0 ? (totalBytes / duration) * 1000 : 0,
|
|
552
|
+
sources,
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
//# sourceMappingURL=downloader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"downloader.js","sourceRoot":"","sources":["../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source probing and range chunk streaming
|
|
4
|
+
*/
|
|
5
|
+
import type { Readable } from 'node:stream';
|
|
6
|
+
import type { RangeCheckResult } from '../types.js';
|
|
7
|
+
/** 下载/探测通用选项 / Common options */
|
|
8
|
+
export interface HttpOptions {
|
|
9
|
+
/** 连接(响应头到达)超时 ms / connect timeout */
|
|
10
|
+
connectTimeout?: number;
|
|
11
|
+
/** 整体超时 ms / overall timeout */
|
|
12
|
+
timeout?: number;
|
|
13
|
+
/** 附加请求头 / extra headers */
|
|
14
|
+
headers?: Record<string, string>;
|
|
15
|
+
/** 外部取消信号 / external abort signal */
|
|
16
|
+
signal?: AbortSignal;
|
|
17
|
+
}
|
|
18
|
+
/** 打开的响应流 / Opened response stream */
|
|
19
|
+
export interface OpenedStream {
|
|
20
|
+
status: number;
|
|
21
|
+
/** 响应体长度(206 时为当前块长度)/ body length */
|
|
22
|
+
contentLength?: number;
|
|
23
|
+
/** 完整文件长度(来自 Content-Range total)/ full file length */
|
|
24
|
+
totalLength?: number;
|
|
25
|
+
/** 是否 206 分块响应 / whether server honored Range */
|
|
26
|
+
supportsRange: boolean;
|
|
27
|
+
/** 响应体(Node 流)/ response body (Node stream) */
|
|
28
|
+
body: Readable;
|
|
29
|
+
/** 立即取消 / cancel immediately */
|
|
30
|
+
cancel: () => void;
|
|
31
|
+
}
|
|
32
|
+
/** HTTP 错误(带状态码,供立即失败判定)/ HTTP error with status code */
|
|
33
|
+
export declare class HttpError extends Error {
|
|
34
|
+
readonly status: number;
|
|
35
|
+
constructor(status: number, url: string);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 探测一个源:文件大小与 Range 支持
|
|
39
|
+
* Probe a source: file size and Range support.
|
|
40
|
+
* 优先 HEAD;HEAD 拿不到大小时回退 GET Range: bytes=0-0。
|
|
41
|
+
*/
|
|
42
|
+
export declare function probe(url: string, options?: HttpOptions): Promise<RangeCheckResult>;
|
|
43
|
+
/**
|
|
44
|
+
* 打开一个分块(Range)下载流 / Open a ranged download stream
|
|
45
|
+
*
|
|
46
|
+
* 服务器忽略 Range 时返回 200 全文,由调用方负责跳过 start 字节。
|
|
47
|
+
*/
|
|
48
|
+
export declare function openRangeStream(url: string, start?: number, end?: number, options?: HttpOptions): Promise<OpenedStream>;
|
|
49
|
+
/** 流式读取响应体,逐块回调(用于写入文件与计数)/ read body chunk by chunk */
|
|
50
|
+
export declare function consumeBody(body: Readable, onChunk: (chunk: Buffer) => void | Promise<void>, signal?: AbortSignal): Promise<void>;
|
|
51
|
+
//# sourceMappingURL=http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/core/http.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpD,iCAAiC;AACjC,MAAM,WAAW,WAAW;IAC1B,uCAAuC;IACvC,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,gCAAgC;IAChC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,4BAA4B;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,qCAAqC;IACrC,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,sCAAsC;AACtC,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,sCAAsC;IACtC,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,uDAAuD;IACvD,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,iDAAiD;IACjD,aAAa,EAAE,OAAO,CAAC;IACvB,+CAA+C;IAC/C,IAAI,EAAE,QAAQ,CAAC;IACf,gCAAgC;IAChC,MAAM,EAAE,MAAM,IAAI,CAAC;CACpB;AAED,yDAAyD;AACzD,qBAAa,SAAU,SAAQ,KAAK;aACN,MAAM,EAAE,MA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