@torrent-tv/proxy 2.89.7 → 2.89.9

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.
Files changed (498) hide show
  1. package/.claude/commands/opsx/apply.md +155 -155
  2. package/.claude/commands/opsx/archive.md +160 -160
  3. package/.claude/commands/opsx/explore.md +174 -174
  4. package/.claude/commands/opsx/propose.md +109 -109
  5. package/.claude/commands/opsx/sync.md +143 -143
  6. package/.claude/skills/openspec-apply-change/SKILL.md +159 -159
  7. package/.claude/skills/openspec-archive-change/SKILL.md +117 -117
  8. package/.claude/skills/openspec-explore/SKILL.md +289 -289
  9. package/.claude/skills/openspec-propose/SKILL.md +113 -113
  10. package/.claude/skills/openspec-sync-specs/SKILL.md +147 -147
  11. package/.dockerignore +5 -5
  12. package/AGENTS.md +363 -338
  13. package/CHANGELOG.md +2345 -2335
  14. package/Dockerfile +30 -30
  15. package/LICENSE +16 -16
  16. package/README.md +369 -369
  17. package/assets/calibration/NOTICE.md +100 -100
  18. package/assets/calibration/audio/NOTICE.md +19 -19
  19. package/assets/calibration/audio/manifest.json +98 -98
  20. package/assets/diagnostics/sctpstate.gdb +31 -31
  21. package/bin/cli.js +684 -684
  22. package/bin/thread-pool.js +30 -30
  23. package/biome.json +412 -412
  24. package/docs/browser-proxy-contract.md +155 -155
  25. package/docs/container-architecture.md +273 -273
  26. package/docs/download-architecture.md +261 -261
  27. package/docs/encode-architecture.md +765 -765
  28. package/docs/encode-run-state.md +96 -96
  29. package/docs/logs.md +97 -97
  30. package/docs/playback-start-forecast.md +113 -113
  31. package/docs/socks5-egress-research.md +114 -114
  32. package/docs/storage-architecture.md +273 -273
  33. package/knip.json +14 -14
  34. package/openspec/changes/adaptive-bitrate/design.md +92 -92
  35. package/openspec/changes/adaptive-bitrate/proposal.md +56 -56
  36. package/openspec/changes/adaptive-bitrate/specs/adaptive-bitrate/spec.md +51 -51
  37. package/openspec/changes/adaptive-bitrate/tasks.md +40 -40
  38. package/openspec/changes/binary-distribution/.openspec.yaml +2 -2
  39. package/openspec/changes/binary-distribution/proposal.md +53 -53
  40. package/openspec/changes/chunked-request-bodies/design.md +81 -81
  41. package/openspec/changes/chunked-request-bodies/proposal.md +53 -53
  42. package/openspec/changes/chunked-request-bodies/specs/chunked-request-bodies/spec.md +30 -30
  43. package/openspec/changes/chunked-request-bodies/tasks.md +40 -40
  44. package/openspec/changes/cold-start/design.md +98 -98
  45. package/openspec/changes/cold-start/proposal.md +62 -62
  46. package/openspec/changes/cold-start/specs/cold-start/spec.md +47 -47
  47. package/openspec/changes/cold-start/tasks.md +48 -48
  48. package/openspec/changes/disk-cap/.openspec.yaml +2 -2
  49. package/openspec/changes/disk-cap/proposal.md +37 -37
  50. package/openspec/changes/disk-cap/specs/disk-cap/spec.md +25 -25
  51. package/openspec/changes/disk-cap/tasks.md +19 -19
  52. package/openspec/changes/proxy-observability/.openspec.yaml +2 -2
  53. package/openspec/changes/proxy-observability/design.md +38 -38
  54. package/openspec/changes/proxy-observability/proposal.md +49 -49
  55. package/openspec/changes/proxy-observability/specs/observability/spec.md +33 -33
  56. package/openspec/changes/proxy-observability/tasks.md +21 -21
  57. package/openspec/changes/seek-debounce/design.md +118 -118
  58. package/openspec/changes/seek-debounce/proposal.md +59 -59
  59. package/openspec/changes/seek-debounce/specs/seek-debounce/spec.md +41 -41
  60. package/openspec/changes/seek-debounce/tasks.md +33 -33
  61. package/openspec/changes/subtitle-language/.openspec.yaml +2 -2
  62. package/openspec/changes/subtitle-language/proposal.md +50 -50
  63. package/openspec/changes/subtitle-language/specs/subtitle-language/spec.md +34 -34
  64. package/openspec/changes/subtitle-language/tasks.md +26 -26
  65. package/openspec/changes/track-selection/.openspec.yaml +2 -2
  66. package/openspec/changes/track-selection/design.md +47 -47
  67. package/openspec/changes/track-selection/proposal.md +45 -45
  68. package/openspec/changes/track-selection/specs/track-selection/spec.md +41 -41
  69. package/openspec/changes/track-selection/tasks.md +20 -20
  70. package/openspec/changes/transcode-quality/.openspec.yaml +2 -2
  71. package/openspec/changes/transcode-quality/proposal.md +51 -51
  72. package/openspec/changes/transcode-quality/specs/transcode-quality/spec.md +139 -139
  73. package/openspec/changes/transcode-quality/tasks.md +63 -63
  74. package/openspec/config.yaml +38 -38
  75. package/package.json +51 -50
  76. package/public/.well-known/appspecific/com.chrome.devtools.json +1 -1
  77. package/research/delivery-wedge-stand-ladder-2026-09-13.md +329 -329
  78. package/research/double-spawn-2026-09-10.md +171 -171
  79. package/research/handover-2026-09-13.md +209 -209
  80. package/research/handover-reader-claims-removal-2026-09-12.md +166 -166
  81. package/research/piece-withdrawn-but-still-claimed-2026-09-12.md +175 -175
  82. package/research/preset-benchmark-size-scaling-2026-08-22.md +55 -55
  83. package/research/priority-map-is-the-truth-2026-09-12.md +225 -225
  84. package/routes/api/client-logs/post.js +86 -86
  85. package/routes/api/delivery-sink/get.js +72 -72
  86. package/routes/api/link-probe/get.js +51 -51
  87. package/routes/api/playback-plan/audio-tracks/post.js +29 -29
  88. package/routes/api/playback-plan/post.js +53 -53
  89. package/routes/api/sources/files/get.js +106 -106
  90. package/routes/api/sources/post.js +36 -36
  91. package/routes/api/sources/stats/get.js +132 -132
  92. package/routes/api/sources/warm/post.js +217 -217
  93. package/routes/api/subtitles/get.js +230 -230
  94. package/routes/api/transcode-sessions/fragment-far/post.js +60 -60
  95. package/routes/api/transcode-sessions/net-report/post.js +138 -138
  96. package/routes/api/transcode-sessions/post.js +178 -178
  97. package/routes/api/transcode-sessions/progress/get.js +27 -27
  98. package/routes/api/transcode-sessions/release/post.js +41 -41
  99. package/routes/api/transcode-sessions/seek/post.js +63 -63
  100. package/routes/health/get.js +13 -13
  101. package/routes/healthz/get.js +13 -13
  102. package/routes/stream/get.js +416 -416
  103. package/routes/transcode/audio-file/get.js +64 -64
  104. package/routes/transcode/audio-warm/get.js +95 -95
  105. package/routes/transcode/session-file/get.js +52 -52
  106. package/routes/transcode/variant-file/get.js +95 -95
  107. package/routes/transcode/variant-warm/get.js +84 -84
  108. package/scripts/build-audio-samples.mjs +31 -31
  109. package/scripts/render-run-graph.js +173 -173
  110. package/server.js +748 -748
  111. package/services/encode/CoverageMap.js +428 -428
  112. package/services/encode/CushionReport.js +345 -345
  113. package/services/encode/EncodeAdmission.js +376 -376
  114. package/services/encode/EncodeOrchestrator.js +1192 -1192
  115. package/services/encode/EncodePlan.js +1368 -1368
  116. package/services/encode/EncodeRun.js +36 -12
  117. package/services/encode/Encoder.js +142 -142
  118. package/services/encode/EncoderSelection.js +38 -38
  119. package/services/encode/LocalObservations.js +359 -359
  120. package/services/encode/NvencEncoder.js +50 -50
  121. package/services/encode/OutputOpening.js +784 -784
  122. package/services/encode/OutputTimes.js +579 -579
  123. package/services/encode/QsvEncoder.js +58 -58
  124. package/services/encode/Renditions.js +2283 -2283
  125. package/services/encode/RunClock.js +87 -87
  126. package/services/encode/RunProgress.js +79 -79
  127. package/services/encode/SegmentDemand.js +95 -95
  128. package/services/encode/SoftwareEncoder.js +121 -121
  129. package/services/encode/V4l2m2mEncoder.js +53 -53
  130. package/services/encode/VaapiEncoder.js +66 -66
  131. package/services/encode/args.js +418 -418
  132. package/services/encode/audio-calibration.js +67 -67
  133. package/services/encode/audio-work-rate.js +15 -15
  134. package/services/encode/available-share.js +89 -89
  135. package/services/encode/calibration.js +361 -361
  136. package/services/encode/contention.js +128 -128
  137. package/services/encode/decode-cost-fit.js +363 -363
  138. package/services/encode/encode-exit.js +116 -116
  139. package/services/encode/encode-run-state.js +551 -551
  140. package/services/encode/encoder-readings.js +43 -43
  141. package/services/encode/fingerprint.js +322 -322
  142. package/services/encode/host-load.js +174 -174
  143. package/services/encode/hwaccel.js +1894 -1894
  144. package/services/encode/index.js +9 -9
  145. package/services/encode/learned-median.js +90 -90
  146. package/services/encode/output/EncodedOutput.js +57 -57
  147. package/services/encode/output/Output.js +131 -131
  148. package/services/encode/output/OutputCatalog.js +414 -414
  149. package/services/encode/output/OutputSpec.js +390 -390
  150. package/services/encode/output/Timeline.js +308 -308
  151. package/services/encode/output/cut-grid.js +107 -107
  152. package/services/encode/output/index.js +24 -24
  153. package/services/encode/output/ladder.js +32 -32
  154. package/services/encode/output/playlists.js +284 -284
  155. package/services/encode/output/rates.js +125 -125
  156. package/services/encode/output-key-format.js +21 -21
  157. package/services/encode/quality/EncodeCost.js +1043 -1043
  158. package/services/encode/quality/HostLoad.js +525 -525
  159. package/services/encode/quality/HostTimings.js +249 -249
  160. package/services/encode/quality/OutputQualityState.js +24 -24
  161. package/services/encode/quality/QualityController.js +758 -758
  162. package/services/encode/quality/QualityOffer.js +416 -416
  163. package/services/encode/quality/drain-threat.js +47 -47
  164. package/services/encode/quality/link-budget.js +337 -337
  165. package/services/encode/quality/output-format.js +319 -319
  166. package/services/encode/quality/serving-output.js +216 -216
  167. package/services/encode/quality/visible-rung.js +57 -57
  168. package/services/encode/run-budget.js +97 -97
  169. package/services/encode/run-command.js +757 -757
  170. package/services/encode/run-costs.js +155 -155
  171. package/services/encode/run-inputs.js +53 -53
  172. package/services/encode/segment-formats/fmp4.js +476 -476
  173. package/services/encode/segment-formats/init-compat.js +324 -324
  174. package/services/encode/segment-formats/mpegts.js +134 -134
  175. package/services/encode/start-stop-cost.js +178 -178
  176. package/services/encode/throughput.js +97 -97
  177. package/services/encode/torrent-cost.js +88 -88
  178. package/services/identity/installation-id.js +38 -38
  179. package/services/media/ContainerOrchestrator.js +202 -202
  180. package/services/media/KeyframeTables.js +356 -356
  181. package/services/media/SourceFile.js +298 -298
  182. package/services/media/SubtitleCues.js +592 -592
  183. package/services/media/SubtitleOrchestrator.js +167 -167
  184. package/services/media/audio-inventory.js +299 -299
  185. package/services/media/container/AviContainer.js +268 -268
  186. package/services/media/container/Container.js +632 -632
  187. package/services/media/container/ContainerFactory.js +38 -38
  188. package/services/media/container/KeyframeTable.js +272 -272
  189. package/services/media/container/MatroskaContainer.js +1031 -1031
  190. package/services/media/container/Mp4Container.js +1354 -1354
  191. package/services/media/container/SubtitleFileContainer.js +339 -339
  192. package/services/media/container/ebml-reader.js +159 -159
  193. package/services/media/container/ebml-stream.js +174 -174
  194. package/services/media/container/matroska-clusters.js +1165 -1165
  195. package/services/media/container/unavailable.js +77 -77
  196. package/services/media/ffmpeg-banner.js +260 -260
  197. package/services/media/keyframe-probe.js +157 -157
  198. package/services/media/media-info-probe.js +93 -93
  199. package/services/media/playback-planner.js +931 -931
  200. package/services/media/tracks/AudioTrack.js +143 -143
  201. package/services/media/tracks/ContainerTrack.js +84 -84
  202. package/services/media/tracks/ImageSubtitleTrack.js +19 -19
  203. package/services/media/tracks/SubtitleTrack.js +38 -38
  204. package/services/media/tracks/TextSubtitleTrack.js +295 -295
  205. package/services/media/tracks/VideoTrack.js +33 -33
  206. package/services/media/tracks/audio-bound.js +233 -233
  207. package/services/media/tracks/index.js +15 -15
  208. package/services/media/tracks/language-detect.js +174 -174
  209. package/services/server/OutputLifecycle.js +418 -418
  210. package/services/server/SegmentServing.js +1152 -1152
  211. package/services/server/ViewerRequests.js +785 -785
  212. package/services/server/WaitLedger.js +142 -142
  213. package/services/server/controllers/PlaybackController.js +40 -40
  214. package/services/server/controllers/SubtitleController.js +109 -109
  215. package/services/server/http-responses.js +60 -60
  216. package/services/server/transcode-session-files.js +325 -325
  217. package/services/server/wire-outputs.js +1029 -1029
  218. package/services/storage/Diagnostics.js +202 -202
  219. package/services/storage/MachineBudget.js +235 -235
  220. package/services/storage/allowance.js +107 -107
  221. package/services/storage/core-dumps.js +106 -106
  222. package/services/storage/files/CompletedFiles.js +391 -391
  223. package/services/storage/files/piece-from-whole-file.js +118 -118
  224. package/services/storage/free.js +71 -71
  225. package/services/storage/keep.js +48 -48
  226. package/services/storage/machine-memory.js +56 -56
  227. package/services/storage/memory-report.js +575 -575
  228. package/services/storage/piece-store/piece-disk-store.js +539 -539
  229. package/services/storage/piece-store/piece-lru.js +495 -495
  230. package/services/storage/piece-store/shared-piece-store.js +1937 -1937
  231. package/services/storage/returns.js +103 -103
  232. package/services/storage/segment-store/SegmentStore.js +1294 -1294
  233. package/services/storage/wire.js +175 -175
  234. package/services/torrent/Contents.js +382 -382
  235. package/services/torrent/demand/DemandRegister.js +202 -202
  236. package/services/torrent/demand/Urgency.js +137 -137
  237. package/services/torrent/demand/Window.js +118 -118
  238. package/services/torrent/demand/index.js +8 -8
  239. package/services/torrent/demand/pieces.js +140 -140
  240. package/services/torrent/download/SwarmSelection.js +386 -386
  241. package/services/torrent/download/registry.js +102 -102
  242. package/services/torrent/download/withdraw-claim.js +80 -80
  243. package/services/torrent/episode-naming.js +182 -182
  244. package/services/torrent/files.js +455 -455
  245. package/services/torrent/naming.js +619 -619
  246. package/services/torrent/piece-store-of.js +29 -29
  247. package/services/torrent/supply-margin.js +264 -264
  248. package/services/torrent/torrent-pool.js +2688 -2688
  249. package/services/torrent/worker/background-fill.js +213 -213
  250. package/services/torrent/worker/channel.js +271 -271
  251. package/services/torrent/worker/client.js +836 -836
  252. package/services/torrent/worker/fastest-wires.js +342 -342
  253. package/services/torrent/worker/handle-state.js +23 -23
  254. package/services/torrent/worker/held-bytes.js +231 -231
  255. package/services/torrent/worker/install-webrtc-shim.js +32 -32
  256. package/services/torrent/worker/piece-reader.js +1078 -1078
  257. package/services/torrent/worker/pool-adapter.js +371 -371
  258. package/services/torrent/worker/protocol.js +184 -184
  259. package/services/torrent/worker/resume-warm.js +107 -107
  260. package/services/torrent/worker/webrtc-shim.js +219 -219
  261. package/services/torrent/worker/worker.js +1137 -1137
  262. package/services/transport/data-channel-handler.js +1406 -1373
  263. package/services/transport/delivery-probe.js +798 -842
  264. package/services/transport/health-collector.js +53 -53
  265. package/services/transport/nat-classifier.js +231 -231
  266. package/services/transport/packet-witness.js +808 -808
  267. package/services/transport/port-mapper.js +235 -235
  268. package/services/transport/registry-api.js +81 -81
  269. package/services/transport/tunnel-client.js +447 -447
  270. package/services/transport/usrsctp-state.js +175 -175
  271. package/services/transport/webrtc-manager.js +484 -484
  272. package/services/viewer/Assignments.js +340 -340
  273. package/services/viewer/PriorityMap.js +421 -421
  274. package/services/viewer/PriorityOrchestrator.js +325 -325
  275. package/services/viewer/Viewer.js +691 -691
  276. package/services/viewer/Viewers.js +368 -368
  277. package/services/viewer/active-output.js +37 -37
  278. package/services/viewer/audio-start.js +46 -46
  279. package/services/viewer/buffer-trend.js +81 -81
  280. package/services/viewer/choices.js +647 -647
  281. package/services/viewer/media-time.js +355 -355
  282. package/services/viewer/playback-readiness.js +635 -635
  283. package/services/viewer/positions.js +126 -126
  284. package/services/viewer/report-intake.js +46 -46
  285. package/store/source-registry.js +71 -71
  286. package/test/asset-paths.test.js +28 -28
  287. package/test/audio-buffer-continuity.test.js +77 -77
  288. package/test/audio-calibration.test.js +23 -23
  289. package/test/audio-inventory.test.js +212 -212
  290. package/test/audio-track-bitrate.test.js +40 -40
  291. package/test/auto-quality-step.test.js +762 -762
  292. package/test/available-share.test.js +76 -76
  293. package/test/background-fill.test.js +190 -190
  294. package/test/client-log-file.test.js +110 -110
  295. package/test/coalesce.test.js +39 -39
  296. package/test/completed-files.test.js +115 -115
  297. package/test/concurrent-cost.test.js +148 -148
  298. package/test/container-media-info.test.js +228 -228
  299. package/test/contention.test.js +76 -76
  300. package/test/core-dumps.test.js +57 -57
  301. package/test/coverage-follows-the-disk.test.js +233 -228
  302. package/test/coverage-map.test.js +195 -195
  303. package/test/cue-push-carries-what-the-walk-found.test.js +58 -58
  304. package/test/cut-times-timeline.test.js +65 -65
  305. package/test/cuts-follow-published-grid.test.js +109 -109
  306. package/test/data-channel-frame.test.js +63 -63
  307. package/test/data-channel-viewer-lifecycle.test.js +162 -92
  308. package/test/dead-torrent-handle.test.js +52 -52
  309. package/test/declared-rates.test.js +116 -116
  310. package/test/declared-tracks.test.js +43 -43
  311. package/test/decode-cost-fit.test.js +202 -202
  312. package/test/decode-cost.test.js +475 -475
  313. package/test/delivery-probe-field.test.js +173 -0
  314. package/test/delivery-probe.test.js +382 -393
  315. package/test/demand-register.test.js +206 -206
  316. package/test/dht-bootstrap.test.js +72 -72
  317. package/test/drain-threat.test.js +65 -65
  318. package/test/duplicate-add.test.js +48 -48
  319. package/test/encode-admission.test.js +146 -146
  320. package/test/encode-cost.test.js +275 -275
  321. package/test/encode-exit.test.js +111 -111
  322. package/test/encode-orchestrator.test.js +426 -426
  323. package/test/encode-plan-viewers.test.js +725 -725
  324. package/test/encode-plan.test.js +545 -545
  325. package/test/encode-run-state.test.js +314 -314
  326. package/test/encode-run.test.js +20 -0
  327. package/test/encode-slope.test.js +46 -46
  328. package/test/encoded-output.test.js +39 -39
  329. package/test/encoder-kinds.test.js +122 -122
  330. package/test/encoder-readings.test.js +68 -68
  331. package/test/episode-naming.test.js +131 -131
  332. package/test/every-claimant-is-told-its-share.test.js +152 -152
  333. package/test/failed-encoder-still-serves-what-exists.test.js +354 -354
  334. package/test/failed-start-breaker.test.js +202 -202
  335. package/test/fastest-wires.test.js +143 -143
  336. package/test/ffmpeg-stream-counts.test.js +78 -78
  337. package/test/file-edges.test.js +191 -191
  338. package/test/fixtures/audio-frame-boundary.json +1 -1
  339. package/test/fmp4-self-contained.test.js +80 -80
  340. package/test/fmp4-timestamps.test.js +140 -140
  341. package/test/fmp4-track-completeness.test.js +41 -41
  342. package/test/format-keyed-outputs.test.js +78 -78
  343. package/test/health-metrics.test.js +36 -38
  344. package/test/held-read-ownership.test.js +77 -77
  345. package/test/held-request-width.test.js +157 -157
  346. package/test/helpers/encode-run.js +248 -240
  347. package/test/helpers/held-file.js +77 -77
  348. package/test/helpers/manager.js +62 -62
  349. package/test/helpers/matroska-file.js +296 -296
  350. package/test/helpers/output-spec.js +63 -63
  351. package/test/host-load.test.js +68 -68
  352. package/test/index-accuracy-is-about-the-file.test.js +128 -128
  353. package/test/init-compat.test.js +219 -219
  354. package/test/input-lost-quiets-the-plan.test.js +261 -261
  355. package/test/input-unavailable.test.js +48 -48
  356. package/test/installation-id.test.js +39 -39
  357. package/test/keeping-period.test.js +83 -83
  358. package/test/keyframe-index-accuracy.test.js +188 -188
  359. package/test/keyframe-table-budget.test.js +123 -123
  360. package/test/keyframes-belong-to-the-file.test.js +249 -249
  361. package/test/learned-median.test.js +37 -37
  362. package/test/letting-go-asks-who-is-watching.test.js +191 -191
  363. package/test/limit-row-by-area.test.js +60 -60
  364. package/test/link-budget.test.js +111 -111
  365. package/test/link-probe.test.js +56 -56
  366. package/test/local-encoding-data.test.js +273 -273
  367. package/test/logger-repeats.test.js +134 -134
  368. package/test/look-ahead-coverage.test.js +56 -56
  369. package/test/manager-surface.test.js +291 -291
  370. package/test/matroska-cluster-discovery.test.js +323 -323
  371. package/test/matroska-cues-track.test.js +192 -192
  372. package/test/media-time.test.js +62 -62
  373. package/test/memory-budget.test.js +390 -390
  374. package/test/move-cost.test.js +216 -216
  375. package/test/mp4-subtitles.test.js +173 -173
  376. package/test/one-authority.test.js +285 -285
  377. package/test/one-budget.test.js +149 -149
  378. package/test/one-output-one-session.test.js +273 -273
  379. package/test/one-resource-divided.test.js +185 -185
  380. package/test/one-serving-output-per-height.test.js +458 -458
  381. package/test/orchestrator-wired.test.js +207 -207
  382. package/test/output-catalog.test.js +39 -39
  383. package/test/output-shape.test.js +21 -21
  384. package/test/output-spec.test.js +251 -251
  385. package/test/output-speed.test.js +92 -87
  386. package/test/outputs-being-prepared.test.js +40 -40
  387. package/test/packet-witness-ring.test.js +236 -236
  388. package/test/packet-witness.test.js +148 -148
  389. package/test/piece-disk-store.test.js +401 -401
  390. package/test/piece-from-whole-file.test.js +129 -129
  391. package/test/piece-lru.test.js +319 -319
  392. package/test/piece-reader.test.js +162 -162
  393. package/test/piece-selection-offset.test.js +85 -85
  394. package/test/piece-store-eviction.test.js +595 -595
  395. package/test/piece-store-never-refuses.test.js +153 -153
  396. package/test/piece-store-reservations.test.js +403 -403
  397. package/test/piece-store-slow-disk.test.js +159 -159
  398. package/test/piece-tail.test.js +97 -97
  399. package/test/plan-host-timings.test.js +68 -68
  400. package/test/plan-is-steady.test.js +225 -225
  401. package/test/playback-readiness.test.js +518 -518
  402. package/test/playback-start-regressions.test.js +101 -101
  403. package/test/priority-map-download.test.js +174 -174
  404. package/test/priority-map-emptied.test.js +204 -204
  405. package/test/priority-map-per-output.test.js +215 -215
  406. package/test/priority-map.test.js +163 -163
  407. package/test/probe-wedge-certainty.test.js +60 -60
  408. package/test/produced-copy-choice.test.js +213 -213
  409. package/test/published-timeline.test.js +83 -83
  410. package/test/quality-offer.test.js +160 -160
  411. package/test/quality-variants.test.js +1274 -1270
  412. package/test/rate-control-in-the-output.test.js +181 -180
  413. package/test/read-survives-withdrawal.test.js +164 -164
  414. package/test/read-window.test.js +365 -365
  415. package/test/reads-not-yet-arrived.test.js +218 -218
  416. package/test/resume-warm.test.js +39 -39
  417. package/test/run-graph-drift.test.js +37 -37
  418. package/test/run-inputs.test.js +80 -80
  419. package/test/run-intervals.test.js +100 -100
  420. package/test/run-position-follows-published-grid.test.js +89 -89
  421. package/test/run-progress.test.js +64 -64
  422. package/test/same-height-switch.test.js +663 -663
  423. package/test/seek-frees-the-viewers-requests.test.js +217 -217
  424. package/test/seek-landing.test.js +109 -109
  425. package/test/seek-target-not-superseded.test.js +195 -195
  426. package/test/segment-closed-on-both-branches.test.js +114 -114
  427. package/test/segment-cut-times.test.js +63 -63
  428. package/test/segment-demand.test.js +80 -80
  429. package/test/segment-hold-supersede.test.js +91 -91
  430. package/test/segment-judged-by-its-own-header.test.js +222 -222
  431. package/test/segment-serve-wiring.test.js +481 -481
  432. package/test/segment-store-eviction.test.js +247 -247
  433. package/test/segment-store.test.js +352 -352
  434. package/test/segment-true-start.test.js +86 -86
  435. package/test/segments-are-shared.test.js +176 -176
  436. package/test/send-chunk-bytes.test.js +86 -86
  437. package/test/serving-existing-output.test.js +111 -111
  438. package/test/serving-output.test.js +102 -102
  439. package/test/shared-piece-store.test.js +310 -310
  440. package/test/sidecar-naming.test.js +142 -142
  441. package/test/soundtrack-rate-bound.test.js +289 -289
  442. package/test/source-file.test.js +133 -133
  443. package/test/source-files-route.test.js +113 -113
  444. package/test/source-registry.test.js +18 -18
  445. package/test/split-a-stretch.test.js +139 -139
  446. package/test/stale-request-after-seek.test.js +158 -158
  447. package/test/startup-readings.test.js +124 -124
  448. package/test/stream-route.test.js +237 -237
  449. package/test/subtitle-cue-framing.test.js +200 -200
  450. package/test/subtitle-cue-source.test.js +102 -102
  451. package/test/subtitle-cue-walk.test.js +385 -385
  452. package/test/subtitle-cursor.test.js +63 -63
  453. package/test/subtitle-defaults.test.js +118 -118
  454. package/test/subtitle-language.test.js +252 -252
  455. package/test/subtitle-metadata.test.js +15 -15
  456. package/test/subtitle-plan-waits.test.js +125 -125
  457. package/test/subtitle-track-numbering.test.js +391 -391
  458. package/test/supply-margin.test.js +190 -190
  459. package/test/swarm-follows-readers.test.js +174 -174
  460. package/test/swarm-reach.test.js +130 -130
  461. package/test/swarm-selection.test.js +220 -220
  462. package/test/tail-duplication.test.js +167 -167
  463. package/test/the-channel-has-both-ends.test.js +91 -91
  464. package/test/the-torrent-thread-serves-bytes.test.js +107 -107
  465. package/test/thread-pool.test.js +53 -53
  466. package/test/timeline.test.js +84 -84
  467. package/test/torrent-contents.test.js +235 -235
  468. package/test/torrent-cost.test.js +71 -71
  469. package/test/torrent-files.test.js +221 -221
  470. package/test/torrent-naming.test.js +255 -255
  471. package/test/tracks-begin-together.test.js +209 -209
  472. package/test/tunnel-renewal.test.js +167 -167
  473. package/test/two-viewers-one-picture.test.js +508 -508
  474. package/test/upload-hurry.test.js +186 -186
  475. package/test/usrsctp-state.test.js +131 -131
  476. package/test/video-facts.test.js +102 -102
  477. package/test/viewer-assignments.test.js +143 -143
  478. package/test/viewer-outputs.test.js +285 -285
  479. package/test/viewer-presence.test.js +116 -116
  480. package/test/viewer-requests-admission.test.js +92 -92
  481. package/test/viewer-starts-on-map.test.js +27 -27
  482. package/test/viewer-states.test.js +192 -189
  483. package/test/viewer-subtitle-subscription.test.js +96 -96
  484. package/test/viewer.test.js +139 -139
  485. package/test/viewing-generations.test.js +390 -390
  486. package/test/visible-rung.test.js +37 -37
  487. package/test/wait-ledger.test.js +100 -100
  488. package/test/webrtc-shim.test.js +149 -149
  489. package/test/wedge-certainty.test.js +131 -131
  490. package/test/whole-file-held-read.test.js +30 -30
  491. package/test/withdraw-piece-claim.test.js +212 -212
  492. package/test/worker-channel.test.js +158 -158
  493. package/utils/client-log-file.js +151 -151
  494. package/utils/coalesce.js +41 -41
  495. package/utils/logger.js +312 -312
  496. package/utils/parse-range.js +25 -25
  497. package/utils/perf.js +59 -59
  498. package/utils/torrent-source-key.js +38 -38
@@ -1,1165 +1,1165 @@
1
- /**
2
- * @file Where a Matroska file's clusters are, found from the bytes already
3
- * downloaded, and the subtitle blocks inside them — RFC 9559 §5.1.3.
4
- *
5
- * **Why the Cues table is not enough.** It used to be the only way in: the walk
6
- * read the clusters the Cues table names for a subtitle track and nothing else.
7
- * RFC 9559 §22.1 says each subtitle frame SHOULD be referenced there — a
8
- * recommendation, not a guarantee — and a file may have no Cues at all. Such a
9
- * file showed no subtitles, ever, while a comment promised the walk would read
10
- * "the clusters the viewer's own playback brings in" instead. Nothing did.
11
- *
12
- * **Where a cluster begins is established three ways, and only the first two
13
- * are proof:**
14
- *
15
- * 1. the file says so — the first cluster after the head, and every
16
- * `CueClusterPosition` of every track (the video's name nearly every
17
- * cluster, because §22.1 has each keyframe referenced);
18
- * 2. a chain from a position already established: a level-1 element begins
19
- * exactly where the one before it ends, so from a cluster whose header is
20
- * downloaded the next one is known without reading anything else;
21
- * 3. a search of downloaded bytes for the Cluster id, used only where the file
22
- * has no Cues. What it finds is a CANDIDATE, checked against the structure a
23
- * cluster must have before anything is read out of it, and taken back —
24
- * together with every cue it gave — if a position established by the first
25
- * two ways later lands inside it.
26
- *
27
- * **What is kept between passes, and what is not.** A cluster read whole is
28
- * DONE and never read again. One whose bytes are not all downloaded is left as
29
- * it is: nothing about it is recorded, because "not here yet" is not a fact
30
- * about the cluster (`unavailable.js`). One whose downloaded, hash-verified
31
- * bytes do not parse is UNREADABLE, with its reason, and not tried again —
32
- * reading the same bytes again gives the same answer.
33
- *
34
- * **Order, and when a pass ends.** The cluster a viewer is standing in is read
35
- * first, then the one before it — a line on screen now may have started there.
36
- * A pass that had such clusters to read ends with them, so what is on screen is
37
- * pushed after two clusters rather than after the whole file; it says `more`,
38
- * and the caller runs the next pass at once. A pass with nothing of that kind
39
- * reads onward from the viewers, one viewer's next cluster after another's, and
40
- * then the rest — and stops, saying `more`, the moment a viewer is somewhere
41
- * else, so a seek is answered by the next pass and not after the whole film.
42
- */
43
-
44
- import { crc32 } from "node:zlib";
45
- import { readUint, readVint } from "./ebml-reader.js";
46
- import { ElementReader, MAX_HEADER_BYTES } from "./ebml-stream.js";
47
- import { isUnavailable } from "./unavailable.js";
48
-
49
- const ID_CLUSTER = 0x1f43b675;
50
- const CLUSTER_ID_BYTES = Buffer.from([0x1f, 0x43, 0xb6, 0x75]);
51
-
52
- /** The top-level elements of a Segment (RFC 9559 §5.1), Void and CRC-32 aside. */
53
- const LEVEL_ONE = new Set([
54
- ID_CLUSTER,
55
- 0x1c53bb6b, // Cues
56
- 0x114d9b74, // SeekHead
57
- 0x1549a966, // Info
58
- 0x1654ae6b, // Tracks
59
- 0x1043a770, // Chapters
60
- 0x1941a469, // Attachments
61
- 0x1254c367 // Tags
62
- ]);
63
-
64
- const ID_CRC32 = 0xbf;
65
- const ID_VOID = 0xec;
66
- const ID_TIMESTAMP = 0xe7;
67
- const ID_SILENT_TRACKS = 0x5854;
68
- const ID_POSITION = 0xa7;
69
- const ID_PREV_SIZE = 0xab;
70
- const ID_SIMPLE_BLOCK = 0xa3;
71
- const ID_BLOCK_GROUP = 0xa0;
72
- const ID_ENCRYPTED_BLOCK = 0xaf;
73
- const ID_BLOCK = 0xa1;
74
- const ID_BLOCK_DURATION = 0x9b;
75
-
76
- /** The children RFC 9559 §5.1.3 allows inside a Cluster, the global two included. */
77
- const CLUSTER_CHILDREN = new Set([
78
- ID_CRC32,
79
- ID_VOID,
80
- ID_TIMESTAMP,
81
- ID_SILENT_TRACKS,
82
- ID_POSITION,
83
- ID_PREV_SIZE,
84
- ID_SIMPLE_BLOCK,
85
- ID_BLOCK_GROUP,
86
- ID_ENCRYPTED_BLOCK
87
- ]);
88
-
89
- /**
90
- * Enough of a cluster's start to hold its header, a CRC-32 and the Timestamp
91
- * that follows it (§5.1.3.1: first, or second after a CRC-32).
92
- */
93
- const CLUSTER_START_PROBE = 64;
94
-
95
- /**
96
- * The bytes one read of a cluster's structure takes: an element header and the
97
- * start of a block's data, which names the block's track — a track number is a
98
- * vint of at most 8 bytes (RFC 9559 §10.1). Reading a cluster this way moves
99
- * the headers of its video and audio blocks across the thread boundary and
100
- * none of their frames.
101
- */
102
- const STRUCTURE_READ_BYTES = MAX_HEADER_BYTES + 8;
103
-
104
- /** Bits 1-2 of a block's flags byte say how it is laced, or that it is not. */
105
- const LACING_MASK = 0x06;
106
- const LACING_NONE = 0x00;
107
- const LACING_XIPH = 0x02;
108
- const LACING_FIXED = 0x04;
109
- const LACING_EBML = 0x06;
110
-
111
- /**
112
- * A block's own header: its track, its time relative to the cluster, its flags.
113
- *
114
- * @param {Buffer} buffer
115
- * @param {number} start - First byte of the block's data.
116
- * @param {number} end - One past its last byte.
117
- * @returns {{ trackNumber: number, relativeTicks: number, flags: number, dataOffset: number } | null}
118
- */
119
- export function readBlockHeader(buffer, start, end) {
120
- const track = readVint(buffer, start, false);
121
- if (!track || track.value === null) {
122
- return null;
123
- }
124
- const timestampAt = start + track.length;
125
- // Signed, and it can be negative: a block may belong slightly before the
126
- // cluster it is stored in.
127
- if (timestampAt + 3 > end) {
128
- return null;
129
- }
130
- return {
131
- trackNumber: Number(track.value),
132
- relativeTicks: buffer.readInt16BE(timestampAt),
133
- flags: buffer[timestampAt + 2],
134
- dataOffset: timestampAt + 3
135
- };
136
- }
137
-
138
- /**
139
- * Where a laced block's first frame begins.
140
- *
141
- * @param {Buffer} buffer
142
- * @param {number} dataOffset - First byte after the block header.
143
- * @param {number} end
144
- * @param {number} flags
145
- * @returns {number | null}
146
- */
147
- export function firstFrameOffset(buffer, dataOffset, end, flags) {
148
- const lacing = flags & LACING_MASK;
149
- if (lacing === LACING_NONE) {
150
- return dataOffset;
151
- }
152
- if (dataOffset >= end) {
153
- return null;
154
- }
155
- const frames = buffer[dataOffset] + 1;
156
- let at = dataOffset + 1;
157
- if (lacing === LACING_FIXED) {
158
- return at;
159
- }
160
- if (lacing === LACING_XIPH) {
161
- for (let frame = 0; frame < frames - 1; frame += 1) {
162
- while (at < end && buffer[at] === 0xff) {
163
- at += 1;
164
- }
165
- at += 1;
166
- }
167
- return at <= end ? at : null;
168
- }
169
- if (lacing === LACING_EBML) {
170
- for (let frame = 0; frame < frames - 1; frame += 1) {
171
- const size = readVint(buffer, at, false);
172
- if (!size) {
173
- return null;
174
- }
175
- at += size.length;
176
- }
177
- return at <= end ? at : null;
178
- }
179
- return null;
180
- }
181
-
182
- /**
183
- * The walk's own record of one file, kept by the caller between passes.
184
- *
185
- * @typedef {object} ClusterStart
186
- * @property {"first" | "cues" | "chain" | "search"} source
187
- * @property {number | null} parent - The start this one was chained from.
188
- * @property {number | null} seconds - The cluster's Timestamp, once read.
189
- * @property {"cluster" | "other" | null} kind - Null until its header is read.
190
- * @property {number | null} end - One past its last byte, null while unknown.
191
- * @property {boolean} stepped - Whether the next start has been taken from it.
192
- */
193
-
194
- /**
195
- * @param {object} progress
196
- * @returns {object}
197
- */
198
- function stateOf(progress) {
199
- if (!progress.matroska) {
200
- progress.matroska = {
201
- seeded: false,
202
- /** @type {Map<number, ClusterStart>} */
203
- starts: new Map(),
204
- done: new Set(),
205
- /** @type {Map<number, string>} */
206
- unreadable: new Map(),
207
- /** Candidates whose bytes are not all here yet. @type {Set<number>} */
208
- candidates: new Set(),
209
- /** Candidates the structure check refused. @type {Set<number>} */
210
- rejected: new Set(),
211
- /** Inclusive intervals the search has finished with. @type {Array<[number, number]>} */
212
- scanned: [],
213
- stats: { refusedElements: 0, rejectedCandidates: 0, contradictions: 0, bytesWalked: 0, fromSearch: 0 }
214
- };
215
- }
216
- return progress.matroska;
217
- }
218
-
219
- /**
220
- * The start a chain of starts began from.
221
- *
222
- * @param {Map<number, ClusterStart>} starts
223
- * @param {number} at
224
- * @returns {ClusterStart | undefined}
225
- */
226
- function rootOf(starts, at) {
227
- let current = starts.get(at);
228
- const seen = new Set();
229
- while (current && current.parent !== null && !seen.has(current.parent)) {
230
- seen.add(current.parent);
231
- const parent = starts.get(current.parent);
232
- if (!parent) {
233
- break;
234
- }
235
- current = parent;
236
- }
237
- return current;
238
- }
239
-
240
- /**
241
- * Whether every byte of an inclusive range lies in one downloaded run.
242
- *
243
- * @param {Array<[number, number]>} ranges
244
- * @param {number} start
245
- * @returns {number} One past the end of the run holding `start`, or `start`.
246
- */
247
- function runEndFrom(ranges, start) {
248
- for (const [from, to] of ranges) {
249
- if (start >= from && start <= to) {
250
- return to + 1;
251
- }
252
- }
253
- return start;
254
- }
255
-
256
- /**
257
- * The Timestamp of a cluster, read from its first children.
258
- *
259
- * Only the children inside the probe are looked at: the Timestamp is the first
260
- * child or the second after a CRC-32, and a cluster that puts it later is read
261
- * whole when it is processed.
262
- *
263
- * @param {ElementReader} reader
264
- * @param {{ dataOffset: number, end: number | null }} header
265
- * @param {number} limit
266
- * @param {number} secondsPerTick
267
- * @returns {Promise<number | null>}
268
- */
269
- async function leadingTimestamp(reader, header, limit, secondsPerTick) {
270
- let at = header.dataOffset;
271
- const stop = Math.min(limit, header.end ?? limit, header.dataOffset + CLUSTER_START_PROBE);
272
- while (at < stop) {
273
- const child = await reader.header(at, header.end ?? limit);
274
- if (!child || child.size === null) {
275
- return null;
276
- }
277
- if (child.id === ID_TIMESTAMP) {
278
- const bytes = await reader.bytes(child.dataOffset, child.size);
279
- return readUint(bytes, 0, child.size) * secondsPerTick;
280
- }
281
- if (child.id !== ID_CRC32 && child.id !== ID_VOID) {
282
- return null;
283
- }
284
- at = child.end;
285
- }
286
- return null;
287
- }
288
-
289
- /**
290
- * Take the next start from every start whose header is downloaded.
291
- *
292
- * @param {object} state
293
- * @param {object} plan
294
- * @param {import("./Container.js").HeldReader} held
295
- * @param {ElementReader} reader
296
- * @returns {Promise<void>}
297
- */
298
- async function extendChains(state, plan, held, reader) {
299
- const queue = [...state.starts.entries()].filter(([, start]) => !start.stepped).map(([at]) => at);
300
- while (queue.length > 0) {
301
- const at = queue.shift();
302
- const start = state.starts.get(at);
303
- if (!start || start.stepped || state.unreadable.has(at)) {
304
- continue;
305
- }
306
- const probeEnd = Math.min(plan.fileSize, at + CLUSTER_START_PROBE) - 1;
307
- if (!held.isHeld(at, Math.min(probeEnd, at + MAX_HEADER_BYTES - 1))) {
308
- continue;
309
- }
310
- let header;
311
- try {
312
- header = await reader.header(at, plan.segmentEnd);
313
- } catch (error) {
314
- if (isUnavailable(error)) {
315
- continue;
316
- }
317
- throw error;
318
- }
319
- start.stepped = true;
320
- if (!header || !(LEVEL_ONE.has(header.id) || header.id === ID_VOID || header.id === ID_CRC32)) {
321
- // A position the file itself names (or one a chain led to) that holds no
322
- // top-level element: the file is damaged here, and reading it again gives
323
- // the same bytes.
324
- state.unreadable.set(at, "no top-level element at a position established as a cluster start");
325
- continue;
326
- }
327
- start.kind = header.id === ID_CLUSTER ? "cluster" : "other";
328
- start.end = header.end;
329
- if (start.kind === "cluster" && start.seconds === null && held.isHeld(at, probeEnd)) {
330
- try {
331
- start.seconds = await leadingTimestamp(reader, header, plan.segmentEnd, plan.secondsPerTick);
332
- } catch (error) {
333
- if (!isUnavailable(error)) {
334
- throw error;
335
- }
336
- }
337
- }
338
- if (header.end === null) {
339
- // An unknown size is legal for a cluster (RFC 9559 §5.1.3); where it ends
340
- // is learned when it is read.
341
- continue;
342
- }
343
- addStart(state, header.end, plan, { source: "chain", parent: at }, queue);
344
- }
345
- }
346
-
347
- /**
348
- * @param {object} state
349
- * @param {number} at
350
- * @param {object} plan
351
- * @param {{ source: ClusterStart["source"], parent: number | null, seconds?: number | null }} origin
352
- * @param {number[]} [queue]
353
- * @returns {void}
354
- */
355
- function addStart(state, at, plan, origin, queue) {
356
- if (!(at < plan.segmentEnd) || at < plan.firstClusterAt) {
357
- return;
358
- }
359
- const known = state.starts.get(at);
360
- if (known) {
361
- // The same position reached a second way. A start the file established
362
- // outranks one the search found, and keeps nothing of its doubt.
363
- if (known.source === "search" && origin.source !== "search") {
364
- known.source = origin.source;
365
- known.parent = origin.parent;
366
- }
367
- if (known.seconds === null && Number.isFinite(origin.seconds)) {
368
- known.seconds = origin.seconds;
369
- }
370
- return;
371
- }
372
- state.starts.set(at, {
373
- source: origin.source,
374
- parent: origin.parent,
375
- seconds: Number.isFinite(origin.seconds) ? origin.seconds : null,
376
- kind: null,
377
- end: null,
378
- stepped: false
379
- });
380
- state.candidates.delete(at);
381
- queue?.push(at);
382
- }
383
-
384
- /**
385
- * The spans of the clusters already known, as inclusive intervals.
386
- *
387
- * @param {object} state
388
- * @returns {Array<[number, number]>}
389
- */
390
- function knownSpans(state) {
391
- const spans = [];
392
- for (const [at, start] of state.starts) {
393
- if (start.kind !== null && start.end !== null) {
394
- spans.push([at, start.end - 1]);
395
- }
396
- }
397
- return spans.sort((left, right) => left[0] - right[0]);
398
- }
399
-
400
- /**
401
- * `[from, to]` with every interval in `taken` removed.
402
- *
403
- * @param {number} from
404
- * @param {number} to
405
- * @param {Array<[number, number]>} taken - Sorted.
406
- * @returns {Array<[number, number]>}
407
- */
408
- function subtract(from, to, taken) {
409
- const left = [];
410
- let cursor = from;
411
- for (const [start, end] of taken) {
412
- if (end < cursor || start > to) {
413
- continue;
414
- }
415
- if (start > cursor) {
416
- left.push([cursor, start - 1]);
417
- }
418
- cursor = Math.max(cursor, end + 1);
419
- if (cursor > to) {
420
- break;
421
- }
422
- }
423
- if (cursor <= to) {
424
- left.push([cursor, to]);
425
- }
426
- return left;
427
- }
428
-
429
- /**
430
- * Search downloaded bytes the search has not finished with for the Cluster id.
431
- *
432
- * The last `MAX_HEADER_BYTES - 1` bytes of every downloaded run are left
433
- * unfinished: an id and a size cut by the end of the run are found when the run
434
- * grows, by searching those bytes again.
435
- *
436
- * Each position found is decided at once ({@link settleCandidate}); one that is
437
- * accepted is followed by its chain, and the search goes on after the chain
438
- * ends. A file whose clusters follow one another is therefore searched only up
439
- * to its first cluster after each gap, and the rest is known by its headers.
440
- *
441
- * @param {object} state
442
- * @param {object} plan
443
- * @param {import("./Container.js").HeldReader} held
444
- * @param {{ whole: ElementReader, structure: ElementReader }} readers
445
- * @returns {Promise<void>}
446
- */
447
- async function searchForClusters(state, plan, held, readers) {
448
- const portion = Number.isFinite(held.portionBytes) && held.portionBytes > 0 ? held.portionBytes : plan.fileSize;
449
- const idLength = CLUSTER_ID_BYTES.length;
450
- for (const [runFrom, runTo] of held.ranges) {
451
- const from = Math.max(runFrom, plan.firstClusterAt);
452
- const to = Math.min(runTo, plan.segmentEnd - 1);
453
- let at = from;
454
- while (at + idLength - 1 <= to) {
455
- // What is left to look at is worked out afresh at every step: a cluster
456
- // accepted below starts a chain, and the bytes the chain covers are
457
- // stepped over rather than searched. They are not marked as searched, so
458
- // if the chain is ever taken back they are searched then.
459
- const taken = [...state.scanned, ...knownSpans(state)].sort((left, right) => left[0] - right[0]);
460
- const next = subtract(at, to, taken)[0];
461
- if (!next || next[1] - next[0] + 1 < idLength) {
462
- break;
463
- }
464
- const [start, end] = next;
465
- const last = Math.min(end, start + portion - 1);
466
- const bytes = await held.read(start, last);
467
- state.stats.bytesWalked += bytes.length;
468
- let accepted = null;
469
- let found = bytes.indexOf(CLUSTER_ID_BYTES);
470
- while (found >= 0) {
471
- const position = start + found;
472
- if (!state.starts.has(position) && !state.rejected.has(position)) {
473
- const outcome = await settleCandidate(state, plan, held, readers, position);
474
- if (outcome === "accepted") {
475
- accepted = position;
476
- break;
477
- }
478
- }
479
- found = bytes.indexOf(CLUSTER_ID_BYTES, found + 1);
480
- }
481
- if (accepted !== null) {
482
- if (accepted - 1 >= start) {
483
- state.scanned = mergeIntervals([...state.scanned, [start, accepted - 1]]);
484
- }
485
- at = accepted;
486
- continue;
487
- }
488
- if (last < end) {
489
- // Every position whose whole id lies in this portion has been looked
490
- // at; the next portion overlaps by the id's length less one, so an id
491
- // cut by the boundary is found there.
492
- state.scanned = mergeIntervals([...state.scanned, [start, last - idLength + 1]]);
493
- at = last - idLength + 2;
494
- continue;
495
- }
496
- // Held back only where the RUN ends here: a span that ends at a known
497
- // element's boundary has nothing after it that could complete an id.
498
- const runEndsHere = end === to && runTo < plan.segmentEnd - 1;
499
- const finished = runEndsHere ? end - (MAX_HEADER_BYTES - 1) : end;
500
- if (finished >= start) {
501
- state.scanned = mergeIntervals([...state.scanned, [start, finished]]);
502
- }
503
- at = end + 1;
504
- }
505
- }
506
- }
507
-
508
- /**
509
- * Decide one position the search found the Cluster ID at.
510
- *
511
- * Accepted, it becomes a start and its chain is followed at once, so the
512
- * clusters after it are known by their headers and never searched for.
513
- *
514
- * @param {object} state
515
- * @param {object} plan
516
- * @param {import("./Container.js").HeldReader} held
517
- * @param {{ whole: ElementReader, structure: ElementReader }} readers
518
- * @param {number} at
519
- * @returns {Promise<"accepted" | "rejected" | "pending">}
520
- */
521
- async function settleCandidate(state, plan, held, readers, at) {
522
- if (knownSpans(state).some(([from, to]) => at > from && at <= to)) {
523
- // Inside an element whose bounds are known: not a cluster start.
524
- state.candidates.delete(at);
525
- state.rejected.add(at);
526
- return "rejected";
527
- }
528
- const verdict = await verifyCandidate(readers.whole, at, plan, state, held);
529
- if (verdict.status === "pending") {
530
- state.candidates.add(at);
531
- return "pending";
532
- }
533
- state.candidates.delete(at);
534
- if (verdict.status === "rejected") {
535
- state.rejected.add(at);
536
- state.stats.rejectedCandidates += 1;
537
- return "rejected";
538
- }
539
- addStart(state, at, plan, { source: "search", parent: null, seconds: verdict.seconds });
540
- const start = state.starts.get(at);
541
- if (start) {
542
- start.kind = "cluster";
543
- start.end = verdict.end;
544
- }
545
- await extendChains(state, plan, held, readers.structure);
546
- return "accepted";
547
- }
548
-
549
- /**
550
- * @param {Array<[number, number]>} intervals
551
- * @returns {Array<[number, number]>}
552
- */
553
- function mergeIntervals(intervals) {
554
- const sorted = [...intervals].sort((left, right) => left[0] - right[0]);
555
- const merged = [];
556
- for (const [start, end] of sorted) {
557
- const last = merged[merged.length - 1];
558
- if (last && start <= last[1] + 1) {
559
- last[1] = Math.max(last[1], end);
560
- } else {
561
- merged.push([start, end]);
562
- }
563
- }
564
- return merged;
565
- }
566
-
567
- /**
568
- * The confirmed starts nearest a position on either side, with their times.
569
- *
570
- * @param {object} state
571
- * @param {number} at
572
- * @returns {{ before: { at: number, start: ClusterStart } | null, after: { at: number, start: ClusterStart } | null }}
573
- */
574
- function neighboursOf(state, at) {
575
- let before = null;
576
- let after = null;
577
- for (const [position, start] of state.starts) {
578
- if (start.kind !== "cluster") {
579
- continue;
580
- }
581
- if (position < at && (!before || position > before.at)) {
582
- before = { at: position, start };
583
- } else if (position > at && (!after || position < after.at)) {
584
- after = { at: position, start };
585
- }
586
- }
587
- return { before, after };
588
- }
589
-
590
- /**
591
- * Check a candidate against everything a cluster must be.
592
- *
593
- * Every child is one RFC 9559 §5.1.3 allows; there is exactly one Timestamp,
594
- * wherever it stands; the children fill the cluster exactly; every block names a
595
- * track the file declares; a CRC-32, where there is one, matches; a PrevSize,
596
- * where there is one and the cluster before is known, matches; and the time
597
- * lies between the times of the known clusters either side.
598
- *
599
- * @param {ElementReader} reader
600
- * @param {number} at
601
- * @param {object} plan
602
- * @param {object} state
603
- * @param {import("./Container.js").HeldReader} held
604
- * @returns {Promise<{ status: "pending" } | { status: "rejected", reason: string } | { status: "confirmed", end: number, seconds: number }>}
605
- */
606
- async function verifyCandidate(reader, at, plan, state, held) {
607
- try {
608
- const header = await reader.header(at, plan.segmentEnd);
609
- if (!header || header.id !== ID_CLUSTER) {
610
- return { status: "rejected", reason: "no cluster header" };
611
- }
612
- if (header.end !== null && header.end > plan.segmentEnd) {
613
- return { status: "rejected", reason: "runs past the segment" };
614
- }
615
- if (header.end !== null && !held.isHeld(at, header.end - 1)) {
616
- return { status: "pending" };
617
- }
618
- const limit = header.end ?? plan.segmentEnd;
619
- let cursor = header.dataOffset;
620
- let timestamps = 0;
621
- let ticks = null;
622
- let crc = null;
623
- let crcEnd = null;
624
- let prevSize = null;
625
- let end = header.end;
626
- let first = true;
627
- while (cursor < limit) {
628
- if (!held.isHeld(cursor, Math.min(plan.fileSize, cursor + MAX_HEADER_BYTES) - 1)) {
629
- return { status: "pending" };
630
- }
631
- const child = await reader.header(cursor, limit);
632
- if (!child) {
633
- return { status: "rejected", reason: "a child header does not parse" };
634
- }
635
- if (header.end === null && LEVEL_ONE.has(child.id)) {
636
- end = cursor;
637
- break;
638
- }
639
- if (!CLUSTER_CHILDREN.has(child.id)) {
640
- return { status: "rejected", reason: `0x${child.id.toString(16)} is not a cluster child` };
641
- }
642
- if (child.size === null || child.end > limit) {
643
- return { status: "rejected", reason: "a child runs past the cluster" };
644
- }
645
- if (child.id === ID_CRC32) {
646
- if (!first || child.size !== 4) {
647
- return { status: "rejected", reason: "a CRC-32 that is not the first child" };
648
- }
649
- crc = (await reader.bytes(child.dataOffset, 4)).readUInt32LE(0);
650
- crcEnd = child.end;
651
- } else if (child.id === ID_TIMESTAMP) {
652
- timestamps += 1;
653
- ticks = readUint(await reader.bytes(child.dataOffset, child.size), 0, child.size);
654
- } else if (child.id === ID_PREV_SIZE) {
655
- prevSize = readUint(await reader.bytes(child.dataOffset, child.size), 0, child.size);
656
- } else if (child.id === ID_SIMPLE_BLOCK || child.id === ID_BLOCK_GROUP) {
657
- const track = await blockTrackOf(reader, child);
658
- if (track === null || !plan.allTrackNumbers.includes(track)) {
659
- return { status: "rejected", reason: "a block names no declared track" };
660
- }
661
- }
662
- first = false;
663
- cursor = child.end;
664
- }
665
- if (end === null) {
666
- end = limit;
667
- }
668
- if (cursor !== end) {
669
- return { status: "rejected", reason: "the children do not fill the cluster" };
670
- }
671
- if (timestamps !== 1) {
672
- return { status: "rejected", reason: `${timestamps} Timestamp elements` };
673
- }
674
- if (crc !== null) {
675
- // RFC 8794 §11.3.1: the CRC covers every byte of the parent's data after
676
- // the CRC-32 element itself, stored little-endian.
677
- const computed = await crcOf(reader, crcEnd, end, held.portionBytes);
678
- if (computed !== crc) {
679
- return { status: "rejected", reason: "CRC-32 does not match" };
680
- }
681
- }
682
- const seconds = ticks * plan.secondsPerTick;
683
- const { before, after } = neighboursOf(state, at);
684
- if (prevSize !== null && before && before.start.end === at && prevSize !== at - before.at) {
685
- return { status: "rejected", reason: "PrevSize does not match the cluster before" };
686
- }
687
- if (before && before.start.seconds !== null && seconds < before.start.seconds) {
688
- return { status: "rejected", reason: "earlier than the cluster before it" };
689
- }
690
- if (after && after.start.seconds !== null && seconds > after.start.seconds) {
691
- return { status: "rejected", reason: "later than the cluster after it" };
692
- }
693
- return { status: "confirmed", end, seconds };
694
- } catch (error) {
695
- if (isUnavailable(error)) {
696
- return { status: "pending" };
697
- }
698
- throw error;
699
- }
700
- }
701
-
702
- /**
703
- * CRC-32 (IEEE 802.3, RFC 8794 §11.3.1) of a span, a portion at a time.
704
- *
705
- * @param {ElementReader} reader
706
- * @param {number} from
707
- * @param {number} to - One past the last byte.
708
- * @param {number} portionBytes
709
- * @returns {Promise<number>}
710
- */
711
- async function crcOf(reader, from, to, portionBytes) {
712
- const step = Number.isFinite(portionBytes) && portionBytes > 0 ? portionBytes : to - from;
713
- let value = 0;
714
- for (let at = from; at < to; at += step) {
715
- value = crc32(await reader.bytes(at, Math.min(step, to - at)), value);
716
- }
717
- return value >>> 0;
718
- }
719
-
720
- /**
721
- * The track a SimpleBlock or a BlockGroup's Block belongs to, read from its
722
- * first bytes only.
723
- *
724
- * @param {ElementReader} reader
725
- * @param {import("./ebml-stream.js").ElementHeader} child
726
- * @returns {Promise<number | null>}
727
- */
728
- async function blockTrackOf(reader, child) {
729
- if (child.id === ID_SIMPLE_BLOCK) {
730
- const head = await reader.peek(child, 8);
731
- const track = readVint(head, 0, false);
732
- return track ? track.value : null;
733
- }
734
- let at = child.dataOffset;
735
- while (at < child.end) {
736
- const inner = await reader.header(at, child.end);
737
- if (!inner || inner.size === null) {
738
- return null;
739
- }
740
- if (inner.id === ID_BLOCK) {
741
- const head = await reader.peek(inner, 8);
742
- const track = readVint(head, 0, false);
743
- return track ? track.value : null;
744
- }
745
- at = inner.end;
746
- }
747
- return null;
748
- }
749
-
750
- /**
751
- * Take back a start the search found, and everything chained from it, when a
752
- * start the file established lands inside it — or it inside one.
753
- *
754
- * @param {object} state
755
- * @returns {number[]} The cluster starts withdrawn, whose cues must be taken back.
756
- */
757
- function withdrawContradicted(state) {
758
- const established = [];
759
- const searched = [];
760
- for (const [at, start] of state.starts) {
761
- if (start.kind !== "cluster") {
762
- continue;
763
- }
764
- const root = rootOf(state.starts, at);
765
- (root?.source === "search" ? searched : established).push([at, start]);
766
- }
767
- const contradicted = new Set();
768
- for (const [at, start] of searched) {
769
- for (const [other, otherStart] of established) {
770
- const insideIt = start.end !== null && other > at && other < start.end;
771
- const itInside = otherStart.end !== null && at > other && at < otherStart.end;
772
- if (insideIt || itInside) {
773
- contradicted.add(at);
774
- break;
775
- }
776
- }
777
- }
778
- if (contradicted.size === 0) {
779
- return [];
780
- }
781
- // Everything chained from a withdrawn start goes with it.
782
- let grew = true;
783
- while (grew) {
784
- grew = false;
785
- for (const [at, start] of state.starts) {
786
- if (!contradicted.has(at) && start.parent !== null && contradicted.has(start.parent)) {
787
- contradicted.add(at);
788
- grew = true;
789
- }
790
- }
791
- }
792
- const withdrawn = [];
793
- for (const at of contradicted) {
794
- const start = state.starts.get(at);
795
- state.starts.delete(at);
796
- state.rejected.add(at);
797
- if (state.done.delete(at) && start?.kind === "cluster") {
798
- withdrawn.push(at);
799
- }
800
- }
801
- state.stats.contradictions += contradicted.size;
802
- return withdrawn;
803
- }
804
-
805
- /**
806
- * The order to read pending clusters in: the ones viewers stand in now, and the
807
- * rest.
808
- *
809
- * Which cluster a viewer stands in is decided over EVERY cluster whose time is
810
- * known, read or not: deciding it over the pending ones only would name an
811
- * earlier cluster whenever the viewer's own one had already been read.
812
- *
813
- * @param {Array<[number, ClusterStart]>} pending
814
- * @param {Array<[number, ClusterStart]>} clusters - Every known cluster start.
815
- * @param {number[]} wantedSeconds
816
- * @returns {{ urgent: number[], rest: number[] }}
817
- */
818
- function readingOrder(pending, clusters, wantedSeconds) {
819
- const isPending = new Set(pending.map(([at]) => at));
820
- const timed = clusters.filter(([, start]) => start.seconds !== null).sort((left, right) => left[1].seconds - right[1].seconds);
821
- const lists = [];
822
- const urgent = [];
823
- for (const seconds of wantedSeconds ?? []) {
824
- if (!Number.isFinite(seconds)) {
825
- continue;
826
- }
827
- let containing = -1;
828
- for (let index = 0; index < timed.length; index += 1) {
829
- if (timed[index][1].seconds <= seconds) {
830
- containing = index;
831
- } else {
832
- break;
833
- }
834
- }
835
- if (containing >= 0) {
836
- urgent.push(timed[containing][0]);
837
- if (containing > 0) {
838
- urgent.push(timed[containing - 1][0]);
839
- }
840
- }
841
- const list = [];
842
- for (let index = containing + 1; index < timed.length; index += 1) {
843
- list.push(timed[index][0]);
844
- }
845
- lists.push(list.filter((at) => isPending.has(at)));
846
- }
847
- const now = [...new Set(urgent.filter((at) => isPending.has(at)))];
848
- const order = [];
849
- const seen = new Set(now);
850
- // One viewer's next cluster after another's, so nobody waits for another
851
- // viewer's whole film.
852
- for (let step = 0; lists.some((list) => step < list.length); step += 1) {
853
- for (const list of lists) {
854
- const at = list[step];
855
- if (at !== undefined && !seen.has(at)) {
856
- seen.add(at);
857
- order.push(at);
858
- }
859
- }
860
- }
861
- for (const [at] of [...pending].sort((left, right) => left[0] - right[0])) {
862
- if (!seen.has(at)) {
863
- order.push(at);
864
- }
865
- }
866
- return { urgent: now, rest: order };
867
- }
868
-
869
- /**
870
- * Where the viewers stand right now, however the reader states it.
871
- *
872
- * @param {import("./Container.js").HeldReader} held
873
- * @returns {number[]}
874
- */
875
- function wantedNow(held) {
876
- const stated = typeof held.wantedSeconds === "function" ? held.wantedSeconds() : held.wantedSeconds;
877
- return Array.isArray(stated) ? stated.filter((value) => Number.isFinite(value)) : [];
878
- }
879
-
880
- /**
881
- * Whether a cluster's bytes are all here to be read now — known only for a
882
- * cluster whose size is known.
883
- *
884
- * @param {Map<number, ClusterStart>} starts
885
- * @param {number} at
886
- * @param {import("./Container.js").HeldReader} held
887
- * @returns {boolean}
888
- */
889
- function readableNow(starts, at, held) {
890
- const start = starts.get(at);
891
- return Boolean(start && start.end !== null && held.isHeld(at, start.end - 1));
892
- }
893
-
894
- /**
895
- * Read one cluster's subtitle blocks.
896
- *
897
- * @param {{ structure: ElementReader, exact: ElementReader }} readers - Headers
898
- * and track numbers through `structure`; a subtitle block's data through
899
- * `exact`, which reads that element and nothing more.
900
- * @param {number} at
901
- * @param {ClusterStart} start
902
- * @param {object} plan
903
- * @param {object} state
904
- * @param {import("./Container.js").HeldReader} held
905
- * @returns {Promise<{ status: "pending" } | { status: "unreadable", reason: string } | { status: "done", blocks: object[], end: number }>}
906
- */
907
- async function readCluster({ structure: reader, exact }, at, start, plan, state, held) {
908
- try {
909
- if (start.end !== null && !held.isHeld(at, start.end - 1)) {
910
- return { status: "pending" };
911
- }
912
- const header = await reader.header(at, plan.segmentEnd);
913
- if (!header || header.id !== ID_CLUSTER) {
914
- return { status: "unreadable", reason: "no cluster header" };
915
- }
916
- const limit = header.end ?? plan.segmentEnd;
917
- const wanted = new Set(plan.subtitleTrackNumbers);
918
- const raw = [];
919
- let ticks = null;
920
- let cursor = header.dataOffset;
921
- let end = header.end;
922
- while (cursor < limit) {
923
- if (!held.isHeld(cursor, Math.min(plan.fileSize, cursor + MAX_HEADER_BYTES) - 1)) {
924
- return { status: "pending" };
925
- }
926
- const child = await reader.header(cursor, limit);
927
- if (!child) {
928
- return { status: "unreadable", reason: "a child header does not parse" };
929
- }
930
- if (header.end === null && LEVEL_ONE.has(child.id)) {
931
- end = cursor;
932
- break;
933
- }
934
- if (child.size === null || child.end > limit) {
935
- return { status: "unreadable", reason: "a child runs past the cluster" };
936
- }
937
- if (header.end === null && !held.isHeld(child.at, child.end - 1)) {
938
- return { status: "pending" };
939
- }
940
- if (child.id === ID_TIMESTAMP) {
941
- ticks = readUint(await reader.bytes(child.dataOffset, child.size), 0, child.size);
942
- } else if (child.id === ID_SIMPLE_BLOCK || child.id === ID_BLOCK_GROUP) {
943
- const track = await blockTrackOf(reader, child);
944
- if (track !== null && wanted.has(track)) {
945
- const data = await exact.data(child);
946
- if (data === null) {
947
- state.stats.refusedElements += 1;
948
- } else {
949
- const block = child.id === ID_SIMPLE_BLOCK ? simpleBlockOf(data) : blockGroupOf(data);
950
- if (block) {
951
- raw.push(block);
952
- }
953
- }
954
- }
955
- }
956
- cursor = child.end;
957
- }
958
- if (end === null) {
959
- end = limit;
960
- }
961
- if (ticks === null) {
962
- return { status: "unreadable", reason: "a cluster with no Timestamp" };
963
- }
964
- state.stats.bytesWalked += end - at;
965
- const blocks = raw.map((block) => {
966
- const startSeconds = (ticks + block.relativeTicks) * plan.secondsPerTick;
967
- return {
968
- trackNumber: block.trackNumber,
969
- startSeconds,
970
- endSeconds: block.durationTicks === null ? null : startSeconds + block.durationTicks * plan.secondsPerTick,
971
- payload: block.payload,
972
- source: at
973
- };
974
- });
975
- return { status: "done", blocks, end, seconds: ticks * plan.secondsPerTick };
976
- } catch (error) {
977
- if (isUnavailable(error)) {
978
- return { status: "pending" };
979
- }
980
- throw error;
981
- }
982
- }
983
-
984
- /**
985
- * @param {Buffer} data - A SimpleBlock's data.
986
- * @returns {{ trackNumber: number, relativeTicks: number, durationTicks: null, payload: Buffer } | null}
987
- */
988
- function simpleBlockOf(data) {
989
- const header = readBlockHeader(data, 0, data.length);
990
- if (!header) {
991
- return null;
992
- }
993
- const payloadAt = firstFrameOffset(data, header.dataOffset, data.length, header.flags);
994
- if (payloadAt === null || payloadAt >= data.length) {
995
- return null;
996
- }
997
- return { trackNumber: header.trackNumber, relativeTicks: header.relativeTicks, durationTicks: null, payload: data.subarray(payloadAt) };
998
- }
999
-
1000
- /**
1001
- * @param {Buffer} data - A BlockGroup's data.
1002
- * @returns {{ trackNumber: number, relativeTicks: number, durationTicks: number | null, payload: Buffer } | null}
1003
- */
1004
- function blockGroupOf(data) {
1005
- let block = null;
1006
- let durationTicks = null;
1007
- let at = 0;
1008
- while (at < data.length) {
1009
- const id = readVint(data, at, true);
1010
- const size = id && readVint(data, at + id.length, false);
1011
- if (!id || !size) {
1012
- return null;
1013
- }
1014
- const dataOffset = at + id.length + size.length;
1015
- const end = dataOffset + size.value;
1016
- if (end > data.length) {
1017
- return null;
1018
- }
1019
- if (id.value === ID_BLOCK) {
1020
- block = { start: dataOffset, end };
1021
- } else if (id.value === ID_BLOCK_DURATION) {
1022
- durationTicks = readUint(data, dataOffset, size.value);
1023
- }
1024
- at = end;
1025
- }
1026
- if (!block) {
1027
- return null;
1028
- }
1029
- const header = readBlockHeader(data, block.start, block.end);
1030
- if (!header) {
1031
- return null;
1032
- }
1033
- const payloadAt = firstFrameOffset(data, header.dataOffset, block.end, header.flags);
1034
- if (payloadAt === null || payloadAt >= block.end) {
1035
- return null;
1036
- }
1037
- return { trackNumber: header.trackNumber, relativeTicks: header.relativeTicks, durationTicks, payload: data.subarray(payloadAt, block.end) };
1038
- }
1039
-
1040
- /**
1041
- * One pass over what a Matroska file holds now.
1042
- *
1043
- * @param {object} params
1044
- * @param {object} params.plan - From `MatroskaContainer.readSubtitlePlan`.
1045
- * @param {object} params.progress - Kept by the caller between passes.
1046
- * @param {import("./Container.js").HeldReader} params.held
1047
- * @returns {Promise<{ found: Map<number, object[]>, covered: number, indexed: number, withdrawn: number[], more: boolean, stats: object }>}
1048
- * `more` says clusters are readable now that this pass left for the next.
1049
- */
1050
- export async function walkHeldClusters({ plan, progress, held }) {
1051
- const state = stateOf(progress);
1052
- const reader = new ElementReader({
1053
- read: held.read,
1054
- fileSize: plan.fileSize,
1055
- portionBytes: held.portionBytes,
1056
- prefetch: true,
1057
- readableUntil: (start) => runEndFrom(held.ranges, start)
1058
- });
1059
- if (!state.seeded) {
1060
- state.seeded = true;
1061
- if (Number.isFinite(plan.firstClusterAt)) {
1062
- addStart(state, plan.firstClusterAt, plan, { source: "first", parent: null });
1063
- }
1064
- for (const entry of plan.entryPoints ?? []) {
1065
- addStart(state, entry.at, plan, { source: "cues", parent: null, seconds: entry.seconds });
1066
- }
1067
- }
1068
-
1069
- // The structure of clusters is read a header at a time, and a subtitle
1070
- // block's data by its own size. The reader above takes a whole portion per
1071
- // read and is for checking a candidate the search found, whose every byte is
1072
- // needed; used for structure, it moved every frame of the film into this
1073
- // thread — measured on the addon host 2026-10-01, the collector then spent
1074
- // three times as long as the walk itself, and the loop's 99th-percentile
1075
- // delay doubled.
1076
- const structure = new ElementReader({
1077
- read: held.read,
1078
- fileSize: plan.fileSize,
1079
- portionBytes: STRUCTURE_READ_BYTES,
1080
- prefetch: true,
1081
- readableUntil: (start) => runEndFrom(held.ranges, start)
1082
- });
1083
- const exact = new ElementReader({ read: held.read, fileSize: plan.fileSize, portionBytes: held.portionBytes });
1084
- await extendChains(state, plan, held, structure);
1085
-
1086
- if (plan.cuesState === "absent") {
1087
- const readers = { whole: reader, structure };
1088
- // Candidates left waiting for bytes by an earlier pass, then the bytes not
1089
- // yet looked at.
1090
- for (const at of [...state.candidates].sort((left, right) => left - right)) {
1091
- if (state.starts.has(at)) {
1092
- state.candidates.delete(at);
1093
- continue;
1094
- }
1095
- await settleCandidate(state, plan, held, readers, at);
1096
- }
1097
- await searchForClusters(state, plan, held, readers);
1098
- }
1099
-
1100
- const withdrawn = withdrawContradicted(state);
1101
-
1102
- /** @type {Map<number, object[]>} */
1103
- const found = new Map();
1104
- const clusters = [...state.starts.entries()].filter(([, start]) => start.kind === "cluster");
1105
- const pending = clusters.filter(([at]) => !state.done.has(at) && !state.unreadable.has(at));
1106
- const wanted = wantedNow(held);
1107
- const { urgent, rest } = readingOrder(pending, clusters, wanted);
1108
- const urgentReadable = urgent.filter((at) => readableNow(state.starts, at, held));
1109
- let more = false;
1110
- /** @type {number[]} */
1111
- let order;
1112
- if (urgentReadable.length > 0) {
1113
- order = urgentReadable;
1114
- more = rest.some((at) => readableNow(state.starts, at, held));
1115
- } else {
1116
- order = [...urgent, ...rest];
1117
- }
1118
- const asked = JSON.stringify(wanted);
1119
- for (const at of order) {
1120
- if (urgentReadable.length === 0 && JSON.stringify(wantedNow(held)) !== asked) {
1121
- // A viewer is somewhere else now: the next pass starts from there.
1122
- more = true;
1123
- break;
1124
- }
1125
- const start = state.starts.get(at);
1126
- if (!start) {
1127
- continue;
1128
- }
1129
- const result = await readCluster({ structure, exact }, at, start, plan, state, held);
1130
- if (result.status === "pending") {
1131
- continue;
1132
- }
1133
- if (result.status === "unreadable") {
1134
- state.unreadable.set(at, result.reason);
1135
- continue;
1136
- }
1137
- state.done.add(at);
1138
- if (start.end === null) {
1139
- start.end = result.end;
1140
- addStart(state, result.end, plan, { source: "chain", parent: at });
1141
- }
1142
- if (start.seconds === null) {
1143
- start.seconds = result.seconds;
1144
- }
1145
- if (rootOf(state.starts, at)?.source === "search") {
1146
- state.stats.fromSearch += 1;
1147
- }
1148
- for (const block of result.blocks) {
1149
- const into = found.get(block.trackNumber) ?? [];
1150
- into.push(block);
1151
- found.set(block.trackNumber, into);
1152
- }
1153
- }
1154
- // A cluster read whole may have made the start after an unknown-sized one
1155
- // known; its header is taken on the next pass, with everything else new.
1156
-
1157
- return {
1158
- found,
1159
- covered: state.done.size,
1160
- indexed: [...state.starts.values()].filter((start) => start.kind === "cluster").length,
1161
- withdrawn,
1162
- more,
1163
- stats: { ...state.stats, unreadable: state.unreadable.size, pendingCandidates: state.candidates.size }
1164
- };
1165
- }
1
+ /**
2
+ * @file Where a Matroska file's clusters are, found from the bytes already
3
+ * downloaded, and the subtitle blocks inside them — RFC 9559 §5.1.3.
4
+ *
5
+ * **Why the Cues table is not enough.** It used to be the only way in: the walk
6
+ * read the clusters the Cues table names for a subtitle track and nothing else.
7
+ * RFC 9559 §22.1 says each subtitle frame SHOULD be referenced there — a
8
+ * recommendation, not a guarantee — and a file may have no Cues at all. Such a
9
+ * file showed no subtitles, ever, while a comment promised the walk would read
10
+ * "the clusters the viewer's own playback brings in" instead. Nothing did.
11
+ *
12
+ * **Where a cluster begins is established three ways, and only the first two
13
+ * are proof:**
14
+ *
15
+ * 1. the file says so — the first cluster after the head, and every
16
+ * `CueClusterPosition` of every track (the video's name nearly every
17
+ * cluster, because §22.1 has each keyframe referenced);
18
+ * 2. a chain from a position already established: a level-1 element begins
19
+ * exactly where the one before it ends, so from a cluster whose header is
20
+ * downloaded the next one is known without reading anything else;
21
+ * 3. a search of downloaded bytes for the Cluster id, used only where the file
22
+ * has no Cues. What it finds is a CANDIDATE, checked against the structure a
23
+ * cluster must have before anything is read out of it, and taken back —
24
+ * together with every cue it gave — if a position established by the first
25
+ * two ways later lands inside it.
26
+ *
27
+ * **What is kept between passes, and what is not.** A cluster read whole is
28
+ * DONE and never read again. One whose bytes are not all downloaded is left as
29
+ * it is: nothing about it is recorded, because "not here yet" is not a fact
30
+ * about the cluster (`unavailable.js`). One whose downloaded, hash-verified
31
+ * bytes do not parse is UNREADABLE, with its reason, and not tried again —
32
+ * reading the same bytes again gives the same answer.
33
+ *
34
+ * **Order, and when a pass ends.** The cluster a viewer is standing in is read
35
+ * first, then the one before it — a line on screen now may have started there.
36
+ * A pass that had such clusters to read ends with them, so what is on screen is
37
+ * pushed after two clusters rather than after the whole file; it says `more`,
38
+ * and the caller runs the next pass at once. A pass with nothing of that kind
39
+ * reads onward from the viewers, one viewer's next cluster after another's, and
40
+ * then the rest — and stops, saying `more`, the moment a viewer is somewhere
41
+ * else, so a seek is answered by the next pass and not after the whole film.
42
+ */
43
+
44
+ import { crc32 } from "node:zlib";
45
+ import { readUint, readVint } from "./ebml-reader.js";
46
+ import { ElementReader, MAX_HEADER_BYTES } from "./ebml-stream.js";
47
+ import { isUnavailable } from "./unavailable.js";
48
+
49
+ const ID_CLUSTER = 0x1f43b675;
50
+ const CLUSTER_ID_BYTES = Buffer.from([0x1f, 0x43, 0xb6, 0x75]);
51
+
52
+ /** The top-level elements of a Segment (RFC 9559 §5.1), Void and CRC-32 aside. */
53
+ const LEVEL_ONE = new Set([
54
+ ID_CLUSTER,
55
+ 0x1c53bb6b, // Cues
56
+ 0x114d9b74, // SeekHead
57
+ 0x1549a966, // Info
58
+ 0x1654ae6b, // Tracks
59
+ 0x1043a770, // Chapters
60
+ 0x1941a469, // Attachments
61
+ 0x1254c367 // Tags
62
+ ]);
63
+
64
+ const ID_CRC32 = 0xbf;
65
+ const ID_VOID = 0xec;
66
+ const ID_TIMESTAMP = 0xe7;
67
+ const ID_SILENT_TRACKS = 0x5854;
68
+ const ID_POSITION = 0xa7;
69
+ const ID_PREV_SIZE = 0xab;
70
+ const ID_SIMPLE_BLOCK = 0xa3;
71
+ const ID_BLOCK_GROUP = 0xa0;
72
+ const ID_ENCRYPTED_BLOCK = 0xaf;
73
+ const ID_BLOCK = 0xa1;
74
+ const ID_BLOCK_DURATION = 0x9b;
75
+
76
+ /** The children RFC 9559 §5.1.3 allows inside a Cluster, the global two included. */
77
+ const CLUSTER_CHILDREN = new Set([
78
+ ID_CRC32,
79
+ ID_VOID,
80
+ ID_TIMESTAMP,
81
+ ID_SILENT_TRACKS,
82
+ ID_POSITION,
83
+ ID_PREV_SIZE,
84
+ ID_SIMPLE_BLOCK,
85
+ ID_BLOCK_GROUP,
86
+ ID_ENCRYPTED_BLOCK
87
+ ]);
88
+
89
+ /**
90
+ * Enough of a cluster's start to hold its header, a CRC-32 and the Timestamp
91
+ * that follows it (§5.1.3.1: first, or second after a CRC-32).
92
+ */
93
+ const CLUSTER_START_PROBE = 64;
94
+
95
+ /**
96
+ * The bytes one read of a cluster's structure takes: an element header and the
97
+ * start of a block's data, which names the block's track — a track number is a
98
+ * vint of at most 8 bytes (RFC 9559 §10.1). Reading a cluster this way moves
99
+ * the headers of its video and audio blocks across the thread boundary and
100
+ * none of their frames.
101
+ */
102
+ const STRUCTURE_READ_BYTES = MAX_HEADER_BYTES + 8;
103
+
104
+ /** Bits 1-2 of a block's flags byte say how it is laced, or that it is not. */
105
+ const LACING_MASK = 0x06;
106
+ const LACING_NONE = 0x00;
107
+ const LACING_XIPH = 0x02;
108
+ const LACING_FIXED = 0x04;
109
+ const LACING_EBML = 0x06;
110
+
111
+ /**
112
+ * A block's own header: its track, its time relative to the cluster, its flags.
113
+ *
114
+ * @param {Buffer} buffer
115
+ * @param {number} start - First byte of the block's data.
116
+ * @param {number} end - One past its last byte.
117
+ * @returns {{ trackNumber: number, relativeTicks: number, flags: number, dataOffset: number } | null}
118
+ */
119
+ export function readBlockHeader(buffer, start, end) {
120
+ const track = readVint(buffer, start, false);
121
+ if (!track || track.value === null) {
122
+ return null;
123
+ }
124
+ const timestampAt = start + track.length;
125
+ // Signed, and it can be negative: a block may belong slightly before the
126
+ // cluster it is stored in.
127
+ if (timestampAt + 3 > end) {
128
+ return null;
129
+ }
130
+ return {
131
+ trackNumber: Number(track.value),
132
+ relativeTicks: buffer.readInt16BE(timestampAt),
133
+ flags: buffer[timestampAt + 2],
134
+ dataOffset: timestampAt + 3
135
+ };
136
+ }
137
+
138
+ /**
139
+ * Where a laced block's first frame begins.
140
+ *
141
+ * @param {Buffer} buffer
142
+ * @param {number} dataOffset - First byte after the block header.
143
+ * @param {number} end
144
+ * @param {number} flags
145
+ * @returns {number | null}
146
+ */
147
+ export function firstFrameOffset(buffer, dataOffset, end, flags) {
148
+ const lacing = flags & LACING_MASK;
149
+ if (lacing === LACING_NONE) {
150
+ return dataOffset;
151
+ }
152
+ if (dataOffset >= end) {
153
+ return null;
154
+ }
155
+ const frames = buffer[dataOffset] + 1;
156
+ let at = dataOffset + 1;
157
+ if (lacing === LACING_FIXED) {
158
+ return at;
159
+ }
160
+ if (lacing === LACING_XIPH) {
161
+ for (let frame = 0; frame < frames - 1; frame += 1) {
162
+ while (at < end && buffer[at] === 0xff) {
163
+ at += 1;
164
+ }
165
+ at += 1;
166
+ }
167
+ return at <= end ? at : null;
168
+ }
169
+ if (lacing === LACING_EBML) {
170
+ for (let frame = 0; frame < frames - 1; frame += 1) {
171
+ const size = readVint(buffer, at, false);
172
+ if (!size) {
173
+ return null;
174
+ }
175
+ at += size.length;
176
+ }
177
+ return at <= end ? at : null;
178
+ }
179
+ return null;
180
+ }
181
+
182
+ /**
183
+ * The walk's own record of one file, kept by the caller between passes.
184
+ *
185
+ * @typedef {object} ClusterStart
186
+ * @property {"first" | "cues" | "chain" | "search"} source
187
+ * @property {number | null} parent - The start this one was chained from.
188
+ * @property {number | null} seconds - The cluster's Timestamp, once read.
189
+ * @property {"cluster" | "other" | null} kind - Null until its header is read.
190
+ * @property {number | null} end - One past its last byte, null while unknown.
191
+ * @property {boolean} stepped - Whether the next start has been taken from it.
192
+ */
193
+
194
+ /**
195
+ * @param {object} progress
196
+ * @returns {object}
197
+ */
198
+ function stateOf(progress) {
199
+ if (!progress.matroska) {
200
+ progress.matroska = {
201
+ seeded: false,
202
+ /** @type {Map<number, ClusterStart>} */
203
+ starts: new Map(),
204
+ done: new Set(),
205
+ /** @type {Map<number, string>} */
206
+ unreadable: new Map(),
207
+ /** Candidates whose bytes are not all here yet. @type {Set<number>} */
208
+ candidates: new Set(),
209
+ /** Candidates the structure check refused. @type {Set<number>} */
210
+ rejected: new Set(),
211
+ /** Inclusive intervals the search has finished with. @type {Array<[number, number]>} */
212
+ scanned: [],
213
+ stats: { refusedElements: 0, rejectedCandidates: 0, contradictions: 0, bytesWalked: 0, fromSearch: 0 }
214
+ };
215
+ }
216
+ return progress.matroska;
217
+ }
218
+
219
+ /**
220
+ * The start a chain of starts began from.
221
+ *
222
+ * @param {Map<number, ClusterStart>} starts
223
+ * @param {number} at
224
+ * @returns {ClusterStart | undefined}
225
+ */
226
+ function rootOf(starts, at) {
227
+ let current = starts.get(at);
228
+ const seen = new Set();
229
+ while (current && current.parent !== null && !seen.has(current.parent)) {
230
+ seen.add(current.parent);
231
+ const parent = starts.get(current.parent);
232
+ if (!parent) {
233
+ break;
234
+ }
235
+ current = parent;
236
+ }
237
+ return current;
238
+ }
239
+
240
+ /**
241
+ * Whether every byte of an inclusive range lies in one downloaded run.
242
+ *
243
+ * @param {Array<[number, number]>} ranges
244
+ * @param {number} start
245
+ * @returns {number} One past the end of the run holding `start`, or `start`.
246
+ */
247
+ function runEndFrom(ranges, start) {
248
+ for (const [from, to] of ranges) {
249
+ if (start >= from && start <= to) {
250
+ return to + 1;
251
+ }
252
+ }
253
+ return start;
254
+ }
255
+
256
+ /**
257
+ * The Timestamp of a cluster, read from its first children.
258
+ *
259
+ * Only the children inside the probe are looked at: the Timestamp is the first
260
+ * child or the second after a CRC-32, and a cluster that puts it later is read
261
+ * whole when it is processed.
262
+ *
263
+ * @param {ElementReader} reader
264
+ * @param {{ dataOffset: number, end: number | null }} header
265
+ * @param {number} limit
266
+ * @param {number} secondsPerTick
267
+ * @returns {Promise<number | null>}
268
+ */
269
+ async function leadingTimestamp(reader, header, limit, secondsPerTick) {
270
+ let at = header.dataOffset;
271
+ const stop = Math.min(limit, header.end ?? limit, header.dataOffset + CLUSTER_START_PROBE);
272
+ while (at < stop) {
273
+ const child = await reader.header(at, header.end ?? limit);
274
+ if (!child || child.size === null) {
275
+ return null;
276
+ }
277
+ if (child.id === ID_TIMESTAMP) {
278
+ const bytes = await reader.bytes(child.dataOffset, child.size);
279
+ return readUint(bytes, 0, child.size) * secondsPerTick;
280
+ }
281
+ if (child.id !== ID_CRC32 && child.id !== ID_VOID) {
282
+ return null;
283
+ }
284
+ at = child.end;
285
+ }
286
+ return null;
287
+ }
288
+
289
+ /**
290
+ * Take the next start from every start whose header is downloaded.
291
+ *
292
+ * @param {object} state
293
+ * @param {object} plan
294
+ * @param {import("./Container.js").HeldReader} held
295
+ * @param {ElementReader} reader
296
+ * @returns {Promise<void>}
297
+ */
298
+ async function extendChains(state, plan, held, reader) {
299
+ const queue = [...state.starts.entries()].filter(([, start]) => !start.stepped).map(([at]) => at);
300
+ while (queue.length > 0) {
301
+ const at = queue.shift();
302
+ const start = state.starts.get(at);
303
+ if (!start || start.stepped || state.unreadable.has(at)) {
304
+ continue;
305
+ }
306
+ const probeEnd = Math.min(plan.fileSize, at + CLUSTER_START_PROBE) - 1;
307
+ if (!held.isHeld(at, Math.min(probeEnd, at + MAX_HEADER_BYTES - 1))) {
308
+ continue;
309
+ }
310
+ let header;
311
+ try {
312
+ header = await reader.header(at, plan.segmentEnd);
313
+ } catch (error) {
314
+ if (isUnavailable(error)) {
315
+ continue;
316
+ }
317
+ throw error;
318
+ }
319
+ start.stepped = true;
320
+ if (!header || !(LEVEL_ONE.has(header.id) || header.id === ID_VOID || header.id === ID_CRC32)) {
321
+ // A position the file itself names (or one a chain led to) that holds no
322
+ // top-level element: the file is damaged here, and reading it again gives
323
+ // the same bytes.
324
+ state.unreadable.set(at, "no top-level element at a position established as a cluster start");
325
+ continue;
326
+ }
327
+ start.kind = header.id === ID_CLUSTER ? "cluster" : "other";
328
+ start.end = header.end;
329
+ if (start.kind === "cluster" && start.seconds === null && held.isHeld(at, probeEnd)) {
330
+ try {
331
+ start.seconds = await leadingTimestamp(reader, header, plan.segmentEnd, plan.secondsPerTick);
332
+ } catch (error) {
333
+ if (!isUnavailable(error)) {
334
+ throw error;
335
+ }
336
+ }
337
+ }
338
+ if (header.end === null) {
339
+ // An unknown size is legal for a cluster (RFC 9559 §5.1.3); where it ends
340
+ // is learned when it is read.
341
+ continue;
342
+ }
343
+ addStart(state, header.end, plan, { source: "chain", parent: at }, queue);
344
+ }
345
+ }
346
+
347
+ /**
348
+ * @param {object} state
349
+ * @param {number} at
350
+ * @param {object} plan
351
+ * @param {{ source: ClusterStart["source"], parent: number | null, seconds?: number | null }} origin
352
+ * @param {number[]} [queue]
353
+ * @returns {void}
354
+ */
355
+ function addStart(state, at, plan, origin, queue) {
356
+ if (!(at < plan.segmentEnd) || at < plan.firstClusterAt) {
357
+ return;
358
+ }
359
+ const known = state.starts.get(at);
360
+ if (known) {
361
+ // The same position reached a second way. A start the file established
362
+ // outranks one the search found, and keeps nothing of its doubt.
363
+ if (known.source === "search" && origin.source !== "search") {
364
+ known.source = origin.source;
365
+ known.parent = origin.parent;
366
+ }
367
+ if (known.seconds === null && Number.isFinite(origin.seconds)) {
368
+ known.seconds = origin.seconds;
369
+ }
370
+ return;
371
+ }
372
+ state.starts.set(at, {
373
+ source: origin.source,
374
+ parent: origin.parent,
375
+ seconds: Number.isFinite(origin.seconds) ? origin.seconds : null,
376
+ kind: null,
377
+ end: null,
378
+ stepped: false
379
+ });
380
+ state.candidates.delete(at);
381
+ queue?.push(at);
382
+ }
383
+
384
+ /**
385
+ * The spans of the clusters already known, as inclusive intervals.
386
+ *
387
+ * @param {object} state
388
+ * @returns {Array<[number, number]>}
389
+ */
390
+ function knownSpans(state) {
391
+ const spans = [];
392
+ for (const [at, start] of state.starts) {
393
+ if (start.kind !== null && start.end !== null) {
394
+ spans.push([at, start.end - 1]);
395
+ }
396
+ }
397
+ return spans.sort((left, right) => left[0] - right[0]);
398
+ }
399
+
400
+ /**
401
+ * `[from, to]` with every interval in `taken` removed.
402
+ *
403
+ * @param {number} from
404
+ * @param {number} to
405
+ * @param {Array<[number, number]>} taken - Sorted.
406
+ * @returns {Array<[number, number]>}
407
+ */
408
+ function subtract(from, to, taken) {
409
+ const left = [];
410
+ let cursor = from;
411
+ for (const [start, end] of taken) {
412
+ if (end < cursor || start > to) {
413
+ continue;
414
+ }
415
+ if (start > cursor) {
416
+ left.push([cursor, start - 1]);
417
+ }
418
+ cursor = Math.max(cursor, end + 1);
419
+ if (cursor > to) {
420
+ break;
421
+ }
422
+ }
423
+ if (cursor <= to) {
424
+ left.push([cursor, to]);
425
+ }
426
+ return left;
427
+ }
428
+
429
+ /**
430
+ * Search downloaded bytes the search has not finished with for the Cluster id.
431
+ *
432
+ * The last `MAX_HEADER_BYTES - 1` bytes of every downloaded run are left
433
+ * unfinished: an id and a size cut by the end of the run are found when the run
434
+ * grows, by searching those bytes again.
435
+ *
436
+ * Each position found is decided at once ({@link settleCandidate}); one that is
437
+ * accepted is followed by its chain, and the search goes on after the chain
438
+ * ends. A file whose clusters follow one another is therefore searched only up
439
+ * to its first cluster after each gap, and the rest is known by its headers.
440
+ *
441
+ * @param {object} state
442
+ * @param {object} plan
443
+ * @param {import("./Container.js").HeldReader} held
444
+ * @param {{ whole: ElementReader, structure: ElementReader }} readers
445
+ * @returns {Promise<void>}
446
+ */
447
+ async function searchForClusters(state, plan, held, readers) {
448
+ const portion = Number.isFinite(held.portionBytes) && held.portionBytes > 0 ? held.portionBytes : plan.fileSize;
449
+ const idLength = CLUSTER_ID_BYTES.length;
450
+ for (const [runFrom, runTo] of held.ranges) {
451
+ const from = Math.max(runFrom, plan.firstClusterAt);
452
+ const to = Math.min(runTo, plan.segmentEnd - 1);
453
+ let at = from;
454
+ while (at + idLength - 1 <= to) {
455
+ // What is left to look at is worked out afresh at every step: a cluster
456
+ // accepted below starts a chain, and the bytes the chain covers are
457
+ // stepped over rather than searched. They are not marked as searched, so
458
+ // if the chain is ever taken back they are searched then.
459
+ const taken = [...state.scanned, ...knownSpans(state)].sort((left, right) => left[0] - right[0]);
460
+ const next = subtract(at, to, taken)[0];
461
+ if (!next || next[1] - next[0] + 1 < idLength) {
462
+ break;
463
+ }
464
+ const [start, end] = next;
465
+ const last = Math.min(end, start + portion - 1);
466
+ const bytes = await held.read(start, last);
467
+ state.stats.bytesWalked += bytes.length;
468
+ let accepted = null;
469
+ let found = bytes.indexOf(CLUSTER_ID_BYTES);
470
+ while (found >= 0) {
471
+ const position = start + found;
472
+ if (!state.starts.has(position) && !state.rejected.has(position)) {
473
+ const outcome = await settleCandidate(state, plan, held, readers, position);
474
+ if (outcome === "accepted") {
475
+ accepted = position;
476
+ break;
477
+ }
478
+ }
479
+ found = bytes.indexOf(CLUSTER_ID_BYTES, found + 1);
480
+ }
481
+ if (accepted !== null) {
482
+ if (accepted - 1 >= start) {
483
+ state.scanned = mergeIntervals([...state.scanned, [start, accepted - 1]]);
484
+ }
485
+ at = accepted;
486
+ continue;
487
+ }
488
+ if (last < end) {
489
+ // Every position whose whole id lies in this portion has been looked
490
+ // at; the next portion overlaps by the id's length less one, so an id
491
+ // cut by the boundary is found there.
492
+ state.scanned = mergeIntervals([...state.scanned, [start, last - idLength + 1]]);
493
+ at = last - idLength + 2;
494
+ continue;
495
+ }
496
+ // Held back only where the RUN ends here: a span that ends at a known
497
+ // element's boundary has nothing after it that could complete an id.
498
+ const runEndsHere = end === to && runTo < plan.segmentEnd - 1;
499
+ const finished = runEndsHere ? end - (MAX_HEADER_BYTES - 1) : end;
500
+ if (finished >= start) {
501
+ state.scanned = mergeIntervals([...state.scanned, [start, finished]]);
502
+ }
503
+ at = end + 1;
504
+ }
505
+ }
506
+ }
507
+
508
+ /**
509
+ * Decide one position the search found the Cluster ID at.
510
+ *
511
+ * Accepted, it becomes a start and its chain is followed at once, so the
512
+ * clusters after it are known by their headers and never searched for.
513
+ *
514
+ * @param {object} state
515
+ * @param {object} plan
516
+ * @param {import("./Container.js").HeldReader} held
517
+ * @param {{ whole: ElementReader, structure: ElementReader }} readers
518
+ * @param {number} at
519
+ * @returns {Promise<"accepted" | "rejected" | "pending">}
520
+ */
521
+ async function settleCandidate(state, plan, held, readers, at) {
522
+ if (knownSpans(state).some(([from, to]) => at > from && at <= to)) {
523
+ // Inside an element whose bounds are known: not a cluster start.
524
+ state.candidates.delete(at);
525
+ state.rejected.add(at);
526
+ return "rejected";
527
+ }
528
+ const verdict = await verifyCandidate(readers.whole, at, plan, state, held);
529
+ if (verdict.status === "pending") {
530
+ state.candidates.add(at);
531
+ return "pending";
532
+ }
533
+ state.candidates.delete(at);
534
+ if (verdict.status === "rejected") {
535
+ state.rejected.add(at);
536
+ state.stats.rejectedCandidates += 1;
537
+ return "rejected";
538
+ }
539
+ addStart(state, at, plan, { source: "search", parent: null, seconds: verdict.seconds });
540
+ const start = state.starts.get(at);
541
+ if (start) {
542
+ start.kind = "cluster";
543
+ start.end = verdict.end;
544
+ }
545
+ await extendChains(state, plan, held, readers.structure);
546
+ return "accepted";
547
+ }
548
+
549
+ /**
550
+ * @param {Array<[number, number]>} intervals
551
+ * @returns {Array<[number, number]>}
552
+ */
553
+ function mergeIntervals(intervals) {
554
+ const sorted = [...intervals].sort((left, right) => left[0] - right[0]);
555
+ const merged = [];
556
+ for (const [start, end] of sorted) {
557
+ const last = merged[merged.length - 1];
558
+ if (last && start <= last[1] + 1) {
559
+ last[1] = Math.max(last[1], end);
560
+ } else {
561
+ merged.push([start, end]);
562
+ }
563
+ }
564
+ return merged;
565
+ }
566
+
567
+ /**
568
+ * The confirmed starts nearest a position on either side, with their times.
569
+ *
570
+ * @param {object} state
571
+ * @param {number} at
572
+ * @returns {{ before: { at: number, start: ClusterStart } | null, after: { at: number, start: ClusterStart } | null }}
573
+ */
574
+ function neighboursOf(state, at) {
575
+ let before = null;
576
+ let after = null;
577
+ for (const [position, start] of state.starts) {
578
+ if (start.kind !== "cluster") {
579
+ continue;
580
+ }
581
+ if (position < at && (!before || position > before.at)) {
582
+ before = { at: position, start };
583
+ } else if (position > at && (!after || position < after.at)) {
584
+ after = { at: position, start };
585
+ }
586
+ }
587
+ return { before, after };
588
+ }
589
+
590
+ /**
591
+ * Check a candidate against everything a cluster must be.
592
+ *
593
+ * Every child is one RFC 9559 §5.1.3 allows; there is exactly one Timestamp,
594
+ * wherever it stands; the children fill the cluster exactly; every block names a
595
+ * track the file declares; a CRC-32, where there is one, matches; a PrevSize,
596
+ * where there is one and the cluster before is known, matches; and the time
597
+ * lies between the times of the known clusters either side.
598
+ *
599
+ * @param {ElementReader} reader
600
+ * @param {number} at
601
+ * @param {object} plan
602
+ * @param {object} state
603
+ * @param {import("./Container.js").HeldReader} held
604
+ * @returns {Promise<{ status: "pending" } | { status: "rejected", reason: string } | { status: "confirmed", end: number, seconds: number }>}
605
+ */
606
+ async function verifyCandidate(reader, at, plan, state, held) {
607
+ try {
608
+ const header = await reader.header(at, plan.segmentEnd);
609
+ if (!header || header.id !== ID_CLUSTER) {
610
+ return { status: "rejected", reason: "no cluster header" };
611
+ }
612
+ if (header.end !== null && header.end > plan.segmentEnd) {
613
+ return { status: "rejected", reason: "runs past the segment" };
614
+ }
615
+ if (header.end !== null && !held.isHeld(at, header.end - 1)) {
616
+ return { status: "pending" };
617
+ }
618
+ const limit = header.end ?? plan.segmentEnd;
619
+ let cursor = header.dataOffset;
620
+ let timestamps = 0;
621
+ let ticks = null;
622
+ let crc = null;
623
+ let crcEnd = null;
624
+ let prevSize = null;
625
+ let end = header.end;
626
+ let first = true;
627
+ while (cursor < limit) {
628
+ if (!held.isHeld(cursor, Math.min(plan.fileSize, cursor + MAX_HEADER_BYTES) - 1)) {
629
+ return { status: "pending" };
630
+ }
631
+ const child = await reader.header(cursor, limit);
632
+ if (!child) {
633
+ return { status: "rejected", reason: "a child header does not parse" };
634
+ }
635
+ if (header.end === null && LEVEL_ONE.has(child.id)) {
636
+ end = cursor;
637
+ break;
638
+ }
639
+ if (!CLUSTER_CHILDREN.has(child.id)) {
640
+ return { status: "rejected", reason: `0x${child.id.toString(16)} is not a cluster child` };
641
+ }
642
+ if (child.size === null || child.end > limit) {
643
+ return { status: "rejected", reason: "a child runs past the cluster" };
644
+ }
645
+ if (child.id === ID_CRC32) {
646
+ if (!first || child.size !== 4) {
647
+ return { status: "rejected", reason: "a CRC-32 that is not the first child" };
648
+ }
649
+ crc = (await reader.bytes(child.dataOffset, 4)).readUInt32LE(0);
650
+ crcEnd = child.end;
651
+ } else if (child.id === ID_TIMESTAMP) {
652
+ timestamps += 1;
653
+ ticks = readUint(await reader.bytes(child.dataOffset, child.size), 0, child.size);
654
+ } else if (child.id === ID_PREV_SIZE) {
655
+ prevSize = readUint(await reader.bytes(child.dataOffset, child.size), 0, child.size);
656
+ } else if (child.id === ID_SIMPLE_BLOCK || child.id === ID_BLOCK_GROUP) {
657
+ const track = await blockTrackOf(reader, child);
658
+ if (track === null || !plan.allTrackNumbers.includes(track)) {
659
+ return { status: "rejected", reason: "a block names no declared track" };
660
+ }
661
+ }
662
+ first = false;
663
+ cursor = child.end;
664
+ }
665
+ if (end === null) {
666
+ end = limit;
667
+ }
668
+ if (cursor !== end) {
669
+ return { status: "rejected", reason: "the children do not fill the cluster" };
670
+ }
671
+ if (timestamps !== 1) {
672
+ return { status: "rejected", reason: `${timestamps} Timestamp elements` };
673
+ }
674
+ if (crc !== null) {
675
+ // RFC 8794 §11.3.1: the CRC covers every byte of the parent's data after
676
+ // the CRC-32 element itself, stored little-endian.
677
+ const computed = await crcOf(reader, crcEnd, end, held.portionBytes);
678
+ if (computed !== crc) {
679
+ return { status: "rejected", reason: "CRC-32 does not match" };
680
+ }
681
+ }
682
+ const seconds = ticks * plan.secondsPerTick;
683
+ const { before, after } = neighboursOf(state, at);
684
+ if (prevSize !== null && before && before.start.end === at && prevSize !== at - before.at) {
685
+ return { status: "rejected", reason: "PrevSize does not match the cluster before" };
686
+ }
687
+ if (before && before.start.seconds !== null && seconds < before.start.seconds) {
688
+ return { status: "rejected", reason: "earlier than the cluster before it" };
689
+ }
690
+ if (after && after.start.seconds !== null && seconds > after.start.seconds) {
691
+ return { status: "rejected", reason: "later than the cluster after it" };
692
+ }
693
+ return { status: "confirmed", end, seconds };
694
+ } catch (error) {
695
+ if (isUnavailable(error)) {
696
+ return { status: "pending" };
697
+ }
698
+ throw error;
699
+ }
700
+ }
701
+
702
+ /**
703
+ * CRC-32 (IEEE 802.3, RFC 8794 §11.3.1) of a span, a portion at a time.
704
+ *
705
+ * @param {ElementReader} reader
706
+ * @param {number} from
707
+ * @param {number} to - One past the last byte.
708
+ * @param {number} portionBytes
709
+ * @returns {Promise<number>}
710
+ */
711
+ async function crcOf(reader, from, to, portionBytes) {
712
+ const step = Number.isFinite(portionBytes) && portionBytes > 0 ? portionBytes : to - from;
713
+ let value = 0;
714
+ for (let at = from; at < to; at += step) {
715
+ value = crc32(await reader.bytes(at, Math.min(step, to - at)), value);
716
+ }
717
+ return value >>> 0;
718
+ }
719
+
720
+ /**
721
+ * The track a SimpleBlock or a BlockGroup's Block belongs to, read from its
722
+ * first bytes only.
723
+ *
724
+ * @param {ElementReader} reader
725
+ * @param {import("./ebml-stream.js").ElementHeader} child
726
+ * @returns {Promise<number | null>}
727
+ */
728
+ async function blockTrackOf(reader, child) {
729
+ if (child.id === ID_SIMPLE_BLOCK) {
730
+ const head = await reader.peek(child, 8);
731
+ const track = readVint(head, 0, false);
732
+ return track ? track.value : null;
733
+ }
734
+ let at = child.dataOffset;
735
+ while (at < child.end) {
736
+ const inner = await reader.header(at, child.end);
737
+ if (!inner || inner.size === null) {
738
+ return null;
739
+ }
740
+ if (inner.id === ID_BLOCK) {
741
+ const head = await reader.peek(inner, 8);
742
+ const track = readVint(head, 0, false);
743
+ return track ? track.value : null;
744
+ }
745
+ at = inner.end;
746
+ }
747
+ return null;
748
+ }
749
+
750
+ /**
751
+ * Take back a start the search found, and everything chained from it, when a
752
+ * start the file established lands inside it — or it inside one.
753
+ *
754
+ * @param {object} state
755
+ * @returns {number[]} The cluster starts withdrawn, whose cues must be taken back.
756
+ */
757
+ function withdrawContradicted(state) {
758
+ const established = [];
759
+ const searched = [];
760
+ for (const [at, start] of state.starts) {
761
+ if (start.kind !== "cluster") {
762
+ continue;
763
+ }
764
+ const root = rootOf(state.starts, at);
765
+ (root?.source === "search" ? searched : established).push([at, start]);
766
+ }
767
+ const contradicted = new Set();
768
+ for (const [at, start] of searched) {
769
+ for (const [other, otherStart] of established) {
770
+ const insideIt = start.end !== null && other > at && other < start.end;
771
+ const itInside = otherStart.end !== null && at > other && at < otherStart.end;
772
+ if (insideIt || itInside) {
773
+ contradicted.add(at);
774
+ break;
775
+ }
776
+ }
777
+ }
778
+ if (contradicted.size === 0) {
779
+ return [];
780
+ }
781
+ // Everything chained from a withdrawn start goes with it.
782
+ let grew = true;
783
+ while (grew) {
784
+ grew = false;
785
+ for (const [at, start] of state.starts) {
786
+ if (!contradicted.has(at) && start.parent !== null && contradicted.has(start.parent)) {
787
+ contradicted.add(at);
788
+ grew = true;
789
+ }
790
+ }
791
+ }
792
+ const withdrawn = [];
793
+ for (const at of contradicted) {
794
+ const start = state.starts.get(at);
795
+ state.starts.delete(at);
796
+ state.rejected.add(at);
797
+ if (state.done.delete(at) && start?.kind === "cluster") {
798
+ withdrawn.push(at);
799
+ }
800
+ }
801
+ state.stats.contradictions += contradicted.size;
802
+ return withdrawn;
803
+ }
804
+
805
+ /**
806
+ * The order to read pending clusters in: the ones viewers stand in now, and the
807
+ * rest.
808
+ *
809
+ * Which cluster a viewer stands in is decided over EVERY cluster whose time is
810
+ * known, read or not: deciding it over the pending ones only would name an
811
+ * earlier cluster whenever the viewer's own one had already been read.
812
+ *
813
+ * @param {Array<[number, ClusterStart]>} pending
814
+ * @param {Array<[number, ClusterStart]>} clusters - Every known cluster start.
815
+ * @param {number[]} wantedSeconds
816
+ * @returns {{ urgent: number[], rest: number[] }}
817
+ */
818
+ function readingOrder(pending, clusters, wantedSeconds) {
819
+ const isPending = new Set(pending.map(([at]) => at));
820
+ const timed = clusters.filter(([, start]) => start.seconds !== null).sort((left, right) => left[1].seconds - right[1].seconds);
821
+ const lists = [];
822
+ const urgent = [];
823
+ for (const seconds of wantedSeconds ?? []) {
824
+ if (!Number.isFinite(seconds)) {
825
+ continue;
826
+ }
827
+ let containing = -1;
828
+ for (let index = 0; index < timed.length; index += 1) {
829
+ if (timed[index][1].seconds <= seconds) {
830
+ containing = index;
831
+ } else {
832
+ break;
833
+ }
834
+ }
835
+ if (containing >= 0) {
836
+ urgent.push(timed[containing][0]);
837
+ if (containing > 0) {
838
+ urgent.push(timed[containing - 1][0]);
839
+ }
840
+ }
841
+ const list = [];
842
+ for (let index = containing + 1; index < timed.length; index += 1) {
843
+ list.push(timed[index][0]);
844
+ }
845
+ lists.push(list.filter((at) => isPending.has(at)));
846
+ }
847
+ const now = [...new Set(urgent.filter((at) => isPending.has(at)))];
848
+ const order = [];
849
+ const seen = new Set(now);
850
+ // One viewer's next cluster after another's, so nobody waits for another
851
+ // viewer's whole film.
852
+ for (let step = 0; lists.some((list) => step < list.length); step += 1) {
853
+ for (const list of lists) {
854
+ const at = list[step];
855
+ if (at !== undefined && !seen.has(at)) {
856
+ seen.add(at);
857
+ order.push(at);
858
+ }
859
+ }
860
+ }
861
+ for (const [at] of [...pending].sort((left, right) => left[0] - right[0])) {
862
+ if (!seen.has(at)) {
863
+ order.push(at);
864
+ }
865
+ }
866
+ return { urgent: now, rest: order };
867
+ }
868
+
869
+ /**
870
+ * Where the viewers stand right now, however the reader states it.
871
+ *
872
+ * @param {import("./Container.js").HeldReader} held
873
+ * @returns {number[]}
874
+ */
875
+ function wantedNow(held) {
876
+ const stated = typeof held.wantedSeconds === "function" ? held.wantedSeconds() : held.wantedSeconds;
877
+ return Array.isArray(stated) ? stated.filter((value) => Number.isFinite(value)) : [];
878
+ }
879
+
880
+ /**
881
+ * Whether a cluster's bytes are all here to be read now — known only for a
882
+ * cluster whose size is known.
883
+ *
884
+ * @param {Map<number, ClusterStart>} starts
885
+ * @param {number} at
886
+ * @param {import("./Container.js").HeldReader} held
887
+ * @returns {boolean}
888
+ */
889
+ function readableNow(starts, at, held) {
890
+ const start = starts.get(at);
891
+ return Boolean(start && start.end !== null && held.isHeld(at, start.end - 1));
892
+ }
893
+
894
+ /**
895
+ * Read one cluster's subtitle blocks.
896
+ *
897
+ * @param {{ structure: ElementReader, exact: ElementReader }} readers - Headers
898
+ * and track numbers through `structure`; a subtitle block's data through
899
+ * `exact`, which reads that element and nothing more.
900
+ * @param {number} at
901
+ * @param {ClusterStart} start
902
+ * @param {object} plan
903
+ * @param {object} state
904
+ * @param {import("./Container.js").HeldReader} held
905
+ * @returns {Promise<{ status: "pending" } | { status: "unreadable", reason: string } | { status: "done", blocks: object[], end: number }>}
906
+ */
907
+ async function readCluster({ structure: reader, exact }, at, start, plan, state, held) {
908
+ try {
909
+ if (start.end !== null && !held.isHeld(at, start.end - 1)) {
910
+ return { status: "pending" };
911
+ }
912
+ const header = await reader.header(at, plan.segmentEnd);
913
+ if (!header || header.id !== ID_CLUSTER) {
914
+ return { status: "unreadable", reason: "no cluster header" };
915
+ }
916
+ const limit = header.end ?? plan.segmentEnd;
917
+ const wanted = new Set(plan.subtitleTrackNumbers);
918
+ const raw = [];
919
+ let ticks = null;
920
+ let cursor = header.dataOffset;
921
+ let end = header.end;
922
+ while (cursor < limit) {
923
+ if (!held.isHeld(cursor, Math.min(plan.fileSize, cursor + MAX_HEADER_BYTES) - 1)) {
924
+ return { status: "pending" };
925
+ }
926
+ const child = await reader.header(cursor, limit);
927
+ if (!child) {
928
+ return { status: "unreadable", reason: "a child header does not parse" };
929
+ }
930
+ if (header.end === null && LEVEL_ONE.has(child.id)) {
931
+ end = cursor;
932
+ break;
933
+ }
934
+ if (child.size === null || child.end > limit) {
935
+ return { status: "unreadable", reason: "a child runs past the cluster" };
936
+ }
937
+ if (header.end === null && !held.isHeld(child.at, child.end - 1)) {
938
+ return { status: "pending" };
939
+ }
940
+ if (child.id === ID_TIMESTAMP) {
941
+ ticks = readUint(await reader.bytes(child.dataOffset, child.size), 0, child.size);
942
+ } else if (child.id === ID_SIMPLE_BLOCK || child.id === ID_BLOCK_GROUP) {
943
+ const track = await blockTrackOf(reader, child);
944
+ if (track !== null && wanted.has(track)) {
945
+ const data = await exact.data(child);
946
+ if (data === null) {
947
+ state.stats.refusedElements += 1;
948
+ } else {
949
+ const block = child.id === ID_SIMPLE_BLOCK ? simpleBlockOf(data) : blockGroupOf(data);
950
+ if (block) {
951
+ raw.push(block);
952
+ }
953
+ }
954
+ }
955
+ }
956
+ cursor = child.end;
957
+ }
958
+ if (end === null) {
959
+ end = limit;
960
+ }
961
+ if (ticks === null) {
962
+ return { status: "unreadable", reason: "a cluster with no Timestamp" };
963
+ }
964
+ state.stats.bytesWalked += end - at;
965
+ const blocks = raw.map((block) => {
966
+ const startSeconds = (ticks + block.relativeTicks) * plan.secondsPerTick;
967
+ return {
968
+ trackNumber: block.trackNumber,
969
+ startSeconds,
970
+ endSeconds: block.durationTicks === null ? null : startSeconds + block.durationTicks * plan.secondsPerTick,
971
+ payload: block.payload,
972
+ source: at
973
+ };
974
+ });
975
+ return { status: "done", blocks, end, seconds: ticks * plan.secondsPerTick };
976
+ } catch (error) {
977
+ if (isUnavailable(error)) {
978
+ return { status: "pending" };
979
+ }
980
+ throw error;
981
+ }
982
+ }
983
+
984
+ /**
985
+ * @param {Buffer} data - A SimpleBlock's data.
986
+ * @returns {{ trackNumber: number, relativeTicks: number, durationTicks: null, payload: Buffer } | null}
987
+ */
988
+ function simpleBlockOf(data) {
989
+ const header = readBlockHeader(data, 0, data.length);
990
+ if (!header) {
991
+ return null;
992
+ }
993
+ const payloadAt = firstFrameOffset(data, header.dataOffset, data.length, header.flags);
994
+ if (payloadAt === null || payloadAt >= data.length) {
995
+ return null;
996
+ }
997
+ return { trackNumber: header.trackNumber, relativeTicks: header.relativeTicks, durationTicks: null, payload: data.subarray(payloadAt) };
998
+ }
999
+
1000
+ /**
1001
+ * @param {Buffer} data - A BlockGroup's data.
1002
+ * @returns {{ trackNumber: number, relativeTicks: number, durationTicks: number | null, payload: Buffer } | null}
1003
+ */
1004
+ function blockGroupOf(data) {
1005
+ let block = null;
1006
+ let durationTicks = null;
1007
+ let at = 0;
1008
+ while (at < data.length) {
1009
+ const id = readVint(data, at, true);
1010
+ const size = id && readVint(data, at + id.length, false);
1011
+ if (!id || !size) {
1012
+ return null;
1013
+ }
1014
+ const dataOffset = at + id.length + size.length;
1015
+ const end = dataOffset + size.value;
1016
+ if (end > data.length) {
1017
+ return null;
1018
+ }
1019
+ if (id.value === ID_BLOCK) {
1020
+ block = { start: dataOffset, end };
1021
+ } else if (id.value === ID_BLOCK_DURATION) {
1022
+ durationTicks = readUint(data, dataOffset, size.value);
1023
+ }
1024
+ at = end;
1025
+ }
1026
+ if (!block) {
1027
+ return null;
1028
+ }
1029
+ const header = readBlockHeader(data, block.start, block.end);
1030
+ if (!header) {
1031
+ return null;
1032
+ }
1033
+ const payloadAt = firstFrameOffset(data, header.dataOffset, block.end, header.flags);
1034
+ if (payloadAt === null || payloadAt >= block.end) {
1035
+ return null;
1036
+ }
1037
+ return { trackNumber: header.trackNumber, relativeTicks: header.relativeTicks, durationTicks, payload: data.subarray(payloadAt, block.end) };
1038
+ }
1039
+
1040
+ /**
1041
+ * One pass over what a Matroska file holds now.
1042
+ *
1043
+ * @param {object} params
1044
+ * @param {object} params.plan - From `MatroskaContainer.readSubtitlePlan`.
1045
+ * @param {object} params.progress - Kept by the caller between passes.
1046
+ * @param {import("./Container.js").HeldReader} params.held
1047
+ * @returns {Promise<{ found: Map<number, object[]>, covered: number, indexed: number, withdrawn: number[], more: boolean, stats: object }>}
1048
+ * `more` says clusters are readable now that this pass left for the next.
1049
+ */
1050
+ export async function walkHeldClusters({ plan, progress, held }) {
1051
+ const state = stateOf(progress);
1052
+ const reader = new ElementReader({
1053
+ read: held.read,
1054
+ fileSize: plan.fileSize,
1055
+ portionBytes: held.portionBytes,
1056
+ prefetch: true,
1057
+ readableUntil: (start) => runEndFrom(held.ranges, start)
1058
+ });
1059
+ if (!state.seeded) {
1060
+ state.seeded = true;
1061
+ if (Number.isFinite(plan.firstClusterAt)) {
1062
+ addStart(state, plan.firstClusterAt, plan, { source: "first", parent: null });
1063
+ }
1064
+ for (const entry of plan.entryPoints ?? []) {
1065
+ addStart(state, entry.at, plan, { source: "cues", parent: null, seconds: entry.seconds });
1066
+ }
1067
+ }
1068
+
1069
+ // The structure of clusters is read a header at a time, and a subtitle
1070
+ // block's data by its own size. The reader above takes a whole portion per
1071
+ // read and is for checking a candidate the search found, whose every byte is
1072
+ // needed; used for structure, it moved every frame of the film into this
1073
+ // thread — measured on the addon host 2026-10-01, the collector then spent
1074
+ // three times as long as the walk itself, and the loop's 99th-percentile
1075
+ // delay doubled.
1076
+ const structure = new ElementReader({
1077
+ read: held.read,
1078
+ fileSize: plan.fileSize,
1079
+ portionBytes: STRUCTURE_READ_BYTES,
1080
+ prefetch: true,
1081
+ readableUntil: (start) => runEndFrom(held.ranges, start)
1082
+ });
1083
+ const exact = new ElementReader({ read: held.read, fileSize: plan.fileSize, portionBytes: held.portionBytes });
1084
+ await extendChains(state, plan, held, structure);
1085
+
1086
+ if (plan.cuesState === "absent") {
1087
+ const readers = { whole: reader, structure };
1088
+ // Candidates left waiting for bytes by an earlier pass, then the bytes not
1089
+ // yet looked at.
1090
+ for (const at of [...state.candidates].sort((left, right) => left - right)) {
1091
+ if (state.starts.has(at)) {
1092
+ state.candidates.delete(at);
1093
+ continue;
1094
+ }
1095
+ await settleCandidate(state, plan, held, readers, at);
1096
+ }
1097
+ await searchForClusters(state, plan, held, readers);
1098
+ }
1099
+
1100
+ const withdrawn = withdrawContradicted(state);
1101
+
1102
+ /** @type {Map<number, object[]>} */
1103
+ const found = new Map();
1104
+ const clusters = [...state.starts.entries()].filter(([, start]) => start.kind === "cluster");
1105
+ const pending = clusters.filter(([at]) => !state.done.has(at) && !state.unreadable.has(at));
1106
+ const wanted = wantedNow(held);
1107
+ const { urgent, rest } = readingOrder(pending, clusters, wanted);
1108
+ const urgentReadable = urgent.filter((at) => readableNow(state.starts, at, held));
1109
+ let more = false;
1110
+ /** @type {number[]} */
1111
+ let order;
1112
+ if (urgentReadable.length > 0) {
1113
+ order = urgentReadable;
1114
+ more = rest.some((at) => readableNow(state.starts, at, held));
1115
+ } else {
1116
+ order = [...urgent, ...rest];
1117
+ }
1118
+ const asked = JSON.stringify(wanted);
1119
+ for (const at of order) {
1120
+ if (urgentReadable.length === 0 && JSON.stringify(wantedNow(held)) !== asked) {
1121
+ // A viewer is somewhere else now: the next pass starts from there.
1122
+ more = true;
1123
+ break;
1124
+ }
1125
+ const start = state.starts.get(at);
1126
+ if (!start) {
1127
+ continue;
1128
+ }
1129
+ const result = await readCluster({ structure, exact }, at, start, plan, state, held);
1130
+ if (result.status === "pending") {
1131
+ continue;
1132
+ }
1133
+ if (result.status === "unreadable") {
1134
+ state.unreadable.set(at, result.reason);
1135
+ continue;
1136
+ }
1137
+ state.done.add(at);
1138
+ if (start.end === null) {
1139
+ start.end = result.end;
1140
+ addStart(state, result.end, plan, { source: "chain", parent: at });
1141
+ }
1142
+ if (start.seconds === null) {
1143
+ start.seconds = result.seconds;
1144
+ }
1145
+ if (rootOf(state.starts, at)?.source === "search") {
1146
+ state.stats.fromSearch += 1;
1147
+ }
1148
+ for (const block of result.blocks) {
1149
+ const into = found.get(block.trackNumber) ?? [];
1150
+ into.push(block);
1151
+ found.set(block.trackNumber, into);
1152
+ }
1153
+ }
1154
+ // A cluster read whole may have made the start after an unknown-sized one
1155
+ // known; its header is taken on the next pass, with everything else new.
1156
+
1157
+ return {
1158
+ found,
1159
+ covered: state.done.size,
1160
+ indexed: [...state.starts.values()].filter((start) => start.kind === "cluster").length,
1161
+ withdrawn,
1162
+ more,
1163
+ stats: { ...state.stats, unreadable: state.unreadable.size, pendingCandidates: state.candidates.size }
1164
+ };
1165
+ }