@torrent-tv/proxy 2.89.6 → 2.89.8

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 (92) hide show
  1. package/AGENTS.md +32 -12
  2. package/CHANGELOG.md +2340 -2330
  3. package/assets/calibration/audio/NOTICE.md +19 -19
  4. package/assets/calibration/audio/manifest.json +98 -98
  5. package/biome.json +412 -412
  6. package/docs/browser-proxy-contract.md +155 -155
  7. package/docs/container-architecture.md +273 -273
  8. package/docs/encode-run-state.md +1 -1
  9. package/docs/playback-start-forecast.md +113 -98
  10. package/package.json +51 -50
  11. package/routes/api/sources/files/get.js +106 -106
  12. package/scripts/build-audio-samples.mjs +31 -31
  13. package/services/encode/EncodeRun.js +67 -15
  14. package/services/encode/EncodeRuns.js +19 -7
  15. package/services/encode/OutputOpening.js +784 -784
  16. package/services/encode/audio-calibration.js +67 -67
  17. package/services/encode/audio-work-rate.js +15 -15
  18. package/services/encode/encode-exit.js +2 -0
  19. package/services/encode/hwaccel.js +1894 -1894
  20. package/services/encode/output/index.js +24 -24
  21. package/services/encode/piece-completeness.js +60 -2
  22. package/services/encode/run-command.js +757 -755
  23. package/services/encode/segment-formats/index.js +1 -0
  24. package/services/encode/segment-formats/mp4-boxes.js +8 -1
  25. package/services/media/ContainerOrchestrator.js +202 -202
  26. package/services/media/KeyframeTables.js +356 -356
  27. package/services/media/SubtitleCues.js +592 -592
  28. package/services/media/audio-inventory.js +299 -299
  29. package/services/media/container/AviContainer.js +268 -268
  30. package/services/media/container/Container.js +632 -632
  31. package/services/media/container/ContainerFactory.js +38 -38
  32. package/services/media/container/MatroskaContainer.js +1031 -1031
  33. package/services/media/container/Mp4Container.js +1354 -1354
  34. package/services/media/container/SubtitleFileContainer.js +339 -339
  35. package/services/media/container/ebml-stream.js +174 -174
  36. package/services/media/container/matroska-clusters.js +1165 -1165
  37. package/services/media/container/unavailable.js +77 -77
  38. package/services/media/tracks/ContainerTrack.js +84 -84
  39. package/services/media/tracks/TextSubtitleTrack.js +295 -295
  40. package/services/storage/piece-store/shared-piece-store.js +1937 -1937
  41. package/services/storage/segment-store/SegmentStore.js +1294 -1294
  42. package/services/torrent/Contents.js +382 -382
  43. package/services/torrent/episode-naming.js +182 -182
  44. package/services/torrent/torrent-pool.js +2688 -2688
  45. package/services/torrent/worker/client.js +836 -836
  46. package/services/torrent/worker/held-bytes.js +231 -231
  47. package/services/torrent/worker/worker.js +1137 -1137
  48. package/services/transport/data-channel-handler.js +1373 -1373
  49. package/services/viewer/PriorityMap.js +421 -421
  50. package/test/audio-calibration.test.js +23 -23
  51. package/test/auto-quality-step.test.js +762 -762
  52. package/test/coalesce.test.js +39 -39
  53. package/test/coverage-follows-the-disk.test.js +5 -0
  54. package/test/decode-cost.test.js +475 -475
  55. package/test/encode-run.test.js +58 -0
  56. package/test/episode-naming.test.js +131 -131
  57. package/test/failed-encoder-still-serves-what-exists.test.js +1 -1
  58. package/test/fixtures/audio-frame-boundary.json +1 -1
  59. package/test/fixtures/refused-audio-20261002.json +10 -0
  60. package/test/fmp4-presentation-clock.test.js +4 -2
  61. package/test/format-keyed-outputs.test.js +78 -78
  62. package/test/health-metrics.test.js +11 -13
  63. package/test/held-read-ownership.test.js +77 -77
  64. package/test/helpers/encode-run.js +14 -6
  65. package/test/helpers/held-file.js +77 -77
  66. package/test/helpers/manager.js +62 -62
  67. package/test/helpers/matroska-file.js +296 -296
  68. package/test/keyframe-table-budget.test.js +123 -123
  69. package/test/matroska-cluster-discovery.test.js +323 -323
  70. package/test/one-output-one-session.test.js +273 -273
  71. package/test/output-speed.test.js +6 -1
  72. package/test/piece-completeness.test.js +37 -1
  73. package/test/quality-variants.test.js +1274 -1270
  74. package/test/rate-control-in-the-output.test.js +4 -3
  75. package/test/reads-not-yet-arrived.test.js +218 -218
  76. package/test/seek-frees-the-viewers-requests.test.js +1 -1
  77. package/test/segment-closed-on-both-branches.test.js +114 -114
  78. package/test/segment-judged-by-its-own-header.test.js +222 -222
  79. package/test/segment-serve-wiring.test.js +481 -481
  80. package/test/serving-existing-output.test.js +111 -111
  81. package/test/source-files-route.test.js +113 -113
  82. package/test/subtitle-cue-walk.test.js +385 -385
  83. package/test/subtitle-metadata.test.js +15 -15
  84. package/test/subtitle-plan-waits.test.js +125 -125
  85. package/test/subtitle-track-numbering.test.js +391 -391
  86. package/test/two-viewers-one-picture.test.js +508 -508
  87. package/test/viewer-requests-admission.test.js +92 -92
  88. package/test/viewer-starts-on-map.test.js +27 -27
  89. package/test/viewer-states.test.js +5 -2
  90. package/test/viewing-generations.test.js +390 -390
  91. package/test/whole-file-held-read.test.js +30 -30
  92. package/utils/coalesce.js +41 -41
@@ -1,356 +1,356 @@
1
- /**
2
- * @file Reading the keyframe table of a file: once, whoever asks, with a bound
3
- * on how long anybody waits for it.
4
- *
5
- * The table itself is `container/KeyframeTable.js`, one object per file. This is
6
- * the policy around filling it in, and it is a policy because three things had
7
- * to be true at once and each of them was learned from a field session:
8
- *
9
- * 1. **once per file, and one WAIT per file.** Two sessions created in the same
10
- * moment used to miss the cache together and read the table twice — which is
11
- * what two viewers opening one film do, measured 13 ms apart on 2026-09-03.
12
- * Whoever asks second joins the read already running;
13
- * 2. **the wait is bounded, the READ is not.** A picture cannot be copied
14
- * without the table, so the answer to "not yet" is to re-encode — but the
15
- * read goes on, lands in the file's own table, and is there for whoever holds
16
- * it. Cancelling it would make every later viewer pay the whole wait again;
17
- * 3. **a read that THREW is not an answer.** "The head is not downloaded" says
18
- * nothing about the file; turning it into "this file has no keyframes" makes
19
- * every picture of it a re-encode for as long as the process lives. Bytes
20
- * that have not arrived reach here as `BytesUnavailable` — until 2026-10-01
21
- * they came back as a reading of no times, which `KeyframeTable.learn`
22
- * records as answered, so point 3 held only for reads that crashed. When
23
- * pieces of such a file arrive the read is made again
24
- * (`readAgainIfUnanswered`), and its answer lands in the same table.
25
- *
26
- * **What it does NOT know.** Where the bytes come from. The reader is one
27
- * function handed in at construction — on this proxy it crosses to the torrent
28
- * thread, where one container per file answers the track table and the media
29
- * info from the same header — so this can be exercised with plain values alone.
30
- * It used to live in the session manager, which also carried a SECOND reader
31
- * that fetched the file over the proxy's own HTTP and parsed it there: a
32
- * transport inside a layer that must not have one, and a second answer to a
33
- * question with one owner.
34
- *
35
- * **Why this is not `ContainerOrchestrator`.** Both live on the main thread
36
- * (the parse moved there on 2026-09-15). That one holds the file's one
37
- * container, which keeps its own reading of the Cues table; this keeps the
38
- * ANSWER the sessions hold — the keyframe times taken from that reading, and
39
- * the wait around it, which a container cannot have because it does not know
40
- * there is a viewer.
41
- */
42
-
43
- import { KeyframeTable } from "./container/KeyframeTable.js";
44
- import { isUnavailable } from "./container/unavailable.js";
45
- import { logger as defaultLogger } from "../../utils/logger.js";
46
-
47
- /**
48
- * How long anybody waits for the table before giving up on copying the picture.
49
- *
50
- * Measured on the addon host, 2026-09-04, over seventeen files from
51
- * `Dropbox/trn` — four containers, pieces from 0.25 to 16 MB, files from 0.36 to
52
- * 20 GB, each torrent registered fresh so nothing of it was downloaded
53
- * (`research/keyframe-table-read-2026-09-04.md`). Every table that arrived did
54
- * so within 24.8 s, most within half a second; the two files that answered
55
- * nothing took 120.9 s and 120.5 s.
56
- *
57
- * Those two figures are not a coincidence and they are what fixes this one:
58
- * they are TWO of the bound the read already has — `READ_ABANDON_MS` in
59
- * `torrent/worker/resume-warm.js`, one for the wait on the file's edges and
60
- * one for the read itself, in series. A caller waiting for two of them is the
61
- * defect; waiting for one is the bound, and it leaves 2.4x over the slowest
62
- * table that did arrive. The line printed when it fires names which case
63
- * happened, so the field can move it rather than an argument.
64
- */
65
- export const KEYFRAME_TABLE_BUDGET_MS = 60_000;
66
-
67
- export class KeyframeTables {
68
- /** One per file, handed out rather than copied. @type {Map<string, KeyframeTable>} */
69
- #byFile = new Map();
70
-
71
- /** The read in flight, so the second asker joins it. @type {Map<string, Promise<KeyframeTable>>} */
72
- #reading = new Map();
73
-
74
- /** @type {((params: { sourceKey: string, fileIndex: number }) => Promise<object | null>) | null} */
75
- #readTable;
76
-
77
- /** @type {number} */
78
- #budgetMs;
79
-
80
- /** @type {{ info: Function, warn: Function }} */
81
- #logger;
82
-
83
- /**
84
- * @param {object} [params]
85
- * @param {(params: { sourceKey: string, fileIndex: number }) => Promise<object | null>} [params.readTable] -
86
- * Whatever can answer what this file's container states. The whole of this
87
- * object's outside world.
88
- * @param {number} [params.budgetMs] - How long `within` waits.
89
- * @param {{ info: Function, warn: Function }} [params.logger]
90
- */
91
- constructor({ readTable = null, budgetMs = KEYFRAME_TABLE_BUDGET_MS, logger = defaultLogger } = {}) {
92
- this.#readTable = typeof readTable === "function" ? readTable : null;
93
- const declared = Number(budgetMs);
94
- this.#budgetMs = Number.isFinite(declared) && declared > 0 ? declared : KEYFRAME_TABLE_BUDGET_MS;
95
- this.#logger = logger;
96
- }
97
-
98
- /**
99
- * @param {string} sourceKey
100
- * @param {number} fileIndex
101
- * @returns {string}
102
- */
103
- static keyFor(sourceKey, fileIndex) {
104
- return `${sourceKey}:${fileIndex}`;
105
- }
106
-
107
- /** How long a caller of `within` waits. @returns {number} */
108
- get budgetMs() {
109
- return this.#budgetMs;
110
- }
111
-
112
- /**
113
- * This file's table — the one object everyone reading it holds.
114
- *
115
- * Made empty on first ask rather than on first answer, so a session created
116
- * before the read finishes holds the very object the answer lands in. Handing
117
- * out a value instead is what used to leave a late table unreachable to the
118
- * sessions that needed it most.
119
- *
120
- * @param {{ sourceKey: string, fileIndex: number }} params
121
- * @returns {KeyframeTable}
122
- */
123
- of({ sourceKey, fileIndex }) {
124
- const key = KeyframeTables.keyFor(sourceKey, fileIndex);
125
- let table = this.#byFile.get(key);
126
- if (!table) {
127
- table = new KeyframeTable();
128
- this.#byFile.set(key, table);
129
- }
130
- return table;
131
- }
132
-
133
- /**
134
- * Fill in the file's table, reading once however many ask.
135
- *
136
- * Rejects when the read rejects — see point 3 in the file comment.
137
- *
138
- * @param {{ sourceKey: string, fileIndex: number, logName?: string }} params
139
- * @returns {Promise<KeyframeTable>}
140
- */
141
- async read({ sourceKey, fileIndex, logName = "" }) {
142
- const key = KeyframeTables.keyFor(sourceKey, fileIndex);
143
- const table = this.of({ sourceKey, fileIndex });
144
- if (table.answered) {
145
- return table;
146
- }
147
- const running = this.#reading.get(key);
148
- if (running) {
149
- return running;
150
- }
151
- if (!this.#readTable) {
152
- // Nothing can answer. NOT recorded as an answer about the file: this is
153
- // an object built without its one connection to the outside, and saying
154
- // "this file has no keyframes" on that basis would be a statement about
155
- // the wiring dressed up as a statement about the bytes.
156
- return table;
157
- }
158
- // The WHOLE wait, as whoever asked for the table experiences it: the swarm
159
- // delivering the head and tail of the file, the parse over those bytes, and
160
- // the crossing to the torrent thread and back. The worker's own line
161
- // (`container-keyframes:`) reports the last two apart from the first, and
162
- // reading the two lines as one figure is what led to a wrong conclusion on
163
- // 2026-09-04 — they differ by up to sixty seconds on a thin swarm.
164
- const startedMs = Date.now();
165
- // Called here and not inside a `.then`, so that "one read per file" is true
166
- // at the instant the second asker arrives rather than one microtask later.
167
- // A reader that throws synchronously is a failed read like any other.
168
- let started;
169
- try {
170
- started = Promise.resolve(this.#readTable({ sourceKey, fileIndex }));
171
- } catch (error) {
172
- started = Promise.reject(error);
173
- }
174
- const work = started
175
- .then((reading) => {
176
- table.learn({
177
- times: Array.isArray(reading?.times) ? reading.times : null,
178
- tolerance: reading?.tolerance,
179
- // Which container answered, whether or not it produced a table: the
180
- // refusal that follows names it, and "unknown" would make that line
181
- // say nothing about the file it is refusing.
182
- format: reading?.format ?? "unrecognised"
183
- });
184
- this.#logger.info(
185
- `keyframe index "${logName}": ${table.readable ? `${table.count} times` : "none"} from the ` +
186
- `${table.format} container, waited ${Date.now() - startedMs}ms`
187
- );
188
- return table;
189
- })
190
- .catch((error) => {
191
- if (isUnavailable(error)) {
192
- this.#logger.info(
193
- `keyframe index "${logName}": not downloaded yet after ${Date.now() - startedMs}ms — ` +
194
- "read again when pieces of the file arrive"
195
- );
196
- } else {
197
- this.#logger.warn(
198
- `keyframe index "${logName}": the read failed after ${Date.now() - startedMs}ms — ` +
199
- `${error?.message ?? error}`
200
- );
201
- }
202
- throw error;
203
- })
204
- .finally(() => {
205
- this.#reading.delete(key);
206
- });
207
- this.#reading.set(key, work);
208
- return work;
209
- }
210
-
211
- /**
212
- * Start the read without waiting for it and without ever rejecting.
213
- *
214
- * The index lives at the END of a Matroska file, which is also where the
215
- * codec probe reads — both wait for the same piece to arrive, and they used
216
- * to do it one after the other: measured 2026-08-04, a probe of 722-1206 ms
217
- * followed by an index read of 311-430 ms, all of it before the first
218
- * segment. Started together, the second costs nothing.
219
- *
220
- * @param {{ sourceKey: string, fileIndex: number, logName?: string }} params
221
- * @returns {Promise<void>}
222
- */
223
- async warm(params) {
224
- try {
225
- await this.read(params);
226
- } catch {
227
- // Best effort by construction: nobody is waiting for this answer yet.
228
- }
229
- }
230
-
231
- /**
232
- * Read again a table somebody asked for and nobody has answered.
233
- *
234
- * Called when pieces of the file arrive. A table nobody asked for is left
235
- * alone — this is not a reason to read every file of a torrent — and one
236
- * already read, or being read, is not touched.
237
- *
238
- * @param {{ sourceKey: string, fileIndex: number, logName?: string }} params
239
- * @returns {void}
240
- */
241
- readAgainIfUnanswered(params) {
242
- const key = KeyframeTables.keyFor(params.sourceKey, params.fileIndex);
243
- const table = this.#byFile.get(key);
244
- if (!table || table.answered || this.#reading.has(key)) {
245
- return;
246
- }
247
- void this.warm(params);
248
- }
249
-
250
- /**
251
- * The same read, with a bound on how long THIS caller waits for it.
252
- *
253
- * The read is not cancelled when the bound is reached — it goes on, and it
254
- * lands in the table this returns, which is the file's own. What the bound
255
- * decides is only whether this caller waits: a copied picture cannot be cut
256
- * without the table, so the answer to "not yet" is to re-encode, which needs
257
- * no table because it places the keyframes itself.
258
- *
259
- * @param {{ sourceKey: string, fileIndex: number, logName?: string }} params
260
- * @returns {Promise<{ table: KeyframeTable, arrived: boolean }>} `arrived` is
261
- * about THIS wait. False with an unanswered table means the read is still
262
- * running, or found the bytes not downloaded yet and will be made again
263
- * when they arrive — neither is the same as a file with no keyframes.
264
- */
265
- async within(params) {
266
- const read = this.read(params);
267
- let timer = null;
268
- const budget = new Promise((resolve) => {
269
- timer = setTimeout(() => resolve(null), this.#budgetMs);
270
- // A caller must not be held open by this timer alone.
271
- timer?.unref?.();
272
- });
273
- // Bytes not downloaded yet are "not arrived" to this caller: the table
274
- // stays unanswered and is read again when pieces of the file arrive.
275
- // Thrown out of here, a shortage of bytes would fail the opening of an
276
- // output. Any other failure is still the caller's to see.
277
- const answer = await Promise.race([
278
- read.then(
279
- (table) => ({ table, arrived: true }),
280
- (error) => {
281
- if (isUnavailable(error)) {
282
- return { table: this.of(params), arrived: false };
283
- }
284
- throw error;
285
- }
286
- ),
287
- budget
288
- ]);
289
- clearTimeout(timer);
290
- return answer ?? { table: this.of(params), arrived: false };
291
- }
292
-
293
- /**
294
- * Take in a table found by something other than the reader — the packet
295
- * probe, which finds keyframes by decoding, where no container index exists.
296
- *
297
- * @param {{ sourceKey: string, fileIndex: number }} params
298
- * @param {{ times?: number[] | null, tolerance?: number, format?: string }} reading
299
- * @returns {KeyframeTable} The file's table, as it now stands.
300
- */
301
- learn(params, reading) {
302
- return this.of(params).learn(reading);
303
- }
304
-
305
- /**
306
- * Drop every table nobody holds any more.
307
- *
308
- * Swept by the OBJECT, the way the timelines, the outputs and the source
309
- * files beside it are: what must survive is what a live session points at,
310
- * and that is a reference rather than a string that happens to match one. A
311
- * map that only ever grows is the shape of half the memory faults recorded in
312
- * this repository.
313
- *
314
- * **A file whose read is still running is kept whatever the set says.** Drop
315
- * it and the read completes into an object nobody holds, the next asker makes
316
- * a fresh empty one, and the whole wait — up to a minute off a thin swarm —
317
- * is paid again for an answer this process already had. That is also the
318
- * window in which a table warmed by the playback planner has no session yet.
319
- *
320
- * @param {Set<import("./container/KeyframeTable.js").KeyframeTable>} inUse
321
- * @returns {number} How many were dropped.
322
- */
323
- forgetUnused(inUse) {
324
- let dropped = 0;
325
- for (const [key, table] of [...this.#byFile.entries()]) {
326
- if (inUse.has(table) || this.#reading.has(key)) {
327
- continue;
328
- }
329
- this.#byFile.delete(key);
330
- dropped += 1;
331
- }
332
- return dropped;
333
- }
334
-
335
- /**
336
- * @param {string} sourceKey
337
- * @param {number} [fileIndex] - Absent, every file of this torrent.
338
- * @returns {void}
339
- */
340
- forget(sourceKey, fileIndex) {
341
- if (fileIndex === undefined) {
342
- for (const key of [...this.#byFile.keys()]) {
343
- if (key.startsWith(`${sourceKey}:`)) {
344
- this.#byFile.delete(key);
345
- }
346
- }
347
- return;
348
- }
349
- this.#byFile.delete(KeyframeTables.keyFor(sourceKey, fileIndex));
350
- }
351
-
352
- /** @returns {number} */
353
- get size() {
354
- return this.#byFile.size;
355
- }
356
- }
1
+ /**
2
+ * @file Reading the keyframe table of a file: once, whoever asks, with a bound
3
+ * on how long anybody waits for it.
4
+ *
5
+ * The table itself is `container/KeyframeTable.js`, one object per file. This is
6
+ * the policy around filling it in, and it is a policy because three things had
7
+ * to be true at once and each of them was learned from a field session:
8
+ *
9
+ * 1. **once per file, and one WAIT per file.** Two sessions created in the same
10
+ * moment used to miss the cache together and read the table twice — which is
11
+ * what two viewers opening one film do, measured 13 ms apart on 2026-09-03.
12
+ * Whoever asks second joins the read already running;
13
+ * 2. **the wait is bounded, the READ is not.** A picture cannot be copied
14
+ * without the table, so the answer to "not yet" is to re-encode — but the
15
+ * read goes on, lands in the file's own table, and is there for whoever holds
16
+ * it. Cancelling it would make every later viewer pay the whole wait again;
17
+ * 3. **a read that THREW is not an answer.** "The head is not downloaded" says
18
+ * nothing about the file; turning it into "this file has no keyframes" makes
19
+ * every picture of it a re-encode for as long as the process lives. Bytes
20
+ * that have not arrived reach here as `BytesUnavailable` — until 2026-10-01
21
+ * they came back as a reading of no times, which `KeyframeTable.learn`
22
+ * records as answered, so point 3 held only for reads that crashed. When
23
+ * pieces of such a file arrive the read is made again
24
+ * (`readAgainIfUnanswered`), and its answer lands in the same table.
25
+ *
26
+ * **What it does NOT know.** Where the bytes come from. The reader is one
27
+ * function handed in at construction — on this proxy it crosses to the torrent
28
+ * thread, where one container per file answers the track table and the media
29
+ * info from the same header — so this can be exercised with plain values alone.
30
+ * It used to live in the session manager, which also carried a SECOND reader
31
+ * that fetched the file over the proxy's own HTTP and parsed it there: a
32
+ * transport inside a layer that must not have one, and a second answer to a
33
+ * question with one owner.
34
+ *
35
+ * **Why this is not `ContainerOrchestrator`.** Both live on the main thread
36
+ * (the parse moved there on 2026-09-15). That one holds the file's one
37
+ * container, which keeps its own reading of the Cues table; this keeps the
38
+ * ANSWER the sessions hold — the keyframe times taken from that reading, and
39
+ * the wait around it, which a container cannot have because it does not know
40
+ * there is a viewer.
41
+ */
42
+
43
+ import { KeyframeTable } from "./container/KeyframeTable.js";
44
+ import { isUnavailable } from "./container/unavailable.js";
45
+ import { logger as defaultLogger } from "../../utils/logger.js";
46
+
47
+ /**
48
+ * How long anybody waits for the table before giving up on copying the picture.
49
+ *
50
+ * Measured on the addon host, 2026-09-04, over seventeen files from
51
+ * `Dropbox/trn` — four containers, pieces from 0.25 to 16 MB, files from 0.36 to
52
+ * 20 GB, each torrent registered fresh so nothing of it was downloaded
53
+ * (`research/keyframe-table-read-2026-09-04.md`). Every table that arrived did
54
+ * so within 24.8 s, most within half a second; the two files that answered
55
+ * nothing took 120.9 s and 120.5 s.
56
+ *
57
+ * Those two figures are not a coincidence and they are what fixes this one:
58
+ * they are TWO of the bound the read already has — `READ_ABANDON_MS` in
59
+ * `torrent/worker/resume-warm.js`, one for the wait on the file's edges and
60
+ * one for the read itself, in series. A caller waiting for two of them is the
61
+ * defect; waiting for one is the bound, and it leaves 2.4x over the slowest
62
+ * table that did arrive. The line printed when it fires names which case
63
+ * happened, so the field can move it rather than an argument.
64
+ */
65
+ export const KEYFRAME_TABLE_BUDGET_MS = 60_000;
66
+
67
+ export class KeyframeTables {
68
+ /** One per file, handed out rather than copied. @type {Map<string, KeyframeTable>} */
69
+ #byFile = new Map();
70
+
71
+ /** The read in flight, so the second asker joins it. @type {Map<string, Promise<KeyframeTable>>} */
72
+ #reading = new Map();
73
+
74
+ /** @type {((params: { sourceKey: string, fileIndex: number }) => Promise<object | null>) | null} */
75
+ #readTable;
76
+
77
+ /** @type {number} */
78
+ #budgetMs;
79
+
80
+ /** @type {{ info: Function, warn: Function }} */
81
+ #logger;
82
+
83
+ /**
84
+ * @param {object} [params]
85
+ * @param {(params: { sourceKey: string, fileIndex: number }) => Promise<object | null>} [params.readTable] -
86
+ * Whatever can answer what this file's container states. The whole of this
87
+ * object's outside world.
88
+ * @param {number} [params.budgetMs] - How long `within` waits.
89
+ * @param {{ info: Function, warn: Function }} [params.logger]
90
+ */
91
+ constructor({ readTable = null, budgetMs = KEYFRAME_TABLE_BUDGET_MS, logger = defaultLogger } = {}) {
92
+ this.#readTable = typeof readTable === "function" ? readTable : null;
93
+ const declared = Number(budgetMs);
94
+ this.#budgetMs = Number.isFinite(declared) && declared > 0 ? declared : KEYFRAME_TABLE_BUDGET_MS;
95
+ this.#logger = logger;
96
+ }
97
+
98
+ /**
99
+ * @param {string} sourceKey
100
+ * @param {number} fileIndex
101
+ * @returns {string}
102
+ */
103
+ static keyFor(sourceKey, fileIndex) {
104
+ return `${sourceKey}:${fileIndex}`;
105
+ }
106
+
107
+ /** How long a caller of `within` waits. @returns {number} */
108
+ get budgetMs() {
109
+ return this.#budgetMs;
110
+ }
111
+
112
+ /**
113
+ * This file's table — the one object everyone reading it holds.
114
+ *
115
+ * Made empty on first ask rather than on first answer, so a session created
116
+ * before the read finishes holds the very object the answer lands in. Handing
117
+ * out a value instead is what used to leave a late table unreachable to the
118
+ * sessions that needed it most.
119
+ *
120
+ * @param {{ sourceKey: string, fileIndex: number }} params
121
+ * @returns {KeyframeTable}
122
+ */
123
+ of({ sourceKey, fileIndex }) {
124
+ const key = KeyframeTables.keyFor(sourceKey, fileIndex);
125
+ let table = this.#byFile.get(key);
126
+ if (!table) {
127
+ table = new KeyframeTable();
128
+ this.#byFile.set(key, table);
129
+ }
130
+ return table;
131
+ }
132
+
133
+ /**
134
+ * Fill in the file's table, reading once however many ask.
135
+ *
136
+ * Rejects when the read rejects — see point 3 in the file comment.
137
+ *
138
+ * @param {{ sourceKey: string, fileIndex: number, logName?: string }} params
139
+ * @returns {Promise<KeyframeTable>}
140
+ */
141
+ async read({ sourceKey, fileIndex, logName = "" }) {
142
+ const key = KeyframeTables.keyFor(sourceKey, fileIndex);
143
+ const table = this.of({ sourceKey, fileIndex });
144
+ if (table.answered) {
145
+ return table;
146
+ }
147
+ const running = this.#reading.get(key);
148
+ if (running) {
149
+ return running;
150
+ }
151
+ if (!this.#readTable) {
152
+ // Nothing can answer. NOT recorded as an answer about the file: this is
153
+ // an object built without its one connection to the outside, and saying
154
+ // "this file has no keyframes" on that basis would be a statement about
155
+ // the wiring dressed up as a statement about the bytes.
156
+ return table;
157
+ }
158
+ // The WHOLE wait, as whoever asked for the table experiences it: the swarm
159
+ // delivering the head and tail of the file, the parse over those bytes, and
160
+ // the crossing to the torrent thread and back. The worker's own line
161
+ // (`container-keyframes:`) reports the last two apart from the first, and
162
+ // reading the two lines as one figure is what led to a wrong conclusion on
163
+ // 2026-09-04 — they differ by up to sixty seconds on a thin swarm.
164
+ const startedMs = Date.now();
165
+ // Called here and not inside a `.then`, so that "one read per file" is true
166
+ // at the instant the second asker arrives rather than one microtask later.
167
+ // A reader that throws synchronously is a failed read like any other.
168
+ let started;
169
+ try {
170
+ started = Promise.resolve(this.#readTable({ sourceKey, fileIndex }));
171
+ } catch (error) {
172
+ started = Promise.reject(error);
173
+ }
174
+ const work = started
175
+ .then((reading) => {
176
+ table.learn({
177
+ times: Array.isArray(reading?.times) ? reading.times : null,
178
+ tolerance: reading?.tolerance,
179
+ // Which container answered, whether or not it produced a table: the
180
+ // refusal that follows names it, and "unknown" would make that line
181
+ // say nothing about the file it is refusing.
182
+ format: reading?.format ?? "unrecognised"
183
+ });
184
+ this.#logger.info(
185
+ `keyframe index "${logName}": ${table.readable ? `${table.count} times` : "none"} from the ` +
186
+ `${table.format} container, waited ${Date.now() - startedMs}ms`
187
+ );
188
+ return table;
189
+ })
190
+ .catch((error) => {
191
+ if (isUnavailable(error)) {
192
+ this.#logger.info(
193
+ `keyframe index "${logName}": not downloaded yet after ${Date.now() - startedMs}ms — ` +
194
+ "read again when pieces of the file arrive"
195
+ );
196
+ } else {
197
+ this.#logger.warn(
198
+ `keyframe index "${logName}": the read failed after ${Date.now() - startedMs}ms — ` +
199
+ `${error?.message ?? error}`
200
+ );
201
+ }
202
+ throw error;
203
+ })
204
+ .finally(() => {
205
+ this.#reading.delete(key);
206
+ });
207
+ this.#reading.set(key, work);
208
+ return work;
209
+ }
210
+
211
+ /**
212
+ * Start the read without waiting for it and without ever rejecting.
213
+ *
214
+ * The index lives at the END of a Matroska file, which is also where the
215
+ * codec probe reads — both wait for the same piece to arrive, and they used
216
+ * to do it one after the other: measured 2026-08-04, a probe of 722-1206 ms
217
+ * followed by an index read of 311-430 ms, all of it before the first
218
+ * segment. Started together, the second costs nothing.
219
+ *
220
+ * @param {{ sourceKey: string, fileIndex: number, logName?: string }} params
221
+ * @returns {Promise<void>}
222
+ */
223
+ async warm(params) {
224
+ try {
225
+ await this.read(params);
226
+ } catch {
227
+ // Best effort by construction: nobody is waiting for this answer yet.
228
+ }
229
+ }
230
+
231
+ /**
232
+ * Read again a table somebody asked for and nobody has answered.
233
+ *
234
+ * Called when pieces of the file arrive. A table nobody asked for is left
235
+ * alone — this is not a reason to read every file of a torrent — and one
236
+ * already read, or being read, is not touched.
237
+ *
238
+ * @param {{ sourceKey: string, fileIndex: number, logName?: string }} params
239
+ * @returns {void}
240
+ */
241
+ readAgainIfUnanswered(params) {
242
+ const key = KeyframeTables.keyFor(params.sourceKey, params.fileIndex);
243
+ const table = this.#byFile.get(key);
244
+ if (!table || table.answered || this.#reading.has(key)) {
245
+ return;
246
+ }
247
+ void this.warm(params);
248
+ }
249
+
250
+ /**
251
+ * The same read, with a bound on how long THIS caller waits for it.
252
+ *
253
+ * The read is not cancelled when the bound is reached — it goes on, and it
254
+ * lands in the table this returns, which is the file's own. What the bound
255
+ * decides is only whether this caller waits: a copied picture cannot be cut
256
+ * without the table, so the answer to "not yet" is to re-encode, which needs
257
+ * no table because it places the keyframes itself.
258
+ *
259
+ * @param {{ sourceKey: string, fileIndex: number, logName?: string }} params
260
+ * @returns {Promise<{ table: KeyframeTable, arrived: boolean }>} `arrived` is
261
+ * about THIS wait. False with an unanswered table means the read is still
262
+ * running, or found the bytes not downloaded yet and will be made again
263
+ * when they arrive — neither is the same as a file with no keyframes.
264
+ */
265
+ async within(params) {
266
+ const read = this.read(params);
267
+ let timer = null;
268
+ const budget = new Promise((resolve) => {
269
+ timer = setTimeout(() => resolve(null), this.#budgetMs);
270
+ // A caller must not be held open by this timer alone.
271
+ timer?.unref?.();
272
+ });
273
+ // Bytes not downloaded yet are "not arrived" to this caller: the table
274
+ // stays unanswered and is read again when pieces of the file arrive.
275
+ // Thrown out of here, a shortage of bytes would fail the opening of an
276
+ // output. Any other failure is still the caller's to see.
277
+ const answer = await Promise.race([
278
+ read.then(
279
+ (table) => ({ table, arrived: true }),
280
+ (error) => {
281
+ if (isUnavailable(error)) {
282
+ return { table: this.of(params), arrived: false };
283
+ }
284
+ throw error;
285
+ }
286
+ ),
287
+ budget
288
+ ]);
289
+ clearTimeout(timer);
290
+ return answer ?? { table: this.of(params), arrived: false };
291
+ }
292
+
293
+ /**
294
+ * Take in a table found by something other than the reader — the packet
295
+ * probe, which finds keyframes by decoding, where no container index exists.
296
+ *
297
+ * @param {{ sourceKey: string, fileIndex: number }} params
298
+ * @param {{ times?: number[] | null, tolerance?: number, format?: string }} reading
299
+ * @returns {KeyframeTable} The file's table, as it now stands.
300
+ */
301
+ learn(params, reading) {
302
+ return this.of(params).learn(reading);
303
+ }
304
+
305
+ /**
306
+ * Drop every table nobody holds any more.
307
+ *
308
+ * Swept by the OBJECT, the way the timelines, the outputs and the source
309
+ * files beside it are: what must survive is what a live session points at,
310
+ * and that is a reference rather than a string that happens to match one. A
311
+ * map that only ever grows is the shape of half the memory faults recorded in
312
+ * this repository.
313
+ *
314
+ * **A file whose read is still running is kept whatever the set says.** Drop
315
+ * it and the read completes into an object nobody holds, the next asker makes
316
+ * a fresh empty one, and the whole wait — up to a minute off a thin swarm —
317
+ * is paid again for an answer this process already had. That is also the
318
+ * window in which a table warmed by the playback planner has no session yet.
319
+ *
320
+ * @param {Set<import("./container/KeyframeTable.js").KeyframeTable>} inUse
321
+ * @returns {number} How many were dropped.
322
+ */
323
+ forgetUnused(inUse) {
324
+ let dropped = 0;
325
+ for (const [key, table] of [...this.#byFile.entries()]) {
326
+ if (inUse.has(table) || this.#reading.has(key)) {
327
+ continue;
328
+ }
329
+ this.#byFile.delete(key);
330
+ dropped += 1;
331
+ }
332
+ return dropped;
333
+ }
334
+
335
+ /**
336
+ * @param {string} sourceKey
337
+ * @param {number} [fileIndex] - Absent, every file of this torrent.
338
+ * @returns {void}
339
+ */
340
+ forget(sourceKey, fileIndex) {
341
+ if (fileIndex === undefined) {
342
+ for (const key of [...this.#byFile.keys()]) {
343
+ if (key.startsWith(`${sourceKey}:`)) {
344
+ this.#byFile.delete(key);
345
+ }
346
+ }
347
+ return;
348
+ }
349
+ this.#byFile.delete(KeyframeTables.keyFor(sourceKey, fileIndex));
350
+ }
351
+
352
+ /** @returns {number} */
353
+ get size() {
354
+ return this.#byFile.size;
355
+ }
356
+ }