@torrent-tv/proxy 2.89.16 → 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/routes/api/client-logs/post.js +9 -2
  4. package/server.js +0 -11
  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 -2373
  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 -138
  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/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,354 +0,0 @@
1
- /**
2
- * @file A failed encoder explains what is MISSING, and takes nothing away.
3
- *
4
- * Whether production failed used to be asked at the door, before the request
5
- * had even been looked at, so an output whose encoding ended in an error
6
- * answered 500 to every request — including requests for segments it had
7
- * already finished and which were lying on disk under their served names. The
8
- * viewer lost what was made as well as what was not, and a seek back into the
9
- * finished stretch could not be served either.
10
- *
11
- * A piece under its served name is whole by construction, whoever wrote it and
12
- * whatever has become of the encoder since. The failure belongs where a file is
13
- * absent: there it is the truthful answer, and holding the request instead only
14
- * spends the viewer's patience.
15
- */
16
-
17
- import test from "node:test";
18
- import assert from "node:assert/strict";
19
- import { rm, writeFile } from "node:fs/promises";
20
- import path from "node:path";
21
- import { wireOutputs } from "../services/server/wire-outputs.js";
22
- import { SourceFile } from "../services/media/SourceFile.js";
23
- import { Timeline } from "../services/encode/output/Timeline.js";
24
- import { Output } from "../services/encode/output/Output.js";
25
- import { KeyframeTable } from "../services/media/container/KeyframeTable.js";
26
- import { ENCODE_RUN_STATE } from "../services/encode/encode-run-state.js";
27
- import { fmp4Format } from "../services/encode/segment-formats/fmp4.js";
28
- import { EncodeRun } from "../services/encode/EncodeRun.js";
29
- import { fakeProcess, silentLogger } from "./helpers/encode-run.js";
30
- import { waitForSessionFile } from "../services/server/transcode-session-files.js";
31
- import { outputSpec } from "./helpers/output-spec.js";
32
-
33
- const SESSION_ID = "3333333344445555";
34
- const OUTPUT_KEY = "failed-serves:fmt=fmp4:grid=uniform:video-only:v=0/copy";
35
- // The other branch: a picture cut at the source's own keyframes, where the
36
- // muxer is handed the cut times and writes each piece self-contained. It has no
37
- // init FILE at all — the header is lifted out of the first piece — which is the
38
- // case `#initFromFirstSegment` answers `null` for instead of throwing `ENOENT`.
39
- const KEYFRAME_OUTPUT_KEY = "failed-serves-kf:fmt=fmp4:grid=kf@0:video-only:v=0/copy";
40
-
41
- /**
42
- * One output with segment #0 finished on disk and its encoder dead.
43
- *
44
- * @returns {Promise<{ manager: object, dirPath: string, outputKey: string }>}
45
- */
46
- async function outputWhoseEncoderDied({ failed = true, segments = [0], cutsAtGivenTimes = false } = {}) {
47
- const manager = wireOutputs({
48
- enabled: true,
49
- ffmpegBin: "ffmpeg",
50
- localBindHost: "127.0.0.1",
51
- localPort: 9090
52
- });
53
- manager.encodeRuns.planEncodersNow = () => {};
54
- manager.encodeRuns.planEncodersSoon = () => {};
55
- const outputKey = cutsAtGivenTimes ? KEYFRAME_OUTPUT_KEY : OUTPUT_KEY;
56
- manager.segmentStore.useFormat(outputKey, fmp4Format);
57
- const dirPath = manager.segmentStore.directoryFor(outputKey);
58
- // On the even grid — the branch that writes finished files outright, so their
59
- // existence is the proof and the fixture needs no header. The other branch
60
- // writes no init file at all.
61
- for (const index of segments) {
62
- await writeFile(path.join(dirPath, fmp4Format.segmentFileName(index)), Buffer.alloc(256, 0x5a));
63
- }
64
-
65
- const session = {
66
- id: SESSION_ID,
67
- spec: cutsAtGivenTimes
68
- ? outputSpec({ transcodeVideo: false, cutGrid: "keyframe" })
69
- : outputSpec({ transcodeVideo: true, cutGrid: "uniform" }),
70
- outputKey,
71
- dirPath,
72
- timeline: new Timeline({
73
- boundaries: [0, 12.5, 25, 37.5, 50, 62.5],
74
- cutGrid: cutsAtGivenTimes ? "keyframe" : "uniform"
75
- }),
76
- state: "ready",
77
- file: new SourceFile({ sourceKey: "source-1", fileIndex: 0, name: "video.mkv" }).learn({ durationSeconds: 62.5 }),
78
- get inputFile() { return this.file; },
79
- get audioFile() { return this.file; },
80
- lastAloneSpeed: 2,
81
- startedAt: Date.now(),
82
- lastAccessedAt: Date.now(),
83
- runs: new Set(),
84
- lastError: "",
85
- consumers: new Set(),
86
- viewers: new Map(),
87
- segmentCount: 5,
88
- segmentFormat: fmp4Format,
89
- useSyntheticPlaylist: true,
90
- playlistText: "#EXTM3U\n",
91
- output: new Output({ encodeWidth: 0, encodeHeight: 0, outputFps: 25, softwarePreset: null, applyTonemap: false }),
92
- keyframes: new KeyframeTable().learn({ times: [0, 12.5, 25, 37.5, 50], format: "matroska" }),
93
- audioOnly: false,
94
- waitEpoch: 0
95
- };
96
- manager.outputs.set(SESSION_ID, session);
97
- manager.viewers.of(session, "viewer-a").moveTo(0);
98
- // The encoder ended for good, which is what every request below is asked
99
- // against.
100
- if (failed) {
101
- manager.encodeOrchestrator.noteEnded({
102
- address: outputKey,
103
- run: { state: ENCODE_RUN_STATE.ENDED_FAILED },
104
- lastError: "ffmpeg exited with code 255"
105
- });
106
- assert.equal(manager.encodeRuns.hasFailed(session), true, "the fixture must describe a failed output");
107
- }
108
- return { manager, dirPath, outputKey };
109
- }
110
-
111
- /**
112
- * Wait until the store really holds a wait for this output.
113
- *
114
- * A check that ends a wait it has not yet registered proves nothing: it passes
115
- * on the code that answers a request AFTER the event just as well as on the code
116
- * that answers one already standing. The deadline is a backstop, never the
117
- * measurement.
118
- *
119
- * @param {object} store
120
- * @param {string} key
121
- * @param {number} [howMany]
122
- * @returns {Promise<void>}
123
- */
124
- async function untilWaitsAreRegistered(store, key, howMany = 1) {
125
- const giveUpAt = Date.now() + 5_000;
126
- while (store.waitingFor(key) < howMany) {
127
- if (Date.now() > giveUpAt) {
128
- throw new Error(`no wait registered on ${key} after 5s — the check would prove nothing`);
129
- }
130
- await new Promise((resolve) => setImmediate(resolve));
131
- }
132
- }
133
-
134
- test("a segment that is on disk is served although the encoder failed", async (t) => {
135
- const { manager, dirPath } = await outputWhoseEncoderDied();
136
- t.after(async () => {
137
- await manager.lifecycle.disposeAll();
138
- await rm(dirPath, { recursive: true, force: true });
139
- });
140
-
141
- const answer = await manager.serving.getFileStream(SESSION_ID, "segment-00000.mp4", { consumerId: "viewer-a" });
142
-
143
- assert.equal(answer.kind, "file", `what is made is made — the answer was ${answer.kind}`);
144
- assert.equal(answer.contentType, fmp4Format.segmentContentType);
145
- });
146
-
147
- test("a segment that is not there says the encoder failed, instead of being held", async (t) => {
148
- const { manager, dirPath } = await outputWhoseEncoderDied();
149
- t.after(async () => {
150
- await manager.lifecycle.disposeAll();
151
- await rm(dirPath, { recursive: true, force: true });
152
- });
153
-
154
- const answer = await manager.serving.getFileStream(SESSION_ID, "segment-00003.mp4", { consumerId: "viewer-a" });
155
-
156
- assert.equal(answer.kind, "failed", "nothing is going to write it, and the viewer is owed the reason");
157
- assert.match(answer.message, /255/);
158
- });
159
-
160
- test("the playlist is a fact of the timeline and outlives the encoder", async (t) => {
161
- const { manager, dirPath } = await outputWhoseEncoderDied();
162
- t.after(async () => {
163
- await manager.lifecycle.disposeAll();
164
- await rm(dirPath, { recursive: true, force: true });
165
- });
166
-
167
- const answer = await manager.serving.getFileStream(SESSION_ID, "index.m3u8", { consumerId: "viewer-a" });
168
-
169
- assert.equal(answer.kind, "file");
170
- assert.equal(answer.isPlaylist, true);
171
- });
172
-
173
- test("a failure reaches a request that was ALREADY waiting", async (t) => {
174
- // The case the other checks in this file do not reach: they set the failure
175
- // and then ask. A wait that began BEFORE it is ended by the publication, by a
176
- // wake, by the requester going or by its own deadline — and a failure is none
177
- // of those, so it sat out the whole deadline for a piece nothing was going to
178
- // make, and where the page states no deadline, until the viewer disconnected.
179
- const { manager, dirPath } = await outputWhoseEncoderDied({ failed: false });
180
- t.after(async () => {
181
- await manager.lifecycle.disposeAll();
182
- await rm(dirPath, { recursive: true, force: true });
183
- });
184
- const session = manager.outputs.get(SESSION_ID);
185
- const child = fakeProcess();
186
- const run = new EncodeRun({
187
- address: OUTPUT_KEY,
188
- encoder: { name: "libx264", kind: "software" },
189
- from: 3,
190
- to: 4,
191
- buildArgs: () => [],
192
- spawn: () => child,
193
- logger: silentLogger,
194
- lastSegmentIndex: () => 4,
195
- because: "a check asked for it",
196
- onEnded: (ended) => manager.encodeRuns.noteRunEnded(session, run, ended)
197
- });
198
- manager.encodeOrchestrator.adopt(OUTPUT_KEY, run);
199
-
200
- // Held with a deadline long enough that running it out IS the failure.
201
- const held = waitForSessionFile(manager.serving, SESSION_ID, "segment-00003.mp4", {
202
- holdMs: 30_000,
203
- consumerId: "viewer-a"
204
- });
205
- // The wait must EXIST before it can be said to have been woken.
206
- await untilWaitsAreRegistered(manager.segmentStore, OUTPUT_KEY);
207
-
208
- child.exit(255);
209
-
210
- // Bounded, because a regression here would otherwise HANG for the whole
211
- // thirty-second hold and take the rest of the file down with it.
212
- const answer = await settledWithin(held, 5_000);
213
-
214
- assert.notEqual(answer, STILL_WAITING, "it was left to sit out its whole hold");
215
- assert.equal(answer.kind, "failed", "a wait nothing will satisfy must be told");
216
- });
217
-
218
- /** What a race answers when the promise did not settle in time. */
219
- const STILL_WAITING = Symbol("still waiting");
220
-
221
- /**
222
- * The promise's answer, or {@link STILL_WAITING} if it has not settled yet.
223
- *
224
- * Used for BOTH directions, and the direction decides what the bound means. On
225
- * "it must be told", the bound is a backstop that turns a regression into a
226
- * failure in seconds instead of a test that hangs until the hold runs out — and
227
- * a hanging test is worse than an absent one, because it takes the rest of the
228
- * file down with it (measured here: three checks cancelled by one hang). On
229
- * "it must go on waiting", the bound IS the grace, and it cannot fail red — it
230
- * can only pass too easily.
231
- *
232
- * @param {Promise<unknown>} promise
233
- * @param {number} withinMs
234
- * @returns {Promise<unknown>}
235
- */
236
- async function settledWithin(promise, withinMs) {
237
- return await Promise.race([
238
- promise,
239
- new Promise((resolve) => setTimeout(() => resolve(STILL_WAITING), withinMs))
240
- ]);
241
- }
242
-
243
- test("a run that exits 0 having produced less than promised also reaches a waiting request", async (t) => {
244
- // ffmpeg exits 0 both at the end of the file and when its input stops
245
- // delivering, so a run that stopped short claims success. That ending is
246
- // terminal all the same — and it was the one branch of the exit handler that
247
- // returned before anybody waiting was told.
248
- const { manager, dirPath } = await outputWhoseEncoderDied({ failed: false });
249
- t.after(async () => {
250
- await manager.lifecycle.disposeAll();
251
- await rm(dirPath, { recursive: true, force: true });
252
- });
253
- const session = manager.outputs.get(SESSION_ID);
254
- const child = fakeProcess();
255
- const run = new EncodeRun({
256
- address: OUTPUT_KEY,
257
- encoder: { name: "libx264", kind: "software" },
258
- from: 3,
259
- to: 4,
260
- buildArgs: () => [],
261
- spawn: () => child,
262
- logger: silentLogger,
263
- lastSegmentIndex: () => 4,
264
- because: "a check asked for it",
265
- onEnded: (ended) => manager.encodeRuns.noteRunEnded(session, run, ended)
266
- });
267
- manager.encodeOrchestrator.adopt(OUTPUT_KEY, run);
268
-
269
- const held = waitForSessionFile(manager.serving, SESSION_ID, "segment-00003.mp4", {
270
- holdMs: 30_000,
271
- consumerId: "viewer-a"
272
- });
273
- await untilWaitsAreRegistered(manager.segmentStore, OUTPUT_KEY);
274
-
275
- // Zero, having made none of the two segments it was given.
276
- child.exit(0);
277
-
278
- const answer = await settledWithin(held, 5_000);
279
-
280
- assert.equal(manager.encodeRuns.hasFailed(session), true, "stopping short is a failure, not a finished file");
281
- assert.notEqual(answer, STILL_WAITING, "it was left to sit out its whole hold");
282
- assert.equal(answer.kind, "failed", "a wait nothing will satisfy must be told");
283
- });
284
-
285
- test("one run of two ending in failure tells nobody — the other is still making it", async (t) => {
286
- // The condition is the OUTPUT's state, not this run's ending. A run that
287
- // failed while another is alive on the same output has not made the segment
288
- // unobtainable, so a request for it goes on waiting rather than being handed
289
- // an error by the run that lost.
290
- const { manager, dirPath } = await outputWhoseEncoderDied({ failed: false });
291
- t.after(async () => {
292
- await manager.lifecycle.disposeAll();
293
- await rm(dirPath, { recursive: true, force: true });
294
- });
295
- const session = manager.outputs.get(SESSION_ID);
296
- const runOf = (from, to, child) => {
297
- const run = new EncodeRun({
298
- address: OUTPUT_KEY,
299
- encoder: { name: "libx264", kind: "software" },
300
- from,
301
- to,
302
- buildArgs: () => [],
303
- spawn: () => child,
304
- logger: silentLogger,
305
- lastSegmentIndex: () => 4,
306
- because: "a check asked for it",
307
- onEnded: (ended) => manager.encodeRuns.noteRunEnded(session, run, ended)
308
- });
309
- manager.encodeOrchestrator.adopt(OUTPUT_KEY, run);
310
- return run;
311
- };
312
- const dying = fakeProcess();
313
- const working = fakeProcess();
314
- runOf(3, 3, dying);
315
- runOf(4, 4, working);
316
-
317
- const held = waitForSessionFile(manager.serving, SESSION_ID, "segment-00003.mp4", {
318
- holdMs: 30_000,
319
- consumerId: "viewer-a"
320
- });
321
- await untilWaitsAreRegistered(manager.segmentStore, OUTPUT_KEY);
322
-
323
- dying.exit(255);
324
-
325
- assert.equal(
326
- manager.encodeRuns.hasFailed(session),
327
- false,
328
- "the output has not failed while a run is alive on it"
329
- );
330
- assert.equal(await settledWithin(held, 300), STILL_WAITING, "the request must go on waiting");
331
- });
332
-
333
- test("an init that will never be made says so instead of warming up for ever", async (t) => {
334
- // On the branch that lifts the header out of the first piece there is no file
335
- // to be missing: `#initFromFirstSegment` answers null rather than throwing, so
336
- // the catch that names a failure is never reached and the answer was always
337
- // "still warming up".
338
- //
339
- // THE FIXTURE MUST BE ON THAT BRANCH, and it was not: with a re-encode on the
340
- // even grid `cutsAtGivenTimes` is false, so the check read a missing init
341
- // FILE and exercised the `ENOENT` path instead — the one that was never
342
- // broken. A copy cut at the source's own keyframes is the configuration that
343
- // writes no init file at all.
344
- const { manager, dirPath } = await outputWhoseEncoderDied({ segments: [], cutsAtGivenTimes: true });
345
- t.after(async () => {
346
- await manager.lifecycle.disposeAll();
347
- await rm(dirPath, { recursive: true, force: true });
348
- });
349
-
350
- const answer = await manager.serving.getFileStream(SESSION_ID, "init.mp4", { consumerId: "viewer-a" });
351
-
352
- assert.equal(answer.kind, "failed", "nothing is going to produce the header it would come from");
353
- assert.match(answer.message, /255/);
354
- });
@@ -1,202 +0,0 @@
1
- /**
2
- * @file A start that cannot succeed must not be commanded for ever.
3
- *
4
- * Two defects met here on 2026-09-05, and only together did they produce an
5
- * unbounded loop.
6
- *
7
- * The first is older than the second and had never once run: `#onRunEnded`
8
- * removed the run from the session and THEN asked whether the run was still the
9
- * session's, a question that always answers "no" after the removal. Everything
10
- * below that point was unreachable — the fallback from a failed hardware
11
- * encoder to software, the retry when the torrent data goes away, the limit on
12
- * retrying a position that keeps failing, and the error line naming the ffmpeg
13
- * command. Measured over both of the field host's log files: zero occurrences
14
- * of that error line and zero of `fast failure at segment`, across every
15
- * session that proxy had ever run.
16
- *
17
- * The second is that the limit only counted past segment #0, because it was
18
- * written for seek restarts and a seek is never to the beginning — leaving the
19
- * one position the plan commands first with no count at all.
20
- *
21
- * With what encoders should exist re-decided on a five-second timer, the two
22
- * showed up as a restart every five seconds, for sixteen minutes, in the field.
23
- * Re-decided the moment its inputs change, they show up as a loop as fast as
24
- * spawning can fail: fifty passes of the plan before a probe stopped it.
25
- */
26
-
27
- import test from "node:test";
28
- import assert from "node:assert/strict";
29
- import { mkdtempSync, rmSync } from "node:fs";
30
- import os from "node:os";
31
- import path from "node:path";
32
- import { wireOutputs } from "../services/server/wire-outputs.js";
33
- import { SourceFile } from "../services/media/SourceFile.js";
34
- import { ENCODE_EXIT } from "../services/encode/encode-exit.js";
35
- import { outputSpec } from "./helpers/output-spec.js";
36
-
37
- const SESSION_ID = "cccccccc00004000";
38
-
39
- /**
40
- * A session with one run in it, shaped as the manager expects.
41
- *
42
- * @param {string} dirPath
43
- * @returns {{ session: object, run: object }}
44
- */
45
- function sessionWithARun(dirPath) {
46
- const run = { from: 0, to: -1, argsDescribed: "ffmpeg …", state: "ENDED_FAILED" };
47
- const session = {
48
- id: SESSION_ID,
49
- spec: outputSpec({ transcodeVideo: false }),
50
- dirPath,
51
- state: "live",
52
- file: new SourceFile({ sourceKey: "source-1", fileIndex: 0, name: "video.mkv" }),
53
- get inputFile() { return this.file; },
54
- get audioFile() { return this.file; },
55
- outputKey: "output-under-test",
56
- lastRequestedSegment: 0,
57
- progress: { updatedAt: 0, processedSeconds: 0, totalSeconds: 100, startPositionSeconds: 0 },
58
- lastAccessedAt: Date.now()
59
- };
60
- return { session, run };
61
- }
62
-
63
- /**
64
- * @param {object} run
65
- * @param {number} from
66
- * @returns {object}
67
- */
68
- function failedFast(run, from) {
69
- return {
70
- address: "output-under-test",
71
- run,
72
- ending: ENCODE_EXIT.FAILED,
73
- from,
74
- to: -1,
75
- livedMs: 20,
76
- because: "the process could not be started: spawn ffmpeg ENOENT",
77
- lastError: "the process could not be started: spawn ffmpeg ENOENT",
78
- producedCount: 0
79
- };
80
- }
81
-
82
- test("a run that ended is still recognised as the session's own", (t) => {
83
- const dirPath = mkdtempSync(path.join(os.tmpdir(), "breaker-"));
84
- t.after(() => rmSync(dirPath, { recursive: true, force: true }));
85
- const manager = wireOutputs({
86
- enabled: true,
87
- ffmpegBin: "ffmpeg",
88
- localBindHost: "127.0.0.1",
89
- localPort: 9090
90
- });
91
- const { session, run } = sessionWithARun(dirPath);
92
- manager.outputs.set(SESSION_ID, session);
93
- manager.encodeOrchestrator.adopt(session.outputKey, run);
94
-
95
- manager.encodeRuns.noteRunEnded(session, run, failedFast(run, 0));
96
-
97
- // If the identity were read after the removal, nothing here would have been
98
- // written: the handler would have returned at its second line.
99
- assert.equal(manager.encodeOrchestrator.startFailureOf(session.outputKey).count, 1, "the failure was counted");
100
- assert.equal(manager.encodeOrchestrator.startFailureOf(session.outputKey).at, 0, "at the position it happened");
101
- assert.ok(manager.encodeOrchestrator.errorOf(session.outputKey).length > 0, "and the encoding owner knows what went wrong");
102
- assert.equal(
103
- manager.encodeOrchestrator.stateOf(session.outputKey),
104
- "ENDED_FAILED",
105
- "the ending remains observable after the ended run leaves the registry"
106
- );
107
- });
108
-
109
- test("the count runs at segment 0, which is where a first start happens", (t) => {
110
- const dirPath = mkdtempSync(path.join(os.tmpdir(), "breaker-"));
111
- t.after(() => rmSync(dirPath, { recursive: true, force: true }));
112
- const manager = wireOutputs({
113
- enabled: true,
114
- ffmpegBin: "ffmpeg",
115
- localBindHost: "127.0.0.1",
116
- localPort: 9090
117
- });
118
- const { session } = sessionWithARun(dirPath);
119
- manager.outputs.set(SESSION_ID, session);
120
-
121
- for (let attempt = 1; attempt <= 3; attempt += 1) {
122
- const run = { from: 0, to: -1, argsDescribed: "ffmpeg …" };
123
- manager.encodeOrchestrator.adopt(session.outputKey, run);
124
- manager.encodeRuns.noteRunEnded(session, run, failedFast(run, 0));
125
- assert.equal(manager.encodeOrchestrator.startFailureOf(session.outputKey).count, attempt);
126
- assert.equal(manager.encodeOrchestrator.mayStartAt(session.outputKey, 0), attempt < 3);
127
- }
128
- assert.equal(manager.encodeOrchestrator.mayStartAt(session.outputKey, 1), true, "another position has its own budget");
129
- });
130
-
131
- test("real work resets the count, so a transient failure is not permanent", (t) => {
132
- const dirPath = mkdtempSync(path.join(os.tmpdir(), "breaker-"));
133
- t.after(() => rmSync(dirPath, { recursive: true, force: true }));
134
- const manager = wireOutputs({
135
- enabled: true,
136
- ffmpegBin: "ffmpeg",
137
- localBindHost: "127.0.0.1",
138
- localPort: 9090
139
- });
140
- const { session } = sessionWithARun(dirPath);
141
- manager.outputs.set(SESSION_ID, session);
142
-
143
- const quick = { from: 0, to: -1 };
144
- manager.encodeOrchestrator.adopt(session.outputKey, quick);
145
- manager.encodeRuns.noteRunEnded(session, quick, failedFast(quick, 0));
146
- assert.equal(manager.encodeOrchestrator.startFailureOf(session.outputKey).count, 1);
147
-
148
- const lived = { from: 0, to: -1 };
149
- manager.encodeOrchestrator.adopt(session.outputKey, lived);
150
- manager.encodeRuns.noteRunEnded(session, lived, { ...failedFast(lived, 0), livedMs: 30_000 });
151
-
152
- assert.equal(manager.encodeOrchestrator.startFailureOf(session.outputKey).count, 0, "a run that did real work is not a failing start");
153
- assert.equal(manager.encodeOrchestrator.startFailureOf(session.outputKey).at, -1);
154
- });
155
-
156
- test("a failure belongs to the output it happened on, not to the next output of the same address", async (t) => {
157
- const dirPath = mkdtempSync(path.join(os.tmpdir(), "breaker-"));
158
- t.after(() => rmSync(dirPath, { recursive: true, force: true }));
159
- const manager = wireOutputs({
160
- enabled: true,
161
- ffmpegBin: "ffmpeg",
162
- localBindHost: "127.0.0.1",
163
- localPort: 9090
164
- });
165
- t.after(() => manager.lifecycle.disposeAll());
166
- const { session } = sessionWithARun(dirPath);
167
- manager.outputs.set(SESSION_ID, session);
168
- for (let attempt = 1; attempt <= 3; attempt += 1) {
169
- const run = { from: 0, to: -1, argsDescribed: "ffmpeg …" };
170
- manager.encodeOrchestrator.adopt(session.outputKey, run);
171
- manager.encodeRuns.noteRunEnded(session, run, failedFast(run, 0));
172
- }
173
- assert.equal(manager.encodeOrchestrator.mayStartAt(session.outputKey, 0), false, "the first output is blocked");
174
-
175
- await manager.lifecycle.disposeSession(SESSION_ID);
176
-
177
- // The same parameters opened again: the name follows from the key, so the
178
- // new output has the same address. What failed on the disposed one is not a
179
- // fact about this one — a viewer arriving is new information.
180
- assert.equal(manager.encodeOrchestrator.stateOf(session.outputKey), "IDLE", "no ending is inherited");
181
- assert.equal(manager.encodeOrchestrator.errorOf(session.outputKey), "", "no error is inherited");
182
- assert.equal(manager.encodeOrchestrator.mayStartAt(session.outputKey, 0), true, "no blocked position is inherited");
183
- });
184
-
185
- test("a run ending after its output was disposed leaves nothing behind for the next output", (t) => {
186
- const dirPath = mkdtempSync(path.join(os.tmpdir(), "breaker-"));
187
- t.after(() => rmSync(dirPath, { recursive: true, force: true }));
188
- const manager = wireOutputs({
189
- enabled: true,
190
- ffmpegBin: "ffmpeg",
191
- localBindHost: "127.0.0.1",
192
- localPort: 9090
193
- });
194
- const { session, run } = sessionWithARun(dirPath);
195
- manager.encodeOrchestrator.adopt(session.outputKey, run);
196
- // Disposal removed the output from the registry; its process ends later.
197
- manager.encodeRuns.noteRunEnded(session, run, failedFast(run, 0));
198
-
199
- assert.equal(manager.encodeOrchestrator.stateOf(session.outputKey), "IDLE");
200
- assert.equal(manager.encodeOrchestrator.errorOf(session.outputKey), "");
201
- assert.equal(manager.encodeOrchestrator.startFailureOf(session.outputKey).count, 0);
202
- });