@torrent-tv/proxy 2.9.127 → 2.9.129
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 +8 -0
- package/package.json +1 -1
- package/routes/stream/get.js +283 -270
- package/services/hls-session-manager.js +3824 -3782
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
## 2.9.129
|
|
2
|
+
|
|
3
|
+
- **New**: A held segment says why it is held. A hold was silent, and that silence has now cost three releases: a file that exists, a route answering "not yet", and nothing saying which of the several reasons applied. Measured 2026-08-09 — a run begun mid-file at segment #317 produced two minutes of video from #317 upwards at 10.5x while #317 itself was held for 46 116 ms and then answered 404, once the browser had already given up. At most once every five seconds per file it now names the reason (not on disk, or present but the next segment has not been started), together with the index the run began at, where the viewer is, and whether the encoder is alive. The readiness rule — a segment counts as finished once the NEXT one exists — is the suspect for a resume, because for the first segment of a mid-file run that rule decides whether playback begins at all.
|
|
4
|
+
|
|
5
|
+
## 2.9.128
|
|
6
|
+
|
|
7
|
+
- **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.
|
|
8
|
+
|
|
1
9
|
## 2.9.127
|
|
2
10
|
|
|
3
11
|
- **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.
|
package/package.json
CHANGED
package/routes/stream/get.js
CHANGED
|
@@ -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
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
reply.header("
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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
|
+
}
|