@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.
- package/CHANGELOG.md +10 -0
- package/docs/encode-architecture.md +51 -2
- package/package.json +1 -1
- package/services/encode/EncodeRun.js +12 -6
- package/services/encode/SegmentStore.js +147 -223
- package/services/encode/run-command.js +16 -1
- package/services/hls-session-manager.js +5 -83
- package/services/orchestrators/EncodeOrchestrator.js +48 -37
- package/services/segment-formats/fmp4.js +54 -0
- package/services/segment-formats/mpegts.js +54 -0
- package/test/coverage-follows-the-disk.test.js +5 -1
- package/test/orchestrator-wired.test.js +5 -1
- package/test/produced-copy-choice.test.js +258 -358
- package/test/segment-serve-wiring.test.js +8 -9
- package/test/segment-store.test.js +238 -216
- package/services/encode/open-piece.js +0 -135
- package/test/open-piece.test.js +0 -152
|
@@ -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:
|
|
294
|
-
//
|
|
295
|
-
// proven the moment it is finished and the
|
|
296
|
-
// This used to be inferred from the
|
|
297
|
-
//
|
|
298
|
-
|
|
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
|
|
349
|
-
//
|
|
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
|
|
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
|
-
//
|
|
67
|
-
//
|
|
68
|
-
//
|
|
69
|
-
|
|
70
|
-
assert.
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
test("a
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
//
|
|
103
|
-
//
|
|
104
|
-
//
|
|
105
|
-
//
|
|
106
|
-
|
|
107
|
-
store.
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const
|
|
146
|
-
assert.equal(
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
assert.
|
|
167
|
-
assert.
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
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
|
-
}
|