dsh-folia 4.25.0 → 4.25.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/lib/client.js +192 -26
- package/lib/download-queue.js +403 -43
- package/lib/routes/downloads.js +104 -1
- package/lib/routes/inventory.js +30 -4
- package/package.json +29 -28
package/lib/download-queue.js
CHANGED
|
@@ -144,6 +144,16 @@ function isEnoentError(e) {
|
|
|
144
144
|
*/
|
|
145
145
|
const KIND_LIMITS = { skin: 2, wallpaper: 2, pet: 1, boot: 1, default: 2 };
|
|
146
146
|
|
|
147
|
+
/**
|
|
148
|
+
* 4.25.1(连点语义):**未开始队列上限** = 8。
|
|
149
|
+
*
|
|
150
|
+
* 用户一次快速连点 30 张时,前 8 张之外不再堆进 queued(它们会显示「排队中」、占满队列位);
|
|
151
|
+
* 超出部分回落到 `pending`(未缓存、不下载、不显示排队位置),用户再点它时立刻拉回队列。
|
|
152
|
+
* `wallpaper` 的**同时下载**上限仍是 `KIND_LIMITS.wallpaper = 2` —— 这两个数不是一回事:
|
|
153
|
+
* 前者约束"等待队列长度",后者才是"并行下载数"。
|
|
154
|
+
*/
|
|
155
|
+
const BURST_QUEUE_MAX = 8;
|
|
156
|
+
|
|
147
157
|
/**
|
|
148
158
|
* 优先级:**同一 kind 内部**决定谁先被取。
|
|
149
159
|
* high = 用户主动点击(安装皮肤 / 手动下载)
|
|
@@ -230,6 +240,49 @@ function urlKey(url) {
|
|
|
230
240
|
return createHash('sha256').update(String(url)).digest('hex').slice(0, 32);
|
|
231
241
|
}
|
|
232
242
|
|
|
243
|
+
/* ── 4.25.1:**条目级身份**(itemKey)──────────────────────────────────────────
|
|
244
|
+
*
|
|
245
|
+
* 真实故障(用户实测 4.25.0):同一张壁纸先按变体 URL(`.1080p.mp4` / `.30fps.mp4`)
|
|
246
|
+
* 入队,后来清单换回原始 URL 再点 ⇒ `add()` 用 URL 做键,于是又建了一条新 job;
|
|
247
|
+
* 旧变体 job 永远停在 `pending`,界面上「缓存中」却没有百分比。
|
|
248
|
+
*
|
|
249
|
+
* 这里引入**条目级键**:一个条目(壁纸)只有一条 job,URL 只是这条 job 当前要下的对象。
|
|
250
|
+
* 客户端从清单条目 id 生成 `wallpaper:<id>`;老客户端/老数据没有 itemKey 时用同一媒体的
|
|
251
|
+
* **族键**(去掉 `1080p` / `30fps` / `4k30` 这类变体后缀)兜底 —— 原画与变体天然同族。
|
|
252
|
+
*/
|
|
253
|
+
const ITEM_MEDIA_EXT_RE = /\.(?:mp4|webm|mov|m4v|mkv|avi|jpg|jpeg|png|webp|gif|avif|zip)$/i;
|
|
254
|
+
const ITEM_VARIANT_SUFFIX_RE = /(?:[._-](?:1080p|1440p|2160p|4k30|4k|30fps|60fps|h264|hevc|av1|transcoded))+$/i;
|
|
255
|
+
|
|
256
|
+
function normalizeItemKey(raw) {
|
|
257
|
+
return String(raw == null ? '' : raw).trim();
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** 同一媒体的族键:忽略变体后缀与查询串;解析失败返回空串(调用方回落到 urlKey)。 */
|
|
261
|
+
function mediaFamilyKey(rawUrl) {
|
|
262
|
+
try {
|
|
263
|
+
const u = new URL(String(rawUrl));
|
|
264
|
+
const parts = u.pathname.split('/');
|
|
265
|
+
const file = parts.pop() || '';
|
|
266
|
+
let stem = file.replace(ITEM_MEDIA_EXT_RE, '');
|
|
267
|
+
stem = stem.replace(ITEM_VARIANT_SUFFIX_RE, '');
|
|
268
|
+
const dir = parts.filter(Boolean).join('/');
|
|
269
|
+
return u.origin + '/' + (dir ? dir + '/' : '') + stem;
|
|
270
|
+
} catch { return ''; }
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** 没有显式 itemKey 时的稳定默认键:同 kind + 同族媒体 ⇒ 同一条目。 */
|
|
274
|
+
function defaultItemKey(url, kind) {
|
|
275
|
+
const k = String(kind || 'media').trim().toLowerCase() || 'media';
|
|
276
|
+
const fam = mediaFamilyKey(url);
|
|
277
|
+
return fam ? k + ':url:' + fam : k + ':' + urlKey(String(url || ''));
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** 这条 job 的 itemKey 是否只是"按 URL 族推出来的默认键"(不是清单条目的稳定 id)。 */
|
|
281
|
+
function isDefaultItemKey(job) {
|
|
282
|
+
return !normalizeItemKey(job && job.itemKey)
|
|
283
|
+
|| normalizeItemKey(job.itemKey) === defaultItemKey(job && job.url, job && job.kind);
|
|
284
|
+
}
|
|
285
|
+
|
|
233
286
|
/**
|
|
234
287
|
* 解析 `Content-Range: bytes <start>-<end>/<total>`(`total` 允许是 `*`)。
|
|
235
288
|
*
|
|
@@ -349,6 +402,13 @@ export function createDownloadQueue(opts = {}) {
|
|
|
349
402
|
* 的单调序号:`boostSeq` 越大越靠前(只影响同优先级内,high 永远在 normal/low 之前)。
|
|
350
403
|
*/
|
|
351
404
|
let boostCounter = 0;
|
|
405
|
+
/**
|
|
406
|
+
* 4.25.1(连点语义):**"本次会话第几次被用户点击"**的单调序号。
|
|
407
|
+
*
|
|
408
|
+
* 不是 `boostSeq`:当前在播的 silent promote 也会用 high/boostSeq,但它不是"用户又点了一张",
|
|
409
|
+
* 不该占连点队列的名额。只有显式 `clicked:true`(用户点击)才记这里。
|
|
410
|
+
*/
|
|
411
|
+
let clickCounter = 0;
|
|
352
412
|
/**
|
|
353
413
|
* 队列级暂停(4.23.18,面板「暂停全部下载」)。
|
|
354
414
|
*
|
|
@@ -490,6 +550,8 @@ export function createDownloadQueue(opts = {}) {
|
|
|
490
550
|
const list = [...jobs.values()].map((j) => ({
|
|
491
551
|
id: j.id, url: j.url, dest: j.dest, kind: j.kind, total: j.total,
|
|
492
552
|
status: j.status, error: j.error || null, at: j.at,
|
|
553
|
+
// 4.25.1:条目级身份跟着落盘 —— 重启后同一条目的原画/变体仍是同一条 job。
|
|
554
|
+
itemKey: normalizeItemKey(j.itemKey),
|
|
493
555
|
// 4.23.2:优先级必须落盘,否则重启后"用户点击"会退化成后台预取,优先级形同虚设。
|
|
494
556
|
priority: normalizePriority(j.priority),
|
|
495
557
|
// 4.23.3:`mode` / `autoSwitched` 也落盘 —— 面板与排障读的是"这条当时怎么下的"。
|
|
@@ -562,6 +624,9 @@ export function createDownloadQueue(opts = {}) {
|
|
|
562
624
|
seqCounter += 1;
|
|
563
625
|
jobs.set(j.id, {
|
|
564
626
|
id: j.id, url: j.url, dest: j.dest, kind: j.kind || '', total: Number(j.total) || 0,
|
|
627
|
+
// 4.25.1:旧 downloads.json 没有 itemKey ⇒ 按媒体族键补一个(`x.1080p.mp4` 与
|
|
628
|
+
// `x.mp4` 同族);之后用户点击时 `add()` 还会把它升级成清单条目的稳定 id。
|
|
629
|
+
itemKey: normalizeItemKey(j.itemKey) || defaultItemKey(j.url, j.kind),
|
|
565
630
|
received: status === 'done' ? Number(j.total) || 0 : 0,
|
|
566
631
|
status, error: status === 'failed' ? (j.error || null) : null, at: Number(j.at) || Date.now(),
|
|
567
632
|
// 旧 jobs.json 没有 priority ⇒ `normal`(不是崩、也不是全员 high)。
|
|
@@ -590,6 +655,9 @@ export function createDownloadQueue(opts = {}) {
|
|
|
590
655
|
hashRetry: false, singleOnly: false,
|
|
591
656
|
// 4.23.18:提权序号只在**本会话**有意义(重启后当前在播会重新 promote)。
|
|
592
657
|
boostSeq: 0,
|
|
658
|
+
// 4.25.1(连点语义):重启后"上次点击"不再算本次会话的连点名额;pending+high 仍由
|
|
659
|
+
// 启动自愈显示「未缓存(点击继续)」。
|
|
660
|
+
sessionClicked: false, clickSeq: 0,
|
|
593
661
|
pauseRequested: false,
|
|
594
662
|
seq: seqCounter,
|
|
595
663
|
});
|
|
@@ -749,6 +817,13 @@ export function createDownloadQueue(opts = {}) {
|
|
|
749
817
|
id: j.id, url: j.url, kind: j.kind, dest: j.dest, total: j.total, received: j.received,
|
|
750
818
|
status: j.status, error: j.error || null,
|
|
751
819
|
priority: normalizePriority(j.priority),
|
|
820
|
+
// 4.25.1:条目级身份(客户端按它绑进度,不再拿 URL 当身份)+ 点击未完成的标记。
|
|
821
|
+
// `pendingHigh` 只用于 UI 文案(「未缓存(点击继续)」),**不代表可自动下载**。
|
|
822
|
+
itemKey: normalizeItemKey(j.itemKey),
|
|
823
|
+
pendingHigh: j.status === 'pending' && normalizePriority(j.priority) === 'high',
|
|
824
|
+
// 4.25.1(连点语义):本次会话是否被用户点过、第几次点(排障与 U1–U4 判据读它)。
|
|
825
|
+
sessionClicked: j.sessionClicked === true,
|
|
826
|
+
clickSeq: Number(j.clickSeq) || 0,
|
|
752
827
|
// 4.23.3:这条 job **正在/曾经**怎么下的 —— `single` 或 `ranged:<n>`;
|
|
753
828
|
// `autoSwitched` 只对 auto 模式成立(= 单流跑着跑着切成了多连接)。
|
|
754
829
|
mode: jobMode(j),
|
|
@@ -793,27 +868,229 @@ export function createDownloadQueue(opts = {}) {
|
|
|
793
868
|
}
|
|
794
869
|
|
|
795
870
|
/**
|
|
796
|
-
*
|
|
871
|
+
* 找一条 job 的**条目身份**(不写盘、不 pump):add / setPriority / inventory 共用。
|
|
872
|
+
*
|
|
873
|
+
* 顺序(从最强到最弱):
|
|
874
|
+
* ① 显式 job id(老调用方只有 URL → id 就是 urlKey);
|
|
875
|
+
* ② 当前 URL 的 urlKey(同 URL 的幂等路径,id 语义不变);
|
|
876
|
+
* ③ itemKey(4.25.1 的条目级身份,解决"原始 URL vs 变体 URL");
|
|
877
|
+
* ④ relatedUrls(清单里的变体地址,精确命中);
|
|
878
|
+
* ⑤ 媒体族键(老 downloads.json 没有 itemKey 时的兜底:`x.1080p.mp4` 与 `x.mp4` 同族)。
|
|
879
|
+
*
|
|
880
|
+
* @param {{id?:string,url?:string,kind?:string,itemKey?:string,relatedUrls?:string[]}|string} inp
|
|
881
|
+
* @returns {any|null}
|
|
882
|
+
*/
|
|
883
|
+
function findJobFor(inp) {
|
|
884
|
+
const o = typeof inp === 'string' ? { url: inp } : (inp || {});
|
|
885
|
+
const url = String(o.url || '');
|
|
886
|
+
const kind = o.kind ? String(o.kind).trim().toLowerCase() : '';
|
|
887
|
+
const itemKey = normalizeItemKey(o.itemKey);
|
|
888
|
+
if (o.id) {
|
|
889
|
+
const byId = jobs.get(String(o.id));
|
|
890
|
+
if (byId) return byId;
|
|
891
|
+
}
|
|
892
|
+
if (url) {
|
|
893
|
+
const byUrl = jobs.get(urlKey(url));
|
|
894
|
+
if (byUrl) return byUrl;
|
|
895
|
+
}
|
|
896
|
+
if (itemKey) {
|
|
897
|
+
for (const j of jobs.values()) {
|
|
898
|
+
if (normalizeItemKey(j.itemKey) !== itemKey) continue;
|
|
899
|
+
if (kind && kindOf(j) !== kind) continue;
|
|
900
|
+
return j;
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
const related = new Set();
|
|
904
|
+
if (url) related.add(url);
|
|
905
|
+
for (const u of (Array.isArray(o.relatedUrls) ? o.relatedUrls : [])) {
|
|
906
|
+
const s = String(u || '').trim();
|
|
907
|
+
if (s) related.add(s);
|
|
908
|
+
}
|
|
909
|
+
if (related.size) {
|
|
910
|
+
for (const j of jobs.values()) {
|
|
911
|
+
if (related.has(String(j.url))) return j;
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
const fam = url ? mediaFamilyKey(url) : '';
|
|
915
|
+
if (fam) {
|
|
916
|
+
for (const j of jobs.values()) {
|
|
917
|
+
if (kind && kindOf(j) !== kind) continue;
|
|
918
|
+
// 显式不同的条目键不许被"同族"吞并;只有老记录(空键 / URL 默认键)才吃族兜底。
|
|
919
|
+
if (itemKey && !isDefaultItemKey(j) && normalizeItemKey(j.itemKey) !== itemKey) continue;
|
|
920
|
+
if (mediaFamilyKey(j.url) === fam) return j;
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
return null;
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
/**
|
|
927
|
+
* 把一条 job 换到另一个 URL / 落点(**同一条目换档**),保留 job id 与条目身份。
|
|
928
|
+
*
|
|
929
|
+
* 只在"没有在跑"时调用:正在下的对象不能中途换,否则已写进 `.part` 的字节会与新 URL 拼在
|
|
930
|
+
* 一起。已下字节 / 上次校验结论都属于旧对象 ⇒ 全部清零,下一次 pump 从 0 开始下新对象。
|
|
931
|
+
*
|
|
932
|
+
* @param {any} job
|
|
933
|
+
* @param {string} url
|
|
934
|
+
* @param {string} dest
|
|
935
|
+
* @returns {boolean}
|
|
936
|
+
*/
|
|
937
|
+
function relocateJob(job, url, dest) {
|
|
938
|
+
if (String(job.url) === String(url) && String(job.dest) === String(dest)) return false;
|
|
939
|
+
try { rmSync(String(job.dest || '') + '.part', { force: true }); } catch { /* 删不掉也不影响换档 */ }
|
|
940
|
+
setJob(job, {
|
|
941
|
+
url: String(url), dest: String(dest), total: 0, received: 0, error: null,
|
|
942
|
+
// 旧对象的校验结论不能留给新对象。
|
|
943
|
+
verified: null, verifiedSha: '', verifiedBytes: 0, verifiedAt: 0,
|
|
944
|
+
verifiedSource: null, verifyReason: null, verifyDetail: null,
|
|
945
|
+
decodeUnsupported: false, hashRetry: false, singleOnly: false, enoentRetries: 0,
|
|
946
|
+
mode: 'single', autoSwitched: false, range416Retry: false,
|
|
947
|
+
// 完成的旧对象 → 新对象还没下;其余状态由 add() 继续归一(pending/failed → queued)。
|
|
948
|
+
status: job.status === 'done' ? 'queued' : job.status,
|
|
949
|
+
});
|
|
950
|
+
return true;
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
/**
|
|
954
|
+
* 同一条目**正在跑**的 job(4.25.1):点击时优先复用它,避免"旧变体还在下、又新建一条原画"。
|
|
955
|
+
* @param {{url?:string,kind?:string,itemKey?:string}} inp
|
|
956
|
+
* @returns {any|null}
|
|
957
|
+
*/
|
|
958
|
+
function findRunningForItem(inp) {
|
|
959
|
+
const o = inp || {};
|
|
960
|
+
const itemKey = normalizeItemKey(o.itemKey);
|
|
961
|
+
const kind = o.kind ? String(o.kind).trim().toLowerCase() : '';
|
|
962
|
+
const fam = mediaFamilyKey(o.url);
|
|
963
|
+
for (const j of jobs.values()) {
|
|
964
|
+
if (j.status !== 'running') continue;
|
|
965
|
+
if (kind && kindOf(j) !== kind) continue;
|
|
966
|
+
if (itemKey && normalizeItemKey(j.itemKey) === itemKey) return j;
|
|
967
|
+
if (itemKey && !isDefaultItemKey(j) && normalizeItemKey(j.itemKey) !== itemKey) continue;
|
|
968
|
+
if (fam && mediaFamilyKey(j.url) === fam) return j;
|
|
969
|
+
}
|
|
970
|
+
return null;
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
/**
|
|
974
|
+
* **清掉同一条目上永远不会被调度的重复记录**(4.25.1)。
|
|
975
|
+
*
|
|
976
|
+
* 4.25.0 的现场可能同时留着"原画 job + 变体 pending job"(旧实现按 URL 建键)。
|
|
977
|
+
* 用户点击其中一条后,另一条 pending 永远不会被 pump 取到,却会一直算进 `pendingCount`。
|
|
978
|
+
* 这里只删**非 running / 非 done** 的重复项:有效的缓存与正在跑的下载都不动。
|
|
979
|
+
*/
|
|
980
|
+
function dropStaleItemDuplicates(canonical, inp) {
|
|
981
|
+
const o = inp || {};
|
|
982
|
+
const itemKey = normalizeItemKey(o.itemKey);
|
|
983
|
+
const kind = o.kind ? String(o.kind).trim().toLowerCase() : '';
|
|
984
|
+
const fam = mediaFamilyKey(canonical && canonical.url) || mediaFamilyKey(o.url);
|
|
985
|
+
let removed = 0;
|
|
986
|
+
for (const [jid, j] of [...jobs.entries()]) {
|
|
987
|
+
if (j === canonical) continue;
|
|
988
|
+
if (kind && kindOf(j) !== kind) continue;
|
|
989
|
+
const sameKey = itemKey && normalizeItemKey(j.itemKey) === itemKey;
|
|
990
|
+
const sameFam = fam && mediaFamilyKey(j.url) === fam
|
|
991
|
+
&& (!itemKey || isDefaultItemKey(j) || normalizeItemKey(j.itemKey) === itemKey);
|
|
992
|
+
if (!sameKey && !sameFam) continue;
|
|
993
|
+
if (j.status === 'running' || j.status === 'done') continue;
|
|
994
|
+
jobs.delete(jid);
|
|
995
|
+
removed += 1;
|
|
996
|
+
}
|
|
997
|
+
if (removed) log('download-queue: dropped ' + removed + ' stale duplicate job(s) for item '
|
|
998
|
+
+ (itemKey || fam || canonical.id));
|
|
999
|
+
return removed;
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
/**
|
|
1003
|
+
* 4.25.1(连点语义):**未开始队列有界**。
|
|
1004
|
+
*
|
|
1005
|
+
* 只约束 `queued` 里的 **wallpaper**(等待队列),不动 `running`(并行下载最多仍是 kind limit,
|
|
1006
|
+
* wallpaper=2):皮肤/桌宠/开机动画的安装队列不受这条影响。
|
|
1007
|
+
* · 用户连点 30 张 ⇒ 2 张在跑 + 最近点击的 8 张 queued + 其余 20 张 pending;
|
|
1008
|
+
* · 超过 8 的按 `clickSeq`(**最近点击**优先)淘汰回 `pending`;非点击的预取排在更后;
|
|
1009
|
+
* · 回落 pending 不取消、不删 `.part`;用户再点一次时 `add()` 会把它拉回队首。
|
|
1010
|
+
*
|
|
1011
|
+
* @returns {number} 被回落 pending 的条数
|
|
1012
|
+
*/
|
|
1013
|
+
function enforceBurstCap() {
|
|
1014
|
+
// 只约束 wallpaper:这条语义来自"用户连点壁纸库",皮肤/桌宠/开机动画的安装队列不走这里。
|
|
1015
|
+
const queued = [...jobs.values()].filter((j) => j.status === 'queued' && kindOf(j) === 'wallpaper');
|
|
1016
|
+
if (queued.length <= BURST_QUEUE_MAX) return 0;
|
|
1017
|
+
queued.sort((a, b) => (Number(b.clickSeq) || 0) - (Number(a.clickSeq) || 0)
|
|
1018
|
+
|| (Number(b.boostSeq) || 0) - (Number(a.boostSeq) || 0)
|
|
1019
|
+
|| (Number(b.at) || 0) - (Number(a.at) || 0)
|
|
1020
|
+
|| (Number(b.seq) || 0) - (Number(a.seq) || 0));
|
|
1021
|
+
let demoted = 0;
|
|
1022
|
+
for (let i = BURST_QUEUE_MAX; i < queued.length; i += 1) {
|
|
1023
|
+
setJob(queued[i], { status: 'pending', error: null, boostSeq: 0, pauseRequested: false });
|
|
1024
|
+
demoted += 1;
|
|
1025
|
+
}
|
|
1026
|
+
if (demoted) {
|
|
1027
|
+
log('download-queue: burst cap ' + queued.length + ' queued → 回落 ' + demoted
|
|
1028
|
+
+ ' 条 pending(只保留最近点击的 ' + BURST_QUEUE_MAX + ' 条在队列)');
|
|
1029
|
+
}
|
|
1030
|
+
return demoted;
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
/**
|
|
1034
|
+
* 入队(幂等,**按条目**)。返回 job 快照;`existed` 表示此前已存在。
|
|
797
1035
|
*
|
|
798
1036
|
* 4.23.22:**只记录"要下哪个 URL + kind + 选档"**。sha256 / bytes / hashSource 不再是
|
|
799
1037
|
* 入队参数 —— 它们要在下载完成那一刻去问当前清单(见 `verifyCompletedFile`)。
|
|
1038
|
+
*
|
|
1039
|
+
* 4.25.1:同一条目(itemKey / 同族 URL)再次入队 ⇒ **复用同一条 job**;若新 URL 与旧的不同
|
|
1040
|
+
* (原画 ↔ 1080p / 30fps 变体)且当前没有在跑,就把这条 job 迁移到新 URL,绝不另建第二条
|
|
1041
|
+
* 永远 pending 的记录。`forceReload` 用于"用户显式换档(用原画)":即使旧档已 done 也迁移。
|
|
800
1042
|
*/
|
|
801
|
-
function add({
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
const
|
|
1043
|
+
function add({
|
|
1044
|
+
url, dest, kind = '', priority = 'normal', chosenVariant = 'original',
|
|
1045
|
+
itemKey = '', relatedUrls = [], forceReload = false, clicked = false,
|
|
1046
|
+
}) {
|
|
1047
|
+
const wantKey = normalizeItemKey(itemKey) || defaultItemKey(url, kind);
|
|
1048
|
+
let existing = findJobFor({ url, kind, itemKey: wantKey, relatedUrls });
|
|
1049
|
+
// 同一条目已有在跑的下载 ⇒ 复用它(旧变体在跑时点原画,不该再并排开第二条)。
|
|
1050
|
+
const runner = findRunningForItem({ url, kind, itemKey: wantKey });
|
|
1051
|
+
if (runner && runner !== existing) existing = runner;
|
|
1052
|
+
const existed = Boolean(existing);
|
|
806
1053
|
if (existed) {
|
|
807
|
-
const job =
|
|
1054
|
+
const job = existing;
|
|
1055
|
+
const id = job.id;
|
|
808
1056
|
// 同一 URL 被**更高优先级**重新入队 ⇒ 提权。语义:用户主动点击(high)应该插到
|
|
809
1057
|
// 后台预取(low)前面;反过来(low 重复入队)**不降级**,免得后台请求把用户刚点的安装降下去。
|
|
810
|
-
if (rankOf({ priority:
|
|
811
|
-
|
|
1058
|
+
if (rankOf({ priority: normalizePriority(priority) }) > rankOf(job)) {
|
|
1059
|
+
setJob(job, { priority: normalizePriority(priority) });
|
|
1060
|
+
}
|
|
1061
|
+
// 老 job(4.25.0 及以前)没有 itemKey:这里补上,下一次重启也能按条目去重。
|
|
1062
|
+
// 显式带来的条目键优先于加载时按 URL 族的默认键(否则 legacy 记录永远升不到条目身份)。
|
|
1063
|
+
const explicitKey = normalizeItemKey(itemKey);
|
|
1064
|
+
if (explicitKey && normalizeItemKey(job.itemKey) !== explicitKey) {
|
|
1065
|
+
setJob(job, { itemKey: explicitKey });
|
|
1066
|
+
} else if (!normalizeItemKey(job.itemKey)) {
|
|
1067
|
+
setJob(job, { itemKey: wantKey });
|
|
1068
|
+
}
|
|
1069
|
+
const wantUrl = String(url);
|
|
1070
|
+
const urlChanged = String(job.url) !== wantUrl;
|
|
1071
|
+
let keptOldUrl = false;
|
|
1072
|
+
if (urlChanged) {
|
|
1073
|
+
// 正在跑的 job 不换 URL(已下载字节属于当前对象);已完成的旧档默认也复用
|
|
1074
|
+
//(同一张壁纸已经缓存了,点它不该再下一遍)。只有显式换档才迁移。
|
|
1075
|
+
const canRelocate = job.status !== 'running'
|
|
1076
|
+
&& (forceReload === true || job.status !== 'done' || !existsSync(job.dest));
|
|
1077
|
+
if (canRelocate) relocateJob(job, wantUrl, dest);
|
|
1078
|
+
else if (forceReload === true && job.status === 'running') {
|
|
1079
|
+
// 用户显式换档(用原画)时正在下变体:先 abort,等 runOne 收尾后再迁移。
|
|
1080
|
+
// 在流的半途换 url/dest 会把两段字节写进同一个 `.part` —— 必须等它停稳。
|
|
1081
|
+
job.replaceOnAbort = { url: wantUrl, dest };
|
|
1082
|
+
try { if (job.ctrl) job.ctrl.abort(); } catch { /* ignore */ }
|
|
1083
|
+
} else {
|
|
1084
|
+
// 复用的是旧档(变体已缓存 / 正在跑):chosenVariant 必须描述**实际 URL**,
|
|
1085
|
+
// 不能把响应里这次没采纳的选档写上去(否则列表说原画、URL 还是变体)。
|
|
1086
|
+
keptOldUrl = true;
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
812
1089
|
// 4.23.4:同一条 URL 再入队时选档结论以**这次**为准(用户可能刚点过「用原画」)。
|
|
813
|
-
|
|
1090
|
+
if (!keptOldUrl) {
|
|
1091
|
+
setJob(job, { chosenVariant: chosenVariant === '1080p' || chosenVariant === '4k30' ? chosenVariant : 'original' });
|
|
1092
|
+
}
|
|
814
1093
|
// 已完成但文件被删 ⇒ 重新排队(4.23.17:这也是"启动后发现成品缺失"的运行时版本)。
|
|
815
|
-
// 4.23.18:启动时这种记录是 `pending`(不调度);用户点击 / 预取 / 当前在播的 `add()`
|
|
816
|
-
// 到这里才把它变回 queued —— 这是三条允许的自动入队路径之一。
|
|
817
1094
|
if (job.status === 'done' && !existsSync(job.dest)) {
|
|
818
1095
|
setJob(job, { status: 'queued', received: 0, error: null, enoentRetries: 0 });
|
|
819
1096
|
}
|
|
@@ -821,44 +1098,47 @@ export function createDownloadQueue(opts = {}) {
|
|
|
821
1098
|
// 才变回 queued。这不是"批量重置"—— 一次 add 只动这一条。
|
|
822
1099
|
if (job.status === 'pending') {
|
|
823
1100
|
setJob(job, { status: 'queued', error: null, received: 0, enoentRetries: 0 });
|
|
824
|
-
}
|
|
825
|
-
//
|
|
826
|
-
// 用户再点安装 → add 返回同一个 failed job → 安装立刻又报错(看起来"怎么点都下不来")。
|
|
827
|
-
// 实测症状:`error:"fetch failed"` 之后每次重试都秒失败。
|
|
828
|
-
// `.part` 保留 ⇒ 重置后再下载会从已下载字节续传,不浪费流量。
|
|
829
|
-
// 4.23.7:同时清掉 `hashRetry` / `singleOnly` —— 用户主动再点一次是**新的**一轮,
|
|
830
|
-
// 它同样值得一次"自动单流重下"的机会(自动重下本身仍然只一次,见 scheduleSingleRetry)。
|
|
1101
|
+
}
|
|
1102
|
+
// ⚠️ **失败/取消的 job 必须在"再点一次"时重置为 queued**(见长注释,略)。
|
|
831
1103
|
if (job.status === 'failed' || job.status === 'cancelled') {
|
|
832
1104
|
setJob(job, {
|
|
833
1105
|
status: 'queued', error: null, hashRetry: false, singleOnly: false, enoentRetries: 0,
|
|
834
|
-
// 4.24.1:用户再点一次是**新的一轮** —— 416 自愈的重试额度也重置。
|
|
835
1106
|
range416Retry: false,
|
|
836
1107
|
});
|
|
837
1108
|
}
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
1109
|
+
// 4.25.1(连点语义):这次是**用户点击** ⇒ 记"最近点击"序号(用于 8 条未开始队列上限)。
|
|
1110
|
+
if (clicked === true) {
|
|
1111
|
+
clickCounter += 1;
|
|
1112
|
+
setJob(job, { sessionClicked: true, clickSeq: clickCounter });
|
|
1113
|
+
}
|
|
1114
|
+
// 4.23.18「当前点击 / 在播 = 队首」:只要这次是 high,就在**状态处理完之后**记一枚新的
|
|
1115
|
+
// 提权序号(done-missing 会先被上面改回 queued,所以必须放在这里)。
|
|
1116
|
+
if (normalizePriority(priority) === 'high' && job.status !== 'done') {
|
|
844
1117
|
boostCounter += 1;
|
|
845
1118
|
setJob(job, { priority: 'high', boostSeq: boostCounter });
|
|
846
1119
|
}
|
|
847
|
-
//
|
|
848
|
-
|
|
849
|
-
|
|
1120
|
+
// 4.25.1:同一条目上遗留的 pending/queued/failed 重复记录一并清掉(只删不会被调度的那些)。
|
|
1121
|
+
dropStaleItemDuplicates(job, { itemKey: normalizeItemKey(job.itemKey) || wantKey, url: job.url, kind });
|
|
1122
|
+
if (clicked === true) enforceBurstCap();
|
|
850
1123
|
persist();
|
|
851
1124
|
pump();
|
|
852
1125
|
return { job: snapshot().find((x) => x.id === id), existed: true };
|
|
853
1126
|
}
|
|
1127
|
+
const id = urlKey(url);
|
|
854
1128
|
seqCounter += 1;
|
|
1129
|
+
const wantPriority = normalizePriority(priority);
|
|
855
1130
|
// 4.23.18:用户点击 / 当前在播(high)的新 job 一入队就在队首;预取(low)按入队顺序排。
|
|
856
1131
|
if (wantPriority === 'high') boostCounter += 1;
|
|
857
|
-
|
|
1132
|
+
if (clicked === true) clickCounter += 1;
|
|
1133
|
+
const created = {
|
|
858
1134
|
id, url: String(url), dest: String(dest), kind: String(kind || ''),
|
|
1135
|
+
itemKey: wantKey,
|
|
859
1136
|
total: 0, received: 0, status: 'queued', error: null, at: Date.now(),
|
|
860
1137
|
priority: wantPriority, boostSeq: wantPriority === 'high' ? boostCounter : 0,
|
|
861
|
-
|
|
1138
|
+
// 4.25.1(连点语义):只有显式 clicked:true 才计入 8 条未开始队列上限。
|
|
1139
|
+
sessionClicked: clicked === true, clickSeq: clicked === true ? clickCounter : 0,
|
|
1140
|
+
mode: 'single', autoSwitched: false, chosenVariant: chosenVariant === '1080p' || chosenVariant === '4k30' ? chosenVariant : 'original',
|
|
1141
|
+
seq: seqCounter,
|
|
862
1142
|
pauseRequested: false,
|
|
863
1143
|
hashRetry: false, singleOnly: false,
|
|
864
1144
|
// 4.23.22:校验记录(**只写结果,不写期望**)。完成那一刻才填。
|
|
@@ -866,7 +1146,10 @@ export function createDownloadQueue(opts = {}) {
|
|
|
866
1146
|
verifiedSource: null, verifyReason: null, verifyDetail: null,
|
|
867
1147
|
// 4.23.17:这一轮"本地文件不见了"的连续次数(成功即清零,见 runOne 的 catch)。
|
|
868
1148
|
enoentRetries: 0,
|
|
869
|
-
}
|
|
1149
|
+
};
|
|
1150
|
+
jobs.set(id, created);
|
|
1151
|
+
dropStaleItemDuplicates(created, { itemKey: wantKey, url, kind });
|
|
1152
|
+
if (clicked === true) enforceBurstCap();
|
|
870
1153
|
persist();
|
|
871
1154
|
pump();
|
|
872
1155
|
return { job: snapshot().find((x) => x.id === id), existed: false };
|
|
@@ -892,28 +1175,45 @@ export function createDownloadQueue(opts = {}) {
|
|
|
892
1175
|
*
|
|
893
1176
|
* 不改变 status / received / job id(不暂停、不取消、不重置已下字节)—— 队列继续跑。
|
|
894
1177
|
*
|
|
895
|
-
* @param {{id?:string,url?:string,kind?:string,priority?:string}|string} inp
|
|
1178
|
+
* @param {{id?:string,url?:string,kind?:string,priority?:string,itemKey?:string,relatedUrls?:string[],clicked?:boolean}|string} inp
|
|
896
1179
|
* @returns {object|null} 更新后的 job 快照行;找不到 job ⇒ null
|
|
897
1180
|
*/
|
|
898
1181
|
function setPriority(inp) {
|
|
899
1182
|
const o = typeof inp === 'string' ? { id: inp } : (inp || {});
|
|
900
1183
|
const id = String(o.id || '');
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
}
|
|
1184
|
+
const itemKey = normalizeItemKey(o.itemKey);
|
|
1185
|
+
const job = findJobFor({
|
|
1186
|
+
id, url: String(o.url || ''), kind: String(o.kind || ''), itemKey,
|
|
1187
|
+
relatedUrls: Array.isArray(o.relatedUrls) ? o.relatedUrls : [],
|
|
1188
|
+
});
|
|
907
1189
|
if (!job) return null;
|
|
908
1190
|
const want = normalizePriority(o.priority);
|
|
909
1191
|
if (want === 'high') {
|
|
910
1192
|
// 用户点回同一张 = 新的"我就要现在看"信号 ⇒ 归队首。
|
|
911
1193
|
boostCounter += 1;
|
|
912
|
-
|
|
1194
|
+
const patch = { priority: 'high', boostSeq: boostCounter };
|
|
1195
|
+
/**
|
|
1196
|
+
* 4.25.1:**high 必须落实成"会被调度"**。
|
|
1197
|
+
*
|
|
1198
|
+
* `pending` 是"未缓存、没排队"(启动 / 清队列 / 清缓存留下的记录)。旧实现只改
|
|
1199
|
+
* priority,状态仍是 pending ⇒ pump 永远挑不到它,"提权"看起来成功、实际永远不开始。
|
|
1200
|
+
* 点击路径现在主要走 `/downloads/add`(add 会 promote),但 `/downloads/priority`
|
|
1201
|
+
* 也是显式"我就要这张"的信号,两条语义必须一致。
|
|
1202
|
+
*/
|
|
1203
|
+
if (job.status === 'pending') {
|
|
1204
|
+
Object.assign(patch, { status: 'queued', error: null, received: 0, enoentRetries: 0 });
|
|
1205
|
+
}
|
|
1206
|
+
setJob(job, patch);
|
|
1207
|
+
// 4.25.1(连点语义):显式 clicked:true 的 high 才占 8 条未开始队列名额。
|
|
1208
|
+
if (o.clicked === true) {
|
|
1209
|
+
clickCounter += 1;
|
|
1210
|
+
setJob(job, { sessionClicked: true, clickSeq: clickCounter });
|
|
1211
|
+
}
|
|
913
1212
|
} else {
|
|
914
1213
|
// 降级:清提权序号,别让它在 low 档里继续冒充队首。
|
|
915
1214
|
setJob(job, { priority: 'low', boostSeq: 0 });
|
|
916
1215
|
}
|
|
1216
|
+
if (o.clicked === true) enforceBurstCap();
|
|
917
1217
|
persist();
|
|
918
1218
|
pump();
|
|
919
1219
|
return snapshot().find((x) => x.id === job.id) || null;
|
|
@@ -1021,12 +1321,16 @@ export function createDownloadQueue(opts = {}) {
|
|
|
1021
1321
|
let queuedCount = 0;
|
|
1022
1322
|
let runningCount = 0;
|
|
1023
1323
|
let pendingCount = 0;
|
|
1324
|
+
let pendingHighCount = 0;
|
|
1024
1325
|
for (const j of jobs.values()) {
|
|
1025
1326
|
if (j.status === 'queued') queuedCount += 1;
|
|
1026
1327
|
else if (j.status === 'running') runningCount += 1;
|
|
1027
|
-
else if (j.status === 'pending')
|
|
1328
|
+
else if (j.status === 'pending') {
|
|
1329
|
+
pendingCount += 1;
|
|
1330
|
+
if (normalizePriority(j.priority) === 'high') pendingHighCount += 1;
|
|
1331
|
+
}
|
|
1028
1332
|
}
|
|
1029
|
-
return { paused, queuedCount, runningCount, pendingCount };
|
|
1333
|
+
return { paused, queuedCount, runningCount, pendingCount, pendingHighCount };
|
|
1030
1334
|
}
|
|
1031
1335
|
|
|
1032
1336
|
/**
|
|
@@ -1532,6 +1836,20 @@ export function createDownloadQueue(opts = {}) {
|
|
|
1532
1836
|
log('download-queue: done ' + job.kind + ' ' + job.dest + ' (' + Math.round(job.total / 1048576) + ' MB)');
|
|
1533
1837
|
} catch (e) {
|
|
1534
1838
|
const aborted = e && (e.name === 'AbortError');
|
|
1839
|
+
/**
|
|
1840
|
+
* 4.25.1:显式换档(user clicked "use original" / quality switch)打断了在跑的旧档。
|
|
1841
|
+
* 此刻流已收尾 ⇒ 安全地把同一条 job 迁到新 URL,重新排队(不产生第二条 job)。
|
|
1842
|
+
*/
|
|
1843
|
+
if (job.replaceOnAbort) {
|
|
1844
|
+
const target = job.replaceOnAbort;
|
|
1845
|
+
job.replaceOnAbort = null;
|
|
1846
|
+
relocateJob(job, target.url, target.dest);
|
|
1847
|
+
setJob(job, { status: 'queued', error: null, received: 0, pauseRequested: false, enoentRetries: 0 });
|
|
1848
|
+
log('download-queue: quality switch ' + job.kind + ' → ' + target.url + ' — requeued');
|
|
1849
|
+
persist();
|
|
1850
|
+
setTimeout(() => pump(), 0);
|
|
1851
|
+
return;
|
|
1852
|
+
}
|
|
1535
1853
|
/**
|
|
1536
1854
|
* 4.23.18:暂停打断(`setPaused(true)`)。**不是失败、也不是取消**:回到 queued,
|
|
1537
1855
|
* `.part` 保留,恢复后从盘上字节续传。放在 hash/ENOENT 分支之前 —— 暂停时抛什么
|
|
@@ -1626,6 +1944,15 @@ export function createDownloadQueue(opts = {}) {
|
|
|
1626
1944
|
});
|
|
1627
1945
|
if (!aborted) log('download-queue: failed ' + job.url + ' — ' + job.error);
|
|
1628
1946
|
} finally {
|
|
1947
|
+
// 显式换档恰好撞上"旧档在 finally 之前已经跑完"(abort 没来得及打断):也要迁过去。
|
|
1948
|
+
if (job.replaceOnAbort) {
|
|
1949
|
+
const target = job.replaceOnAbort;
|
|
1950
|
+
job.replaceOnAbort = null;
|
|
1951
|
+
relocateJob(job, target.url, target.dest);
|
|
1952
|
+
setJob(job, { status: 'queued', error: null, received: 0, pauseRequested: false, enoentRetries: 0 });
|
|
1953
|
+
log('download-queue: quality switch ' + job.kind + ' → ' + target.url + ' — requeued');
|
|
1954
|
+
setTimeout(() => pump(), 0);
|
|
1955
|
+
}
|
|
1629
1956
|
setJob(job, { ctrl: null });
|
|
1630
1957
|
persist();
|
|
1631
1958
|
}
|
|
@@ -1678,6 +2005,8 @@ export function createDownloadQueue(opts = {}) {
|
|
|
1678
2005
|
if (j.dest !== dest) continue;
|
|
1679
2006
|
return {
|
|
1680
2007
|
id: j.id, dest: j.dest, status: j.status, total: j.total, received: j.received, kind: j.kind,
|
|
2008
|
+
// 4.25.1:代理/清单按条目认同一份下载,不再只看 URL。
|
|
2009
|
+
itemKey: normalizeItemKey(j.itemKey), priority: normalizePriority(j.priority),
|
|
1681
2010
|
// 4.23.15:`localCopyFor()` 要用它判"这份本地内容本机解不了 ⇒ 别选它播"。
|
|
1682
2011
|
decodeUnsupported: j.decodeUnsupported === true,
|
|
1683
2012
|
};
|
|
@@ -1700,7 +2029,36 @@ export function createDownloadQueue(opts = {}) {
|
|
|
1700
2029
|
* `urlKey()` 暴露出来 —— 键的定义仍然只有一处。
|
|
1701
2030
|
*/
|
|
1702
2031
|
function idForUrl(url) {
|
|
1703
|
-
|
|
2032
|
+
const key = urlKey(String(url || ''));
|
|
2033
|
+
if (jobs.has(key)) return key;
|
|
2034
|
+
/**
|
|
2035
|
+
* 4.25.1:job id 在"原画 ↔ 变体"换档后仍保持稳定(不重建 job),因此 URL 的散列不再
|
|
2036
|
+
* 必然等于 id。这里按“当前 URL → 同族 URL”反查一次,让对账器/清单继续用 URL 找到同一条目。
|
|
2037
|
+
*/
|
|
2038
|
+
const hit = findJobFor({ url: String(url || '') });
|
|
2039
|
+
return hit ? hit.id : key;
|
|
2040
|
+
}
|
|
2041
|
+
|
|
2042
|
+
/**
|
|
2043
|
+
* 按**条目身份**取一条 job(inventory 用来给条目挂 `jobStatus/jobPriority`)。
|
|
2044
|
+
* 不写盘、不 pump:清单只读事实,绝不因为渲染列表而排下载。
|
|
2045
|
+
*
|
|
2046
|
+
* @param {{id?:string,url?:string,kind?:string,itemKey?:string,relatedUrls?:string[]}|string} inp
|
|
2047
|
+
* @returns {null|{id:string,itemKey:string,url:string,dest:string,kind:string,status:string,priority:string,pendingHigh:boolean}}
|
|
2048
|
+
*/
|
|
2049
|
+
function jobForItem(inp) {
|
|
2050
|
+
const job = findJobFor(inp);
|
|
2051
|
+
if (!job) return null;
|
|
2052
|
+
return {
|
|
2053
|
+
id: job.id,
|
|
2054
|
+
itemKey: normalizeItemKey(job.itemKey),
|
|
2055
|
+
url: job.url,
|
|
2056
|
+
dest: job.dest,
|
|
2057
|
+
kind: job.kind,
|
|
2058
|
+
status: job.status,
|
|
2059
|
+
priority: normalizePriority(job.priority),
|
|
2060
|
+
pendingHigh: job.status === 'pending' && normalizePriority(job.priority) === 'high',
|
|
2061
|
+
};
|
|
1704
2062
|
}
|
|
1705
2063
|
|
|
1706
2064
|
/**
|
|
@@ -1797,6 +2155,8 @@ export function createDownloadQueue(opts = {}) {
|
|
|
1797
2155
|
add, cancel, snapshot, pump, statePath, pathFor, jobFor, isSlowHost,
|
|
1798
2156
|
// 4.23.19:URL → job id(与 add() 同一个键,供对账器做 O(1) 查询)。
|
|
1799
2157
|
idForUrl,
|
|
2158
|
+
// 4.25.1:条目身份查询(清单挂 job 状态 / 点击前判断 pending+high)。
|
|
2159
|
+
jobForItem,
|
|
1800
2160
|
// 4.23.15「本地副本自愈」:查一条 job 的完整视图 / 作废成品 / 标记"本机解不了"。
|
|
1801
2161
|
jobById, invalidate, markDecodeUnsupported,
|
|
1802
2162
|
// 4.23.18「队列可运维」:暂停全部 / 清空未开始(queued→pending)/ 读汇总。
|