@torrent-tv/proxy 2.67.0 → 2.68.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,8 @@
1
+ ## 2.68.0
2
+
3
+ - **Fix**: The background fetch of a soundtrack now stands aside after a stall, not only during one. Pausing only while a reader is blocked is not enough on a swarm with no surplus: between stalls the fetch still takes bandwidth the picture needs, and the stalls themselves are the proof there was none to spare. Field 2026-08-31 — the swarm delivered 200-600 KB/s against the 399 KB/s the film eats, one piece waited 101 s after a seek, and the picture stood still 145.6 s before the player gave up. A chunk is fetched only when the stall count has not moved since the previous one, re-baselined after each quiet pause so it can resume; on a starving swarm it stops altogether, which is the right answer.
4
+ - **Chore**: `piece-reader.js` counts the stalls a torrent's readers have had, so work of lower importance can ask "did the viewer stall while I was busy?" — a stricter and more useful question than "is the viewer stalled right now".
5
+
1
6
  ## 2.67.0
2
7
 
3
8
  - **New**: A soundtrack or subtitle file shipped beside the picture is fetched WHOLE as soon as the viewer opens the episode, using only the room their own reading leaves. The ordering is the design: what plays now comes first — the picture at the playhead, the soundtrack being heard, the subtitles being shown — the other tracks next, and reading the film far ahead last. The middle tier stays below the first by a condition that is measured rather than chosen: it fetches only while NO reader on the torrent is inside a wait, re-asked before every piece, because a torrent that was healthy a moment ago is not evidence about the next second. A soundtrack is about a twentieth of the picture, and having it on disk is what turns a later switch into a local read: in the field on 2026-08-31 the first piece of one took 27.7 s to arrive, which is longer than a switch is willing to wait.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@torrent-tv/proxy",
3
- "version": "2.67.0",
3
+ "version": "2.68.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": {
@@ -28,7 +28,7 @@
28
28
  */
29
29
 
30
30
  import { logger } from "../../utils/logger.js";
31
- import { readersAreBlockedOn } from "./piece-reader.js";
31
+ import { readersAreBlockedOn, stallsSeenOn } from "./piece-reader.js";
32
32
 
33
33
  /**
34
34
  * How long to stand aside after finding the viewer's own reading blocked.
@@ -101,7 +101,7 @@ function readRange(file, start, end) {
101
101
  * @param {object} torrent
102
102
  * @param {number} fileIndex
103
103
  * @param {string} sourceKey
104
- * @param {{ chunkBytes?: number, isBlocked?: (infoHash: string) => boolean }} [options]
104
+ * @param {{ chunkBytes?: number, isBlocked?: (infoHash: string) => boolean, stallsSeen?: (infoHash: string) => number }} [options]
105
105
  * `isBlocked` answers "is the viewer's own reading starving right now"; it is
106
106
  * the tier boundary, and it is a parameter so it can be exercised without a
107
107
  * swarm.
@@ -123,7 +123,8 @@ export function fillFileInBackground(torrent, fileIndex, sourceKey, options = {}
123
123
  ? options.chunkBytes
124
124
  : (Number(torrent?.pieceLength) || 4 * 1024 * 1024);
125
125
  const isBlocked = typeof options.isBlocked === "function" ? options.isBlocked : readersAreBlockedOn;
126
- const work = fill(torrent, file, fileIndex, chunkBytes, isBlocked).finally(() => {
126
+ const stallsSeen = typeof options.stallsSeen === "function" ? options.stallsSeen : stallsSeenOn;
127
+ const work = fill(torrent, file, fileIndex, chunkBytes, isBlocked, stallsSeen).finally(() => {
127
128
  running.delete(key);
128
129
  });
129
130
  running.set(key, work);
@@ -136,25 +137,41 @@ export function fillFileInBackground(torrent, fileIndex, sourceKey, options = {}
136
137
  * @param {number} fileIndex
137
138
  * @param {number} chunkBytes
138
139
  * @param {(infoHash: string) => boolean} isBlocked
140
+ * @param {(infoHash: string) => number} stallsSeen
139
141
  * @returns {Promise<void>}
140
142
  */
141
- async function fill(torrent, file, fileIndex, chunkBytes, isBlocked) {
143
+ async function fill(torrent, file, fileIndex, chunkBytes, isBlocked, stallsSeen) {
142
144
  const startedAt = Date.now();
143
145
  const infoHash = String(torrent?.infoHash ?? "");
144
146
  let read = 0;
145
147
  let stoodAsideMs = 0;
148
+ // The stall count this fill last saw. A chunk is fetched only when it has not
149
+ // moved since the previous one.
150
+ let quietSince = stallsSeen(infoHash);
146
151
  logger.info(
147
152
  `background-fill: "${String(file.name).slice(0, 40)}" (${(file.length / 1e6).toFixed(1)}MB) will be ` +
148
153
  "fetched whole while the viewer's own reading leaves room"
149
154
  );
150
155
  for (let start = 0; start < file.length; start += chunkBytes) {
151
- // Stand aside for as long as anything the viewer is watching is waiting on
152
- // the swarm. This is the tier boundary, and it is checked before every
153
- // chunk rather than once at the beginning: a torrent that was healthy a
154
- // moment ago is not evidence about the next second.
155
- while (isBlocked(infoHash)) {
156
+ // Stand aside while anything the viewer is watching is waiting on the
157
+ // swarm AND for as long after it as it takes for a quiet stretch to
158
+ // pass. Pausing only DURING a stall is not enough: on a swarm delivering
159
+ // exactly what the film needs, this still takes bandwidth between stalls,
160
+ // and the stalls themselves are the proof there was none to spare. Field
161
+ // 2026-08-31, the case that forced this: 200-600 KB/s delivered against
162
+ // the 399 KB/s the film eats, one piece waited 101 s, and the picture
163
+ // stood still 145.6 s.
164
+ //
165
+ // "A quiet stretch" is measured, not chosen: the stall counter must not
166
+ // have moved while the previous chunk was being fetched. On a starving
167
+ // swarm it moves constantly and this stops altogether, which is the right
168
+ // answer — there is no spare room to use.
169
+ while (isBlocked(infoHash) || stallsSeen(infoHash) !== quietSince) {
156
170
  stoodAsideMs += STAND_ASIDE_MS;
157
171
  await pause(STAND_ASIDE_MS);
172
+ // Re-baselined after the pause, so a stretch that passes without a new
173
+ // stall lets the fill go on. Without this it could never resume.
174
+ quietSince = stallsSeen(infoHash);
158
175
  }
159
176
  // The torrent may have been destroyed under us — a viewer who left, the
160
177
  // disk sweep, a restart. Reading a destroyed file throws, and there is
@@ -162,6 +179,11 @@ async function fill(torrent, file, fileIndex, chunkBytes, isBlocked) {
162
179
  if (!torrent?.files?.[fileIndex]) {
163
180
  return;
164
181
  }
182
+ // Taken BEFORE the read, and deliberately not refreshed after it: a stall
183
+ // that happens while this chunk is in flight must still be visible to the
184
+ // next iteration. Refreshing afterwards erased exactly that evidence, which
185
+ // is the defect a test caught here.
186
+ quietSince = stallsSeen(infoHash);
165
187
  const bytes = await readRange(file, start, Math.min(start + chunkBytes, file.length) - 1);
166
188
  if (bytes === 0) {
167
189
  logger.info(
@@ -617,6 +617,14 @@ function describeSteering(key) {
617
617
  */
618
618
  const blockedReaders = new Map();
619
619
 
620
+ /**
621
+ * How many stalls a torrent's readers have had, ever. Only differences between
622
+ * two readings of it mean anything.
623
+ *
624
+ * @type {Map<string, number>}
625
+ */
626
+ const stallsSeen = new Map();
627
+
620
628
  /**
621
629
  * Whether any reader on this torrent is waiting for a piece right now.
622
630
  *
@@ -627,6 +635,25 @@ export function readersAreBlockedOn(infoHash) {
627
635
  return (blockedReaders.get(infoHash) ?? 0) > 0;
628
636
  }
629
637
 
638
+ /**
639
+ * How many times a reader on this torrent has been blocked since the process
640
+ * started.
641
+ *
642
+ * Exists so that work of lower importance can ask "did the viewer stall while I
643
+ * was busy?" — which is a different and stricter question than "is the viewer
644
+ * stalled right now". On a swarm delivering exactly what the film needs, a
645
+ * background fetch that only pauses DURING a stall still takes bandwidth
646
+ * between them, and the stalls are the proof it had none to spare. Field
647
+ * 2026-08-31: the swarm delivered 200-600 KB/s against the 399 KB/s the film
648
+ * needs, and the picture stood still 145.6 s.
649
+ *
650
+ * @param {string} infoHash
651
+ * @returns {number}
652
+ */
653
+ export function stallsSeenOn(infoHash) {
654
+ return stallsSeen.get(infoHash) ?? 0;
655
+ }
656
+
630
657
  /**
631
658
  * @param {string} infoHash
632
659
  * @param {number} delta
@@ -636,6 +663,9 @@ function countBlockedReader(infoHash, delta) {
636
663
  if (!infoHash) {
637
664
  return;
638
665
  }
666
+ if (delta > 0) {
667
+ stallsSeen.set(infoHash, (stallsSeen.get(infoHash) ?? 0) + 1);
668
+ }
639
669
  const next = (blockedReaders.get(infoHash) ?? 0) + delta;
640
670
  if (next > 0) {
641
671
  blockedReaders.set(infoHash, next);
@@ -154,3 +154,37 @@ test("the gate is re-asked before every chunk, not once at the start", async ()
154
154
  await waitFor(() => !fillIsRunning("source-f", 0));
155
155
  assert.equal(file.reads.length, 3);
156
156
  });
157
+
158
+ test("a stall during a chunk makes the next one wait, not just a stall in progress", async () => {
159
+ // The case this exists for: a swarm delivering exactly what the film needs.
160
+ // Nothing is blocked at the moment the gate is asked, but the viewer stalled
161
+ // while the last chunk was in flight — which is the proof there was no room.
162
+ const file = fakeFile({ length: 12 });
163
+ const torrent = { infoHash: "ggg", pieceLength: 4, files: [file] };
164
+ let stalls = 0;
165
+ let starving = true;
166
+ // A stall happens DURING every chunk, exactly as it does on a swarm with no
167
+ // surplus — and nothing is blocked at the moment the gate is asked.
168
+ const originalRead = file.createReadStream.bind(file);
169
+ file.createReadStream = (range) => {
170
+ if (starving) {
171
+ stalls += 1;
172
+ }
173
+ return originalRead(range);
174
+ };
175
+
176
+ fillFileInBackground(torrent, 0, "source-g", {
177
+ chunkBytes: 4,
178
+ isBlocked: () => false,
179
+ stallsSeen: () => stalls
180
+ });
181
+
182
+ await waitFor(() => file.reads.length === 1);
183
+ await new Promise((resolve) => setTimeout(resolve, 300));
184
+ assert.equal(file.reads.length, 1, "a stall during the chunk holds the next one back");
185
+
186
+ // The swarm settles: no further stalls, so it may go on.
187
+ starving = false;
188
+ await waitFor(() => !fillIsRunning("source-g", 0), 8000);
189
+ assert.equal(file.reads.length, 3);
190
+ });