@torrent-tv/proxy 2.89.17 → 2.89.19

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 (300) hide show
  1. package/README.md +14 -3
  2. package/package.json +10 -2
  3. package/server.js +0 -11
  4. package/utils/client-log-file.js +147 -42
  5. package/.claude/commands/opsx/apply.md +0 -155
  6. package/.claude/commands/opsx/archive.md +0 -160
  7. package/.claude/commands/opsx/explore.md +0 -174
  8. package/.claude/commands/opsx/propose.md +0 -109
  9. package/.claude/commands/opsx/sync.md +0 -143
  10. package/.claude/skills/openspec-apply-change/SKILL.md +0 -159
  11. package/.claude/skills/openspec-archive-change/SKILL.md +0 -117
  12. package/.claude/skills/openspec-explore/SKILL.md +0 -289
  13. package/.claude/skills/openspec-propose/SKILL.md +0 -113
  14. package/.claude/skills/openspec-sync-specs/SKILL.md +0 -147
  15. package/.dockerignore +0 -5
  16. package/AGENTS.md +0 -363
  17. package/CHANGELOG.md +0 -2377
  18. package/Dockerfile +0 -30
  19. package/biome.json +0 -412
  20. package/corpus.tmp.json +0 -1
  21. package/docs/browser-proxy-contract.md +0 -155
  22. package/docs/container-architecture.md +0 -273
  23. package/docs/download-architecture.md +0 -261
  24. package/docs/encode-architecture.md +0 -765
  25. package/docs/encode-run-state.md +0 -96
  26. package/docs/logs.md +0 -151
  27. package/docs/playback-start-forecast.md +0 -113
  28. package/docs/socks5-egress-research.md +0 -114
  29. package/docs/storage-architecture.md +0 -273
  30. package/knip.json +0 -14
  31. package/main2.cpuprofile +0 -1
  32. package/openspec/changes/adaptive-bitrate/.openspec.yaml +0 -0
  33. package/openspec/changes/adaptive-bitrate/design.md +0 -92
  34. package/openspec/changes/adaptive-bitrate/proposal.md +0 -56
  35. package/openspec/changes/adaptive-bitrate/specs/adaptive-bitrate/spec.md +0 -51
  36. package/openspec/changes/adaptive-bitrate/tasks.md +0 -40
  37. package/openspec/changes/binary-distribution/.openspec.yaml +0 -2
  38. package/openspec/changes/binary-distribution/proposal.md +0 -53
  39. package/openspec/changes/chunked-request-bodies/design.md +0 -81
  40. package/openspec/changes/chunked-request-bodies/proposal.md +0 -53
  41. package/openspec/changes/chunked-request-bodies/specs/chunked-request-bodies/spec.md +0 -30
  42. package/openspec/changes/chunked-request-bodies/tasks.md +0 -40
  43. package/openspec/changes/cold-start/design.md +0 -98
  44. package/openspec/changes/cold-start/proposal.md +0 -62
  45. package/openspec/changes/cold-start/specs/cold-start/spec.md +0 -47
  46. package/openspec/changes/cold-start/tasks.md +0 -48
  47. package/openspec/changes/disk-cap/.openspec.yaml +0 -2
  48. package/openspec/changes/disk-cap/proposal.md +0 -37
  49. package/openspec/changes/disk-cap/specs/disk-cap/spec.md +0 -25
  50. package/openspec/changes/disk-cap/tasks.md +0 -19
  51. package/openspec/changes/proxy-observability/.openspec.yaml +0 -2
  52. package/openspec/changes/proxy-observability/design.md +0 -38
  53. package/openspec/changes/proxy-observability/proposal.md +0 -49
  54. package/openspec/changes/proxy-observability/specs/observability/spec.md +0 -33
  55. package/openspec/changes/proxy-observability/tasks.md +0 -21
  56. package/openspec/changes/seek-debounce/.openspec.yaml +0 -0
  57. package/openspec/changes/seek-debounce/design.md +0 -118
  58. package/openspec/changes/seek-debounce/proposal.md +0 -59
  59. package/openspec/changes/seek-debounce/specs/seek-debounce/spec.md +0 -41
  60. package/openspec/changes/seek-debounce/tasks.md +0 -33
  61. package/openspec/changes/subtitle-language/.openspec.yaml +0 -2
  62. package/openspec/changes/subtitle-language/proposal.md +0 -50
  63. package/openspec/changes/subtitle-language/specs/subtitle-language/spec.md +0 -34
  64. package/openspec/changes/subtitle-language/tasks.md +0 -26
  65. package/openspec/changes/track-selection/.openspec.yaml +0 -2
  66. package/openspec/changes/track-selection/design.md +0 -47
  67. package/openspec/changes/track-selection/proposal.md +0 -45
  68. package/openspec/changes/track-selection/specs/track-selection/spec.md +0 -41
  69. package/openspec/changes/track-selection/tasks.md +0 -20
  70. package/openspec/changes/transcode-quality/.openspec.yaml +0 -2
  71. package/openspec/changes/transcode-quality/proposal.md +0 -51
  72. package/openspec/changes/transcode-quality/specs/transcode-quality/spec.md +0 -139
  73. package/openspec/changes/transcode-quality/tasks.md +0 -63
  74. package/openspec/config.yaml +0 -38
  75. package/public/.well-known/appspecific/com.chrome.devtools.json +0 -1
  76. package/research/delivery-wedge-stand-ladder-2026-09-13.md +0 -329
  77. package/research/double-spawn-2026-09-10.md +0 -171
  78. package/research/handover-2026-09-13.md +0 -209
  79. package/research/handover-reader-claims-removal-2026-09-12.md +0 -166
  80. package/research/piece-withdrawn-but-still-claimed-2026-09-12.md +0 -175
  81. package/research/preset-benchmark-size-scaling-2026-08-22.md +0 -55
  82. package/research/priority-map-is-the-truth-2026-09-12.md +0 -225
  83. package/research/valgrind-replica-2026-08-27/logs.tar.gz +0 -0
  84. package/scripts/build-audio-samples.mjs +0 -31
  85. package/scripts/render-run-graph.js +0 -173
  86. package/test/asset-paths.test.js +0 -28
  87. package/test/audio-buffer-continuity.test.js +0 -77
  88. package/test/audio-calibration.test.js +0 -23
  89. package/test/audio-inventory.test.js +0 -212
  90. package/test/audio-track-bitrate.test.js +0 -40
  91. package/test/audio-warm-recorded.test.js +0 -63
  92. package/test/auto-quality-step.test.js +0 -762
  93. package/test/available-share.test.js +0 -76
  94. package/test/background-fill.test.js +0 -190
  95. package/test/client-log-file.test.js +0 -126
  96. package/test/client-logs-route.test.js +0 -34
  97. package/test/coalesce.test.js +0 -39
  98. package/test/completed-files.test.js +0 -115
  99. package/test/concurrent-cost.test.js +0 -148
  100. package/test/container-media-info.test.js +0 -228
  101. package/test/contention.test.js +0 -76
  102. package/test/core-dumps.test.js +0 -57
  103. package/test/coverage-follows-the-disk.test.js +0 -233
  104. package/test/coverage-map.test.js +0 -195
  105. package/test/cue-push-carries-what-the-walk-found.test.js +0 -58
  106. package/test/cut-times-timeline.test.js +0 -65
  107. package/test/cuts-follow-published-grid.test.js +0 -109
  108. package/test/data-channel-frame.test.js +0 -63
  109. package/test/data-channel-viewer-lifecycle.test.js +0 -193
  110. package/test/dead-torrent-handle.test.js +0 -52
  111. package/test/declared-rates.test.js +0 -116
  112. package/test/declared-tracks.test.js +0 -43
  113. package/test/decode-cost-fit.test.js +0 -202
  114. package/test/decode-cost.test.js +0 -475
  115. package/test/delivery-probe-field.test.js +0 -173
  116. package/test/delivery-probe.test.js +0 -382
  117. package/test/demand-register.test.js +0 -206
  118. package/test/dht-bootstrap.test.js +0 -72
  119. package/test/drain-threat.test.js +0 -65
  120. package/test/duplicate-add.test.js +0 -48
  121. package/test/encode-admission.test.js +0 -170
  122. package/test/encode-cost.test.js +0 -275
  123. package/test/encode-exit.test.js +0 -111
  124. package/test/encode-orchestrator.test.js +0 -426
  125. package/test/encode-plan-viewers.test.js +0 -725
  126. package/test/encode-plan.test.js +0 -545
  127. package/test/encode-run-state.test.js +0 -314
  128. package/test/encode-run.test.js +0 -248
  129. package/test/encode-slope.test.js +0 -46
  130. package/test/encoded-output.test.js +0 -39
  131. package/test/encoder-kinds.test.js +0 -122
  132. package/test/encoder-readings.test.js +0 -68
  133. package/test/episode-naming.test.js +0 -131
  134. package/test/every-claimant-is-told-its-share.test.js +0 -152
  135. package/test/failed-encoder-still-serves-what-exists.test.js +0 -354
  136. package/test/failed-start-breaker.test.js +0 -202
  137. package/test/fastest-wires.test.js +0 -143
  138. package/test/ffmpeg-stream-counts.test.js +0 -101
  139. package/test/file-edges.test.js +0 -191
  140. package/test/files-in-use.test.js +0 -34
  141. package/test/fixtures/audio-frame-boundary.json +0 -1
  142. package/test/fixtures/refused-audio-20261002.json +0 -10
  143. package/test/fmp4-presentation-clock.test.js +0 -148
  144. package/test/fmp4-self-contained.test.js +0 -80
  145. package/test/fmp4-timestamps.test.js +0 -140
  146. package/test/fmp4-track-completeness.test.js +0 -41
  147. package/test/format-keyed-outputs.test.js +0 -78
  148. package/test/health-metrics.test.js +0 -36
  149. package/test/held-read-ownership.test.js +0 -77
  150. package/test/held-request-width.test.js +0 -157
  151. package/test/helpers/encode-run.js +0 -248
  152. package/test/helpers/held-file.js +0 -77
  153. package/test/helpers/manager.js +0 -62
  154. package/test/helpers/matroska-file.js +0 -296
  155. package/test/helpers/output-spec.js +0 -63
  156. package/test/host-load.test.js +0 -68
  157. package/test/index-accuracy-is-about-the-file.test.js +0 -128
  158. package/test/init-compat.test.js +0 -219
  159. package/test/input-lost-quiets-the-plan.test.js +0 -261
  160. package/test/input-unavailable.test.js +0 -48
  161. package/test/installation-id.test.js +0 -39
  162. package/test/keeping-period.test.js +0 -83
  163. package/test/keyframe-index-accuracy.test.js +0 -188
  164. package/test/keyframe-table-budget.test.js +0 -123
  165. package/test/keyframes-belong-to-the-file.test.js +0 -249
  166. package/test/learned-median.test.js +0 -37
  167. package/test/letting-go-asks-who-is-watching.test.js +0 -191
  168. package/test/limit-row-by-area.test.js +0 -60
  169. package/test/link-budget.test.js +0 -111
  170. package/test/link-probe.test.js +0 -56
  171. package/test/local-encoding-data.test.js +0 -273
  172. package/test/logger-repeats.test.js +0 -134
  173. package/test/look-ahead-coverage.test.js +0 -56
  174. package/test/manager-surface.test.js +0 -291
  175. package/test/matroska-blocks.test.js +0 -0
  176. package/test/matroska-cluster-discovery.test.js +0 -323
  177. package/test/matroska-cues-track.test.js +0 -192
  178. package/test/media-time.test.js +0 -62
  179. package/test/memory-budget.test.js +0 -390
  180. package/test/move-cost.test.js +0 -216
  181. package/test/mp4-composition-times.test.js +0 -0
  182. package/test/mp4-subtitles.test.js +0 -173
  183. package/test/one-authority.test.js +0 -285
  184. package/test/one-budget.test.js +0 -149
  185. package/test/one-output-one-session.test.js +0 -273
  186. package/test/one-resource-divided.test.js +0 -185
  187. package/test/one-serving-output-per-height.test.js +0 -458
  188. package/test/orchestrator-wired.test.js +0 -207
  189. package/test/output-catalog.test.js +0 -39
  190. package/test/output-shape.test.js +0 -21
  191. package/test/output-spec.test.js +0 -251
  192. package/test/output-speed.test.js +0 -92
  193. package/test/outputs-being-prepared.test.js +0 -40
  194. package/test/packet-witness-ring.test.js +0 -236
  195. package/test/packet-witness.test.js +0 -148
  196. package/test/piece-completeness.test.js +0 -78
  197. package/test/piece-disk-store.test.js +0 -401
  198. package/test/piece-from-whole-file.test.js +0 -129
  199. package/test/piece-lru.test.js +0 -319
  200. package/test/piece-reader.test.js +0 -162
  201. package/test/piece-selection-offset.test.js +0 -85
  202. package/test/piece-store-eviction.test.js +0 -595
  203. package/test/piece-store-never-refuses.test.js +0 -153
  204. package/test/piece-store-reservations.test.js +0 -403
  205. package/test/piece-store-slow-disk.test.js +0 -159
  206. package/test/piece-tail.test.js +0 -97
  207. package/test/plan-host-timings.test.js +0 -68
  208. package/test/plan-is-steady.test.js +0 -225
  209. package/test/playback-readiness.test.js +0 -518
  210. package/test/playback-start-regressions.test.js +0 -101
  211. package/test/priority-map-download.test.js +0 -174
  212. package/test/priority-map-emptied.test.js +0 -204
  213. package/test/priority-map-per-output.test.js +0 -215
  214. package/test/priority-map.test.js +0 -163
  215. package/test/probe-wedge-certainty.test.js +0 -60
  216. package/test/produced-copy-choice.test.js +0 -213
  217. package/test/published-timeline.test.js +0 -83
  218. package/test/quality-offer.test.js +0 -160
  219. package/test/quality-variants.test.js +0 -1274
  220. package/test/rate-control-in-the-output.test.js +0 -181
  221. package/test/read-survives-withdrawal.test.js +0 -164
  222. package/test/read-window.test.js +0 -365
  223. package/test/reads-not-yet-arrived.test.js +0 -218
  224. package/test/resume-warm.test.js +0 -39
  225. package/test/run-graph-drift.test.js +0 -37
  226. package/test/run-inputs.test.js +0 -80
  227. package/test/run-intervals.test.js +0 -100
  228. package/test/run-position-follows-published-grid.test.js +0 -89
  229. package/test/run-progress.test.js +0 -64
  230. package/test/same-height-switch.test.js +0 -663
  231. package/test/seek-frees-the-viewers-requests.test.js +0 -217
  232. package/test/seek-landing.test.js +0 -109
  233. package/test/seek-target-not-superseded.test.js +0 -195
  234. package/test/segment-closed-on-both-branches.test.js +0 -114
  235. package/test/segment-cut-times.test.js +0 -63
  236. package/test/segment-demand.test.js +0 -80
  237. package/test/segment-hold-supersede.test.js +0 -91
  238. package/test/segment-judged-by-its-own-header.test.js +0 -222
  239. package/test/segment-serve-wiring.test.js +0 -481
  240. package/test/segment-store-eviction.test.js +0 -247
  241. package/test/segment-store.test.js +0 -352
  242. package/test/segment-true-start.test.js +0 -86
  243. package/test/segments-are-shared.test.js +0 -176
  244. package/test/send-chunk-bytes.test.js +0 -86
  245. package/test/serving-existing-output.test.js +0 -111
  246. package/test/serving-output.test.js +0 -102
  247. package/test/shared-piece-store.test.js +0 -310
  248. package/test/sidecar-naming.test.js +0 -142
  249. package/test/soundtrack-rate-bound.test.js +0 -289
  250. package/test/source-file.test.js +0 -133
  251. package/test/source-files-route.test.js +0 -113
  252. package/test/source-registry.test.js +0 -18
  253. package/test/split-a-stretch.test.js +0 -139
  254. package/test/stale-request-after-seek.test.js +0 -158
  255. package/test/startup-readings.test.js +0 -124
  256. package/test/stream-route.test.js +0 -237
  257. package/test/subtitle-cue-framing.test.js +0 -200
  258. package/test/subtitle-cue-source.test.js +0 -102
  259. package/test/subtitle-cue-walk.test.js +0 -385
  260. package/test/subtitle-cursor.test.js +0 -63
  261. package/test/subtitle-defaults.test.js +0 -118
  262. package/test/subtitle-language.test.js +0 -252
  263. package/test/subtitle-metadata.test.js +0 -15
  264. package/test/subtitle-plan-waits.test.js +0 -125
  265. package/test/subtitle-track-numbering.test.js +0 -391
  266. package/test/supply-margin.test.js +0 -190
  267. package/test/swarm-follows-readers.test.js +0 -174
  268. package/test/swarm-reach.test.js +0 -130
  269. package/test/swarm-selection.test.js +0 -220
  270. package/test/tail-duplication.test.js +0 -167
  271. package/test/the-channel-has-both-ends.test.js +0 -91
  272. package/test/the-torrent-thread-serves-bytes.test.js +0 -107
  273. package/test/thread-pool.test.js +0 -53
  274. package/test/timeline.test.js +0 -84
  275. package/test/torrent-contents.test.js +0 -235
  276. package/test/torrent-cost.test.js +0 -71
  277. package/test/torrent-files.test.js +0 -221
  278. package/test/torrent-naming.test.js +0 -255
  279. package/test/tracks-begin-together.test.js +0 -209
  280. package/test/tunnel-renewal.test.js +0 -167
  281. package/test/two-viewers-one-picture.test.js +0 -508
  282. package/test/upload-hurry.test.js +0 -186
  283. package/test/usrsctp-state.test.js +0 -131
  284. package/test/video-facts.test.js +0 -102
  285. package/test/viewer-assignments.test.js +0 -143
  286. package/test/viewer-outputs.test.js +0 -285
  287. package/test/viewer-presence.test.js +0 -116
  288. package/test/viewer-requests-admission.test.js +0 -92
  289. package/test/viewer-starts-on-map.test.js +0 -27
  290. package/test/viewer-states.test.js +0 -192
  291. package/test/viewer-subtitle-subscription.test.js +0 -96
  292. package/test/viewer.test.js +0 -139
  293. package/test/viewing-generations.test.js +0 -390
  294. package/test/visible-rung.test.js +0 -37
  295. package/test/wait-ledger.test.js +0 -100
  296. package/test/webrtc-shim.test.js +0 -149
  297. package/test/wedge-certainty.test.js +0 -131
  298. package/test/whole-file-held-read.test.js +0 -30
  299. package/test/withdraw-piece-claim.test.js +0 -212
  300. package/test/worker-channel.test.js +0 -158
@@ -1,157 +0,0 @@
1
- /**
2
- * @file How far ahead of the viewer a held segment request may sit.
3
- *
4
- * A request is released when the viewer has moved away from it. "Moved away"
5
- * used to mean more than `MAX_LOOKAHEAD_SEGMENTS` — eight segments, which
6
- * happened to match a browser holding thirty seconds and would refuse three
7
- * quarters of the requests of one holding the whole cushion. The width is the
8
- * encoder's own look-ahead now, which is the same figure the browser sizes its
9
- * buffer from. Roadmap item 4, step 2.
10
- */
11
-
12
- import test from "node:test";
13
- import assert from "node:assert/strict";
14
- import { Timeline } from "../services/encode/output/Timeline.js";
15
- import { mkdtemp, rm } from "node:fs/promises";
16
- import os from "node:os";
17
- import path from "node:path";
18
- import { wireOutputs } from "../services/server/wire-outputs.js";
19
- import { fmp4Format } from "../services/encode/segment-formats/fmp4.js";
20
-
21
- const SESSION_ID = "aaaaaaaabbbbcccc";
22
- const SEGMENT_SECONDS = 4;
23
-
24
- /**
25
- * @returns {Promise<{ manager: object, dirPath: string }>}
26
- */
27
- async function managerWithSession() {
28
- const dirPath = await mkdtemp(path.join(os.tmpdir(), "held-request-"));
29
- const manager = wireOutputs({
30
- enabled: true,
31
- ffmpegBin: "ffmpeg",
32
- localBindHost: "127.0.0.1",
33
- localPort: 9090
34
- });
35
- const session = {
36
- id: SESSION_ID,
37
- dirPath,
38
- // Where this file is cut, held by the file. A fixture that stated it
39
- // on the session was describing what production no longer does.
40
- timeline: new Timeline({
41
- boundaries: Array.from({ length: 201 }, (_, index) => index * SEGMENT_SECONDS),
42
- cutGrid: "uniform"
43
- }),
44
- state: "ready",
45
- segmentFormat: fmp4Format,
46
- segmentCount: 200,
47
- useSyntheticPlaylist: true
48
- };
49
- manager.outputs.set(SESSION_ID, session);
50
- // The viewer is at segment #25, and says so themselves.
51
- manager.viewers.of(session, "viewer").moveTo(25 * SEGMENT_SECONDS);
52
- manager.viewers.of(session, "viewer").playing = false;
53
- return { manager, dirPath };
54
- }
55
-
56
- /**
57
- * @param {number} index
58
- * @returns {string}
59
- */
60
- function segment(index) {
61
- return `segment-${String(index).padStart(5, "0")}.mp4`;
62
- }
63
-
64
- test("the width is the encoder's own look-ahead, in segments", async (t) => {
65
- const { manager, dirPath } = await managerWithSession();
66
- t.after(async () => {
67
- await rm(dirPath, { recursive: true, force: true });
68
- });
69
-
70
- const width = Math.ceil(manager.lookaheadSeconds / manager.segmentDurationSec);
71
- assert.equal(width, 30, "120 s of look-ahead over 4 s segments");
72
- assert.equal(manager.serving.requestStillWanted(SESSION_ID, segment(25 + width)), true, "the far edge");
73
- assert.equal(
74
- manager.serving.requestStillWanted(SESSION_ID, segment(25 + width + 1)),
75
- false,
76
- "past everything the encoder is allowed to have produced"
77
- );
78
- });
79
-
80
- test("a request the old eight-segment width would have refused is kept", async (t) => {
81
- const { manager, dirPath } = await managerWithSession();
82
- t.after(async () => {
83
- await rm(dirPath, { recursive: true, force: true });
84
- });
85
-
86
- // #34 is nine segments ahead of the viewer — inside a 120 s cushion and
87
- // outside the eight the width used to be. This is the request a browser
88
- // holding the whole cushion makes constantly.
89
- assert.equal(manager.serving.requestStillWanted(SESSION_ID, segment(34)), true);
90
- });
91
-
92
- test("a segment behind the viewer is still released", async (t) => {
93
- const { manager, dirPath } = await managerWithSession();
94
- t.after(async () => {
95
- await rm(dirPath, { recursive: true, force: true });
96
- });
97
-
98
- assert.equal(manager.serving.requestStillWanted(SESSION_ID, segment(24)), false);
99
- });
100
-
101
- test("a seek by one viewer does not release the request held for another", async (t) => {
102
- const { manager, dirPath } = await managerWithSession();
103
- t.after(async () => {
104
- await rm(dirPath, { recursive: true, force: true });
105
- });
106
- // Both are watching the same copied picture, so both are this one session.
107
- // One is at segment #25, the other far ahead at #150 — inside the fixture's
108
- // own 200-segment timeline, since a position past the end of the grid is
109
- // clamped to it and would prove nothing about the width.
110
- manager.viewerRequests.requestSeek(SESSION_ID, 100, "behind");
111
- manager.viewerRequests.requestSeek(SESSION_ID, 600, "ahead");
112
-
113
- // A SEEK DOES ONE THING: it puts the viewer where they now are. It used to
114
- // write that position into five places, this session's shared field among
115
- // them, and every held request was then judged against whoever moved last —
116
- // which is exactly what this test exists to refuse. Each viewer's own
117
- // position is what decides, and the two below are asked separately.
118
- assert.equal(manager.viewers.get("behind").positionSeconds(), 100);
119
- assert.equal(manager.viewers.get("ahead").positionSeconds(), 600);
120
-
121
- assert.equal(
122
- manager.serving.requestStillWanted(SESSION_ID, segment(26), "behind"),
123
- true,
124
- "the segment the viewer behind is waiting for is still theirs to wait for"
125
- );
126
- assert.equal(
127
- manager.serving.requestStillWanted(SESSION_ID, segment(26), "ahead"),
128
- false,
129
- "and for the one in front it is a place they have left"
130
- );
131
- });
132
-
133
- test("a viewer's own head moves with their seek", async (t) => {
134
- const { manager, dirPath } = await managerWithSession();
135
- t.after(async () => {
136
- await rm(dirPath, { recursive: true, force: true });
137
- });
138
-
139
- // Their last request was at #25; they jump to 600 s, which is #150. The
140
- // segment at the target must be wanted — refusing it there is the freeze of
141
- // 2026-08-18.
142
- manager.viewerRequests.requestSeek(SESSION_ID, 600, "viewer");
143
- assert.equal(manager.serving.requestStillWanted(SESSION_ID, segment(150), "viewer"), true);
144
- assert.equal(manager.serving.requestStillWanted(SESSION_ID, segment(25), "viewer"), false);
145
- });
146
-
147
- test("a viewer nobody can name is judged against the one shared position", async (t) => {
148
- const { manager, dirPath } = await managerWithSession();
149
- t.after(async () => {
150
- await rm(dirPath, { recursive: true, force: true });
151
- });
152
-
153
- // A plain HTTP transport builds its own URLs and carries no id. The old
154
- // behaviour is what remains, which for a single viewer is the same thing.
155
- assert.equal(manager.serving.requestStillWanted(SESSION_ID, segment(26), ""), true);
156
- assert.equal(manager.serving.requestStillWanted(SESSION_ID, segment(24), ""), false);
157
- });
@@ -1,248 +0,0 @@
1
- /**
2
- * @file A real `EncodeRun` over a process that is not real.
3
- *
4
- * Tests used to set a session's process fields by hand — `session.ffmpeg`,
5
- * `session.runState`, `session.encodeStartIndex` — which is how a test can pass
6
- * over code that no longer works: the fields were the thing under test as much
7
- * as the behaviour was. A run is an object now, so a test builds the object the
8
- * product builds and injects only the one thing a test may not have, which is a
9
- * child process.
10
- *
11
- * The fake has a pid, as a started process does: a run signals only a process
12
- * that started. The number is never used to signal anything — every signal goes
13
- * through the child object, and this one records it.
14
- */
15
-
16
- import { EncodeRun } from "../../services/encode/EncodeRun.js";
17
-
18
- /** The working time between two closed pieces of a test measurement. */
19
- const MEASURED_STEP_MS = 1000;
20
-
21
- /** Closed pieces in a test measurement: the last two give the reading. */
22
- const MEASURED_STEPS = 3;
23
-
24
- /**
25
- * The clock each run built by `startRunOn` reads: standing while a measurement
26
- * is said, the wall clock otherwise. @type {WeakMap<EncodeRun, { at: number | null }>}
27
- */
28
- const wallClocks = new WeakMap();
29
-
30
- /**
31
- * Make a run measure a speed the way it does in the product: progress reports
32
- * and the names of closed pieces on the encoder's own channels, one second of
33
- * the run's own work apart. A closed piece is published when the next name
34
- * arrives, and each publication is one reading against the one before, so the
35
- * last of three steps reads exactly `speedX` whatever the run measured before.
36
- *
37
- * @param {EncodeRun} run - Its process exposes `stdout` and `stdio[3]`.
38
- * @param {number} speedX - Film seconds per second of the run's work.
39
- * @param {{ at: number } | null} [clock] - What the run's `now` reads; advanced
40
- * here. Absent for a run built by `startRunOn` without one: its clock is
41
- * stood just behind the wall clock for the measurement and released after,
42
- * so the reading is dated now.
43
- * @param {number} [waitedMs] - How long the run's input waits for the swarm in
44
- * each step, on top of its second of work.
45
- * @returns {void}
46
- */
47
- export function measureSpeed(run, speedX, clock = null, waitedMs = 0) {
48
- if (clock === null) {
49
- const wall = wallClocks.get(run);
50
- if (!wall) {
51
- throw new TypeError("measureSpeed needs the clock the run reads");
52
- }
53
- // Never behind the run's start or its own last reading: a step back in
54
- // time is no work.
55
- wall.at = Math.max(
56
- Date.now() - (MEASURED_STEP_MS + waitedMs) * MEASURED_STEPS,
57
- run.startedAt,
58
- run.speedReading?.at ?? Number.NEGATIVE_INFINITY
59
- );
60
- measureSpeed(run, speedX, wall, waitedMs);
61
- wall.at = null;
62
- return;
63
- }
64
- const { stdout, stdio } = run.process;
65
- let produced = run.progress.processedSeconds - run.progress.startPositionSeconds;
66
- for (let step = 0; step < MEASURED_STEPS; step += 1) {
67
- if (waitedMs > 0) {
68
- run.inputWaitBegins();
69
- clock.at += waitedMs;
70
- run.inputWaitEnds();
71
- }
72
- clock.at += MEASURED_STEP_MS;
73
- produced += (speedX * MEASURED_STEP_MS) / 1000;
74
- say(stdout, `out_time_ms=${Math.round(produced * 1_000_000)}\n`);
75
- say(stdio[3], `measured-${step}\n`);
76
- }
77
- }
78
-
79
- /**
80
- * @param {{ write?: (text: string) => void, emit?: (event: string, text: string) => void }} channel
81
- * @param {string} text
82
- */
83
- function say(channel, text) {
84
- if (typeof channel.write === "function") {
85
- channel.write(text);
86
- } else {
87
- channel.emit("data", text);
88
- }
89
- }
90
-
91
- /**
92
- * A child process that records what was done to it.
93
- *
94
- * @param {object} [options]
95
- * @param {number | null} [options.pid] - Null for a process that did not start.
96
- * @returns {object}
97
- */
98
- export function fakeProcess({ pid = 4242, exitsWhenKilled = true } = {}) {
99
- /** @type {Map<string, (...args: unknown[]) => void>} */
100
- const listeners = new Map();
101
- const stdout = channel();
102
- const closedPieces = channel();
103
- return {
104
- pid,
105
- killed: false,
106
- exitCode: null,
107
- signalCode: null,
108
- /** Every signal it was sent, in order. */
109
- signals: [],
110
- stdout,
111
- stderr: { on() {} },
112
- // The descriptor the encoder names its closed pieces on (`-segment_list pipe:3`).
113
- stdio: [null, stdout, null, closedPieces],
114
- on(event, handler) {
115
- const kept = listeners.get(event) ?? [];
116
- kept.push(handler);
117
- listeners.set(event, kept);
118
- return this;
119
- },
120
- once(event, handler) {
121
- return this.on(event, handler);
122
- },
123
- kill(signal = "SIGTERM") {
124
- this.signals.push(signal);
125
- // Suspending and continuing are not ends.
126
- if (signal === "SIGSTOP" || signal === "SIGCONT") {
127
- return true;
128
- }
129
- this.killed = true;
130
- // A real process answers a signal by exiting, and it does so on a later
131
- // turn. A fake that never exits makes every disposal wait out the grace
132
- // period, which is two seconds a test spends proving nothing.
133
- if (exitsWhenKilled && this.exitCode === null && this.signalCode === null) {
134
- queueMicrotask(() => this.exit(null, signal));
135
- }
136
- return true;
137
- },
138
- /**
139
- * Report an exit, as the real thing would.
140
- *
141
- * @param {number | null} code
142
- * @param {string | null} [signal]
143
- */
144
- exit(code, signal = null) {
145
- this.exitCode = code;
146
- this.signalCode = signal;
147
- for (const handler of listeners.get("exit") ?? []) {
148
- handler(code, signal);
149
- }
150
- // A real child process emits `close` after `exit`, once its stdio has
151
- // closed, and the run takes its ending from `close`.
152
- for (const handler of listeners.get("close") ?? []) {
153
- handler(code, signal);
154
- }
155
- }
156
- };
157
- }
158
-
159
- /**
160
- * A readable side of a pipe that a test writes into.
161
- *
162
- * @returns {{ on: (event: string, handler: (chunk: string) => void) => void, write: (text: string) => void }}
163
- */
164
- function channel() {
165
- /** @type {((chunk: string) => void)[]} */
166
- const handlers = [];
167
- return {
168
- on(event, handler) {
169
- if (event === "data") handlers.push(handler);
170
- },
171
- write(text) {
172
- for (const handler of handlers) handler(text);
173
- }
174
- };
175
- }
176
-
177
- /** A logger that says nothing, for tests that are not about the log. */
178
- export const silentLogger = { info() {}, warn() {}, error() {} };
179
-
180
- /**
181
- * Add a run to a session, started, over a fake process.
182
- *
183
- * A session holds a SET of runs — as many as the machine affords — so a test
184
- * that wants two heads on one output calls this twice.
185
- *
186
- * @param {object} session - The session under test.
187
- * @param {object} [options]
188
- * @param {number} [options.from] - First segment number it is making.
189
- * @param {number} [options.to] - Last, inclusive; below `from` means no end.
190
- * @param {object} [options.process] - The process it should own.
191
- * @param {boolean} [options.producing] - Whether it has already made its first
192
- * segment, which is what moves it out of starting.
193
- * @param {number | null} [options.lastSegmentIndex] - The film's last number,
194
- * for telling a finished file from an input that dried up.
195
- * @param {boolean} [options.usesExplicitCuts]
196
- * @returns {import("../../services/encode/EncodeRun.js").EncodeRun}
197
- */
198
- export function startRunOn(session, options = {}) {
199
- const {
200
- from = 0,
201
- to = -1,
202
- process: child = fakeProcess(),
203
- producing = true,
204
- lastSegmentIndex = null,
205
- usesExplicitCuts = false,
206
- // THE SPEED IT HAS MEASURED: film made over its own working time between
207
- // two closed pieces. Left at zero, the plan has no speed to compute an
208
- // arrival from, every arrangement of encoders is equally hopeless, they all
209
- // tie, and the encoder is taken away for changing nothing.
210
- speedX = 0,
211
- // What the run reads as now, for a test that drives time itself. Absent,
212
- // the run reads the wall clock, which stands still only while a
213
- // measurement is said.
214
- clock: given = null
215
- } = options;
216
- const clock = given ?? { at: null };
217
- const run = new EncodeRun({
218
- address: session.outputKey ?? session.id ?? "output",
219
- encoder: { name: "libx264", kind: "software" },
220
- from,
221
- to,
222
- buildArgs: () => [],
223
- spawn: () => child,
224
- logger: silentLogger,
225
- lastSegmentIndex: () => lastSegmentIndex,
226
- usesExplicitCuts,
227
- now: () => clock.at ?? Date.now(),
228
- because: "a test asked for it"
229
- });
230
- if (given === null) {
231
- wallClocks.set(run, clock);
232
- }
233
- if (speedX > 0) {
234
- measureSpeed(run, speedX, given);
235
- }
236
- if (producing) {
237
- // What moves a run out of starting is its first segment, in the product as
238
- // here — so a run that is producing has made one, and its head stands one
239
- // past where it began. `from` is still where it started, which is what a
240
- // test asserting a run's position asks for.
241
- run.noteProduced(from);
242
- }
243
- if (!(session.runs instanceof Set)) {
244
- session.runs = new Set();
245
- }
246
- session.runs.add(run);
247
- return run;
248
- }
@@ -1,77 +0,0 @@
1
- /**
2
- * @file A torrent reduced to what the subtitle walk may know about one file.
3
- *
4
- * Built from the REAL two functions the torrent thread answers with
5
- * (`torrent/worker/held-bytes.js`), so a check that walks cues exercises both
6
- * sides of the seam rather than a stand-in for one of them: the bitfield
7
- * becoming a list of ranges, and a range read that never fetches.
8
- *
9
- * The file's container is the one a `ContainerOrchestrator` builds, over a
10
- * reader that FETCHES — the fake file's own stream, which serves any range —
11
- * because the head and the Cues table are read that way in the product.
12
- */
13
-
14
- import { heldRangesOf, readHeldBytes } from "../../services/torrent/worker/held-bytes.js";
15
- import { ContainerOrchestrator } from "../../services/media/ContainerOrchestrator.js";
16
-
17
- /**
18
- * Every byte of a range from the fake file's stream.
19
- *
20
- * @param {object} file
21
- * @param {number} start
22
- * @param {number} end
23
- * @returns {Promise<Buffer>}
24
- */
25
- async function readThroughStream(file, start, end) {
26
- const chunks = [];
27
- for await (const chunk of file.createReadStream({ start, end })) {
28
- chunks.push(Buffer.from(chunk));
29
- }
30
- return Buffer.concat(chunks);
31
- }
32
-
33
- /** One orchestrator for the checks that do not bring their own. */
34
- const sharedContainers = new ContainerOrchestrator();
35
-
36
- /**
37
- * @param {object} torrent - The fake a check builds: `pieceLength`, `bitfield`,
38
- * `files[]` with `offset`, `length`, `name`, `createReadStream`.
39
- * @param {number} fileIndex
40
- * @param {string} sourceKey
41
- * @param {{ containers?: ContainerOrchestrator, readRange?: (start: number, end: number) => Promise<Buffer | null>, wantedSeconds?: () => number[], portionBytes?: number }} [options]
42
- * @returns {import("../../services/media/SubtitleCues.js").HeldFile}
43
- */
44
- export function heldFileOver(torrent, fileIndex, sourceKey, options = {}) {
45
- const file = torrent?.files?.[fileIndex] ?? {};
46
- const containers = options.containers ?? sharedContainers;
47
- const readRange = options.readRange ?? ((start, end) => readThroughStream(file, start, end));
48
- return {
49
- sourceKey,
50
- fileIndex,
51
- name: String(file.name ?? ""),
52
- length: Number(file.length) || 0,
53
- portionBytes: options.portionBytes,
54
- container: () =>
55
- containers.containerFor({
56
- sourceKey,
57
- fileIndex,
58
- readRange,
59
- fileSize: Number(file.length) || 0,
60
- label: String(file.name ?? ""),
61
- portionBytes: options.portionBytes
62
- }),
63
- heldRanges: async () => heldRangesOf(torrent, fileIndex),
64
- readHeld: (start, end) => readHeldBytes(torrent, fileIndex, start, end),
65
- wantedSeconds: options.wantedSeconds
66
- };
67
- }
68
-
69
- /**
70
- * Forget what the shared orchestrator built for a source.
71
- *
72
- * @param {string} sourceKey
73
- * @returns {void}
74
- */
75
- export function forgetContainers(sourceKey) {
76
- sharedContainers.forget(sourceKey);
77
- }
@@ -1,62 +0,0 @@
1
- /**
2
- * @file A session manager for a test, with a store of its own.
3
- *
4
- * ONE MANAGER, ONE ROOT. In production that is true by construction: one
5
- * process holds one manager, and the store's root is a fixed path so that a
6
- * restart can find what the process before it finished (`SegmentStore.sweep`).
7
- * Under `node --test` it is false — the runner starts one PROCESS PER FILE and
8
- * runs them in parallel, so every manager built without a store of its own
9
- * shared the one default root with every other file running at that moment.
10
- *
11
- * What that cost, measured 2026-09-14: `disposeAll` removes the store's whole
12
- * root, and four files call it between them 40 times. Running the twenty-one
13
- * files that build a manager gave 2, 3 and 4 failures on consecutive runs and
14
- * zero with `--test-concurrency=1`; the victim was always
15
- * `segment-serve-wiring.test.js`, reporting a segment missing that it had
16
- * written itself a moment earlier, because another process had deleted the
17
- * directory under it. `npm test` is `node --test`, so this was the project's
18
- * own test command, and it is why a red result had become something to read
19
- * past rather than to act on.
20
- *
21
- * A test that fails by luck is worse than one that does not exist: it costs a
22
- * run every time, and it trains everyone to ignore red.
23
- */
24
-
25
- import { mkdtempSync, rmSync } from "node:fs";
26
- import os from "node:os";
27
- import path from "node:path";
28
- import { wireOutputs } from "../../services/server/wire-outputs.js";
29
- import { SegmentStore } from "../../services/storage/segment-store/SegmentStore.js";
30
-
31
- /**
32
- * A manager whose produced segments live where nothing else can reach them.
33
- *
34
- * @param {object} [options] - Passed to the manager, over the defaults below.
35
- * A `segmentStore` given here wins, for a test that wants to watch the store
36
- * itself.
37
- * @returns {{ manager: object, store: SegmentStore, root: string,
38
- * cleanup: () => void }} `cleanup` removes the root; call it from `t.after`.
39
- */
40
- export function managerWithOwnStore(options = {}) {
41
- const root = mkdtempSync(path.join(os.tmpdir(), "ttv-store-"));
42
- const store = options.segmentStore ?? new SegmentStore({ root });
43
- const manager = wireOutputs({
44
- enabled: true,
45
- ffmpegBin: "ffmpeg",
46
- localBindHost: "127.0.0.1",
47
- localPort: 9090,
48
- // What this host has measured about itself is kept beside the store, not
49
- // beside the installed proxy: read from there, one run's figures became
50
- // the next run's premises, and a check passed only on a machine that had
51
- // run the suite before.
52
- stateDir: root,
53
- ...options,
54
- segmentStore: store
55
- });
56
- return {
57
- manager,
58
- store,
59
- root,
60
- cleanup: () => rmSync(root, { recursive: true, force: true })
61
- };
62
- }