@torrent-tv/proxy 2.89.12 → 2.89.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 2.89.14
2
+
3
+ - **Fix**: A failed run counts only the source's own streams. The planner probes with an output, so ffmpeg's banner goes on to list the streams it maps and writes, and those were counted too: a file with one picture, one soundtrack, one subtitle and one font read as `2 video, 2 audio, 1 subtitle, 3 other` in the field — enough to make a second soundtrack look present when it is not.
4
+
5
+ ## 2.89.13
6
+
7
+ - **Fix**: The proxy no longer stops answering everybody when one output is priced near zero. Asking how many encoders an output may run counted them one at a time until the machine was full, so the number of steps grew as the price shrank. On the stand on 2026-10-04 a soundtrack priced at 5.4e-14 s of machine per second of film held the main thread in that count for minutes, past 3.3 billion steps: no data channel was served, `/healthz` did not answer, and every viewer stopped. The count is now found by doubling and halving, in at most a few hundred steps whatever the price. The price itself is wrong and is not corrected here.
8
+
1
9
  ## 2.89.12
2
10
 
3
11
  - **Fix**: A connection that is gone is no longer probed. The delivery probe stopped only when a data channel reported `onClosed`, and a peer connection closed moments after its channels opened reports nothing: on the stand on 2026-10-04 a trial connection the page closed 70 ms after opening was still sent probes twice a second hours later, one timer left behind per such connection. When the transport watcher finds the connection gone, its probes now end with it.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@torrent-tv/proxy",
3
- "version": "2.89.12",
3
+ "version": "2.89.14",
4
4
  "description": "Torrent proxy client that exposes webseed-like HTTP stream endpoint.",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "repository": {
@@ -356,17 +356,37 @@ export class EncodeAdmission {
356
356
  return { runs: Number.POSITIVE_INFINITY, because: "the output is not priced" };
357
357
  }
358
358
  const others = this.#occupied(address);
359
+ const fits = (count) => this.#speedAt(others.costSec + EncodeAdmission.#added(load, others.files, count)) >= 1;
359
360
  let runs = 0;
360
- for (;;) {
361
- const next = runs + 1;
362
- if (this.#speedAt(others.costSec + EncodeAdmission.#added(load, others.files, next)) < 1) {
363
- break;
364
- }
365
- runs = next;
361
+ if (fits(1)) {
366
362
  if (!(load.costSec > 0)) {
367
363
  // Free to run: the per-output limit is the only bound.
368
364
  return { runs: Number.POSITIVE_INFINITY, because: "it costs nothing measurable" };
369
365
  }
366
+ // The largest count that still fits, found by doubling and halving: the
367
+ // speed only falls as encoders are added. Counting up one at a time took
368
+ // as many steps as the price is small — field 2026-10-04, a soundtrack
369
+ // priced at 5.4e-14 s per film second kept the main thread in this loop
370
+ // for minutes, past 3.3 billion steps, and every viewer of the proxy
371
+ // stopped receiving.
372
+ let low = 1;
373
+ let high = 2;
374
+ while (fits(high)) {
375
+ low = high;
376
+ high *= 2;
377
+ }
378
+ for (;;) {
379
+ const middle = Math.floor((low + high) / 2);
380
+ if (middle === low || middle === high) {
381
+ break;
382
+ }
383
+ if (fits(middle)) {
384
+ low = middle;
385
+ } else {
386
+ high = middle;
387
+ }
388
+ }
389
+ runs = low;
370
390
  }
371
391
  if (runs >= floor) {
372
392
  return { runs, because: "the machine" };
@@ -141,7 +141,13 @@ export function parseFfmpegStreamCounts(stderrText) {
141
141
  if (typeof stderrText !== "string" || stderrText.length === 0) {
142
142
  return null;
143
143
  }
144
- const lines = stderrText.match(/^\s*Stream #\d+:\d+.*$/gim);
144
+ // The source's streams only. A probe that writes somewhere — the planner's
145
+ // `-f null -` does — goes on to print `Stream mapping:` and `Output #0` with
146
+ // stream lines of their own, and counting those doubled the picture and the
147
+ // sound (field 2026-10-04: one soundtrack read as two).
148
+ const end = stderrText.search(/^(?:Stream mapping:|Output #\d+)/m);
149
+ const inputText = end >= 0 ? stderrText.slice(0, end) : stderrText;
150
+ const lines = inputText.match(/^\s*Stream #\d+:\d+.*$/gim);
145
151
  if (!lines || lines.length === 0) {
146
152
  return null;
147
153
  }
@@ -144,3 +144,27 @@ test("unpriced occupied work is neither spare pool headroom nor capacity for ano
144
144
  assert.equal(answer.admitted, false);
145
145
  assert.match(answer.reason, /capacity is unknown/);
146
146
  });
147
+
148
+ test("a near-free output is answered at once, whatever its price", () => {
149
+ // Field 2026-10-04: a soundtrack priced at 5.4e-14 s per film second kept
150
+ // the main thread counting encoders one at a time for minutes.
151
+ const { admission } = machine({ costs: { sound: 5.36680075607109e-14 }, share: 0.6 });
152
+ const started = Date.now();
153
+ const { runs } = admission.placesFor("sound", 0);
154
+ assert.ok(Date.now() - started < 1_000, "answered without counting one at a time");
155
+ assert.ok(runs > 1e12 && Number.isFinite(runs), `as many as fit: ${runs}`);
156
+ });
157
+
158
+ test("the count is the largest that keeps every output at realtime", () => {
159
+ for (const cost of [0.5, 0.1, 0.03, 0.007, 0.0011]) {
160
+ for (const share of [null, 1, 0.37]) {
161
+ const { admission } = machine({ costs: { a: cost, b: PICTURE_1080 / 4 }, running: { b: 1 }, share });
162
+ const usable = share ?? 1;
163
+ let expected = 0;
164
+ while (usable / (PICTURE_1080 / 4 + (expected + 1) * cost) >= 1) {
165
+ expected += 1;
166
+ }
167
+ assert.equal(admission.placesFor("a", 0).runs, Math.max(expected, 0), `cost ${cost}, share ${share}`);
168
+ }
169
+ }
170
+ });
@@ -76,3 +76,26 @@ test("a stream carrying metadata in brackets is still read", () => {
76
76
  `);
77
77
  assert.deepEqual(counts, { video: 1, audio: 1, subtitle: 0, other: 0 });
78
78
  });
79
+
80
+ test("only the input's streams are counted, not the output ffmpeg describes after them", () => {
81
+ // The planner probes with `-i <file> -t 0.1 -f null -`, so the banner goes on
82
+ // to describe what it maps and the null output it writes. Counted whole, this
83
+ // field file (one picture, one soundtrack, one subtitle, one font) read as
84
+ // "2 video, 2 audio, 1 subtitle, 3 other" on 2026-10-04 — which would make a
85
+ // second soundtrack look present when it is not.
86
+ const banner = `
87
+ Input #0, matroska,webm, from 'http://127.0.0.1:9090/stream?sourceKey=torrent%3A84acab5f&fileIndex=35':
88
+ Duration: 00:23:41.44, start: 0.000000, bitrate: 3195 kb/s
89
+ Stream #0:0: Video: h264 (High), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], 23.81 fps, 23.81 tbr, 1k tbn (default)
90
+ Stream #0:1(jpn): Audio: aac (LC), 44100 Hz, stereo, fltp (default)
91
+ Stream #0:2(eng): Subtitle: ass (ssa) (default)
92
+ Stream #0:3: Attachment: ttf
93
+ Stream mapping:
94
+ Stream #0:0 -> #0:0 (h264 (native) -> wrapped_avframe (native))
95
+ Stream #0:1 -> #0:1 (aac (native) -> pcm_s16le (native))
96
+ Output #0, null, to 'pipe:':
97
+ Stream #0:0: Video: wrapped_avframe, yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 23.81 fps, 23.81 tbn (default)
98
+ Stream #0:1(jpn): Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s (default)
99
+ `;
100
+ assert.deepEqual(parseFfmpegStreamCounts(banner), { video: 1, audio: 1, subtitle: 1, other: 1 });
101
+ });