@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.
- package/CHANGELOG.md +22 -0
- package/docs/encode-architecture.md +51 -2
- package/package.json +1 -1
- package/research/double-spawn-2026-09-10.md +171 -0
- package/services/disk/DiskSpace.js +146 -0
- package/services/disk/wire.js +60 -0
- package/services/encode/EncodeRun.js +37 -9
- package/services/encode/SegmentStore.js +284 -232
- package/services/encode/run-command.js +16 -1
- package/services/hls-session-manager.js +31 -128
- package/services/orchestrators/EncodeOrchestrator.js +52 -38
- package/services/piece-store/allowance.js +107 -0
- package/services/piece-store/piece-disk-store.js +365 -0
- package/services/piece-store/shared-piece-store.js +1549 -1535
- package/services/segment-formats/fmp4.js +54 -0
- package/services/segment-formats/mpegts.js +54 -0
- package/services/torrent-worker/client.js +32 -0
- package/services/torrent-worker/pool-adapter.js +15 -0
- package/services/torrent-worker/protocol.js +9 -0
- package/services/torrent-worker/worker.js +8 -1
- package/services/viewer/positions.js +48 -0
- package/test/audio-inventory.test.js +176 -176
- package/test/auto-quality-step.test.js +514 -514
- package/test/concurrent-cost.test.js +138 -138
- package/test/coverage-follows-the-disk.test.js +191 -187
- package/test/coverage-map.test.js +195 -195
- package/test/declared-tracks.test.js +35 -35
- package/test/disk-space.test.js +138 -0
- package/test/encode-orchestrator.test.js +0 -3
- package/test/encode-run.test.js +5 -12
- package/test/held-request-width.test.js +155 -155
- package/test/helpers/encode-run.js +2 -2
- package/test/matroska-blocks.test.js +0 -0
- package/test/matroska-cues-track.test.js +192 -192
- package/test/mp4-composition-times.test.js +0 -0
- package/test/mp4-subtitles.test.js +173 -173
- package/test/one-authority.test.js +281 -220
- package/test/orchestrator-wired.test.js +199 -195
- package/test/packet-witness-ring.test.js +236 -236
- package/test/packet-witness.test.js +148 -148
- package/test/piece-disk-store.test.js +267 -0
- package/test/piece-reader.test.js +4 -4
- package/test/piece-store-eviction.test.js +17 -17
- package/test/piece-store-reservations.test.js +20 -1
- package/test/piece-store-slow-disk.test.js +16 -1
- package/test/produced-copy-choice.test.js +258 -358
- package/test/read-window.test.js +6 -6
- package/test/run-intervals.test.js +100 -100
- package/test/seek-landing.test.js +109 -109
- package/test/segment-serve-wiring.test.js +8 -9
- package/test/segment-store-eviction.test.js +232 -0
- package/test/segment-store.test.js +238 -216
- package/test/segments-are-shared.test.js +1 -1
- package/test/shared-piece-store.test.js +12 -12
- package/test/sidecar-naming.test.js +142 -142
- package/test/subtitle-cue-framing.test.js +200 -200
- package/test/subtitle-cue-walk.test.js +369 -369
- package/test/subtitle-defaults.test.js +97 -97
- package/test/subtitle-track-numbering.test.js +370 -370
- package/test/tail-duplication.test.js +167 -167
- package/test/tracks-begin-together.test.js +195 -195
- package/test/two-viewers-one-picture.test.js +374 -374
- package/test/video-facts.test.js +102 -102
- package/test/wedge-certainty.test.js +131 -131
- package/services/encode/open-piece.js +0 -135
- package/services/piece-store/disk-tier.js +0 -151
- 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
|
+
});
|