@torrent-tv/proxy 2.80.19 → 2.81.1

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 (58) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/package.json +1 -1
  3. package/research/double-spawn-2026-09-10.md +171 -0
  4. package/services/disk/DiskSpace.js +150 -0
  5. package/services/disk/wire.js +60 -0
  6. package/services/encode/EncodeRun.js +25 -3
  7. package/services/encode/SegmentStore.js +137 -9
  8. package/services/hls-session-manager.js +26 -45
  9. package/services/orchestrators/EncodeOrchestrator.js +4 -1
  10. package/services/piece-store/allowance.js +107 -0
  11. package/services/piece-store/piece-disk-store.js +365 -0
  12. package/services/piece-store/shared-piece-store.js +1549 -1535
  13. package/services/torrent-worker/client.js +32 -0
  14. package/services/torrent-worker/pool-adapter.js +15 -0
  15. package/services/torrent-worker/protocol.js +9 -0
  16. package/services/torrent-worker/worker.js +8 -1
  17. package/services/viewer/positions.js +48 -0
  18. package/test/audio-inventory.test.js +176 -176
  19. package/test/auto-quality-step.test.js +514 -514
  20. package/test/concurrent-cost.test.js +138 -138
  21. package/test/coverage-follows-the-disk.test.js +191 -191
  22. package/test/coverage-map.test.js +195 -195
  23. package/test/declared-tracks.test.js +35 -35
  24. package/test/disk-space.test.js +150 -0
  25. package/test/encode-orchestrator.test.js +0 -3
  26. package/test/encode-run.test.js +5 -12
  27. package/test/held-request-width.test.js +155 -155
  28. package/test/helpers/encode-run.js +2 -2
  29. package/test/matroska-blocks.test.js +0 -0
  30. package/test/matroska-cues-track.test.js +192 -192
  31. package/test/mp4-composition-times.test.js +0 -0
  32. package/test/mp4-subtitles.test.js +173 -173
  33. package/test/one-authority.test.js +281 -220
  34. package/test/orchestrator-wired.test.js +199 -199
  35. package/test/packet-witness-ring.test.js +236 -236
  36. package/test/packet-witness.test.js +148 -148
  37. package/test/piece-disk-store.test.js +267 -0
  38. package/test/piece-reader.test.js +4 -4
  39. package/test/piece-store-eviction.test.js +17 -17
  40. package/test/piece-store-reservations.test.js +20 -1
  41. package/test/piece-store-slow-disk.test.js +16 -1
  42. package/test/read-window.test.js +6 -6
  43. package/test/run-intervals.test.js +100 -100
  44. package/test/seek-landing.test.js +109 -109
  45. package/test/segment-store-eviction.test.js +232 -0
  46. package/test/segments-are-shared.test.js +1 -1
  47. package/test/shared-piece-store.test.js +12 -12
  48. package/test/sidecar-naming.test.js +142 -142
  49. package/test/subtitle-cue-framing.test.js +200 -200
  50. package/test/subtitle-cue-walk.test.js +369 -369
  51. package/test/subtitle-defaults.test.js +97 -97
  52. package/test/subtitle-track-numbering.test.js +370 -370
  53. package/test/tail-duplication.test.js +167 -167
  54. package/test/tracks-begin-together.test.js +195 -195
  55. package/test/two-viewers-one-picture.test.js +374 -374
  56. package/test/video-facts.test.js +102 -102
  57. package/test/wedge-certainty.test.js +131 -131
  58. package/services/piece-store/disk-tier.js +0 -151
@@ -1,236 +1,236 @@
1
- import test from "node:test";
2
- import assert from "node:assert/strict";
3
- import { EventEmitter } from "node:events";
4
- import { mkdtemp, readdir, rm, writeFile } from "node:fs/promises";
5
- import os from "node:os";
6
- import path from "node:path";
7
-
8
- import {
9
- adoptOrphanRingFiles,
10
- createPacketWitness,
11
- WITNESS_RING_BASENAME
12
- } from "../services/packet-witness.js";
13
-
14
- /**
15
- * A stand-in for a spawned tcpdump: it records how it was called and stays
16
- * "running" until something kills it.
17
- */
18
- class FakeChild extends EventEmitter {
19
- constructor(command, args) {
20
- super();
21
- this.command = command;
22
- this.args = args;
23
- this.killed = false;
24
- this.signals = [];
25
- }
26
-
27
- kill(signal) {
28
- this.signals.push(signal);
29
- if (!this.killed) {
30
- this.killed = true;
31
- // A real child exits asynchronously, which is what the code must wait for.
32
- setImmediate(() => this.emit("close", 0, signal));
33
- }
34
- return true;
35
- }
36
- }
37
-
38
- /**
39
- * @returns {{ spawnProcess: Function, children: FakeChild[], rings: FakeChild[] }}
40
- */
41
- function makeSpawn() {
42
- const children = [];
43
- const spawnProcess = (command, args) => {
44
- const child = new FakeChild(command, args);
45
- children.push(child);
46
- if (args.includes("--version")) {
47
- // The availability probe: answer at once, like a present tcpdump.
48
- setImmediate(() => child.emit("spawn"));
49
- }
50
- return child;
51
- };
52
- return {
53
- spawnProcess,
54
- children,
55
- get rings() {
56
- return children.filter((child) => !child.args.includes("--version"));
57
- }
58
- };
59
- }
60
-
61
- /**
62
- * Wait until `check` holds, rather than for a chosen interval.
63
- *
64
- * A fixed sleep passes alone and fails in a full run — the defect roadmap item
65
- * 51 names — and the work here is several awaits deep (probe, stop, readdir,
66
- * copy, restart), so its duration is whatever the machine is busy with.
67
- *
68
- * @param {() => boolean | Promise<boolean>} check
69
- * @param {string} what
70
- * @returns {Promise<void>}
71
- */
72
- async function waitFor(check, what) {
73
- const deadline = Date.now() + 10_000;
74
- for (;;) {
75
- if (await check()) {
76
- return;
77
- }
78
- if (Date.now() > deadline) {
79
- throw new Error(`timed out waiting for ${what}`);
80
- }
81
- await new Promise((resolve) => setTimeout(resolve, 5));
82
- }
83
- }
84
-
85
- /** Let every already-queued microtask and immediate run. */
86
- const settle = () => new Promise((resolve) => setTimeout(resolve, 20));
87
-
88
- async function withDir(run) {
89
- const dir = await mkdtemp(path.join(os.tmpdir(), "witness-ring-"));
90
- try {
91
- await run(dir);
92
- } finally {
93
- await rm(dir, { recursive: true, force: true });
94
- }
95
- }
96
-
97
- test("the ring runs while a channel is open and stops with the last one", async () => {
98
- await withDir(async (dir) => {
99
- const spawn = makeSpawn();
100
- const witness = createPacketWitness({
101
- log: () => {},
102
- dir,
103
- port: 9090,
104
- spawnProcess: spawn.spawnProcess
105
- });
106
-
107
- witness.holdRing();
108
- witness.holdRing();
109
- await waitFor(() => spawn.rings.length === 1, "the ring to start");
110
- assert.equal(spawn.rings.length, 1, "a second channel must not start a second ring");
111
- assert.equal(spawn.rings[0].killed, false);
112
-
113
- witness.releaseRing();
114
- await settle();
115
- assert.equal(spawn.rings[0].killed, false, "one channel left still wants the ring");
116
-
117
- witness.releaseRing();
118
- await waitFor(() => spawn.rings[0].killed, "the ring to stop");
119
- });
120
- });
121
-
122
- test("a channel that opens and closes while the ring is starting leaves nothing running", async () => {
123
- await withDir(async (dir) => {
124
- const spawn = makeSpawn();
125
- const witness = createPacketWitness({
126
- log: () => {},
127
- dir,
128
- port: 9090,
129
- spawnProcess: spawn.spawnProcess
130
- });
131
- // Release before the availability probe has resolved — the start is still
132
- // in flight. Without the second look after the await this leaves a tcpdump
133
- // nobody holds and nobody will ever stop.
134
- witness.holdRing();
135
- witness.releaseRing();
136
- await waitFor(
137
- () => spawn.rings.every((child) => child.killed),
138
- "any ring started mid-flight to be stopped"
139
- );
140
- const alive = spawn.rings.filter((child) => !child.killed);
141
- assert.deepEqual(alive, [], "no ring may outlive the channels that wanted it");
142
- });
143
- });
144
-
145
- test("the ring's files are removed once nobody is being served", async () => {
146
- await withDir(async (dir) => {
147
- const spawn = makeSpawn();
148
- const witness = createPacketWitness({
149
- log: () => {},
150
- dir,
151
- port: 9090,
152
- spawnProcess: spawn.spawnProcess
153
- });
154
- witness.holdRing();
155
- await waitFor(() => spawn.rings.length === 1, "the ring to start");
156
- await writeFile(path.join(dir, `${WITNESS_RING_BASENAME}0`), "pcap");
157
- witness.releaseRing();
158
- await waitFor(async () => (await readdir(dir)).length === 0, "the ring files to be removed");
159
- });
160
- });
161
-
162
- test("a wedge keeps the ring's history, and the ring keeps recording afterwards", async () => {
163
- await withDir(async (dir) => {
164
- const spawn = makeSpawn();
165
- const lines = [];
166
- const witness = createPacketWitness({
167
- log: (message) => lines.push(message),
168
- dir,
169
- port: 9090,
170
- spawnProcess: spawn.spawnProcess
171
- });
172
- witness.holdRing();
173
- await waitFor(() => spawn.rings.length === 1, "the ring to start");
174
- await writeFile(path.join(dir, `${WITNESS_RING_BASENAME}0`), "before-the-freeze");
175
- await writeFile(path.join(dir, `${WITNESS_RING_BASENAME}1`), "also-before");
176
-
177
- const started = witness.maybeCapture({
178
- sessionId: "68296f7d-0000-0000-0000-000000000000",
179
- tag: "68296f7d",
180
- label: "proxy",
181
- remote: { address: "2001:db8::1", port: 61649 },
182
- queuedBytes: 67_372_267,
183
- stuckForMs: 4000
184
- });
185
- assert.equal(started, true);
186
- await waitFor(
187
- async () => (await readdir(dir)).filter((name) => name.includes(".before")).length === 2,
188
- "both ring files to be copied aside"
189
- );
190
-
191
- const kept = (await readdir(dir)).filter((name) => name.includes(".before"));
192
- assert.equal(kept.length, 2, "both ring files must be kept, not just the finished one");
193
- assert.ok(kept.every((name) => name.startsWith("packet-witness.68296f7d.")));
194
- // Stopped to flush, then started again: two ring processes over the episode.
195
- await waitFor(() => spawn.rings.length >= 2, "the ring to resume after the copy");
196
- assert.equal(spawn.rings.at(-1).killed, false);
197
- assert.ok(lines.some((line) => line.includes("kept 2 ring file(s)")));
198
-
199
- // End the tail capture: it would otherwise sit out its whole window, and
200
- // its timer would hold this process open.
201
- for (const child of spawn.children) {
202
- child.kill("SIGTERM");
203
- }
204
- await settle();
205
- });
206
- });
207
-
208
- test("dispose stops the ring and clears its files", async () => {
209
- await withDir(async (dir) => {
210
- const spawn = makeSpawn();
211
- const witness = createPacketWitness({
212
- log: () => {},
213
- dir,
214
- port: 9090,
215
- spawnProcess: spawn.spawnProcess
216
- });
217
- witness.holdRing();
218
- await waitFor(() => spawn.rings.length === 1, "the ring to start");
219
- await writeFile(path.join(dir, `${WITNESS_RING_BASENAME}0`), "pcap");
220
- await witness.dispose();
221
- assert.equal(spawn.rings.at(-1).killed, true);
222
- assert.deepEqual(await readdir(dir), []);
223
- });
224
- });
225
-
226
- test("ring files left by a killed process are kept, not deleted by the next one", async () => {
227
- await withDir(async (dir) => {
228
- await writeFile(path.join(dir, `${WITNESS_RING_BASENAME}0`), "the seconds before the crash");
229
- await writeFile(path.join(dir, `${WITNESS_RING_BASENAME}2`), "and these");
230
- const adopted = await adoptOrphanRingFiles(dir);
231
- assert.equal(adopted.length, 2);
232
- const left = await readdir(dir);
233
- assert.equal(left.filter((name) => name.startsWith(WITNESS_RING_BASENAME)).length, 0);
234
- assert.ok(left.every((name) => name.startsWith("packet-witness.orphan.")));
235
- });
236
- });
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { EventEmitter } from "node:events";
4
+ import { mkdtemp, readdir, rm, writeFile } from "node:fs/promises";
5
+ import os from "node:os";
6
+ import path from "node:path";
7
+
8
+ import {
9
+ adoptOrphanRingFiles,
10
+ createPacketWitness,
11
+ WITNESS_RING_BASENAME
12
+ } from "../services/packet-witness.js";
13
+
14
+ /**
15
+ * A stand-in for a spawned tcpdump: it records how it was called and stays
16
+ * "running" until something kills it.
17
+ */
18
+ class FakeChild extends EventEmitter {
19
+ constructor(command, args) {
20
+ super();
21
+ this.command = command;
22
+ this.args = args;
23
+ this.killed = false;
24
+ this.signals = [];
25
+ }
26
+
27
+ kill(signal) {
28
+ this.signals.push(signal);
29
+ if (!this.killed) {
30
+ this.killed = true;
31
+ // A real child exits asynchronously, which is what the code must wait for.
32
+ setImmediate(() => this.emit("close", 0, signal));
33
+ }
34
+ return true;
35
+ }
36
+ }
37
+
38
+ /**
39
+ * @returns {{ spawnProcess: Function, children: FakeChild[], rings: FakeChild[] }}
40
+ */
41
+ function makeSpawn() {
42
+ const children = [];
43
+ const spawnProcess = (command, args) => {
44
+ const child = new FakeChild(command, args);
45
+ children.push(child);
46
+ if (args.includes("--version")) {
47
+ // The availability probe: answer at once, like a present tcpdump.
48
+ setImmediate(() => child.emit("spawn"));
49
+ }
50
+ return child;
51
+ };
52
+ return {
53
+ spawnProcess,
54
+ children,
55
+ get rings() {
56
+ return children.filter((child) => !child.args.includes("--version"));
57
+ }
58
+ };
59
+ }
60
+
61
+ /**
62
+ * Wait until `check` holds, rather than for a chosen interval.
63
+ *
64
+ * A fixed sleep passes alone and fails in a full run — the defect roadmap item
65
+ * 51 names — and the work here is several awaits deep (probe, stop, readdir,
66
+ * copy, restart), so its duration is whatever the machine is busy with.
67
+ *
68
+ * @param {() => boolean | Promise<boolean>} check
69
+ * @param {string} what
70
+ * @returns {Promise<void>}
71
+ */
72
+ async function waitFor(check, what) {
73
+ const deadline = Date.now() + 10_000;
74
+ for (;;) {
75
+ if (await check()) {
76
+ return;
77
+ }
78
+ if (Date.now() > deadline) {
79
+ throw new Error(`timed out waiting for ${what}`);
80
+ }
81
+ await new Promise((resolve) => setTimeout(resolve, 5));
82
+ }
83
+ }
84
+
85
+ /** Let every already-queued microtask and immediate run. */
86
+ const settle = () => new Promise((resolve) => setTimeout(resolve, 20));
87
+
88
+ async function withDir(run) {
89
+ const dir = await mkdtemp(path.join(os.tmpdir(), "witness-ring-"));
90
+ try {
91
+ await run(dir);
92
+ } finally {
93
+ await rm(dir, { recursive: true, force: true });
94
+ }
95
+ }
96
+
97
+ test("the ring runs while a channel is open and stops with the last one", async () => {
98
+ await withDir(async (dir) => {
99
+ const spawn = makeSpawn();
100
+ const witness = createPacketWitness({
101
+ log: () => {},
102
+ dir,
103
+ port: 9090,
104
+ spawnProcess: spawn.spawnProcess
105
+ });
106
+
107
+ witness.holdRing();
108
+ witness.holdRing();
109
+ await waitFor(() => spawn.rings.length === 1, "the ring to start");
110
+ assert.equal(spawn.rings.length, 1, "a second channel must not start a second ring");
111
+ assert.equal(spawn.rings[0].killed, false);
112
+
113
+ witness.releaseRing();
114
+ await settle();
115
+ assert.equal(spawn.rings[0].killed, false, "one channel left still wants the ring");
116
+
117
+ witness.releaseRing();
118
+ await waitFor(() => spawn.rings[0].killed, "the ring to stop");
119
+ });
120
+ });
121
+
122
+ test("a channel that opens and closes while the ring is starting leaves nothing running", async () => {
123
+ await withDir(async (dir) => {
124
+ const spawn = makeSpawn();
125
+ const witness = createPacketWitness({
126
+ log: () => {},
127
+ dir,
128
+ port: 9090,
129
+ spawnProcess: spawn.spawnProcess
130
+ });
131
+ // Release before the availability probe has resolved — the start is still
132
+ // in flight. Without the second look after the await this leaves a tcpdump
133
+ // nobody holds and nobody will ever stop.
134
+ witness.holdRing();
135
+ witness.releaseRing();
136
+ await waitFor(
137
+ () => spawn.rings.every((child) => child.killed),
138
+ "any ring started mid-flight to be stopped"
139
+ );
140
+ const alive = spawn.rings.filter((child) => !child.killed);
141
+ assert.deepEqual(alive, [], "no ring may outlive the channels that wanted it");
142
+ });
143
+ });
144
+
145
+ test("the ring's files are removed once nobody is being served", async () => {
146
+ await withDir(async (dir) => {
147
+ const spawn = makeSpawn();
148
+ const witness = createPacketWitness({
149
+ log: () => {},
150
+ dir,
151
+ port: 9090,
152
+ spawnProcess: spawn.spawnProcess
153
+ });
154
+ witness.holdRing();
155
+ await waitFor(() => spawn.rings.length === 1, "the ring to start");
156
+ await writeFile(path.join(dir, `${WITNESS_RING_BASENAME}0`), "pcap");
157
+ witness.releaseRing();
158
+ await waitFor(async () => (await readdir(dir)).length === 0, "the ring files to be removed");
159
+ });
160
+ });
161
+
162
+ test("a wedge keeps the ring's history, and the ring keeps recording afterwards", async () => {
163
+ await withDir(async (dir) => {
164
+ const spawn = makeSpawn();
165
+ const lines = [];
166
+ const witness = createPacketWitness({
167
+ log: (message) => lines.push(message),
168
+ dir,
169
+ port: 9090,
170
+ spawnProcess: spawn.spawnProcess
171
+ });
172
+ witness.holdRing();
173
+ await waitFor(() => spawn.rings.length === 1, "the ring to start");
174
+ await writeFile(path.join(dir, `${WITNESS_RING_BASENAME}0`), "before-the-freeze");
175
+ await writeFile(path.join(dir, `${WITNESS_RING_BASENAME}1`), "also-before");
176
+
177
+ const started = witness.maybeCapture({
178
+ sessionId: "68296f7d-0000-0000-0000-000000000000",
179
+ tag: "68296f7d",
180
+ label: "proxy",
181
+ remote: { address: "2001:db8::1", port: 61649 },
182
+ queuedBytes: 67_372_267,
183
+ stuckForMs: 4000
184
+ });
185
+ assert.equal(started, true);
186
+ await waitFor(
187
+ async () => (await readdir(dir)).filter((name) => name.includes(".before")).length === 2,
188
+ "both ring files to be copied aside"
189
+ );
190
+
191
+ const kept = (await readdir(dir)).filter((name) => name.includes(".before"));
192
+ assert.equal(kept.length, 2, "both ring files must be kept, not just the finished one");
193
+ assert.ok(kept.every((name) => name.startsWith("packet-witness.68296f7d.")));
194
+ // Stopped to flush, then started again: two ring processes over the episode.
195
+ await waitFor(() => spawn.rings.length >= 2, "the ring to resume after the copy");
196
+ assert.equal(spawn.rings.at(-1).killed, false);
197
+ assert.ok(lines.some((line) => line.includes("kept 2 ring file(s)")));
198
+
199
+ // End the tail capture: it would otherwise sit out its whole window, and
200
+ // its timer would hold this process open.
201
+ for (const child of spawn.children) {
202
+ child.kill("SIGTERM");
203
+ }
204
+ await settle();
205
+ });
206
+ });
207
+
208
+ test("dispose stops the ring and clears its files", async () => {
209
+ await withDir(async (dir) => {
210
+ const spawn = makeSpawn();
211
+ const witness = createPacketWitness({
212
+ log: () => {},
213
+ dir,
214
+ port: 9090,
215
+ spawnProcess: spawn.spawnProcess
216
+ });
217
+ witness.holdRing();
218
+ await waitFor(() => spawn.rings.length === 1, "the ring to start");
219
+ await writeFile(path.join(dir, `${WITNESS_RING_BASENAME}0`), "pcap");
220
+ await witness.dispose();
221
+ assert.equal(spawn.rings.at(-1).killed, true);
222
+ assert.deepEqual(await readdir(dir), []);
223
+ });
224
+ });
225
+
226
+ test("ring files left by a killed process are kept, not deleted by the next one", async () => {
227
+ await withDir(async (dir) => {
228
+ await writeFile(path.join(dir, `${WITNESS_RING_BASENAME}0`), "the seconds before the crash");
229
+ await writeFile(path.join(dir, `${WITNESS_RING_BASENAME}2`), "and these");
230
+ const adopted = await adoptOrphanRingFiles(dir);
231
+ assert.equal(adopted.length, 2);
232
+ const left = await readdir(dir);
233
+ assert.equal(left.filter((name) => name.startsWith(WITNESS_RING_BASENAME)).length, 0);
234
+ assert.ok(left.every((name) => name.startsWith("packet-witness.orphan.")));
235
+ });
236
+ });