@torrent-tv/proxy 2.89.17 → 2.89.18

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 (299) hide show
  1. package/README.md +14 -3
  2. package/package.json +10 -2
  3. package/server.js +0 -11
  4. package/.claude/commands/opsx/apply.md +0 -155
  5. package/.claude/commands/opsx/archive.md +0 -160
  6. package/.claude/commands/opsx/explore.md +0 -174
  7. package/.claude/commands/opsx/propose.md +0 -109
  8. package/.claude/commands/opsx/sync.md +0 -143
  9. package/.claude/skills/openspec-apply-change/SKILL.md +0 -159
  10. package/.claude/skills/openspec-archive-change/SKILL.md +0 -117
  11. package/.claude/skills/openspec-explore/SKILL.md +0 -289
  12. package/.claude/skills/openspec-propose/SKILL.md +0 -113
  13. package/.claude/skills/openspec-sync-specs/SKILL.md +0 -147
  14. package/.dockerignore +0 -5
  15. package/AGENTS.md +0 -363
  16. package/CHANGELOG.md +0 -2377
  17. package/Dockerfile +0 -30
  18. package/biome.json +0 -412
  19. package/corpus.tmp.json +0 -1
  20. package/docs/browser-proxy-contract.md +0 -155
  21. package/docs/container-architecture.md +0 -273
  22. package/docs/download-architecture.md +0 -261
  23. package/docs/encode-architecture.md +0 -765
  24. package/docs/encode-run-state.md +0 -96
  25. package/docs/logs.md +0 -151
  26. package/docs/playback-start-forecast.md +0 -113
  27. package/docs/socks5-egress-research.md +0 -114
  28. package/docs/storage-architecture.md +0 -273
  29. package/knip.json +0 -14
  30. package/main2.cpuprofile +0 -1
  31. package/openspec/changes/adaptive-bitrate/.openspec.yaml +0 -0
  32. package/openspec/changes/adaptive-bitrate/design.md +0 -92
  33. package/openspec/changes/adaptive-bitrate/proposal.md +0 -56
  34. package/openspec/changes/adaptive-bitrate/specs/adaptive-bitrate/spec.md +0 -51
  35. package/openspec/changes/adaptive-bitrate/tasks.md +0 -40
  36. package/openspec/changes/binary-distribution/.openspec.yaml +0 -2
  37. package/openspec/changes/binary-distribution/proposal.md +0 -53
  38. package/openspec/changes/chunked-request-bodies/design.md +0 -81
  39. package/openspec/changes/chunked-request-bodies/proposal.md +0 -53
  40. package/openspec/changes/chunked-request-bodies/specs/chunked-request-bodies/spec.md +0 -30
  41. package/openspec/changes/chunked-request-bodies/tasks.md +0 -40
  42. package/openspec/changes/cold-start/design.md +0 -98
  43. package/openspec/changes/cold-start/proposal.md +0 -62
  44. package/openspec/changes/cold-start/specs/cold-start/spec.md +0 -47
  45. package/openspec/changes/cold-start/tasks.md +0 -48
  46. package/openspec/changes/disk-cap/.openspec.yaml +0 -2
  47. package/openspec/changes/disk-cap/proposal.md +0 -37
  48. package/openspec/changes/disk-cap/specs/disk-cap/spec.md +0 -25
  49. package/openspec/changes/disk-cap/tasks.md +0 -19
  50. package/openspec/changes/proxy-observability/.openspec.yaml +0 -2
  51. package/openspec/changes/proxy-observability/design.md +0 -38
  52. package/openspec/changes/proxy-observability/proposal.md +0 -49
  53. package/openspec/changes/proxy-observability/specs/observability/spec.md +0 -33
  54. package/openspec/changes/proxy-observability/tasks.md +0 -21
  55. package/openspec/changes/seek-debounce/.openspec.yaml +0 -0
  56. package/openspec/changes/seek-debounce/design.md +0 -118
  57. package/openspec/changes/seek-debounce/proposal.md +0 -59
  58. package/openspec/changes/seek-debounce/specs/seek-debounce/spec.md +0 -41
  59. package/openspec/changes/seek-debounce/tasks.md +0 -33
  60. package/openspec/changes/subtitle-language/.openspec.yaml +0 -2
  61. package/openspec/changes/subtitle-language/proposal.md +0 -50
  62. package/openspec/changes/subtitle-language/specs/subtitle-language/spec.md +0 -34
  63. package/openspec/changes/subtitle-language/tasks.md +0 -26
  64. package/openspec/changes/track-selection/.openspec.yaml +0 -2
  65. package/openspec/changes/track-selection/design.md +0 -47
  66. package/openspec/changes/track-selection/proposal.md +0 -45
  67. package/openspec/changes/track-selection/specs/track-selection/spec.md +0 -41
  68. package/openspec/changes/track-selection/tasks.md +0 -20
  69. package/openspec/changes/transcode-quality/.openspec.yaml +0 -2
  70. package/openspec/changes/transcode-quality/proposal.md +0 -51
  71. package/openspec/changes/transcode-quality/specs/transcode-quality/spec.md +0 -139
  72. package/openspec/changes/transcode-quality/tasks.md +0 -63
  73. package/openspec/config.yaml +0 -38
  74. package/public/.well-known/appspecific/com.chrome.devtools.json +0 -1
  75. package/research/delivery-wedge-stand-ladder-2026-09-13.md +0 -329
  76. package/research/double-spawn-2026-09-10.md +0 -171
  77. package/research/handover-2026-09-13.md +0 -209
  78. package/research/handover-reader-claims-removal-2026-09-12.md +0 -166
  79. package/research/piece-withdrawn-but-still-claimed-2026-09-12.md +0 -175
  80. package/research/preset-benchmark-size-scaling-2026-08-22.md +0 -55
  81. package/research/priority-map-is-the-truth-2026-09-12.md +0 -225
  82. package/research/valgrind-replica-2026-08-27/logs.tar.gz +0 -0
  83. package/scripts/build-audio-samples.mjs +0 -31
  84. package/scripts/render-run-graph.js +0 -173
  85. package/test/asset-paths.test.js +0 -28
  86. package/test/audio-buffer-continuity.test.js +0 -77
  87. package/test/audio-calibration.test.js +0 -23
  88. package/test/audio-inventory.test.js +0 -212
  89. package/test/audio-track-bitrate.test.js +0 -40
  90. package/test/audio-warm-recorded.test.js +0 -63
  91. package/test/auto-quality-step.test.js +0 -762
  92. package/test/available-share.test.js +0 -76
  93. package/test/background-fill.test.js +0 -190
  94. package/test/client-log-file.test.js +0 -126
  95. package/test/client-logs-route.test.js +0 -34
  96. package/test/coalesce.test.js +0 -39
  97. package/test/completed-files.test.js +0 -115
  98. package/test/concurrent-cost.test.js +0 -148
  99. package/test/container-media-info.test.js +0 -228
  100. package/test/contention.test.js +0 -76
  101. package/test/core-dumps.test.js +0 -57
  102. package/test/coverage-follows-the-disk.test.js +0 -233
  103. package/test/coverage-map.test.js +0 -195
  104. package/test/cue-push-carries-what-the-walk-found.test.js +0 -58
  105. package/test/cut-times-timeline.test.js +0 -65
  106. package/test/cuts-follow-published-grid.test.js +0 -109
  107. package/test/data-channel-frame.test.js +0 -63
  108. package/test/data-channel-viewer-lifecycle.test.js +0 -193
  109. package/test/dead-torrent-handle.test.js +0 -52
  110. package/test/declared-rates.test.js +0 -116
  111. package/test/declared-tracks.test.js +0 -43
  112. package/test/decode-cost-fit.test.js +0 -202
  113. package/test/decode-cost.test.js +0 -475
  114. package/test/delivery-probe-field.test.js +0 -173
  115. package/test/delivery-probe.test.js +0 -382
  116. package/test/demand-register.test.js +0 -206
  117. package/test/dht-bootstrap.test.js +0 -72
  118. package/test/drain-threat.test.js +0 -65
  119. package/test/duplicate-add.test.js +0 -48
  120. package/test/encode-admission.test.js +0 -170
  121. package/test/encode-cost.test.js +0 -275
  122. package/test/encode-exit.test.js +0 -111
  123. package/test/encode-orchestrator.test.js +0 -426
  124. package/test/encode-plan-viewers.test.js +0 -725
  125. package/test/encode-plan.test.js +0 -545
  126. package/test/encode-run-state.test.js +0 -314
  127. package/test/encode-run.test.js +0 -248
  128. package/test/encode-slope.test.js +0 -46
  129. package/test/encoded-output.test.js +0 -39
  130. package/test/encoder-kinds.test.js +0 -122
  131. package/test/encoder-readings.test.js +0 -68
  132. package/test/episode-naming.test.js +0 -131
  133. package/test/every-claimant-is-told-its-share.test.js +0 -152
  134. package/test/failed-encoder-still-serves-what-exists.test.js +0 -354
  135. package/test/failed-start-breaker.test.js +0 -202
  136. package/test/fastest-wires.test.js +0 -143
  137. package/test/ffmpeg-stream-counts.test.js +0 -101
  138. package/test/file-edges.test.js +0 -191
  139. package/test/files-in-use.test.js +0 -34
  140. package/test/fixtures/audio-frame-boundary.json +0 -1
  141. package/test/fixtures/refused-audio-20261002.json +0 -10
  142. package/test/fmp4-presentation-clock.test.js +0 -148
  143. package/test/fmp4-self-contained.test.js +0 -80
  144. package/test/fmp4-timestamps.test.js +0 -140
  145. package/test/fmp4-track-completeness.test.js +0 -41
  146. package/test/format-keyed-outputs.test.js +0 -78
  147. package/test/health-metrics.test.js +0 -36
  148. package/test/held-read-ownership.test.js +0 -77
  149. package/test/held-request-width.test.js +0 -157
  150. package/test/helpers/encode-run.js +0 -248
  151. package/test/helpers/held-file.js +0 -77
  152. package/test/helpers/manager.js +0 -62
  153. package/test/helpers/matroska-file.js +0 -296
  154. package/test/helpers/output-spec.js +0 -63
  155. package/test/host-load.test.js +0 -68
  156. package/test/index-accuracy-is-about-the-file.test.js +0 -128
  157. package/test/init-compat.test.js +0 -219
  158. package/test/input-lost-quiets-the-plan.test.js +0 -261
  159. package/test/input-unavailable.test.js +0 -48
  160. package/test/installation-id.test.js +0 -39
  161. package/test/keeping-period.test.js +0 -83
  162. package/test/keyframe-index-accuracy.test.js +0 -188
  163. package/test/keyframe-table-budget.test.js +0 -123
  164. package/test/keyframes-belong-to-the-file.test.js +0 -249
  165. package/test/learned-median.test.js +0 -37
  166. package/test/letting-go-asks-who-is-watching.test.js +0 -191
  167. package/test/limit-row-by-area.test.js +0 -60
  168. package/test/link-budget.test.js +0 -111
  169. package/test/link-probe.test.js +0 -56
  170. package/test/local-encoding-data.test.js +0 -273
  171. package/test/logger-repeats.test.js +0 -134
  172. package/test/look-ahead-coverage.test.js +0 -56
  173. package/test/manager-surface.test.js +0 -291
  174. package/test/matroska-blocks.test.js +0 -0
  175. package/test/matroska-cluster-discovery.test.js +0 -323
  176. package/test/matroska-cues-track.test.js +0 -192
  177. package/test/media-time.test.js +0 -62
  178. package/test/memory-budget.test.js +0 -390
  179. package/test/move-cost.test.js +0 -216
  180. package/test/mp4-composition-times.test.js +0 -0
  181. package/test/mp4-subtitles.test.js +0 -173
  182. package/test/one-authority.test.js +0 -285
  183. package/test/one-budget.test.js +0 -149
  184. package/test/one-output-one-session.test.js +0 -273
  185. package/test/one-resource-divided.test.js +0 -185
  186. package/test/one-serving-output-per-height.test.js +0 -458
  187. package/test/orchestrator-wired.test.js +0 -207
  188. package/test/output-catalog.test.js +0 -39
  189. package/test/output-shape.test.js +0 -21
  190. package/test/output-spec.test.js +0 -251
  191. package/test/output-speed.test.js +0 -92
  192. package/test/outputs-being-prepared.test.js +0 -40
  193. package/test/packet-witness-ring.test.js +0 -236
  194. package/test/packet-witness.test.js +0 -148
  195. package/test/piece-completeness.test.js +0 -78
  196. package/test/piece-disk-store.test.js +0 -401
  197. package/test/piece-from-whole-file.test.js +0 -129
  198. package/test/piece-lru.test.js +0 -319
  199. package/test/piece-reader.test.js +0 -162
  200. package/test/piece-selection-offset.test.js +0 -85
  201. package/test/piece-store-eviction.test.js +0 -595
  202. package/test/piece-store-never-refuses.test.js +0 -153
  203. package/test/piece-store-reservations.test.js +0 -403
  204. package/test/piece-store-slow-disk.test.js +0 -159
  205. package/test/piece-tail.test.js +0 -97
  206. package/test/plan-host-timings.test.js +0 -68
  207. package/test/plan-is-steady.test.js +0 -225
  208. package/test/playback-readiness.test.js +0 -518
  209. package/test/playback-start-regressions.test.js +0 -101
  210. package/test/priority-map-download.test.js +0 -174
  211. package/test/priority-map-emptied.test.js +0 -204
  212. package/test/priority-map-per-output.test.js +0 -215
  213. package/test/priority-map.test.js +0 -163
  214. package/test/probe-wedge-certainty.test.js +0 -60
  215. package/test/produced-copy-choice.test.js +0 -213
  216. package/test/published-timeline.test.js +0 -83
  217. package/test/quality-offer.test.js +0 -160
  218. package/test/quality-variants.test.js +0 -1274
  219. package/test/rate-control-in-the-output.test.js +0 -181
  220. package/test/read-survives-withdrawal.test.js +0 -164
  221. package/test/read-window.test.js +0 -365
  222. package/test/reads-not-yet-arrived.test.js +0 -218
  223. package/test/resume-warm.test.js +0 -39
  224. package/test/run-graph-drift.test.js +0 -37
  225. package/test/run-inputs.test.js +0 -80
  226. package/test/run-intervals.test.js +0 -100
  227. package/test/run-position-follows-published-grid.test.js +0 -89
  228. package/test/run-progress.test.js +0 -64
  229. package/test/same-height-switch.test.js +0 -663
  230. package/test/seek-frees-the-viewers-requests.test.js +0 -217
  231. package/test/seek-landing.test.js +0 -109
  232. package/test/seek-target-not-superseded.test.js +0 -195
  233. package/test/segment-closed-on-both-branches.test.js +0 -114
  234. package/test/segment-cut-times.test.js +0 -63
  235. package/test/segment-demand.test.js +0 -80
  236. package/test/segment-hold-supersede.test.js +0 -91
  237. package/test/segment-judged-by-its-own-header.test.js +0 -222
  238. package/test/segment-serve-wiring.test.js +0 -481
  239. package/test/segment-store-eviction.test.js +0 -247
  240. package/test/segment-store.test.js +0 -352
  241. package/test/segment-true-start.test.js +0 -86
  242. package/test/segments-are-shared.test.js +0 -176
  243. package/test/send-chunk-bytes.test.js +0 -86
  244. package/test/serving-existing-output.test.js +0 -111
  245. package/test/serving-output.test.js +0 -102
  246. package/test/shared-piece-store.test.js +0 -310
  247. package/test/sidecar-naming.test.js +0 -142
  248. package/test/soundtrack-rate-bound.test.js +0 -289
  249. package/test/source-file.test.js +0 -133
  250. package/test/source-files-route.test.js +0 -113
  251. package/test/source-registry.test.js +0 -18
  252. package/test/split-a-stretch.test.js +0 -139
  253. package/test/stale-request-after-seek.test.js +0 -158
  254. package/test/startup-readings.test.js +0 -124
  255. package/test/stream-route.test.js +0 -237
  256. package/test/subtitle-cue-framing.test.js +0 -200
  257. package/test/subtitle-cue-source.test.js +0 -102
  258. package/test/subtitle-cue-walk.test.js +0 -385
  259. package/test/subtitle-cursor.test.js +0 -63
  260. package/test/subtitle-defaults.test.js +0 -118
  261. package/test/subtitle-language.test.js +0 -252
  262. package/test/subtitle-metadata.test.js +0 -15
  263. package/test/subtitle-plan-waits.test.js +0 -125
  264. package/test/subtitle-track-numbering.test.js +0 -391
  265. package/test/supply-margin.test.js +0 -190
  266. package/test/swarm-follows-readers.test.js +0 -174
  267. package/test/swarm-reach.test.js +0 -130
  268. package/test/swarm-selection.test.js +0 -220
  269. package/test/tail-duplication.test.js +0 -167
  270. package/test/the-channel-has-both-ends.test.js +0 -91
  271. package/test/the-torrent-thread-serves-bytes.test.js +0 -107
  272. package/test/thread-pool.test.js +0 -53
  273. package/test/timeline.test.js +0 -84
  274. package/test/torrent-contents.test.js +0 -235
  275. package/test/torrent-cost.test.js +0 -71
  276. package/test/torrent-files.test.js +0 -221
  277. package/test/torrent-naming.test.js +0 -255
  278. package/test/tracks-begin-together.test.js +0 -209
  279. package/test/tunnel-renewal.test.js +0 -167
  280. package/test/two-viewers-one-picture.test.js +0 -508
  281. package/test/upload-hurry.test.js +0 -186
  282. package/test/usrsctp-state.test.js +0 -131
  283. package/test/video-facts.test.js +0 -102
  284. package/test/viewer-assignments.test.js +0 -143
  285. package/test/viewer-outputs.test.js +0 -285
  286. package/test/viewer-presence.test.js +0 -116
  287. package/test/viewer-requests-admission.test.js +0 -92
  288. package/test/viewer-starts-on-map.test.js +0 -27
  289. package/test/viewer-states.test.js +0 -192
  290. package/test/viewer-subtitle-subscription.test.js +0 -96
  291. package/test/viewer.test.js +0 -139
  292. package/test/viewing-generations.test.js +0 -390
  293. package/test/visible-rung.test.js +0 -37
  294. package/test/wait-ledger.test.js +0 -100
  295. package/test/webrtc-shim.test.js +0 -149
  296. package/test/wedge-certainty.test.js +0 -131
  297. package/test/whole-file-held-read.test.js +0 -30
  298. package/test/withdraw-piece-claim.test.js +0 -212
  299. package/test/worker-channel.test.js +0 -158
@@ -1,289 +0,0 @@
1
- /**
2
- * @file The most a soundtrack's codec allows it to carry, how it is read from
3
- * the containers, how a later reading is merged, and how the mode a track is
4
- * produced in is chosen from it.
5
- *
6
- * Plain values and buffers built here: no process, no torrent, no file on disk.
7
- */
8
-
9
- import test from "node:test";
10
- import assert from "node:assert/strict";
11
- import {
12
- codecParametersOf,
13
- parseAudioSpecificConfig,
14
- peakKbpsOf
15
- } from "../services/media/tracks/audio-bound.js";
16
- import { AudioTrack } from "../services/media/tracks/AudioTrack.js";
17
- import { Mp4Container } from "../services/media/container/Mp4Container.js";
18
- import { Container } from "../services/media/container/Container.js";
19
- import { buildAudioInventory, mergeInventoryEntry } from "../services/media/audio-inventory.js";
20
- import {
21
- chooseSoundtrackMode,
22
- LINK_VERDICT,
23
- linkCouldCarry,
24
- linkRefusalReason,
25
- loadOf,
26
- PEAK_CLASS,
27
- SOUNDTRACK_MODE_CAUSE,
28
- soundtrackLoadOf
29
- } from "../services/encode/quality/link-budget.js";
30
- import { AUDIO_TRANSCODE_KBPS } from "../services/encode/args.js";
31
-
32
- // AudioSpecificConfig, ISO/IEC 14496-3 §1.6.2.1: objectType(5) frequencyIndex(4)
33
- // channelConfiguration(4) then, for AAC-LC, frameLengthFlag(1) dependsOnCoreCoder(1)
34
- // extensionFlag(1).
35
- const LC_48K_STEREO = Buffer.from([0x11, 0x90]);
36
- const LC_44K_STEREO = Buffer.from([0x12, 0x10]);
37
- const LC_48K_SIX = Buffer.from([0x11, 0xb0]);
38
- const LC_48K_STEREO_960 = Buffer.from([0x11, 0x94]);
39
- // objectType 5 (SBR) signalled explicitly: HE-AAC, which has no confirmed bound here.
40
- const HE_AAC = Buffer.from([0x2b, 0x92, 0x08, 0x00]);
41
-
42
- const aacParams = (bytes) => codecParametersOf({ codec: "aac", codecPrivateB64: bytes.toString("base64") });
43
-
44
- test("an AudioSpecificConfig gives its object type, frequency, channels and frame length", () => {
45
- assert.deepEqual(parseAudioSpecificConfig(LC_48K_STEREO), { objectType: 2, sampleRate: 48000, channels: 2, frameLength: 1024 });
46
- assert.deepEqual(parseAudioSpecificConfig(LC_44K_STEREO), { objectType: 2, sampleRate: 44100, channels: 2, frameLength: 1024 });
47
- assert.equal(parseAudioSpecificConfig(LC_48K_STEREO_960).frameLength, 960);
48
- assert.equal(parseAudioSpecificConfig(LC_48K_SIX).channels, 6);
49
- assert.equal(parseAudioSpecificConfig(HE_AAC).objectType, 5);
50
- assert.equal(parseAudioSpecificConfig(Buffer.from([0x11])), null, "too short to be a config");
51
- });
52
-
53
- test("AAC-LC with 1024-sample frames is bounded at 6144 bits per channel per frame", () => {
54
- assert.equal(peakKbpsOf(aacParams(LC_48K_STEREO)), 576);
55
- assert.equal(peakKbpsOf(aacParams(LC_44K_STEREO)), 529.2);
56
- assert.equal(peakKbpsOf(aacParams(LC_48K_SIX)), 1728, "every channel the config states");
57
- });
58
-
59
- test("an AAC configuration no source confirms a bound for has none", () => {
60
- assert.equal(peakKbpsOf(aacParams(LC_48K_STEREO_960)), null, "960-sample frames");
61
- assert.equal(peakKbpsOf(aacParams(HE_AAC)), null, "explicit HE-AAC");
62
- assert.equal(peakKbpsOf(codecParametersOf({ codec: "aac" })), null, "no config at all");
63
- });
64
-
65
- test("AC-3 is bounded by the largest syncframe of A/52 Table 5.18", () => {
66
- assert.equal(peakKbpsOf(codecParametersOf({ codec: "ac3", samplingFrequency: 48000 })), 640);
67
- assert.equal(peakKbpsOf(codecParametersOf({ codec: "ac3", samplingFrequency: 44100 })), 640.36875);
68
- assert.equal(peakKbpsOf(codecParametersOf({ codec: "ac3" })), 640.36875, "with no frequency, the largest of the three");
69
- });
70
-
71
- test("codecs with no confirmed bound give none", () => {
72
- for (const codec of ["opus", "mp3", "eac3", "dts", "flac", "vorbis"]) {
73
- assert.equal(peakKbpsOf(codecParametersOf({ codec, channels: 2, samplingFrequency: 48000 })), null, codec);
74
- }
75
- });
76
-
77
- test("a Matroska audio track takes its bound from its own CodecPrivate, not the container's channel count", () => {
78
- const track = new AudioTrack({
79
- trackNumber: 2,
80
- declaredIndex: 0,
81
- codecId: "A_AAC",
82
- codecPrivateB64: LC_48K_STEREO.toString("base64"),
83
- // What a Matroska Audio element may state for an HE-AAC stream's output,
84
- // or simply get wrong; the config is what a frame is.
85
- channels: 6,
86
- samplingFrequency: 96000
87
- });
88
- assert.equal(track.peakKbps, 576);
89
- assert.equal(track.codecParameters.channels, 2);
90
- });
91
-
92
- // ---- MP4: the AudioSpecificConfig inside `esds` ----
93
-
94
- function box(type, payload) {
95
- const header = Buffer.alloc(8);
96
- header.writeUInt32BE(payload.length + 8, 0);
97
- header.write(type, 4, "latin1");
98
- return Buffer.concat([header, payload]);
99
- }
100
-
101
- function fullBox(type, payload) {
102
- return box(type, Buffer.concat([Buffer.from([0, 0, 0, 0]), payload]));
103
- }
104
-
105
- function u32(...values) {
106
- const buffer = Buffer.alloc(values.length * 4);
107
- values.forEach((value, index) => buffer.writeUInt32BE(value, index * 4));
108
- return buffer;
109
- }
110
-
111
- function descriptor(tag, payload) {
112
- return Buffer.concat([Buffer.from([tag, payload.length]), payload]);
113
- }
114
-
115
- function mp4WithAac(config) {
116
- const decoderSpecificInfo = descriptor(0x05, config);
117
- const decoderConfig = descriptor(0x04, Buffer.concat([
118
- Buffer.from([0x40, 0x15, 0, 0, 0]), u32(0, 0), decoderSpecificInfo
119
- ]));
120
- const esDescriptor = descriptor(0x03, Buffer.concat([Buffer.from([0, 1, 0]), decoderConfig]));
121
- const esds = fullBox("esds", esDescriptor);
122
- const entry = Buffer.alloc(28);
123
- entry.writeUInt16BE(1, 6); // data_reference_index
124
- entry.writeUInt16BE(2, 16); // channelcount
125
- entry.writeUInt16BE(16, 18); // samplesize
126
- entry.writeUInt32BE(48000 * 65536, 24); // samplerate, 16.16
127
- const mp4a = box("mp4a", Buffer.concat([entry, esds]));
128
- const stsd = fullBox("stsd", Buffer.concat([u32(1), mp4a]));
129
- const trak = box("trak", Buffer.concat([
130
- fullBox("tkhd", Buffer.concat([u32(0, 0, 1, 0, 0), Buffer.alloc(60)])),
131
- box("mdia", Buffer.concat([
132
- fullBox("mdhd", Buffer.concat([u32(0, 0, 48000, 48000), Buffer.from([0x15, 0xc7, 0, 0])])),
133
- fullBox("hdlr", Buffer.concat([u32(0), Buffer.from("soun", "latin1"), u32(0, 0, 0)])),
134
- box("minf", box("stbl", stsd))
135
- ]))
136
- ]));
137
- // tkhd flag "enabled" so the track is offered.
138
- const tkhd = trak.indexOf("tkhd", 0, "latin1");
139
- trak[tkhd + 7] = 1;
140
- const ftyp = box("ftyp", Buffer.from("isom\0\0\0\0isom", "latin1"));
141
- return Buffer.concat([ftyp, box("moov", trak), box("mdat", Buffer.alloc(4))]);
142
- }
143
-
144
- test("an MP4 soundtrack's AudioSpecificConfig is read out of its esds", async () => {
145
- const file = mp4WithAac(LC_48K_STEREO);
146
- const container = new Mp4Container({
147
- readRange: async (start, end) => file.subarray(start, Math.min(end, file.length - 1) + 1),
148
- fileSize: file.length
149
- });
150
-
151
- const tracks = await container.readTracks();
152
- const audio = tracks.find((track) => track.type === "audio");
153
-
154
- assert.ok(audio, "the sound track is read");
155
- assert.equal(audio.codecId, "mp4a");
156
- assert.equal(audio.codecPrivateB64, LC_48K_STEREO.toString("base64"));
157
- assert.equal(audio.channels, 2);
158
- assert.equal(audio.samplingFrequency, 48000);
159
- assert.equal(audio.peakKbps, 576);
160
- });
161
-
162
- // ---- The inventory, and what a later reading adds ----
163
-
164
- test("the bound travels from the container through the merge into the inventory", () => {
165
- const declared = new AudioTrack({
166
- trackNumber: 2, declaredIndex: 0, codecId: "A_AAC", codecPrivateB64: LC_48K_STEREO.toString("base64"), language: "jpn"
167
- });
168
- const merged = Container.mergeAudioFlags([{ index: 0, language: "jpn", codec: "aac", bitrateKbps: 128 }], [declared]);
169
- const [entry] = buildAudioInventory({ embedded: merged.tracks, videoFileIndex: 0, sidecars: [] });
170
-
171
- assert.equal(entry.peakKbps, 576);
172
- assert.equal(entry.bitrateKbps, 128, "the stated average stays beside it");
173
- });
174
-
175
- const entryWith = (codecParameters, extra = {}) => ({
176
- index: 1,
177
- fileIndex: 7,
178
- sourceTrackIndex: 0,
179
- kind: "sidecar",
180
- language: "rus",
181
- codecParameters,
182
- peakKbps: peakKbpsOf(codecParameters),
183
- ...extra
184
- });
185
-
186
- test("a later reading that says less keeps the bound already established", () => {
187
- const known = entryWith(aacParams(LC_48K_STEREO));
188
- const partial = entryWith({ codec: "aac", objectType: null, frameLength: null, channels: null, sampleRate: null }, { language: "" });
189
-
190
- const merged = mergeInventoryEntry(known, partial);
191
-
192
- assert.equal(merged.peakKbps, 576);
193
- assert.equal(merged.language, "rus", "an empty field of the new reading does not erase the old value");
194
- });
195
-
196
- test("a later reading that contradicts the known configuration withdraws the bound", () => {
197
- const known = entryWith(aacParams(LC_48K_STEREO));
198
- const contradicting = entryWith({ codec: "aac", objectType: null, frameLength: null, channels: null, sampleRate: 44100 });
199
-
200
- const merged = mergeInventoryEntry(known, contradicting);
201
-
202
- assert.equal(merged.codecParameters, null);
203
- assert.equal(merged.peakKbps, null);
204
- });
205
-
206
- test("a complete reading that contradicts the known one establishes neither, and the next complete one stands", () => {
207
- const known = entryWith(aacParams(LC_48K_STEREO));
208
- const other = entryWith(aacParams(LC_44K_STEREO));
209
-
210
- const contradicted = mergeInventoryEntry(known, other);
211
- assert.equal(contradicted.peakKbps, null, "two readings that disagree establish neither");
212
-
213
- const settled = mergeInventoryEntry(contradicted, other);
214
- assert.equal(settled.peakKbps, 529.2);
215
- });
216
-
217
- test("a complete reading that agrees replaces the configuration whole, and never moves the track", () => {
218
- const known = entryWith({ codec: "aac", objectType: 2, frameLength: null, channels: 2, sampleRate: 48000 });
219
- const complete = { ...entryWith(aacParams(LC_48K_STEREO)), index: 99, fileIndex: 99, sourceTrackIndex: 99, kind: "embedded" };
220
-
221
- const merged = mergeInventoryEntry(known, complete);
222
-
223
- assert.equal(merged.peakKbps, 576);
224
- assert.deepEqual(
225
- [merged.index, merged.fileIndex, merged.sourceTrackIndex, merged.kind],
226
- [1, 7, 0, "sidecar"],
227
- "the address a player uses stays"
228
- );
229
- });
230
-
231
- // ---- What the link is asked, and how a track is produced ----
232
-
233
- test("a copied soundtrack weighs its codec's bound, else its stated rate, else nothing", () => {
234
- assert.deepEqual(soundtrackLoadOf({ peakKbps: 576, bitrateKbps: 128 }, false), { mbps: 0.576, peakClass: PEAK_CLASS.KNOWN });
235
- assert.deepEqual(soundtrackLoadOf({ peakKbps: null, bitrateKbps: 128 }, false), { mbps: 0.128, peakClass: PEAK_CLASS.ESTIMATED });
236
- assert.deepEqual(soundtrackLoadOf({ peakKbps: null, bitrateKbps: null }, false), { mbps: null, peakClass: PEAK_CLASS.UNKNOWN });
237
- assert.deepEqual(soundtrackLoadOf(null, false), { mbps: null, peakClass: PEAK_CLASS.UNKNOWN });
238
- });
239
-
240
- test("a re-encoded soundtrack weighs the rate it is asked for, as an estimate — ffmpeg's -b:a is a target", () => {
241
- assert.deepEqual(
242
- soundtrackLoadOf({ peakKbps: 576, bitrateKbps: 640 }, true),
243
- { mbps: AUDIO_TRANSCODE_KBPS / 1000, peakClass: PEAK_CLASS.ESTIMATED }
244
- );
245
- assert.equal(soundtrackLoadOf({ peakKbps: 576 }, null), null, "no sound, no part");
246
- });
247
-
248
- test("the field case: a dub with no stated rate on an 80 Mbit/s link is now re-encoded and fits", () => {
249
- // 2026-10-01, Drifters 06: picture 3.186 Mbit/s copied, the `Rus Sound` .mka
250
- // stating nothing. Copied, nothing can be compared; re-encoded, it can.
251
- const dub = { peakKbps: null, bitrateKbps: null };
252
- const picture = { mbps: 3.186, peakClass: PEAK_CLASS.ESTIMATED };
253
-
254
- const copied = linkCouldCarry(80.57, loadOf(picture, soundtrackLoadOf(dub, false)));
255
- assert.equal(copied.verdict, LINK_VERDICT.NO_SAFE_BOUND);
256
-
257
- const mode = chooseSoundtrackMode({ entry: dub, browserPlays: true, transcodeAllowed: true });
258
- assert.equal(mode.transcode, true);
259
- assert.equal(mode.cause, SOUNDTRACK_MODE_CAUSE.NO_FIGURE);
260
- const sent = linkCouldCarry(80.57, loadOf(picture, soundtrackLoadOf(dub, mode.transcode)));
261
- assert.equal(sent.admitted, true);
262
- assert.equal(sent.verdict, LINK_VERDICT.ESTIMATED_TO_FIT);
263
- });
264
-
265
- test("a track's mode follows from what the browser plays, whether it has a figure, and whether re-encoding is allowed", () => {
266
- const stated = { bitrateKbps: 128 };
267
- const bounded = { peakKbps: 576 };
268
- const silent = {};
269
- const mode = (entry, browserPlays, transcodeAllowed = true) =>
270
- chooseSoundtrackMode({ entry, browserPlays, transcodeAllowed });
271
-
272
- assert.deepEqual(mode(stated, true), { transcode: false, cause: SOUNDTRACK_MODE_CAUSE.COPY });
273
- assert.deepEqual(mode(bounded, true), { transcode: false, cause: SOUNDTRACK_MODE_CAUSE.COPY });
274
- assert.deepEqual(mode(silent, true), { transcode: true, cause: SOUNDTRACK_MODE_CAUSE.NO_FIGURE });
275
- assert.deepEqual(mode(stated, false), { transcode: true, cause: SOUNDTRACK_MODE_CAUSE.UNPLAYABLE });
276
- assert.deepEqual(mode(silent, true, false), { transcode: false, cause: SOUNDTRACK_MODE_CAUSE.COPY_WITHOUT_FIGURE });
277
- assert.deepEqual(mode(stated, false, false), { transcode: null, cause: SOUNDTRACK_MODE_CAUSE.CANNOT_SERVE });
278
- });
279
-
280
- test("a refusal with no figure names the part that has none", () => {
281
- const picture = { mbps: null, peakClass: PEAK_CLASS.UNKNOWN };
282
- const sound = { mbps: null, peakClass: PEAK_CLASS.UNKNOWN };
283
- const known = { mbps: 1, peakClass: PEAK_CLASS.KNOWN };
284
-
285
- assert.match(linkRefusalReason(linkCouldCarry(50, loadOf(known, sound)), "too big"), /how much the soundtrack would send/);
286
- assert.match(linkRefusalReason(linkCouldCarry(50, loadOf(picture, known)), "too big"), /how much the picture would send/);
287
- assert.match(linkRefusalReason(linkCouldCarry(50, loadOf(picture, sound)), "too big"), /the picture or the soundtrack/);
288
- assert.equal(linkRefusalReason(linkCouldCarry(0.5, loadOf(known, known)), "too big"), "too big");
289
- });
@@ -1,133 +0,0 @@
1
- /**
2
- * @file The file as a handle: its key, its name, what a probe said about it,
3
- * and the address this proxy serves it at.
4
- *
5
- * The reason this object exists is the key. Twenty places in the session
6
- * manager assembled `${sourceKey}:${fileIndex}` by hand, through five different
7
- * spellings, to reach caches that are keyed by a file. These checks pin the
8
- * shape those call sites now depend on, and the one property the registry has
9
- * to have: the same file asked for twice is the same object, or a fact learned
10
- * through one reference would be invisible through the other.
11
- */
12
-
13
- import test from "node:test";
14
- import assert from "node:assert/strict";
15
- import { SourceFile, SourceFiles, sourceDecodeCharacteristics } from "../services/media/SourceFile.js";
16
-
17
- test("a file's key is the pair that identifies it, spelled in one place", () => {
18
- const file = new SourceFile({ sourceKey: "abc123", fileIndex: 4 });
19
- assert.equal(file.key, "abc123:4");
20
- assert.equal(SourceFiles.keyFor("abc123", 4), file.key);
21
- });
22
-
23
- test("a file with no name is named by its index, so no log line is about a nameless thing", () => {
24
- assert.equal(new SourceFile({ sourceKey: "s", fileIndex: 7 }).name, "file#7");
25
- assert.equal(new SourceFile({ sourceKey: "s", fileIndex: 7, name: " Film.mkv " }).name, "Film.mkv");
26
- });
27
-
28
- test("facts are absent until a probe answers, and 'absent' is not 'zero'", () => {
29
- const file = new SourceFile({ sourceKey: "s", fileIndex: 0 });
30
- assert.equal(file.width, null, "an unread frame size is unknown, not 0x0");
31
- assert.equal(file.height, null);
32
- assert.equal(file.fps, null);
33
- assert.equal(file.durationSeconds, null);
34
- assert.equal(file.hasDuration, false);
35
- assert.equal(file.isHdr, false, "a file nobody has read is not claimed to be HDR");
36
- assert.equal(file.startTime, 0, "no stated start time means the timeline begins at zero");
37
- assert.equal(file.decode, null, "and nothing is priced from facts that were never read");
38
- });
39
-
40
- test("readings are merged, because two readers answer about different things", () => {
41
- const file = new SourceFile({ sourceKey: "s", fileIndex: 0 });
42
- // ffmpeg's banner: the frame size and rate, and no start time.
43
- file.learn({ width: 1920, height: 1080, fps: 24 });
44
- // The container's own header, read for one field — a sidecar soundtrack is
45
- // read for exactly this and nothing else.
46
- file.learn({ startTime: 0.083 });
47
- assert.equal(file.startTime, 0.083);
48
- assert.equal(file.width, 1920, "the second reader must not erase what the first found");
49
- });
50
-
51
- test("a fresher reading replaces the first, because a cold torrent answers incompletely", () => {
52
- const file = new SourceFile({ sourceKey: "s", fileIndex: 0 });
53
- file.learn({ width: 1920, height: 1080, fps: 24, durationSeconds: 0, startTime: 1.5 });
54
- assert.equal(file.width, 1920);
55
- assert.equal(file.hasDuration, false, "a duration of zero is no duration");
56
- assert.equal(file.startTime, 1.5);
57
-
58
- file.learn({ width: 1920, height: 1080, fps: 24, durationSeconds: 5400, startTime: 1.5 });
59
- assert.equal(file.durationSeconds, 5400);
60
- assert.equal(file.hasDuration, true);
61
-
62
- file.learn(null);
63
- assert.equal(file.durationSeconds, 5400, "nothing is unlearned by an answer that did not come");
64
- });
65
-
66
- test("what decoding costs is derived from the file's own facts, per codec family", () => {
67
- const file = new SourceFile({ sourceKey: "s", fileIndex: 0 });
68
- file.learn({ width: 1920, height: 1080, fps: 24, bitrateKbps: 8000, codec: "hevc", bitDepth: 10 });
69
- assert.deepEqual(file.decode, {
70
- megapixelsPerSecond: (1920 * 1080 * 24) / 1e6,
71
- megabitsPerSecond: 8,
72
- codec: "hevc",
73
- bitDepth: 10
74
- });
75
- // The same arithmetic, still callable on a plain reading — the offer prices a
76
- // file it has not made a session for.
77
- assert.deepEqual(sourceDecodeCharacteristics({ width: 1920, height: 1080, fps: 24, bitrateKbps: 8000 }), {
78
- megapixelsPerSecond: (1920 * 1080 * 24) / 1e6,
79
- megabitsPerSecond: 8,
80
- codec: "",
81
- bitDepth: null
82
- });
83
- });
84
-
85
- test("the address carries the file and the session, and the proxy's own base is a parameter", () => {
86
- const file = new SourceFile({ sourceKey: "abc", fileIndex: 2 });
87
- const url = file.streamUrl("http://127.0.0.1:9090", { sessionId: "sess-1" });
88
- assert.equal(url.pathname, "/stream");
89
- assert.equal(url.searchParams.get("sourceKey"), "abc");
90
- assert.equal(url.searchParams.get("fileIndex"), "2");
91
- assert.equal(url.searchParams.get("session"), "sess-1");
92
-
93
- const noSession = file.streamUrl("http://127.0.0.1:9090");
94
- assert.equal(noSession.searchParams.has("session"), false, "a read nobody is accounting for says so");
95
- });
96
-
97
- test("the same file asked for twice is the same object, or a fact learned once is lost", () => {
98
- const files = new SourceFiles();
99
- const first = files.get("abc", 1, "Film.mkv");
100
- first.learn({ width: 1280, height: 720, fps: 25, durationSeconds: 60 });
101
-
102
- const second = files.get("abc", 1);
103
- assert.equal(second, first, "two viewers of one file must not hold two copies of its facts");
104
- assert.equal(second.width, 1280);
105
- assert.equal(second.name, "Film.mkv");
106
-
107
- const other = files.get("abc", 2);
108
- assert.notEqual(other, first, "and a different file in the same torrent is a different file");
109
- });
110
-
111
- test("a name learned later is kept, since the first caller often has only an index", () => {
112
- const files = new SourceFiles();
113
- const bare = files.get("abc", 1);
114
- assert.equal(bare.name, "file#1");
115
- files.get("abc", 1, "Film.mkv");
116
- assert.equal(bare.name, "Film.mkv");
117
- files.get("abc", 1, "Something.else.mkv");
118
- assert.equal(bare.name, "Film.mkv", "and it is not renamed underneath a log line already written");
119
- });
120
-
121
- test("files nobody names are dropped, and a file still named survives the sweep", () => {
122
- const files = new SourceFiles();
123
- const kept = files.get("abc", 1);
124
- files.get("abc", 2);
125
- files.get("def", 9);
126
- assert.equal(files.size, 3);
127
-
128
- const dropped = files.forgetUnused(new Set([kept]));
129
- assert.equal(dropped, 2);
130
- assert.equal(files.size, 1);
131
- assert.equal(files.peek(kept.key), kept);
132
- assert.equal(files.peek("abc:2"), null);
133
- });
@@ -1,113 +0,0 @@
1
- /**
2
- * @file WHAT THE BROWSER IS TOLD IS IN A TORRENT.
3
- *
4
- * This is the one place the product answers it. The browser used to answer it
5
- * as well — a list of video extensions in its parser and a second, shorter pair
6
- * inside its picker — and the three had already diverged: measured 2026-09-12,
7
- * `.dat` was offered there as video and not counted here, which also decides
8
- * whether a sidecar whose name matches nothing can belong to the only video
9
- * present.
10
- *
11
- * So the shape of this answer is a contract with the page, and the page shows
12
- * what it is given without looking at a name.
13
- */
14
-
15
- import test from "node:test";
16
- import assert from "node:assert/strict";
17
- import { handleApiSourceFilesGet } from "../routes/api/sources/files/get.js";
18
-
19
- /**
20
- * A request, a reply and the torrent behind them.
21
- *
22
- * @param {Array<{ path: string, length: number }>} files
23
- * @returns {{ req: object, reply: object, sent: { body: object | null, code: number }, deps: object }}
24
- */
25
- function harness(files) {
26
- const sent = { body: null, code: 200 };
27
- const reply = {
28
- code(value) {
29
- sent.code = value;
30
- return reply;
31
- },
32
- send(body) {
33
- sent.body = body;
34
- return reply;
35
- }
36
- };
37
- const deps = {
38
- sourceRegistry: { get: () => ({ sourceType: "magnet", source: "magnet:?xt=urn:btih:abc" }) },
39
- torrentPool: {
40
- async getTorrent() {
41
- return { name: "Drifters", infoHash: "abc", files };
42
- }
43
- }
44
- };
45
- return { req: { params: { sourceKey: "key" }, query: {} }, reply, sent, deps };
46
- }
47
-
48
- test("the files come back in reading order, each saying what it is", () => {
49
- const { req, reply, sent, deps } = harness([
50
- { path: "Drifters/ep 10.mkv", length: 9 },
51
- { path: "Drifters/ep 2.mkv", length: 9 },
52
- { path: "Drifters/Rus Sound/ep 2.mka", length: 2 },
53
- { path: "Drifters/notes.nfo", length: 1 }
54
- ]);
55
-
56
- return handleApiSourceFilesGet(req, reply, deps).then(() => {
57
- assert.equal(sent.code, 200);
58
- assert.deepEqual(
59
- sent.body.files.map((file) => [file.relativePath, file.kind]),
60
- [
61
- // Runs of digits compare as numbers, so 2 comes before 10; the torrent's
62
- // own order is whatever the tool that made it chose.
63
- ["ep 2.mkv", "video"],
64
- ["ep 10.mkv", "video"],
65
- ["notes.nfo", "other"],
66
- ["Rus Sound/ep 2.mka", "audio"]
67
- ]
68
- );
69
- // Relative to the torrent root: WebTorrent prefixes its own name to every
70
- // path, and stripping it here means one rule in the product rather than two
71
- // that can disagree.
72
- assert.ok(sent.body.files.every((file) => !file.relativePath.startsWith("Drifters/")));
73
- });
74
- });
75
-
76
- test("each picture is named with what belongs to it, by index", () => {
77
- const { req, reply, sent, deps } = harness([
78
- { path: "Drifters/ep 1.mkv", length: 9 },
79
- { path: "Drifters/Rus Sound/ep 1.mka", length: 2 },
80
- { path: "Drifters/Sub/ep 1.ass", length: 1 },
81
- { path: "Drifters/ep 2.mkv", length: 9 }
82
- ]);
83
-
84
- return handleApiSourceFilesGet(req, reply, deps).then(() => {
85
- const episode = (number) => ({
86
- season: null,
87
- episodes: [number],
88
- part: null,
89
- special: false,
90
- showHint: "",
91
- titleHint: ""
92
- });
93
- assert.deepEqual(sent.body.items, [
94
- { fileIndex: 0, audio: [1], subtitles: [2], images: [], episode: episode(1) },
95
- { fileIndex: 3, audio: [], subtitles: [], images: [], episode: episode(2) }
96
- ]);
97
- // Two numbered pictures of one show: the release is a series.
98
- assert.equal(sent.body.shape, "series");
99
- // The files themselves are in the list above; saying them twice is how two
100
- // copies of one fact start.
101
- assert.ok(sent.body.items.every((item) => item.audio.every((one) => Number.isInteger(one))));
102
- });
103
- });
104
-
105
- test("a torrent whose metadata has not arrived says so and lists nothing", () => {
106
- const { req, reply, sent, deps } = harness([]);
107
-
108
- return handleApiSourceFilesGet(req, reply, deps).then(() => {
109
- assert.deepEqual(sent.body.files, []);
110
- assert.deepEqual(sent.body.items, []);
111
- assert.equal(sent.body.name, "Drifters");
112
- });
113
- });
@@ -1,18 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import test from "node:test";
3
- import { createSourceRegistry } from "../store/source-registry.js";
4
-
5
- const infoHash = "0123456789abcdef0123456789abcdef01234567";
6
-
7
- test("magnet links for one torrent share a source key", async () => {
8
- const registry = createSourceRegistry();
9
- const first = `magnet:?xt=urn:btih:${infoHash}&dn=First+name&tr=udp%3A%2F%2Ftracker.one%3A80`;
10
- const second = `magnet:?xt=urn:btih:${infoHash}&dn=Different+name&tr=udp%3A%2F%2Ftracker.two%3A80`;
11
-
12
- const firstKey = await registry.upsert("magnet", first);
13
- const secondKey = await registry.upsert("magnet", second);
14
-
15
- assert.equal(firstKey, `torrent:${infoHash}`);
16
- assert.equal(secondKey, firstKey);
17
- assert.equal(registry.get(firstKey)?.source, second);
18
- });