Skip to content

一分钟搭建影视站,支持Docker等部署方式

fix(download): 源站缺失分片(4xx)不再无效重试,立即失败并提示换源;失败即掐停其余 worker

bestZwei3db88c9e2f4c
diff --git a/src/lib/m3u8-downloader.test.ts b/src/lib/m3u8-downloader.test.ts
index ea85434..75139de 100644
--- a/src/lib/m3u8-downloader.test.ts
+++ b/src/lib/m3u8-downloader.test.ts
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
-import { decryptSegment } from './m3u8-downloader';
+import { decryptSegment, PauseResumeController, runDownloadJob, type DownloadJobOptions } from './m3u8-downloader';
import { parseM3u8Playlist } from './m3u8-parse';
const KEY = crypto.getRandomValues(new Uint8Array(16));
@@ -90,3 +90,115 @@ describe('parseM3u8Playlist · mediaSequence 与 AES URI 绝对化', () => {
expect(parsed.mediaSequence).toBe(0);
});
});
+
+const PLAYLIST = [
+ '#EXTM3U',
+ '#EXT-X-VERSION:3',
+ '#EXT-X-TARGETDURATION:6',
+ '#EXT-X-PLAYLIST-TYPE:VOD',
+ '#EXTINF:4,',
+ 's0.ts',
+ '#EXTINF:4,',
+ 's1.ts',
+ '#EXTINF:4,',
+ 's2.ts',
+ '#EXT-X-ENDLIST',
+].join('\n');
+
+/** 极简 Cache Storage 替身:只实现 runDownloadJob 用到的 match/put/keys/delete */
+function stubCaches(onPut?: () => Promise<void>): void {
+ const store = new Map<string, ArrayBuffer>();
+ const cache = {
+ match: async (req: Request) => {
+ const hit = store.get(req.url);
+ return hit ? new Response(hit.slice(0)) : undefined;
+ },
+ put: async (req: Request, res: Response) => {
+ if (onPut) {
+ await onPut();
+ return;
+ }
+ store.set(req.url, await res.arrayBuffer());
+ },
+ keys: async () => [...store.keys()].map((u) => new Request(u)),
+ delete: async (req: Request) => store.delete(req.url),
+ };
+ vi.stubGlobal('caches', { open: async () => cache });
+}
+
+function jobOptions(overrides: Partial<DownloadJobOptions> = {}): DownloadJobOptions {
+ return {
+ taskId: 'task-1',
+ url: 'https://cdn.example.com/index.m3u8',
+ title: '测试剧集',
+ format: 'TS',
+ concurrency: 1,
+ signal: new AbortController().signal,
+ pause: new PauseResumeController(),
+ onProgress: () => {},
+ target: { kind: 'memory', write: async () => {}, close: async () => {}, abort: async () => {} },
+ ...overrides,
+ };
+}
+
+describe('runDownloadJob · 源站缺片与存储错误', () => {
+ it('分片 404 视为确定性缺失:不重试、立即失败并提示换源', async () => {
+ stubCaches();
+ const calls: string[] = [];
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(async (input: RequestInfo | URL) => {
+ const url = String(input);
+ calls.push(url);
+ if (url.endsWith('index.m3u8')) return new Response(PLAYLIST, { status: 200 });
+ if (url.endsWith('s1.ts')) return new Response('', { status: 404 });
+ return new Response(new Uint8Array([0x47, 0x40, 0x11, 0x10]), { status: 200 });
+ })
+ );
+
+ const job = runDownloadJob(jobOptions());
+ await expect(job).rejects.toThrow(/分片 2 源站缺失/);
+ await expect(job).rejects.toThrow(/建议更换来源/);
+ // 不重试:m3u8 一次 + s0 一次 + s1 一次(整集失败后 s2 不再请求)
+ expect(calls.filter((u) => u.endsWith('.ts'))).toEqual([
+ 'https://cdn.example.com/s0.ts',
+ 'https://cdn.example.com/s1.ts',
+ ]);
+ });
+
+ it('5xx 仍走退避重试,重试耗尽后才失败', async () => {
+ stubCaches();
+ vi.useFakeTimers();
+ const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
+ const url = String(input);
+ if (url.endsWith('index.m3u8')) return new Response(PLAYLIST, { status: 200 });
+ return new Response('', { status: 503 });
+ });
+ vi.stubGlobal('fetch', fetchMock);
+
+ const job = runDownloadJob(jobOptions());
+ const assertion = expect(job).rejects.toThrow(/已重试 3 次/);
+ await vi.advanceTimersByTimeAsync(10_000);
+ await assertion;
+
+ const segCalls = fetchMock.mock.calls.filter(([input]) => String(input).endsWith('.ts'));
+ expect(segCalls).toHaveLength(3);
+ vi.useRealTimers();
+ });
+
+ it('缓存写入失败(如配额不足)时给出可读提示', async () => {
+ stubCaches(async () => {
+ throw new DOMException('Quota exceeded', 'QuotaExceededError');
+ });
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(async (input: RequestInfo | URL) => {
+ const url = String(input);
+ if (url.endsWith('index.m3u8')) return new Response(PLAYLIST, { status: 200 });
+ return new Response(new Uint8Array([0x47]), { status: 200 });
+ })
+ );
+
+ await expect(runDownloadJob(jobOptions())).rejects.toThrow(/存储空间可能不足/);
+ });
+});
diff --git a/src/lib/m3u8-downloader.ts b/src/lib/m3u8-downloader.ts
index 79bfcc7..74c2871 100644
--- a/src/lib/m3u8-downloader.ts
+++ b/src/lib/m3u8-downloader.ts
@@ -76,9 +76,28 @@ export async function clearDownloadChunks(taskId: string): Promise<void> {
} catch { /* 忽略 */ }
}
+/**
+ * 分片取回失败。retryable=false 表示确定性失败——源站根本没有这个分片(404/410)。
+ * 这类缺失在采集站很常见:m3u8 完整列出了整集,但服务器上的分片是转码/上传
+ * 不完整的产物。对它重试只会白等(3 次退避共 6 秒),而且最终必然失败——
+ * 播放侧 hls.js 会直接跳过这类分片,所以表现为「能播、下不了」。
+ */
+class SegmentFetchError extends Error {
+ constructor(
+ message: string,
+ readonly retryable: boolean
+ ) {
+ super(message);
+ }
+}
+
async function fetchSegment(url: string, signal: AbortSignal): Promise<ArrayBuffer> {
const res = await fetch(url, { signal: AbortSignal.any([AbortSignal.timeout(SEGMENT_TIMEOUT_MS), signal]), headers: { Accept: '*/*' } });
- if (!res.ok) throw new Error(`分片 HTTP ${res.status}`);
+ if (!res.ok) {
+ // 5xx / 408 / 429 多为源站抖动或限流,值得退避重试;其余 4xx 视为确定性缺失
+ const retryable = res.status >= 500 || res.status === 408 || res.status === 429;
+ throw new SegmentFetchError(`HTTP ${res.status}`, retryable);
+ }
return res.arrayBuffer();
}
@@ -135,6 +154,12 @@ export async function runDownloadJob(opts: DownloadJobOptions): Promise<void> {
const { taskId, url, format, signal, pause, onProgress, target } = opts;
const concurrency = Math.min(Math.max(opts.concurrency ?? 8, 1), 16);
+ // 内部硬停控制器:任一 worker 判失败就掐掉其余 worker。否则 Promise.all 已经
+ // reject、任务早已标记失败,后台 worker 还在继续请求分片耗流量。与外部 signal
+ // 组合后,用户取消(外部 abort)的语义保持不变。
+ const hardStop = new AbortController();
+ const jobSignal = AbortSignal.any([signal, hardStop.signal]);
+
const report = (current: number, total: number, status: DownloadProgress['status'], message?: string) => {
onProgress({
current,
@@ -168,7 +193,7 @@ export async function runDownloadJob(opts: DownloadJobOptions): Promise<void> {
// —— AES key 预取 ——
let aesKey: ArrayBuffer | undefined;
if (parsed.aesConf) {
- const res = await fetch(parsed.aesConf.uri, { signal });
+ const res = await fetch(parsed.aesConf.uri, { signal: jobSignal });
if (!res.ok) throw new Error(`解密密钥加载失败:HTTP ${res.status}`);
aesKey = await res.arrayBuffer();
}
@@ -186,10 +211,10 @@ export async function runDownloadJob(opts: DownloadJobOptions): Promise<void> {
const worker = async (): Promise<void> => {
while (cursor < total) {
- if (signal.aborted) return;
+ if (jobSignal.aborted) return;
await pause.waitIfPaused();
const index = ++cursor; // 1 基
- if (signal.aborted) return;
+ if (jobSignal.aborted) return;
await pause.waitIfPaused();
const cached = await cache.match(new Request(chunkKey(index)));
if (cached) {
@@ -199,28 +224,46 @@ export async function runDownloadJob(opts: DownloadJobOptions): Promise<void> {
const seg = parsed.segments[index - 1];
let buffer: ArrayBuffer | undefined;
for (let retry = 0; retry < MAX_SEGMENT_RETRIES; retry++) {
- if (signal.aborted) return;
+ if (jobSignal.aborted) return;
try {
- buffer = await fetchSegment(seg.url, signal);
+ buffer = await fetchSegment(seg.url, jobSignal);
break;
- } catch {
- if (signal.aborted) return;
+ } catch (err) {
+ if (jobSignal.aborted) return;
+ // 源站没有这片(404/410 等 4xx):重试再多次也是同样结果。立刻整集失败
+ // 并把原因说清楚——用户需要的是「换源」,不是白等 6 秒后一句笼统的失败
+ if (err instanceof SegmentFetchError && !err.retryable) {
+ throw new Error(`分片 ${index} 源站缺失(${err.message}),该来源无法完整下载,建议更换来源`);
+ }
await new Promise((r) => setTimeout(r, 1000 * (retry + 1)));
}
}
if (!buffer) {
throw new Error(`分片 ${index} 下载失败(已重试 ${MAX_SEGMENT_RETRIES} 次)`);
}
- await cache.put(
- new Request(chunkKey(index)),
- new Response(buffer.slice(0), { headers: { 'Content-Type': 'video/mp2t' } })
- );
+ try {
+ await cache.put(
+ new Request(chunkKey(index)),
+ new Response(buffer.slice(0), { headers: { 'Content-Type': 'video/mp2t' } })
+ );
+ } catch (err) {
+ // 配额不足等存储层错误:浏览器原文(QuotaExceededError)用户看不懂
+ throw new Error(
+ `分片缓存写入失败(浏览器存储空间可能不足):${err instanceof Error ? err.message : String(err)}`
+ );
+ }
current += 1;
report(current, total, 'downloading');
}
};
- await Promise.all(Array.from({ length: concurrency }, worker));
- if (signal.aborted) return;
+ try {
+ await Promise.all(Array.from({ length: concurrency }, worker));
+ } catch (err) {
+ // 先把在途的其它 worker 掐掉再向上抛:任务已判失败,不该继续耗流量
+ hardStop.abort();
+ throw err;
+ }
+ if (jobSignal.aborted) return;
// —— 顺序写出(转封装 / 拼接) ——
report(total, total, 'processing');
@@ -229,7 +272,7 @@ export async function runDownloadJob(opts: DownloadJobOptions): Promise<void> {
// → 播放器显示 13:15:21),必须在写出 initSegment 前回填真实值
const transmuxer = new StreamingTransmuxer(target, { durationSeconds: parsed.totalDuration });
for (let i = 1; i <= total; i++) {
- if (signal.aborted) return;
+ if (jobSignal.aborted) return;
await pause.waitIfPaused();
const resp = await cache.match(new Request(chunkKey(i)));
if (!resp) throw new Error(`分片 ${i} 缓存丢失,请重试`);
@@ -242,7 +285,7 @@ export async function runDownloadJob(opts: DownloadJobOptions): Promise<void> {
await transmuxer.finish();
} else {
for (let i = 1; i <= total; i++) {
- if (signal.aborted) return;
+ if (jobSignal.aborted) return;
await pause.waitIfPaused();
const resp = await cache.match(new Request(chunkKey(i)));
if (!resp) throw new Error(`分片 ${i} 缓存丢失,请重试`);