@torrent-tv/proxy 2.86.0 → 2.87.0

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 (382) hide show
  1. package/CHANGELOG.md +2138 -1915
  2. package/CLAUDE.md +318 -197
  3. package/assets/calibration/NOTICE.md +2 -2
  4. package/bin/cli.js +684 -616
  5. package/biome.json +409 -233
  6. package/docs/container-architecture.md +218 -213
  7. package/docs/download-architecture.md +7 -7
  8. package/docs/encode-architecture.md +287 -11
  9. package/docs/encode-run-state.md +2 -2
  10. package/docs/logs.md +97 -97
  11. package/docs/{disk-architecture.md → storage-architecture.md} +107 -1
  12. package/openspec/changes/adaptive-bitrate/design.md +2 -2
  13. package/openspec/changes/adaptive-bitrate/proposal.md +1 -1
  14. package/openspec/changes/adaptive-bitrate/tasks.md +1 -1
  15. package/openspec/changes/chunked-request-bodies/design.md +1 -1
  16. package/openspec/changes/chunked-request-bodies/proposal.md +1 -1
  17. package/openspec/changes/chunked-request-bodies/tasks.md +1 -1
  18. package/openspec/changes/cold-start/design.md +1 -1
  19. package/openspec/changes/disk-cap/proposal.md +1 -1
  20. package/openspec/changes/proxy-observability/proposal.md +2 -2
  21. package/openspec/changes/track-selection/proposal.md +1 -1
  22. package/openspec/changes/transcode-quality/proposal.md +1 -1
  23. package/package.json +2 -2
  24. package/research/delivery-wedge-stand-ladder-2026-09-13.md +329 -329
  25. package/research/double-spawn-2026-09-10.md +171 -171
  26. package/routes/api/playback-plan/post.js +2 -2
  27. package/routes/api/sources/files/get.js +1 -1
  28. package/routes/api/sources/stats/get.js +1 -1
  29. package/routes/api/sources/warm/post.js +10 -3
  30. package/routes/api/subtitles/get.js +230 -230
  31. package/routes/api/transcode-sessions/fragment-far/post.js +3 -3
  32. package/routes/api/transcode-sessions/net-report/post.js +129 -94
  33. package/routes/api/transcode-sessions/post.js +160 -139
  34. package/routes/api/transcode-sessions/progress/get.js +3 -3
  35. package/routes/api/transcode-sessions/release/post.js +3 -3
  36. package/routes/api/transcode-sessions/seek/post.js +12 -3
  37. package/routes/stream/get.js +2 -2
  38. package/routes/transcode/audio-file/get.js +15 -6
  39. package/routes/transcode/audio-warm/get.js +8 -5
  40. package/routes/transcode/session-file/get.js +429 -268
  41. package/routes/transcode/variant-file/get.js +47 -7
  42. package/routes/transcode/variant-warm/get.js +18 -6
  43. package/scripts/render-run-graph.js +1 -1
  44. package/server.js +720 -465
  45. package/services/encode/CoverageMap.js +428 -428
  46. package/services/encode/CushionReport.js +345 -0
  47. package/services/encode/EncodeAdmission.js +332 -0
  48. package/services/{orchestrators → encode}/EncodeOrchestrator.js +285 -17
  49. package/services/encode/EncodeRun.js +16 -9
  50. package/services/encode/EncodeRuns.js +1219 -0
  51. package/services/encode/Encoder.js +18 -2
  52. package/services/encode/EncoderSelection.js +38 -0
  53. package/services/encode/LocalObservations.js +359 -0
  54. package/services/encode/NvencEncoder.js +5 -0
  55. package/services/encode/OutputOpening.js +745 -0
  56. package/services/encode/OutputTimes.js +579 -0
  57. package/services/encode/Renditions.js +2162 -0
  58. package/services/encode/RunProgress.js +84 -0
  59. package/services/encode/SoftwareEncoder.js +12 -7
  60. package/services/encode/args.js +402 -186
  61. package/services/encode/calibration.js +361 -0
  62. package/services/encode/contention.js +128 -128
  63. package/services/encode/encode-run-state.js +16 -20
  64. package/services/encode/fingerprint.js +322 -0
  65. package/services/{hwaccel.js → encode/hwaccel.js} +82 -42
  66. package/services/{learned-median.js → encode/learned-median.js} +11 -0
  67. package/services/encode/output/EncodedOutput.js +57 -0
  68. package/services/encode/output/Output.js +131 -0
  69. package/services/{output/LiveOutputs.js → encode/output/OutputCatalog.js} +141 -40
  70. package/services/encode/output/OutputSpec.js +390 -0
  71. package/services/{output → encode/output}/Timeline.js +12 -37
  72. package/services/encode/output/index.js +20 -0
  73. package/services/encode/output/ladder.js +32 -0
  74. package/services/{output → encode/output}/playlists.js +3 -0
  75. package/services/encode/output-key-format.js +21 -0
  76. package/services/encode/quality/EncodeCost.js +1083 -0
  77. package/services/encode/quality/HostLoad.js +525 -0
  78. package/services/encode/quality/HostTimings.js +249 -0
  79. package/services/encode/quality/OutputQualityState.js +28 -0
  80. package/services/encode/quality/QualityController.js +931 -0
  81. package/services/encode/quality/QualityOffer.js +416 -0
  82. package/services/encode/quality/drain-threat.js +47 -0
  83. package/services/encode/quality/link-budget.js +254 -0
  84. package/services/encode/quality/output-format.js +266 -0
  85. package/services/encode/quality/serving-output.js +216 -0
  86. package/services/encode/quality/visible-rung.js +57 -0
  87. package/services/encode/run-command.js +23 -20
  88. package/services/encode/run-inputs.js +42 -0
  89. package/services/{segment-formats → encode/segment-formats}/fmp4.js +63 -0
  90. package/services/{segment-formats → encode/segment-formats}/index.js +3 -3
  91. package/services/encode/segment-formats/init-compat.js +324 -0
  92. package/services/{segment-formats → encode/segment-formats}/mp4-boxes.js +8 -3
  93. package/services/encode/throughput.js +97 -0
  94. package/services/{orchestrators → media}/ContainerOrchestrator.js +45 -7
  95. package/services/media/KeyframeTables.js +316 -0
  96. package/services/{source → media}/SourceFile.js +7 -55
  97. package/services/{torrent-worker/subtitle-cues.js → media/SubtitleCues.js} +103 -132
  98. package/services/media/SubtitleOrchestrator.js +167 -0
  99. package/services/{audio-inventory.js → media/audio-inventory.js} +7 -1
  100. package/services/{container → media/container}/AviContainer.js +266 -266
  101. package/services/{container → media/container}/Container.js +596 -601
  102. package/services/media/container/ContainerFactory.js +56 -0
  103. package/services/media/container/KeyframeTable.js +272 -0
  104. package/services/{container → media/container}/MatroskaContainer.js +1485 -1485
  105. package/services/{container → media/container}/Mp4Container.js +1244 -1244
  106. package/services/{container → media/container}/SubtitleFileContainer.js +322 -322
  107. package/services/media/keyframe-probe.js +157 -0
  108. package/services/media/media-info-probe.js +93 -0
  109. package/services/{playback-planner.js → media/playback-planner.js} +82 -39
  110. package/services/{tracks → media/tracks}/AudioTrack.js +131 -131
  111. package/services/{tracks → media/tracks}/language-detect.js +174 -174
  112. package/services/server/OutputLifecycle.js +418 -0
  113. package/services/server/SegmentServing.js +1146 -0
  114. package/services/server/ViewerRequests.js +522 -0
  115. package/services/server/controllers/PlaybackController.js +36 -0
  116. package/services/{controllers → server/controllers}/SubtitleController.js +17 -33
  117. package/services/server/wire-outputs.js +958 -0
  118. package/services/storage/Diagnostics.js +202 -0
  119. package/services/storage/MachineBudget.js +235 -0
  120. package/services/{piece-store → storage}/allowance.js +107 -107
  121. package/services/{core-dumps.js → storage/core-dumps.js} +1 -1
  122. package/services/{files → storage/files}/CompletedFiles.js +116 -1
  123. package/services/{disk → storage}/free.js +33 -0
  124. package/services/storage/machine-memory.js +56 -0
  125. package/services/{memory-report.js → storage/memory-report.js} +575 -596
  126. package/services/{piece-store → storage/piece-store}/piece-disk-store.js +0 -7
  127. package/services/{piece-store → storage/piece-store}/shared-piece-store.js +28 -21
  128. package/services/{encode → storage/segment-store}/SegmentStore.js +395 -8
  129. package/services/storage/wire.js +175 -0
  130. package/services/{download → torrent/download}/SwarmSelection.js +2 -2
  131. package/services/{download → torrent/download}/registry.js +2 -2
  132. package/services/torrent/files.js +1 -1
  133. package/services/torrent/piece-store-of.js +29 -0
  134. package/services/{torrent-pool.js → torrent/torrent-pool.js} +26 -119
  135. package/services/{torrent-worker → torrent/worker}/background-fill.js +2 -2
  136. package/services/{torrent-worker → torrent/worker}/channel.js +271 -250
  137. package/services/{torrent-worker → torrent/worker}/client.js +77 -58
  138. package/services/torrent/worker/held-bytes.js +155 -0
  139. package/services/{torrent-worker → torrent/worker}/install-webrtc-shim.js +1 -1
  140. package/services/{torrent-worker → torrent/worker}/piece-reader.js +21 -30
  141. package/services/{torrent-worker → torrent/worker}/pool-adapter.js +101 -137
  142. package/services/{torrent-worker → torrent/worker}/protocol.js +26 -30
  143. package/services/torrent/worker/resume-warm.js +107 -0
  144. package/services/{torrent-worker → torrent/worker}/webrtc-shim.js +2 -2
  145. package/services/{torrent-worker → torrent/worker}/worker.js +68 -159
  146. package/services/{data-channel-handler.js → transport/data-channel-handler.js} +1356 -1356
  147. package/services/{delivery-probe.js → transport/delivery-probe.js} +842 -842
  148. package/services/{health-collector.js → transport/health-collector.js} +6 -35
  149. package/services/{packet-witness.js → transport/packet-witness.js} +808 -784
  150. package/services/{port-mapper.js → transport/port-mapper.js} +1 -1
  151. package/services/{usrsctp-state.js → transport/usrsctp-state.js} +1 -1
  152. package/services/{webrtc-manager.js → transport/webrtc-manager.js} +1 -1
  153. package/services/viewer/Assignments.js +340 -0
  154. package/services/{priority → viewer}/PriorityMap.js +390 -390
  155. package/services/{priority → viewer}/PriorityOrchestrator.js +325 -320
  156. package/services/viewer/Viewer.js +657 -483
  157. package/services/viewer/Viewers.js +368 -257
  158. package/services/viewer/active-output.js +37 -54
  159. package/services/viewer/audio-start.js +4 -3
  160. package/services/viewer/buffer-trend.js +81 -0
  161. package/services/viewer/choices.js +647 -0
  162. package/services/viewer/positions.js +126 -120
  163. package/services/viewer/report-intake.js +46 -45
  164. package/test/asset-paths.test.js +28 -0
  165. package/test/audio-inventory.test.js +3 -3
  166. package/test/audio-track-bitrate.test.js +40 -0
  167. package/test/auto-quality-step.test.js +707 -515
  168. package/test/available-share.test.js +1 -1
  169. package/test/background-fill.test.js +1 -1
  170. package/test/client-log-file.test.js +110 -110
  171. package/test/completed-files.test.js +1 -1
  172. package/test/concurrent-cost.test.js +13 -3
  173. package/test/container-media-info.test.js +3 -3
  174. package/test/core-dumps.test.js +1 -1
  175. package/test/coverage-follows-the-disk.test.js +40 -8
  176. package/test/cue-push-carries-what-the-walk-found.test.js +58 -0
  177. package/test/cut-times-timeline.test.js +2 -2
  178. package/test/cuts-follow-published-grid.test.js +9 -8
  179. package/test/data-channel-frame.test.js +1 -1
  180. package/test/dead-torrent-handle.test.js +1 -1
  181. package/test/declared-rates.test.js +2 -2
  182. package/test/declared-tracks.test.js +2 -2
  183. package/test/decode-cost-fit.test.js +1 -1
  184. package/test/decode-cost.test.js +34 -25
  185. package/test/delivery-probe.test.js +1 -1
  186. package/test/demand-register.test.js +4 -4
  187. package/test/dht-bootstrap.test.js +1 -1
  188. package/test/drain-threat.test.js +65 -0
  189. package/test/duplicate-add.test.js +1 -1
  190. package/test/encode-admission.test.js +146 -0
  191. package/test/encode-cost.test.js +199 -10
  192. package/test/encode-orchestrator.test.js +39 -1
  193. package/test/encode-plan-viewers.test.js +2 -2
  194. package/test/encode-run.test.js +2 -2
  195. package/test/encode-slope.test.js +1 -1
  196. package/test/encoded-output.test.js +39 -0
  197. package/test/encoder-readings.test.js +1 -1
  198. package/test/every-claimant-is-told-its-share.test.js +152 -0
  199. package/test/failed-encoder-still-serves-what-exists.test.js +354 -0
  200. package/test/failed-start-breaker.test.js +81 -30
  201. package/test/fastest-wires.test.js +1 -1
  202. package/test/ffmpeg-stream-counts.test.js +1 -1
  203. package/test/file-edges.test.js +3 -3
  204. package/test/fmp4-self-contained.test.js +1 -1
  205. package/test/fmp4-timestamps.test.js +1 -1
  206. package/test/fmp4-track-completeness.test.js +1 -1
  207. package/test/format-keyed-outputs.test.js +78 -0
  208. package/test/health-metrics.test.js +3 -2
  209. package/test/held-request-width.test.js +22 -23
  210. package/test/helpers/encode-run.js +136 -136
  211. package/test/helpers/held-file.js +29 -0
  212. package/test/helpers/manager.js +57 -0
  213. package/test/helpers/output-spec.js +63 -0
  214. package/test/host-load.test.js +1 -1
  215. package/test/index-accuracy-is-about-the-file.test.js +128 -0
  216. package/test/init-compat.test.js +219 -0
  217. package/test/input-lost-quiets-the-plan.test.js +1 -1
  218. package/test/input-unavailable.test.js +1 -1
  219. package/test/keeping-period.test.js +4 -4
  220. package/test/keyframe-index-accuracy.test.js +97 -45
  221. package/test/keyframe-table-budget.test.js +28 -33
  222. package/test/keyframes-belong-to-the-file.test.js +174 -57
  223. package/test/learned-median.test.js +1 -1
  224. package/test/letting-go-asks-who-is-watching.test.js +191 -0
  225. package/test/limit-row-by-area.test.js +60 -0
  226. package/test/link-budget.test.js +111 -0
  227. package/test/link-probe.test.js +56 -56
  228. package/test/local-encoding-data.test.js +273 -0
  229. package/test/look-ahead-coverage.test.js +1 -1
  230. package/test/manager-surface.test.js +294 -0
  231. package/test/matroska-blocks.test.js +0 -0
  232. package/test/matroska-cues-track.test.js +1 -1
  233. package/test/memory-budget.test.js +2 -2
  234. package/test/mp4-composition-times.test.js +0 -0
  235. package/test/mp4-subtitles.test.js +1 -1
  236. package/test/one-authority.test.js +45 -41
  237. package/test/one-budget.test.js +149 -0
  238. package/test/one-output-one-session.test.js +204 -0
  239. package/test/{disk-space.test.js → one-resource-divided.test.js} +30 -10
  240. package/test/one-serving-output-per-height.test.js +458 -0
  241. package/test/orchestrator-wired.test.js +46 -45
  242. package/test/output-catalog.test.js +39 -0
  243. package/test/output-shape.test.js +4 -51
  244. package/test/output-spec.test.js +251 -157
  245. package/test/output-speed.test.js +17 -15
  246. package/test/outputs-being-prepared.test.js +40 -0
  247. package/test/packet-witness-ring.test.js +2 -2
  248. package/test/packet-witness.test.js +1 -1
  249. package/test/piece-disk-store.test.js +2 -2
  250. package/test/piece-from-whole-file.test.js +1 -1
  251. package/test/piece-lru.test.js +1 -1
  252. package/test/piece-reader.test.js +2 -2
  253. package/test/piece-store-eviction.test.js +1 -1
  254. package/test/piece-store-never-refuses.test.js +1 -1
  255. package/test/piece-store-reservations.test.js +1 -1
  256. package/test/piece-store-slow-disk.test.js +1 -1
  257. package/test/piece-tail.test.js +1 -1
  258. package/test/plan-host-timings.test.js +1 -1
  259. package/test/plan-is-steady.test.js +225 -225
  260. package/test/priority-map-download.test.js +4 -4
  261. package/test/priority-map-emptied.test.js +204 -205
  262. package/test/priority-map-per-output.test.js +215 -209
  263. package/test/priority-map.test.js +1 -1
  264. package/test/probe-wedge-certainty.test.js +1 -1
  265. package/test/produced-copy-choice.test.js +15 -60
  266. package/test/quality-offer.test.js +162 -0
  267. package/test/quality-variants.test.js +1229 -1167
  268. package/test/rate-control-in-the-output.test.js +180 -0
  269. package/test/read-survives-withdrawal.test.js +2 -2
  270. package/test/read-window.test.js +10 -3
  271. package/test/resume-warm.test.js +1 -1
  272. package/test/run-inputs.test.js +80 -0
  273. package/test/run-position-follows-published-grid.test.js +13 -11
  274. package/test/run-progress.test.js +65 -0
  275. package/test/same-height-switch.test.js +664 -0
  276. package/test/seek-frees-the-viewers-requests.test.js +217 -0
  277. package/test/seek-landing.test.js +13 -13
  278. package/test/seek-target-not-superseded.test.js +28 -29
  279. package/test/segment-closed-on-both-branches.test.js +96 -0
  280. package/test/segment-cut-times.test.js +1 -1
  281. package/test/segment-hold-supersede.test.js +28 -35
  282. package/test/segment-judged-by-its-own-header.test.js +221 -0
  283. package/test/segment-serve-wiring.test.js +130 -39
  284. package/test/segment-store-eviction.test.js +22 -7
  285. package/test/segment-store.test.js +98 -14
  286. package/test/segment-true-start.test.js +1 -1
  287. package/test/segments-are-shared.test.js +17 -16
  288. package/test/send-chunk-bytes.test.js +1 -1
  289. package/test/serving-existing-output.test.js +102 -0
  290. package/test/serving-output.test.js +102 -0
  291. package/test/shared-piece-store.test.js +1 -1
  292. package/test/source-file.test.js +1 -1
  293. package/test/stale-request-after-seek.test.js +17 -21
  294. package/test/startup-readings.test.js +10 -5
  295. package/test/subtitle-cue-framing.test.js +5 -5
  296. package/test/subtitle-cue-source.test.js +36 -38
  297. package/test/subtitle-cue-walk.test.js +11 -10
  298. package/test/subtitle-defaults.test.js +1 -1
  299. package/test/subtitle-language.test.js +4 -4
  300. package/test/subtitle-track-numbering.test.js +5 -4
  301. package/test/supply-margin.test.js +1 -1
  302. package/test/swarm-follows-readers.test.js +3 -3
  303. package/test/swarm-reach.test.js +1 -1
  304. package/test/swarm-selection.test.js +3 -3
  305. package/test/tail-duplication.test.js +1 -1
  306. package/test/the-channel-has-both-ends.test.js +91 -0
  307. package/test/the-torrent-thread-serves-bytes.test.js +107 -0
  308. package/test/thread-pool.test.js +3 -3
  309. package/test/timeline.test.js +1 -12
  310. package/test/torrent-cost.test.js +1 -1
  311. package/test/tracks-begin-together.test.js +38 -24
  312. package/test/tunnel-renewal.test.js +1 -1
  313. package/test/two-viewers-one-picture.test.js +120 -87
  314. package/test/upload-hurry.test.js +3 -3
  315. package/test/usrsctp-state.test.js +1 -1
  316. package/test/video-facts.test.js +1 -1
  317. package/test/viewer-assignments.test.js +143 -0
  318. package/test/viewer-outputs.test.js +60 -52
  319. package/test/viewer-presence.test.js +22 -25
  320. package/test/viewer-requests-admission.test.js +89 -0
  321. package/test/viewer-states.test.js +189 -202
  322. package/test/viewer-subtitle-subscription.test.js +1 -1
  323. package/test/viewer.test.js +139 -136
  324. package/test/viewing-generations.test.js +390 -0
  325. package/test/visible-rung.test.js +37 -0
  326. package/test/wait-ledger.test.js +1 -1
  327. package/test/webrtc-shim.test.js +3 -3
  328. package/test/wedge-certainty.test.js +2 -2
  329. package/test/withdraw-piece-claim.test.js +2 -2
  330. package/test/worker-channel.test.js +1 -31
  331. package/utils/client-log-file.js +151 -151
  332. package/scripts/read-container-index.mjs +0 -108
  333. package/services/container/ContainerFactory.js +0 -122
  334. package/services/controllers/PlaybackController.js +0 -33
  335. package/services/disk/DiskSpace.js +0 -150
  336. package/services/disk/wire.js +0 -61
  337. package/services/hls-session-manager.js +0 -9608
  338. package/services/orchestrators/SubtitleOrchestrator.js +0 -160
  339. package/services/output/Output.js +0 -94
  340. package/services/output/OutputSpec.js +0 -202
  341. package/services/output/index.js +0 -1
  342. package/services/output/ladder.js +0 -26
  343. package/services/produced-index.js +0 -222
  344. package/services/quality/EncodeCost.js +0 -555
  345. package/services/torrent-worker/container-tracks.js +0 -414
  346. package/services/viewer/link-readings.js +0 -56
  347. package/test/produced-index.test.js +0 -142
  348. /package/{services → bin}/thread-pool.js +0 -0
  349. /package/services/{available-share.js → encode/available-share.js} +0 -0
  350. /package/services/{decode-cost-fit.js → encode/decode-cost-fit.js} +0 -0
  351. /package/services/{encoder-readings.js → encode/encoder-readings.js} +0 -0
  352. /package/services/{host-load.js → encode/host-load.js} +0 -0
  353. /package/services/{output → encode/output}/cut-grid.js +0 -0
  354. /package/services/{output → encode/output}/rates.js +0 -0
  355. /package/services/{segment-formats → encode/segment-formats}/mpegts.js +0 -0
  356. /package/services/{torrent-cost.js → encode/torrent-cost.js} +0 -0
  357. /package/services/{container → media/container}/ebml-reader.js +0 -0
  358. /package/services/{ffmpeg-banner.js → media/ffmpeg-banner.js} +0 -0
  359. /package/services/{tracks → media/tracks}/ContainerTrack.js +0 -0
  360. /package/services/{tracks → media/tracks}/ImageSubtitleTrack.js +0 -0
  361. /package/services/{tracks → media/tracks}/SubtitleTrack.js +0 -0
  362. /package/services/{tracks → media/tracks}/TextSubtitleTrack.js +0 -0
  363. /package/services/{tracks → media/tracks}/VideoTrack.js +0 -0
  364. /package/services/{tracks → media/tracks}/index.js +0 -0
  365. /package/services/{files → storage/files}/piece-from-whole-file.js +0 -0
  366. /package/services/{disk → storage}/keep.js +0 -0
  367. /package/services/{piece-store → storage/piece-store}/piece-lru.js +0 -0
  368. /package/services/{disk → storage}/returns.js +0 -0
  369. /package/services/{demand → torrent/demand}/DemandRegister.js +0 -0
  370. /package/services/{demand → torrent/demand}/Urgency.js +0 -0
  371. /package/services/{demand → torrent/demand}/Window.js +0 -0
  372. /package/services/{demand → torrent/demand}/index.js +0 -0
  373. /package/services/{demand → torrent/demand}/pieces.js +0 -0
  374. /package/services/{download → torrent/download}/withdraw-claim.js +0 -0
  375. /package/services/{supply-margin.js → torrent/supply-margin.js} +0 -0
  376. /package/services/{torrent-source-key.js → torrent/torrent-source-key.js} +0 -0
  377. /package/services/{torrent-worker → torrent/worker}/fastest-wires.js +0 -0
  378. /package/services/{torrent-worker → torrent/worker}/handle-state.js +0 -0
  379. /package/services/{nat-classifier.js → transport/nat-classifier.js} +0 -0
  380. /package/services/{registry-api.js → transport/registry-api.js} +0 -0
  381. /package/services/{tunnel-client.js → transport/tunnel-client.js} +0 -0
  382. /package/services/{priority → viewer}/WaitLedger.js +0 -0
package/server.js CHANGED
@@ -1,465 +1,720 @@
1
- /**
2
- * @file Proxy HTTP server bootstrap.
3
- *
4
- * Creates and configures the Fastify application, registers all routes and
5
- * plugins, then starts listening on the first available port at or above the
6
- * requested one.
7
- */
8
-
9
- import Fastify from "fastify";
10
- import fastifyCors from "@fastify/cors";
11
- import fastifyHelmet from "@fastify/helmet";
12
- import fastifyStatic from "@fastify/static";
13
- import getPort from "get-port";
14
- import path from "node:path";
15
- import { createRequire } from "node:module";
16
- import { fileURLToPath } from "node:url";
17
- import { handleHealthGet } from "./routes/health/get.js";
18
- import { handleHealthzGet } from "./routes/healthz/get.js";
19
- import { handleApiDeliverySinkGet } from "./routes/api/delivery-sink/get.js";
20
- import { handleApiLinkProbeGet } from "./routes/api/link-probe/get.js";
21
- import { handleApiSourcesPost } from "./routes/api/sources/post.js";
22
- import { handleApiSourceStatsGet } from "./routes/api/sources/stats/get.js";
23
- import { handleApiSourceFilesGet } from "./routes/api/sources/files/get.js";
24
- import { handleApiSourceWarmPost } from "./routes/api/sources/warm/post.js";
25
- import { handleApiPlaybackPlanPost } from "./routes/api/playback-plan/post.js";
26
- import { handleApiClientLogsPost } from "./routes/api/client-logs/post.js";
27
- import { createClientLogFiles } from "./utils/client-log-file.js";
28
- import { handleApiSubtitlesGet } from "./routes/api/subtitles/get.js";
29
- import { handleApiTranscodeSessionsPost } from "./routes/api/transcode-sessions/post.js";
30
- import { handleApiTranscodeSessionsProgressGet } from "./routes/api/transcode-sessions/progress/get.js";
31
- import { handleApiTranscodeSessionReleasePost } from "./routes/api/transcode-sessions/release/post.js";
32
- import { handleApiTranscodeSessionNetReportPost } from "./routes/api/transcode-sessions/net-report/post.js";
33
- import { handleApiTranscodeSessionFragmentFarPost } from "./routes/api/transcode-sessions/fragment-far/post.js";
34
- import { handleApiTranscodeSessionSeekPost } from "./routes/api/transcode-sessions/seek/post.js";
35
- import { handleStreamGet } from "./routes/stream/get.js";
36
- import { handleTranscodeSessionFileGet } from "./routes/transcode/session-file/get.js";
37
- import { handleTranscodeVariantFileGet } from "./routes/transcode/variant-file/get.js";
38
- import { handleTranscodeAudioFileGet } from "./routes/transcode/audio-file/get.js";
39
- import { handleTranscodeVariantWarmGet } from "./routes/transcode/variant-warm/get.js";
40
- import { handleTranscodeAudioWarmGet } from "./routes/transcode/audio-warm/get.js";
41
- import { createSourceRegistry } from "./store/source-registry.js";
42
- import { WorkerTorrentPool } from "./services/torrent-worker/pool-adapter.js";
43
- import { HlsSessionManager } from "./services/hls-session-manager.js";
44
- import { createPlaybackPlanner } from "./services/playback-planner.js";
45
- import { detectVideoEncoder, benchmarkSoftwarePresets, benchmarkDecodeCost, benchmarkContention, benchmarkCopySpeed, detectTonemapSupport } from "./services/hwaccel.js";
46
- import { measureStartAndStop } from "./services/encode/start-stop-cost.js";
47
- import { logger } from "./utils/logger.js";
48
-
49
- const __filename = fileURLToPath(import.meta.url);
50
- const __dirname = path.dirname(__filename);
51
- const require = createRequire(import.meta.url);
52
- const { version } = require("./package.json");
53
- const publicRoot = path.resolve(__dirname, "./public");
54
-
55
- /**
56
- * Build a list of candidate port numbers starting at `startPort`.
57
- *
58
- * @param {number} startPort
59
- * @param {number} [maxAttempts=51]
60
- * @returns {number[]}
61
- */
62
- function buildPortCandidates(startPort, maxAttempts = 51) {
63
- const ports = [];
64
- for (let index = 0; index < maxAttempts; index += 1) {
65
- ports.push(startPort + index);
66
- }
67
- return ports;
68
- }
69
-
70
- /**
71
- * @typedef {Object} ProxyServerOptions
72
- * @property {string} host - Bind host (e.g. "127.0.0.1" or "0.0.0.0").
73
- * @property {number} port - Preferred listen port.
74
- * @property {boolean} transcodeAudio - Whether HLS audio transcoding is enabled.
75
- * @property {string} ffmpegBin - Path to the ffmpeg executable.
76
- * @property {number} [maxDiskBytes] - Global disk cap for torrent data (undefined = pool default).
77
- * @property {number} [memoryBytes] - Per-torrent budget for pieces held in memory (undefined = store default).
78
- * @property {string} [segmentFormat] - HLS output container: "fmp4" (default) or "mpegts".
79
- * @property {string} [stateDir] - Where to keep what this host has measured about itself.
80
- */
81
-
82
- /**
83
- * Create, configure, and start the proxy HTTP server.
84
- *
85
- * @param {ProxyServerOptions} options
86
- * @returns {Promise<{ app: import("fastify").FastifyInstance, port: number }>}
87
- */
88
- export async function startProxyServer({
89
- host, port, transcodeAudio, ffmpegBin, maxDiskBytes, memoryBytes, segmentFormat, stateDir, onSubtitleCues,
90
- deliverySink = false,
91
- // Where the proxy writes its own log. Its DIRECTORY is what matters here:
92
- // the browser's half of every session is written beside it, so the two are
93
- // on one durable disk and join by name.
94
- logFile = ""
95
- }) {
96
- const app = Fastify({
97
- // No practical body-size limit — the proxy server is localhost-only and
98
- // receives torrent source payloads that may be arbitrarily large.
99
- bodyLimit: 256 * 1024 * 1024 // 256 MB
100
- });
101
-
102
- await app.register(fastifyHelmet, {
103
- // Proxy serves media to a different origin (registry UI), so CORP must allow cross-origin usage.
104
- crossOriginResourcePolicy: {
105
- policy: "cross-origin"
106
- }
107
- });
108
- await app.register(fastifyCors, {
109
- origin: true,
110
- methods: ["GET", "POST", "OPTIONS"],
111
- allowedHeaders: ["Content-Type", "Range"]
112
- });
113
-
114
- // Allow browser requests from an HTTPS page to this private-network proxy
115
- // without triggering Chromium's Private Network Access permission prompt.
116
- app.addHook("onRequest", async (_req, reply) => {
117
- reply.header("Access-Control-Allow-Private-Network", "true");
118
- });
119
-
120
- const sourceRegistry = createSourceRegistry(200);
121
- // The torrent runs on its own thread. Profiling a live seek (2026-08-02)
122
- // found the main thread ~85% occupied by WebTorrent — buffer concatenation
123
- // ~15%, wire updates ~9%, garbage collection ~5% — while three of four cores
124
- // idled. Serving a segment shared that thread, so reading an already-finished
125
- // 10 MB file took 12-23 s against 125 ms to hand it to the channel. The
126
- // adapter keeps TorrentPool's interface, so nothing downstream changed.
127
- const torrentPool = new WorkerTorrentPool({ maxDiskBytes, memoryBytes, stateDir, onSubtitleCues });
128
- const selectedPort = await getPort({
129
- port: buildPortCandidates(port)
130
- });
131
- // Auto-detect the best available H.264 encoder (hardware-accelerated or
132
- // software) once at startup, with a real test-encode and graceful fallback.
133
- // Only needed when transcoding can occur.
134
- const videoEncoder = transcodeAudio
135
- ? await detectVideoEncoder({ ffmpegBin, logger })
136
- : null;
137
- // WHAT THIS HOST DOES, measured before any viewer exists. Every one of these
138
- // was gated on the chosen encoder being SOFTWARE, and two of the three are
139
- // not about the encoder at all: a host with a GPU decodes in software just
140
- // the same — no hardware decoder is asked for anywhere — and what a second
141
- // job costs is a property of the machine. So a GPU host had no decode cost,
142
- // no contention penalty and no encoder throughput, and the quality offer,
143
- // which is arithmetic over those three, had nothing to compute from.
144
- //
145
- // The decode model first and the throughput second — they are independent
146
- // (the rungs are timed on raw frames), but the order keeps the two figures
147
- // side by side in the log.
148
- const decodeCostModel = transcodeAudio
149
- ? await benchmarkDecodeCost({ ffmpegBin, logger })
150
- : null;
151
- // What a second job costs on this host. Measured because the budget adds
152
- // independent prices and this host says two jobs that each fit alone do not
153
- // fit together — 2.6× on the addon box (2026-08-18).
154
- const contentionPenalties = transcodeAudio
155
- ? await benchmarkContention({ ffmpegBin, logger })
156
- : null;
157
- // The chosen encoder walked over its OWN speed ladder: libx264's presets,
158
- // NVENC's p1…p7, QSV's veryfast…veryslow, VAAPI's quality levels. A kind with
159
- // no ladder is measured once, which is still a reading where there was none.
160
- const softwarePresetBenchmark = videoEncoder
161
- ? await benchmarkSoftwarePresets({ ffmpegBin, logger, encoder: videoEncoder })
162
- : null;
163
- // What this host does with a picture it does NOT re-encode. Every other
164
- // startup measurement prices encoding or decoding, and a copied picture does
165
- // neither — it reads packets and writes them out again — so that whole branch
166
- // had no speed until its own run had been running long enough to report one.
167
- // The encoding layer decides where encoders go from arrivals, and an arrival
168
- // cannot be computed without a speed, so the moment it mattered most was the
169
- // moment nothing was known. Measured whatever the encoder is: copying does not
170
- // touch it.
171
- const copySpeedX = transcodeAudio
172
- ? await benchmarkCopySpeed({ ffmpegBin, logger })
173
- : null;
174
- // WHAT A START AND A STOP COST HERE, before any viewer exists. Both decide one
175
- // thing — leave an encoder where it stands, or kill it and start another —
176
- // and both used to be learned only from runs that had ENDED, so at a cold
177
- // open they were zero. Zero does not read as "not measured": it reads as
178
- // "free", and a free move is always taken. Field 2026-09-08: an encoder moved
179
- // between two adjacent numbers every half second and produced nothing.
180
- const startStopCost = transcodeAudio
181
- ? await measureStartAndStop({ ffmpegBin, encoder: videoEncoder, logger })
182
- : null;
183
- // Whether this ffmpeg build can tone-map HDR→SDR (zscale + tonemap filters).
184
- // Detected once; the session manager applies the tonemap chain only for HDR
185
- // sources on the software path when available.
186
- const tonemapSupported = transcodeAudio
187
- ? await detectTonemapSupport({ ffmpegBin, logger })
188
- : false;
189
- const hlsSessionManager = new HlsSessionManager({
190
- enabled: transcodeAudio,
191
- ffmpegBin,
192
- localBindHost: host,
193
- localPort: selectedPort,
194
- videoEncoder,
195
- softwarePresetBenchmark,
196
- decodeCostModel,
197
- contentionPenalties,
198
- copySpeedX,
199
- startStopCost,
200
- tonemapSupported,
201
- segmentFormatId: segmentFormat,
202
- stateDir,
203
- // Live download stats accessor for the realtime budget: lets it tell a
204
- // CPU-bound transcode from a download-starved input before downscaling.
205
- // What every torrent here has moved, so the proxy can price its own
206
- // downloading, hashing and delivery against the machine (roadmap item 7).
207
- // What the spilled pieces weigh on the torrent thread, and how to tell them
208
- // their share of the disk. The owner of the disk is on this side, where the
209
- // segments are; the pieces are on the other.
210
- spillDisk: typeof torrentPool.allowSpillBytes === "function"
211
- ? {
212
- held: () => torrentPool.spilledBytes ?? 0,
213
- allow: (bytes) => torrentPool.allowSpillBytes(bytes)
214
- }
215
- : null,
216
- getTorrentTotals: async () => {
217
- if (typeof torrentPool.getTorrentTotals !== "function") {
218
- return null;
219
- }
220
- return torrentPool.getTorrentTotals();
221
- },
222
- // The priority map, on its way to the downloading. It lives in another
223
- // thread, so the map crosses the worker channel; what it does with it —
224
- // seconds into bytes, what to ask the swarm for, what to keep in memory —
225
- // is its own business.
226
- setPriorityMap: async ({ sourceKey, fileIndex, durationSeconds, zones }) => {
227
- const record = sourceRegistry.get(sourceKey);
228
- if (!record) {
229
- return;
230
- }
231
- try {
232
- await torrentPool.setPriorityMap({ sourceKey, fileIndex, durationSeconds, zones });
233
- } catch {
234
- // Best effort: the map is republished on the next change, and the
235
- // downloading goes on serving reads meanwhile.
236
- }
237
- },
238
- getSourceStats: async (sourceKey, fileIndex) => {
239
- const record = sourceRegistry.get(sourceKey);
240
- if (!record) {
241
- return null;
242
- }
243
- try {
244
- const torrent = await torrentPool.getTorrent(record.sourceType, record.source);
245
- // Awaited for the same reason as the stats route: this now crosses a
246
- // thread boundary and returns a promise.
247
- return await torrentPool.getFileStats(torrent, Number.isInteger(fileIndex) ? fileIndex : null);
248
- } catch {
249
- return null;
250
- }
251
- },
252
- // Reuse the media info the planner already probed for this file (same
253
- // ffmpeg scan) so createSession skips its own probe. Late-bound: invoked
254
- // only at session-create time, after playbackPlanner is initialised.
255
- getCachedMediaInfo: (params) => playbackPlanner.getCachedMediaInfo(params),
256
- // The file's audio tracks, for the master playlist's rendition group. Already
257
- // probed for the browser's audio menu; read from there rather than probed again.
258
- getCachedAudioTracks: (params) => playbackPlanner.getCachedAudioTracks(params),
259
- // What a file declares about itself, read by the container layer from the
260
- // same header its track table comes from. This is how the session learns
261
- // where a soundtrack shipped as its own file begins — it used to spawn an
262
- // ffmpeg over this proxy's own HTTP to ask the same question of the same
263
- // bytes, and that read cost 8.1 s of every cold start (field 2026-09-03).
264
- getContainerMediaInfo: async ({ sourceKey, fileIndex }) => {
265
- const record = sourceRegistry.get(sourceKey);
266
- if (!record || typeof torrentPool.getContainerMediaInfo !== "function") {
267
- return null;
268
- }
269
- try {
270
- const torrent = await torrentPool.getTorrent(record.sourceType, record.source);
271
- return await torrentPool.getContainerMediaInfo(torrent, fileIndex);
272
- } catch {
273
- return null;
274
- }
275
- },
276
- // Where the file's keyframes are, read by the same container that answered
277
- // the two above. It used to be read by the session itself over this proxy's
278
- // own HTTP, once per session — so two viewers opening one film read the
279
- // same table twice, and each of them could get a different answer about
280
- // whether the picture can be copied at all.
281
- getContainerKeyframes: async ({ sourceKey, fileIndex }) => {
282
- const record = sourceRegistry.get(sourceKey);
283
- if (!record || typeof torrentPool.getContainerKeyframes !== "function") {
284
- return null;
285
- }
286
- try {
287
- const torrent = await torrentPool.getTorrent(record.sourceType, record.source);
288
- return await torrentPool.getContainerKeyframes(torrent, fileIndex);
289
- } catch {
290
- return null;
291
- }
292
- },
293
- // Pull one whole file onto the disk. Used for a soundtrack that ships beside
294
- // the picture, once the encoder is as far ahead of the viewer as it is
295
- // allowed to get — the one moment the swarm's capacity is demonstrably
296
- // spare. A bounded read of the file's whole length, NOT `file.select()`:
297
- // selecting a file alongside the readers' own windows is what made a seek
298
- // wait 93 s while the swarm fetched 2.47 GB in file order (see
299
- // `#syncSelections` in `torrent-pool.js`).
300
- fetchWholeFile: async ({ sourceKey, fileIndex }) => {
301
- const record = sourceRegistry.get(sourceKey);
302
- if (!record) {
303
- return;
304
- }
305
- const torrent = await torrentPool.getTorrent(record.sourceType, record.source);
306
- // The same background fill the warm-up starts when a file is chosen, not a
307
- // second way of doing it. It is guarded against running twice on one file,
308
- // so the two triggers converge instead of putting two readers on the same
309
- // soundtrack — and only one of them would have stood aside for the
310
- // picture. This trigger remains for the session that never had a warm-up
311
- // before it.
312
- await torrentPool.fillFileInBackground?.(torrent, fileIndex);
313
- }
314
- });
315
- // What the last life of this process left on the disk. The kernel kills this
316
- // one often enough for that to be an ordinary state rather than an odd one —
317
- // twice in a single viewing on 2026-09-02 — and when it does, no exit handler
318
- // runs and nothing is cleared up. So this is both the cleanup and the only
319
- // record that those encoders ended at all: it says what it found before it
320
- // decides anything, keeps the segments whose closure is proven, and removes
321
- // the one piece per output that was being written when the process died.
322
- hlsSessionManager.adoptSegmentsLeftBehind();
323
- const playbackPlanner = createPlaybackPlanner({
324
- ffmpegBin,
325
- transcodeAudioEnabled: transcodeAudio,
326
- localBaseUrl: hlsSessionManager.localBaseUrl,
327
- sourceRegistry,
328
- torrentPool,
329
- warmKeyframeIndex: (params) => hlsSessionManager.warmKeyframeIndex(params),
330
- expectedFirstSegmentMs: () => hlsSessionManager.expectedFirstSegmentMs(),
331
- expectedSessionCreateMs: () => hlsSessionManager.expectedSessionCreateMs(),
332
- // The quality menu is on screen from the moment a file is opened, so the
333
- // heights this host can actually serve have to be answerable before any
334
- // encoder exists — from the probe and the startup benchmarks alone.
335
- predictOfferedHeights: (mediaInfo) => hlsSessionManager.predictOfferedHeights(mediaInfo)
336
- });
337
-
338
- app.get("/health", async (req, reply) => handleHealthGet(req, reply, { version }));
339
- app.get("/healthz", async (req, reply) => handleHealthzGet(req, reply, { version }));
340
- // Off unless --delivery-sink was given; see the route for why it exists.
341
- app.get("/api/delivery-sink", async (req, reply) =>
342
- handleApiDeliverySinkGet(req, reply, { enabled: deliverySink === true })
343
- );
344
- // Bytes a browser can time its link with before any film has been chosen.
345
- app.get("/api/link-probe", async (req, reply) => handleApiLinkProbeGet(req, reply));
346
- app.post("/api/sources", async (req, reply) =>
347
- handleApiSourcesPost(req, reply, { sourceRegistry })
348
- );
349
- app.get("/api/sources/:sourceKey/stats", async (req, reply) =>
350
- handleApiSourceStatsGet(req, reply, { sourceRegistry, torrentPool })
351
- );
352
- app.get("/api/sources/:sourceKey/files", async (req, reply) =>
353
- handleApiSourceFilesGet(req, reply, { sourceRegistry, torrentPool })
354
- );
355
- app.post("/api/sources/:sourceKey/warm", async (req, reply) =>
356
- handleApiSourceWarmPost(req, reply, { sourceRegistry, torrentPool })
357
- );
358
- // The browser's own log, kept beside the proxy's. See the route's own file
359
- // for why it is here and not only on the registry server.
360
- const clientLogs = logFile ? createClientLogFiles(logFile) : null;
361
- app.post("/api/client-logs", async (req, reply) =>
362
- handleApiClientLogsPost(req, reply, { clientLogs })
363
- );
364
- app.post("/api/playback-plan", async (req, reply) =>
365
- handleApiPlaybackPlanPost(req, reply, { playbackPlanner })
366
- );
367
- app.get("/api/subtitles", async (req, reply) =>
368
- handleApiSubtitlesGet(req, reply, {
369
- sourceRegistry,
370
- torrentPool,
371
- ffmpegBin,
372
- localBaseUrl: hlsSessionManager.localBaseUrl,
373
- viewers: hlsSessionManager.viewers
374
- })
375
- );
376
- app.get("/stream", async (req, reply) =>
377
- handleStreamGet(req, reply, {
378
- sourceRegistry,
379
- torrentPool,
380
- // So a session that has produced nothing yet can still show it is being
381
- // fed. The route knows only files; the session id rides on the URL the
382
- // session itself built.
383
- noteInputBytes: (sessionId, bytes) => hlsSessionManager.noteInputBytes(sessionId, bytes)
384
- })
385
- );
386
- app.post("/api/transcode-sessions", async (req, reply) =>
387
- handleApiTranscodeSessionsPost(req, reply, { hlsSessionManager, sourceRegistry, torrentPool })
388
- );
389
- app.post("/api/transcode-sessions/:sessionId/release", async (req, reply) =>
390
- handleApiTranscodeSessionReleasePost(req, reply, { hlsSessionManager })
391
- );
392
- app.get("/api/transcode-sessions/:sessionId/progress", async (req, reply) =>
393
- handleApiTranscodeSessionsProgressGet(req, reply, { hlsSessionManager })
394
- );
395
- app.post("/api/transcode-sessions/:sessionId/net-report", async (req, reply) =>
396
- // A viewer's statement about itself goes to the VIEWER layer, not through
397
- // the session manager: what it needs is the live sessions and the registry
398
- // of viewers, and nothing about encoding.
399
- handleApiTranscodeSessionNetReportPost(req, reply, {
400
- sessions: hlsSessionManager.sessionsById,
401
- viewers: hlsSessionManager.viewers
402
- })
403
- );
404
- app.post("/api/transcode-sessions/:sessionId/fragment-far", async (req, reply) =>
405
- handleApiTranscodeSessionFragmentFarPost(req, reply, { hlsSessionManager })
406
- );
407
- app.post("/api/transcode-sessions/:sessionId/seek", async (req, reply) =>
408
- handleApiTranscodeSessionSeekPost(req, reply, { hlsSessionManager })
409
- );
410
- app.get("/transcode/:sessionId/:fileName", async (req, reply) =>
411
- handleTranscodeSessionFileGet(req, reply, { hlsSessionManager })
412
- );
413
- // A quality variant's files. Registered before the static handler for the
414
- // same reason as the line above, and kept a separate route rather than a
415
- // wildcard so the height stays a parsed parameter.
416
- // Registered BEFORE the variant file route: `warm` is not a file name, and
417
- // Fastify matches a static segment ahead of a parameter either way — stated
418
- // here so the order is not "tidied" into a bug.
419
- app.get("/transcode/:sessionId/v/:height/warm", async (req, reply) =>
420
- handleTranscodeVariantWarmGet(req, reply, { hlsSessionManager })
421
- );
422
- app.get("/transcode/:sessionId/a/:track/warm", async (req, reply) =>
423
- handleTranscodeAudioWarmGet(req, reply, { hlsSessionManager })
424
- );
425
- app.get("/transcode/:sessionId/a/:trackIndex/:fileName", async (req, reply) =>
426
- handleTranscodeAudioFileGet(req, reply, { hlsSessionManager })
427
- );
428
- app.get("/transcode/:sessionId/v/:height/:fileName", async (req, reply) =>
429
- handleTranscodeVariantFileGet(req, reply, { hlsSessionManager })
430
- );
431
- await app.register(fastifyStatic, {
432
- root: publicRoot,
433
- prefix: "/",
434
- serveDotFiles: true
435
- });
436
-
437
- app.addHook("onClose", async () => {
438
- // Order matters: stop the ffmpeg readers (HLS sessions) before destroying
439
- // the torrents whose files they read from, then remove the torrent data.
440
- await hlsSessionManager.disposeAll();
441
- await torrentPool.destroyAll();
442
- });
443
-
444
- await app.listen({ host, port: selectedPort });
445
- return {
446
- app,
447
- port: selectedPort,
448
- // Asked over the tunnel when the proxy a viewer landed on has refused their
449
- // file: could THIS host sustain it? Answered from the startup benchmarks
450
- // and a description, so it needs no torrent and costs milliseconds.
451
- hlsSessionManager,
452
- // The browser only ever knows a source by its REGISTRY key (a hash of the
453
- // raw request bytes, scoped to one API session) — never the torrent
454
- // pool's own key (the content's infohash, shared across a magnet and a
455
- // `.torrent` naming the same film). The subtitle push subscription is
456
- // recorded from a browser request and published from the pool's side, so
457
- // resolving one into the other is what lets the two ends agree on what
458
- // they are both calling "sourceKey".
459
- sourceRegistry,
460
- // Which films this host holds, for the health poll the browser makes before
461
- // it picks a proxy. The pool is on the worker thread and this is the way to
462
- // it from the process that answers that poll.
463
- torrentPool
464
- };
465
- }
1
+ /**
2
+ * @file Proxy HTTP server bootstrap.
3
+ *
4
+ * Creates and configures the Fastify application, registers all routes and
5
+ * plugins, then starts listening on the first available port at or above the
6
+ * requested one.
7
+ */
8
+
9
+ import Fastify from "fastify";
10
+ import fastifyCors from "@fastify/cors";
11
+ import fastifyHelmet from "@fastify/helmet";
12
+ import fastifyStatic from "@fastify/static";
13
+ import getPort from "get-port";
14
+ import path from "node:path";
15
+ import { createRequire } from "node:module";
16
+ import { fileURLToPath } from "node:url";
17
+ import { handleHealthGet } from "./routes/health/get.js";
18
+ import { handleHealthzGet } from "./routes/healthz/get.js";
19
+ import { handleApiDeliverySinkGet } from "./routes/api/delivery-sink/get.js";
20
+ import { handleApiLinkProbeGet } from "./routes/api/link-probe/get.js";
21
+ import { handleApiSourcesPost } from "./routes/api/sources/post.js";
22
+ import { handleApiSourceStatsGet } from "./routes/api/sources/stats/get.js";
23
+ import { handleApiSourceFilesGet } from "./routes/api/sources/files/get.js";
24
+ import { handleApiSourceWarmPost } from "./routes/api/sources/warm/post.js";
25
+ import { handleApiPlaybackPlanPost } from "./routes/api/playback-plan/post.js";
26
+ import { handleApiClientLogsPost } from "./routes/api/client-logs/post.js";
27
+ import { createClientLogFiles } from "./utils/client-log-file.js";
28
+ import { handleApiSubtitlesGet } from "./routes/api/subtitles/get.js";
29
+ import { handleApiTranscodeSessionsPost } from "./routes/api/transcode-sessions/post.js";
30
+ import { handleApiTranscodeSessionsProgressGet } from "./routes/api/transcode-sessions/progress/get.js";
31
+ import { handleApiTranscodeSessionReleasePost } from "./routes/api/transcode-sessions/release/post.js";
32
+ import { handleApiTranscodeSessionNetReportPost } from "./routes/api/transcode-sessions/net-report/post.js";
33
+ import { handleApiTranscodeSessionFragmentFarPost } from "./routes/api/transcode-sessions/fragment-far/post.js";
34
+ import { handleApiTranscodeSessionSeekPost } from "./routes/api/transcode-sessions/seek/post.js";
35
+ import { handleStreamGet } from "./routes/stream/get.js";
36
+ import { handleTranscodeSessionFileGet } from "./routes/transcode/session-file/get.js";
37
+ import { handleTranscodeVariantFileGet } from "./routes/transcode/variant-file/get.js";
38
+ import { handleTranscodeAudioFileGet } from "./routes/transcode/audio-file/get.js";
39
+ import { handleTranscodeVariantWarmGet } from "./routes/transcode/variant-warm/get.js";
40
+ import { handleTranscodeAudioWarmGet } from "./routes/transcode/audio-warm/get.js";
41
+ import { createSourceRegistry } from "./store/source-registry.js";
42
+ import { WorkerTorrentPool } from "./services/torrent/worker/pool-adapter.js";
43
+ import { wireOutputs } from "./services/server/wire-outputs.js";
44
+ import { createPlaybackPlanner } from "./services/media/playback-planner.js";
45
+ import { KeyframeTables } from "./services/media/KeyframeTables.js";
46
+ import { contentsOf } from "./services/torrent/Contents.js";
47
+ import { SubtitleOrchestrator } from "./services/media/SubtitleOrchestrator.js";
48
+ import { containerOrchestrator, CONTAINER_HEAD_BYTES } from "./services/media/ContainerOrchestrator.js";
49
+ import {
50
+ warmSubtitleCues,
51
+ cuesHeldFor,
52
+ subtitleTracksOf,
53
+ declaredSubtitleTracksOf,
54
+ forgetSubtitles
55
+ } from "./services/media/SubtitleCues.js";
56
+ import { detectVideoEncoder, benchmarkDecodeCost, benchmarkContention, benchmarkCopySpeed, detectTonemapSupport, softwareDescriptor } from "./services/encode/hwaccel.js";
57
+ import { calibrateEncoder, HostCalibration } from "./services/encode/calibration.js";
58
+ import { readHostFingerprint } from "./services/encode/fingerprint.js";
59
+ import { measureStartAndStop } from "./services/encode/start-stop-cost.js";
60
+ import { logger } from "./utils/logger.js";
61
+ import { completedFilesRoot } from "./services/storage/files/CompletedFiles.js";
62
+
63
+ const __filename = fileURLToPath(import.meta.url);
64
+ const __dirname = path.dirname(__filename);
65
+ const require = createRequire(import.meta.url);
66
+ const { version } = require("./package.json");
67
+ const publicRoot = path.resolve(__dirname, "./public");
68
+
69
+ /**
70
+ * Build a list of candidate port numbers starting at `startPort`.
71
+ *
72
+ * @param {number} startPort
73
+ * @param {number} [maxAttempts=51]
74
+ * @returns {number[]}
75
+ */
76
+ function buildPortCandidates(startPort, maxAttempts = 51) {
77
+ const ports = [];
78
+ for (let index = 0; index < maxAttempts; index += 1) {
79
+ ports.push(startPort + index);
80
+ }
81
+ return ports;
82
+ }
83
+
84
+ /**
85
+ * @typedef {Object} ProxyServerOptions
86
+ * @property {string} host - Bind host (e.g. "127.0.0.1" or "0.0.0.0").
87
+ * @property {number} port - Preferred listen port.
88
+ * @property {boolean} transcodeAudio - Whether HLS audio transcoding is enabled.
89
+ * @property {string} ffmpegBin - Path to the ffmpeg executable.
90
+ * @property {number} [memoryBytes] - Per-torrent budget for pieces held in memory (undefined = store default).
91
+ * @property {string} [segmentFormat] - HLS output container: "fmp4" (default) or "mpegts".
92
+ * @property {string} [stateDir] - Where to keep what this host has measured about itself.
93
+ */
94
+
95
+ /**
96
+ * Create, configure, and start the proxy HTTP server.
97
+ *
98
+ * @param {ProxyServerOptions} options
99
+ * @returns {Promise<{ app: import("fastify").FastifyInstance, port: number }>}
100
+ */
101
+ export async function startProxyServer({
102
+ host, port, transcodeAudio, ffmpegBin, memoryBytes, segmentFormat, stateDir, onSubtitleCues, diagnostics, budgetPolicy,
103
+ deliverySink = false,
104
+ // Where the proxy writes its own log. Its DIRECTORY is what matters here:
105
+ // the browser's half of every session is written beside it, so the two are
106
+ // on one durable disk and join by name.
107
+ logFile = ""
108
+ }) {
109
+ const app = Fastify({
110
+ // No practical body-size limit — the proxy server is localhost-only and
111
+ // receives torrent source payloads that may be arbitrarily large.
112
+ bodyLimit: 256 * 1024 * 1024 // 256 MB
113
+ });
114
+
115
+ await app.register(fastifyHelmet, {
116
+ // Proxy serves media to a different origin (registry UI), so CORP must allow cross-origin usage.
117
+ crossOriginResourcePolicy: {
118
+ policy: "cross-origin"
119
+ }
120
+ });
121
+ await app.register(fastifyCors, {
122
+ origin: true,
123
+ methods: ["GET", "POST", "OPTIONS"],
124
+ allowedHeaders: ["Content-Type", "Range"]
125
+ });
126
+
127
+ // Allow browser requests from an HTTPS page to this private-network proxy
128
+ // without triggering Chromium's Private Network Access permission prompt.
129
+ app.addHook("onRequest", async (_req, reply) => {
130
+ reply.header("Access-Control-Allow-Private-Network", "true");
131
+ });
132
+
133
+ const sourceRegistry = createSourceRegistry(200);
134
+ // The torrent runs on its own thread. Profiling a live seek (2026-08-02)
135
+ // found the main thread ~85% occupied by WebTorrent — buffer concatenation
136
+ // ~15%, wire updates ~9%, garbage collection ~5% — while three of four cores
137
+ // idled. Serving a segment shared that thread, so reading an already-finished
138
+ // 10 MB file took 12-23 s against 125 ms to hand it to the channel. The
139
+ // adapter keeps TorrentPool's interface, so nothing downstream changed.
140
+ const torrentPool = new WorkerTorrentPool({
141
+ memoryBytes,
142
+ stateDir,
143
+ // Pieces arriving is ANNOUNCED by the thread that owns the swarm; what is
144
+ // done about it — walking a file's new subtitle clusters and pushing what
145
+ // came out — happens here, because that is a reading of what the file says
146
+ // about itself. The walk used to run in that thread, which is what put a
147
+ // container parse there.
148
+ // The ONE forward reference here, and it is safe by construction: this fires
149
+ // only once the worker thread is up and a piece has verified, which is long
150
+ // after the declarations below have run.
151
+ onPiecesArrived: ({ sourceKey, fileIndexes }) => {
152
+ for (const fileIndex of fileIndexes) {
153
+ void pushFreshCues(sourceKey, fileIndex);
154
+ }
155
+ }
156
+ });
157
+ const selectedPort = await getPort({
158
+ port: buildPortCandidates(port)
159
+ });
160
+ // Auto-detect the best available H.264 encoder (hardware-accelerated or
161
+ // software) once at startup, with a real test-encode and graceful fallback.
162
+ // Only needed when transcoding can occur.
163
+ let videoEncoder = transcodeAudio
164
+ ? await detectVideoEncoder({ ffmpegBin, logger })
165
+ : null;
166
+ // WHAT THIS HOST DOES, measured before any viewer exists. Every one of these
167
+ // was gated on the chosen encoder being SOFTWARE, and two of the three are
168
+ // not about the encoder at all: a host with a GPU decodes in software just
169
+ // the same — no hardware decoder is asked for anywhere — and what a second
170
+ // job costs is a property of the machine. So a GPU host had no decode cost,
171
+ // no contention penalty and no encoder throughput, and the quality offer,
172
+ // which is arithmetic over those three, had nothing to compute from.
173
+ //
174
+ // The decode model first and the throughput second — they are independent
175
+ // (the rungs are timed on raw frames), but the order keeps the two figures
176
+ // side by side in the log.
177
+ const decodeCostModel = transcodeAudio
178
+ ? await benchmarkDecodeCost({ ffmpegBin, logger })
179
+ : null;
180
+ // What a second job costs on this host. Measured because the budget adds
181
+ // independent prices and this host says two jobs that each fit alone do not
182
+ // fit together — 2.6× on the addon box (2026-08-18).
183
+ const contentionPenalties = transcodeAudio
184
+ ? await benchmarkContention({ ffmpegBin, logger })
185
+ : null;
186
+ // WHICH MODES THIS HOST MAY ENCODE WITH, AND WHAT EACH COSTS AT EVERY SIZE
187
+ // (roadmap item 97, step 14). Every setting an output can actually be
188
+ // encoded at is put through the product's own arguments and its segments
189
+ // decoded, then timed at the sizes of the ladder; a mode that fails either is
190
+ // not used, and a size nothing was timed at is not offered. Software is
191
+ // calibrated too when detection chose a device: it is what a failing device
192
+ // falls back to for the rest of the process, and it must not be priced by
193
+ // the device's figures.
194
+ //
195
+ // And WHICH CONFIGURATION all of it describes — the ffmpeg and x264 builds,
196
+ // the processor and the threads, the device and its driver — which is what
197
+ // decides whether anything kept from an earlier run still applies.
198
+ const fingerprint = videoEncoder
199
+ ? await readHostFingerprint({ ffmpegBin, encoder: videoEncoder })
200
+ : null;
201
+ /** @type {Record<string, object[]>} */
202
+ const calibratedModes = {};
203
+ if (videoEncoder) {
204
+ calibratedModes[videoEncoder.kind] =
205
+ (await calibrateEncoder({ ffmpegBin, encoder: videoEncoder, logger })).modes;
206
+ if (videoEncoder.kind !== "software") {
207
+ calibratedModes.software =
208
+ (await calibrateEncoder({ ffmpegBin, encoder: softwareDescriptor(), logger })).modes;
209
+ // A device that passed detection's own test and then none of whose modes
210
+ // passed the product's is not used: detection encodes a synthetic pattern
211
+ // with arguments of its own, and it is the product's arguments a viewer
212
+ // is served with.
213
+ if (calibratedModes[videoEncoder.kind].length === 0) {
214
+ logger.warn(
215
+ `calibration: ${videoEncoder.name} passed detection but none of its modes produced correct segments ` +
216
+ "through the product's own arguments; encoding in software instead"
217
+ );
218
+ videoEncoder = softwareDescriptor();
219
+ }
220
+ }
221
+ }
222
+ const calibration = videoEncoder ? new HostCalibration({ byKind: calibratedModes, fingerprint }) : null;
223
+ // What this host does with a picture it does NOT re-encode. Every other
224
+ // startup measurement prices encoding or decoding, and a copied picture does
225
+ // neither — it reads packets and writes them out again — so that whole branch
226
+ // had no speed until its own run had been running long enough to report one.
227
+ // The encoding layer decides where encoders go from arrivals, and an arrival
228
+ // cannot be computed without a speed, so the moment it mattered most was the
229
+ // moment nothing was known. Measured whatever the encoder is: copying does not
230
+ // touch it.
231
+ const copySpeedX = transcodeAudio
232
+ ? await benchmarkCopySpeed({ ffmpegBin, logger })
233
+ : null;
234
+ // WHAT A START AND A STOP COST HERE, before any viewer exists. Both decide one
235
+ // thing — leave an encoder where it stands, or kill it and start another —
236
+ // and both used to be learned only from runs that had ENDED, so at a cold
237
+ // open they were zero. Zero does not read as "not measured": it reads as
238
+ // "free", and a free move is always taken. Field 2026-09-08: an encoder moved
239
+ // between two adjacent numbers every half second and produced nothing.
240
+ const startStopCost = transcodeAudio
241
+ ? await measureStartAndStop({ ffmpegBin, encoder: videoEncoder, logger })
242
+ : null;
243
+ // Whether this ffmpeg build can tone-map HDR→SDR (zscale + tonemap filters).
244
+ // Detected once; the session manager applies the tonemap chain only for HDR
245
+ // sources on the software path when available.
246
+ const tonemapSupported = transcodeAudio
247
+ ? await detectTonemapSupport({ ffmpegBin, logger })
248
+ : false;
249
+ // Where every file's keyframe table lives, and the only thing that reads one.
250
+ // Built here rather than inside the session manager because it is not a fact
251
+ // about a session: the playback planner warms it while a file is being opened
252
+ // and no session exists yet, and a session created later is handed the very
253
+ // object that warm read filled in.
254
+ // Subtitles are two layers put together, and this is the one place that knows
255
+ // about both: what a file STATES about its subtitle tracks and how a cue is
256
+ // written out is the media layer's, while which clusters may be read, where
257
+ // the cursor stands and what one walk at a time means are the TORRENT's own
258
+ // rules. The media layer used to import the second directly.
259
+ /**
260
+ * One file of one torrent, reduced to what the subtitle walk may know: a
261
+ * name, a length, which ranges are downloaded whole, and how to read one of
262
+ * those without asking the swarm. Piece length, file offsets and the bitfield
263
+ * stay in the torrent's thread.
264
+ *
265
+ * @param {string} sourceKey
266
+ * @param {number} fileIndex
267
+ * @param {object} [known] - The torrent handle when the caller already has it.
268
+ * @returns {Promise<import("./services/media/SubtitleCues.js").HeldFile | null>}
269
+ */
270
+ const heldFileFor = async (sourceKey, fileIndex, known = null) => {
271
+ let torrent = known;
272
+ if (!torrent) {
273
+ const record = sourceRegistry.get(sourceKey);
274
+ if (!record) {
275
+ return null;
276
+ }
277
+ torrent = await torrentPool.getTorrent(record.sourceType, record.source);
278
+ }
279
+ const file = torrent?.files?.[fileIndex];
280
+ if (!file || !(file.length > 0)) {
281
+ return null;
282
+ }
283
+ return {
284
+ sourceKey,
285
+ fileIndex,
286
+ name: String(file.name ?? ""),
287
+ length: file.length,
288
+ heldRanges: () => torrentPool.heldRangesOf(torrent, fileIndex),
289
+ readHeld: (start, end) => torrentPool.readHeldOf(torrent, fileIndex, start, end)
290
+ };
291
+ };
292
+ /** @type {Set<string>} */
293
+ const walksInFlight = new Set();
294
+ /**
295
+ * Walk whatever new cues a file now holds and push them to the viewers.
296
+ *
297
+ * @param {string} sourceKey
298
+ * @param {number} fileIndex
299
+ * @returns {Promise<void>}
300
+ */
301
+ const pushFreshCues = async (sourceKey, fileIndex) => {
302
+ // A pass that arrives while the previous one is still walking is dropped
303
+ // rather than queued: `verified` fires per piece, so on a fast download
304
+ // these arrive many times a second, and a queue of identical passes would
305
+ // only postpone the one that has something new to find. The walk is
306
+ // serialized per file inside `SubtitleCues` anyway.
307
+ const key = `${sourceKey}:${fileIndex}`;
308
+ if (walksInFlight.has(key)) {
309
+ return;
310
+ }
311
+ walksInFlight.add(key);
312
+ try {
313
+ const file = await heldFileFor(sourceKey, fileIndex);
314
+ if (!file) {
315
+ return;
316
+ }
317
+ for (const entry of await warmSubtitleCues(file)) {
318
+ const span = entry.spanStartSeconds === null
319
+ ? "empty"
320
+ : `${entry.spanStartSeconds.toFixed(1)}-${entry.spanEndSeconds.toFixed(1)}s`;
321
+ logger.info(
322
+ `subtitle push ${sourceKey.slice(0, 8)}:${fileIndex} track ${entry.trackIndex}: ` +
323
+ `${entry.cues.length} new cue(s) covering ${span}, ` +
324
+ `clusters walked ${entry.walkedClusters}/${entry.indexedClusters}, cursor ${entry.cursor}`
325
+ );
326
+ onSubtitleCues?.({ sourceKey, fileIndex, ...entry });
327
+ }
328
+ } catch (error) {
329
+ logger.warn(`subtitle push ${sourceKey.slice(0, 8)}:${fileIndex} failed: ${error?.message ?? error}`);
330
+ } finally {
331
+ walksInFlight.delete(key);
332
+ }
333
+ };
334
+ const subtitles = new SubtitleOrchestrator(containerOrchestrator, {
335
+ warm: async (torrent, fileIndex, sourceKey) => {
336
+ const file = await heldFileFor(sourceKey, fileIndex, torrent);
337
+ return file ? warmSubtitleCues(file) : [];
338
+ },
339
+ held: async (torrent, fileIndex, sourceKey, trackNumber) => {
340
+ const file = await heldFileFor(sourceKey, fileIndex, torrent);
341
+ return file ? cuesHeldFor(file, trackNumber) : null;
342
+ },
343
+ tracksOf: async (torrent, fileIndex, sourceKey) => {
344
+ const file = await heldFileFor(sourceKey, fileIndex, torrent);
345
+ return file ? subtitleTracksOf(file) : [];
346
+ },
347
+ declaredTracksOf: async (torrent, fileIndex, sourceKey) => {
348
+ const file = await heldFileFor(sourceKey, fileIndex, torrent);
349
+ return file ? declaredSubtitleTracksOf(file) : [];
350
+ },
351
+ forget: forgetSubtitles
352
+ });
353
+ // WHAT A FILE STATES ABOUT ITSELF IS READ HERE, ON THIS THREAD.
354
+ //
355
+ // The container layer is built from one function — `readRange(start, end)` —
356
+ // and the bytes behind it live in shared memory, so the parse has no reason
357
+ // to happen anywhere else. It used to happen in the torrent thread for one
358
+ // stated reason, that "the main thread cannot open a read stream on one of
359
+ // its files": true of WebTorrent's own API, and not of the bytes. The price
360
+ // was three commands, a second `ContainerOrchestrator` in that thread, and
361
+ // every answer carried back across the channel — which is what made "where is
362
+ // this fact kept" a question at all.
363
+ //
364
+ // The edges are fetched first because the file this is asked about is usually
365
+ // one nobody has played: a sidecar soundtrack is asked about before anyone
366
+ // has chosen it, and a Matroska file's Cues sit at the END behind a SeekHead
367
+ // in the head, so a read that has neither waits for the swarm twice over.
368
+ const containerOver = async ({ sourceKey, fileIndex, tailBytes = 0 }) => {
369
+ const record = sourceRegistry.get(sourceKey);
370
+ if (!record) {
371
+ return null;
372
+ }
373
+ const torrent = await torrentPool.getTorrent(record.sourceType, record.source);
374
+ const file = torrent?.files?.[fileIndex];
375
+ if (!file || !(file.length > 0)) {
376
+ return null;
377
+ }
378
+ try {
379
+ await torrentPool.prefetchFileEdges(torrent, fileIndex, {
380
+ headBytes: CONTAINER_HEAD_BYTES,
381
+ tailBytes,
382
+ timeoutMs: 60_000
383
+ });
384
+ } catch {
385
+ // A prefetch that failed is not a reason to skip the read: the read
386
+ // fetches what it needs itself, only more slowly.
387
+ }
388
+ return {
389
+ sourceKey,
390
+ fileIndex,
391
+ readRange: (start, end) =>
392
+ torrentPool.readRangeOf(torrent, fileIndex, start, Math.min(end, file.length - 1)),
393
+ fileSize: file.length,
394
+ label: String(file.name ?? "")
395
+ };
396
+ };
397
+ const keyframeTables = new KeyframeTables({
398
+ // Read by the same container that answers the track table and the media
399
+ // info, from the same header, and now in the same thread as the session
400
+ // that is waiting for it.
401
+ readTable: async ({ sourceKey, fileIndex }) => {
402
+ // Both edges: the Cues of a Matroska file are at the end.
403
+ const params = await containerOver({ sourceKey, fileIndex, tailBytes: CONTAINER_HEAD_BYTES });
404
+ if (!params) {
405
+ return null;
406
+ }
407
+ const index = await containerOrchestrator.getKeyframeIndex(params);
408
+ return {
409
+ times: index?.times ?? null,
410
+ tolerance: index?.tolerance ?? 0,
411
+ // Which container answered, whether or not it produced a table: the
412
+ // refusal that follows names it, and a measurement of how often an
413
+ // index disagrees with its own file cannot be read without it.
414
+ format: (await containerOrchestrator.getContainer(params))?.formatName ?? "unrecognised"
415
+ };
416
+ }
417
+ });
418
+ const outputParts = wireOutputs({
419
+ enabled: transcodeAudio,
420
+ keyframeTables,
421
+ ffmpegBin,
422
+ localBindHost: host,
423
+ localPort: selectedPort,
424
+ videoEncoder,
425
+ calibration,
426
+ decodeCostModel,
427
+ contentionPenalties,
428
+ copySpeedX,
429
+ startStopCost,
430
+ tonemapSupported,
431
+ segmentFormatId: segmentFormat,
432
+ stateDir,
433
+ diagnostics,
434
+ // Live download stats accessor for the realtime budget: lets it tell a
435
+ // CPU-bound transcode from a download-starved input before downscaling.
436
+ // What every torrent here has moved, so the proxy can price its own
437
+ // downloading, hashing and delivery against the machine (roadmap item 7).
438
+ // What the spilled pieces weigh on the torrent thread, and how to tell them
439
+ // their share of the disk. The owner of the disk is on this side, where the
440
+ // segments are; the pieces are on the other.
441
+ spillDisk: typeof torrentPool.allowSpillBytes === "function"
442
+ ? {
443
+ held: () => torrentPool.spilledBytes ?? 0,
444
+ allow: (bytes) => torrentPool.allowSpillBytes(bytes)
445
+ }
446
+ : null,
447
+ // Files downloaded whole, kept as files. They live on the torrent thread
448
+ // too, and until 2026-09-14 they had no bound of any kind — a 2.8 GB film
449
+ // on a host whose disk is often a 32 GB card.
450
+ wholeFiles: typeof torrentPool.allowWholeFileBytes === "function"
451
+ ? {
452
+ held: () => torrentPool.wholeFileBytes ?? 0,
453
+ allow: (bytes) => torrentPool.allowWholeFileBytes(bytes),
454
+ root: completedFilesRoot()
455
+ }
456
+ : null,
457
+ // The pieces held IN memory, on the torrent thread. Told their share by the
458
+ // same owner that divides the disk: what does not fit here is spilled
459
+ // there, so the two cannot be divided apart.
460
+ memoryClaimant: typeof torrentPool.allowMemoryBytes === "function"
461
+ ? {
462
+ held: () => torrentPool.memoryClaim?.held ?? 0,
463
+ wanted: () => torrentPool.memoryClaim?.wanted ?? 0,
464
+ allow: (bytes) => torrentPool.allowMemoryBytes(bytes)
465
+ }
466
+ : null,
467
+ diagnosticsRoot: stateDir || "",
468
+ budgetPolicy,
469
+ getTorrentTotals: async () => {
470
+ if (typeof torrentPool.getTorrentTotals !== "function") {
471
+ return null;
472
+ }
473
+ return torrentPool.getTorrentTotals();
474
+ },
475
+ // The priority map, on its way to the downloading. It lives in another
476
+ // thread, so the map crosses the worker channel; what it does with it —
477
+ // seconds into bytes, what to ask the swarm for, what to keep in memory —
478
+ // is its own business.
479
+ setPriorityMap: async ({ sourceKey, fileIndex, durationSeconds, zones }) => {
480
+ const record = sourceRegistry.get(sourceKey);
481
+ if (!record) {
482
+ return;
483
+ }
484
+ try {
485
+ await torrentPool.setPriorityMap({ sourceKey, fileIndex, durationSeconds, zones });
486
+ } catch {
487
+ // Best effort: the map is republished on the next change, and the
488
+ // downloading goes on serving reads meanwhile.
489
+ }
490
+ },
491
+ getSourceStats: async (sourceKey, fileIndex) => {
492
+ const record = sourceRegistry.get(sourceKey);
493
+ if (!record) {
494
+ return null;
495
+ }
496
+ try {
497
+ const torrent = await torrentPool.getTorrent(record.sourceType, record.source);
498
+ // Awaited for the same reason as the stats route: this now crosses a
499
+ // thread boundary and returns a promise.
500
+ return await torrentPool.getFileStats(torrent, Number.isInteger(fileIndex) ? fileIndex : null);
501
+ } catch {
502
+ return null;
503
+ }
504
+ },
505
+ // Reuse the media info the planner already probed for this file (same
506
+ // ffmpeg scan) so createSession skips its own probe. Late-bound: invoked
507
+ // only at session-create time, after playbackPlanner is initialised.
508
+ getCachedMediaInfo: (params) => playbackPlanner.getCachedMediaInfo(params),
509
+ // The file's audio tracks, for the master playlist's rendition group. Already
510
+ // probed for the browser's audio menu; read from there rather than probed again.
511
+ getCachedAudioTracks: (params) => playbackPlanner.getCachedAudioTracks(params),
512
+ // What a file declares about itself, read by the container layer from the
513
+ // same header its track table comes from. This is how the session learns
514
+ // where a soundtrack shipped as its own file begins — it used to spawn an
515
+ // ffmpeg over this proxy's own HTTP to ask the same question of the same
516
+ // bytes, and that read cost 8.1 s of every cold start (field 2026-09-03).
517
+ getContainerMediaInfo: async ({ sourceKey, fileIndex }) => {
518
+ try {
519
+ const params = await containerOver({ sourceKey, fileIndex });
520
+ return params ? await containerOrchestrator.getMediaInfo(params) : null;
521
+ } catch {
522
+ return null;
523
+ }
524
+ },
525
+ // Pull one whole file onto the disk. Used for a soundtrack that ships beside
526
+ // the picture, once the encoder is as far ahead of the viewer as it is
527
+ // allowed to get — the one moment the swarm's capacity is demonstrably
528
+ // spare. A bounded read of the file's whole length, NOT `file.select()`:
529
+ // selecting a file alongside the readers' own windows is what made a seek
530
+ // wait 93 s while the swarm fetched 2.47 GB in file order (see
531
+ // `#syncSelections` in `torrent/torrent-pool.js`).
532
+ fetchWholeFile: async ({ sourceKey, fileIndex }) => {
533
+ const record = sourceRegistry.get(sourceKey);
534
+ if (!record) {
535
+ return;
536
+ }
537
+ const torrent = await torrentPool.getTorrent(record.sourceType, record.source);
538
+ // The same background fill the warm-up starts when a file is chosen, not a
539
+ // second way of doing it. It is guarded against running twice on one file,
540
+ // so the two triggers converge instead of putting two readers on the same
541
+ // soundtrack — and only one of them would have stood aside for the
542
+ // picture. This trigger remains for the session that never had a warm-up
543
+ // before it.
544
+ await torrentPool.fillFileInBackground?.(torrent, fileIndex);
545
+ }
546
+ });
547
+ // What the last life of this process left on the disk. The kernel kills this
548
+ // one often enough for that to be an ordinary state rather than an odd one —
549
+ // twice in a single viewing on 2026-09-02 — and when it does, no exit handler
550
+ // runs and nothing is cleared up. So this is both the cleanup and the only
551
+ // record that those encoders ended at all: it says what it found before it
552
+ // decides anything, keeps the segments whose closure is proven, and removes
553
+ // the one piece per output that was being written when the process died.
554
+ outputParts.lifecycle.adoptSegmentsLeftBehind();
555
+ const playbackPlanner = createPlaybackPlanner({
556
+ ffmpegBin,
557
+ transcodeAudioEnabled: transcodeAudio,
558
+ localBaseUrl: outputParts.localBaseUrl,
559
+ sourceRegistry,
560
+ torrentPool,
561
+ sidecarsFromTorrent: (torrent, fileIndex) => contentsOf(torrent).sidecarsOf(fileIndex),
562
+ // What a file declares about its own tracks, parsed on this thread from the
563
+ // header the swarm delivered. The planner used to ask the torrent pool for
564
+ // this, which meant the media layer asking the torrent layer to parse a
565
+ // container on its behalf, in the other thread, with the answer carried
566
+ // back over the channel.
567
+ declaredTracksOf: async ({ sourceKey, fileIndex }) => {
568
+ const params = await containerOver({ sourceKey, fileIndex });
569
+ return params ? await containerOrchestrator.getTracks(params) : [];
570
+ },
571
+ warmKeyframeIndex: (params) => keyframeTables.warm(params),
572
+ expectedFirstSegmentMs: () => outputParts.hostTimings.expectedFirstSegmentMs(),
573
+ expectedSessionCreateMs: () => outputParts.hostTimings.expectedSessionCreateMs(),
574
+ // The quality menu is on screen from the moment a file is opened, so the
575
+ // heights this host can actually serve have to be answerable before any
576
+ // encoder exists — from the probe and the startup benchmarks alone.
577
+ predictOfferedHeights: (mediaInfo) => outputParts.quality.predictOfferedHeights(mediaInfo)
578
+ });
579
+
580
+ app.get("/health", async (req, reply) => handleHealthGet(req, reply, { version }));
581
+ app.get("/healthz", async (req, reply) => handleHealthzGet(req, reply, { version }));
582
+ // Off unless --delivery-sink was given; see the route for why it exists.
583
+ app.get("/api/delivery-sink", async (req, reply) =>
584
+ handleApiDeliverySinkGet(req, reply, { enabled: deliverySink === true })
585
+ );
586
+ // Bytes a browser can time its link with before any film has been chosen.
587
+ app.get("/api/link-probe", async (req, reply) => handleApiLinkProbeGet(req, reply));
588
+ app.post("/api/sources", async (req, reply) =>
589
+ handleApiSourcesPost(req, reply, { sourceRegistry })
590
+ );
591
+ app.get("/api/sources/:sourceKey/stats", async (req, reply) =>
592
+ handleApiSourceStatsGet(req, reply, { sourceRegistry, torrentPool })
593
+ );
594
+ app.get("/api/sources/:sourceKey/files", async (req, reply) =>
595
+ handleApiSourceFilesGet(req, reply, { sourceRegistry, torrentPool })
596
+ );
597
+ app.post("/api/sources/:sourceKey/warm", async (req, reply) =>
598
+ handleApiSourceWarmPost(req, reply, {
599
+ sourceRegistry,
600
+ torrentPool,
601
+ // How long the file runs, read on this thread from the header. The warm
602
+ // turns a position in seconds into a byte offset and needs it; it used to
603
+ // read the container itself, in the torrent thread, to find out.
604
+ durationOf: async (params) => {
605
+ const over = await containerOver(params);
606
+ return over ? (await containerOrchestrator.getMediaInfo(over))?.durationSeconds ?? null : null;
607
+ }
608
+ })
609
+ );
610
+ // The browser's own log, kept beside the proxy's. See the route's own file
611
+ // for why it is here and not only on the registry server.
612
+ const clientLogs = logFile ? createClientLogFiles(logFile) : null;
613
+ app.post("/api/client-logs", async (req, reply) =>
614
+ handleApiClientLogsPost(req, reply, { clientLogs })
615
+ );
616
+ app.post("/api/playback-plan", async (req, reply) =>
617
+ handleApiPlaybackPlanPost(req, reply, { playbackPlanner })
618
+ );
619
+ app.get("/api/subtitles", async (req, reply) =>
620
+ handleApiSubtitlesGet(req, reply, {
621
+ sourceRegistry,
622
+ torrentPool,
623
+ ffmpegBin,
624
+ localBaseUrl: outputParts.localBaseUrl,
625
+ viewers: outputParts.viewers,
626
+ subtitles
627
+ })
628
+ );
629
+ app.get("/stream", async (req, reply) =>
630
+ handleStreamGet(req, reply, {
631
+ sourceRegistry,
632
+ torrentPool,
633
+ // So a session that has produced nothing yet can still show it is being
634
+ // fed. The route knows only files; the session id rides on the URL the
635
+ // session itself built.
636
+ noteInputBytes: (sessionId, bytes) => outputParts.encodeRuns.noteInputBytes(sessionId, bytes)
637
+ })
638
+ );
639
+ app.post("/api/transcode-sessions", async (req, reply) =>
640
+ handleApiTranscodeSessionsPost(req, reply, { viewerRequests: outputParts.viewerRequests, renditions: outputParts.renditions, quality: outputParts.quality, outputs: outputParts.outputs, lookaheadSeconds: outputParts.lookaheadSeconds, sourceRegistry, torrentPool })
641
+ );
642
+ app.post("/api/transcode-sessions/:sessionId/release", async (req, reply) =>
643
+ handleApiTranscodeSessionReleasePost(req, reply, { lifecycle: outputParts.lifecycle })
644
+ );
645
+ app.get("/api/transcode-sessions/:sessionId/progress", async (req, reply) =>
646
+ handleApiTranscodeSessionsProgressGet(req, reply, { viewerRequests: outputParts.viewerRequests })
647
+ );
648
+ app.post("/api/transcode-sessions/:sessionId/net-report", async (req, reply) =>
649
+ // A viewer's statement about itself goes to the VIEWER layer, not through
650
+ // the session manager: what it needs is the live sessions and the registry
651
+ // of viewers, and nothing about encoding.
652
+ handleApiTranscodeSessionNetReportPost(req, reply, {
653
+ outputs: outputParts.outputs,
654
+ viewers: outputParts.viewers,
655
+ renditions: outputParts.renditions,
656
+ quality: outputParts.quality
657
+ })
658
+ );
659
+ app.post("/api/transcode-sessions/:sessionId/fragment-far", async (req, reply) =>
660
+ handleApiTranscodeSessionFragmentFarPost(req, reply, { serving: outputParts.serving })
661
+ );
662
+ app.post("/api/transcode-sessions/:sessionId/seek", async (req, reply) =>
663
+ handleApiTranscodeSessionSeekPost(req, reply, { viewerRequests: outputParts.viewerRequests })
664
+ );
665
+ app.get("/transcode/:sessionId/:fileName", async (req, reply) =>
666
+ handleTranscodeSessionFileGet(req, reply, { serving: outputParts.serving, viewerRequests: outputParts.viewerRequests })
667
+ );
668
+ // A quality variant's files. Registered before the static handler for the
669
+ // same reason as the line above, and kept a separate route rather than a
670
+ // wildcard so the height stays a parsed parameter.
671
+ // Registered BEFORE the variant file route: `warm` is not a file name, and
672
+ // Fastify matches a static segment ahead of a parameter either way — stated
673
+ // here so the order is not "tidied" into a bug.
674
+ app.get("/transcode/:sessionId/v/:height/warm", async (req, reply) =>
675
+ handleTranscodeVariantWarmGet(req, reply, { renditions: outputParts.renditions, serving: outputParts.serving, viewerRequests: outputParts.viewerRequests })
676
+ );
677
+ app.get("/transcode/:sessionId/a/:track/warm", async (req, reply) =>
678
+ handleTranscodeAudioWarmGet(req, reply, { renditions: outputParts.renditions, serving: outputParts.serving, viewerRequests: outputParts.viewerRequests })
679
+ );
680
+ app.get("/transcode/:sessionId/a/:trackIndex/:fileName", async (req, reply) =>
681
+ handleTranscodeAudioFileGet(req, reply, { renditions: outputParts.renditions, serving: outputParts.serving, viewerRequests: outputParts.viewerRequests })
682
+ );
683
+ app.get("/transcode/:sessionId/v/:height/:fileName", async (req, reply) =>
684
+ handleTranscodeVariantFileGet(req, reply, { renditions: outputParts.renditions, serving: outputParts.serving, viewerRequests: outputParts.viewerRequests })
685
+ );
686
+ await app.register(fastifyStatic, {
687
+ root: publicRoot,
688
+ prefix: "/",
689
+ serveDotFiles: true
690
+ });
691
+
692
+ app.addHook("onClose", async () => {
693
+ // Order matters: stop the ffmpeg readers (HLS sessions) before destroying
694
+ // the torrents whose files they read from, then remove the torrent data.
695
+ await outputParts.lifecycle.disposeAll();
696
+ await torrentPool.destroyAll();
697
+ });
698
+
699
+ await app.listen({ host, port: selectedPort });
700
+ return {
701
+ app,
702
+ port: selectedPort,
703
+ // Asked over the tunnel when the proxy a viewer landed on has refused their
704
+ // file: could THIS host sustain it? Answered from the startup benchmarks
705
+ // and a description, so it needs no torrent and costs milliseconds.
706
+ outputParts,
707
+ // The browser only ever knows a source by its REGISTRY key (a hash of the
708
+ // raw request bytes, scoped to one API session) — never the torrent
709
+ // pool's own key (the content's infohash, shared across a magnet and a
710
+ // `.torrent` naming the same film). The subtitle push subscription is
711
+ // recorded from a browser request and published from the pool's side, so
712
+ // resolving one into the other is what lets the two ends agree on what
713
+ // they are both calling "sourceKey".
714
+ sourceRegistry,
715
+ // Which films this host holds, for the health poll the browser makes before
716
+ // it picks a proxy. The pool is on the worker thread and this is the way to
717
+ // it from the process that answers that poll.
718
+ torrentPool
719
+ };
720
+ }