@torrent-tv/proxy 2.80.18 → 2.81.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/docs/encode-architecture.md +51 -2
  3. package/package.json +1 -1
  4. package/research/double-spawn-2026-09-10.md +171 -0
  5. package/services/disk/DiskSpace.js +146 -0
  6. package/services/disk/wire.js +60 -0
  7. package/services/encode/EncodeRun.js +37 -9
  8. package/services/encode/SegmentStore.js +284 -232
  9. package/services/encode/run-command.js +16 -1
  10. package/services/hls-session-manager.js +31 -128
  11. package/services/orchestrators/EncodeOrchestrator.js +52 -38
  12. package/services/piece-store/allowance.js +107 -0
  13. package/services/piece-store/piece-disk-store.js +365 -0
  14. package/services/piece-store/shared-piece-store.js +1549 -1535
  15. package/services/segment-formats/fmp4.js +54 -0
  16. package/services/segment-formats/mpegts.js +54 -0
  17. package/services/torrent-worker/client.js +32 -0
  18. package/services/torrent-worker/pool-adapter.js +15 -0
  19. package/services/torrent-worker/protocol.js +9 -0
  20. package/services/torrent-worker/worker.js +8 -1
  21. package/services/viewer/positions.js +48 -0
  22. package/test/audio-inventory.test.js +176 -176
  23. package/test/auto-quality-step.test.js +514 -514
  24. package/test/concurrent-cost.test.js +138 -138
  25. package/test/coverage-follows-the-disk.test.js +191 -187
  26. package/test/coverage-map.test.js +195 -195
  27. package/test/declared-tracks.test.js +35 -35
  28. package/test/disk-space.test.js +138 -0
  29. package/test/encode-orchestrator.test.js +0 -3
  30. package/test/encode-run.test.js +5 -12
  31. package/test/held-request-width.test.js +155 -155
  32. package/test/helpers/encode-run.js +2 -2
  33. package/test/matroska-blocks.test.js +0 -0
  34. package/test/matroska-cues-track.test.js +192 -192
  35. package/test/mp4-composition-times.test.js +0 -0
  36. package/test/mp4-subtitles.test.js +173 -173
  37. package/test/one-authority.test.js +281 -220
  38. package/test/orchestrator-wired.test.js +199 -195
  39. package/test/packet-witness-ring.test.js +236 -236
  40. package/test/packet-witness.test.js +148 -148
  41. package/test/piece-disk-store.test.js +267 -0
  42. package/test/piece-reader.test.js +4 -4
  43. package/test/piece-store-eviction.test.js +17 -17
  44. package/test/piece-store-reservations.test.js +20 -1
  45. package/test/piece-store-slow-disk.test.js +16 -1
  46. package/test/produced-copy-choice.test.js +258 -358
  47. package/test/read-window.test.js +6 -6
  48. package/test/run-intervals.test.js +100 -100
  49. package/test/seek-landing.test.js +109 -109
  50. package/test/segment-serve-wiring.test.js +8 -9
  51. package/test/segment-store-eviction.test.js +232 -0
  52. package/test/segment-store.test.js +238 -216
  53. package/test/segments-are-shared.test.js +1 -1
  54. package/test/shared-piece-store.test.js +12 -12
  55. package/test/sidecar-naming.test.js +142 -142
  56. package/test/subtitle-cue-framing.test.js +200 -200
  57. package/test/subtitle-cue-walk.test.js +369 -369
  58. package/test/subtitle-defaults.test.js +97 -97
  59. package/test/subtitle-track-numbering.test.js +370 -370
  60. package/test/tail-duplication.test.js +167 -167
  61. package/test/tracks-begin-together.test.js +195 -195
  62. package/test/two-viewers-one-picture.test.js +374 -374
  63. package/test/video-facts.test.js +102 -102
  64. package/test/wedge-certainty.test.js +131 -131
  65. package/services/encode/open-piece.js +0 -135
  66. package/services/piece-store/disk-tier.js +0 -151
  67. package/test/open-piece.test.js +0 -152
@@ -52,7 +52,8 @@ function makeRun(span = {}) {
52
52
  warn: (line) => lines.push(["warn", line])
53
53
  },
54
54
  now: () => 1000,
55
- onEnded: (ended) => ends.push(ended)
55
+ onEnded: (ended) => ends.push(ended),
56
+ because: "nobody is making #10 and a viewer is waiting for it"
56
57
  });
57
58
  return { run, process: process_, lines, ends };
58
59
  }
@@ -61,8 +62,7 @@ test("a start says why it is starting and with what", () => {
61
62
  // The argument list alone cannot say whether this was a first open, a seek, a
62
63
  // quality step or a move off covered material — and with several runs at once
63
64
  // that is the only way to tell one start from another.
64
- const { run, lines } = makeRun();
65
- run.start("nobody is making #10 and a viewer is waiting for it");
65
+ const { lines } = makeRun();
66
66
  const [level, line] = lines[0];
67
67
  assert.equal(level, "info");
68
68
  // A run is named by what it produces: the stretch it was given, on the output
@@ -74,7 +74,6 @@ test("a start says why it is starting and with what", () => {
74
74
 
75
75
  test("its head is the next number it will make, and moves as it makes them", () => {
76
76
  const { run } = makeRun();
77
- run.start("first");
78
77
  assert.equal(run.head, 10);
79
78
  run.noteProduced(10);
80
79
  run.noteProduced(11);
@@ -84,7 +83,6 @@ test("its head is the next number it will make, and moves as it makes them", ()
84
83
 
85
84
  test("reaching the end of its stretch and exiting is the one normal ending", () => {
86
85
  const { run, process, ends, lines } = makeRun({ from: 10, to: 12 });
87
- run.start("first");
88
86
  for (const index of [10, 11, 12]) {
89
87
  run.noteProduced(index);
90
88
  }
@@ -99,7 +97,6 @@ test("exiting cleanly short of its stretch is abnormal and says where it stopped
99
97
  // ffmpeg exits zero both at the end of a file and when its input stops
100
98
  // producing bytes; over a torrent the two look identical to it.
101
99
  const { run, process, ends } = makeRun({ from: 10, to: 14 });
102
- run.start("first");
103
100
  run.noteProduced(10);
104
101
  run.noteProduced(11);
105
102
  process.exitWith(0, null);
@@ -110,8 +107,7 @@ test("exiting cleanly short of its stretch is abnormal and says where it stopped
110
107
  });
111
108
 
112
109
  test("a non-zero exit is a failure carrying its code", () => {
113
- const { run, process, ends, lines } = makeRun();
114
- run.start("first");
110
+ const { process, ends, lines } = makeRun();
115
111
  process.exitWith(255, null);
116
112
  assert.equal(ends[0].ending, ENCODE_EXIT.FAILED);
117
113
  assert.equal(ends[0].code, 255);
@@ -122,7 +118,6 @@ test("our own kill is abnormal too, and carries the reason we gave", () => {
122
118
  // Hiding it among the normal endings would make a count of abnormal endings
123
119
  // useless, which is the whole point of counting them.
124
120
  const { run, process, ends } = makeRun();
125
- run.start("first");
126
121
  run.stop("moved past 20 segments that are already made");
127
122
  assert.deepEqual(process.signals, ["SIGTERM"]);
128
123
  process.exitWith(null, "SIGTERM");
@@ -133,7 +128,6 @@ test("our own kill is abnormal too, and carries the reason we gave", () => {
133
128
 
134
129
  test("an ending reports the stretch, how far it got and how long it lived", () => {
135
130
  const { run, process, ends } = makeRun({ from: 10, to: 14 });
136
- run.start("first");
137
131
  run.noteProduced(10);
138
132
  process.exitWith(1, null);
139
133
  const ended = ends[0];
@@ -145,8 +139,7 @@ test("an ending reports the stretch, how far it got and how long it lived", () =
145
139
  });
146
140
 
147
141
  test("a process that could not be started ends like any other failure", () => {
148
- const { run, process, ends } = makeRun();
149
- run.start("first");
142
+ const { process, ends } = makeRun();
150
143
  process.emit("error", new Error("ENOENT"));
151
144
  assert.equal(ends[0].ending, ENCODE_EXIT.FAILED);
152
145
  assert.match(ends[0].because, /ENOENT/);
@@ -1,155 +1,155 @@
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/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 { HlsSessionManager } from "../services/hls-session-manager.js";
19
- import { fmp4Format } from "../services/segment-formats/fmp4.js";
20
-
21
- const SESSION_ID = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
22
- const SEGMENT_SECONDS = 4;
23
-
24
- /**
25
- * @returns {Promise<{ manager: HlsSessionManager, dirPath: string }>}
26
- */
27
- async function managerWithSession() {
28
- const dirPath = await mkdtemp(path.join(os.tmpdir(), "held-request-"));
29
- const manager = new HlsSessionManager({
30
- enabled: true,
31
- ffmpegBin: "ffmpeg",
32
- localBindHost: "127.0.0.1",
33
- localPort: 9090
34
- });
35
- manager.sessionsById.set(SESSION_ID, {
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
- // The viewer is at segment #25.
49
- furthestViewerSeconds: 100
50
- });
51
- return { manager, dirPath };
52
- }
53
-
54
- /**
55
- * @param {number} index
56
- * @returns {string}
57
- */
58
- function segment(index) {
59
- return `segment-${String(index).padStart(5, "0")}.mp4`;
60
- }
61
-
62
- test("the width is the encoder's own look-ahead, in segments", async (t) => {
63
- const { manager, dirPath } = await managerWithSession();
64
- t.after(async () => {
65
- await rm(dirPath, { recursive: true, force: true });
66
- });
67
-
68
- const width = Math.ceil(manager.lookaheadSeconds / manager.segmentDurationSec);
69
- assert.equal(width, 30, "120 s of look-ahead over 4 s segments");
70
- assert.equal(manager.requestStillWanted(SESSION_ID, segment(25 + width)), true, "the far edge");
71
- assert.equal(
72
- manager.requestStillWanted(SESSION_ID, segment(25 + width + 1)),
73
- false,
74
- "past everything the encoder is allowed to have produced"
75
- );
76
- });
77
-
78
- test("a request the old eight-segment width would have refused is kept", async (t) => {
79
- const { manager, dirPath } = await managerWithSession();
80
- t.after(async () => {
81
- await rm(dirPath, { recursive: true, force: true });
82
- });
83
-
84
- // #34 is nine segments ahead of the viewer — inside a 120 s cushion and
85
- // outside the eight the width used to be. This is the request a browser
86
- // holding the whole cushion makes constantly.
87
- assert.equal(manager.requestStillWanted(SESSION_ID, segment(34)), true);
88
- });
89
-
90
- test("a segment behind the viewer is still released", async (t) => {
91
- const { manager, dirPath } = await managerWithSession();
92
- t.after(async () => {
93
- await rm(dirPath, { recursive: true, force: true });
94
- });
95
-
96
- assert.equal(manager.requestStillWanted(SESSION_ID, segment(24)), false);
97
- });
98
-
99
- test("a seek by one viewer does not release the request held for another", async (t) => {
100
- const { manager, dirPath } = await managerWithSession();
101
- t.after(async () => {
102
- await rm(dirPath, { recursive: true, force: true });
103
- });
104
- // Both are watching the same copied picture, so both are this one session.
105
- // One is at segment #25, the other far ahead at #150 — inside the fixture's
106
- // own 200-segment timeline, since a position past the end of the grid is
107
- // clamped to it and would prove nothing about the width.
108
- manager.requestSeek(SESSION_ID, 100, "behind");
109
- manager.requestSeek(SESSION_ID, 600, "ahead");
110
-
111
- // A SEEK DOES ONE THING: it puts the viewer where they now are. It used to
112
- // write that position into five places, this session's shared field among
113
- // them, and every held request was then judged against whoever moved last —
114
- // which is exactly what this test exists to refuse. Each viewer's own
115
- // position is what decides, and the two below are asked separately.
116
- assert.equal(manager.viewers.get("behind").positionSeconds(), 100);
117
- assert.equal(manager.viewers.get("ahead").positionSeconds(), 600);
118
-
119
- assert.equal(
120
- manager.requestStillWanted(SESSION_ID, segment(26), "behind"),
121
- true,
122
- "the segment the viewer behind is waiting for is still theirs to wait for"
123
- );
124
- assert.equal(
125
- manager.requestStillWanted(SESSION_ID, segment(26), "ahead"),
126
- false,
127
- "and for the one in front it is a place they have left"
128
- );
129
- });
130
-
131
- test("a viewer's own head moves with their seek", async (t) => {
132
- const { manager, dirPath } = await managerWithSession();
133
- t.after(async () => {
134
- await rm(dirPath, { recursive: true, force: true });
135
- });
136
-
137
- // Their last request was at #25; they jump to 600 s, which is #150. The
138
- // segment at the target must be wanted — refusing it there is the freeze of
139
- // 2026-08-18.
140
- manager.requestSeek(SESSION_ID, 600, "viewer");
141
- assert.equal(manager.requestStillWanted(SESSION_ID, segment(150), "viewer"), true);
142
- assert.equal(manager.requestStillWanted(SESSION_ID, segment(25), "viewer"), false);
143
- });
144
-
145
- test("a viewer nobody can name is judged against the one shared position", async (t) => {
146
- const { manager, dirPath } = await managerWithSession();
147
- t.after(async () => {
148
- await rm(dirPath, { recursive: true, force: true });
149
- });
150
-
151
- // A plain HTTP transport builds its own URLs and carries no id. The old
152
- // behaviour is what remains, which for a single viewer is the same thing.
153
- assert.equal(manager.requestStillWanted(SESSION_ID, segment(26), ""), true);
154
- assert.equal(manager.requestStillWanted(SESSION_ID, segment(24), ""), false);
155
- });
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/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 { HlsSessionManager } from "../services/hls-session-manager.js";
19
+ import { fmp4Format } from "../services/segment-formats/fmp4.js";
20
+
21
+ const SESSION_ID = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
22
+ const SEGMENT_SECONDS = 4;
23
+
24
+ /**
25
+ * @returns {Promise<{ manager: HlsSessionManager, dirPath: string }>}
26
+ */
27
+ async function managerWithSession() {
28
+ const dirPath = await mkdtemp(path.join(os.tmpdir(), "held-request-"));
29
+ const manager = new HlsSessionManager({
30
+ enabled: true,
31
+ ffmpegBin: "ffmpeg",
32
+ localBindHost: "127.0.0.1",
33
+ localPort: 9090
34
+ });
35
+ manager.sessionsById.set(SESSION_ID, {
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
+ // The viewer is at segment #25.
49
+ furthestViewerSeconds: 100
50
+ });
51
+ return { manager, dirPath };
52
+ }
53
+
54
+ /**
55
+ * @param {number} index
56
+ * @returns {string}
57
+ */
58
+ function segment(index) {
59
+ return `segment-${String(index).padStart(5, "0")}.mp4`;
60
+ }
61
+
62
+ test("the width is the encoder's own look-ahead, in segments", async (t) => {
63
+ const { manager, dirPath } = await managerWithSession();
64
+ t.after(async () => {
65
+ await rm(dirPath, { recursive: true, force: true });
66
+ });
67
+
68
+ const width = Math.ceil(manager.lookaheadSeconds / manager.segmentDurationSec);
69
+ assert.equal(width, 30, "120 s of look-ahead over 4 s segments");
70
+ assert.equal(manager.requestStillWanted(SESSION_ID, segment(25 + width)), true, "the far edge");
71
+ assert.equal(
72
+ manager.requestStillWanted(SESSION_ID, segment(25 + width + 1)),
73
+ false,
74
+ "past everything the encoder is allowed to have produced"
75
+ );
76
+ });
77
+
78
+ test("a request the old eight-segment width would have refused is kept", async (t) => {
79
+ const { manager, dirPath } = await managerWithSession();
80
+ t.after(async () => {
81
+ await rm(dirPath, { recursive: true, force: true });
82
+ });
83
+
84
+ // #34 is nine segments ahead of the viewer — inside a 120 s cushion and
85
+ // outside the eight the width used to be. This is the request a browser
86
+ // holding the whole cushion makes constantly.
87
+ assert.equal(manager.requestStillWanted(SESSION_ID, segment(34)), true);
88
+ });
89
+
90
+ test("a segment behind the viewer is still released", async (t) => {
91
+ const { manager, dirPath } = await managerWithSession();
92
+ t.after(async () => {
93
+ await rm(dirPath, { recursive: true, force: true });
94
+ });
95
+
96
+ assert.equal(manager.requestStillWanted(SESSION_ID, segment(24)), false);
97
+ });
98
+
99
+ test("a seek by one viewer does not release the request held for another", async (t) => {
100
+ const { manager, dirPath } = await managerWithSession();
101
+ t.after(async () => {
102
+ await rm(dirPath, { recursive: true, force: true });
103
+ });
104
+ // Both are watching the same copied picture, so both are this one session.
105
+ // One is at segment #25, the other far ahead at #150 — inside the fixture's
106
+ // own 200-segment timeline, since a position past the end of the grid is
107
+ // clamped to it and would prove nothing about the width.
108
+ manager.requestSeek(SESSION_ID, 100, "behind");
109
+ manager.requestSeek(SESSION_ID, 600, "ahead");
110
+
111
+ // A SEEK DOES ONE THING: it puts the viewer where they now are. It used to
112
+ // write that position into five places, this session's shared field among
113
+ // them, and every held request was then judged against whoever moved last —
114
+ // which is exactly what this test exists to refuse. Each viewer's own
115
+ // position is what decides, and the two below are asked separately.
116
+ assert.equal(manager.viewers.get("behind").positionSeconds(), 100);
117
+ assert.equal(manager.viewers.get("ahead").positionSeconds(), 600);
118
+
119
+ assert.equal(
120
+ manager.requestStillWanted(SESSION_ID, segment(26), "behind"),
121
+ true,
122
+ "the segment the viewer behind is waiting for is still theirs to wait for"
123
+ );
124
+ assert.equal(
125
+ manager.requestStillWanted(SESSION_ID, segment(26), "ahead"),
126
+ false,
127
+ "and for the one in front it is a place they have left"
128
+ );
129
+ });
130
+
131
+ test("a viewer's own head moves with their seek", async (t) => {
132
+ const { manager, dirPath } = await managerWithSession();
133
+ t.after(async () => {
134
+ await rm(dirPath, { recursive: true, force: true });
135
+ });
136
+
137
+ // Their last request was at #25; they jump to 600 s, which is #150. The
138
+ // segment at the target must be wanted — refusing it there is the freeze of
139
+ // 2026-08-18.
140
+ manager.requestSeek(SESSION_ID, 600, "viewer");
141
+ assert.equal(manager.requestStillWanted(SESSION_ID, segment(150), "viewer"), true);
142
+ assert.equal(manager.requestStillWanted(SESSION_ID, segment(25), "viewer"), false);
143
+ });
144
+
145
+ test("a viewer nobody can name is judged against the one shared position", async (t) => {
146
+ const { manager, dirPath } = await managerWithSession();
147
+ t.after(async () => {
148
+ await rm(dirPath, { recursive: true, force: true });
149
+ });
150
+
151
+ // A plain HTTP transport builds its own URLs and carries no id. The old
152
+ // behaviour is what remains, which for a single viewer is the same thing.
153
+ assert.equal(manager.requestStillWanted(SESSION_ID, segment(26), ""), true);
154
+ assert.equal(manager.requestStillWanted(SESSION_ID, segment(24), ""), false);
155
+ });
@@ -117,9 +117,9 @@ export function startRunOn(session, options = {}) {
117
117
  spawn: () => child,
118
118
  logger: silentLogger,
119
119
  lastSegmentIndex: () => lastSegmentIndex,
120
- usesExplicitCuts
120
+ usesExplicitCuts,
121
+ because: "a test asked for it"
121
122
  });
122
- run.start("a test asked for it");
123
123
  run.noteSpeed(speedX);
124
124
  if (producing) {
125
125
  // What moves a run out of starting is its first segment, in the product as
Binary file