@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
@@ -0,0 +1,232 @@
1
+ /**
2
+ * @file What goes first when the disk is short: the viewers decide.
3
+ *
4
+ * The store used to throw away whole outputs by when their directory was last
5
+ * read, which says nothing about what anybody is about to watch. The order here
6
+ * is the priority map's own, read from the other end: nobody's output first,
7
+ * then what is behind the earliest viewer, then what is ahead of the furthest.
8
+ */
9
+
10
+ import test from "node:test";
11
+ import assert from "node:assert/strict";
12
+ import fs from "node:fs/promises";
13
+ import os from "node:os";
14
+ import path from "node:path";
15
+ import { SegmentStore } from "../services/encode/SegmentStore.js";
16
+
17
+ const SEGMENT = 1024;
18
+
19
+ const format = {
20
+ isSegmentFileName: (name) => /^segment-\d{5}\.mp4$/.test(name),
21
+ segmentIndexFromName: (name) => {
22
+ const match = /^segment-(\d{5})\.mp4$/.exec(name);
23
+ return match ? Number(match[1]) : -1;
24
+ }
25
+ };
26
+
27
+ /**
28
+ * @param {number} [now]
29
+ */
30
+ async function makeStore(now = 1000) {
31
+ const root = await fs.mkdtemp(path.join(os.tmpdir(), "segment-store-test-"));
32
+ const clock = { at: now };
33
+ const store = new SegmentStore({ root, now: () => clock.at });
34
+ return { store, root, clock };
35
+ }
36
+
37
+ /**
38
+ * @param {SegmentStore} store
39
+ * @param {string} key
40
+ * @param {number[]} indexes
41
+ */
42
+ async function fill(store, key, indexes) {
43
+ store.useFormat(key, format);
44
+ const dir = store.directoryFor(key);
45
+ for (const index of indexes) {
46
+ await fs.writeFile(
47
+ path.join(dir, `segment-${String(index).padStart(5, "0")}.mp4`),
48
+ Buffer.alloc(SEGMENT, index % 251)
49
+ );
50
+ }
51
+ }
52
+
53
+ /**
54
+ * @param {SegmentStore} store
55
+ * @param {string} key
56
+ */
57
+ async function heldNumbers(store, key) {
58
+ const names = await fs.readdir(store.pathFor(key)).catch(() => []);
59
+ return names
60
+ .filter((name) => format.isSegmentFileName(name))
61
+ .map((name) => format.segmentIndexFromName(name))
62
+ .sort((left, right) => left - right);
63
+ }
64
+
65
+ test("what nobody is watching goes before anything anybody is", async () => {
66
+ const { store, root } = await makeStore();
67
+ try {
68
+ await fill(store, "watched", [0, 1, 2, 3]);
69
+ await fill(store, "abandoned", [0, 1, 2, 3]);
70
+
71
+ // Room for five segments of the eight held.
72
+ store.enforce({
73
+ idleMs: Number.POSITIVE_INFINITY,
74
+ maxBytes: 5 * SEGMENT,
75
+ viewersAt: (key) => (key === "watched" ? [2] : [])
76
+ });
77
+
78
+ // Three of eight had to go, and all three came from the output nobody is on.
79
+ assert.deepEqual(
80
+ await heldNumbers(store, "watched"),
81
+ [0, 1, 2, 3],
82
+ "the watched output lost segments while an unwatched one still held some"
83
+ );
84
+ assert.equal((await heldNumbers(store, "abandoned")).length, 1, "the unwatched output kept too much");
85
+ } finally {
86
+ await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
87
+ }
88
+ });
89
+
90
+ test("behind the viewer goes before ahead of the viewer, furthest behind first", async () => {
91
+ const { store, root } = await makeStore();
92
+ try {
93
+ await fill(store, "one", [0, 1, 2, 3, 4, 5]);
94
+
95
+ // The viewer stands on #3. Room for four of the six.
96
+ store.enforce({
97
+ idleMs: Number.POSITIVE_INFINITY,
98
+ maxBytes: 4 * SEGMENT,
99
+ viewersAt: () => [3]
100
+ });
101
+
102
+ assert.deepEqual(
103
+ await heldNumbers(store, "one"),
104
+ [2, 3, 4, 5],
105
+ "the two furthest behind the viewer should have gone, and nothing ahead"
106
+ );
107
+ } finally {
108
+ await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
109
+ }
110
+ });
111
+
112
+ test("with nothing left behind, the furthest ahead goes next", async () => {
113
+ const { store, root } = await makeStore();
114
+ try {
115
+ await fill(store, "one", [4, 5, 6, 7, 8]);
116
+
117
+ // The viewer is on #4, so nothing is behind. Room for three of the five.
118
+ store.enforce({
119
+ idleMs: Number.POSITIVE_INFINITY,
120
+ maxBytes: 3 * SEGMENT,
121
+ viewersAt: () => [4]
122
+ });
123
+
124
+ assert.deepEqual(
125
+ await heldNumbers(store, "one"),
126
+ [4, 5, 6],
127
+ "the two furthest ahead should have gone, nearest kept"
128
+ );
129
+ } finally {
130
+ await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
131
+ }
132
+ });
133
+
134
+ test("the segment a viewer is standing on is never taken", async () => {
135
+ const { store, root } = await makeStore();
136
+ try {
137
+ await fill(store, "one", [0, 1, 2]);
138
+
139
+ // Two viewers, one on each end, and room for one segment only.
140
+ store.enforce({
141
+ idleMs: Number.POSITIVE_INFINITY,
142
+ maxBytes: SEGMENT,
143
+ viewersAt: () => [0, 2]
144
+ });
145
+
146
+ const left = await heldNumbers(store, "one");
147
+ assert.ok(left.includes(0), "the segment the first viewer is on was taken");
148
+ assert.ok(left.includes(2), "the segment the second viewer is on was taken");
149
+ assert.deepEqual(left, [0, 2], "the one between two viewers should have gone");
150
+ } finally {
151
+ await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
152
+ }
153
+ });
154
+
155
+ test("two viewers of one output: behind the EARLIEST, ahead of the FURTHEST", async () => {
156
+ const { store, root } = await makeStore();
157
+ try {
158
+ await fill(store, "one", [0, 1, 2, 3, 4, 5, 6]);
159
+
160
+ // Viewers on #2 and #5. Behind means below #2; ahead means above #5.
161
+ store.enforce({
162
+ idleMs: Number.POSITIVE_INFINITY,
163
+ maxBytes: 5 * SEGMENT,
164
+ viewersAt: () => [2, 5]
165
+ });
166
+
167
+ assert.deepEqual(
168
+ await heldNumbers(store, "one"),
169
+ [2, 3, 4, 5, 6],
170
+ "what lies behind the earliest viewer must go before anything ahead of the furthest"
171
+ );
172
+ } finally {
173
+ await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
174
+ }
175
+ });
176
+
177
+ test("an output nobody has read for long enough goes whole, however much room there is", async () => {
178
+ const { store, root, clock } = await makeStore();
179
+ try {
180
+ await fill(store, "stale", [0, 1, 2]);
181
+ clock.at += 60 * 60 * 1000;
182
+ await fill(store, "fresh", [0, 1, 2]);
183
+
184
+ const result = store.enforce({
185
+ idleMs: 30 * 60 * 1000,
186
+ // Room for everything: this is the rule that does not wait for pressure.
187
+ maxBytes: Number.MAX_SAFE_INTEGER,
188
+ viewersAt: () => []
189
+ });
190
+
191
+ assert.equal(result.droppedIdle, 1);
192
+ assert.deepEqual(await heldNumbers(store, "stale"), [], "the stale output was kept");
193
+ assert.deepEqual(await heldNumbers(store, "fresh"), [0, 1, 2], "the fresh output was taken");
194
+ } finally {
195
+ await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
196
+ }
197
+ });
198
+
199
+ test("told nothing about viewers, it falls back to the oldest directory", async () => {
200
+ const { store, root, clock } = await makeStore();
201
+ try {
202
+ await fill(store, "older", [0, 1, 2]);
203
+ clock.at += 1000;
204
+ await fill(store, "newer", [0, 1, 2]);
205
+
206
+ const result = store.enforce({ idleMs: Number.POSITIVE_INFINITY, maxBytes: 4 * SEGMENT });
207
+
208
+ assert.equal(result.droppedForRoom, 1);
209
+ assert.deepEqual(await heldNumbers(store, "older"), []);
210
+ assert.deepEqual(await heldNumbers(store, "newer"), [0, 1, 2]);
211
+ } finally {
212
+ await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
213
+ }
214
+ });
215
+
216
+ test("a clean exit leaves nothing of ours, so what is found next time is from a kill", async () => {
217
+ const { store, root } = await makeStore();
218
+ try {
219
+ await fill(store, "one", [0, 1, 2]);
220
+ await fill(store, "two", [0, 1]);
221
+ // A directory this process adopted at startup and no session owns: the very
222
+ // thing the old rule left behind, which is how an orphan became permanent.
223
+ await fs.mkdir(path.join(root, "abandoned"), { recursive: true });
224
+ await fs.writeFile(path.join(root, "abandoned", "segment-00000.mp4"), Buffer.alloc(SEGMENT));
225
+
226
+ assert.equal(store.dropAll("the proxy is shutting down"), 2);
227
+
228
+ await assert.rejects(() => fs.stat(root), /ENOENT/, "the store left its root behind");
229
+ } finally {
230
+ await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
231
+ }
232
+ });