@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.
@@ -290,12 +290,12 @@ test("a run's FIRST segment is served once the encoder has passed it, without wa
290
290
  // successor and nothing is producing one. Waiting for that successor is what
291
291
  // held #317 for 46 s and then answered 404 to a browser that had given up.
292
292
  await rm(path.join(dirPath, "segment-00001.mp4"));
293
- // WHAT PROVES IT INSTEAD: the encoder named it. ffmpeg writes the name of each
294
- // piece on a channel of its own as it closes it, so a run's own first piece is
295
- // proven the moment it is finished and the absence of a next one says nothing.
296
- // This used to be inferred from the encoder's reported position instead, which
297
- // is a different question where it has read to, not what it has closed.
298
- manager.segmentStore.markClosed(OUTPUT_KEY, 0);
293
+ // WHAT PROVES IT INSTEAD: its own NAME. A piece is called something else while
294
+ // it is being written and takes its served name when its writer says it is
295
+ // closed, so a run's first piece is proven the moment it is finished and the
296
+ // absence of a next one says nothing. This used to be inferred from the
297
+ // encoder's reported position — a different question, where it has read to
298
+ // rather than what it has closed.
299
299
 
300
300
  const result = await manager.getFileStream(SESSION_ID, "segment-00000.mp4", { requestSeq: 1 });
301
301
 
@@ -345,10 +345,9 @@ test("serving a run's own segment moves the run out of STARTING", async (t) => {
345
345
  // ITS OWN PIECE, AND FILM STILL TO MAKE BEHIND IT. A run standing on material
346
346
  // that already exists is moved forward — correctly — so a fixture that wants a
347
347
  // live run has to put it where the work is. #1 is what it is making; #2..#4
348
- // are unmade, so it is wanted; and the encoder has named #1 on its ready
349
- // channel, which is what makes the piece servable at all.
348
+ // are unmade, so it is wanted; and #1 stands under its served name, which is
349
+ // what makes the piece servable at all.
350
350
  const run = startRunOn(session, { from: 1, producing: false, usesExplicitCuts: true, speedX: 2 });
351
- manager.segmentStore.markClosed(OUTPUT_KEY, 1);
352
351
  assert.equal(run.state, ENCODE_RUN_STATE.STARTING);
353
352
  assert.deepEqual(run.produced, [], "it has made nothing yet");
354
353
 
@@ -1,216 +1,238 @@
1
- /**
2
- * @file One store of segments, addressed by what they are, and what it makes of
3
- * what a killed process left behind.
4
- */
5
-
6
- import test from "node:test";
7
- import assert from "node:assert/strict";
8
- import { mkdtempSync, mkdirSync, readdirSync, rmSync, writeFileSync } from "node:fs";
9
- import os from "node:os";
10
- import path from "node:path";
11
- import { directoryNameFor, SegmentStore } from "../services/encode/SegmentStore.js";
12
- import { fmp4Format } from "../services/segment-formats/fmp4.js";
13
-
14
- /**
15
- * @returns {{ store: SegmentStore, root: string, lines: string[] }}
16
- */
17
- function storeInATempRoot() {
18
- const root = mkdtempSync(path.join(os.tmpdir(), "segment-store-"));
19
- const lines = [];
20
- const store = new SegmentStore({
21
- root,
22
- logger: { info: (line) => lines.push(line), warn: (line) => lines.push(line) }
23
- });
24
- return { store, root, lines };
25
- }
26
-
27
- /**
28
- * @param {string} dir
29
- * @param {number} index
30
- * @param {number} bytes
31
- */
32
- function writeSegment(dir, index, bytes = 16) {
33
- writeFileSync(path.join(dir, fmp4Format.segmentFileName(index)), Buffer.alloc(bytes));
34
- }
35
-
36
- const KEY = "torrent:abc:fmt=fmp4:grid=kf@0:video-only:v=0/copy";
37
-
38
- test("two viewers of one output are given the same directory", (t) => {
39
- const { store, root } = storeInATempRoot();
40
- t.after(() => rmSync(root, { recursive: true, force: true }));
41
-
42
- // Which viewer asked never enters it — the address is what the segments are.
43
- assert.equal(store.directoryFor(KEY), store.directoryFor(KEY));
44
- assert.notEqual(store.directoryFor(KEY), store.directoryFor(`${KEY}:other`));
45
- });
46
-
47
- test("the directory says what it holds, so a later process can tell", (t) => {
48
- const { store, root } = storeInATempRoot();
49
- t.after(() => rmSync(root, { recursive: true, force: true }));
50
-
51
- const dir = store.directoryFor(KEY);
52
- const names = readdirSync(dir);
53
- assert.deepEqual(names, ["key.txt"]);
54
- });
55
-
56
- test("a segment is proven closed by the existence of the next one", (t) => {
57
- const { store, root } = storeInATempRoot();
58
- t.after(() => rmSync(root, { recursive: true, force: true }));
59
-
60
- const dir = store.directoryFor(KEY);
61
- store.useFormat(KEY, fmp4Format);
62
- writeSegment(dir, 0);
63
- writeSegment(dir, 1);
64
- writeSegment(dir, 2);
65
-
66
- // The `hls` muxer carries no channel to report on and renames into place when
67
- // it closes a piece, so on that branch existence IS the proof; the same
68
- // answers for the pieces a previous life of this process left behind.
69
- assert.deepEqual(store.provenNumbers(KEY), [0, 1]);
70
- assert.equal(store.unprovenNumber(KEY), 2);
71
- });
72
-
73
- test("a file of no bytes is not a segment, whatever it is called", (t) => {
74
- const { store, root } = storeInATempRoot();
75
- t.after(() => rmSync(root, { recursive: true, force: true }));
76
-
77
- const dir = store.directoryFor(KEY);
78
- store.useFormat(KEY, fmp4Format);
79
- writeSegment(dir, 0);
80
- writeSegment(dir, 1);
81
- writeSegment(dir, 2, 0);
82
-
83
- // What a run killed the moment after opening its next piece leaves behind.
84
- // Counted as a segment once, it closed the only hole in the numbering and
85
- // convinced the look-ahead the encoder had produced it.
86
- assert.equal(store.pathOf(KEY, 2), null);
87
- assert.deepEqual(store.provenNumbers(KEY), [0]);
88
- });
89
-
90
- test("a run's own stretch bounds what it unproves", (t) => {
91
- const { store, root } = storeInATempRoot();
92
- t.after(() => rmSync(root, { recursive: true, force: true }));
93
-
94
- store.useFormat(KEY, fmp4Format);
95
- store.directoryFor(KEY);
96
- for (let index = 0; index <= 5; index += 1) {
97
- store.markClosed(KEY, index);
98
- }
99
-
100
- // A run of #0..#0 rewrites #0 and nothing else. Unbounded, this forgot the
101
- // whole film beyond it — and with readiness a projection of what is proven,
102
- // that is an output declaring itself unmade whenever an encoder starts near
103
- // the beginning, which is a fresh encoder for every segment of it.
104
- //
105
- // Asked with no files on the disk, so the successor rule cannot answer for
106
- // the statements and only the statements are under test.
107
- store.forgetClosed(KEY, 0, 0);
108
- assert.equal(store.isClosed(KEY, 0), false, "the one number it will rewrite");
109
- assert.equal(store.isClosed(KEY, 1), true, "and nothing beyond its stretch");
110
- assert.equal(store.isClosed(KEY, 5), true);
111
-
112
- store.forgetClosed(KEY, 3);
113
- assert.equal(store.isClosed(KEY, 2), true);
114
- assert.equal(store.isClosed(KEY, 3), false, "no end given means to the end of the film");
115
- assert.equal(store.isClosed(KEY, 5), false);
116
- });
117
-
118
- test("a directory that has not moved is not read again", (t) => {
119
- const { store, root } = storeInATempRoot();
120
- t.after(() => rmSync(root, { recursive: true, force: true }));
121
-
122
- const dir = store.directoryFor(KEY);
123
- store.useFormat(KEY, fmp4Format);
124
- writeSegment(dir, 0);
125
- writeSegment(dir, 1);
126
-
127
- const first = store.refresh(KEY);
128
- const second = store.refresh(KEY);
129
- assert.equal(first, second, "the same reading is handed back, not a fresh listing");
130
- });
131
-
132
- test("what a killed process left is found, named and counted", (t) => {
133
- const { store, root, lines } = storeInATempRoot();
134
- t.after(() => rmSync(root, { recursive: true, force: true }));
135
-
136
- const dir = path.join(root, directoryNameFor(KEY));
137
- mkdirSync(dir, { recursive: true });
138
- writeFileSync(path.join(dir, "key.txt"), `${KEY}\n`);
139
- writeSegment(dir, 0);
140
- writeSegment(dir, 1);
141
- const orphan = path.join(root, "0123456789abcdef");
142
- mkdirSync(orphan, { recursive: true });
143
- writeSegment(orphan, 0);
144
-
145
- const swept = store.sweep();
146
- assert.equal(swept.directories, 2);
147
- assert.equal(swept.unidentified, 1, "the one with no key file cannot be matched to a request");
148
- assert.equal(swept.segments, 3);
149
- assert.match(lines.join("\n"), /startup sweep/);
150
- assert.match(lines.join("\n"), /ended\s+without anything recording why/);
151
- });
152
-
153
- test("what survived a kill is taken back, minus the piece nothing proves", (t) => {
154
- const { store, root } = storeInATempRoot();
155
- t.after(() => rmSync(root, { recursive: true, force: true }));
156
-
157
- const dir = path.join(root, directoryNameFor(KEY));
158
- mkdirSync(dir, { recursive: true });
159
- writeFileSync(path.join(dir, "key.txt"), `${KEY}\n`);
160
- for (let index = 0; index <= 5; index += 1) {
161
- writeSegment(dir, index);
162
- }
163
-
164
- const taken = store.adoptWhatSurvived(() => fmp4Format);
165
-
166
- assert.equal(taken.adopted, 1);
167
- assert.equal(taken.unprovenRemoved, 1, "the highest number was being written when the run died");
168
- // Five kept rather than six thrown away: a copied segment's bytes depend only
169
- // on the source, and re-encoding them costs a machine already short of it.
170
- assert.deepEqual(store.provenNumbers(KEY), [0, 1, 2, 3, 4]);
171
- assert.equal(store.pathOf(KEY, 5), null);
172
- });
173
-
174
- test("a directory that cannot name itself is thrown away rather than served", (t) => {
175
- const { store, root } = storeInATempRoot();
176
- t.after(() => rmSync(root, { recursive: true, force: true }));
177
-
178
- const orphan = path.join(root, "0123456789abcdef");
179
- mkdirSync(orphan, { recursive: true });
180
- writeSegment(orphan, 0);
181
-
182
- const taken = store.adoptWhatSurvived(() => fmp4Format);
183
-
184
- assert.equal(taken.adopted, 0);
185
- assert.equal(taken.dropped, 1);
186
- assert.equal(readdirSync(root).length, 0);
187
- });
188
-
189
- test("an output this proxy can no longer serve is thrown away too", (t) => {
190
- const { store, root } = storeInATempRoot();
191
- t.after(() => rmSync(root, { recursive: true, force: true }));
192
-
193
- const dir = path.join(root, directoryNameFor(KEY));
194
- mkdirSync(dir, { recursive: true });
195
- writeFileSync(path.join(dir, "key.txt"), `${KEY}\n`);
196
- writeSegment(dir, 0);
197
-
198
- const taken = store.adoptWhatSurvived(() => null);
199
-
200
- assert.equal(taken.dropped, 1);
201
- assert.equal(readdirSync(root).length, 0);
202
- });
203
-
204
- test("what the store weighs is reported, and dropping one output frees it", (t) => {
205
- const { store, root } = storeInATempRoot();
206
- t.after(() => rmSync(root, { recursive: true, force: true }));
207
-
208
- const dir = store.directoryFor(KEY);
209
- store.useFormat(KEY, fmp4Format);
210
- writeSegment(dir, 0, 1000);
211
- writeSegment(dir, 1, 1000);
212
-
213
- assert.equal(store.stats().bytes, 2000);
214
- store.drop(KEY, "nobody is watching it");
215
- assert.equal(store.stats().outputs, 0);
216
- });
1
+ /**
2
+ * @file One store of segments, addressed by what they are, and what it makes of
3
+ * what a killed process left behind.
4
+ */
5
+
6
+ import test from "node:test";
7
+ import assert from "node:assert/strict";
8
+ import { mkdtempSync, mkdirSync, readdirSync, rmSync, writeFileSync } from "node:fs";
9
+ import os from "node:os";
10
+ import path from "node:path";
11
+ import { directoryNameFor, SegmentStore } from "../services/encode/SegmentStore.js";
12
+ import { fmp4Format } from "../services/segment-formats/fmp4.js";
13
+
14
+ /**
15
+ * @returns {{ store: SegmentStore, root: string, lines: string[] }}
16
+ */
17
+ function storeInATempRoot() {
18
+ const root = mkdtempSync(path.join(os.tmpdir(), "segment-store-"));
19
+ const lines = [];
20
+ const store = new SegmentStore({
21
+ root,
22
+ logger: { info: (line) => lines.push(line), warn: (line) => lines.push(line) }
23
+ });
24
+ return { store, root, lines };
25
+ }
26
+
27
+ /**
28
+ * @param {string} dir
29
+ * @param {number} index
30
+ * @param {number} bytes
31
+ */
32
+ function writeSegment(dir, index, bytes = 16) {
33
+ writeFileSync(path.join(dir, fmp4Format.segmentFileName(index)), Buffer.alloc(bytes));
34
+ }
35
+
36
+ const KEY = "torrent:abc:fmt=fmp4:grid=kf@0:video-only:v=0/copy";
37
+
38
+ test("two viewers of one output are given the same directory", (t) => {
39
+ const { store, root } = storeInATempRoot();
40
+ t.after(() => rmSync(root, { recursive: true, force: true }));
41
+
42
+ // Which viewer asked never enters it — the address is what the segments are.
43
+ assert.equal(store.directoryFor(KEY), store.directoryFor(KEY));
44
+ assert.notEqual(store.directoryFor(KEY), store.directoryFor(`${KEY}:other`));
45
+ });
46
+
47
+ test("the directory says what it holds, so a later process can tell", (t) => {
48
+ const { store, root } = storeInATempRoot();
49
+ t.after(() => rmSync(root, { recursive: true, force: true }));
50
+
51
+ const dir = store.directoryFor(KEY);
52
+ const names = readdirSync(dir);
53
+ assert.deepEqual(names, ["key.txt"]);
54
+ });
55
+
56
+ test("a piece under its served name is finished, and the last one too", (t) => {
57
+ const { store, root } = storeInATempRoot();
58
+ t.after(() => rmSync(root, { recursive: true, force: true }));
59
+
60
+ const dir = store.directoryFor(KEY);
61
+ store.useFormat(KEY, fmp4Format);
62
+ writeSegment(dir, 0);
63
+ writeSegment(dir, 1);
64
+ writeSegment(dir, 2);
65
+
66
+ // It was "the NEXT number exists", which left the last piece of every run
67
+ // unprovable for ever and the moment two runs share an output declared a
68
+ // half-written file finished because somebody else had written the one after
69
+ // it. The name is the proof now, so all three count.
70
+ assert.deepEqual(store.provenNumbers(KEY), [0, 1, 2]);
71
+ });
72
+
73
+ test("a piece still being written is not a piece, and its name says so", (t) => {
74
+ const { store, root } = storeInATempRoot();
75
+ t.after(() => rmSync(root, { recursive: true, force: true }));
76
+
77
+ const dir = store.directoryFor(KEY);
78
+ store.useFormat(KEY, fmp4Format);
79
+ writeSegment(dir, 0);
80
+ // What ffmpeg is writing into right now, tagged with the run that opened it.
81
+ writeFileSync(path.join(dir, "making-0-00001.mp4"), Buffer.alloc(64));
82
+
83
+ assert.deepEqual(store.provenNumbers(KEY), [0]);
84
+ assert.equal(store.isClosed(KEY, 1), false);
85
+ assert.equal(store.pathOf(KEY, 1), null);
86
+
87
+ // And closing it is one rename, after which it is servable.
88
+ assert.equal(store.publish(KEY, "making-0-00001.mp4", fmp4Format), "segment-00001.mp4");
89
+ assert.equal(store.isClosed(KEY, 1), true);
90
+ });
91
+
92
+ test("a file of no bytes is not a segment, whatever it is called", (t) => {
93
+ const { store, root } = storeInATempRoot();
94
+ t.after(() => rmSync(root, { recursive: true, force: true }));
95
+
96
+ const dir = store.directoryFor(KEY);
97
+ store.useFormat(KEY, fmp4Format);
98
+ writeSegment(dir, 0);
99
+ writeSegment(dir, 1);
100
+ writeSegment(dir, 2, 0);
101
+
102
+ // A file of no bytes under a served name cannot arise from this proxy any
103
+ // more a piece takes that name only on being closed — but a killed process
104
+ // can leave one, and taking it for a segment once closed the only hole in the
105
+ // numbering and convinced the look-ahead the encoder had produced it.
106
+ assert.equal(store.pathOf(KEY, 2), null);
107
+ assert.deepEqual(store.provenNumbers(KEY), [0, 1]);
108
+ });
109
+
110
+ test("clearing up after one run leaves every other run's work alone", (t) => {
111
+ const { store, root } = storeInATempRoot();
112
+ t.after(() => rmSync(root, { recursive: true, force: true }));
113
+
114
+ const dir = store.directoryFor(KEY);
115
+ store.useFormat(KEY, fmp4Format);
116
+ // Two live runs, each with a piece open, plus finished pieces of both.
117
+ writeSegment(dir, 0);
118
+ writeSegment(dir, 100);
119
+ writeFileSync(path.join(dir, "making-0-00001.mp4"), Buffer.alloc(64));
120
+ writeFileSync(path.join(dir, "making-100-00101.mp4"), Buffer.alloc(64));
121
+
122
+ // The run that began at #0 ends. Its own unfinished piece goes and nothing
123
+ // else does — which the old rule could not manage: it took the highest SERVED
124
+ // name inside the ended run's stretch and judged its bytes, so with the
125
+ // naming rule above it would have removed #0, a piece that run had closed.
126
+ assert.equal(store.clearUpAfter(KEY, 0), 1);
127
+ assert.deepEqual(store.provenNumbers(KEY), [0, 100]);
128
+ assert.equal(
129
+ readdirSync(dir).includes("making-100-00101.mp4"),
130
+ true,
131
+ "the other run is still writing its own"
132
+ );
133
+ });
134
+
135
+ test("a directory that has not moved is not read again", (t) => {
136
+ const { store, root } = storeInATempRoot();
137
+ t.after(() => rmSync(root, { recursive: true, force: true }));
138
+
139
+ const dir = store.directoryFor(KEY);
140
+ store.useFormat(KEY, fmp4Format);
141
+ writeSegment(dir, 0);
142
+ writeSegment(dir, 1);
143
+
144
+ const first = store.refresh(KEY);
145
+ const second = store.refresh(KEY);
146
+ assert.equal(first, second, "the same reading is handed back, not a fresh listing");
147
+ });
148
+
149
+ test("what a killed process left is found, named and counted", (t) => {
150
+ const { store, root, lines } = storeInATempRoot();
151
+ t.after(() => rmSync(root, { recursive: true, force: true }));
152
+
153
+ const dir = path.join(root, directoryNameFor(KEY));
154
+ mkdirSync(dir, { recursive: true });
155
+ writeFileSync(path.join(dir, "key.txt"), `${KEY}\n`);
156
+ writeSegment(dir, 0);
157
+ writeSegment(dir, 1);
158
+ const orphan = path.join(root, "0123456789abcdef");
159
+ mkdirSync(orphan, { recursive: true });
160
+ writeSegment(orphan, 0);
161
+
162
+ const swept = store.sweep();
163
+ assert.equal(swept.directories, 2);
164
+ assert.equal(swept.unidentified, 1, "the one with no key file cannot be matched to a request");
165
+ assert.equal(swept.segments, 3);
166
+ assert.match(lines.join("\n"), /startup sweep/);
167
+ assert.match(lines.join("\n"), /ended\s+without anything recording why/);
168
+ });
169
+
170
+ test("what survived a kill is taken back whole, minus what was still being written", (t) => {
171
+ const { store, root } = storeInATempRoot();
172
+ t.after(() => rmSync(root, { recursive: true, force: true }));
173
+
174
+ const dir = path.join(root, directoryNameFor(KEY));
175
+ mkdirSync(dir, { recursive: true });
176
+ writeFileSync(path.join(dir, "key.txt"), `${KEY}
177
+ `);
178
+ for (let index = 0; index <= 5; index += 1) {
179
+ writeSegment(dir, index);
180
+ }
181
+ // The piece the kernel interrupted. It never had a served name, so it cannot
182
+ // be mistaken for one — which is what the old rule could only guess at, by
183
+ // always throwing the highest number away.
184
+ writeFileSync(path.join(dir, "making-0-00006.mp4"), Buffer.alloc(64));
185
+
186
+ const taken = store.adoptWhatSurvived(() => fmp4Format);
187
+
188
+ assert.equal(taken.adopted, 1);
189
+ assert.equal(taken.unprovenRemoved, 1, "the one under a working name");
190
+ // All six kept rather than five: the highest served name used to be thrown
191
+ // away because nothing could prove it, and on the copy branch that is a piece
192
+ // whose bytes depend only on the source and cost a short machine to remake.
193
+ assert.deepEqual(store.provenNumbers(KEY), [0, 1, 2, 3, 4, 5]);
194
+ });
195
+
196
+ test("a directory that cannot name itself is thrown away rather than served", (t) => {
197
+ const { store, root } = storeInATempRoot();
198
+ t.after(() => rmSync(root, { recursive: true, force: true }));
199
+
200
+ const orphan = path.join(root, "0123456789abcdef");
201
+ mkdirSync(orphan, { recursive: true });
202
+ writeSegment(orphan, 0);
203
+
204
+ const taken = store.adoptWhatSurvived(() => fmp4Format);
205
+
206
+ assert.equal(taken.adopted, 0);
207
+ assert.equal(taken.dropped, 1);
208
+ assert.equal(readdirSync(root).length, 0);
209
+ });
210
+
211
+ test("an output this proxy can no longer serve is thrown away too", (t) => {
212
+ const { store, root } = storeInATempRoot();
213
+ t.after(() => rmSync(root, { recursive: true, force: true }));
214
+
215
+ const dir = path.join(root, directoryNameFor(KEY));
216
+ mkdirSync(dir, { recursive: true });
217
+ writeFileSync(path.join(dir, "key.txt"), `${KEY}\n`);
218
+ writeSegment(dir, 0);
219
+
220
+ const taken = store.adoptWhatSurvived(() => null);
221
+
222
+ assert.equal(taken.dropped, 1);
223
+ assert.equal(readdirSync(root).length, 0);
224
+ });
225
+
226
+ test("what the store weighs is reported, and dropping one output frees it", (t) => {
227
+ const { store, root } = storeInATempRoot();
228
+ t.after(() => rmSync(root, { recursive: true, force: true }));
229
+
230
+ const dir = store.directoryFor(KEY);
231
+ store.useFormat(KEY, fmp4Format);
232
+ writeSegment(dir, 0, 1000);
233
+ writeSegment(dir, 1, 1000);
234
+
235
+ assert.equal(store.stats().bytes, 2000);
236
+ store.drop(KEY, "nobody is watching it");
237
+ assert.equal(store.stats().outputs, 0);
238
+ });
@@ -1,135 +0,0 @@
1
- /**
2
- * @file The piece a run had open when it ended.
3
- *
4
- * A fact of a run's output directory, and therefore of the encoding layer. It
5
- * lived in the eleven-thousand-line file that is being taken apart, where it was
6
- * called by the one place that killed a run; stopping is decided in one place
7
- * now and carried out in another, so the cleanup belongs to the layer that owns
8
- * the directories rather than to whoever happened to do the killing.
9
- */
10
-
11
- import { readdir, readFile, stat, unlink } from "node:fs/promises";
12
- import path from "node:path";
13
-
14
- /**
15
- * Remove the piece a run had open when it ended, if that piece is unusable.
16
- *
17
- * The `segment` muxer creates its output file when it OPENS it and writes into
18
- * it until the next cut, so at any instant exactly one file in a run's
19
- * directory is unfinished: the highest-numbered one. A run that reaches the end
20
- * of its work closes that file and it is a good piece; a run killed for a seek
21
- * does not — measured 2026-09-03, ffmpeg exited 19 ms after SIGTERM and left
22
- * `segment-00025.mp4` at zero bytes, which then closed the only hole in the
23
- * numbering and convinced the look-ahead to keep the encoder stopped for having
24
- * "produced" it.
25
- *
26
- * **A piece is whole only if the run PROVED it, and reading it proves nothing.**
27
- *
28
- * The highest-numbered file in a run's stretch is the one it had open. How a
29
- * run ends decides what became of that file, and all three outcomes leave it
30
- * readable-looking: stopped with SIGTERM, ffmpeg writes it out and names it on
31
- * the ready channel exactly as it names a finished one; killed harder, or dying
32
- * on its own, it leaves the bytes it had written with no name at all. In every
33
- * case the file decodes and holds film only up to the instant the run ended,
34
- * under a number whose playlist entry promises a whole span. Field 2026-09-06:
35
- * `segment-00010.mp4` held 3.92 s of its declared 5.589 s — 96 frames — and the
36
- * picture jumped 1.5 s at 1:02; the soundtrack did the same at 17.5 s, 2.8 s
37
- * wide, in the same session. Both decoded, which is how both reached the viewer.
38
- *
39
- * So the question asked here is not what the file contains but whether the run
40
- * named it while it was still running normally. That is a fact the run holds,
41
- * so there is no span to measure and no tolerance to choose.
42
- *
43
- * A piece finished in the moment between the last such name and the end is
44
- * then made a second time. That is the cheaper error: the other is a hole the
45
- * viewer sees.
46
- *
47
- * @param {string | null | undefined} runDirPath
48
- * @param {{ isSegmentFileName: (name: string) => boolean, segmentIndexFromName: (name: string) => number }} segmentFormat
49
- * @param {((raw: Buffer) => boolean) | null} judgeUsable - Whether a non-empty
50
- * piece carries what it should. Null where nothing can say, and then only an
51
- * empty file is removed.
52
- * @param {string | null} [provenName] - The last piece the run named while it
53
- * was running normally. A file beyond it was open when the run ended and goes
54
- * whether or not it reads. Null where the run proved nothing, and then every
55
- * piece it left is unproven.
56
- * @returns {Promise<number | null>} The segment number removed, or null.
57
- */
58
- export async function discardOpenPiece(runDirPath, segmentFormat, within, judgeUsable, provenName = null) {
59
- if (!runDirPath || typeof segmentFormat?.isSegmentFileName !== "function") {
60
- return null;
61
- }
62
- // Only inside the stretch the ended run was given. Every run of an output
63
- // writes into one directory now — they are kept apart by their intervals
64
- // rather than by a directory each — so the highest-numbered file in there may
65
- // belong to a run that is still going, and removing it would take away a
66
- // piece somebody is producing.
67
- const from = Number.isInteger(within?.from) ? within.from : 0;
68
- // THE RUN'S OWN REACH, which is a fact it holds and needs nothing measured.
69
- //
70
- // A run cannot have opened a file above the one just past the last it named:
71
- // ffmpeg names a piece when it closes it and opens the next, so the open piece
72
- // is at most `proven + 1`, and where it named nothing at all the open one is
73
- // the first it was given.
74
- //
75
- // Used only where no end was declared — `to` below `from`, which is how "to
76
- // the end of the track" is written everywhere here. Such a run had no bound at
77
- // all: the search covered the whole directory and took the highest-numbered
78
- // file in it. Every run of an output writes into that one
79
- // directory, so what it took was a piece a LIVE run had just finished. Field
80
- // 2026-09-06: the piece holding 2:47-2:57 went that way, its number is spent
81
- // for good because names only grow, and the picture stood still for 647 s.
82
- const provenIndex = typeof provenName === "string" && segmentFormat.isSegmentFileName(provenName)
83
- ? segmentFormat.segmentIndexFromName(provenName)
84
- : null;
85
- const reach = Number.isInteger(provenIndex) && provenIndex >= from ? provenIndex + 1 : from;
86
- // The declared end is the bound wherever there is one. The run's own reach is
87
- // the bound of LAST RESORT, for a run given none: before it, such a run had no
88
- // bound at all and the search covered the whole directory.
89
- const to = Number.isInteger(within?.to) && within.to >= from ? within.to : reach;
90
- let highest = null;
91
- try {
92
- for (const name of await readdir(runDirPath)) {
93
- if (!segmentFormat.isSegmentFileName(name)) {
94
- continue;
95
- }
96
- const index = segmentFormat.segmentIndexFromName(name);
97
- if (index < from || index > to) {
98
- continue;
99
- }
100
- if (index >= 0 && (highest === null || index > highest.index)) {
101
- highest = { index, name };
102
- }
103
- }
104
- } catch {
105
- return null; // The run wrote nothing, or its directory is already gone.
106
- }
107
- if (highest === null) {
108
- return null;
109
- }
110
- const filePath = path.join(runDirPath, highest.name);
111
- // Proven finished only if the run said so while it was running. Anything
112
- // beyond that name was open when the run ended, and its contents cannot say
113
- // so — it decodes.
114
- const proven = typeof provenName === "string" && provenName === highest.name;
115
- let unusable = !proven;
116
- try {
117
- const info = await stat(filePath);
118
- if (info.size === 0) {
119
- unusable = true;
120
- } else if (!unusable && typeof judgeUsable === "function") {
121
- unusable = !judgeUsable(await readFile(filePath));
122
- }
123
- } catch {
124
- return null; // Gone between the listing and the question.
125
- }
126
- if (!unusable) {
127
- return null;
128
- }
129
- try {
130
- await unlink(filePath);
131
- return highest.index;
132
- } catch {
133
- return null; // Already removed.
134
- }
135
- }