@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,358 +1,258 @@
1
- /**
2
- * @file A piece that cannot be played: waited for, or removed and made again.
3
- *
4
- * Field 2026-09-03. A run was suspended 548 ms after it started with
5
- * `segment-00025.mp4` newly opened, so the file stayed at zero bytes. From then
6
- * on the two halves of the proxy disagreed about it and neither could see the
7
- * other's reason: the look-ahead counted the NAME, found the numbering unbroken
8
- * through #83, called it `420s ahead of the viewer` and kept the encoder
9
- * stopped; the serving path read the FILE, found it short of a track, and
10
- * waited for a run that had produced nothing to finish it.
11
- *
12
- * The viewer's picture stood still for ten minutes on a transport measuring
13
- * 3-9 ms round trip.
14
- *
15
- * The question of WHICH copy answers is gone with the directory-per-run scheme:
16
- * there is one directory per output, runs are kept apart by their stretches,
17
- * and one name is one file. What is left is the question that remains real —
18
- * whether a file that cannot be played is being written now or was left by a
19
- * run that has ended.
20
- */
21
-
22
- import test from "node:test";
23
- import assert from "node:assert/strict";
24
- import { SourceFile } from "../services/source/SourceFile.js";
25
- import { Timeline } from "../services/output/Timeline.js";
26
- import { mkdtemp, readdir, rm, writeFile } from "node:fs/promises";
27
- import os from "node:os";
28
- import path from "node:path";
29
- import { HlsSessionManager, usableSegmentIndices } from "../services/hls-session-manager.js";
30
- import { discardOpenPiece } from "../services/encode/open-piece.js";
31
- import { startRunOn } from "./helpers/encode-run.js";
32
- import { fmp4Format } from "../services/segment-formats/fmp4.js";
33
-
34
- const MOVIE_TIMESCALE = 1000;
35
- const VIDEO_TIMESCALE = 90_000;
36
- const AUDIO_TIMESCALE = 48_000;
37
- const SEGMENT_SECONDS = 12.5;
38
- const SESSION_ID = "aef21c88-a8d6-4a9a-8e7a-d0a9536351cf";
39
-
40
- /**
41
- * @param {string} type
42
- * @param {Buffer} body
43
- * @returns {Buffer}
44
- */
45
- function box(type, body) {
46
- const head = Buffer.alloc(8);
47
- head.writeUInt32BE(8 + body.length, 0);
48
- head.write(type, 4, "latin1");
49
- return Buffer.concat([head, body]);
50
- }
51
-
52
- /**
53
- * @param {number} offsetSeconds
54
- * @returns {Buffer}
55
- */
56
- function emptyEdit(offsetSeconds) {
57
- const body = Buffer.alloc(16);
58
- body.writeUInt32BE(1, 4);
59
- body.writeUInt32BE(Math.round(offsetSeconds * MOVIE_TIMESCALE), 8);
60
- body.writeInt32BE(-1, 12);
61
- return box("elst", body);
62
- }
63
-
64
- /**
65
- * @param {number} trackId
66
- * @param {number} timescale
67
- * @param {number} offsetSeconds
68
- * @returns {Buffer}
69
- */
70
- function trak(trackId, timescale, offsetSeconds) {
71
- const tkhdBody = Buffer.alloc(84);
72
- tkhdBody.writeUInt32BE(trackId, 12);
73
- const mdhdBody = Buffer.alloc(20);
74
- mdhdBody.writeUInt32BE(timescale, 12);
75
- return box("trak", Buffer.concat([
76
- box("tkhd", tkhdBody),
77
- box("edts", emptyEdit(offsetSeconds)),
78
- box("mdia", box("mdhd", mdhdBody))
79
- ]));
80
- }
81
-
82
- /**
83
- * @param {number} trackId
84
- * @returns {Buffer}
85
- */
86
- function traf(trackId) {
87
- const tfhdBody = Buffer.alloc(8);
88
- tfhdBody.writeUInt32BE(trackId, 4);
89
- const tfdtBody = Buffer.alloc(12);
90
- tfdtBody.writeUInt8(1, 0);
91
- tfdtBody.writeBigUInt64BE(0n, 4);
92
- return box("traf", Buffer.concat([box("tfhd", tfhdBody), box("tfdt", tfdtBody)]));
93
- }
94
-
95
- /**
96
- * A piece shaped like the `segment` muxer's output, carrying both tracks.
97
- *
98
- * @param {number} offsetSeconds
99
- * @returns {Buffer}
100
- */
101
- function wholePiece(offsetSeconds) {
102
- const mvhdBody = Buffer.alloc(100);
103
- mvhdBody.writeUInt32BE(MOVIE_TIMESCALE, 12);
104
- const moov = box("moov", Buffer.concat([
105
- box("mvhd", mvhdBody),
106
- trak(1, VIDEO_TIMESCALE, offsetSeconds),
107
- trak(2, AUDIO_TIMESCALE, offsetSeconds)
108
- ]));
109
- const moof = box("moof", Buffer.concat([traf(1), traf(2)]));
110
- return Buffer.concat([
111
- box("ftyp", Buffer.alloc(16, 0)),
112
- moov,
113
- moof,
114
- box("mdat", Buffer.alloc(64, 0x5a)),
115
- box("mfra", Buffer.alloc(24, 0))
116
- ]);
117
- }
118
-
119
- /**
120
- * The same shape with ONE track in the fragment what a run killed mid-write
121
- * leaves when it had muxed the picture and not yet the sound.
122
- *
123
- * @param {number} offsetSeconds
124
- * @returns {Buffer}
125
- */
126
- function halfPiece(offsetSeconds) {
127
- const mvhdBody = Buffer.alloc(100);
128
- mvhdBody.writeUInt32BE(MOVIE_TIMESCALE, 12);
129
- const moov = box("moov", Buffer.concat([
130
- box("mvhd", mvhdBody),
131
- trak(1, VIDEO_TIMESCALE, offsetSeconds),
132
- trak(2, AUDIO_TIMESCALE, offsetSeconds)
133
- ]));
134
- return Buffer.concat([
135
- box("ftyp", Buffer.alloc(16, 0)),
136
- moov,
137
- box("moof", traf(1)),
138
- box("mdat", Buffer.alloc(64, 0x5a))
139
- ]);
140
- }
141
-
142
- /**
143
- * A session with the output directory every run writes into.
144
- *
145
- * @returns {Promise<{ manager: HlsSessionManager, session: object, dirPath: string }>}
146
- */
147
- async function sessionOnOneDirectory() {
148
- const dirPath = await mkdtemp(path.join(os.tmpdir(), "produced-copy-"));
149
- const manager = new HlsSessionManager({
150
- enabled: true,
151
- ffmpegBin: "ffmpeg",
152
- localBindHost: "127.0.0.1",
153
- localPort: 9090
154
- });
155
- const session = {
156
- id: SESSION_ID,
157
- dirPath,
158
- // Where this file is cut, held by the file. A fixture that stated it
159
- // on the session was describing what production no longer does.
160
- timeline: new Timeline({
161
- boundaries: [0, SEGMENT_SECONDS, 25, 37.5, 50],
162
- cutGrid: "uniform"
163
- }),
164
- state: "ready",
165
- file: new SourceFile({ sourceKey: "source-1", fileIndex: 0, name: "Drifters - 04.mkv" }),
166
- // An ordinary session reads its own file, and its sound is inside it. The
167
- // three differ only for a soundtrack shipped as a file of its own.
168
- get inputFile() { return this.file; },
169
- get audioFile() { return this.file; },
170
- startedAt: Date.now(),
171
- createEntryMs: Date.now(),
172
- lastAccessedAt: Date.now(),
173
- runs: new Set(),
174
- lastError: "",
175
- consumers: new Set(),
176
- viewers: new Map(),
177
- segmentFormat: fmp4Format,
178
- useSyntheticPlaylist: true,
179
- playlistText: "#EXTM3U\n",
180
- initBytes: fmp4Format.extractInit(wholePiece(0)),
181
- firstSegmentLogged: false,
182
- waitEpoch: 0
183
- };
184
- manager.sessionsById.set(SESSION_ID, session);
185
- // A run exists and is alive the state the field case was in, and the one in
186
- // which the old test called every leftover "still being written".
187
- startRunOn(session, { from: 0, usesExplicitCuts: true });
188
- return { manager, session, dirPath };
189
- }
190
-
191
-
192
-
193
- test("a leftover of a run that has ended is not served, and answering does not delete it", async (t) => {
194
- const { manager, session, dirPath } = await sessionOnOneDirectory();
195
- t.after(async () => {
196
- await manager.disposeAll();
197
- await rm(dirPath, { recursive: true, force: true });
198
- });
199
- // Opened by a run that is gone. It has a name and no bytes, so there is
200
- // nothing to serve.
201
- session.runs = new Set();
202
- await writeFile(path.join(dirPath, "segment-00001.mp4"), Buffer.alloc(0));
203
-
204
- const result = await manager.getFileStream(SESSION_ID, "segment-00001.mp4", { requestSeq: 1 });
205
-
206
- assert.equal(result.kind, "warming-up", "nothing servable exists yet, so the viewer waits");
207
- // AND ANSWERING A REQUEST DELETES NOTHING. It used to: this test was written
208
- // when the serving path cleared up after ended runs, and several runs of an
209
- // output share one directory so what a request removed could be the piece a
210
- // LIVE run had just finished. Clearing up belongs to the ending of a run,
211
- // which is the only thing that knows what it left open and what it named
212
- // (`discardOpenPiece`, checked directly below). A file with no bytes is
213
- // ignored by everything that reads the directory, so leaving it costs nothing
214
- // and deleting it from here costs a segment.
215
- const left = (await readdir(dirPath)).filter((name) => name === "segment-00001.mp4");
216
- assert.deepEqual(left, ["segment-00001.mp4"]);
217
- });
218
-
219
- test("the current run's own unfinished piece is waited for, never deleted", async (t) => {
220
- const { manager, session, dirPath } = await sessionOnOneDirectory();
221
- t.after(async () => {
222
- await manager.disposeAll();
223
- await rm(dirPath, { recursive: true, force: true });
224
- });
225
- // The same file, in the directory of the run that is alive. It is being
226
- // written right now. Deleting it is the 2026-08-06 incident: #225 was removed
227
- // 14 s into the run producing it, which then wrote on into a file nobody
228
- // could open, and the segment never appeared.
229
- //
230
- // The live run begins at #1, so #1 is its own first piece.
231
- startRunOn(session, { from: 1, producing: false, usesExplicitCuts: true });
232
- await writeFile(path.join(dirPath, "segment-00001.mp4"), Buffer.alloc(0));
233
-
234
- const result = await manager.getFileStream(SESSION_ID, "segment-00001.mp4", { requestSeq: 1 });
235
-
236
- assert.equal(result.kind, "warming-up");
237
- const left = (await readdir(dirPath)).filter((name) => name === "segment-00001.mp4");
238
- assert.deepEqual(left, ["segment-00001.mp4"], "the live run's own output must be left alone");
239
- });
240
-
241
- test("the look-ahead does not count a file with nothing in it", async (t) => {
242
- const dirPath = await mkdtemp(path.join(os.tmpdir(), "usable-indices-"));
243
- t.after(async () => {
244
- await rm(dirPath, { recursive: true, force: true });
245
- });
246
- await writeFile(path.join(dirPath, "segment-00000.mp4"), wholePiece(0));
247
- await writeFile(path.join(dirPath, "segment-00001.mp4"), Buffer.alloc(0));
248
-
249
- assert.deepEqual(
250
- [...usableSegmentIndices([dirPath], fmp4Format, new Set())].sort((a, b) => a - b),
251
- [0],
252
- "an empty file bridged the hole and bought the encoder a suspension it had not earned"
253
- );
254
-
255
- // The same number, made properly: now it genuinely is ready.
256
- await writeFile(path.join(dirPath, "segment-00001.mp4"), wholePiece(SEGMENT_SECONDS));
257
- assert.deepEqual(
258
- [...usableSegmentIndices([dirPath], fmp4Format, new Set())].sort((a, b) => a - b),
259
- [0, 1],
260
- "a copy with bytes in it, which is what the serving path will find"
261
- );
262
- });
263
-
264
- test("what the output holds is asked of the filesystem once per file", async (t) => {
265
- const dirPath = await mkdtemp(path.join(os.tmpdir(), "usable-memo-"));
266
- t.after(async () => {
267
- await rm(dirPath, { recursive: true, force: true });
268
- });
269
- await writeFile(path.join(dirPath, "segment-00000.mp4"), wholePiece(0));
270
- const known = new Set();
271
-
272
- usableSegmentIndices([dirPath], fmp4Format, known);
273
-
274
- assert.deepEqual(
275
- [...known],
276
- [path.join(dirPath, "segment-00000.mp4")],
277
- "a piece that has bytes never loses them, and this runs on the thread carrying the data channel"
278
- );
279
- });
280
-
281
- test("a run killed with a piece open leaves nothing behind", async (t) => {
282
- const dirPath = await mkdtemp(path.join(os.tmpdir(), "open-piece-"));
283
- t.after(async () => {
284
- await rm(dirPath, { recursive: true, force: true });
285
- });
286
- await writeFile(path.join(dirPath, "segment-00000.mp4"), wholePiece(0));
287
- await writeFile(path.join(dirPath, "segment-00001.mp4"), Buffer.alloc(0));
288
-
289
- // WHAT THE RUN NAMED IS WHAT IT FINISHED. It named #0 on the ready channel and
290
- // was killed with #1 open, so #1 goes and #0 stays. Asking the file instead
291
- // cannot answer: a piece cut short still decodes, which is how 3.92 s of a
292
- // declared 5.589 s reached a viewer on 2026-09-06.
293
- assert.equal(
294
- await discardOpenPiece(dirPath, fmp4Format, { from: 0, to: 1 }, null, "segment-00000.mp4"),
295
- 1
296
- );
297
- assert.deepEqual(
298
- await readdir(dirPath),
299
- ["segment-00000.mp4"],
300
- "only the piece that was open goes; everything the run named stays"
301
- );
302
- });
303
-
304
- test("a run that named nothing can only have opened the first piece it was given", async (t) => {
305
- const dirPath = await mkdtemp(path.join(os.tmpdir(), "open-piece-unnamed-"));
306
- t.after(async () => {
307
- await rm(dirPath, { recursive: true, force: true });
308
- });
309
- await writeFile(path.join(dirPath, "segment-00000.mp4"), wholePiece(0));
310
- await writeFile(path.join(dirPath, "segment-00001.mp4"), wholePiece(SEGMENT_SECONDS));
311
-
312
- // THE CASE THIS CONTRACT WAS WRITTEN FOR. ffmpeg names a piece as it closes it
313
- // and opens the next, so a run that named nothing had only its first open. It
314
- // used to search the whole directory and take the highest number in it —
315
- // every run of an output writes into that one directory, so what it took was
316
- // a piece a LIVE run had just finished. Field 2026-09-06: the piece holding
317
- // 2:47-2:57 went that way, its number is spent for good because names only
318
- // grow, and the picture stood still for 647 s.
319
- assert.equal(await discardOpenPiece(dirPath, fmp4Format, null, null), 0);
320
- assert.deepEqual(
321
- await readdir(dirPath),
322
- ["segment-00001.mp4"],
323
- "the piece belonging to somebody else is not touched"
324
- );
325
- });
326
-
327
- test("a run that finished its last piece keeps it", async (t) => {
328
- const dirPath = await mkdtemp(path.join(os.tmpdir(), "open-piece-good-"));
329
- t.after(async () => {
330
- await rm(dirPath, { recursive: true, force: true });
331
- });
332
- await writeFile(path.join(dirPath, "segment-00000.mp4"), wholePiece(0));
333
- await writeFile(path.join(dirPath, "segment-00001.mp4"), wholePiece(SEGMENT_SECONDS));
334
-
335
- assert.equal(
336
- await discardOpenPiece(dirPath, fmp4Format, { from: 0, to: 1 }, () => true, "segment-00001.mp4"),
337
- null,
338
- "a stop between two cuts leaves good output; deleting it means encoding it twice"
339
- );
340
- assert.equal((await readdir(dirPath)).length, 2);
341
- });
342
-
343
- test("a last piece short of a track goes, even though it has bytes", async (t) => {
344
- const dirPath = await mkdtemp(path.join(os.tmpdir(), "open-piece-half-"));
345
- t.after(async () => {
346
- await rm(dirPath, { recursive: true, force: true });
347
- });
348
- await writeFile(path.join(dirPath, "segment-00000.mp4"), wholePiece(0));
349
- await writeFile(path.join(dirPath, "segment-00001.mp4"), halfPiece(SEGMENT_SECONDS));
350
- const init = fmp4Format.extractInit(wholePiece(0));
351
-
352
- assert.equal(
353
- await discardOpenPiece(dirPath, fmp4Format, { from: 0, to: 1 }, (raw) =>
354
- fmp4Format.hasEveryTrack(fmp4Format.stripInit(raw), init), "segment-00001.mp4"),
355
- 1,
356
- "a size above zero is not the same as a piece that can be played"
357
- );
358
- });
1
+ /**
2
+ * @file A piece that cannot be played: waited for, or removed and made again.
3
+ *
4
+ * Field 2026-09-03. A run was suspended 548 ms after it started with
5
+ * `segment-00025.mp4` newly opened, so the file stayed at zero bytes. From then
6
+ * on the two halves of the proxy disagreed about it and neither could see the
7
+ * other's reason: the look-ahead counted the NAME, found the numbering unbroken
8
+ * through #83, called it `420s ahead of the viewer` and kept the encoder
9
+ * stopped; the serving path read the FILE, found it short of a track, and
10
+ * waited for a run that had produced nothing to finish it.
11
+ *
12
+ * The viewer's picture stood still for ten minutes on a transport measuring
13
+ * 3-9 ms round trip.
14
+ *
15
+ * The question of WHICH copy answers is gone with the directory-per-run scheme:
16
+ * there is one directory per output, runs are kept apart by their stretches,
17
+ * and one name is one file. What is left is the question that remains real —
18
+ * whether a file that cannot be played is being written now or was left by a
19
+ * run that has ended.
20
+ */
21
+
22
+ import test from "node:test";
23
+ import assert from "node:assert/strict";
24
+ import { SourceFile } from "../services/source/SourceFile.js";
25
+ import { Timeline } from "../services/output/Timeline.js";
26
+ import { mkdtemp, readdir, rm, writeFile } from "node:fs/promises";
27
+ import os from "node:os";
28
+ import path from "node:path";
29
+ import { HlsSessionManager, usableSegmentIndices } from "../services/hls-session-manager.js";
30
+ import { startRunOn } from "./helpers/encode-run.js";
31
+ import { fmp4Format } from "../services/segment-formats/fmp4.js";
32
+
33
+ const MOVIE_TIMESCALE = 1000;
34
+ const VIDEO_TIMESCALE = 90_000;
35
+ const AUDIO_TIMESCALE = 48_000;
36
+ const SEGMENT_SECONDS = 12.5;
37
+ const SESSION_ID = "aef21c88-a8d6-4a9a-8e7a-d0a9536351cf";
38
+
39
+ /**
40
+ * @param {string} type
41
+ * @param {Buffer} body
42
+ * @returns {Buffer}
43
+ */
44
+ function box(type, body) {
45
+ const head = Buffer.alloc(8);
46
+ head.writeUInt32BE(8 + body.length, 0);
47
+ head.write(type, 4, "latin1");
48
+ return Buffer.concat([head, body]);
49
+ }
50
+
51
+ /**
52
+ * @param {number} offsetSeconds
53
+ * @returns {Buffer}
54
+ */
55
+ function emptyEdit(offsetSeconds) {
56
+ const body = Buffer.alloc(16);
57
+ body.writeUInt32BE(1, 4);
58
+ body.writeUInt32BE(Math.round(offsetSeconds * MOVIE_TIMESCALE), 8);
59
+ body.writeInt32BE(-1, 12);
60
+ return box("elst", body);
61
+ }
62
+
63
+ /**
64
+ * @param {number} trackId
65
+ * @param {number} timescale
66
+ * @param {number} offsetSeconds
67
+ * @returns {Buffer}
68
+ */
69
+ function trak(trackId, timescale, offsetSeconds) {
70
+ const tkhdBody = Buffer.alloc(84);
71
+ tkhdBody.writeUInt32BE(trackId, 12);
72
+ const mdhdBody = Buffer.alloc(20);
73
+ mdhdBody.writeUInt32BE(timescale, 12);
74
+ return box("trak", Buffer.concat([
75
+ box("tkhd", tkhdBody),
76
+ box("edts", emptyEdit(offsetSeconds)),
77
+ box("mdia", box("mdhd", mdhdBody))
78
+ ]));
79
+ }
80
+
81
+ /**
82
+ * @param {number} trackId
83
+ * @returns {Buffer}
84
+ */
85
+ function traf(trackId) {
86
+ const tfhdBody = Buffer.alloc(8);
87
+ tfhdBody.writeUInt32BE(trackId, 4);
88
+ const tfdtBody = Buffer.alloc(12);
89
+ tfdtBody.writeUInt8(1, 0);
90
+ tfdtBody.writeBigUInt64BE(0n, 4);
91
+ return box("traf", Buffer.concat([box("tfhd", tfhdBody), box("tfdt", tfdtBody)]));
92
+ }
93
+
94
+ /**
95
+ * A piece shaped like the `segment` muxer's output, carrying both tracks.
96
+ *
97
+ * @param {number} offsetSeconds
98
+ * @returns {Buffer}
99
+ */
100
+ function wholePiece(offsetSeconds) {
101
+ const mvhdBody = Buffer.alloc(100);
102
+ mvhdBody.writeUInt32BE(MOVIE_TIMESCALE, 12);
103
+ const moov = box("moov", Buffer.concat([
104
+ box("mvhd", mvhdBody),
105
+ trak(1, VIDEO_TIMESCALE, offsetSeconds),
106
+ trak(2, AUDIO_TIMESCALE, offsetSeconds)
107
+ ]));
108
+ const moof = box("moof", Buffer.concat([traf(1), traf(2)]));
109
+ return Buffer.concat([
110
+ box("ftyp", Buffer.alloc(16, 0)),
111
+ moov,
112
+ moof,
113
+ box("mdat", Buffer.alloc(64, 0x5a)),
114
+ box("mfra", Buffer.alloc(24, 0))
115
+ ]);
116
+ }
117
+
118
+
119
+ /**
120
+ * A session with the output directory every run writes into.
121
+ *
122
+ * @returns {Promise<{ manager: HlsSessionManager, session: object, dirPath: string }>}
123
+ */
124
+ async function sessionOnOneDirectory() {
125
+ const dirPath = await mkdtemp(path.join(os.tmpdir(), "produced-copy-"));
126
+ const manager = new HlsSessionManager({
127
+ enabled: true,
128
+ ffmpegBin: "ffmpeg",
129
+ localBindHost: "127.0.0.1",
130
+ localPort: 9090
131
+ });
132
+ const session = {
133
+ id: SESSION_ID,
134
+ dirPath,
135
+ // Where this file is cut, held by the file. A fixture that stated it
136
+ // on the session was describing what production no longer does.
137
+ timeline: new Timeline({
138
+ boundaries: [0, SEGMENT_SECONDS, 25, 37.5, 50],
139
+ cutGrid: "uniform"
140
+ }),
141
+ state: "ready",
142
+ file: new SourceFile({ sourceKey: "source-1", fileIndex: 0, name: "Drifters - 04.mkv" }),
143
+ // An ordinary session reads its own file, and its sound is inside it. The
144
+ // three differ only for a soundtrack shipped as a file of its own.
145
+ get inputFile() { return this.file; },
146
+ get audioFile() { return this.file; },
147
+ startedAt: Date.now(),
148
+ createEntryMs: Date.now(),
149
+ lastAccessedAt: Date.now(),
150
+ runs: new Set(),
151
+ lastError: "",
152
+ consumers: new Set(),
153
+ viewers: new Map(),
154
+ segmentFormat: fmp4Format,
155
+ useSyntheticPlaylist: true,
156
+ playlistText: "#EXTM3U\n",
157
+ initBytes: fmp4Format.extractInit(wholePiece(0)),
158
+ firstSegmentLogged: false,
159
+ waitEpoch: 0
160
+ };
161
+ manager.sessionsById.set(SESSION_ID, session);
162
+ // A run exists and is alive — the state the field case was in, and the one in
163
+ // which the old test called every leftover "still being written".
164
+ startRunOn(session, { from: 0, usesExplicitCuts: true });
165
+ return { manager, session, dirPath };
166
+ }
167
+
168
+
169
+
170
+ test("a leftover of a run that has ended is not served, and answering does not delete it", async (t) => {
171
+ const { manager, session, dirPath } = await sessionOnOneDirectory();
172
+ t.after(async () => {
173
+ await manager.disposeAll();
174
+ await rm(dirPath, { recursive: true, force: true });
175
+ });
176
+ // Opened by a run that is gone. It has a name and no bytes, so there is
177
+ // nothing to serve.
178
+ session.runs = new Set();
179
+ await writeFile(path.join(dirPath, "segment-00001.mp4"), Buffer.alloc(0));
180
+
181
+ const result = await manager.getFileStream(SESSION_ID, "segment-00001.mp4", { requestSeq: 1 });
182
+
183
+ assert.equal(result.kind, "warming-up", "nothing servable exists yet, so the viewer waits");
184
+ // AND ANSWERING A REQUEST DELETES NOTHING. It used to: this test was written
185
+ // when the serving path cleared up after ended runs, and several runs of an
186
+ // output share one directory so what a request removed could be the piece a
187
+ // LIVE run had just finished. Clearing up belongs to the ending of a run,
188
+ // which is the only thing that knows what it left open and what it named
189
+ // and it knows because the piece is under that run's own working name
190
+ // (`SegmentStore.clearUpAfter`, checked in `segment-store.test.js`). A file
191
+ // with no bytes is
192
+ // ignored by everything that reads the directory, so leaving it costs nothing
193
+ // and deleting it from here costs a segment.
194
+ const left = (await readdir(dirPath)).filter((name) => name === "segment-00001.mp4");
195
+ assert.deepEqual(left, ["segment-00001.mp4"]);
196
+ });
197
+
198
+ test("the current run's own unfinished piece is waited for, never deleted", async (t) => {
199
+ const { manager, session, dirPath } = await sessionOnOneDirectory();
200
+ t.after(async () => {
201
+ await manager.disposeAll();
202
+ await rm(dirPath, { recursive: true, force: true });
203
+ });
204
+ // The same file, in the directory of the run that is alive. It is being
205
+ // written right now. Deleting it is the 2026-08-06 incident: #225 was removed
206
+ // 14 s into the run producing it, which then wrote on into a file nobody
207
+ // could open, and the segment never appeared.
208
+ //
209
+ // The live run begins at #1, so #1 is its own first piece.
210
+ startRunOn(session, { from: 1, producing: false, usesExplicitCuts: true });
211
+ await writeFile(path.join(dirPath, "segment-00001.mp4"), Buffer.alloc(0));
212
+
213
+ const result = await manager.getFileStream(SESSION_ID, "segment-00001.mp4", { requestSeq: 1 });
214
+
215
+ assert.equal(result.kind, "warming-up");
216
+ const left = (await readdir(dirPath)).filter((name) => name === "segment-00001.mp4");
217
+ assert.deepEqual(left, ["segment-00001.mp4"], "the live run's own output must be left alone");
218
+ });
219
+
220
+ test("the look-ahead does not count a file with nothing in it", async (t) => {
221
+ const dirPath = await mkdtemp(path.join(os.tmpdir(), "usable-indices-"));
222
+ t.after(async () => {
223
+ await rm(dirPath, { recursive: true, force: true });
224
+ });
225
+ await writeFile(path.join(dirPath, "segment-00000.mp4"), wholePiece(0));
226
+ await writeFile(path.join(dirPath, "segment-00001.mp4"), Buffer.alloc(0));
227
+
228
+ assert.deepEqual(
229
+ [...usableSegmentIndices([dirPath], fmp4Format, new Set())].sort((a, b) => a - b),
230
+ [0],
231
+ "an empty file bridged the hole and bought the encoder a suspension it had not earned"
232
+ );
233
+
234
+ // The same number, made properly: now it genuinely is ready.
235
+ await writeFile(path.join(dirPath, "segment-00001.mp4"), wholePiece(SEGMENT_SECONDS));
236
+ assert.deepEqual(
237
+ [...usableSegmentIndices([dirPath], fmp4Format, new Set())].sort((a, b) => a - b),
238
+ [0, 1],
239
+ "a copy with bytes in it, which is what the serving path will find"
240
+ );
241
+ });
242
+
243
+ test("what the output holds is asked of the filesystem once per file", async (t) => {
244
+ const dirPath = await mkdtemp(path.join(os.tmpdir(), "usable-memo-"));
245
+ t.after(async () => {
246
+ await rm(dirPath, { recursive: true, force: true });
247
+ });
248
+ await writeFile(path.join(dirPath, "segment-00000.mp4"), wholePiece(0));
249
+ const known = new Set();
250
+
251
+ usableSegmentIndices([dirPath], fmp4Format, known);
252
+
253
+ assert.deepEqual(
254
+ [...known],
255
+ [path.join(dirPath, "segment-00000.mp4")],
256
+ "a piece that has bytes never loses them, and this runs on the thread carrying the data channel"
257
+ );
258
+ });