@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
@@ -1,167 +1,167 @@
1
- /**
2
- * @file Asking a second wire for the blocks a reader is still waiting on.
3
- *
4
- * Measured on a real swarm 2026-08-19: a blocked read's tail is 2-14 blocks of
5
- * 512, and it sits on wires at 109-886 KB/s — above the 48 KB/s gate that
6
- * WebTorrent's own `_hotswap` requires, so the library never duplicates them.
7
- * The mechanism is the library's: free the reservation with `Piece.cancel` and
8
- * leave the first request in flight, then ask a second wire through the
9
- * library's own request path.
10
- */
11
-
12
- import test from "node:test";
13
- import assert from "node:assert/strict";
14
- import { duplicateTailFor } from "../services/torrent-worker/fastest-wires.js";
15
-
16
- /** A piece with `chunks` blocks, of which `missing` at the end are absent. */
17
- function piece(chunks, missing) {
18
- const buffer = new Array(chunks);
19
- for (let index = 0; index < chunks - missing; index += 1) {
20
- buffer[index] = new Uint8Array(1);
21
- }
22
- const cancelled = [];
23
- return {
24
- _buffer: buffer,
25
- _chunks: chunks,
26
- cancel: (index) => cancelled.push(index),
27
- cancelled
28
- };
29
- }
30
-
31
- function wire({ speed = 500_000, has = true } = {}) {
32
- return {
33
- peerPieces: { get: () => has },
34
- downloadSpeed: () => speed,
35
- peerChoking: false,
36
- destroyed: false,
37
- requests: []
38
- };
39
- }
40
-
41
- test("the missing blocks are freed and asked of a second wire", () => {
42
- const target = piece(512, 3);
43
- const placed = [];
44
- const torrent = {
45
- pieces: [target],
46
- wires: [wire({ speed: 900_000 }), wire({ speed: 800_000 }), wire({ speed: 700_000 })],
47
- _request: (wireAsked, index) => {
48
- placed.push({ wireAsked, index });
49
- return true;
50
- }
51
- };
52
-
53
- const result = duplicateTailFor(torrent, 0);
54
-
55
- assert.equal(result.missing, 3, "the tail is what is still absent from the piece");
56
- assert.equal(result.duplicated, 3);
57
- assert.deepEqual(target.cancelled, [509, 510, 511], "each block's reservation is freed first");
58
- assert.equal(placed.length, 3, "and each is then asked of a wire through the library's own path");
59
- assert.equal(placed[0].wireAsked, torrent.wires[0], "fastest wire first");
60
- });
61
-
62
- test("at most one duplicate per wire, however long the tail", () => {
63
- const target = piece(512, 16);
64
- const torrent = {
65
- pieces: [target],
66
- wires: [wire(), wire()],
67
- _request: () => true
68
- };
69
-
70
- const result = duplicateTailFor(torrent, 0);
71
-
72
- assert.equal(result.duplicated, 2, "two wires, two duplicates — the rest waits for the next attempt");
73
- assert.equal(target.cancelled.length, 2, "and no reservation is freed that nobody was asked for");
74
- });
75
-
76
- test("a piece still arriving normally is not duplicated at all", () => {
77
- // Forty blocks outstanding is not a tail, it is a piece in transit. Asking
78
- // for a second copy of it would spend the shared link on bytes that are
79
- // already on their way — which is the whole difference between this and the
80
- // 2-14 blocks a blocked reader was measured waiting on.
81
- const target = piece(512, 40);
82
- const torrent = {
83
- pieces: [target],
84
- wires: [wire(), wire()],
85
- _request: () => true
86
- };
87
-
88
- const result = duplicateTailFor(torrent, 0);
89
-
90
- assert.equal(result.duplicated, 0);
91
- assert.equal(target.cancelled.length, 0, "and nothing is freed either");
92
- });
93
-
94
- test("a wire whose pipeline is full does not end the attempt", () => {
95
- // Pipelines are per wire, so one wire being full says nothing about the next.
96
- // This used to stop the whole pass on the first refusal, on the stated
97
- // reasoning that the remaining wires were "no emptier" — an assumption about
98
- // other peers' queues that nothing measures.
99
- const target = piece(512, 5);
100
- const torrent = {
101
- pieces: [target],
102
- wires: [wire(), wire(), wire()],
103
- // The library refuses when the wire already has as many requests as its
104
- // measured speed justifies.
105
- _request: () => false
106
- };
107
-
108
- const result = duplicateTailFor(torrent, 0);
109
-
110
- assert.equal(result.duplicated, 0);
111
- assert.equal(
112
- target.cancelled.length,
113
- 3,
114
- "every wire was offered a block; a freed reservation nobody took is handed to whoever asks next"
115
- );
116
- });
117
-
118
- test("a refusal by one wire does not cost the block a faster wire would have taken", () => {
119
- const target = piece(512, 3);
120
- const placed = [];
121
- const torrent = {
122
- pieces: [target],
123
- wires: [wire({ speed: 900_000 }), wire({ speed: 800_000 })],
124
- _request: (wireAsked, index) => {
125
- // The first wire is full; the second is not.
126
- if (wireAsked === torrent.wires[0]) {
127
- return false;
128
- }
129
- placed.push(index);
130
- return true;
131
- }
132
- };
133
-
134
- const result = duplicateTailFor(torrent, 0);
135
-
136
- assert.equal(result.duplicated, 1);
137
- assert.deepEqual(placed, [0], "the second wire was still asked");
138
- });
139
-
140
- test("a piece with nothing missing is left alone", () => {
141
- const target = piece(4, 0);
142
- const torrent = { pieces: [target], wires: [wire()], _request: () => true };
143
-
144
- const result = duplicateTailFor(torrent, 0);
145
-
146
- assert.equal(result.duplicated, 0);
147
- assert.equal(result.missing, 0);
148
- assert.equal(target.cancelled.length, 0);
149
- });
150
-
151
- test("with no wire holding the piece there is nothing to duplicate onto", () => {
152
- const target = piece(8, 2);
153
- const torrent = { pieces: [target], wires: [wire({ has: false })], _request: () => true };
154
-
155
- const result = duplicateTailFor(torrent, 0);
156
-
157
- assert.equal(result.wires, 0);
158
- assert.equal(result.duplicated, 0);
159
- assert.equal(target.cancelled.length, 0, "and no reservation is freed for nobody");
160
- });
161
-
162
- test("a completed piece is not touched", () => {
163
- assert.deepEqual(
164
- duplicateTailFor({ pieces: [null], wires: [wire()], _request: () => true }, 0),
165
- { duplicated: 0, missing: 0, wires: 0 }
166
- );
167
- });
1
+ /**
2
+ * @file Asking a second wire for the blocks a reader is still waiting on.
3
+ *
4
+ * Measured on a real swarm 2026-08-19: a blocked read's tail is 2-14 blocks of
5
+ * 512, and it sits on wires at 109-886 KB/s — above the 48 KB/s gate that
6
+ * WebTorrent's own `_hotswap` requires, so the library never duplicates them.
7
+ * The mechanism is the library's: free the reservation with `Piece.cancel` and
8
+ * leave the first request in flight, then ask a second wire through the
9
+ * library's own request path.
10
+ */
11
+
12
+ import test from "node:test";
13
+ import assert from "node:assert/strict";
14
+ import { duplicateTailFor } from "../services/torrent-worker/fastest-wires.js";
15
+
16
+ /** A piece with `chunks` blocks, of which `missing` at the end are absent. */
17
+ function piece(chunks, missing) {
18
+ const buffer = new Array(chunks);
19
+ for (let index = 0; index < chunks - missing; index += 1) {
20
+ buffer[index] = new Uint8Array(1);
21
+ }
22
+ const cancelled = [];
23
+ return {
24
+ _buffer: buffer,
25
+ _chunks: chunks,
26
+ cancel: (index) => cancelled.push(index),
27
+ cancelled
28
+ };
29
+ }
30
+
31
+ function wire({ speed = 500_000, has = true } = {}) {
32
+ return {
33
+ peerPieces: { get: () => has },
34
+ downloadSpeed: () => speed,
35
+ peerChoking: false,
36
+ destroyed: false,
37
+ requests: []
38
+ };
39
+ }
40
+
41
+ test("the missing blocks are freed and asked of a second wire", () => {
42
+ const target = piece(512, 3);
43
+ const placed = [];
44
+ const torrent = {
45
+ pieces: [target],
46
+ wires: [wire({ speed: 900_000 }), wire({ speed: 800_000 }), wire({ speed: 700_000 })],
47
+ _request: (wireAsked, index) => {
48
+ placed.push({ wireAsked, index });
49
+ return true;
50
+ }
51
+ };
52
+
53
+ const result = duplicateTailFor(torrent, 0);
54
+
55
+ assert.equal(result.missing, 3, "the tail is what is still absent from the piece");
56
+ assert.equal(result.duplicated, 3);
57
+ assert.deepEqual(target.cancelled, [509, 510, 511], "each block's reservation is freed first");
58
+ assert.equal(placed.length, 3, "and each is then asked of a wire through the library's own path");
59
+ assert.equal(placed[0].wireAsked, torrent.wires[0], "fastest wire first");
60
+ });
61
+
62
+ test("at most one duplicate per wire, however long the tail", () => {
63
+ const target = piece(512, 16);
64
+ const torrent = {
65
+ pieces: [target],
66
+ wires: [wire(), wire()],
67
+ _request: () => true
68
+ };
69
+
70
+ const result = duplicateTailFor(torrent, 0);
71
+
72
+ assert.equal(result.duplicated, 2, "two wires, two duplicates — the rest waits for the next attempt");
73
+ assert.equal(target.cancelled.length, 2, "and no reservation is freed that nobody was asked for");
74
+ });
75
+
76
+ test("a piece still arriving normally is not duplicated at all", () => {
77
+ // Forty blocks outstanding is not a tail, it is a piece in transit. Asking
78
+ // for a second copy of it would spend the shared link on bytes that are
79
+ // already on their way — which is the whole difference between this and the
80
+ // 2-14 blocks a blocked reader was measured waiting on.
81
+ const target = piece(512, 40);
82
+ const torrent = {
83
+ pieces: [target],
84
+ wires: [wire(), wire()],
85
+ _request: () => true
86
+ };
87
+
88
+ const result = duplicateTailFor(torrent, 0);
89
+
90
+ assert.equal(result.duplicated, 0);
91
+ assert.equal(target.cancelled.length, 0, "and nothing is freed either");
92
+ });
93
+
94
+ test("a wire whose pipeline is full does not end the attempt", () => {
95
+ // Pipelines are per wire, so one wire being full says nothing about the next.
96
+ // This used to stop the whole pass on the first refusal, on the stated
97
+ // reasoning that the remaining wires were "no emptier" — an assumption about
98
+ // other peers' queues that nothing measures.
99
+ const target = piece(512, 5);
100
+ const torrent = {
101
+ pieces: [target],
102
+ wires: [wire(), wire(), wire()],
103
+ // The library refuses when the wire already has as many requests as its
104
+ // measured speed justifies.
105
+ _request: () => false
106
+ };
107
+
108
+ const result = duplicateTailFor(torrent, 0);
109
+
110
+ assert.equal(result.duplicated, 0);
111
+ assert.equal(
112
+ target.cancelled.length,
113
+ 3,
114
+ "every wire was offered a block; a freed reservation nobody took is handed to whoever asks next"
115
+ );
116
+ });
117
+
118
+ test("a refusal by one wire does not cost the block a faster wire would have taken", () => {
119
+ const target = piece(512, 3);
120
+ const placed = [];
121
+ const torrent = {
122
+ pieces: [target],
123
+ wires: [wire({ speed: 900_000 }), wire({ speed: 800_000 })],
124
+ _request: (wireAsked, index) => {
125
+ // The first wire is full; the second is not.
126
+ if (wireAsked === torrent.wires[0]) {
127
+ return false;
128
+ }
129
+ placed.push(index);
130
+ return true;
131
+ }
132
+ };
133
+
134
+ const result = duplicateTailFor(torrent, 0);
135
+
136
+ assert.equal(result.duplicated, 1);
137
+ assert.deepEqual(placed, [0], "the second wire was still asked");
138
+ });
139
+
140
+ test("a piece with nothing missing is left alone", () => {
141
+ const target = piece(4, 0);
142
+ const torrent = { pieces: [target], wires: [wire()], _request: () => true };
143
+
144
+ const result = duplicateTailFor(torrent, 0);
145
+
146
+ assert.equal(result.duplicated, 0);
147
+ assert.equal(result.missing, 0);
148
+ assert.equal(target.cancelled.length, 0);
149
+ });
150
+
151
+ test("with no wire holding the piece there is nothing to duplicate onto", () => {
152
+ const target = piece(8, 2);
153
+ const torrent = { pieces: [target], wires: [wire({ has: false })], _request: () => true };
154
+
155
+ const result = duplicateTailFor(torrent, 0);
156
+
157
+ assert.equal(result.wires, 0);
158
+ assert.equal(result.duplicated, 0);
159
+ assert.equal(target.cancelled.length, 0, "and no reservation is freed for nobody");
160
+ });
161
+
162
+ test("a completed piece is not touched", () => {
163
+ assert.deepEqual(
164
+ duplicateTailFor({ pieces: [null], wires: [wire()], _request: () => true }, 0),
165
+ { duplicated: 0, missing: 0, wires: 0 }
166
+ );
167
+ });