@torrent-tv/proxy 2.83.2 → 2.83.4
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 +24 -0
- package/package.json +1 -1
- package/research/handover-reader-claims-removal-2026-09-12.md +166 -0
- package/research/priority-map-is-the-truth-2026-09-12.md +207 -0
- package/routes/api/sources/files/get.js +23 -9
- package/routes/api/sources/warm/post.js +26 -22
- package/routes/api/transcode-sessions/post.js +1 -49
- package/routes/stream/get.js +2 -36
- package/services/controllers/SubtitleController.js +0 -3
- package/services/hls-session-manager.js +3 -68
- package/services/playback-planner.js +12 -13
- package/services/priority/PriorityOrchestrator.js +40 -6
- package/services/torrent/Contents.js +324 -0
- package/services/torrent/files.js +8 -1
- package/services/torrent-pool.js +378 -187
- package/services/torrent-worker/client.js +0 -24
- package/services/torrent-worker/pool-adapter.js +3 -33
- package/services/torrent-worker/protocol.js +0 -4
- package/services/torrent-worker/worker.js +52 -59
- package/test/file-edges.test.js +191 -0
- package/test/priority-map-emptied.test.js +207 -0
- package/test/source-files-route.test.js +103 -0
- package/test/stream-route.test.js +4 -8
- package/test/swarm-follows-readers.test.js +107 -101
- package/test/torrent-contents.test.js +235 -0
- package/test/upload-hurry.test.js +66 -28
- package/services/torrent-worker/file-claims.js +0 -91
- package/test/file-claims.test.js +0 -64
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file What one torrent says is in it: its pictures, what belongs to each, and
|
|
3
|
+
* the order a person reads them in.
|
|
4
|
+
*
|
|
5
|
+
* The fixtures are real releases. `Drifters` ships twelve episodes in the root
|
|
6
|
+
* with a Russian soundtrack per episode under `Rus Sound/` and a subtitle file
|
|
7
|
+
* per episode under `Sub/[group]/`; the single-film shape ships one `.mkv` with
|
|
8
|
+
* a dub whose name has nothing in common with it. Both are the cases the
|
|
9
|
+
* pairing rules beside this were written against, and this file is about what
|
|
10
|
+
* is built ON them — the grouping, the order and the leftovers.
|
|
11
|
+
*
|
|
12
|
+
* Measured on the survey collection, 2026-09-12, and recorded here because it
|
|
13
|
+
* is what says the grouping is not an invention: 134 torrents, 44 of them with
|
|
14
|
+
* more than one picture, 1357 items and 421 files paired to one. Not a single
|
|
15
|
+
* file was paired to two pictures, and in all 1357 the grouping agreed with the
|
|
16
|
+
* per-picture call it replaces.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import test from "node:test";
|
|
20
|
+
import assert from "node:assert/strict";
|
|
21
|
+
import { TorrentContents, contentsOf } from "../services/torrent/Contents.js";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The Drifters torrent, as WebTorrent reports it: every path prefixed with the
|
|
25
|
+
* torrent's own name, and the episodes in the order the tool that made it chose
|
|
26
|
+
* — which is not the order anybody reads them in.
|
|
27
|
+
*
|
|
28
|
+
* @param {number[]} episodes
|
|
29
|
+
* @returns {Array<{ path: string, name: string, length: number }>}
|
|
30
|
+
*/
|
|
31
|
+
function driftersFiles(episodes) {
|
|
32
|
+
const files = [];
|
|
33
|
+
const push = (relative, length) => {
|
|
34
|
+
const name = relative.slice(relative.lastIndexOf("/") + 1);
|
|
35
|
+
files.push({ path: `Drifters/${relative}`, name, length });
|
|
36
|
+
};
|
|
37
|
+
for (const episode of episodes) {
|
|
38
|
+
const stem = `[HorribleSubs] Drifters - ${String(episode).padStart(2, "0")} [1080p]`;
|
|
39
|
+
push(`Sub/[Stan WarHammer & Nesitach]/${stem}.ass`, 29_000);
|
|
40
|
+
push(`Rus Sound/${stem}.mka`, 30_000_000);
|
|
41
|
+
push(`${stem}.mkv`, 566_000_000);
|
|
42
|
+
}
|
|
43
|
+
return files;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
test("every picture is an item, with its own sound and subtitles on it", () => {
|
|
47
|
+
const contents = new TorrentContents({ files: driftersFiles([1, 2]), name: "Drifters" });
|
|
48
|
+
|
|
49
|
+
assert.equal(contents.videoCount, 2);
|
|
50
|
+
assert.equal(contents.items.length, 2);
|
|
51
|
+
const [first, second] = contents.items;
|
|
52
|
+
assert.match(first.name, / - 01 /);
|
|
53
|
+
assert.match(second.name, / - 02 /);
|
|
54
|
+
assert.deepEqual(
|
|
55
|
+
first.audio.map((file) => file.name),
|
|
56
|
+
["[HorribleSubs] Drifters - 01 [1080p].mka"]
|
|
57
|
+
);
|
|
58
|
+
assert.deepEqual(
|
|
59
|
+
first.subtitles.map((file) => file.name),
|
|
60
|
+
["[HorribleSubs] Drifters - 01 [1080p].ass"]
|
|
61
|
+
);
|
|
62
|
+
// The sound of episode 1 must never appear on the picture of episode 2: the
|
|
63
|
+
// wrong pairing is silent everywhere downstream.
|
|
64
|
+
assert.deepEqual(
|
|
65
|
+
second.audio.map((file) => file.name),
|
|
66
|
+
["[HorribleSubs] Drifters - 02 [1080p].mka"]
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("items come out in the order a person reads them, not the torrent's own", () => {
|
|
71
|
+
// A real release lists its episodes 08, 06, 07, 01 — the order of whatever
|
|
72
|
+
// tool made the torrent, routinely by size.
|
|
73
|
+
const contents = new TorrentContents({ files: driftersFiles([8, 6, 10, 2]), name: "Drifters" });
|
|
74
|
+
|
|
75
|
+
assert.deepEqual(
|
|
76
|
+
contents.items.map((item) => item.name.match(/ - (\d+) /)[1]),
|
|
77
|
+
["02", "06", "08", "10"]
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("runs of digits are compared as numbers, so 2 comes before 10", () => {
|
|
82
|
+
const files = [
|
|
83
|
+
{ path: "Show/ep10.mkv", name: "ep10.mkv", length: 9 },
|
|
84
|
+
{ path: "Show/ep2.mkv", name: "ep2.mkv", length: 9 }
|
|
85
|
+
];
|
|
86
|
+
const contents = new TorrentContents({ files, name: "Show" });
|
|
87
|
+
|
|
88
|
+
assert.deepEqual(
|
|
89
|
+
contents.items.map((item) => item.name),
|
|
90
|
+
["ep2.mkv", "ep10.mkv"]
|
|
91
|
+
);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("an item keeps the torrent's own index, whatever order it is read in", () => {
|
|
95
|
+
const contents = new TorrentContents({ files: driftersFiles([8, 2]), name: "Drifters" });
|
|
96
|
+
|
|
97
|
+
// Episode 2 is read first and is the fifth file of the torrent.
|
|
98
|
+
assert.equal(contents.items[0].fileIndex, 5);
|
|
99
|
+
assert.equal(contents.items[1].fileIndex, 2);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("a file is answered for by the item it belongs to, as a picture or as a part", () => {
|
|
103
|
+
const contents = new TorrentContents({ files: driftersFiles([1]), name: "Drifters" });
|
|
104
|
+
const [item] = contents.items;
|
|
105
|
+
|
|
106
|
+
assert.equal(contents.itemOf(item.fileIndex), item);
|
|
107
|
+
assert.equal(contents.itemOf(item.audio[0].fileIndex), item);
|
|
108
|
+
assert.equal(contents.itemOf(item.subtitles[0].fileIndex), item);
|
|
109
|
+
assert.equal(contents.itemOf(404), null);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("what belongs beside one picture is answered from what was decided once", () => {
|
|
113
|
+
const contents = new TorrentContents({ files: driftersFiles([1, 2]), name: "Drifters" });
|
|
114
|
+
const [first] = contents.items;
|
|
115
|
+
|
|
116
|
+
const beside = contents.sidecarsOf(first.fileIndex);
|
|
117
|
+
assert.deepEqual(beside.audio, first.audio);
|
|
118
|
+
assert.deepEqual(beside.subtitles, first.subtitles);
|
|
119
|
+
// Asked about a file that is not a picture, the answer is nothing rather than
|
|
120
|
+
// the item that file happens to sit in: the question is "what goes beside
|
|
121
|
+
// this picture", and a soundtrack is not one.
|
|
122
|
+
assert.deepEqual(contents.sidecarsOf(first.audio[0].fileIndex), {
|
|
123
|
+
audio: [],
|
|
124
|
+
subtitles: [],
|
|
125
|
+
images: []
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("a torrent with one picture takes the dub whose name has nothing in common", () => {
|
|
130
|
+
const files = [
|
|
131
|
+
{ path: "Film/Film.2024.1080p.mkv", name: "Film.2024.1080p.mkv", length: 4_000_000_000 },
|
|
132
|
+
{ path: "Film/Rus Sound/dub.mka", name: "dub.mka", length: 30_000_000 }
|
|
133
|
+
];
|
|
134
|
+
const contents = new TorrentContents({ files, name: "Film" });
|
|
135
|
+
|
|
136
|
+
assert.equal(contents.items.length, 1);
|
|
137
|
+
assert.deepEqual(
|
|
138
|
+
contents.items[0].audio.map((file) => file.name),
|
|
139
|
+
["dub.mka"]
|
|
140
|
+
);
|
|
141
|
+
assert.deepEqual(contents.leftovers, []);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("what belongs to no picture is listed as such, and nothing is lost", () => {
|
|
145
|
+
// `.nfo` is the release's own note and belongs to nothing. A `.txt` would
|
|
146
|
+
// NOT do here: the pairing layer counts it among the subtitle formats, and a
|
|
147
|
+
// torrent with one picture takes every sidecar there is.
|
|
148
|
+
const files = [
|
|
149
|
+
{ path: "Film/Film.mkv", name: "Film.mkv", length: 9 },
|
|
150
|
+
{ path: "Film/release.nfo", name: "release.nfo", length: 1 },
|
|
151
|
+
{ path: "Film/Screens/unrelated.jpg", name: "unrelated.jpg", length: 1 }
|
|
152
|
+
];
|
|
153
|
+
const contents = new TorrentContents({ files, name: "Film" });
|
|
154
|
+
|
|
155
|
+
assert.deepEqual(
|
|
156
|
+
contents.leftovers.map((file) => file.relativePath),
|
|
157
|
+
["release.nfo", "Screens/unrelated.jpg"]
|
|
158
|
+
);
|
|
159
|
+
const accounted =
|
|
160
|
+
contents.items.length +
|
|
161
|
+
contents.items.reduce(
|
|
162
|
+
(count, item) => count + item.audio.length + item.subtitles.length + item.images.length,
|
|
163
|
+
0
|
|
164
|
+
) +
|
|
165
|
+
contents.leftovers.length;
|
|
166
|
+
assert.equal(accounted, files.length);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test("a torrent whose metadata has not arrived says it holds nothing", () => {
|
|
170
|
+
const contents = new TorrentContents({ files: [], name: "" });
|
|
171
|
+
|
|
172
|
+
assert.deepEqual(contents.items, []);
|
|
173
|
+
assert.deepEqual(contents.leftovers, []);
|
|
174
|
+
assert.equal(contents.videoCount, 0);
|
|
175
|
+
assert.equal(contents.itemOf(0), null);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("the answer is worked out once per torrent, and again when its files arrive", () => {
|
|
179
|
+
const torrent = { name: "Drifters", files: [] };
|
|
180
|
+
|
|
181
|
+
const empty = contentsOf(torrent);
|
|
182
|
+
assert.equal(contentsOf(torrent), empty, "asking twice must not work it out twice");
|
|
183
|
+
|
|
184
|
+
// A magnet has no files until its metadata lands, which happens exactly once.
|
|
185
|
+
torrent.files = driftersFiles([1]);
|
|
186
|
+
const full = contentsOf(torrent);
|
|
187
|
+
assert.notEqual(full, empty);
|
|
188
|
+
assert.equal(full.items.length, 1);
|
|
189
|
+
assert.equal(contentsOf(torrent), full);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test("folders order before names, so seasons stay together", () => {
|
|
193
|
+
const files = [
|
|
194
|
+
{ path: "Show/Season 2/ep 1.mkv", length: 9 },
|
|
195
|
+
{ path: "Show/Season 10/ep 1.mkv", length: 9 },
|
|
196
|
+
{ path: "Show/Season 1/ep 2.mkv", length: 9 },
|
|
197
|
+
{ path: "Show/Season 1/ep 1.mkv", length: 9 }
|
|
198
|
+
];
|
|
199
|
+
const contents = new TorrentContents({ files, name: "Show" });
|
|
200
|
+
|
|
201
|
+
assert.deepEqual(
|
|
202
|
+
contents.items.map((item) => item.relativePath),
|
|
203
|
+
["Season 1/ep 1.mkv", "Season 1/ep 2.mkv", "Season 2/ep 1.mkv", "Season 10/ep 1.mkv"]
|
|
204
|
+
);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test("every file is described by what it is, in the order a person reads them", () => {
|
|
208
|
+
// What the browser is given. It used to answer this itself — a list of video
|
|
209
|
+
// extensions in its parser and a second, shorter pair inside its picker —
|
|
210
|
+
// against this one, and the three had already diverged.
|
|
211
|
+
const files = [
|
|
212
|
+
{ path: "Film/notes.nfo", length: 1 },
|
|
213
|
+
{ path: "Film/cover.jpg", length: 2 },
|
|
214
|
+
{ path: "Film/film.mkv", length: 9 },
|
|
215
|
+
{ path: "Film/Rus Sound/dub.mka", length: 3 },
|
|
216
|
+
{ path: "Film/Sub/film.ass", length: 1 }
|
|
217
|
+
];
|
|
218
|
+
const contents = new TorrentContents({ files, name: "Film" });
|
|
219
|
+
|
|
220
|
+
assert.deepEqual(
|
|
221
|
+
contents.files().map((file) => [file.relativePath, file.kind]),
|
|
222
|
+
[
|
|
223
|
+
["cover.jpg", "image"],
|
|
224
|
+
["film.mkv", "video"],
|
|
225
|
+
["notes.nfo", "other"],
|
|
226
|
+
["Rus Sound/dub.mka", "audio"],
|
|
227
|
+
["Sub/film.ass", "subtitle"]
|
|
228
|
+
]
|
|
229
|
+
);
|
|
230
|
+
assert.deepEqual(
|
|
231
|
+
contents.files().map((file) => file.fileIndex),
|
|
232
|
+
[1, 2, 0, 3, 4],
|
|
233
|
+
"the torrent's own numbers travel with them"
|
|
234
|
+
);
|
|
235
|
+
});
|
|
@@ -18,6 +18,40 @@
|
|
|
18
18
|
import test from "node:test";
|
|
19
19
|
import assert from "node:assert/strict";
|
|
20
20
|
import { decideUploadLimit, torrentsForUploadPolicy } from "../services/torrent-pool.js";
|
|
21
|
+
import { demandFor, forgetTorrent } from "../services/download/registry.js";
|
|
22
|
+
import { Urgency } from "../services/demand/index.js";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A torrent whose metadata has arrived and which nothing is wanted of.
|
|
26
|
+
*
|
|
27
|
+
* The files matter: a torrent with no list of files cannot be stated about at
|
|
28
|
+
* all, and is wanted for that reason alone — it is still being fetched.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} name
|
|
31
|
+
* @returns {object}
|
|
32
|
+
*/
|
|
33
|
+
function quiet(name) {
|
|
34
|
+
return { name, files: [{ offset: 0, length: 1_000, name }] };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The same, with something stated for it — which is what "somebody wants this"
|
|
39
|
+
* means now that nothing counts readers.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} name
|
|
42
|
+
* @returns {object}
|
|
43
|
+
*/
|
|
44
|
+
function wanted(name) {
|
|
45
|
+
const torrent = quiet(name);
|
|
46
|
+
demandFor(torrent).register.state({
|
|
47
|
+
claimant: "priority-map:0:0",
|
|
48
|
+
fileIndex: 0,
|
|
49
|
+
byteStart: 0,
|
|
50
|
+
byteEnd: 999,
|
|
51
|
+
urgency: Urgency.NEAR
|
|
52
|
+
});
|
|
53
|
+
return torrent;
|
|
54
|
+
}
|
|
21
55
|
|
|
22
56
|
const NOW = 1_000_000;
|
|
23
57
|
const healthy = (extra = {}) => ({
|
|
@@ -65,34 +99,38 @@ test("the reciprocity boost still works when no hurry is on", () => {
|
|
|
65
99
|
assert.match(decision.reason, /earn unchoke/);
|
|
66
100
|
});
|
|
67
101
|
|
|
68
|
-
test("a torrent
|
|
102
|
+
test("a torrent nothing is wanted of still reaches the policy while it is in a hurry", () => {
|
|
69
103
|
// The moment that matters: a torrent has just been added and its head and
|
|
70
|
-
// tail are being fetched for the codec probe.
|
|
71
|
-
//
|
|
72
|
-
//
|
|
73
|
-
const hurrying =
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
104
|
+
// tail are being fetched for the codec probe. Nothing is stated for it yet,
|
|
105
|
+
// and the selection only ever kept torrents something was — which is where
|
|
106
|
+
// the gap was.
|
|
107
|
+
const hurrying = quiet("film.mkv");
|
|
108
|
+
hurrying.hurryUntil = NOW + 20_000;
|
|
109
|
+
const idle = quiet("other.mkv");
|
|
110
|
+
const watched = wanted("watched.mkv");
|
|
111
|
+
try {
|
|
112
|
+
assert.deepEqual(
|
|
113
|
+
torrentsForUploadPolicy([hurrying, idle], NOW),
|
|
114
|
+
[hurrying],
|
|
115
|
+
"a torrent nothing is stated for yet was ignored"
|
|
116
|
+
);
|
|
82
117
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
118
|
+
assert.deepEqual(
|
|
119
|
+
torrentsForUploadPolicy([{ ...hurrying, hurryUntil: NOW - 1 }, idle], NOW),
|
|
120
|
+
[],
|
|
121
|
+
"and stops counting once the rush is over"
|
|
122
|
+
);
|
|
88
123
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
124
|
+
assert.deepEqual(
|
|
125
|
+
torrentsForUploadPolicy([watched], NOW),
|
|
126
|
+
[watched],
|
|
127
|
+
"a torrent somebody wants something of still counts, hurry or not"
|
|
128
|
+
);
|
|
129
|
+
} finally {
|
|
130
|
+
forgetTorrent(hurrying);
|
|
131
|
+
forgetTorrent(idle);
|
|
132
|
+
forgetTorrent(watched);
|
|
133
|
+
}
|
|
96
134
|
});
|
|
97
135
|
|
|
98
136
|
test("a torrent nobody is reading is not treated as starving", () => {
|
|
@@ -102,7 +140,7 @@ test("a torrent nobody is reading is not treated as starving", () => {
|
|
|
102
140
|
// minutes, every one of them reported as `earn unchoke ... down=0KB/s`.
|
|
103
141
|
const idleButChoked = {
|
|
104
142
|
name: "film.mkv",
|
|
105
|
-
|
|
143
|
+
isWanted: false,
|
|
106
144
|
wires: [
|
|
107
145
|
{ amInterested: true, peerChoking: true },
|
|
108
146
|
{ amInterested: true, peerChoking: true }
|
|
@@ -112,7 +150,7 @@ test("a torrent nobody is reading is not treated as starving", () => {
|
|
|
112
150
|
};
|
|
113
151
|
assert.equal(decideUploadLimit([idleButChoked], { now: NOW }).bytesPerSec, 50 * 1024);
|
|
114
152
|
|
|
115
|
-
const waiting = { ...idleButChoked,
|
|
153
|
+
const waiting = { ...idleButChoked, isWanted: true };
|
|
116
154
|
assert.equal(
|
|
117
155
|
decideUploadLimit([waiting], { now: NOW }).bytesPerSec,
|
|
118
156
|
512 * 1024,
|
|
@@ -128,7 +166,7 @@ test("a torrent short of nothing is not worth uploading for", () => {
|
|
|
128
166
|
// buying while somebody is still short of bytes.
|
|
129
167
|
const complete = {
|
|
130
168
|
name: "watched to the end",
|
|
131
|
-
|
|
169
|
+
isWanted: true,
|
|
132
170
|
hasUnmetDemand: false,
|
|
133
171
|
done: false,
|
|
134
172
|
downloadSpeed: 0,
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Who is holding which file open.
|
|
3
|
-
*
|
|
4
|
-
* A claim keeps a file's data from being removed while something is reading it.
|
|
5
|
-
* Until 2.9.77 claims were keyed by `sourceKey:fileIndex`, which quietly made
|
|
6
|
-
* them **shared**: a second reader of the same file found the key taken and
|
|
7
|
-
* added nothing, so the first reader to finish released the claim out from
|
|
8
|
-
* under the second. The proxy reads one file from several places at once —
|
|
9
|
-
* ffmpeg's input, the keyframe index, the codec probe, a second viewer — so
|
|
10
|
-
* this was the normal case, not an edge one.
|
|
11
|
-
*
|
|
12
|
-
* The fix is not a counter. A counter restores the arithmetic but keeps the
|
|
13
|
-
* ambiguity: a release names a file, not a claim, so a duplicate or late
|
|
14
|
-
* release still decrements someone else's hold and nothing can detect it. Here
|
|
15
|
-
* every claim gets its own identity and a release names exactly that claim —
|
|
16
|
-
* so a stray release matches nothing, is reported, and harms no one.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* @typedef {object} FileClaims
|
|
21
|
-
* @property {(sourceKey: string, fileIndex: number, release: () => void) => string} open
|
|
22
|
-
* @property {(claimId: string) => boolean} close
|
|
23
|
-
* @property {() => void} closeAll
|
|
24
|
-
* @property {number} size
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Track file claims by identity.
|
|
29
|
-
*
|
|
30
|
-
* @returns {FileClaims}
|
|
31
|
-
*/
|
|
32
|
-
export function createFileClaims() {
|
|
33
|
-
/** Claim id → the function that releases that one claim. */
|
|
34
|
-
const claims = new Map();
|
|
35
|
-
let counter = 0;
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
/**
|
|
39
|
-
* Record a new claim and return its identity.
|
|
40
|
-
*
|
|
41
|
-
* Each call is a distinct claim even for the same file — that is the whole
|
|
42
|
-
* point.
|
|
43
|
-
*
|
|
44
|
-
* @param {string} sourceKey
|
|
45
|
-
* @param {number} fileIndex
|
|
46
|
-
* @param {() => void} release
|
|
47
|
-
* @returns {string}
|
|
48
|
-
*/
|
|
49
|
-
open(sourceKey, fileIndex, release) {
|
|
50
|
-
counter += 1;
|
|
51
|
-
// The file is in the id purely so a log line reads usefully; matching is
|
|
52
|
-
// on the whole string.
|
|
53
|
-
const claimId = `${sourceKey}:${fileIndex}:${counter}`;
|
|
54
|
-
claims.set(claimId, release);
|
|
55
|
-
return claimId;
|
|
56
|
-
},
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Release one claim. Returns false when there was no such claim, which is
|
|
60
|
-
* worth logging: it means a release arrived twice, or after teardown.
|
|
61
|
-
*
|
|
62
|
-
* @param {string} claimId
|
|
63
|
-
* @returns {boolean}
|
|
64
|
-
*/
|
|
65
|
-
close(claimId) {
|
|
66
|
-
const release = claims.get(claimId);
|
|
67
|
-
if (!release) {
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
|
-
claims.delete(claimId);
|
|
71
|
-
release();
|
|
72
|
-
return true;
|
|
73
|
-
},
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Release everything — the worker is shutting down.
|
|
77
|
-
*
|
|
78
|
-
* @returns {void}
|
|
79
|
-
*/
|
|
80
|
-
closeAll() {
|
|
81
|
-
for (const [, release] of claims) {
|
|
82
|
-
release();
|
|
83
|
-
}
|
|
84
|
-
claims.clear();
|
|
85
|
-
},
|
|
86
|
-
|
|
87
|
-
get size() {
|
|
88
|
-
return claims.size;
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
}
|
package/test/file-claims.test.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file File claims must be held per reader, not per file.
|
|
3
|
-
*
|
|
4
|
-
* The proxy reads one file from several places at once — ffmpeg's input, the
|
|
5
|
-
* keyframe index, the codec probe, a second viewer. Keying claims by file made
|
|
6
|
-
* them shared, so the first reader to finish released the hold while the others
|
|
7
|
-
* were still reading, and the file's data could then be removed under them.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import test from "node:test";
|
|
11
|
-
import assert from "node:assert/strict";
|
|
12
|
-
import { createFileClaims } from "../services/torrent-worker/file-claims.js";
|
|
13
|
-
|
|
14
|
-
test("two readers of one file hold two claims", () => {
|
|
15
|
-
const claims = createFileClaims();
|
|
16
|
-
let released = 0;
|
|
17
|
-
|
|
18
|
-
const first = claims.open("source", 0, () => (released += 1));
|
|
19
|
-
const second = claims.open("source", 0, () => (released += 1));
|
|
20
|
-
|
|
21
|
-
assert.notEqual(first, second, "the second reader reused the first one's claim");
|
|
22
|
-
assert.equal(claims.size, 2);
|
|
23
|
-
|
|
24
|
-
claims.close(first);
|
|
25
|
-
assert.equal(released, 1, "closing one claim released more than one hold");
|
|
26
|
-
assert.equal(claims.size, 1, "the second reader's claim went with the first");
|
|
27
|
-
|
|
28
|
-
claims.close(second);
|
|
29
|
-
assert.equal(released, 2);
|
|
30
|
-
assert.equal(claims.size, 0);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test("a repeated release affects nothing and reports itself", () => {
|
|
34
|
-
const claims = createFileClaims();
|
|
35
|
-
let released = 0;
|
|
36
|
-
const claimId = claims.open("source", 3, () => (released += 1));
|
|
37
|
-
|
|
38
|
-
assert.equal(claims.close(claimId), true);
|
|
39
|
-
assert.equal(claims.close(claimId), false, "a second release was accepted as valid");
|
|
40
|
-
assert.equal(released, 1, "the hold was released twice");
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
test("a release naming nothing is rejected rather than guessed at", () => {
|
|
44
|
-
const claims = createFileClaims();
|
|
45
|
-
let released = 0;
|
|
46
|
-
claims.open("source", 0, () => (released += 1));
|
|
47
|
-
|
|
48
|
-
assert.equal(claims.close("source:0:999"), false);
|
|
49
|
-
assert.equal(released, 0, "an unknown claim released a real hold");
|
|
50
|
-
assert.equal(claims.size, 1);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
test("teardown releases every outstanding claim", () => {
|
|
54
|
-
const claims = createFileClaims();
|
|
55
|
-
let released = 0;
|
|
56
|
-
claims.open("a", 0, () => (released += 1));
|
|
57
|
-
claims.open("a", 1, () => (released += 1));
|
|
58
|
-
claims.open("b", 0, () => (released += 1));
|
|
59
|
-
|
|
60
|
-
claims.closeAll();
|
|
61
|
-
|
|
62
|
-
assert.equal(released, 3);
|
|
63
|
-
assert.equal(claims.size, 0);
|
|
64
|
-
});
|