@torrent-tv/proxy 2.89.5 → 2.89.6

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.
@@ -1,221 +1,222 @@
1
- /**
2
- * @file A piece short of a track is judged against ITS OWN header.
3
- *
4
- * The question "does this piece carry every track" has two possible answers and
5
- * they call for opposite actions, which is why asking it against the session's
6
- * cached header alone was not enough: a piece that disagrees with that header
7
- * may be short, or the header may be the odd one out. A self-contained piece
8
- * carries a `moov` of its own, so it can be compared with itself, and that
9
- * comparison tells the two apart without asking anything about encoders.
10
- *
11
- * WHAT THIS REPLACED. The action used to be chosen by "is any run of this
12
- * session alive?" — a question about the SESSION, asked about a fact of the
13
- * BYTES. A run anywhere in the film meant "still being written; waiting for
14
- * it", and nothing re-judged or re-made the piece afterwards, so one
15
- * misjudgement became permanent.
16
- *
17
- * Field 2026-09-14: `segment-00033.mp4` of a copied picture, already served
18
- * whole to one viewer at 5 099 421 bytes, was refused to a second viewer for
19
- * five minutes — four holds of sixty seconds each, then 503 — while the run in
20
- * force was at #192 and beyond. That viewer's picture stood still 196.2 s and
21
- * then a further 56.2 s.
22
- *
23
- * The run question is also unanswerable and does not need asking: a piece being
24
- * written is called `making-<tag>-NNNNN.mp4` and takes its served name by a
25
- * rename, which is atomic. A file under its served name is never being written.
26
- */
27
-
28
- import test from "node:test";
29
- import assert from "node:assert/strict";
30
- import { access, writeFile } from "node:fs/promises";
31
- import path from "node:path";
32
- import { SourceFile } from "../services/media/SourceFile.js";
33
- import { Timeline } from "../services/encode/output/Timeline.js";
34
- import { managerWithOwnStore } from "./helpers/manager.js";
35
- import { startRunOn } from "./helpers/encode-run.js";
36
- import { fmp4Format } from "../services/encode/segment-formats/fmp4.js";
37
- import { outputSpec } from "./helpers/output-spec.js";
38
-
39
- const SESSION_ID = "3333333344445555";
40
- const OUTPUT_KEY = "own-header:fmt=fmp4:grid=kf@0:video-only:v=0/copy";
41
- const MOVIE_TIMESCALE = 1000;
42
-
43
- /**
44
- * @param {string} type
45
- * @param {Buffer} body
46
- * @returns {Buffer}
47
- */
48
- function box(type, body) {
49
- const head = Buffer.alloc(8);
50
- head.writeUInt32BE(8 + body.length, 0);
51
- head.write(type, 4, "latin1");
52
- return Buffer.concat([head, body]);
53
- }
54
-
55
- /**
56
- * @param {number} trackId
57
- * @returns {Buffer}
58
- */
59
- function trak(trackId) {
60
- const tkhdBody = Buffer.alloc(84);
61
- tkhdBody.writeUInt32BE(trackId, 12);
62
- const mdhdBody = Buffer.alloc(20);
63
- mdhdBody.writeUInt32BE(90_000, 12);
64
- return box("trak", Buffer.concat([
65
- box("tkhd", tkhdBody),
66
- box("mdia", box("mdhd", mdhdBody))
67
- ]));
68
- }
69
-
70
- /**
71
- * A header declaring `tracks` tracks.
72
- *
73
- * @param {number} tracks
74
- * @returns {Buffer}
75
- */
76
- function moovOf(tracks) {
77
- const mvhdBody = Buffer.alloc(100);
78
- mvhdBody.writeUInt32BE(MOVIE_TIMESCALE, 12);
79
- const traks = [];
80
- for (let id = 1; id <= tracks; id += 1) {
81
- traks.push(trak(id));
82
- }
83
- return box("moov", Buffer.concat([box("mvhd", mvhdBody), ...traks]));
84
- }
85
-
86
- /**
87
- * @param {number} trackId
88
- * @returns {Buffer}
89
- */
90
- function traf(trackId) {
91
- const tfhdBody = Buffer.alloc(8);
92
- tfhdBody.writeUInt32BE(trackId, 4);
93
- const tfdtBody = Buffer.alloc(12);
94
- tfdtBody.writeUInt8(1, 0);
95
- tfdtBody.writeBigUInt64BE(0n, 4);
96
- return box("traf", Buffer.concat([box("tfhd", tfhdBody), box("tfdt", tfdtBody)]));
97
- }
98
-
99
- /**
100
- * A piece shaped like the `segment` muxer's output.
101
- *
102
- * @param {{ declares: number, carries: number }} shape - How many tracks its own
103
- * header declares, and how many its fragments actually carry. The two differ
104
- * only in a piece a run closed short.
105
- * @returns {Buffer}
106
- */
107
- function selfContainedPiece({ declares, carries }) {
108
- const trafs = [];
109
- for (let id = 1; id <= carries; id += 1) {
110
- trafs.push(traf(id));
111
- }
112
- return Buffer.concat([
113
- box("ftyp", Buffer.alloc(16, 0)),
114
- moovOf(declares),
115
- box("moof", Buffer.concat(trafs)),
116
- box("mdat", Buffer.alloc(64, 0x5a)),
117
- box("mfra", Buffer.alloc(24, 0))
118
- ]);
119
- }
120
-
121
- /**
122
- * A manager with one session whose first two pieces are already on disk.
123
- *
124
- * @param {{ piece: Buffer, sessionHeader: Buffer }} params - The piece written
125
- * for every number, and what the session has cached as its header.
126
- * @returns {Promise<{ manager: object, dirPath: string }>}
127
- */
128
- async function managerHolding({ piece, sessionHeader }) {
129
- const { manager } = managerWithOwnStore();
130
- manager.segmentStore.useFormat(OUTPUT_KEY, fmp4Format);
131
- const dirPath = manager.segmentStore.directoryFor(OUTPUT_KEY);
132
- await writeFile(path.join(dirPath, "segment-00000.mp4"), piece);
133
- await writeFile(path.join(dirPath, "segment-00001.mp4"), piece);
134
-
135
- const session = {
136
- id: SESSION_ID,
137
- spec: outputSpec({ transcodeVideo: false }),
138
- outputKey: OUTPUT_KEY,
139
- dirPath,
140
- timeline: new Timeline({ boundaries: [0, 12.5, 25, 37.5, 50, 62.5], cutGrid: "uniform" }),
141
- state: "ready",
142
- file: new SourceFile({ sourceKey: "source-1", fileIndex: 0, name: "video.mkv" })
143
- .learn({ durationSeconds: 62.5 }),
144
- get inputFile() { return this.file; },
145
- get audioFile() { return this.file; },
146
- lastAloneSpeed: 2,
147
- startedAt: Date.now(),
148
- lastAccessedAt: Date.now(),
149
- runs: new Set(),
150
- lastError: "",
151
- segmentCount: 5,
152
- segmentFormat: fmp4Format,
153
- useSyntheticPlaylist: true,
154
- playlistText: "#EXTM3U\n",
155
- initBytes: sessionHeader,
156
- waitEpoch: 0
157
- };
158
- manager.outputs.set(SESSION_ID, session);
159
- // No plan runs here: this file is about the path that answers a request.
160
- manager.encodeRuns.planEncodersNow = () => {};
161
- manager.encodeRuns.planEncodersSoon = () => {};
162
- // A LIVE RUN, deliberately. Under the rule this replaced, a live run anywhere
163
- // was what turned a refusal into a permanent one — so both cases below are
164
- // asked with one running, which is the state the field failure happened in.
165
- startRunOn(session, { from: 0, usesExplicitCuts: true, speedX: 2 });
166
- return { manager, dirPath };
167
- }
168
-
169
- test("a piece that agrees with its own header is served, whatever the session's header says", async () => {
170
- // Two tracks declared, two carried: whole by its own account. The session's
171
- // cached header declares three — the state that cannot be told from a short
172
- // piece without asking the piece itself.
173
- const piece = selfContainedPiece({ declares: 2, carries: 2 });
174
- const { manager, dirPath } = await managerHolding({
175
- piece,
176
- sessionHeader: moovOf(3)
177
- });
178
-
179
- const answer = await manager.serving.getFileStream(SESSION_ID, "segment-00000.mp4");
180
-
181
- assert.equal(answer.kind, "file", "the piece is whole and must reach the viewer");
182
- await access(path.join(dirPath, "segment-00000.mp4"));
183
- });
184
-
185
- test("a piece that disagrees with its own header is removed, so it can be made again", async () => {
186
- // Two tracks declared, one carried: closed short by a run that was killed
187
- // mid-piece, which is what this check exists to catch.
188
- const piece = selfContainedPiece({ declares: 2, carries: 1 });
189
- const { manager, dirPath } = await managerHolding({
190
- piece,
191
- sessionHeader: moovOf(2)
192
- });
193
-
194
- const answer = await manager.serving.getFileStream(SESSION_ID, "segment-00000.mp4");
195
-
196
- assert.equal(answer.kind, "warming-up", "a short piece is not servable");
197
- await assert.rejects(
198
- () => access(path.join(dirPath, "segment-00000.mp4")),
199
- /ENOENT/,
200
- "and it is removed, or nothing will ever make it again"
201
- );
202
- });
203
-
204
- test("a live run elsewhere in the film does not make a short piece permanent", async () => {
205
- const piece = selfContainedPiece({ declares: 2, carries: 1 });
206
- const { manager, dirPath } = await managerHolding({
207
- piece,
208
- sessionHeader: moovOf(2)
209
- });
210
- // The field state exactly: the run in force is hundreds of segments ahead of
211
- // the piece being asked for.
212
- startRunOn(manager.outputs.get(SESSION_ID), { from: 192, usesExplicitCuts: true, speedX: 2 });
213
-
214
- await manager.serving.getFileStream(SESSION_ID, "segment-00000.mp4");
215
-
216
- await assert.rejects(
217
- () => access(path.join(dirPath, "segment-00000.mp4")),
218
- /ENOENT/,
219
- "a run a thousand segments away says nothing about these bytes"
220
- );
221
- });
1
+ /**
2
+ * @file A piece short of a track is judged against ITS OWN header.
3
+ *
4
+ * The question "does this piece carry every track" has two possible answers and
5
+ * they call for opposite actions, which is why asking it against the session's
6
+ * cached header alone was not enough: a piece that disagrees with that header
7
+ * may be short, or the header may be the odd one out. A self-contained piece
8
+ * carries a `moov` of its own, so it can be compared with itself, and that
9
+ * comparison tells the two apart without asking anything about encoders.
10
+ *
11
+ * WHAT THIS REPLACED. The action used to be chosen by "is any run of this
12
+ * session alive?" — a question about the SESSION, asked about a fact of the
13
+ * BYTES. A run anywhere in the film meant "still being written; waiting for
14
+ * it", and nothing re-judged or re-made the piece afterwards, so one
15
+ * misjudgement became permanent.
16
+ *
17
+ * Field 2026-09-14: `segment-00033.mp4` of a copied picture, already served
18
+ * whole to one viewer at 5 099 421 bytes, was refused to a second viewer for
19
+ * five minutes — four holds of sixty seconds each, then 503 — while the run in
20
+ * force was at #192 and beyond. That viewer's picture stood still 196.2 s and
21
+ * then a further 56.2 s.
22
+ *
23
+ * The run question is also unanswerable and does not need asking: a piece being
24
+ * written is called `making-<tag>-NNNNN.mp4` and takes its served name by a
25
+ * rename, which is atomic. A file under its served name is never being written.
26
+ */
27
+
28
+ import test from "node:test";
29
+ import assert from "node:assert/strict";
30
+ import { access, writeFile } from "node:fs/promises";
31
+ import path from "node:path";
32
+ import { SourceFile } from "../services/media/SourceFile.js";
33
+ import { Timeline } from "../services/encode/output/Timeline.js";
34
+ import { managerWithOwnStore } from "./helpers/manager.js";
35
+ import { startRunOn } from "./helpers/encode-run.js";
36
+ import { fmp4Format } from "../services/encode/segment-formats/fmp4.js";
37
+ import { outputSpec } from "./helpers/output-spec.js";
38
+
39
+ const SESSION_ID = "3333333344445555";
40
+ const OUTPUT_KEY = "own-header:fmt=fmp4:grid=kf@0:video-only:v=0/copy";
41
+ const MOVIE_TIMESCALE = 1000;
42
+
43
+ /**
44
+ * @param {string} type
45
+ * @param {Buffer} body
46
+ * @returns {Buffer}
47
+ */
48
+ function box(type, body) {
49
+ const head = Buffer.alloc(8);
50
+ head.writeUInt32BE(8 + body.length, 0);
51
+ head.write(type, 4, "latin1");
52
+ return Buffer.concat([head, body]);
53
+ }
54
+
55
+ /**
56
+ * @param {number} trackId
57
+ * @returns {Buffer}
58
+ */
59
+ function trak(trackId) {
60
+ const tkhdBody = Buffer.alloc(84);
61
+ tkhdBody.writeUInt32BE(trackId, 12);
62
+ const mdhdBody = Buffer.alloc(20);
63
+ mdhdBody.writeUInt32BE(90_000, 12);
64
+ return box("trak", Buffer.concat([
65
+ box("tkhd", tkhdBody),
66
+ box("mdia", box("mdhd", mdhdBody))
67
+ ]));
68
+ }
69
+
70
+ /**
71
+ * A header declaring `tracks` tracks.
72
+ *
73
+ * @param {number} tracks
74
+ * @returns {Buffer}
75
+ */
76
+ function moovOf(tracks) {
77
+ const mvhdBody = Buffer.alloc(100);
78
+ mvhdBody.writeUInt32BE(MOVIE_TIMESCALE, 12);
79
+ const traks = [];
80
+ for (let id = 1; id <= tracks; id += 1) {
81
+ traks.push(trak(id));
82
+ }
83
+ return box("moov", Buffer.concat([box("mvhd", mvhdBody), ...traks]));
84
+ }
85
+
86
+ /**
87
+ * @param {number} trackId
88
+ * @returns {Buffer}
89
+ */
90
+ function traf(trackId) {
91
+ const tfhdBody = Buffer.alloc(8);
92
+ tfhdBody.writeUInt32BE(trackId, 4);
93
+ const tfdtBody = Buffer.alloc(12);
94
+ tfdtBody.writeUInt8(1, 0);
95
+ tfdtBody.writeBigUInt64BE(0n, 4);
96
+ return box("traf", Buffer.concat([box("tfhd", tfhdBody), box("tfdt", tfdtBody)]));
97
+ }
98
+
99
+ /**
100
+ * A piece shaped like the `segment` muxer's output.
101
+ *
102
+ * @param {{ declares: number, carries: number }} shape - How many tracks its own
103
+ * header declares, and how many its fragments actually carry. The two differ
104
+ * only in a piece a run closed short.
105
+ * @returns {Buffer}
106
+ */
107
+ function selfContainedPiece({ declares, carries }) {
108
+ const trafs = [];
109
+ for (let id = 1; id <= carries; id += 1) {
110
+ trafs.push(traf(id));
111
+ }
112
+ return Buffer.concat([
113
+ box("ftyp", Buffer.alloc(16, 0)),
114
+ moovOf(declares),
115
+ box("moof", Buffer.concat(trafs)),
116
+ box("mdat", Buffer.alloc(64, 0x5a)),
117
+ box("mfra", Buffer.alloc(24, 0))
118
+ ]);
119
+ }
120
+
121
+ /**
122
+ * A manager with one session whose first two pieces are already on disk.
123
+ *
124
+ * @param {{ piece: Buffer, sessionHeader: Buffer }} params - The piece written
125
+ * for every number, and what the session has cached as its header.
126
+ * @returns {Promise<{ manager: object, dirPath: string }>}
127
+ */
128
+ async function managerHolding({ piece, sessionHeader }) {
129
+ const { manager } = managerWithOwnStore();
130
+ manager.segmentStore.useFormat(OUTPUT_KEY, fmp4Format);
131
+ const dirPath = manager.segmentStore.directoryFor(OUTPUT_KEY);
132
+ await writeFile(path.join(dirPath, "segment-00000.mp4"), piece);
133
+ await writeFile(path.join(dirPath, "segment-00001.mp4"), piece);
134
+
135
+ const session = {
136
+ id: SESSION_ID,
137
+ spec: outputSpec({ transcodeVideo: false }),
138
+ outputKey: OUTPUT_KEY,
139
+ dirPath,
140
+ timeline: new Timeline({ boundaries: [0, 12.5, 25, 37.5, 50, 62.5], cutGrid: "uniform" }),
141
+ state: "ready",
142
+ file: new SourceFile({ sourceKey: "source-1", fileIndex: 0, name: "video.mkv" })
143
+ .learn({ durationSeconds: 62.5 }),
144
+ get inputFile() { return this.file; },
145
+ get audioFile() { return this.file; },
146
+ lastAloneSpeed: 2,
147
+ startedAt: Date.now(),
148
+ lastAccessedAt: Date.now(),
149
+ runs: new Set(),
150
+ lastError: "",
151
+ segmentCount: 5,
152
+ segmentFormat: fmp4Format,
153
+ useSyntheticPlaylist: true,
154
+ playlistText: "#EXTM3U\n",
155
+ waitEpoch: 0
156
+ };
157
+ manager.outputs.set(SESSION_ID, session);
158
+ // The header this output serves is the store's, one owner for it.
159
+ manager.segmentStore.keepInit(OUTPUT_KEY, sessionHeader);
160
+ // No plan runs here: this file is about the path that answers a request.
161
+ manager.encodeRuns.planEncodersNow = () => {};
162
+ manager.encodeRuns.planEncodersSoon = () => {};
163
+ // A LIVE RUN, deliberately. Under the rule this replaced, a live run anywhere
164
+ // was what turned a refusal into a permanent one — so both cases below are
165
+ // asked with one running, which is the state the field failure happened in.
166
+ startRunOn(session, { from: 0, usesExplicitCuts: true, speedX: 2 });
167
+ return { manager, dirPath };
168
+ }
169
+
170
+ test("a piece that agrees with its own header is served, whatever the session's header says", async () => {
171
+ // Two tracks declared, two carried: whole by its own account. The session's
172
+ // cached header declares three — the state that cannot be told from a short
173
+ // piece without asking the piece itself.
174
+ const piece = selfContainedPiece({ declares: 2, carries: 2 });
175
+ const { manager, dirPath } = await managerHolding({
176
+ piece,
177
+ sessionHeader: moovOf(3)
178
+ });
179
+
180
+ const answer = await manager.serving.getFileStream(SESSION_ID, "segment-00000.mp4");
181
+
182
+ assert.equal(answer.kind, "file", "the piece is whole and must reach the viewer");
183
+ await access(path.join(dirPath, "segment-00000.mp4"));
184
+ });
185
+
186
+ test("a piece that disagrees with its own header is removed, so it can be made again", async () => {
187
+ // Two tracks declared, one carried: closed short by a run that was killed
188
+ // mid-piece, which is what this check exists to catch.
189
+ const piece = selfContainedPiece({ declares: 2, carries: 1 });
190
+ const { manager, dirPath } = await managerHolding({
191
+ piece,
192
+ sessionHeader: moovOf(2)
193
+ });
194
+
195
+ const answer = await manager.serving.getFileStream(SESSION_ID, "segment-00000.mp4");
196
+
197
+ assert.equal(answer.kind, "warming-up", "a short piece is not servable");
198
+ await assert.rejects(
199
+ () => access(path.join(dirPath, "segment-00000.mp4")),
200
+ /ENOENT/,
201
+ "and it is removed, or nothing will ever make it again"
202
+ );
203
+ });
204
+
205
+ test("a live run elsewhere in the film does not make a short piece permanent", async () => {
206
+ const piece = selfContainedPiece({ declares: 2, carries: 1 });
207
+ const { manager, dirPath } = await managerHolding({
208
+ piece,
209
+ sessionHeader: moovOf(2)
210
+ });
211
+ // The field state exactly: the run in force is hundreds of segments ahead of
212
+ // the piece being asked for.
213
+ startRunOn(manager.outputs.get(SESSION_ID), { from: 192, usesExplicitCuts: true, speedX: 2 });
214
+
215
+ await manager.serving.getFileStream(SESSION_ID, "segment-00000.mp4");
216
+
217
+ await assert.rejects(
218
+ () => access(path.join(dirPath, "segment-00000.mp4")),
219
+ /ENOENT/,
220
+ "a run a thousand segments away says nothing about these bytes"
221
+ );
222
+ });
@@ -52,16 +52,19 @@ function box(type, body) {
52
52
 
53
53
  /**
54
54
  * `elst` holding one empty edit — how the `segment` muxer records where the
55
- * piece sits on the source timeline.
55
+ * piece sits on the source timeline. A version 0 entry is twelve bytes
56
+ * (ISO/IEC 14496-12): duration, media time, and the media rate as an integer
57
+ * and a fraction.
56
58
  *
57
59
  * @param {number} offsetSeconds
58
60
  * @returns {Buffer}
59
61
  */
60
62
  function emptyEdit(offsetSeconds) {
61
- const body = Buffer.alloc(16);
63
+ const body = Buffer.alloc(20);
62
64
  body.writeUInt32BE(1, 4); // entry count
63
65
  body.writeUInt32BE(Math.round(offsetSeconds * MOVIE_TIMESCALE), 8); // duration
64
66
  body.writeInt32BE(-1, 12); // media_time
67
+ body.writeInt16BE(1, 16); // media_rate_integer
65
68
  return box("elst", body);
66
69
  }
67
70
 
@@ -206,7 +209,10 @@ async function managerWithReadySegment(overrides = {}) {
206
209
  // `prepareSegmentBytes` has no timescales without it and hands the piece back
207
210
  // unstamped, which is silent and is exactly the fault stamping exists to
208
211
  // prevent.
209
- manager.segmentStore.keepInit(OUTPUT_KEY, fmp4Format.extractInit(piece));
212
+ // Kept as production keeps the header lifted out of a self-contained piece:
213
+ // shared by every piece, so its piece-specific empty edit is neutralized and
214
+ // each piece is placed by its own.
215
+ manager.segmentStore.keepInit(OUTPUT_KEY, fmp4Format.prepareSharedInit(fmp4Format.extractInit(piece)));
210
216
  manager.outputs.set(SESSION_ID, session);
211
217
  // SOMEBODY IS WATCHING IT. A segment is requested by a viewer, so a fixture
212
218
  // that asks for one without stating a viewer describes a state production