@torrent-tv/proxy 2.83.3 → 2.83.5

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 (39) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/package.json +1 -1
  3. package/research/handover-reader-claims-removal-2026-09-12.md +166 -0
  4. package/research/piece-withdrawn-but-still-claimed-2026-09-12.md +175 -0
  5. package/research/priority-map-is-the-truth-2026-09-12.md +225 -0
  6. package/routes/api/sources/files/get.js +23 -9
  7. package/routes/api/sources/warm/post.js +26 -22
  8. package/routes/api/transcode-sessions/post.js +1 -49
  9. package/routes/stream/get.js +2 -36
  10. package/services/controllers/SubtitleController.js +0 -3
  11. package/services/download/withdraw-claim.js +80 -0
  12. package/services/hls-session-manager.js +17 -110
  13. package/services/orchestrators/EncodeOrchestrator.js +133 -0
  14. package/services/piece-store/piece-disk-store.js +24 -1
  15. package/services/piece-store/shared-piece-store.js +71 -1
  16. package/services/playback-planner.js +12 -13
  17. package/services/priority/PriorityOrchestrator.js +40 -6
  18. package/services/torrent/Contents.js +324 -0
  19. package/services/torrent/files.js +8 -1
  20. package/services/torrent-pool.js +378 -102
  21. package/services/torrent-worker/client.js +0 -24
  22. package/services/torrent-worker/piece-reader.js +30 -1
  23. package/services/torrent-worker/pool-adapter.js +3 -33
  24. package/services/torrent-worker/protocol.js +0 -4
  25. package/services/torrent-worker/worker.js +60 -60
  26. package/test/file-edges.test.js +191 -0
  27. package/test/input-lost-quiets-the-plan.test.js +261 -0
  28. package/test/logger-repeats.test.js +120 -0
  29. package/test/priority-map-emptied.test.js +207 -0
  30. package/test/read-survives-withdrawal.test.js +164 -0
  31. package/test/source-files-route.test.js +103 -0
  32. package/test/stream-route.test.js +4 -8
  33. package/test/swarm-follows-readers.test.js +96 -14
  34. package/test/torrent-contents.test.js +235 -0
  35. package/test/upload-hurry.test.js +66 -28
  36. package/test/withdraw-piece-claim.test.js +212 -0
  37. package/utils/logger.js +105 -7
  38. package/services/torrent-worker/file-claims.js +0 -91
  39. package/test/file-claims.test.js +0 -64
@@ -0,0 +1,324 @@
1
+ /**
2
+ * @file WHAT IS IN ONE TORRENT — its files, which of them carry a picture, and
3
+ * which files belong to which picture.
4
+ *
5
+ * The torrent's own statement about itself, held instead of recomputed. The
6
+ * pairing rules already lived beside this file, as functions; what did not
7
+ * exist was anywhere to keep their answer, so every caller worked it out again
8
+ * — the warm-up on one path and the playback plan on another, over the same
9
+ * list, several times per opened film.
10
+ *
11
+ * It is a class rather than a set of functions because the fact it holds passes
12
+ * the test this project uses for that: the composition is born with the file
13
+ * list, dies with the record of the torrent, and is addressed by one name.
14
+ *
15
+ * **What it must NOT hold**, and the boundary is the point:
16
+ *
17
+ * - nothing about a FILM. A poster, a title, a description are answers from a
18
+ * third party about an identity, and that identity comes from a file's own
19
+ * bytes rather than from the torrent's list of names. They are keyed by the
20
+ * picture's file, arrive late or not at all, and must never delay playback;
21
+ * - no reference to another layer. Not a container, whose life is a file's and
22
+ * which reads bytes; not a priority map, which is built from viewers and
23
+ * lives above this; not a viewer. What leaves here is plain values — indices
24
+ * into the torrent's own list — and whoever takes them holds nothing of this
25
+ * object.
26
+ *
27
+ * **What was measured and deliberately NOT built**, so that it is not proposed
28
+ * again as an oversight: telling a real episode from an extra. Over the 134
29
+ * torrents of the survey collection, the words that would say so (`sample`,
30
+ * `trailer`, `extra`, `bonus`, `preview`, `making`) matched four files, and all
31
+ * four were ordinary titles — "Making cash with her pussy" is an episode, not a
32
+ * making-of. Size says no more: the ratio of a video to the median of its own
33
+ * torrent runs continuously from 0.00 to 1.00 with no gap anywhere, because a
34
+ * collection of short clips is made of short clips. So there is nothing here to
35
+ * derive a rule from, and a rule invented anyway would decide the order in
36
+ * which a stranger's bandwidth is spent. Every picture is an item.
37
+ *
38
+ * Nothing here reads bytes, waits on the swarm or knows about ffmpeg. Like the
39
+ * functions it is built on, it is a function of the list of names.
40
+ */
41
+
42
+ import {
43
+ AUDIO_SIDECAR_EXTENSIONS,
44
+ IMAGE_SIDECAR_EXTENSIONS,
45
+ SUBTITLE_SIDECAR_EXTENSIONS,
46
+ VIDEO_FILE_EXTENSIONS,
47
+ extensionOf,
48
+ matchSidecarFiles,
49
+ splitTorrentPath
50
+ } from "./files.js";
51
+
52
+ /**
53
+ * One picture of a torrent, with the files that belong to it.
54
+ *
55
+ * @typedef {object} TorrentItem
56
+ * @property {number} fileIndex - Index of the picture in the torrent's own list.
57
+ * @property {string} name - Its file name, extension included.
58
+ * @property {string[]} folders - The folders above it, relative to the torrent root.
59
+ * @property {string} relativePath - Both of those as one path.
60
+ * @property {number} length - Bytes.
61
+ * @property {import("./files.js").SidecarFile[]} audio - Soundtracks beside it.
62
+ * @property {import("./files.js").SidecarFile[]} subtitles - Subtitle files beside it.
63
+ * @property {import("./files.js").SidecarFile[]} images - Contact sheets and covers.
64
+ */
65
+
66
+ /**
67
+ * One file of a torrent that belongs to no picture.
68
+ *
69
+ * @typedef {object} TorrentLeftover
70
+ * @property {number} fileIndex
71
+ * @property {string} name
72
+ * @property {string[]} folders
73
+ * @property {string} relativePath
74
+ * @property {number} length
75
+ */
76
+
77
+ /**
78
+ * Files in the order a person reads them: by folder, then by name, with runs of
79
+ * digits compared as numbers.
80
+ *
81
+ * A torrent's own order is whatever the tool that made it chose, and it is
82
+ * routinely by size — one measured release lists its episodes 08, 06, 07, 01,
83
+ * 02, 10. Nothing downstream depends on the position: every item carries the
84
+ * torrent's own index, and that is what a file is opened by.
85
+ *
86
+ * `numeric` is what makes 2 come before 10; comparing the strings would put
87
+ * "10" before "2".
88
+ *
89
+ * @param {{ relativePath: string }} left
90
+ * @param {{ relativePath: string }} right
91
+ * @returns {number}
92
+ */
93
+ function inReadingOrder(left, right) {
94
+ return left.relativePath.localeCompare(right.relativePath, undefined, {
95
+ numeric: true,
96
+ sensitivity: "base"
97
+ });
98
+ }
99
+
100
+ export class TorrentContents {
101
+ /** @type {TorrentItem[]} */
102
+ #items = [];
103
+
104
+ /** Which item a file belongs to, as its picture or as one of its parts. @type {Map<number, TorrentItem>} */
105
+ #itemByFile = new Map();
106
+
107
+ /** How many files carry a picture. @type {number} */
108
+ #videoCount = 0;
109
+
110
+ /** @type {TorrentLeftover[]} */
111
+ #leftovers = [];
112
+
113
+ /** Every file as this class reads it, in the torrent's own order. @type {object[]} */
114
+ #described = [];
115
+
116
+ /**
117
+ * @param {object} params
118
+ * @param {Array<{ path?: string, name?: string, length?: number }>} params.files -
119
+ * The torrent's own list, in its own order.
120
+ * @param {string} [params.name] - The torrent's name, which WebTorrent
121
+ * prefixes to every path in a multi-file torrent.
122
+ */
123
+ constructor({ files, name = "" }) {
124
+ const list = Array.isArray(files) ? files : [];
125
+ const described = list.map((file, fileIndex) => {
126
+ const { folders, name: fileName } = splitTorrentPath(file?.path ?? file?.name ?? "", name);
127
+ return {
128
+ fileIndex,
129
+ name: fileName,
130
+ folders,
131
+ relativePath: [...folders, fileName].join("/"),
132
+ length: Number.isFinite(file?.length) ? file.length : 0,
133
+ isVideo: VIDEO_FILE_EXTENSIONS.has(extensionOf(fileName))
134
+ };
135
+ });
136
+ this.#described = described;
137
+ this.#videoCount = described.filter((file) => file.isVideo).length;
138
+
139
+ const claimed = new Set();
140
+ for (const file of described.filter((one) => one.isVideo).sort(inReadingOrder)) {
141
+ const { audio, subtitles, images } = matchSidecarFiles({
142
+ files: list,
143
+ videoIndex: file.fileIndex,
144
+ torrentName: name,
145
+ videoCount: this.#videoCount
146
+ });
147
+ /** @type {TorrentItem} */
148
+ const item = {
149
+ fileIndex: file.fileIndex,
150
+ name: file.name,
151
+ folders: file.folders,
152
+ relativePath: file.relativePath,
153
+ length: file.length,
154
+ audio,
155
+ subtitles,
156
+ images
157
+ };
158
+ this.#items.push(item);
159
+ claimed.add(file.fileIndex);
160
+ this.#itemByFile.set(file.fileIndex, item);
161
+ for (const part of [...audio, ...subtitles, ...images]) {
162
+ claimed.add(part.fileIndex);
163
+ // A part paired with two pictures is answered for by the first that
164
+ // took it, which in this order is the earlier episode. Both items keep
165
+ // it in their own lists, because both genuinely offer it; this map
166
+ // answers the other question — which picture to fetch it beside — and
167
+ // there one answer is what stops it being fetched twice.
168
+ if (!this.#itemByFile.has(part.fileIndex)) {
169
+ this.#itemByFile.set(part.fileIndex, item);
170
+ }
171
+ }
172
+ }
173
+
174
+ this.#leftovers = described
175
+ .filter((file) => !claimed.has(file.fileIndex))
176
+ .sort(inReadingOrder)
177
+ .map((file) => ({
178
+ fileIndex: file.fileIndex,
179
+ name: file.name,
180
+ folders: file.folders,
181
+ relativePath: file.relativePath,
182
+ length: file.length
183
+ }));
184
+ }
185
+
186
+ /**
187
+ * Every picture of this torrent with what belongs to it, in reading order.
188
+ *
189
+ * @returns {TorrentItem[]}
190
+ */
191
+ get items() {
192
+ return this.#items;
193
+ }
194
+
195
+ /**
196
+ * How many files of this torrent carry a picture.
197
+ *
198
+ * @returns {number}
199
+ */
200
+ get videoCount() {
201
+ return this.#videoCount;
202
+ }
203
+
204
+ /**
205
+ * Files that belong to no picture: the release's own notes, a screenshot pack
206
+ * matched to nothing, anything a torrent carries beside its films.
207
+ *
208
+ * @returns {TorrentLeftover[]}
209
+ */
210
+ get leftovers() {
211
+ return this.#leftovers;
212
+ }
213
+
214
+ /**
215
+ * WHAT THIS FILE IS, by its name alone.
216
+ *
217
+ * The one place the question is answered for the whole product: the browser
218
+ * used to answer it again, twice — a list of extensions in its torrent parser
219
+ * and a second, shorter pair inside its picker — and the three had already
220
+ * diverged.
221
+ *
222
+ * @param {number} fileIndex
223
+ * @returns {"video" | "audio" | "subtitle" | "image" | "other"}
224
+ */
225
+ kindOf(fileIndex) {
226
+ const file = this.#described[fileIndex];
227
+ if (!file) {
228
+ return "other";
229
+ }
230
+ if (file.isVideo) {
231
+ return "video";
232
+ }
233
+ const extension = extensionOf(file.name);
234
+ if (AUDIO_SIDECAR_EXTENSIONS.has(extension)) {
235
+ return "audio";
236
+ }
237
+ if (SUBTITLE_SIDECAR_EXTENSIONS.has(extension)) {
238
+ return "subtitle";
239
+ }
240
+ if (IMAGE_SIDECAR_EXTENSIONS.has(extension)) {
241
+ return "image";
242
+ }
243
+ return "other";
244
+ }
245
+
246
+ /**
247
+ * Every file of the torrent, in the order a person reads them, with what each
248
+ * one is and where it sits.
249
+ *
250
+ * What the browser is given: it shows this list and asks nothing about the
251
+ * names itself.
252
+ *
253
+ * @returns {{ fileIndex: number, name: string, relativePath: string, length: number, kind: string }[]}
254
+ */
255
+ files() {
256
+ return [...this.#described]
257
+ .sort(inReadingOrder)
258
+ .map((file) => ({
259
+ fileIndex: file.fileIndex,
260
+ name: file.name,
261
+ relativePath: file.relativePath,
262
+ length: file.length,
263
+ kind: this.kindOf(file.fileIndex)
264
+ }));
265
+ }
266
+
267
+ /**
268
+ * The item this file belongs to — as its picture, or as one of its parts.
269
+ *
270
+ * @param {number} fileIndex
271
+ * @returns {TorrentItem | null}
272
+ */
273
+ itemOf(fileIndex) {
274
+ return this.#itemByFile.get(fileIndex) ?? null;
275
+ }
276
+
277
+ /**
278
+ * The files beside one picture that belong to it, in three groups.
279
+ *
280
+ * The shape the warm-up and the playback plan each worked out for themselves,
281
+ * answered from what was decided once, at construction.
282
+ *
283
+ * @param {number} fileIndex - The picture's index in the torrent.
284
+ * @returns {{ audio: import("./files.js").SidecarFile[], subtitles: import("./files.js").SidecarFile[], images: import("./files.js").SidecarFile[] }}
285
+ */
286
+ sidecarsOf(fileIndex) {
287
+ const item = this.#itemByFile.get(fileIndex);
288
+ if (!item || item.fileIndex !== fileIndex) {
289
+ return { audio: [], subtitles: [], images: [] };
290
+ }
291
+ return { audio: item.audio, subtitles: item.subtitles, images: item.images };
292
+ }
293
+ }
294
+
295
+ /** What has been worked out per torrent, and from how many files. @type {WeakMap<object, { contents: TorrentContents, fileCount: number }>} */
296
+ const byTorrent = new WeakMap();
297
+
298
+ /**
299
+ * What is in this torrent, worked out once.
300
+ *
301
+ * Found from the torrent itself, in the shape the piece store and the demand
302
+ * register already use, because the same instance is wanted by callers that
303
+ * have no way to hand it to one another.
304
+ *
305
+ * A torrent added from a magnet has no files until its metadata arrives, so the
306
+ * answer is rebuilt when the list changes size — which it does exactly once,
307
+ * from nothing to everything.
308
+ *
309
+ * @param {{ files?: unknown[], name?: string }} torrent
310
+ * @returns {TorrentContents}
311
+ */
312
+ export function contentsOf(torrent) {
313
+ const files = Array.isArray(torrent?.files) ? torrent.files : [];
314
+ const held = byTorrent.get(torrent);
315
+ if (held && held.fileCount === files.length) {
316
+ return held.contents;
317
+ }
318
+ const contents = new TorrentContents({
319
+ files,
320
+ name: typeof torrent?.name === "string" ? torrent.name : ""
321
+ });
322
+ byTorrent.set(torrent, { contents, fileCount: files.length });
323
+ return contents;
324
+ }
@@ -135,7 +135,14 @@ export const VIDEO_FILE_EXTENSIONS = new Set([
135
135
  ".m2p",
136
136
  ".mxf",
137
137
  ".rm",
138
- ".rmvb"
138
+ ".rmvb",
139
+ // A Video CD's MPEG-1 stream. It was offered by the browser and not counted
140
+ // here, which is the divergence that made this one list rather than two
141
+ // (measured 2026-09-12, and the only extension the two disagreed on). The
142
+ // union is the safe way to settle it: a file that is offered and turns out to
143
+ // be unplayable is one probe, while a file that is a film and is never
144
+ // offered cannot be reached at all.
145
+ ".dat"
139
146
  ]);
140
147
 
141
148
  /**