@torrent-tv/proxy 2.9.126 → 2.9.128

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,11 @@
1
+ ## 2.9.127
2
+
3
+ - **Fix**: A read that ends because the reader left is no longer reported as a failure. ffmpeg is terminated on every seek and whenever the look-ahead bound suspends it, and its connection closes with it, so `write ECANCELED` on the stream route is the ordinary end of a read — yet it was logged as a warning several times a minute through healthy playback. On 2026-08-09 it was read as the cause of broken audio, which it was not. The line now says whose end it was: a reader that disconnected is recorded at debug and says so, anything else stays a warning.
4
+
5
+ ## 2.9.127
6
+
7
+ - **New**: A read that hands the file over out of order now says so. A sequential read walks forwards, so each fragment either continues the piece before it or moves to the very next one; anything else means the bytes reaching the decoder are not the file's bytes in order. Measured 2026-08-09 on a 1080p file with an AC-3 track: the encoder ran at 7.7-9.3x, produced its first segment in 9.1 s, reached 00:02:19 of 02:29:58 — and the AC-3 decoder reported "new coupling strategy must be present in block 0", "exponent 26 is out-of-range" and "invalid coupling range" while the piece store showed no spills and 100% of reads served from memory. Video was being COPIED in the same run, so the viewer lost the picture and the sound together: one fault, not two. The bounds check added in 2.9.126 catches a fragment outside the shared pool and stayed silent throughout, so the bytes came from the pool legitimately and belonged somewhere else. This names which piece arrived where.
8
+
1
9
  ## 2.9.126
2
10
 
3
11
  - **Fix**: A read whose offset lies outside its piece pool ends short and says so, instead of taking the whole source down without a word. The pool is a growable `SharedArrayBuffer` shared with the torrent thread, and an offset only means anything against the buffer of the store that produced it; when the two disagreed, building the view threw `RangeError: Invalid typed array length: 8388608` — one piece — which the process-wide handler swallowed. Reads then stopped answering for good. Measured 2026-08-09: ffmpeg was fed cut-up frames and reported them as a broken AC-3 stream ("new coupling strategy must be present in block 0"), no segment could be closed because no audio frames were produced, and segment #305 was held for a minute eight times running while 76 seeders delivered 35 MB/s. The file was fine; the reads were not. The log line now carries the offset, the length and the pool's size, so a recurrence names its own cause instead of being reconstructed from a decoder's complaints.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@torrent-tv/proxy",
3
- "version": "2.9.126",
3
+ "version": "2.9.128",
4
4
  "description": "Torrent proxy client that exposes webseed-like HTTP stream endpoint.",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "publishConfig": {
@@ -1,270 +1,283 @@
1
- /**
2
- * @file Byte-range aware torrent file streaming endpoint.
3
- *
4
- * Accepts either a `sourceKey` (registered via POST /api/sources) or a raw
5
- * `sourceType` + `source` pair. Responds with HTTP 206 for range requests
6
- * and HTTP 200 for full-file requests.
7
- */
8
-
9
- import { parseRange } from "../../utils/parse-range.js";
10
- import { logger } from "../../utils/logger.js";
11
-
12
- /**
13
- * Resolve source parameters from the query string.
14
- * Prefers a registered `sourceKey`; falls back to inline `sourceType`+`source`.
15
- *
16
- * @param {import("fastify").FastifyRequest["query"]} query
17
- * @param {ReturnType<import("../../store/source-registry.js").createSourceRegistry>} sourceRegistry
18
- * @returns {{ sourceType: string, source: string }}
19
- */
20
- function getSourceParams(query, sourceRegistry) {
21
- const sourceKey = typeof query.sourceKey === "string" ? query.sourceKey : "";
22
- const sourceTypeFromQuery = typeof query.sourceType === "string" ? query.sourceType : "";
23
- const sourceFromQuery = typeof query.source === "string" ? query.source : "";
24
-
25
- const sourceRecord = sourceKey ? sourceRegistry.get(sourceKey) : null;
26
- const sourceType = sourceRecord?.sourceType ?? sourceTypeFromQuery;
27
- const source = sourceRecord?.source ?? sourceFromQuery;
28
- return { sourceType, source };
29
- }
30
-
31
- /**
32
- * How long a request may wait for a torrent that is still being added.
33
- *
34
- * Adding a magnet takes as long as its metadata does — seconds when peers
35
- * answer, forever when none do — and `getTorrent` waits for it. Awaiting that
36
- * with no bound is what made this route answer nothing at all.
37
- */
38
- const TORRENT_READY_TIMEOUT_MS = 10_000;
39
-
40
- /**
41
- * The torrent for a source, or a `TORRENT_NOT_READY` error once the wait has
42
- * gone on long enough to be worth reporting.
43
- *
44
- * The underlying add is NOT cancelled: it keeps running and warms the pool, so
45
- * the client's next attempt is likely to find it ready.
46
- *
47
- * @param {import("../../services/torrent-pool.js").TorrentPool} torrentPool
48
- * @param {string} sourceType
49
- * @param {string} source
50
- * @returns {Promise<import("webtorrent").Torrent>}
51
- */
52
- async function waitForTorrent(torrentPool, sourceType, source) {
53
- let timer = null;
54
- const expiry = new Promise((_resolve, reject) => {
55
- timer = setTimeout(() => {
56
- const error = new Error("Torrent metadata is not available yet.");
57
- error.code = "TORRENT_NOT_READY";
58
- reject(error);
59
- }, TORRENT_READY_TIMEOUT_MS);
60
- timer.unref?.();
61
- });
62
- try {
63
- return await Promise.race([torrentPool.getTorrent(sourceType, source), expiry]);
64
- } finally {
65
- if (timer) {
66
- clearTimeout(timer);
67
- }
68
- }
69
- }
70
-
71
- /**
72
- * Stream a torrent file over HTTP with byte-range support.
73
- *
74
- * GET /stream
75
- *
76
- * @param {import("fastify").FastifyRequest} req
77
- * @param {import("fastify").FastifyReply} reply
78
- * @param {{ sourceRegistry: ReturnType<import("../../store/source-registry.js").createSourceRegistry>, torrentPool: import("../../services/torrent-pool.js").TorrentPool }} deps
79
- * @returns {Promise<void>}
80
- */
81
- export async function handleStreamGet(req, reply, { sourceRegistry, torrentPool }) {
82
- const fileIndexRaw = typeof req.query.fileIndex === "string" ? req.query.fileIndex : "";
83
- const fileIndex = Number(fileIndexRaw);
84
- const { sourceType, source } = getSourceParams(req.query, sourceRegistry);
85
-
86
- if (!sourceType || !source || !Number.isInteger(fileIndex) || fileIndex < 0) {
87
- return reply
88
- .code(400)
89
- .send({ error: "sourceKey or sourceType+source with fileIndex are required." });
90
- }
91
-
92
- let torrent;
93
- try {
94
- torrent = await waitForTorrent(torrentPool, sourceType, source);
95
- } catch (error) {
96
- if (error instanceof Error && error.code === "TORRENT_NOT_READY") {
97
- // Say so, rather than holding the connection until the client gives up.
98
- // Reproduced 2026-08-04 on a magnet whose metadata never arrived: both a
99
- // ranged GET and a HEAD returned nothing at all for the full 30 s the
100
- // probe was willing to wait, and the route had written neither a status
101
- // nor a header — from the client that is indistinguishable from the proxy
102
- // having died, and it left no trace in the log either.
103
- reply.header("Retry-After", "1");
104
- return reply.code(503).send({ error: "Torrent metadata is not available yet." });
105
- }
106
- const message = error instanceof Error ? error.message : String(error);
107
- return reply.code(500).send({ error: `Failed to load torrent source: ${message}` });
108
- }
109
-
110
- const file = torrent.files[fileIndex];
111
- if (!file) {
112
- return reply.code(404).send({ error: "File index was not found in torrent." });
113
- }
114
-
115
- // HEAD asks what a GET would return, not for the bytes. Fastify serves HEAD
116
- // from this same handler, which used to mean a HEAD started a read of the
117
- // WHOLE file: the body was discarded by Node, but the read ran on, the
118
- // response never finished, and the next request on that keep-alive connection
119
- // waited behind it. Measured on the field host: the keyframe-index HEAD
120
- // returned headers in 23 ms and then held the connection until its 15 s
121
- // timeout, which is where the 73 s transcode-session create went.
122
- if (req.method === "HEAD") {
123
- // Written to the raw response on purpose. Answering through `reply.send()`
124
- // with no payload makes Fastify set `content-length: 0`, which is worse
125
- // than useless here: the keyframe index asks for the file size with this
126
- // very request and treats 0 as "no index available", silently falling back
127
- // to an invented segment grid. Hijacking leaves the response to us, and
128
- // Node omits the body for HEAD by itself.
129
- reply.hijack();
130
- reply.raw.writeHead(200, {
131
- "Accept-Ranges": "bytes",
132
- "Content-Type": "application/octet-stream",
133
- "Content-Length": String(file.length),
134
- "Content-Disposition": `inline; filename*=UTF-8''${encodeURIComponent(file.name)}`
135
- });
136
- reply.raw.end();
137
- return;
138
- }
139
-
140
- const releaseFile = torrentPool.acquireFile(torrent, fileIndex);
141
-
142
- const range = parseRange(req.headers.range, file.length);
143
- // Prioritize the pieces at this read position so a seek (a request at a new
144
- // byte offset) downloads first instead of waiting behind the sequential
145
- // backlog — this is what caused ~15-18 s stalls when seeking into an
146
- // undownloaded region.
147
- // Only the encoder's own input read tracks where the viewer is. Everything
148
- // else that comes through here — the codec probe, the keyframe index, a
149
- // subtitle fetch — reads the file's edges, and treating those as a viewer
150
- // position made every session start and every encoder restart look like a
151
- // burst of seeks (measured: two spurious "the viewer moved" per start).
152
- torrentPool.prioritizeByteRange(torrent, fileIndex, range ? range.start : 0, undefined, {
153
- wholeFileRead: range === null,
154
- isPlaybackRead: req.query.reader === "playback"
155
- });
156
-
157
- const start = range ? range.start : 0;
158
- const end = range ? range.end : file.length - 1;
159
- const contentLength = end - start + 1;
160
-
161
- // Written straight out of the torrent's shared memory when that is available:
162
- // no copy on either thread, at the cost of doing the writing by hand, because
163
- // only the write callback tells us when a piece may be released. Falls back to
164
- // the ordinary stream for sources without a shared pool.
165
- // How far ahead of its own read head this reader should ask the swarm for.
166
- // Supplied by whoever knows the media's byte rate — the transcode session
167
- // puts it on the ffmpeg input URL, sized in seconds of playback — because
168
- // this thread knows only bytes, and 32 MB is half a minute of a 1080p film
169
- // but four seconds of a disc remux. Absent or unusable, the reader's own
170
- // default stands.
171
- const windowBytesRaw = Number(req.query.windowBytes);
172
- const windowBytes =
173
- Number.isFinite(windowBytesRaw) && windowBytesRaw > 0 ? windowBytesRaw : undefined;
174
-
175
- const fragments = typeof file.createFragmentReader === "function"
176
- ? file.createFragmentReader({ start, end, windowBytes })
177
- : null;
178
-
179
- if (fragments) {
180
- reply.hijack();
181
- reply.raw.writeHead(range ? 206 : 200, {
182
- "Accept-Ranges": "bytes",
183
- "Content-Type": "application/octet-stream",
184
- "Content-Length": String(contentLength),
185
- "Content-Disposition": `inline; filename*=UTF-8''${encodeURIComponent(file.name)}`,
186
- ...(range ? { "Content-Range": `bytes ${start}-${end}/${file.length}` } : {})
187
- });
188
-
189
- // A client that goes away mid-response must stop the read, or pieces keep
190
- // being fetched for nobody.
191
- reply.raw.once("close", () => fragments.cancel());
192
-
193
- let sent = 0;
194
- try {
195
- for await (const fragment of fragments) {
196
- if (reply.raw.writableEnded || reply.raw.destroyed) {
197
- fragment.release();
198
- break;
199
- }
200
- await new Promise((resolve, reject) => {
201
- reply.raw.write(fragment.bytes, (error) => (error ? reject(error) : resolve()));
202
- });
203
- sent += fragment.bytes.length;
204
- // Only now are these bytes gone: the piece can be unpinned, and the
205
- // slot it occupies reused. Releasing before this point corrupts the
206
- // response silently.
207
- fragment.release();
208
- }
209
- reply.raw.end();
210
- } catch (error) {
211
- // The body is already committed by its headers, so there is nothing
212
- // useful to send instead — drop the connection and let the client retry.
213
- // But say why: swallowing this made the route close connections with no
214
- // status and no trace, which from the client looks like the proxy died
215
- // and from the log looks like nothing happened at all.
216
- logger.warn(
217
- `stream: read of "${file.name}" bytes ${start}-${end} failed after ` +
218
- `${sent} of ${contentLength} bytes: ${error instanceof Error ? error.message : String(error)}`
219
- );
220
- reply.raw.destroy();
221
- } finally {
222
- releaseFile();
223
- }
224
- return;
225
- }
226
-
227
- reply.header("Accept-Ranges", "bytes");
228
- reply.header("Content-Type", "application/octet-stream");
229
- reply.header("Content-Disposition", `inline; filename*=UTF-8''${encodeURIComponent(file.name)}`);
230
-
231
- if (!range) {
232
- reply.header("Content-Length", String(file.length));
233
- const stream = file.createReadStream();
234
- bindRelease(stream, reply, releaseFile);
235
- return reply.send(stream);
236
- }
237
-
238
- reply.code(206);
239
- reply.header("Content-Length", String(contentLength));
240
- reply.header("Content-Range", `bytes ${start}-${end}/${file.length}`);
241
- const stream = file.createReadStream({ start, end });
242
- bindRelease(stream, reply, releaseFile);
243
- return reply.send(stream);
244
- }
245
-
246
- /**
247
- * Attach event listeners that release the file reference exactly once when
248
- * the stream or the underlying HTTP connection closes.
249
- *
250
- * @param {import("node:stream").Readable} stream
251
- * @param {import("fastify").FastifyReply} reply
252
- * @param {() => void} release
253
- * @returns {void}
254
- */
255
- function bindRelease(stream, reply, release) {
256
- let released = false;
257
- const releaseOnce = () => {
258
- if (released) {
259
- return;
260
- }
261
- released = true;
262
- release();
263
- };
264
-
265
- stream.on("close", releaseOnce);
266
- stream.on("end", releaseOnce);
267
- stream.on("error", releaseOnce);
268
- reply.raw.once("close", releaseOnce);
269
- reply.raw.once("finish", releaseOnce);
270
- }
1
+ /**
2
+ * @file Byte-range aware torrent file streaming endpoint.
3
+ *
4
+ * Accepts either a `sourceKey` (registered via POST /api/sources) or a raw
5
+ * `sourceType` + `source` pair. Responds with HTTP 206 for range requests
6
+ * and HTTP 200 for full-file requests.
7
+ */
8
+
9
+ import { parseRange } from "../../utils/parse-range.js";
10
+ import { logger } from "../../utils/logger.js";
11
+
12
+ /**
13
+ * Resolve source parameters from the query string.
14
+ * Prefers a registered `sourceKey`; falls back to inline `sourceType`+`source`.
15
+ *
16
+ * @param {import("fastify").FastifyRequest["query"]} query
17
+ * @param {ReturnType<import("../../store/source-registry.js").createSourceRegistry>} sourceRegistry
18
+ * @returns {{ sourceType: string, source: string }}
19
+ */
20
+ function getSourceParams(query, sourceRegistry) {
21
+ const sourceKey = typeof query.sourceKey === "string" ? query.sourceKey : "";
22
+ const sourceTypeFromQuery = typeof query.sourceType === "string" ? query.sourceType : "";
23
+ const sourceFromQuery = typeof query.source === "string" ? query.source : "";
24
+
25
+ const sourceRecord = sourceKey ? sourceRegistry.get(sourceKey) : null;
26
+ const sourceType = sourceRecord?.sourceType ?? sourceTypeFromQuery;
27
+ const source = sourceRecord?.source ?? sourceFromQuery;
28
+ return { sourceType, source };
29
+ }
30
+
31
+ /**
32
+ * How long a request may wait for a torrent that is still being added.
33
+ *
34
+ * Adding a magnet takes as long as its metadata does — seconds when peers
35
+ * answer, forever when none do — and `getTorrent` waits for it. Awaiting that
36
+ * with no bound is what made this route answer nothing at all.
37
+ */
38
+ const TORRENT_READY_TIMEOUT_MS = 10_000;
39
+
40
+ /**
41
+ * The torrent for a source, or a `TORRENT_NOT_READY` error once the wait has
42
+ * gone on long enough to be worth reporting.
43
+ *
44
+ * The underlying add is NOT cancelled: it keeps running and warms the pool, so
45
+ * the client's next attempt is likely to find it ready.
46
+ *
47
+ * @param {import("../../services/torrent-pool.js").TorrentPool} torrentPool
48
+ * @param {string} sourceType
49
+ * @param {string} source
50
+ * @returns {Promise<import("webtorrent").Torrent>}
51
+ */
52
+ async function waitForTorrent(torrentPool, sourceType, source) {
53
+ let timer = null;
54
+ const expiry = new Promise((_resolve, reject) => {
55
+ timer = setTimeout(() => {
56
+ const error = new Error("Torrent metadata is not available yet.");
57
+ error.code = "TORRENT_NOT_READY";
58
+ reject(error);
59
+ }, TORRENT_READY_TIMEOUT_MS);
60
+ timer.unref?.();
61
+ });
62
+ try {
63
+ return await Promise.race([torrentPool.getTorrent(sourceType, source), expiry]);
64
+ } finally {
65
+ if (timer) {
66
+ clearTimeout(timer);
67
+ }
68
+ }
69
+ }
70
+
71
+ /**
72
+ * Stream a torrent file over HTTP with byte-range support.
73
+ *
74
+ * GET /stream
75
+ *
76
+ * @param {import("fastify").FastifyRequest} req
77
+ * @param {import("fastify").FastifyReply} reply
78
+ * @param {{ sourceRegistry: ReturnType<import("../../store/source-registry.js").createSourceRegistry>, torrentPool: import("../../services/torrent-pool.js").TorrentPool }} deps
79
+ * @returns {Promise<void>}
80
+ */
81
+ export async function handleStreamGet(req, reply, { sourceRegistry, torrentPool }) {
82
+ const fileIndexRaw = typeof req.query.fileIndex === "string" ? req.query.fileIndex : "";
83
+ const fileIndex = Number(fileIndexRaw);
84
+ const { sourceType, source } = getSourceParams(req.query, sourceRegistry);
85
+
86
+ if (!sourceType || !source || !Number.isInteger(fileIndex) || fileIndex < 0) {
87
+ return reply
88
+ .code(400)
89
+ .send({ error: "sourceKey or sourceType+source with fileIndex are required." });
90
+ }
91
+
92
+ let torrent;
93
+ try {
94
+ torrent = await waitForTorrent(torrentPool, sourceType, source);
95
+ } catch (error) {
96
+ if (error instanceof Error && error.code === "TORRENT_NOT_READY") {
97
+ // Say so, rather than holding the connection until the client gives up.
98
+ // Reproduced 2026-08-04 on a magnet whose metadata never arrived: both a
99
+ // ranged GET and a HEAD returned nothing at all for the full 30 s the
100
+ // probe was willing to wait, and the route had written neither a status
101
+ // nor a header — from the client that is indistinguishable from the proxy
102
+ // having died, and it left no trace in the log either.
103
+ reply.header("Retry-After", "1");
104
+ return reply.code(503).send({ error: "Torrent metadata is not available yet." });
105
+ }
106
+ const message = error instanceof Error ? error.message : String(error);
107
+ return reply.code(500).send({ error: `Failed to load torrent source: ${message}` });
108
+ }
109
+
110
+ const file = torrent.files[fileIndex];
111
+ if (!file) {
112
+ return reply.code(404).send({ error: "File index was not found in torrent." });
113
+ }
114
+
115
+ // HEAD asks what a GET would return, not for the bytes. Fastify serves HEAD
116
+ // from this same handler, which used to mean a HEAD started a read of the
117
+ // WHOLE file: the body was discarded by Node, but the read ran on, the
118
+ // response never finished, and the next request on that keep-alive connection
119
+ // waited behind it. Measured on the field host: the keyframe-index HEAD
120
+ // returned headers in 23 ms and then held the connection until its 15 s
121
+ // timeout, which is where the 73 s transcode-session create went.
122
+ if (req.method === "HEAD") {
123
+ // Written to the raw response on purpose. Answering through `reply.send()`
124
+ // with no payload makes Fastify set `content-length: 0`, which is worse
125
+ // than useless here: the keyframe index asks for the file size with this
126
+ // very request and treats 0 as "no index available", silently falling back
127
+ // to an invented segment grid. Hijacking leaves the response to us, and
128
+ // Node omits the body for HEAD by itself.
129
+ reply.hijack();
130
+ reply.raw.writeHead(200, {
131
+ "Accept-Ranges": "bytes",
132
+ "Content-Type": "application/octet-stream",
133
+ "Content-Length": String(file.length),
134
+ "Content-Disposition": `inline; filename*=UTF-8''${encodeURIComponent(file.name)}`
135
+ });
136
+ reply.raw.end();
137
+ return;
138
+ }
139
+
140
+ const releaseFile = torrentPool.acquireFile(torrent, fileIndex);
141
+
142
+ const range = parseRange(req.headers.range, file.length);
143
+ // Prioritize the pieces at this read position so a seek (a request at a new
144
+ // byte offset) downloads first instead of waiting behind the sequential
145
+ // backlog — this is what caused ~15-18 s stalls when seeking into an
146
+ // undownloaded region.
147
+ // Only the encoder's own input read tracks where the viewer is. Everything
148
+ // else that comes through here — the codec probe, the keyframe index, a
149
+ // subtitle fetch — reads the file's edges, and treating those as a viewer
150
+ // position made every session start and every encoder restart look like a
151
+ // burst of seeks (measured: two spurious "the viewer moved" per start).
152
+ torrentPool.prioritizeByteRange(torrent, fileIndex, range ? range.start : 0, undefined, {
153
+ wholeFileRead: range === null,
154
+ isPlaybackRead: req.query.reader === "playback"
155
+ });
156
+
157
+ const start = range ? range.start : 0;
158
+ const end = range ? range.end : file.length - 1;
159
+ const contentLength = end - start + 1;
160
+
161
+ // Written straight out of the torrent's shared memory when that is available:
162
+ // no copy on either thread, at the cost of doing the writing by hand, because
163
+ // only the write callback tells us when a piece may be released. Falls back to
164
+ // the ordinary stream for sources without a shared pool.
165
+ // How far ahead of its own read head this reader should ask the swarm for.
166
+ // Supplied by whoever knows the media's byte rate — the transcode session
167
+ // puts it on the ffmpeg input URL, sized in seconds of playback — because
168
+ // this thread knows only bytes, and 32 MB is half a minute of a 1080p film
169
+ // but four seconds of a disc remux. Absent or unusable, the reader's own
170
+ // default stands.
171
+ const windowBytesRaw = Number(req.query.windowBytes);
172
+ const windowBytes =
173
+ Number.isFinite(windowBytesRaw) && windowBytesRaw > 0 ? windowBytesRaw : undefined;
174
+
175
+ const fragments = typeof file.createFragmentReader === "function"
176
+ ? file.createFragmentReader({ start, end, windowBytes })
177
+ : null;
178
+
179
+ if (fragments) {
180
+ reply.hijack();
181
+ reply.raw.writeHead(range ? 206 : 200, {
182
+ "Accept-Ranges": "bytes",
183
+ "Content-Type": "application/octet-stream",
184
+ "Content-Length": String(contentLength),
185
+ "Content-Disposition": `inline; filename*=UTF-8''${encodeURIComponent(file.name)}`,
186
+ ...(range ? { "Content-Range": `bytes ${start}-${end}/${file.length}` } : {})
187
+ });
188
+
189
+ // A client that goes away mid-response must stop the read, or pieces keep
190
+ // being fetched for nobody.
191
+ reply.raw.once("close", () => fragments.cancel());
192
+
193
+ let sent = 0;
194
+ try {
195
+ for await (const fragment of fragments) {
196
+ if (reply.raw.writableEnded || reply.raw.destroyed) {
197
+ fragment.release();
198
+ break;
199
+ }
200
+ await new Promise((resolve, reject) => {
201
+ reply.raw.write(fragment.bytes, (error) => (error ? reject(error) : resolve()));
202
+ });
203
+ sent += fragment.bytes.length;
204
+ // Only now are these bytes gone: the piece can be unpinned, and the
205
+ // slot it occupies reused. Releasing before this point corrupts the
206
+ // response silently.
207
+ fragment.release();
208
+ }
209
+ reply.raw.end();
210
+ } catch (error) {
211
+ // The body is already committed by its headers, so there is nothing
212
+ // useful to send instead — drop the connection and let the client retry.
213
+ // But say why: swallowing this made the route close connections with no
214
+ // status and no trace, which from the client looks like the proxy died
215
+ // and from the log looks like nothing happened at all.
216
+ // WHOSE end it was. A write cancelled because the consumer went away is
217
+ // the ordinary end of a read: ffmpeg is terminated on every seek and
218
+ // whenever the look-ahead bound suspends it, and its connection closes
219
+ // with it. Reported as a failure, that line fired several times a minute
220
+ // during healthy playback — and on 2026-08-09 it was read as the cause of
221
+ // broken audio, which it was not. A read that ends because the reader
222
+ // left is not a fault and must not be dressed as one; anything else is.
223
+ const message = error instanceof Error ? error.message : String(error);
224
+ const consumerLeft = req.raw.aborted || reply.raw.destroyed || /ECANCELED|EPIPE|ERR_STREAM_DESTROYED/.test(message);
225
+ const line =
226
+ `stream: read of "${file.name}" bytes ${start}-${end} ended after ` +
227
+ `${sent} of ${contentLength} bytes: ${message}`;
228
+ if (consumerLeft) {
229
+ logger.debug(`${line} (the reader disconnected — expected on an encoder restart)`);
230
+ } else {
231
+ logger.warn(line);
232
+ }
233
+ reply.raw.destroy();
234
+ } finally {
235
+ releaseFile();
236
+ }
237
+ return;
238
+ }
239
+
240
+ reply.header("Accept-Ranges", "bytes");
241
+ reply.header("Content-Type", "application/octet-stream");
242
+ reply.header("Content-Disposition", `inline; filename*=UTF-8''${encodeURIComponent(file.name)}`);
243
+
244
+ if (!range) {
245
+ reply.header("Content-Length", String(file.length));
246
+ const stream = file.createReadStream();
247
+ bindRelease(stream, reply, releaseFile);
248
+ return reply.send(stream);
249
+ }
250
+
251
+ reply.code(206);
252
+ reply.header("Content-Length", String(contentLength));
253
+ reply.header("Content-Range", `bytes ${start}-${end}/${file.length}`);
254
+ const stream = file.createReadStream({ start, end });
255
+ bindRelease(stream, reply, releaseFile);
256
+ return reply.send(stream);
257
+ }
258
+
259
+ /**
260
+ * Attach event listeners that release the file reference exactly once when
261
+ * the stream or the underlying HTTP connection closes.
262
+ *
263
+ * @param {import("node:stream").Readable} stream
264
+ * @param {import("fastify").FastifyReply} reply
265
+ * @param {() => void} release
266
+ * @returns {void}
267
+ */
268
+ function bindRelease(stream, reply, release) {
269
+ let released = false;
270
+ const releaseOnce = () => {
271
+ if (released) {
272
+ return;
273
+ }
274
+ released = true;
275
+ release();
276
+ };
277
+
278
+ stream.on("close", releaseOnce);
279
+ stream.on("end", releaseOnce);
280
+ stream.on("error", releaseOnce);
281
+ reply.raw.once("close", releaseOnce);
282
+ reply.raw.once("finish", releaseOnce);
283
+ }
@@ -37,6 +37,14 @@ export class TorrentWorkerClient {
37
37
  #caller;
38
38
  /** Receive-side handles for in-flight reads, keyed by request id. */
39
39
  #reads = new Map();
40
+
41
+ /**
42
+ * The last piece served to each open read, so a fragment arriving out of
43
+ * order can be named. Cleared when the read ends.
44
+ *
45
+ * @type {Map<number, number>}
46
+ */
47
+ #lastPieceByRead = new Map();
40
48
  /** Each torrent's piece pool, so a fragment can be read where it lies. */
41
49
  #poolBySource = new Map();
42
50
  /** Which pool an in-flight read belongs to, keyed by request id. */
@@ -104,6 +112,25 @@ export class TorrentWorkerClient {
104
112
  this.#worker.postMessage({ type: Event.FRAGMENT_DONE, id: message.id });
105
113
  break;
106
114
  }
115
+ // A sequential read walks the file forwards, so each fragment either
116
+ // continues the piece before it or moves to the very next one.
117
+ // Anything else means the bytes handed to the decoder are not the
118
+ // file's bytes in order — which is what a decoder complaining about
119
+ // its input has twice turned out to mean (2.9.126, and the AC-3
120
+ // failure of 2026-08-09: the encoder ran at 9.3x, the piece store
121
+ // reported no spills and 100% of reads from memory, and the decoder
122
+ // still saw "new coupling strategy must be present in block 0"). The
123
+ // bounds check catches a fragment outside the pool; this catches one
124
+ // inside it that belongs somewhere else.
125
+ const lastPiece = this.#lastPieceByRead.get(message.id);
126
+ if (lastPiece !== undefined && message.pieceIndex !== lastPiece && message.pieceIndex !== lastPiece + 1) {
127
+ logger.warn(
128
+ `torrent-worker: read ${message.id} jumped from piece ${lastPiece} to ` +
129
+ `${message.pieceIndex} (${message.length}B at pool offset ${message.offset}) — ` +
130
+ "the consumer is being handed the file out of order"
131
+ );
132
+ }
133
+ this.#lastPieceByRead.set(message.id, message.pieceIndex);
107
134
  const view = new Uint8Array(pool, message.offset, message.length);
108
135
 
109
136
  const reader = this.#fragmentReaders.get(message.id);
@@ -132,6 +159,7 @@ export class TorrentWorkerClient {
132
159
  break;
133
160
  }
134
161
  case Event.READ_END:
162
+ this.#lastPieceByRead.delete(message.id);
135
163
  this.#reads.get(message.id)?.close();
136
164
  this.#reads.delete(message.id);
137
165
  this.#fragmentReaders.get(message.id)?.close();
@@ -260,6 +288,7 @@ export class TorrentWorkerClient {
260
288
  void this.#caller.call(Command.CANCEL_READ, { readId }).catch(() => undefined);
261
289
  this.#reads.delete(readId);
262
290
  this.#poolByRead.delete(readId);
291
+ this.#lastPieceByRead.delete(readId);
263
292
  }
264
293
  });
265
294
  this.#reads.set(readId, receive);