@torrent-tv/proxy 2.80.18 → 2.80.19

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,152 +0,0 @@
1
- /**
2
- * @file The piece a run had open when it ended, and what proves one whole.
3
- *
4
- * `-segment_list pipe:3` was taken as proof: ffmpeg names a file when it closes
5
- * it, so a named file is closed. True of the FILE and false of the SPAN. The
6
- * highest-numbered file in a run's stretch is the one it had open, and how the
7
- * run ended decides what became of it — stopped with SIGTERM, ffmpeg writes it
8
- * out and names it exactly as it names a finished one; killed harder, or dying
9
- * on its own, it leaves the bytes it had written. All three outcomes decode,
10
- * and all three hold film only up to the instant the run ended.
11
- *
12
- * Field 2026-09-06, one session, both tracks: `segment-00010.mp4` held 3.92 s
13
- * of its declared 5.589 s (96 frames), and the picture jumped 1.5 s at 1:02;
14
- * the soundtrack's own stopped run left the same shape at 17.5 s, 2.8 s wide.
15
- * Judging such a file by whether it decodes answers yes, which is how both
16
- * reached the viewer.
17
- *
18
- * So what is kept is what the run PROVED it finished: the last piece it named
19
- * while it was still running normally.
20
- */
21
-
22
- import test from "node:test";
23
- import assert from "node:assert/strict";
24
- import { existsSync } from "node:fs";
25
- import { mkdtemp, rm, writeFile, readdir } from "node:fs/promises";
26
- import os from "node:os";
27
- import path from "node:path";
28
- import { discardOpenPiece } from "../services/encode/open-piece.js";
29
-
30
- const format = {
31
- isSegmentFileName: (name) => /^segment-\d{5}\.mp4$/.test(name),
32
- segmentIndexFromName: (name) => Number(name.slice(8, 13))
33
- };
34
-
35
- /** A run directory holding pieces 0..last, every one of them readable. */
36
- async function runDirectory(last) {
37
- const dir = await mkdtemp(path.join(os.tmpdir(), "open-piece-"));
38
- for (let index = 0; index <= last; index += 1) {
39
- await writeFile(path.join(dir, `segment-${String(index).padStart(5, "0")}.mp4`), Buffer.alloc(64, 7));
40
- }
41
- return dir;
42
- }
43
-
44
- test("the piece written out on the way to being stopped goes, though it reads", async () => {
45
- // The field case: ffmpeg named #10 while shutting down, so #9 is the last it
46
- // proved. Everything decodes, and nothing about #10's contents betrays it.
47
- const dir = await runDirectory(10);
48
- try {
49
- const removed = await discardOpenPiece(
50
- dir, format, { from: 0, to: 535 }, () => true, "segment-00009.mp4"
51
- );
52
- assert.equal(removed, 10, "the open piece was kept because it decodes");
53
- const left = await readdir(dir);
54
- assert.ok(!left.includes("segment-00010.mp4"));
55
- assert.ok(left.includes("segment-00009.mp4"), "a piece the run proved was taken too");
56
- } finally {
57
- await rm(dir, { recursive: true, force: true });
58
- }
59
- });
60
-
61
- test("a piece the run never named goes too, however it was killed", async () => {
62
- // Killed harder than SIGTERM, or dead on its own: the open piece is left
63
- // half-written and unnamed. It still decodes, and it is still short.
64
- const dir = await runDirectory(10);
65
- try {
66
- const removed = await discardOpenPiece(
67
- dir, format, { from: 0, to: 535 }, () => true, "segment-00009.mp4"
68
- );
69
- assert.equal(removed, 10);
70
- } finally {
71
- await rm(dir, { recursive: true, force: true });
72
- }
73
- });
74
-
75
- test("the piece the run proved finished stays", async () => {
76
- // A run stopped in the moment after closing #10 and before opening #11
77
- // proved #10. Removing it would mean encoding it a second time for nothing.
78
- const dir = await runDirectory(10);
79
- try {
80
- const removed = await discardOpenPiece(
81
- dir, format, { from: 0, to: 535 }, () => true, "segment-00010.mp4"
82
- );
83
- assert.equal(removed, null);
84
- assert.ok((await readdir(dir)).includes("segment-00010.mp4"));
85
- } finally {
86
- await rm(dir, { recursive: true, force: true });
87
- }
88
- });
89
-
90
- test("a run that proved nothing keeps nothing", async () => {
91
- const dir = await runDirectory(3);
92
- try {
93
- const removed = await discardOpenPiece(dir, format, { from: 0, to: 535 }, () => true, null);
94
- assert.equal(removed, 3, "a run that named nothing had its last piece believed");
95
- } finally {
96
- await rm(dir, { recursive: true, force: true });
97
- }
98
- });
99
-
100
- test("only inside the stretch the ended run was given", async () => {
101
- // Several runs write into one directory, kept apart by their intervals. The
102
- // highest file overall may belong to a run that is still going.
103
- const dir = await runDirectory(20);
104
- try {
105
- const removed = await discardOpenPiece(
106
- dir, format, { from: 0, to: 10 }, () => true, "segment-00009.mp4"
107
- );
108
- assert.equal(removed, 10);
109
- assert.ok((await readdir(dir)).includes("segment-00020.mp4"), "another run's piece was taken");
110
- } finally {
111
- await rm(dir, { recursive: true, force: true });
112
- }
113
- });
114
-
115
- test("a run given no end never reaches past what it made, so a live run's piece is safe", async () => {
116
- // Field 2026-09-06. Three encoders wrote into one directory; the first was
117
- // stopped, its stretch was `#0..#-1` — "to the end of the track" — and the
118
- // cleanup after it took the highest-numbered file anywhere in that directory,
119
- // which a LIVE encoder had just finished. The number is spent for good,
120
- // because names only grow, and the picture stood still for 647 seconds.
121
- const dir = await mkdtemp(path.join(os.tmpdir(), "open-piece-"));
122
- // What the stopped run made: #0 and #1, with #2 left open.
123
- await writeFile(path.join(dir, "segment-00000.mp4"), Buffer.alloc(1000, 1));
124
- await writeFile(path.join(dir, "segment-00001.mp4"), Buffer.alloc(1000, 1));
125
- await writeFile(path.join(dir, "segment-00002.mp4"), Buffer.alloc(0));
126
- // What a live encoder, working further along the same track, has finished.
127
- await writeFile(path.join(dir, "segment-00040.mp4"), Buffer.alloc(9000, 1));
128
-
129
- const removed = await discardOpenPiece(
130
- dir,
131
- format,
132
- { from: 0, to: -1 },
133
- null,
134
- "segment-00001.mp4"
135
- );
136
-
137
- assert.equal(removed, 2, "its own open piece goes");
138
- assert.ok(existsSync(path.join(dir, "segment-00040.mp4")), "the live run's piece stays");
139
- });
140
-
141
- test("a run that named nothing leaves only its own first piece", async () => {
142
- // It opened one file and died — 548 ms after starting, in the field. Nothing
143
- // above that can be its.
144
- const dir = await mkdtemp(path.join(os.tmpdir(), "open-piece-"));
145
- await writeFile(path.join(dir, "segment-00010.mp4"), Buffer.alloc(0));
146
- await writeFile(path.join(dir, "segment-00011.mp4"), Buffer.alloc(9000, 1));
147
-
148
- const removed = await discardOpenPiece(dir, format, { from: 10, to: -1 }, null, null);
149
-
150
- assert.equal(removed, 10, "the one it opened");
151
- assert.ok(existsSync(path.join(dir, "segment-00011.mp4")), "and nothing beyond it");
152
- });