@torrent-tv/proxy 2.80.19 → 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 (58) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/package.json +1 -1
  3. package/research/double-spawn-2026-09-10.md +171 -0
  4. package/services/disk/DiskSpace.js +146 -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 +138 -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,173 +1,173 @@
1
- /**
2
- * @file Reading an MP4's text subtitle track out of its sample table.
3
- *
4
- * The file is built here, so every answer is known in advance: two cues at
5
- * stated times, in stated places, with an empty sample between them — the way
6
- * the format says "nothing on screen just now".
7
- */
8
-
9
- import test from "node:test";
10
- import assert from "node:assert/strict";
11
- import { Mp4Container } from "../services/container/Mp4Container.js";
12
-
13
- function box(type, payload) {
14
- const header = Buffer.alloc(8);
15
- header.writeUInt32BE(payload.length + 8, 0);
16
- header.write(type, 4, "latin1");
17
- return Buffer.concat([header, payload]);
18
- }
19
-
20
- function fullBox(type, payload) {
21
- return box(type, Buffer.concat([Buffer.from([0, 0, 0, 0]), payload]));
22
- }
23
-
24
- function u32(...values) {
25
- const buffer = Buffer.alloc(values.length * 4);
26
- values.forEach((value, index) => buffer.writeUInt32BE(value, index * 4));
27
- return buffer;
28
- }
29
-
30
- /** A `tx3g` sample: a two-byte length, then the text. */
31
- function textSample(text) {
32
- const bytes = Buffer.from(text, "utf8");
33
- const length = Buffer.alloc(2);
34
- length.writeUInt16BE(bytes.length, 0);
35
- return Buffer.concat([length, bytes]);
36
- }
37
-
38
- /**
39
- * A file with one text track: three samples, the middle one empty.
40
- *
41
- * @param {number} [displayFlags] - The `tx3g` sample entry's display flags.
42
- * @param {boolean} [withFtyp] - Open the file with an `ftyp` box, which is what
43
- * the container layer sniffs for before it will read anything.
44
- * @returns {{ file: Buffer, samples: Buffer[] }}
45
- */
46
- function buildFile(displayFlags = 0, withFtyp = false) {
47
- const samples = [textSample("First line"), Buffer.alloc(2), textSample("Second line")];
48
- const timescale = 1000;
49
-
50
- const mdhd = fullBox("mdhd", Buffer.concat([
51
- u32(0, 0, timescale, 60_000),
52
- // Language "eng", five bits a letter offset from 0x60, then a spare word.
53
- Buffer.from([0x15, 0xc7, 0, 0])
54
- ]));
55
- const hdlr = fullBox("hdlr", Buffer.concat([u32(0), Buffer.from("sbtl", "latin1"), u32(0, 0, 0)]));
56
- const tkhd = fullBox("tkhd", u32(0, 0, 7, 0, 60_000));
57
-
58
- // A sample entry opens with 6 reserved bytes and a 2-byte data reference
59
- // index, and `displayFlags` is the 32 bits after them.
60
- const tx3g = Buffer.alloc(24);
61
- tx3g.writeUInt32BE(displayFlags >>> 0, 8);
62
- const stsd = fullBox("stsd", Buffer.concat([u32(1), box("tx3g", tx3g)]));
63
- // Two seconds a sample, so the cues sit at 0-2, 2-4 and 4-6 seconds.
64
- const stts = fullBox("stts", Buffer.concat([u32(1), u32(3, 2000)]));
65
- const stsz = fullBox("stsz", Buffer.concat([u32(0, 3), u32(...samples.map((s) => s.length))]));
66
- const stsc = fullBox("stsc", Buffer.concat([u32(1), u32(1, 3, 1)]));
67
-
68
- const stbl = box("stbl", Buffer.concat([stsd, stts, stsz, stsc, fullBox("stco", Buffer.concat([u32(1), u32(0)]))]));
69
- const minf = box("minf", stbl);
70
- const mdia = box("mdia", Buffer.concat([mdhd, hdlr, minf]));
71
- const trak = box("trak", Buffer.concat([tkhd, mdia]));
72
- const moovDraft = box("moov", trak);
73
-
74
- // The samples sit after moov, so the chunk offset is known only now. Its
75
- // length does not change when the placeholder becomes the real value.
76
- const ftyp = withFtyp
77
- ? box("ftyp", Buffer.concat([Buffer.from("isom", "latin1"), u32(512), Buffer.from("isom", "latin1")]))
78
- : Buffer.alloc(0);
79
- const mdatStart = ftyp.length + moovDraft.length + 8;
80
- const stcoReal = fullBox("stco", Buffer.concat([u32(1), u32(mdatStart)]));
81
- const stblReal = box("stbl", Buffer.concat([stsd, stts, stsz, stsc, stcoReal]));
82
- const moov = box("moov", box("trak", Buffer.concat([
83
- tkhd,
84
- box("mdia", Buffer.concat([mdhd, hdlr, box("minf", stblReal)]))
85
- ])));
86
- const mdat = box("mdat", Buffer.concat(samples));
87
- return { file: Buffer.concat([ftyp, moov, mdat]), samples };
88
- }
89
-
90
- function readerOver(file) {
91
- return async (start, end) => {
92
- const last = Math.min(end, file.length - 1);
93
- return start > last ? null : file.subarray(start, last + 1);
94
- };
95
- }
96
-
97
- test("a text track's cues are found with their times and their byte ranges", async () => {
98
- const { file, samples } = buildFile();
99
-
100
- const plan = await Mp4Container.readSubtitlePlan(readerOver(file), file.length);
101
-
102
- assert.equal(plan.tracks.length, 1);
103
- const track = plan.tracks[0];
104
- assert.equal(track.format, "tx3g");
105
- assert.equal(track.language, "eng");
106
- assert.equal(track.samples.length, 2, "the empty sample is a gap, not a cue");
107
- assert.deepEqual(
108
- track.samples.map((sample) => [sample.startSeconds, sample.endSeconds]),
109
- [[0, 2], [4, 6]],
110
- "times come from the duration table, and the gap keeps its place in it"
111
- );
112
- // The bytes the plan points at are the ones that hold the text.
113
- const first = file.subarray(track.samples[0].offset, track.samples[0].offset + track.samples[0].size);
114
- assert.equal(Mp4Container.cueTextOf(first, "tx3g"), "First line");
115
- const second = file.subarray(track.samples[1].offset, track.samples[1].offset + track.samples[1].size);
116
- assert.equal(Mp4Container.cueTextOf(second, "tx3g"), "Second line");
117
- assert.equal(second.length, samples[2].length);
118
- });
119
-
120
- test("a file with no text track offers nothing", async () => {
121
- const moov = box("moov", box("trak", box("mdia", Buffer.concat([
122
- fullBox("mdhd", Buffer.concat([u32(0, 0, 1000, 100), Buffer.from([0, 0, 0, 0])])),
123
- fullBox("hdlr", Buffer.concat([u32(0), Buffer.from("vide", "latin1"), u32(0, 0, 0)]))
124
- ]))));
125
- const file = Buffer.concat([moov, box("mdat", Buffer.alloc(4))]);
126
-
127
- const plan = await Mp4Container.readSubtitlePlan(readerOver(file), file.length);
128
-
129
- assert.deepEqual(plan.tracks, []);
130
- });
131
-
132
- test("a WebVTT sample gives up the text inside its payload box", () => {
133
- const payl = box("payl", Buffer.from("Hello there", "utf8"));
134
- const sample = box("vttc", payl);
135
-
136
- assert.equal(Mp4Container.cueTextOf(sample, "wvtt"), "Hello there");
137
- });
138
-
139
- test("the sample entry's display flags say whether the track is forced", async () => {
140
- // Apple's QuickTime File Format, "Display flags" under Subtitle sample
141
- // description: `0x40000000` "Some samples are forced", `0x80000000` "All
142
- // samples are forced", and setting the second requires the first — together
143
- // `0xC0000000`. `0x20000000` is vertical placement and says nothing about
144
- // forcing.
145
- const readFlags = async (displayFlags) => {
146
- const { file } = buildFile(displayFlags);
147
- const plan = await Mp4Container.readSubtitlePlan(readerOver(file), file.length);
148
- const track = plan.tracks[0];
149
- return [track.someSamplesForced, track.allSamplesForced];
150
- };
151
-
152
- assert.deepEqual(await readFlags(0), [false, false]);
153
- assert.deepEqual(await readFlags(0x40000000), [true, false]);
154
- assert.deepEqual(await readFlags(0xc0000000), [true, true]);
155
- // A writer that set only the second bit still means the track is forced.
156
- assert.deepEqual(await readFlags(0x80000000), [false, true]);
157
- // Vertical placement is a different field's business.
158
- assert.deepEqual(await readFlags(0x20000000), [false, false]);
159
- });
160
-
161
- test("a forced sample entry reaches the track the viewer is offered", async () => {
162
- const forced = buildFile(0xc0000000, true).file;
163
- const plain = buildFile(0, true).file;
164
-
165
- const trackOf = async (file) => {
166
- const container = new Mp4Container({ readRange: readerOver(file), fileSize: file.length });
167
- const tracks = await container.readTracks();
168
- return tracks.find((track) => track.type === "subtitle");
169
- };
170
-
171
- assert.equal((await trackOf(forced))?.isForced, true);
172
- assert.equal((await trackOf(plain))?.isForced, false);
173
- });
1
+ /**
2
+ * @file Reading an MP4's text subtitle track out of its sample table.
3
+ *
4
+ * The file is built here, so every answer is known in advance: two cues at
5
+ * stated times, in stated places, with an empty sample between them — the way
6
+ * the format says "nothing on screen just now".
7
+ */
8
+
9
+ import test from "node:test";
10
+ import assert from "node:assert/strict";
11
+ import { Mp4Container } from "../services/container/Mp4Container.js";
12
+
13
+ function box(type, payload) {
14
+ const header = Buffer.alloc(8);
15
+ header.writeUInt32BE(payload.length + 8, 0);
16
+ header.write(type, 4, "latin1");
17
+ return Buffer.concat([header, payload]);
18
+ }
19
+
20
+ function fullBox(type, payload) {
21
+ return box(type, Buffer.concat([Buffer.from([0, 0, 0, 0]), payload]));
22
+ }
23
+
24
+ function u32(...values) {
25
+ const buffer = Buffer.alloc(values.length * 4);
26
+ values.forEach((value, index) => buffer.writeUInt32BE(value, index * 4));
27
+ return buffer;
28
+ }
29
+
30
+ /** A `tx3g` sample: a two-byte length, then the text. */
31
+ function textSample(text) {
32
+ const bytes = Buffer.from(text, "utf8");
33
+ const length = Buffer.alloc(2);
34
+ length.writeUInt16BE(bytes.length, 0);
35
+ return Buffer.concat([length, bytes]);
36
+ }
37
+
38
+ /**
39
+ * A file with one text track: three samples, the middle one empty.
40
+ *
41
+ * @param {number} [displayFlags] - The `tx3g` sample entry's display flags.
42
+ * @param {boolean} [withFtyp] - Open the file with an `ftyp` box, which is what
43
+ * the container layer sniffs for before it will read anything.
44
+ * @returns {{ file: Buffer, samples: Buffer[] }}
45
+ */
46
+ function buildFile(displayFlags = 0, withFtyp = false) {
47
+ const samples = [textSample("First line"), Buffer.alloc(2), textSample("Second line")];
48
+ const timescale = 1000;
49
+
50
+ const mdhd = fullBox("mdhd", Buffer.concat([
51
+ u32(0, 0, timescale, 60_000),
52
+ // Language "eng", five bits a letter offset from 0x60, then a spare word.
53
+ Buffer.from([0x15, 0xc7, 0, 0])
54
+ ]));
55
+ const hdlr = fullBox("hdlr", Buffer.concat([u32(0), Buffer.from("sbtl", "latin1"), u32(0, 0, 0)]));
56
+ const tkhd = fullBox("tkhd", u32(0, 0, 7, 0, 60_000));
57
+
58
+ // A sample entry opens with 6 reserved bytes and a 2-byte data reference
59
+ // index, and `displayFlags` is the 32 bits after them.
60
+ const tx3g = Buffer.alloc(24);
61
+ tx3g.writeUInt32BE(displayFlags >>> 0, 8);
62
+ const stsd = fullBox("stsd", Buffer.concat([u32(1), box("tx3g", tx3g)]));
63
+ // Two seconds a sample, so the cues sit at 0-2, 2-4 and 4-6 seconds.
64
+ const stts = fullBox("stts", Buffer.concat([u32(1), u32(3, 2000)]));
65
+ const stsz = fullBox("stsz", Buffer.concat([u32(0, 3), u32(...samples.map((s) => s.length))]));
66
+ const stsc = fullBox("stsc", Buffer.concat([u32(1), u32(1, 3, 1)]));
67
+
68
+ const stbl = box("stbl", Buffer.concat([stsd, stts, stsz, stsc, fullBox("stco", Buffer.concat([u32(1), u32(0)]))]));
69
+ const minf = box("minf", stbl);
70
+ const mdia = box("mdia", Buffer.concat([mdhd, hdlr, minf]));
71
+ const trak = box("trak", Buffer.concat([tkhd, mdia]));
72
+ const moovDraft = box("moov", trak);
73
+
74
+ // The samples sit after moov, so the chunk offset is known only now. Its
75
+ // length does not change when the placeholder becomes the real value.
76
+ const ftyp = withFtyp
77
+ ? box("ftyp", Buffer.concat([Buffer.from("isom", "latin1"), u32(512), Buffer.from("isom", "latin1")]))
78
+ : Buffer.alloc(0);
79
+ const mdatStart = ftyp.length + moovDraft.length + 8;
80
+ const stcoReal = fullBox("stco", Buffer.concat([u32(1), u32(mdatStart)]));
81
+ const stblReal = box("stbl", Buffer.concat([stsd, stts, stsz, stsc, stcoReal]));
82
+ const moov = box("moov", box("trak", Buffer.concat([
83
+ tkhd,
84
+ box("mdia", Buffer.concat([mdhd, hdlr, box("minf", stblReal)]))
85
+ ])));
86
+ const mdat = box("mdat", Buffer.concat(samples));
87
+ return { file: Buffer.concat([ftyp, moov, mdat]), samples };
88
+ }
89
+
90
+ function readerOver(file) {
91
+ return async (start, end) => {
92
+ const last = Math.min(end, file.length - 1);
93
+ return start > last ? null : file.subarray(start, last + 1);
94
+ };
95
+ }
96
+
97
+ test("a text track's cues are found with their times and their byte ranges", async () => {
98
+ const { file, samples } = buildFile();
99
+
100
+ const plan = await Mp4Container.readSubtitlePlan(readerOver(file), file.length);
101
+
102
+ assert.equal(plan.tracks.length, 1);
103
+ const track = plan.tracks[0];
104
+ assert.equal(track.format, "tx3g");
105
+ assert.equal(track.language, "eng");
106
+ assert.equal(track.samples.length, 2, "the empty sample is a gap, not a cue");
107
+ assert.deepEqual(
108
+ track.samples.map((sample) => [sample.startSeconds, sample.endSeconds]),
109
+ [[0, 2], [4, 6]],
110
+ "times come from the duration table, and the gap keeps its place in it"
111
+ );
112
+ // The bytes the plan points at are the ones that hold the text.
113
+ const first = file.subarray(track.samples[0].offset, track.samples[0].offset + track.samples[0].size);
114
+ assert.equal(Mp4Container.cueTextOf(first, "tx3g"), "First line");
115
+ const second = file.subarray(track.samples[1].offset, track.samples[1].offset + track.samples[1].size);
116
+ assert.equal(Mp4Container.cueTextOf(second, "tx3g"), "Second line");
117
+ assert.equal(second.length, samples[2].length);
118
+ });
119
+
120
+ test("a file with no text track offers nothing", async () => {
121
+ const moov = box("moov", box("trak", box("mdia", Buffer.concat([
122
+ fullBox("mdhd", Buffer.concat([u32(0, 0, 1000, 100), Buffer.from([0, 0, 0, 0])])),
123
+ fullBox("hdlr", Buffer.concat([u32(0), Buffer.from("vide", "latin1"), u32(0, 0, 0)]))
124
+ ]))));
125
+ const file = Buffer.concat([moov, box("mdat", Buffer.alloc(4))]);
126
+
127
+ const plan = await Mp4Container.readSubtitlePlan(readerOver(file), file.length);
128
+
129
+ assert.deepEqual(plan.tracks, []);
130
+ });
131
+
132
+ test("a WebVTT sample gives up the text inside its payload box", () => {
133
+ const payl = box("payl", Buffer.from("Hello there", "utf8"));
134
+ const sample = box("vttc", payl);
135
+
136
+ assert.equal(Mp4Container.cueTextOf(sample, "wvtt"), "Hello there");
137
+ });
138
+
139
+ test("the sample entry's display flags say whether the track is forced", async () => {
140
+ // Apple's QuickTime File Format, "Display flags" under Subtitle sample
141
+ // description: `0x40000000` "Some samples are forced", `0x80000000` "All
142
+ // samples are forced", and setting the second requires the first — together
143
+ // `0xC0000000`. `0x20000000` is vertical placement and says nothing about
144
+ // forcing.
145
+ const readFlags = async (displayFlags) => {
146
+ const { file } = buildFile(displayFlags);
147
+ const plan = await Mp4Container.readSubtitlePlan(readerOver(file), file.length);
148
+ const track = plan.tracks[0];
149
+ return [track.someSamplesForced, track.allSamplesForced];
150
+ };
151
+
152
+ assert.deepEqual(await readFlags(0), [false, false]);
153
+ assert.deepEqual(await readFlags(0x40000000), [true, false]);
154
+ assert.deepEqual(await readFlags(0xc0000000), [true, true]);
155
+ // A writer that set only the second bit still means the track is forced.
156
+ assert.deepEqual(await readFlags(0x80000000), [false, true]);
157
+ // Vertical placement is a different field's business.
158
+ assert.deepEqual(await readFlags(0x20000000), [false, false]);
159
+ });
160
+
161
+ test("a forced sample entry reaches the track the viewer is offered", async () => {
162
+ const forced = buildFile(0xc0000000, true).file;
163
+ const plain = buildFile(0, true).file;
164
+
165
+ const trackOf = async (file) => {
166
+ const container = new Mp4Container({ readRange: readerOver(file), fileSize: file.length });
167
+ const tracks = await container.readTracks();
168
+ return tracks.find((track) => track.type === "subtitle");
169
+ };
170
+
171
+ assert.equal((await trackOf(forced))?.isForced, true);
172
+ assert.equal((await trackOf(plain))?.isForced, false);
173
+ });