@torrent-tv/proxy 2.36.0 → 2.36.1

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 CHANGED
@@ -1,3 +1,8 @@
1
+ ## 2.36.1
2
+
3
+ - **Fix**: The cut list of a copied picture is built from the picture's own keyframes, and no longer from every entry in the container's table. A Matroska CuePoint belongs to the track named inside it, and RFC 9559 leaves the muxer free to index whichever tracks it likes — both field files index their subtitles as well. Measured over the swarm on 2026-08-18, reading only the head and the table: `Minions.and.Monsters.1080p.mkv` has **2778 video entries, one every 2.002 s, and 4669 more across four subtitle tracks**; `Moana.2.2024.720p.BluRay … MegaPeer.mkv` has **1055 video entries and 5007 across five**. Read without the track, the extra times entered the cut list as though they were keyframes; ffmpeg can only cut a copied picture at a real keyframe at or after the time it is given, so each such cut landed at the next one instead — which is exactly the disagreement the field measured, and why it was always positive: 2.002 s on the first file (its own keyframe spacing), a median of 6.3 s and a worst case of 21 s on the second. The reader now takes the first video track's number from Tracks — already inside the head it fetches, with one short extra read only for a file that keeps Tracks elsewhere — and keeps the entries of that track. Nothing else about the two-read approach changes, and a session costs nothing more. With the fix the same two files read 2778 and 1055 times, all of them keyframes. When the filter leaves NOTHING — a table whose entries name a track number Tracks never declares — the unfiltered table is used rather than no table: that case is this reader failing to recognise the file, and answering with nothing would put an even grid on a copied picture, which is the failure it exists to prevent.
4
+ - **Chore**: `scripts/read-container-index.mjs` reads the index of any `.torrent` over the swarm — two short ranged reads, in memory, no file written — so a claim about what a container says can be checked against a real film in seconds. Written after the measurement above was made by hand three times.
5
+
1
6
  ## 2.36.0
2
7
 
3
8
  - **New**: The torrent is charged for the megabytes it is measured to be moving, and the price it is charged at no longer contains work that is not the torrent's. Two faults, both visible in one field log from the addon host (2026-08-18): the same session reported **145.4 ms of CPU per MB over 8.7 MB and 23.1 ms per MB over 54 MB**, a sixfold disagreement that followed the size of the interval rather than anything about the torrent — because a process with nothing to do still runs its timers, its tunnel and its session sweeps, and that draw does not shrink when fewer megabytes move. A minimum-megabytes threshold stood against exactly this and did not hold, because a chosen number was standing in for a measured one. The draw is now measured directly, in the intervals where nothing encodes and not one byte moves, and subtracted before the rest is called the torrent's (`services/torrent-cost.js`). What the threshold was reaching for is arrived at from the readings instead: the draw's own readings disagree by a measured amount, that disagreement is worth `scatter × elapsed` seconds over an interval, and a remainder smaller than it measures the wobble in the subtraction rather than the torrent — so a small interval fails on the same arithmetic that lets a large one through, with no size chosen anywhere. The second fault: the price was then charged against the file's own byte rate — what the viewer consumes — so a fully downloaded file moving nothing still paid, and a file being fetched ahead of the viewer, which is how every session starts, paid too little. It is charged against the rate the torrent is measured to be moving, sampled every five seconds per watched torrent and divided among the files of it being read, so two episodes of one pack do not each pay for the whole download.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@torrent-tv/proxy",
3
- "version": "2.36.0",
3
+ "version": "2.36.1",
4
4
  "description": "Torrent proxy client that exposes webseed-like HTTP stream endpoint.",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "publishConfig": {
@@ -0,0 +1,108 @@
1
+ /**
2
+ * @file What this proxy would cut a file at, read from a real torrent.
3
+ *
4
+ * The keyframe readers take two short ranged reads, so the index of a 7 GB film
5
+ * can be examined over the swarm in seconds without downloading it. Run against
6
+ * a `.torrent` file to see what the container's own table says, per track, and
7
+ * what the reader hands to the cut list.
8
+ *
9
+ * node scripts/read-container-index.mjs path/to/film.torrent [more.torrent …]
10
+ *
11
+ * Developer tool: nothing here runs in a session, and no viewer is involved.
12
+ */
13
+
14
+ import fs from "node:fs";
15
+ import path from "node:path";
16
+ import MemoryChunkStore from "memory-chunk-store";
17
+ import WebTorrent from "webtorrent";
18
+ import { readMatroskaKeyframeTimes } from "../services/container-index/matroska.js";
19
+ import { readMp4KeyframeTimes } from "../services/container-index/mp4.js";
20
+ import { readAviKeyframeTimes } from "../services/container-index/avi.js";
21
+
22
+ const torrents = process.argv.slice(2);
23
+ if (torrents.length === 0) {
24
+ console.error("usage: node scripts/read-container-index.mjs <file.torrent> [...]");
25
+ process.exit(1);
26
+ }
27
+
28
+ /**
29
+ * Collect one byte range of a torrent file.
30
+ *
31
+ * @param {import("webtorrent").TorrentFile} file
32
+ * @param {number} start
33
+ * @param {number} end - Inclusive.
34
+ * @returns {Promise<Buffer>}
35
+ */
36
+ function readRangeOf(file, start, end) {
37
+ return new Promise((resolve, reject) => {
38
+ const chunks = [];
39
+ const stream = file.createReadStream({ start, end });
40
+ stream.on("data", (chunk) => chunks.push(chunk));
41
+ stream.on("end", () => resolve(Buffer.concat(chunks)));
42
+ stream.on("error", reject);
43
+ });
44
+ }
45
+
46
+ /**
47
+ * Say what a set of times looks like: how many, where it starts, how far apart.
48
+ *
49
+ * @param {string} label
50
+ * @param {number[] | null} times
51
+ * @returns {void}
52
+ */
53
+ function describe(label, times) {
54
+ if (!times || times.length === 0) {
55
+ console.log(`${label}: no index`);
56
+ return;
57
+ }
58
+ const gaps = times.slice(1).map((time, index) => time - times[index]).sort((left, right) => left - right);
59
+ const median = gaps.length > 0 ? gaps[Math.floor(gaps.length / 2)] : 0;
60
+ console.log(
61
+ `${label}: ${times.length} times, first ${times.slice(0, 4).map((time) => time.toFixed(3)).join(" ")}, ` +
62
+ `median gap ${median.toFixed(3)}s, last ${times[times.length - 1].toFixed(3)}`
63
+ );
64
+ }
65
+
66
+ for (const torrentPath of torrents) {
67
+ // uTP is off: binding its socket needs a permission this does not have on
68
+ // every developer machine, and TCP peers are enough to read an index.
69
+ const client = new WebTorrent({ utp: false });
70
+ console.log(`\n=== ${path.basename(torrentPath)} ===`);
71
+ await new Promise((resolve) => {
72
+ // In memory, deliberately. The file store creates the whole file on disk at
73
+ // its full length before a byte of it is wanted — reading the index of four
74
+ // films that way filled 34 GB and then failed with the disk full. Only two
75
+ // short ranges are ever read here, so they can simply be held.
76
+ client.add(fs.readFileSync(torrentPath), { store: MemoryChunkStore }, async (torrent) => {
77
+ try {
78
+ // Nothing is wanted up front; the ranged reads below select what they need.
79
+ torrent.deselect(0, torrent.pieces.length - 1, 0);
80
+ const file = torrent.files
81
+ .filter((candidate) => /\.(mkv|mp4|avi)$/i.test(candidate.name))
82
+ .sort((left, right) => right.length - left.length)[0];
83
+ if (!file) {
84
+ console.log("no video file in this torrent");
85
+ return resolve();
86
+ }
87
+ console.log(`file: ${file.name} (${(file.length / 1e9).toFixed(2)} GB)`);
88
+ const readRange = async (start, end) => {
89
+ const last = Math.min(end, file.length - 1);
90
+ return start > last ? null : readRangeOf(file, start, last);
91
+ };
92
+ if (/\.mkv$/i.test(file.name)) {
93
+ describe("matroska", await readMatroskaKeyframeTimes(readRange, file.length));
94
+ } else if (/\.mp4$/i.test(file.name)) {
95
+ describe("mp4", await readMp4KeyframeTimes(readRange, file.length));
96
+ } else {
97
+ describe("avi", await readAviKeyframeTimes(readRange, file.length));
98
+ }
99
+ } catch (error) {
100
+ console.log(`failed: ${error?.message ?? error}`);
101
+ } finally {
102
+ resolve();
103
+ }
104
+ });
105
+ });
106
+ await new Promise((done) => client.destroy(done));
107
+ }
108
+ process.exit(0);
@@ -28,6 +28,14 @@ const ID_TIMESTAMP_SCALE = 0x2ad7b1;
28
28
  const ID_CUES = 0x1c53bb6b;
29
29
  const ID_CUE_POINT = 0xbb;
30
30
  const ID_CUE_TIME = 0xb3;
31
+ const ID_CUE_TRACK_POSITIONS = 0xb7;
32
+ const ID_CUE_TRACK = 0xf7;
33
+ const ID_TRACKS = 0x1654ae6b;
34
+ const ID_TRACK_ENTRY = 0xae;
35
+ const ID_TRACK_NUMBER = 0xd7;
36
+ const ID_TRACK_TYPE = 0x83;
37
+ // TrackType 1 is video; 2 is audio, 17 subtitles, and the rest are rarer still.
38
+ const TRACK_TYPE_VIDEO = 1;
31
39
 
32
40
  // How much of the file start to read. Must cover the EBML header, the SeekHead
33
41
  // and Info; 64 KB is generous for every real muxer (the file measured needed
@@ -36,6 +44,10 @@ const HEAD_BYTES = 64 * 1024;
36
44
  // Cap on the Cues read. A two-hour film indexes to tens of KB; anything beyond
37
45
  // this is not a normal index and not worth pulling over a torrent.
38
46
  const MAX_CUES_BYTES = 8 * 1024 * 1024;
47
+ // Cap on a Tracks read, for the rare file whose Tracks element sits outside the
48
+ // head window. Track entries are small, so a file with dozens of them still
49
+ // fits well inside this.
50
+ const MAX_TRACKS_BYTES = 1024 * 1024;
39
51
  // Matroska's default timestamp scale (nanoseconds per tick) when Info omits it.
40
52
  const DEFAULT_TIMESTAMP_SCALE = 1_000_000;
41
53
 
@@ -121,13 +133,64 @@ function readTimestampScale(head, segmentDataOffset) {
121
133
  }
122
134
 
123
135
  /**
124
- * Cue times (seconds, ascending) from a Cues payload.
136
+ * The number of the first video track, from a Tracks payload.
137
+ *
138
+ * The FIRST one, because that is the track ffmpeg is told to copy (`0:v:0`).
139
+ *
140
+ * @param {Buffer} buffer
141
+ * @param {{ dataOffset: number, size: number }} tracks
142
+ * @returns {number | null}
143
+ */
144
+ function readVideoTrackNumber(buffer, tracks) {
145
+ const tracksEnd = Math.min(buffer.length, tracks.dataOffset + tracks.size);
146
+ for (const entry of iterateElements(buffer, tracks.dataOffset, tracksEnd)) {
147
+ if (entry.id !== ID_TRACK_ENTRY) {
148
+ continue;
149
+ }
150
+ const entryEnd = Math.min(tracksEnd, entry.dataOffset + entry.size);
151
+ let number = null;
152
+ let type = null;
153
+ for (const field of iterateElements(buffer, entry.dataOffset, entryEnd)) {
154
+ if (field.id === ID_TRACK_NUMBER) {
155
+ number = readUint(buffer, field.dataOffset, field.size);
156
+ } else if (field.id === ID_TRACK_TYPE) {
157
+ type = readUint(buffer, field.dataOffset, field.size);
158
+ }
159
+ }
160
+ if (number !== null && type === TRACK_TYPE_VIDEO) {
161
+ return number;
162
+ }
163
+ }
164
+ return null;
165
+ }
166
+
167
+ /**
168
+ * Cue times (seconds, ascending) of ONE track, from a Cues payload.
169
+ *
170
+ * The track is the whole point, and leaving it out is what this reader got
171
+ * wrong until 2026-08-18. A CuePoint belongs to the track named inside its
172
+ * CueTrackPositions, and a muxer indexes whatever tracks it likes: RFC 9559
173
+ * says each keyframe of a video track SHOULD be referenced, and that the Cues
174
+ * Element "can be used to index every single timestamp of every Block or they
175
+ * can be indexed selectively". Both field files index their SUBTITLE tracks as
176
+ * well — `Minions.and.Monsters.1080p.mkv` has 2778 video entries every 2.002 s
177
+ * plus 4669 across four subtitle tracks; `Moana.2 … MegaPeer.mkv` has 1055
178
+ * video entries plus 5007 across five.
179
+ *
180
+ * Read without the track, those extra times enter the cut list as though they
181
+ * were keyframes. ffmpeg can only cut a COPIED picture at a real keyframe at or
182
+ * after the time it is given, so every cut asked for at one of them lands late
183
+ * — which is exactly what the field measured: on the first file every deviation
184
+ * was 2.002 s, that file's own keyframe spacing, and on the second the median
185
+ * was 6.3 s with a worst case of 21 s. Never once negative.
125
186
  *
126
187
  * @param {Buffer} cues
127
188
  * @param {number} timestampScale - Nanoseconds per tick.
189
+ * @param {number | null} trackNumber - Null keeps every entry, which is right
190
+ * only for a file that indexes one track.
128
191
  * @returns {number[]}
129
192
  */
130
- function readCueTimes(cues, timestampScale) {
193
+ function readCueTimes(cues, timestampScale, trackNumber) {
131
194
  const times = [];
132
195
  const secondsPerTick = timestampScale / 1e9;
133
196
  for (const point of iterateElements(cues)) {
@@ -135,11 +198,26 @@ function readCueTimes(cues, timestampScale) {
135
198
  continue;
136
199
  }
137
200
  const pointEnd = Math.min(cues.length, point.dataOffset + point.size);
201
+ let time = null;
202
+ let belongsToTrack = trackNumber === null;
138
203
  for (const field of iterateElements(cues, point.dataOffset, pointEnd)) {
139
204
  if (field.id === ID_CUE_TIME) {
140
- times.push(readUint(cues, field.dataOffset, field.size) * secondsPerTick);
141
- break;
205
+ time = readUint(cues, field.dataOffset, field.size) * secondsPerTick;
206
+ continue;
207
+ }
208
+ if (field.id !== ID_CUE_TRACK_POSITIONS || belongsToTrack) {
209
+ continue;
142
210
  }
211
+ const positionsEnd = Math.min(pointEnd, field.dataOffset + field.size);
212
+ for (const inner of iterateElements(cues, field.dataOffset, positionsEnd)) {
213
+ if (inner.id === ID_CUE_TRACK && readUint(cues, inner.dataOffset, inner.size) === trackNumber) {
214
+ belongsToTrack = true;
215
+ break;
216
+ }
217
+ }
218
+ }
219
+ if (time !== null && belongsToTrack) {
220
+ times.push(time);
143
221
  }
144
222
  }
145
223
  times.sort((left, right) => left - right);
@@ -195,6 +273,64 @@ export async function readMatroskaKeyframeTimes(readRange, fileSize) {
195
273
  const payloadEnd = Math.min(cuesChunk.length, cuesElement.dataOffset + cuesElement.size);
196
274
  const payload = cuesChunk.subarray(cuesElement.dataOffset, payloadEnd);
197
275
 
198
- const times = readCueTimes(payload, readTimestampScale(head, seekHead.segmentDataOffset));
199
- return times.length > 0 ? times : null;
276
+ // Whose entries to keep. Tracks sits near the head and is normally inside the
277
+ // bytes already fetched; when it is not, SeekHead says where it is and one
278
+ // more short read gets it. Nothing is fetched twice and nothing is scanned.
279
+ const videoTrack = await readVideoTrack(readRange, head, seekHead, fileSize);
280
+ const timestampScale = readTimestampScale(head, seekHead.segmentDataOffset);
281
+ const times = readCueTimes(payload, timestampScale, videoTrack);
282
+ if (times.length > 0) {
283
+ return times;
284
+ }
285
+ if (videoTrack === null) {
286
+ return null;
287
+ }
288
+ // The filter left nothing, and that is not an answer about the file: a table
289
+ // exists, and this reader simply failed to recognise which of its entries
290
+ // belong to the picture — a track numbered one way in Tracks and another in
291
+ // the cue points, or an entry with no CueTrack at all. Returning null here
292
+ // would put an EVEN grid on a copied picture, which is the failure this
293
+ // module exists to prevent, so the unfiltered table is used instead: less
294
+ // exact than the picture's own keyframes, better than a grid that has nothing
295
+ // to do with the file.
296
+ const unfiltered = readCueTimes(payload, timestampScale, null);
297
+ return unfiltered.length > 0 ? unfiltered : null;
298
+ }
299
+
300
+ /**
301
+ * The video track's number — from the head when it is there, and from one extra
302
+ * short read when it is not.
303
+ *
304
+ * @param {(start: number, end: number) => Promise<Buffer | null>} readRange
305
+ * @param {Buffer} head
306
+ * @param {{ segmentDataOffset: number, entries: Map<number, number> }} seekHead
307
+ * @param {number} fileSize
308
+ * @returns {Promise<number | null>} Null when Tracks cannot be read at all, and
309
+ * then every cue entry is kept — right for a file that indexes only its
310
+ * picture, wrong for one that does not, and nothing here can tell them apart.
311
+ * Refusing the index instead would put an even grid on a copied picture,
312
+ * which is the failure this reader exists to prevent.
313
+ */
314
+ async function readVideoTrack(readRange, head, seekHead, fileSize) {
315
+ const inHead = findElement(head, ID_TRACKS, [], seekHead.segmentDataOffset);
316
+ if (inHead && inHead.dataOffset + inHead.size <= head.length) {
317
+ return readVideoTrackNumber(head, inHead);
318
+ }
319
+ const relative = seekHead.entries.get(ID_TRACKS);
320
+ if (relative === undefined) {
321
+ return null;
322
+ }
323
+ const offset = seekHead.segmentDataOffset + relative;
324
+ if (!Number.isFinite(offset) || offset <= 0 || offset >= fileSize) {
325
+ return null;
326
+ }
327
+ const chunk = await readRange(offset, Math.min(fileSize - 1, offset + MAX_TRACKS_BYTES));
328
+ if (!chunk || chunk.length === 0) {
329
+ return null;
330
+ }
331
+ const element = [...iterateElements(chunk, 0, chunk.length)][0];
332
+ if (!element || element.id !== ID_TRACKS) {
333
+ return null;
334
+ }
335
+ return readVideoTrackNumber(chunk, { dataOffset: element.dataOffset, size: element.size });
200
336
  }
@@ -0,0 +1,192 @@
1
+ /**
2
+ * @file The container's table names a track, and only the picture's entries are
3
+ * cut points.
4
+ *
5
+ * Measured 2026-08-18 on the two files the field sessions were recorded from:
6
+ * `Minions.and.Monsters.1080p.mkv` carries 2778 cue entries for its video track
7
+ * — one every 2.002 s — and 4669 more across four subtitle tracks;
8
+ * `Moana.2 … MegaPeer.mkv` carries 1055 for video and 5007 across five subtitle
9
+ * tracks. Read without the track, both sets went into the cut list together,
10
+ * and ffmpeg — which can only cut a copied picture at a real keyframe at or
11
+ * after the time it is asked for — moved every such cut forward to the next
12
+ * keyframe. That is the whole of the disagreement the field reported: 2.002 s
13
+ * on the first file, a median of 6.3 s and a worst case of 21 s on the second,
14
+ * and never once negative.
15
+ */
16
+
17
+ import test from "node:test";
18
+ import assert from "node:assert/strict";
19
+ import { readMatroskaKeyframeTimes } from "../services/container-index/matroska.js";
20
+
21
+ const ID_EBML = 0x1a45dfa3;
22
+ const ID_SEGMENT = 0x18538067;
23
+ const ID_SEEK_HEAD = 0x114d9b74;
24
+ const ID_SEEK = 0x4dbb;
25
+ const ID_SEEK_ID = 0x53ab;
26
+ const ID_SEEK_POSITION = 0x53ac;
27
+ const ID_INFO = 0x1549a966;
28
+ const ID_TIMESTAMP_SCALE = 0x2ad7b1;
29
+ const ID_TRACKS = 0x1654ae6b;
30
+ const ID_TRACK_ENTRY = 0xae;
31
+ const ID_TRACK_NUMBER = 0xd7;
32
+ const ID_TRACK_TYPE = 0x83;
33
+ const ID_CUES = 0x1c53bb6b;
34
+ const ID_CUE_POINT = 0xbb;
35
+ const ID_CUE_TIME = 0xb3;
36
+ const ID_CUE_TRACK_POSITIONS = 0xb7;
37
+ const ID_CUE_TRACK = 0xf7;
38
+ const ID_CUE_CLUSTER_POSITION = 0xf1;
39
+
40
+ /** An element id, as the bytes the specification gives it. */
41
+ function idBytes(id) {
42
+ const bytes = [];
43
+ let rest = id;
44
+ while (rest > 0) {
45
+ bytes.unshift(rest & 0xff);
46
+ rest = Math.floor(rest / 256);
47
+ }
48
+ return Buffer.from(bytes);
49
+ }
50
+
51
+ /** A size, as a four-byte EBML variable-length integer. */
52
+ function sizeBytes(size) {
53
+ const buffer = Buffer.alloc(4);
54
+ buffer.writeUInt32BE(size, 0);
55
+ buffer[0] |= 0x10;
56
+ return buffer;
57
+ }
58
+
59
+ function element(id, payload) {
60
+ return Buffer.concat([idBytes(id), sizeBytes(payload.length), payload]);
61
+ }
62
+
63
+ /** An unsigned value, in as few bytes as it needs. */
64
+ function uintElement(id, value) {
65
+ const bytes = [];
66
+ let rest = value;
67
+ do {
68
+ bytes.unshift(rest & 0xff);
69
+ rest = Math.floor(rest / 256);
70
+ } while (rest > 0);
71
+ return element(id, Buffer.from(bytes));
72
+ }
73
+
74
+ function cuePoint(timeMs, tracks) {
75
+ return element(ID_CUE_POINT, Buffer.concat([
76
+ uintElement(ID_CUE_TIME, timeMs),
77
+ ...tracks.map((track) => element(ID_CUE_TRACK_POSITIONS, Buffer.concat([
78
+ uintElement(ID_CUE_TRACK, track),
79
+ uintElement(ID_CUE_CLUSTER_POSITION, 4096)
80
+ ])))
81
+ ]));
82
+ }
83
+
84
+ /**
85
+ * A file with the shape the field files have: one picture, one set of subtitles,
86
+ * and a table that indexes both.
87
+ *
88
+ * @param {{ withTracks?: boolean }} [options]
89
+ * @returns {Buffer}
90
+ */
91
+ function buildFile({ withTracks = true, cueTrack = null } = {}) {
92
+ const info = element(ID_INFO, uintElement(ID_TIMESTAMP_SCALE, 1_000_000));
93
+ const tracks = element(ID_TRACKS, Buffer.concat([
94
+ element(ID_TRACK_ENTRY, Buffer.concat([
95
+ uintElement(ID_TRACK_NUMBER, 1),
96
+ uintElement(ID_TRACK_TYPE, 1) // video
97
+ ])),
98
+ element(ID_TRACK_ENTRY, Buffer.concat([
99
+ uintElement(ID_TRACK_NUMBER, 2),
100
+ uintElement(ID_TRACK_TYPE, 17) // subtitles
101
+ ]))
102
+ ]));
103
+ const forPicture = cueTrack ?? 1;
104
+ const forSubtitles = cueTrack ?? 2;
105
+ const cues = element(ID_CUES, Buffer.concat([
106
+ cuePoint(0, [forPicture]),
107
+ cuePoint(1070, [forSubtitles]),
108
+ cuePoint(2002, [forPicture]),
109
+ cuePoint(3141, [forSubtitles]),
110
+ cuePoint(4004, [forPicture])
111
+ ]));
112
+
113
+ const seekEntry = (targetId, position) => element(ID_SEEK, Buffer.concat([
114
+ element(ID_SEEK_ID, idBytes(targetId)),
115
+ element(ID_SEEK_POSITION, (() => {
116
+ const buffer = Buffer.alloc(4);
117
+ buffer.writeUInt32BE(position, 0);
118
+ return buffer;
119
+ })())
120
+ ]));
121
+ // Positions are relative to the Segment's payload, so the SeekHead has to be
122
+ // measured before they can be stated. Its own length does not change when the
123
+ // placeholders become real values: every size and position here is written at
124
+ // a fixed width.
125
+ const draft = element(ID_SEEK_HEAD, Buffer.concat([
126
+ seekEntry(ID_INFO, 0),
127
+ ...(withTracks ? [seekEntry(ID_TRACKS, 0)] : []),
128
+ seekEntry(ID_CUES, 0)
129
+ ]));
130
+ const infoAt = draft.length;
131
+ const tracksAt = infoAt + info.length;
132
+ const cuesAt = withTracks ? tracksAt + tracks.length : infoAt + info.length;
133
+ const seekHead = element(ID_SEEK_HEAD, Buffer.concat([
134
+ seekEntry(ID_INFO, infoAt),
135
+ ...(withTracks ? [seekEntry(ID_TRACKS, tracksAt)] : []),
136
+ seekEntry(ID_CUES, cuesAt)
137
+ ]));
138
+
139
+ const segmentPayload = Buffer.concat(
140
+ withTracks ? [seekHead, info, tracks, cues] : [seekHead, info, cues]
141
+ );
142
+ return Buffer.concat([
143
+ element(ID_EBML, Buffer.from([0x42, 0x86, 0x81, 0x01])),
144
+ element(ID_SEGMENT, segmentPayload)
145
+ ]);
146
+ }
147
+
148
+ function readerOver(file) {
149
+ return async (start, end) => {
150
+ const last = Math.min(end, file.length - 1);
151
+ return start > last ? null : file.subarray(start, last + 1);
152
+ };
153
+ }
154
+
155
+ test("only the picture's entries become cut times", async () => {
156
+ const file = buildFile();
157
+
158
+ const times = await readMatroskaKeyframeTimes(readerOver(file), file.length);
159
+
160
+ assert.deepEqual(
161
+ times.map((time) => Number(time.toFixed(3))),
162
+ [0, 2.002, 4.004],
163
+ "the subtitle entries at 1.070 and 3.141 are not keyframes, and a cut asked for there lands late"
164
+ );
165
+ });
166
+
167
+ test("a table whose entries name no known track is still used", async () => {
168
+ // The cue points reference track 9, which Tracks never declares. Filtering
169
+ // leaves nothing — and returning nothing would put an even grid on a copied
170
+ // picture, the failure this reader exists to prevent.
171
+ const file = buildFile({ cueTrack: 9 });
172
+
173
+ const times = await readMatroskaKeyframeTimes(readerOver(file), file.length);
174
+
175
+ assert.deepEqual(
176
+ times.map((time) => Number(time.toFixed(3))),
177
+ [0, 1.07, 2.002, 3.141, 4.004],
178
+ "an unrecognised table beats no table at all"
179
+ );
180
+ });
181
+
182
+ test("a file whose tracks cannot be read keeps every entry", async () => {
183
+ const file = buildFile({ withTracks: false });
184
+
185
+ const times = await readMatroskaKeyframeTimes(readerOver(file), file.length);
186
+
187
+ assert.deepEqual(
188
+ times.map((time) => Number(time.toFixed(3))),
189
+ [0, 1.07, 2.002, 3.141, 4.004],
190
+ "with nothing to tell the tracks apart, the old behaviour is the only one available"
191
+ );
192
+ });