@torrent-tv/proxy 2.36.1 → 2.37.0

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,14 @@
1
+ ## 2.37.0
2
+
3
+ - **Fix**: The segment the viewer seeks TO is no longer refused as stale. A seek bumps the wait epoch so requests made for the position being LEFT stop being held, and the epoch alone cannot tell those apart from the request for the position just arrived at — hls.js asks for it within milliseconds of the seek, and it raced the bump. Measured 2026-08-18: a seek to 1061.0 s, `segment-00101` answered 503 twice within 80 ms, the player never asked for it again, and instead re-fetched `a/0/segment-00103` and `a/0/segment-00104` **737 and 736 times over 149 seconds** — about half a gigabyte of the same two segments — while the picture stood at `t=1061.0s readyState=1` until the session ended. A held request is now released only when its segment lies behind where the viewer now is, or so far ahead that the running encode will not reach it; anything between is what the viewer is waiting for and is held.
4
+ - **New**: The log survives the container. `--log-file <path>` writes every line to a file as well as the console, appending across restarts and rotating at 32 MB with one previous turn kept. The console is the container's stdout, and the container is exactly what does not survive a crash: thirteen SIGSEGVs on 2026-08-18 each had the watchdog recreate it, taking every line before the crash away, and a deploy of ours destroyed the evidence for two field reports the same day. Opt-in and named by the caller, so nothing here assumes Home Assistant — the addon points it at `/data`.
5
+ - **New**: A refusal says what it refused. `[hold] <segment> refused: the viewer is at <position>s and this is not the segment there`, and a request kept across a seek says so too. The old line said only "superseded", which is why the freeze above took a day to explain.
6
+
7
+ ## 2.36.2
8
+
9
+ - **Fix**: A live session no longer answers 404 to the master playlist it has just published. The browser is handed `master.m3u8` when the session is created, and which rungs are worth OFFERING is recomputed every five seconds — so on 2026-08-18 a five-rung offer became a one-rung offer **192 ms** after creation (the session's own encoder started, charging the contention penalty of 2.35.0, and the first supply reading raised the bar of 2.36.0 from 1.00x to 1.06x), `buildMasterPlaylist` returned null for having fewer than two rungs, and the master answered 404. hls.js treats that as fatal and unrecoverable, so nothing played at all. The master now lists what CAN be spliced onto this session's cut grid — a fact about the source, settled once — while the live judgement stays where it belongs, in `offeredHeights` and in every progress report, which is what the viewer's menu already follows. The variant routes honour the published set too, so a quality switch can no longer meet a 404 on a rung the master named.
10
+ - **Fix**: Peer discovery no longer starves behind name resolution. Node resolves host names on the libuv thread pool, which holds four threads by default; a torrent announces to every tracker in its file at once, so four names resolve and the rest queue — and a tracker that no longer exists holds its thread for the resolver's full ten-second timeout while every announce behind it blows its own fifteen-second deadline. Measured inside the addon container: the ten trackers of one film took **7.58 s** to resolve as a burst and **27-42 ms** each with a larger pool. That film has 517 seeders on a tracker that answers in 50 ms, and it spent eleven minutes with **zero peers** while four other torrents in the same process were fine — they were the ones whose live trackers happened to fall in the first four. The pool is now stated before anything can create it (`services/thread-pool.js`, imported first by the entry point), and a deployment that states its own size is left alone.
11
+
1
12
  ## 2.36.1
2
13
 
3
14
  - **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.
package/bin/cli.js CHANGED
@@ -8,6 +8,11 @@
8
8
  * automatically on reconnect so the server's in-memory store stays consistent.
9
9
  */
10
10
 
11
+ // FIRST, and it must stay first: it sets how many blocking calls this process
12
+ // can have in flight, and a module's imports are evaluated before its body, so
13
+ // anything imported above it would get the default pool. See the file itself
14
+ // for the measurement that made it necessary.
15
+ import "../services/thread-pool.js";
11
16
  import { Command } from "commander";
12
17
  import crypto from "node:crypto";
13
18
  import { spawnSync } from "node:child_process";
@@ -22,7 +27,7 @@ import { collectHealthMetrics } from "../services/health-collector.js";
22
27
  import { createPortMapper } from "../services/port-mapper.js";
23
28
  import { classifyNat } from "../services/nat-classifier.js";
24
29
  import { DEFAULT_SEGMENT_FORMAT_ID, SEGMENT_FORMAT_IDS } from "../services/segment-formats/index.js";
25
- import { logger } from "../utils/logger.js";
30
+ import { logToFile, logger } from "../utils/logger.js";
26
31
 
27
32
  const require = createRequire(import.meta.url);
28
33
  const { version: PROXY_VERSION } = require("../package.json");
@@ -80,6 +85,10 @@ program
80
85
  "--state-dir <path>",
81
86
  "Where to keep what this host has measured about itself (default: beside the installed proxy)"
82
87
  )
88
+ .option(
89
+ "--log-file <path>",
90
+ "Also write the log to this file, so a crash or an update does not take it with them"
91
+ )
83
92
  .option(
84
93
  "--segment-format <format>",
85
94
  `HLS output container: ${SEGMENT_FORMAT_IDS.join(" | ")}`,
@@ -267,6 +276,7 @@ async function shutdown(signal) {
267
276
  }
268
277
 
269
278
  try {
279
+ logToFile(options.logFile);
270
280
  if (transcodeAudio) {
271
281
  assertFfmpegAvailability();
272
282
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@torrent-tv/proxy",
3
- "version": "2.36.1",
3
+ "version": "2.37.0",
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,180 +1,201 @@
1
- import { logger } from "../../../utils/logger.js";
2
-
3
- /**
4
- * How long a request for a not-yet-produced file is held before answering with
5
- * a retryable 503.
6
- *
7
- * MEASUREMENT MODE (2026-08-02): deliberately far above any plausible player
8
- * deadline, so OUR limit never fires first. Whatever ends the wait is then the
9
- * player's own behaviour — which is exactly what we need to observe. The
10
- * `[hold]` log line at the call site records, per request, whether the segment
11
- * arrived, whether we gave up, or whether the CLIENT aborted, and after how
12
- * long.
13
- *
14
- * The previous value (2 s) was chosen to dodge a reported iOS AVPlayer ~3.5 s
15
- * response-header deadline. That deadline never appears in our own logs (no
16
- * -12889 across six hours of production logs), and every reference project
17
- * holds rather than refusing: Jellyfin and hls-vod-too hold unbounded,
18
- * hls-media-server holds 10 s. The early refusal is what made the player probe
19
- * scattered positions, which then steered the encoder off target. Choose the
20
- * final value from what this measurement shows, not from a number read
21
- * elsewhere.
22
- */
23
- const SEGMENT_WAIT_MS = 60_000;
24
-
25
- /**
26
- * Serve HLS playlist and segment files from an active transcode session.
27
- *
28
- * Briefly waits for the requested file to appear, then answers with a
29
- * retryable 503 rather than holding the connection, so clients — in
30
- * particular iOS's native HLS player — never hit their own response
31
- * deadline while a segment is still being produced.
32
- *
33
- * GET /transcode/:sessionId/:fileName
34
- *
35
- * @param {import("fastify").FastifyRequest} req
36
- * @param {import("fastify").FastifyReply} reply
37
- * @param {{ hlsSessionManager: import("../../../services/hls-session-manager.js").HlsSessionManager }} deps
38
- * @returns {Promise<void>}
39
- */
40
- export async function handleTranscodeSessionFileGet(req, reply, { hlsSessionManager }) {
41
- const sessionId = typeof req.params.sessionId === "string" ? req.params.sessionId : "";
42
- const fileName = typeof req.params.fileName === "string" ? req.params.fileName : "";
43
- return serveSessionFile(req, reply, { hlsSessionManager, sessionId, fileName });
44
- }
45
-
46
- /**
47
- * Serve one playlist or segment from a named session.
48
- *
49
- * Split from the route above because the variant route
50
- * (`/transcode/:sessionId/v/:height/:fileName`) serves the same files from
51
- * another session of the same family, and must hold, log and answer them
52
- * identically — a switch of quality must not go through a different code path
53
- * from the stream it switches away from.
54
- *
55
- * @param {import("fastify").FastifyRequest} req
56
- * @param {import("fastify").FastifyReply} reply
57
- * @param {{ hlsSessionManager: import("../../../services/hls-session-manager.js").HlsSessionManager, sessionId: string, fileName: string }} params
58
- * @returns {Promise<void>}
59
- */
60
- export async function serveSessionFile(req, reply, { hlsSessionManager, sessionId, fileName }) {
61
- // Hold the request only briefly, then answer "retry" instead of waiting for
62
- // the segment. iOS's native HLS player (AVPlayer) enforces a hard ~3.5 s
63
- // deadline on RESPONSE HEADERS and raises -12889 ("No response for media
64
- // file") when it passes — it then cancels in-flight requests, probes
65
- // neighbouring positions and can restart the stream from the beginning. That
66
- // is exactly the post-seek "player thrashing" seen in the field, because a
67
- // seek restarts ffmpeg and the first segment then takes far longer than 3.5 s
68
- // to appear. Holding the connection for 30 s (as this did) guaranteed the
69
- // timeout on every seek. A short hold keeps the fast path intact (a ready or
70
- // nearly-ready segment is still served on the first request) while a slow one
71
- // gets a prompt retryable answer, which resets the player's own deadline.
72
- // hls.js is unaffected: it consumes the 503 through its retry policy, whose
73
- // budget the client widens to match (see hls-player.js fragLoadPolicy).
74
- // Instrumented wait. `clientAborted` flips when the player drops the
75
- // connection while we are still holding it — the single most informative
76
- // signal about its real patience, and observable only from this side.
77
- const holdStartedAt = Date.now();
78
- let clientAborted = false;
79
- const onClientAbort = () => { clientAborted = true; };
80
- req.raw.on("close", onClientAbort);
81
-
82
- const result = await waitForSessionFile(hlsSessionManager, sessionId, fileName, SEGMENT_WAIT_MS);
83
-
84
- req.raw.off("close", onClientAbort);
85
- const heldMs = Date.now() - holdStartedAt;
86
- if (result.isPlaylist !== true) {
87
- const outcome = clientAborted
88
- ? "client-aborted"
89
- : result.kind === "ok" ? "served" : result.kind;
90
- logger.info(`[hold] ${fileName} ${outcome} after ${heldMs}ms`);
91
- }
92
-
93
- if (result.kind === "not-found") {
94
- return reply.code(404).send({ error: "Transcode session file was not found." });
95
- }
96
- if (result.kind === "superseded") {
97
- // The viewer moved while this was being held. Answer at once so the player
98
- // can ask for where it is now; `Retry-After: 0` because there is nothing to
99
- // wait for this segment is simply no longer the one being watched.
100
- reply.header("Retry-After", "0");
101
- return reply.code(503).send({ error: "Superseded by a seek." });
102
- }
103
- if (result.kind === "warming-up") {
104
- // The segment is still being produced (e.g. just after a seek-restart).
105
- // Return a retryable 503 — never 202, which hls.js cannot consume as a
106
- // media segment — so the player retries the fetch shortly.
107
- reply.header("Retry-After", "1");
108
- // `Retry-After` tells the player to re-request THIS segment after a short
109
- // pause. Without it a bare 503 reads as "nothing here", and the player goes
110
- // looking elsewhere: because our synthetic VOD playlist lists every segment
111
- // of the file, it believes they all exist and SCANS them (field log: one
112
- // user seek produced probes at #617, #717, #732…). That scan is what used
113
- // to steer the encoder off the real target. Whether iOS's native player
114
- // honours the hint is not guaranteed its behaviour is closed — but this
115
- // is the standard, correct way to say "wait, don't look elsewhere", and
116
- // hls.js already retries the same fragment regardless.
117
- reply.header("Retry-After", "1");
118
- return reply.code(503).send({ error: "Transcode segment is still being produced." });
119
- }
120
- if (result.kind === "failed") {
121
- return reply.code(500).send({ error: result.message });
122
- }
123
-
124
- if (result.isPlaylist) {
125
- reply.header("Cache-Control", "no-store");
126
- } else {
127
- reply.header("Cache-Control", "public, max-age=60");
128
- }
129
- reply.header("Content-Type", result.contentType);
130
- return reply.send(result.stream);
131
- }
132
-
133
- /**
134
- * Poll `hlsSessionManager.getFileStream()` until the file is available,
135
- * the session fails, or the timeout elapses.
136
- *
137
- * @param {import("../../../services/hls-session-manager.js").HlsSessionManager} hlsSessionManager
138
- * @param {string} sessionId
139
- * @param {string} fileName
140
- * @param {number} timeoutMs
141
- * @returns {Promise<Awaited<ReturnType<import("../../../services/hls-session-manager.js").HlsSessionManager["getFileStream"]>>>}
142
- */
143
- export async function waitForSessionFile(hlsSessionManager, sessionId, fileName, timeoutMs) {
144
- const startedAt = Date.now();
145
- // One sequence number for THIS request, reused by every poll below, so the
146
- // session can tell a newly-arrived request apart from an old one polling
147
- // again see HlsSessionManager#ensureEncodingFor for the encoder ping-pong
148
- // this prevents when one seek-bar scrub fires several segment requests.
149
- const requestSeq = hlsSessionManager.nextRequestSeq(sessionId);
150
- // The viewer's position when this request was made. A seek makes every held
151
- // request stale — it asks for a segment nobody is going to watch — and hls.js
152
- // keeps only ONE fragment load outstanding, so holding on blocks the request
153
- // the player actually needs now. Measured: 57 s of a 58 s backward seek was
154
- // this wait, and the segment the viewer wanted took 15 ms once it was asked
155
- // for.
156
- const seekEpoch = hlsSessionManager.seekEpoch(sessionId);
157
- while (Date.now() - startedAt < timeoutMs) {
158
- const result = await hlsSessionManager.getFileStream(sessionId, fileName, { requestSeq });
159
- if (result.kind !== "warming-up") {
160
- return result;
161
- }
162
- if (hlsSessionManager.seekEpoch(sessionId) !== seekEpoch) {
163
- return { kind: "superseded" };
164
- }
165
- await delay(300);
166
- }
167
- return { kind: "warming-up" };
168
- }
169
-
170
- /**
171
- * Resolve after a given number of milliseconds.
172
- *
173
- * @param {number} ms
174
- * @returns {Promise<void>}
175
- */
176
- function delay(ms) {
177
- return new Promise((resolve) => {
178
- setTimeout(resolve, ms);
179
- });
180
- }
1
+ import { logger } from "../../../utils/logger.js";
2
+
3
+ /**
4
+ * How long a request for a not-yet-produced file is held before answering with
5
+ * a retryable 503.
6
+ *
7
+ * MEASUREMENT MODE (2026-08-02): deliberately far above any plausible player
8
+ * deadline, so OUR limit never fires first. Whatever ends the wait is then the
9
+ * player's own behaviour — which is exactly what we need to observe. The
10
+ * `[hold]` log line at the call site records, per request, whether the segment
11
+ * arrived, whether we gave up, or whether the CLIENT aborted, and after how
12
+ * long.
13
+ *
14
+ * The previous value (2 s) was chosen to dodge a reported iOS AVPlayer ~3.5 s
15
+ * response-header deadline. That deadline never appears in our own logs (no
16
+ * -12889 across six hours of production logs), and every reference project
17
+ * holds rather than refusing: Jellyfin and hls-vod-too hold unbounded,
18
+ * hls-media-server holds 10 s. The early refusal is what made the player probe
19
+ * scattered positions, which then steered the encoder off target. Choose the
20
+ * final value from what this measurement shows, not from a number read
21
+ * elsewhere.
22
+ */
23
+ const SEGMENT_WAIT_MS = 60_000;
24
+
25
+ /**
26
+ * Serve HLS playlist and segment files from an active transcode session.
27
+ *
28
+ * Briefly waits for the requested file to appear, then answers with a
29
+ * retryable 503 rather than holding the connection, so clients — in
30
+ * particular iOS's native HLS player — never hit their own response
31
+ * deadline while a segment is still being produced.
32
+ *
33
+ * GET /transcode/:sessionId/:fileName
34
+ *
35
+ * @param {import("fastify").FastifyRequest} req
36
+ * @param {import("fastify").FastifyReply} reply
37
+ * @param {{ hlsSessionManager: import("../../../services/hls-session-manager.js").HlsSessionManager }} deps
38
+ * @returns {Promise<void>}
39
+ */
40
+ export async function handleTranscodeSessionFileGet(req, reply, { hlsSessionManager }) {
41
+ const sessionId = typeof req.params.sessionId === "string" ? req.params.sessionId : "";
42
+ const fileName = typeof req.params.fileName === "string" ? req.params.fileName : "";
43
+ return serveSessionFile(req, reply, { hlsSessionManager, sessionId, fileName });
44
+ }
45
+
46
+ /**
47
+ * Serve one playlist or segment from a named session.
48
+ *
49
+ * Split from the route above because the variant route
50
+ * (`/transcode/:sessionId/v/:height/:fileName`) serves the same files from
51
+ * another session of the same family, and must hold, log and answer them
52
+ * identically — a switch of quality must not go through a different code path
53
+ * from the stream it switches away from.
54
+ *
55
+ * @param {import("fastify").FastifyRequest} req
56
+ * @param {import("fastify").FastifyReply} reply
57
+ * @param {{ hlsSessionManager: import("../../../services/hls-session-manager.js").HlsSessionManager, sessionId: string, fileName: string }} params
58
+ * @returns {Promise<void>}
59
+ */
60
+ export async function serveSessionFile(req, reply, { hlsSessionManager, sessionId, fileName }) {
61
+ // Hold the request only briefly, then answer "retry" instead of waiting for
62
+ // the segment. iOS's native HLS player (AVPlayer) enforces a hard ~3.5 s
63
+ // deadline on RESPONSE HEADERS and raises -12889 ("No response for media
64
+ // file") when it passes — it then cancels in-flight requests, probes
65
+ // neighbouring positions and can restart the stream from the beginning. That
66
+ // is exactly the post-seek "player thrashing" seen in the field, because a
67
+ // seek restarts ffmpeg and the first segment then takes far longer than 3.5 s
68
+ // to appear. Holding the connection for 30 s (as this did) guaranteed the
69
+ // timeout on every seek. A short hold keeps the fast path intact (a ready or
70
+ // nearly-ready segment is still served on the first request) while a slow one
71
+ // gets a prompt retryable answer, which resets the player's own deadline.
72
+ // hls.js is unaffected: it consumes the 503 through its retry policy, whose
73
+ // budget the client widens to match (see hls-player.js fragLoadPolicy).
74
+ // Instrumented wait. `clientAborted` flips when the player drops the
75
+ // connection while we are still holding it — the single most informative
76
+ // signal about its real patience, and observable only from this side.
77
+ const holdStartedAt = Date.now();
78
+ let clientAborted = false;
79
+ const onClientAbort = () => { clientAborted = true; };
80
+ req.raw.on("close", onClientAbort);
81
+
82
+ const result = await waitForSessionFile(hlsSessionManager, sessionId, fileName, SEGMENT_WAIT_MS);
83
+
84
+ req.raw.off("close", onClientAbort);
85
+ const heldMs = Date.now() - holdStartedAt;
86
+ if (result.isPlaylist !== true) {
87
+ const outcome = clientAborted
88
+ ? "client-aborted"
89
+ : result.kind === "ok" ? "served" : result.kind;
90
+ logger.info(`[hold] ${fileName} ${outcome} after ${heldMs}ms`);
91
+ }
92
+
93
+ if (result.kind === "not-found") {
94
+ return reply.code(404).send({ error: "Transcode session file was not found." });
95
+ }
96
+ if (result.kind === "superseded") {
97
+ // The viewer moved while this was being held, and this segment is not the
98
+ // one they moved TO that case is kept and waited out, see
99
+ // `waitForSessionFile`. Answer at once so the player can ask for where it
100
+ // is now; `Retry-After: 0` because there is nothing to wait for.
101
+ //
102
+ // Named in the log, because a refusal that says only "superseded" is what
103
+ // made the 2026-08-18 freeze take a day to explain: the player was refused
104
+ // the segment at its own seek target and nothing recorded which segment or
105
+ // where the viewer was.
106
+ logger.info(
107
+ `[hold] ${fileName} refused: the viewer is at ` +
108
+ `${hlsSessionManager.viewerPositionOf(sessionId).toFixed(1)}s and this is not the segment there`
109
+ );
110
+ reply.header("Retry-After", "0");
111
+ return reply.code(503).send({ error: "Superseded by a seek." });
112
+ }
113
+ if (result.kind === "warming-up") {
114
+ // The segment is still being produced (e.g. just after a seek-restart).
115
+ // Return a retryable 503 never 202, which hls.js cannot consume as a
116
+ // media segment — so the player retries the fetch shortly.
117
+ reply.header("Retry-After", "1");
118
+ // `Retry-After` tells the player to re-request THIS segment after a short
119
+ // pause. Without it a bare 503 reads as "nothing here", and the player goes
120
+ // looking elsewhere: because our synthetic VOD playlist lists every segment
121
+ // of the file, it believes they all exist and SCANS them (field log: one
122
+ // user seek produced probes at #617, #717, #732…). That scan is what used
123
+ // to steer the encoder off the real target. Whether iOS's native player
124
+ // honours the hint is not guaranteed — its behaviour is closed — but this
125
+ // is the standard, correct way to say "wait, don't look elsewhere", and
126
+ // hls.js already retries the same fragment regardless.
127
+ reply.header("Retry-After", "1");
128
+ return reply.code(503).send({ error: "Transcode segment is still being produced." });
129
+ }
130
+ if (result.kind === "failed") {
131
+ return reply.code(500).send({ error: result.message });
132
+ }
133
+
134
+ if (result.isPlaylist) {
135
+ reply.header("Cache-Control", "no-store");
136
+ } else {
137
+ reply.header("Cache-Control", "public, max-age=60");
138
+ }
139
+ reply.header("Content-Type", result.contentType);
140
+ return reply.send(result.stream);
141
+ }
142
+
143
+ /**
144
+ * Poll `hlsSessionManager.getFileStream()` until the file is available,
145
+ * the session fails, or the timeout elapses.
146
+ *
147
+ * @param {import("../../../services/hls-session-manager.js").HlsSessionManager} hlsSessionManager
148
+ * @param {string} sessionId
149
+ * @param {string} fileName
150
+ * @param {number} timeoutMs
151
+ * @returns {Promise<Awaited<ReturnType<import("../../../services/hls-session-manager.js").HlsSessionManager["getFileStream"]>>>}
152
+ */
153
+ export async function waitForSessionFile(hlsSessionManager, sessionId, fileName, timeoutMs) {
154
+ const startedAt = Date.now();
155
+ // One sequence number for THIS request, reused by every poll below, so the
156
+ // session can tell a newly-arrived request apart from an old one polling
157
+ // again see HlsSessionManager#ensureEncodingFor for the encoder ping-pong
158
+ // this prevents when one seek-bar scrub fires several segment requests.
159
+ const requestSeq = hlsSessionManager.nextRequestSeq(sessionId);
160
+ // The viewer's position when this request was made. A seek makes every held
161
+ // request stale — it asks for a segment nobody is going to watch — and hls.js
162
+ // keeps only ONE fragment load outstanding, so holding on blocks the request
163
+ // the player actually needs now. Measured: 57 s of a 58 s backward seek was
164
+ // this wait, and the segment the viewer wanted took 15 ms once it was asked
165
+ // for.
166
+ let seekEpoch = hlsSessionManager.seekEpoch(sessionId);
167
+ while (Date.now() - startedAt < timeoutMs) {
168
+ const result = await hlsSessionManager.getFileStream(sessionId, fileName, { requestSeq });
169
+ if (result.kind !== "warming-up") {
170
+ return result;
171
+ }
172
+ if (hlsSessionManager.seekEpoch(sessionId) !== seekEpoch) {
173
+ // A seek moved the epoch. Whether THIS request is stale depends on which
174
+ // segment it asks for: the one the viewer has just landed on races the
175
+ // seek notification and would otherwise be refused at the exact moment it
176
+ // is needed — measured 2026-08-18, two 503s within 80 ms on the segment
177
+ // at the seek target, after which the player never asked for it again.
178
+ if (!hlsSessionManager.requestStillWanted(sessionId, fileName)) {
179
+ return { kind: "superseded" };
180
+ }
181
+ logger.info(
182
+ `[hold] ${fileName} kept across a seek: it is the segment the viewer now needs`
183
+ );
184
+ seekEpoch = hlsSessionManager.seekEpoch(sessionId);
185
+ }
186
+ await delay(300);
187
+ }
188
+ return { kind: "warming-up" };
189
+ }
190
+
191
+ /**
192
+ * Resolve after a given number of milliseconds.
193
+ *
194
+ * @param {number} ms
195
+ * @returns {Promise<void>}
196
+ */
197
+ function delay(ms) {
198
+ return new Promise((resolve) => {
199
+ setTimeout(resolve, ms);
200
+ });
201
+ }
@@ -2065,7 +2065,7 @@ export class HlsSessionManager {
2065
2065
  // flip the arrangement under a stream that is playing.
2066
2066
  session.audioSeparate = inheritedAudioSeparate === null
2067
2067
  ? audioRenditions === true &&
2068
- this.#variantHeights(session).length >= 2 &&
2068
+ this.#splicableHeights(session).length >= 2 &&
2069
2069
  this.#audioRenditionsOf(session).length > 0
2070
2070
  : inheritedAudioSeparate === true;
2071
2071
 
@@ -5379,6 +5379,42 @@ export class HlsSessionManager {
5379
5379
  return session.variantHeight;
5380
5380
  }
5381
5381
 
5382
+ /**
5383
+ * The heights this file's variants CAN be spliced at — a fact about the
5384
+ * source and the cut grid, settled once and never moved.
5385
+ *
5386
+ * Separate from {@link #variantHeights}, which answers a different question:
5387
+ * which of them are worth OFFERING to the viewer right now, on a machine
5388
+ * whose load moves every five seconds. Both were the same list until
5389
+ * 2026-08-18, and that is what broke playback outright: the browser is told
5390
+ * at session creation that a master playlist exists, and 192 ms later — after
5391
+ * the session's own encoder had started and the first supply reading had
5392
+ * arrived — the live list had fallen from five rungs to one, `buildMaster
5393
+ * Playlist` returned null for having fewer than two, and the master answered
5394
+ * 404 to the very session that had just published it. hls.js treats that as
5395
+ * fatal and unrecoverable, so nothing played at all (session `4ef731d8`,
5396
+ * "Moana (2016).mkv", 17:43:01).
5397
+ *
5398
+ * A live figure may decide what to offer. It may not decide whether a
5399
+ * published document exists.
5400
+ *
5401
+ * @param {HlsSession} session
5402
+ * @returns {number[]} Largest first.
5403
+ */
5404
+ #splicableHeights(session) {
5405
+ const owner = this.#baseOf(session);
5406
+ if (Array.isArray(owner.splicableHeights)) {
5407
+ return owner.splicableHeights;
5408
+ }
5409
+ const heights = new Set(variantHeightsFor(Number(owner.sourceHeight) || 0));
5410
+ const own = this.variantHeightOf(owner);
5411
+ if (own > 0) {
5412
+ heights.add(own);
5413
+ }
5414
+ owner.splicableHeights = [...heights].sort((left, right) => right - left);
5415
+ return owner.splicableHeights;
5416
+ }
5417
+
5382
5418
  /**
5383
5419
  * The heights this session's file is offered at, largest first.
5384
5420
  *
@@ -6540,8 +6576,11 @@ export class HlsSessionManager {
6540
6576
  return null;
6541
6577
  }
6542
6578
  // Only the heights the master offers. Anything else is a made-up request,
6543
- // and honouring it would let a client start encoder runs at will.
6544
- if (!this.#variantHeights(base).includes(height)) {
6579
+ // and honouring it would let a client start encoder runs at will. The
6580
+ // MASTER's list, not the live one: a rung is published for the session's
6581
+ // whole life, and refusing what we published is how a quality switch became
6582
+ // a 404 storm across every level.
6583
+ if (!this.#splicableHeights(base).includes(height)) {
6545
6584
  return null;
6546
6585
  }
6547
6586
  if (height === this.variantHeightOf(base)) {
@@ -6690,7 +6729,7 @@ export class HlsSessionManager {
6690
6729
  if (!isPlaylist && !isInit && !isSegment) {
6691
6730
  return { sessionId: null };
6692
6731
  }
6693
- if (!this.#variantHeights(base).includes(height)) {
6732
+ if (!this.#splicableHeights(base).includes(height)) {
6694
6733
  return { sessionId: null };
6695
6734
  }
6696
6735
  // Answered from the base, and no encoder is started for it. Every variant of
@@ -6945,7 +6984,11 @@ export class HlsSessionManager {
6945
6984
  return null;
6946
6985
  }
6947
6986
  const sourceHeight = Number(session.sourceHeight) || 0;
6948
- const rungs = this.#variantHeights(session);
6987
+ // What CAN be spliced, not what is worth offering this second. The live
6988
+ // judgement travels in `offeredHeights` and in every progress report, which
6989
+ // is what the viewer's menu follows; letting it decide the master's
6990
+ // existence made a live session answer 404 to its own published address.
6991
+ const rungs = this.#splicableHeights(session);
6949
6992
  if (rungs.length < 2) {
6950
6993
  return null;
6951
6994
  }
@@ -7223,6 +7266,59 @@ export class HlsSessionManager {
7223
7266
  return session?.waitEpoch ?? 0;
7224
7267
  }
7225
7268
 
7269
+ /**
7270
+ * Where the viewer of this session is, in seconds.
7271
+ *
7272
+ * Exists so a refusal can name it. A log line that says only "superseded"
7273
+ * cannot be read afterwards: it does not say what was refused or against what
7274
+ * position, which is exactly what the 2026-08-18 investigation lacked.
7275
+ *
7276
+ * @param {string} sessionId
7277
+ * @returns {number} Zero when the session is gone or nothing has been reported.
7278
+ */
7279
+ viewerPositionOf(sessionId) {
7280
+ const session = isSafeSessionId(sessionId) ? this.sessionsById.get(sessionId) : null;
7281
+ const position = Number(session?.viewerPositionSeconds);
7282
+ return Number.isFinite(position) ? position : 0;
7283
+ }
7284
+
7285
+ /**
7286
+ * Whether a held request is for a segment the viewer STILL needs.
7287
+ *
7288
+ * The epoch alone says a seek happened; it cannot say whether this particular
7289
+ * request was made for the position left behind or for the one just arrived
7290
+ * at. That distinction is the whole of the failure measured 2026-08-18: the
7291
+ * viewer seeked to 1061.0 s, the request for `segment-00101` — the segment AT
7292
+ * that position — raced the seek notification, the epoch moved underneath it,
7293
+ * and it was answered 503 twice within 80 ms. The player then hunted at
7294
+ * sn=105-107, never came back to 101, and looped two audio segments 1473
7295
+ * times over 149 s while the picture stood still.
7296
+ *
7297
+ * A request is stale when its segment lies behind where the viewer now is, or
7298
+ * so far ahead that the running encode will not reach it. Anything between is
7299
+ * exactly what the viewer is waiting for, and holding it is the point.
7300
+ *
7301
+ * @param {string} sessionId
7302
+ * @param {string} fileName
7303
+ * @returns {boolean} True when the request should keep waiting.
7304
+ */
7305
+ requestStillWanted(sessionId, fileName) {
7306
+ const session = isSafeSessionId(sessionId) ? this.sessionsById.get(sessionId) : null;
7307
+ if (!session) {
7308
+ return false;
7309
+ }
7310
+ const index = session.segmentFormat?.segmentIndexFromName?.(fileName) ?? -1;
7311
+ if (!(index >= 0)) {
7312
+ return true; // a playlist or an init segment belongs to no position
7313
+ }
7314
+ const position = Number(session.viewerPositionSeconds);
7315
+ if (!Number.isFinite(position)) {
7316
+ return true; // nothing said where the viewer is; refusing would be a guess
7317
+ }
7318
+ const at = this.#segmentIndexForTime(session, position);
7319
+ return index >= at && index <= at + MAX_LOOKAHEAD_SEGMENTS;
7320
+ }
7321
+
7226
7322
  /**
7227
7323
  * Open a read stream for an HLS segment or playlist file from a session.
7228
7324
  *
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @file How many blocking calls this process can have in flight — set before
3
+ * anything makes one.
4
+ *
5
+ * Node resolves host names with `getaddrinfo`, which runs on the libuv thread
6
+ * pool, and that pool holds FOUR threads by default. A torrent announces to
7
+ * every tracker in its file at once — ten or thirteen of them — so four names
8
+ * are resolved and the rest queue; a tracker that no longer exists holds its
9
+ * thread for the resolver's full ten-second timeout, and every announce behind
10
+ * it blows its own fifteen-second deadline.
11
+ *
12
+ * Measured inside the addon container on 2026-08-18, resolving the ten trackers
13
+ * of one film: as a burst on the default pool, two names answered in 30-40 ms
14
+ * and the other seven took **7.58 s**; with a larger pool every live name
15
+ * answered in **27-42 ms**. The film itself had 517 seeders on a tracker that
16
+ * answers in 50 ms, and spent eleven minutes with zero peers, because every
17
+ * announce — UDP and HTTP alike — timed out waiting for a name.
18
+ *
19
+ * Sized to hold several torrents' announce lists at once, since the same pool
20
+ * also serves this process's file reads. Idle threads cost memory and nothing
21
+ * else, and a deployment that states its own size is left alone.
22
+ *
23
+ * Imported FIRST by the entry point, because a module's imports are evaluated
24
+ * before its body: written as a statement in `cli.js` this would run after
25
+ * every other import had already had its chance to create the pool.
26
+ */
27
+
28
+ if (!process.env.UV_THREADPOOL_SIZE) {
29
+ process.env.UV_THREADPOOL_SIZE = "64";
30
+ }
@@ -288,7 +288,7 @@ test("the source's decode figures come off the probe, or not at all", () => {
288
288
  assert.equal(sourceDecodeCharacteristics(null), null);
289
289
  });
290
290
 
291
- test("the master playlist drops the rungs the host cannot hold", async (t) => {
291
+ test("the OFFER drops the rungs the host cannot hold, and the master keeps addressing them", async (t) => {
292
292
  const dirPath = await mkdtemp(path.join(os.tmpdir(), "decode-cost-"));
293
293
  const manager = new HlsSessionManager({
294
294
  enabled: true,
@@ -339,15 +339,22 @@ test("the master playlist drops the rungs the host cannot hold", async (t) => {
339
339
  };
340
340
  manager.sessionsById.set(session.id, session);
341
341
 
342
- assert.equal(
343
- manager.buildMasterPlaylist(session.id),
344
- null,
345
- "every rung under the copy runs below realtime here, so there is nothing to switch to"
346
- );
347
342
  assert.deepEqual(
348
343
  manager.offeredHeights(session),
349
344
  [1080],
350
- "and the list the browser is given says the same, since both come from one answer"
345
+ "every rung under the copy runs below realtime here, so there is nothing to switch to"
346
+ );
347
+ // The master is NOT that answer. It says which rungs can be spliced onto this
348
+ // cut grid, which is a fact about the file, and it has to hold still for the
349
+ // session's life: the browser is handed its address at creation, and a live
350
+ // figure that withdrew it left a session answering 404 to itself (field
351
+ // 2026-08-18, "Moana (2016).mkv" — nothing played at all).
352
+ const weakMaster = manager.buildMasterPlaylist(session.id);
353
+ assert.ok(weakMaster, "published once, whatever the host is managing this second");
354
+ assert.deepEqual(
355
+ [...weakMaster.matchAll(/^v\/(\d+)\/index\.m3u8$/gm)].map((match) => Number(match[1])),
356
+ [1080, 720, 540, 480, 360, 240],
357
+ "the ladder of the source, addressable — the menu the viewer sees is offeredHeights"
351
358
  );
352
359
 
353
360
  // A host with a little more encoder keeps the rungs it can actually hold. A
@@ -355,11 +362,18 @@ test("the master playlist drops the rungs the host cannot hold", async (t) => {
355
362
  manager.softwarePresetBenchmark = [{ preset: "ultrafast", pixelsPerSec: 12e6 }];
356
363
  const stronger = { ...session, id: "dddddddd-eeee-ffff-0000-111111111111", offeredHeightsCache: undefined };
357
364
  manager.sessionsById.set(stronger.id, stronger);
365
+ assert.deepEqual(
366
+ manager.offeredHeights(stronger),
367
+ [1080, 360, 240],
368
+ "nothing is known about this swarm, so the bar is realtime"
369
+ );
358
370
  const master = manager.buildMasterPlaylist(stronger.id);
359
- assert.ok(master, "1080p copied plus the rungs this host can produce");
360
- const heights = [...master.matchAll(/^v\/(\d+)\/index\.m3u8$/gm)].map((match) => Number(match[1]));
361
- assert.deepEqual(heights, [1080, 360, 240], "nothing is known about this swarm, so the bar is realtime");
362
- assert.deepEqual(manager.offeredHeights(stronger), heights);
371
+ assert.ok(master, "1080p copied plus every rung that can be spliced beside it");
372
+ assert.deepEqual(
373
+ [...master.matchAll(/^v\/(\d+)\/index\.m3u8$/gm)].map((match) => Number(match[1])),
374
+ [1080, 720, 540, 480, 360, 240],
375
+ "the same published set as before: what the host manages is the offer's business, not the document's"
376
+ );
363
377
 
364
378
  // The same host, once the reader has measured what this file's supply
365
379
  // demands: waits arriving as they did on the field torrent of 2026-08-17 ask
@@ -813,3 +813,49 @@ test("a quality step being warmed is not refused by its own cost", async (t) =>
813
813
  "offer by the act of warming it — and its next segment 404s on a stream that is playing"
814
814
  );
815
815
  });
816
+
817
+ test("the master survives a live offer that has collapsed to one rung", async (t) => {
818
+ // The field case of 2026-08-18, in the smallest form that reproduces it: a
819
+ // host too slow for any re-encoded rung, and a swarm whose interruptions
820
+ // demand far more than realtime. The live offer then holds only the height an
821
+ // encoder is already producing — and until this test existed, that made
822
+ // `buildMasterPlaylist` answer null and the route answer 404 to a session
823
+ // that had just published the address.
824
+ const dirPath = await mkdtemp(path.join(os.tmpdir(), "quality-variants-collapse-"));
825
+ const manager = new HlsSessionManager({
826
+ enabled: true,
827
+ ffmpegBin: "ffmpeg",
828
+ localBindHost: "127.0.0.1",
829
+ localPort: 9090,
830
+ // A megapixel a second: every rung below the source costs more than the
831
+ // machine has.
832
+ softwarePresetBenchmark: [{ preset: "veryfast", pixelsPerSec: 1_000_000 }],
833
+ decodeCostModel: { pixelTerm: 0.01, bitrateTerm: 0, constantTerm: 0 }
834
+ });
835
+ const base = fakeSession({ id: BASE_ID, encodeHeight: 812, dirPath });
836
+ base.sourceDecode = { megapixelsPerSecond: 50, megabitsPerSecond: 10 };
837
+ // What this file's own reader measured: a step must run at eight times
838
+ // realtime to survive this swarm.
839
+ base.supplyFigures = { requiredSpeed: 8 };
840
+ manager.sessionsById.set(BASE_ID, base);
841
+ t.after(async () => {
842
+ await manager.disposeAll();
843
+ await rm(dirPath, { recursive: true, force: true });
844
+ });
845
+
846
+ assert.deepEqual(
847
+ manager.offeredHeights(base),
848
+ [812],
849
+ "the live judgement is unchanged: nothing but the running height is worth offering"
850
+ );
851
+
852
+ const master = manager.buildMasterPlaylist(BASE_ID);
853
+
854
+ assert.ok(master, "the master is a published document, not a live figure");
855
+ const heights = [...master.matchAll(/^v\/(\d+)\/index\.m3u8$/gm)].map((match) => Number(match[1]));
856
+ assert.deepEqual(
857
+ heights,
858
+ [1080, 812, 720, 540, 480, 360, 240],
859
+ "every rung that can be spliced onto this cut grid stays addressable"
860
+ );
861
+ });
@@ -0,0 +1,106 @@
1
+ /**
2
+ * @file The segment the viewer just seeked TO must not be refused as stale.
3
+ *
4
+ * A seek bumps the wait epoch so that requests made for the position being left
5
+ * stop being held. The request for the position being ARRIVED at races that
6
+ * bump: hls.js asks for the new segment within milliseconds, and if the epoch
7
+ * moves underneath it, the epoch check alone cannot tell the two apart.
8
+ *
9
+ * Measured 2026-08-18: the viewer seeked to 1061.0 s, `segment-00101` — the
10
+ * segment at that very position — was answered 503 twice within 80 ms, the
11
+ * player never asked for it again, and instead looped `a/0/segment-00103` and
12
+ * `a/0/segment-00104` 737 and 736 times over 149 seconds while the picture
13
+ * stood at `t=1061.0s readyState=1`. The session ended without another frame.
14
+ */
15
+
16
+ import test from "node:test";
17
+ import assert from "node:assert/strict";
18
+ import { mkdtemp, rm } from "node:fs/promises";
19
+ import os from "node:os";
20
+ import path from "node:path";
21
+ import { HlsSessionManager } from "../services/hls-session-manager.js";
22
+ import { fmp4Format } from "../services/segment-formats/fmp4.js";
23
+
24
+ const SESSION_ID = "aaaaaaaa-1111-2222-3333-444444444444";
25
+ const SEGMENT_SECONDS = 10.4;
26
+ const SEEK_TO_SECONDS = 1061;
27
+ const SEGMENT_AT_SEEK = Math.floor(SEEK_TO_SECONDS / SEGMENT_SECONDS);
28
+
29
+ /**
30
+ * A session whose viewer has just landed at {@link SEEK_TO_SECONDS}.
31
+ *
32
+ * @returns {Promise<{ manager: HlsSessionManager, dirPath: string }>}
33
+ */
34
+ async function managerAfterSeek() {
35
+ const dirPath = await mkdtemp(path.join(os.tmpdir(), "seek-target-"));
36
+ const manager = new HlsSessionManager({
37
+ enabled: true,
38
+ ffmpegBin: "ffmpeg",
39
+ localBindHost: "127.0.0.1",
40
+ localPort: 9090
41
+ });
42
+ manager.sessionsById.set(SESSION_ID, {
43
+ id: SESSION_ID,
44
+ dirPath,
45
+ state: "ready",
46
+ fileName: "film.mkv",
47
+ startedAt: Date.now(),
48
+ lastAccessedAt: Date.now(),
49
+ consumers: new Set(),
50
+ segmentFormat: fmp4Format,
51
+ transcodeVideo: false,
52
+ useSyntheticPlaylist: true,
53
+ segmentBoundaries: Array.from({ length: 400 }, (_, index) => index * SEGMENT_SECONDS),
54
+ segmentCount: 399,
55
+ encodeStartIndex: SEGMENT_AT_SEEK,
56
+ waitEpoch: 1,
57
+ viewerPositionSeconds: SEEK_TO_SECONDS,
58
+ runState: "PRODUCING"
59
+ });
60
+ return { manager, dirPath };
61
+ }
62
+
63
+ test("the segment at the seek target is still wanted", async () => {
64
+ const { manager, dirPath } = await managerAfterSeek();
65
+
66
+ assert.equal(
67
+ manager.requestStillWanted(SESSION_ID, `segment-${String(SEGMENT_AT_SEEK).padStart(5, "0")}.mp4`),
68
+ true,
69
+ "this is the segment the viewer is waiting for; refusing it is what froze the field session"
70
+ );
71
+
72
+ await rm(dirPath, { recursive: true, force: true });
73
+ });
74
+
75
+ test("a segment behind the viewer is not wanted any more", async () => {
76
+ const { manager, dirPath } = await managerAfterSeek();
77
+
78
+ assert.equal(
79
+ manager.requestStillWanted(SESSION_ID, `segment-${String(SEGMENT_AT_SEEK - 3).padStart(5, "0")}.mp4`),
80
+ false,
81
+ "a request for the position the viewer left is exactly what the epoch exists to release"
82
+ );
83
+
84
+ await rm(dirPath, { recursive: true, force: true });
85
+ });
86
+
87
+ test("a segment far beyond what the run will reach is not wanted", async () => {
88
+ const { manager, dirPath } = await managerAfterSeek();
89
+
90
+ assert.equal(
91
+ manager.requestStillWanted(SESSION_ID, `segment-${String(SEGMENT_AT_SEEK + 200).padStart(5, "0")}.mp4`),
92
+ false,
93
+ "nothing will produce it before the viewer moves again"
94
+ );
95
+
96
+ await rm(dirPath, { recursive: true, force: true });
97
+ });
98
+
99
+ test("a playlist belongs to no position and is never stale", async () => {
100
+ const { manager, dirPath } = await managerAfterSeek();
101
+
102
+ assert.equal(manager.requestStillWanted(SESSION_ID, "index.m3u8"), true);
103
+ assert.equal(manager.requestStillWanted(SESSION_ID, "init.mp4"), true);
104
+
105
+ await rm(dirPath, { recursive: true, force: true });
106
+ });
@@ -51,6 +51,11 @@ test("a seek releases a held segment request instead of running out the hold", a
51
51
  const hlsSessionManager = {
52
52
  nextRequestSeq: () => 1,
53
53
  seekEpoch: () => epoch,
54
+ // The viewer seeked AWAY from this segment, so it is genuinely stale. A
55
+ // request for the segment they seeked TO is kept instead — see
56
+ // `test/seek-target-not-superseded.test.js`.
57
+ requestStillWanted: () => false,
58
+ viewerPositionOf: () => 0,
54
59
  async getFileStream() {
55
60
  polls += 1;
56
61
  // The viewer moves while this request is being held.
@@ -0,0 +1,53 @@
1
+ /**
2
+ * @file The blocking-call pool is stated before anything can create it.
3
+ *
4
+ * Field 2026-08-18: a film with 517 seeders spent eleven minutes with zero
5
+ * peers on the addon host. Every announce timed out — UDP and HTTP alike — and
6
+ * the cause was not the network: resolving that torrent's ten tracker names as
7
+ * a burst took 7.58 s on the default four-thread pool, against 27-42 ms each
8
+ * when the pool was larger. A dead tracker holds a thread for the resolver's
9
+ * whole timeout, and every announce queued behind it misses its own deadline.
10
+ */
11
+
12
+ import test from "node:test";
13
+ import assert from "node:assert/strict";
14
+ import { readFile } from "node:fs/promises";
15
+ import path from "node:path";
16
+ import { fileURLToPath } from "node:url";
17
+
18
+ const here = path.dirname(fileURLToPath(import.meta.url));
19
+
20
+ test("the pool is set, and left alone when the deployment states its own", async () => {
21
+ const previous = process.env.UV_THREADPOOL_SIZE;
22
+ try {
23
+ process.env.UV_THREADPOOL_SIZE = "";
24
+ await import(`../services/thread-pool.js?first=${Date.now()}`);
25
+ assert.equal(
26
+ Number(process.env.UV_THREADPOOL_SIZE) >= 16,
27
+ true,
28
+ "enough threads for a torrent's whole announce list to resolve at once"
29
+ );
30
+
31
+ process.env.UV_THREADPOOL_SIZE = "8";
32
+ await import(`../services/thread-pool.js?stated=${Date.now()}`);
33
+ assert.equal(process.env.UV_THREADPOOL_SIZE, "8", "a stated size is the deployment's to choose");
34
+ } finally {
35
+ if (previous === undefined) {
36
+ delete process.env.UV_THREADPOOL_SIZE;
37
+ } else {
38
+ process.env.UV_THREADPOOL_SIZE = previous;
39
+ }
40
+ }
41
+ });
42
+
43
+ test("the entry point imports it before anything that could create the pool", async () => {
44
+ const cli = await readFile(path.join(here, "..", "bin", "cli.js"), "utf8");
45
+ const imports = [...cli.matchAll(/^import\s.*?from\s+["'](.+?)["'];|^import\s+["'](.+?)["'];/gm)]
46
+ .map((match) => match[1] ?? match[2]);
47
+
48
+ assert.equal(
49
+ imports[0],
50
+ "../services/thread-pool.js",
51
+ "a module's imports run before its body, so this cannot be a statement in cli.js"
52
+ );
53
+ });
package/utils/logger.js CHANGED
@@ -1,13 +1,43 @@
1
1
  /**
2
- * @file Centralised console logger for the proxy process.
2
+ * @file Centralised logger for the proxy process — to the console always, and
3
+ * to a file when one is named.
3
4
  *
4
- * All messages are prefixed with `[proxy-client]` and coloured with chalk
5
- * for consistent, readable terminal output.
5
+ * All messages are prefixed with `[proxy-client]` and coloured with chalk for
6
+ * consistent, readable terminal output.
7
+ *
8
+ * **Why a file at all.** The console is the container's stdout, and the
9
+ * container does not survive what we most need to read about. On 2026-08-18 the
10
+ * proxy died thirteen times with SIGSEGV; each death had Home Assistant's
11
+ * watchdog RECREATE the container, and every line leading up to the crash went
12
+ * with it. The same day a deploy of ours destroyed the evidence for two field
13
+ * reports that were being investigated at the time. A log that disappears
14
+ * exactly when something goes wrong is not a log, and no amount of care in
15
+ * choosing what to print compensates for it.
16
+ *
17
+ * The file is opt-in and named by the caller (`--log-file`), so nothing here
18
+ * assumes Home Assistant or any other host: the addon points it at `/data`,
19
+ * which survives restarts and updates, and a bare npm or Docker run may point
20
+ * it anywhere or leave it off.
6
21
  */
7
22
 
23
+ import { createWriteStream, renameSync, statSync } from "node:fs";
8
24
  import chalk from "chalk";
9
25
 
10
26
  const PREFIX = "[proxy-client]";
27
+ /**
28
+ * When the file is rotated, and how many turns are kept.
29
+ *
30
+ * One turn holds a few hours of a busy session at the current rate; two turns
31
+ * therefore cover a night's worth of restarts, which is the span a morning
32
+ * report asks about. Bounded because the addon's `/data` is the owner's disk.
33
+ */
34
+ const MAX_FILE_BYTES = 32 * 1024 * 1024;
35
+
36
+ /** @type {import("node:fs").WriteStream | null} */
37
+ let fileStream = null;
38
+ /** @type {string} */
39
+ let filePath = "";
40
+ let writtenBytes = 0;
11
41
 
12
42
  /**
13
43
  * Return the current time as a compact ISO-8601 (UTC) string, e.g.
@@ -20,6 +50,61 @@ function ts() {
20
50
  return new Date().toISOString().slice(11, 23); // "HH:MM:SS.mmm" (UTC)
21
51
  }
22
52
 
53
+ /**
54
+ * Start writing every message to a file as well as the console.
55
+ *
56
+ * Appends: a restart must not erase what led up to it, which is the entire
57
+ * reason this exists. Failures are reported once and then ignored — a proxy
58
+ * that cannot write its log still has a viewer to serve.
59
+ *
60
+ * @param {string} pathToFile - Empty or absent leaves logging console-only.
61
+ * @returns {void}
62
+ */
63
+ export function logToFile(pathToFile) {
64
+ if (typeof pathToFile !== "string" || pathToFile.length === 0) {
65
+ return;
66
+ }
67
+ try {
68
+ filePath = pathToFile;
69
+ writtenBytes = statSync(pathToFile, { throwIfNoEntry: false })?.size ?? 0;
70
+ fileStream = createWriteStream(pathToFile, { flags: "a" });
71
+ fileStream.on("error", (error) => {
72
+ fileStream = null;
73
+ console.warn(chalk.yellow(`${PREFIX} [${ts()}] log file ${pathToFile} stopped: ${error?.message}`));
74
+ });
75
+ console.log(chalk.cyan(`${PREFIX} [${ts()}] logging to ${pathToFile} as well as the console`));
76
+ } catch (error) {
77
+ fileStream = null;
78
+ console.warn(chalk.yellow(`${PREFIX} [${ts()}] cannot log to ${pathToFile}: ${error?.message}`));
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Write one line to the file, rotating when it has grown past the cap.
84
+ *
85
+ * @param {string} line
86
+ * @returns {void}
87
+ */
88
+ function toFile(line) {
89
+ if (!fileStream) {
90
+ return;
91
+ }
92
+ const text = `${line}\n`;
93
+ writtenBytes += Buffer.byteLength(text);
94
+ if (writtenBytes > MAX_FILE_BYTES) {
95
+ try {
96
+ fileStream.end();
97
+ renameSync(filePath, `${filePath}.1`);
98
+ fileStream = createWriteStream(filePath, { flags: "a" });
99
+ writtenBytes = Buffer.byteLength(text);
100
+ } catch {
101
+ // Rotation failed; keep writing to whatever handle still works rather
102
+ // than losing the line that prompted it.
103
+ }
104
+ }
105
+ fileStream.write(text);
106
+ }
107
+
23
108
  /**
24
109
  * @typedef {Object} ProxyLogger
25
110
  * @property {(message: string) => void} info - Informational message (cyan).
@@ -34,8 +119,24 @@ function ts() {
34
119
  * @type {ProxyLogger}
35
120
  */
36
121
  export const logger = {
37
- info: (message) => console.log(chalk.cyan(`${PREFIX} [${ts()}] ${message}`)),
38
- success: (message) => console.log(chalk.green(`${PREFIX} [${ts()}] ${message}`)),
39
- warn: (message) => console.warn(chalk.yellow(`${PREFIX} [${ts()}] ${message}`)),
40
- error: (message) => console.error(chalk.red(`${PREFIX} [${ts()}] ${message}`)),
122
+ info: (message) => {
123
+ const line = `${PREFIX} [${ts()}] ${message}`;
124
+ console.log(chalk.cyan(line));
125
+ toFile(line);
126
+ },
127
+ success: (message) => {
128
+ const line = `${PREFIX} [${ts()}] ${message}`;
129
+ console.log(chalk.green(line));
130
+ toFile(line);
131
+ },
132
+ warn: (message) => {
133
+ const line = `${PREFIX} [${ts()}] ${message}`;
134
+ console.warn(chalk.yellow(line));
135
+ toFile(line);
136
+ },
137
+ error: (message) => {
138
+ const line = `${PREFIX} [${ts()}] ${message}`;
139
+ console.error(chalk.red(line));
140
+ toFile(line);
141
+ }
41
142
  };