@torrent-tv/proxy 2.83.7 → 2.84.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 +16 -0
- package/CLAUDE.md +5 -3
- package/bin/cli.js +1 -0
- package/docs/encode-architecture.md +26 -1
- package/docs/logs.md +97 -51
- package/package.json +1 -1
- package/routes/api/client-logs/post.js +86 -0
- package/routes/api/transcode-sessions/post.js +139 -137
- package/server.js +13 -1
- package/services/data-channel-handler.js +7 -0
- package/services/hls-session-manager.js +9777 -10006
- package/services/output/OutputSpec.js +202 -195
- package/services/priority/PriorityMap.js +390 -362
- package/services/priority/PriorityOrchestrator.js +316 -318
- package/services/viewer/Viewer.js +60 -33
- package/services/viewer/positions.js +120 -48
- package/test/client-log-file.test.js +110 -0
- package/test/held-request-width.test.js +8 -5
- package/test/orchestrator-wired.test.js +14 -8
- package/test/output-spec.test.js +157 -157
- package/test/plan-is-steady.test.js +223 -0
- package/test/priority-map-emptied.test.js +207 -207
- package/test/priority-map-per-output.test.js +209 -211
- package/test/quality-variants.test.js +1167 -1222
- package/test/seek-target-not-superseded.test.js +14 -12
- package/test/stale-request-after-seek.test.js +2 -3
- package/test/viewer.test.js +115 -108
- package/utils/client-log-file.js +151 -0
- package/test/viewer-position-on-open.test.js +0 -92
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
## 2.84.0
|
|
2
|
+
|
|
3
|
+
- **Fix**: WHERE A VIEWER IS HAS ONE WRITER AND IS A FUNCTION OF TIME. It had two: a seek wrote `position.seeked`, a segment request overwrote `position.seconds`, and the reading preferred whichever was set — so the two took turns and the priority map jumped backwards and forwards several times a second. Field 2026-09-13, one output over one second: `p100:#9..#10`, `p100:#18..#20`, `p100:#16..#18`, `p100:#18..#20`, with the second viewer stopped dead at 1673.6 s throughout. Encoder placement follows the map, as it must, so the machine spent six minutes on **77 starts and 141 stops against one normal end**, produced almost nothing, and both browsers sat frozen with empty buffers while the swarm delivered 3 MB/s from 129 peers. A segment request now says only that the viewer is still here; where they are is what they state, and between statements it is carried forward by the clock at the rate the film moves — `position(now) = stated + (playing ? 1 : 0) × (now − statedAt)`. Continuous and monotone by construction, so nothing derived from it can dither.
|
|
4
|
+
- **Fix**: A picture starved of data is no longer reported as moving. `paused` is false while an element waits for bytes, so a frozen player told the proxy it was playing and the position walked away from where the viewer actually stood — the exact case a frozen player is in. It is `readyState >= HAVE_FUTURE_DATA` as well now, and `waiting` joins `pause`/`playing`/`ended` in the events that report at once rather than waiting out the ten-second interval.
|
|
5
|
+
- **Fix**: PRESENCE IS THE CONNECTION AND ONLY THE CONNECTION. Silence longer than "any silence a watching viewer can produce" used to end it, and there is no such interval: a paused viewer, a hidden tab whose timers the browser has throttled, and one holding two minutes of cushion all say nothing and are all still watching. A viewer is here until something says otherwise — the browser releasing the session, or the connection closing. `presenceStaleAfterMs` and every threshold it fed are gone.
|
|
6
|
+
- **Fix**: A soundtrack begins where the earliest viewer of the picture stands, with nothing subtracted. What that replaced was an apparatus for turning a request edge back into a playhead: three position sources ranked by priority, a second function naming which had answered, a subtraction of the deepest reported buffer, and a special case for a session nobody had asked anything of yet. Each repaired a quantity that meant two things. Field 2026-08-31, what it cost: a page opened at 588 s started its sound at 460 s — 131 seconds of film nobody would hear, and the segment the viewer needed took 38.8 s against the picture's 8.4 s.
|
|
7
|
+
- **Chore**: `resolveViewerPosition`, `viewerPositionSource`, `#viewerPositionSourceOf`, `#consumerPositionOf`, `#noteConsumerHead`, `session.lastRequestedSegment`, `session.furthestViewerSeconds` and `headsOf` are deleted. Where a viewer is now lives in the viewer's own layer (`viewer/positions.js`), and the session manager is 250 lines shorter for it.
|
|
8
|
+
- **Chore**: The flag that pins a rung's size is `exactSize`, not `manualQuality`, and the key it writes reads `enc:1280x720:exact` rather than `:manual`. It says nothing about who asked — every rung of a master sets it, whether the viewer picked that rung or the player moved itself onto one — and the old name was read as "the viewer did this" four times in a few weeks, in field logs where the viewer had touched nothing. The log line says `size=exact` for the same reason. Requires server 0.27.14 or newer in the page.
|
|
9
|
+
- **New**: `test/plan-is-steady.test.js` — five checks that the placement does not move while the facts do not, over the whole chain from what a viewer states to what the plan answers, with the field's own two positions. Confirmed to go red on the reading this release replaced.
|
|
10
|
+
|
|
11
|
+
## 2.83.8
|
|
12
|
+
|
|
13
|
+
- **New**: THE BROWSER'S OWN LOG IS KEPT BESIDE THE PROXY'S, ON A DISK THAT SURVIVES. Explaining any failure here needs both halves — what the proxy did and what the page saw — and the second half went to the registry server's standard output on the droplet, which every release of it destroys. On 2026-09-13 a viewer reported a desync and a frozen picture and the analysis ran on half the evidence for exactly that reason. `POST /api/client-logs` on the proxy writes them into the same directory as its own log (`/data` on the addon host), one file per viewing session: `client-<start>-<session>-<film>-<infohash>.log`. The start time and the session id are what join the two halves without guessing, and the film says which one it is without opening the file. Seven checks over the writer, one confirmed to go red.
|
|
14
|
+
- **New**: A session that has not chosen a film yet still gets its file (`-no-torrent-yet`), because the page opening, a proxy being chosen and a connection failing all happen before any torrent exists and are worth keeping. The file is keyed by the SESSION, so the film arriving mid-session does not start a second one.
|
|
15
|
+
- **Chore**: A torrent name is arbitrary bytes chosen by a stranger and here it becomes a path, so it is reduced to letters, digits, dot, dash and underscore and truncated; a check holds that `../../etc/passwd` cannot escape the directory. Closing the sink is awaitable: a write stream flushes asynchronously, and a process that ends without waiting loses the last lines of every session — the ones a crash is explained by.
|
|
16
|
+
|
|
1
17
|
## 2.83.7
|
|
2
18
|
- **Fix**: A SUBTITLE SUBSCRIPTION BELONGS TO THE VIEWER, NOT TO THE CHANNEL. It was a `Set<DataChannel>` inside the transport, so a seamless reconnect lost subtitles for the rest of the session (recorded 2026-08-26 and never fixed), and a transport that rotates its association on purpose — which is what the delivery wedge now requires — would lose them at every rotation. It is held against the viewer now (`Viewers.wantsCues` / `wantingCues`), registered by the request that turns subtitles on, which is the only place that knows both the person and the file. Whatever channel that person is reachable on next, they are still subscribed. Eight checks over the registry, three of which go red if the file index stops being part of the key.
|
|
3
19
|
- **Chore**: The transport no longer sniffs `/api/subtitles` out of the request path, no longer derives a torrent source key, and no longer takes `sourceRegistry` at all — 41 lines of application routing and torrent identity out of the layer whose job is carrying bytes. It is given one plain function returning opaque ids (`viewersWantingCues`) and keeps only "this name is reachable on this channel", which is its own fact. Requires server 0.27.11 or newer in the page: an older page sends no `consumerId`, and then nothing subscribes.
|
package/CLAUDE.md
CHANGED
|
@@ -89,9 +89,11 @@ Linux-only host (e.g. POSIX-only signals must degrade elsewhere).
|
|
|
89
89
|
why the priority map is read at two scopes (per FILE for the swarm, per
|
|
90
90
|
OUTPUT for the encoders), and what each of the eight other places that used
|
|
91
91
|
to place or kill an encoder now states instead.
|
|
92
|
-
- `docs/logs.md` — where to find logs
|
|
93
|
-
|
|
94
|
-
|
|
92
|
+
- `docs/logs.md` — where to find logs, and the fact that the BROWSER's own log
|
|
93
|
+
is in TWO places split by phase: on the droplet while a transport is being
|
|
94
|
+
acquired and at unload, on THIS proxy for the whole of a viewing
|
|
95
|
+
(`/data/client-<start>-<session>-<torrent>.log`, one file per session).
|
|
96
|
+
Read both before concluding anything from a half-session.
|
|
95
97
|
- `playback-planner.js` — single ffmpeg probe returns audioCodec, videoCodec,
|
|
96
98
|
container, durationSeconds. `mode` is advisory; the browser decides.
|
|
97
99
|
- `hls-session-manager.js` — one ffmpeg per (source, file, settings). Serves a
|
package/bin/cli.js
CHANGED
|
@@ -323,6 +323,7 @@ try {
|
|
|
323
323
|
memoryBytes,
|
|
324
324
|
segmentFormat: options.segmentFormat,
|
|
325
325
|
stateDir: options.stateDir,
|
|
326
|
+
logFile: options.logFile ?? "",
|
|
326
327
|
deliverySink: options.deliverySink === true,
|
|
327
328
|
// Late-bound the same way `webRtcManager` is below: the torrent pool is
|
|
328
329
|
// built inside `startProxyServer`, before `dataChannelHandler` — the
|
|
@@ -28,12 +28,37 @@ It decides from four things and no others:
|
|
|
28
28
|
name a consumer id, or hold a person. What crosses is a priority map: zones of
|
|
29
29
|
segment numbers with a rank and a real time, and nobody's name on it.
|
|
30
30
|
|
|
31
|
+
## Where a viewer is, and how it reaches the encoders
|
|
32
|
+
|
|
33
|
+
A viewer's position has ONE writer — the viewer — and is a function of time:
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
position(now) = stated + (playing ? 1 : 0) × (now − statedAt)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
A viewer whose picture moves covers a second of film every second, so between
|
|
40
|
+
the moments they speak their position is known exactly; one whose picture is
|
|
41
|
+
stopped covers nothing. Nothing else writes it, and in particular a request for
|
|
42
|
+
a segment does not: a request says the viewer is still here, and the segment
|
|
43
|
+
either exists and is served or does not and is waited for.
|
|
44
|
+
|
|
45
|
+
Held as a stored number instead it was a staircase, flat for the ten seconds
|
|
46
|
+
between reports and then a jump — and with two writers filling it in turn the
|
|
47
|
+
steps went backwards as often as forwards. Field 2026-09-13, one output over one
|
|
48
|
+
second: `p100:#9..#10`, `p100:#18..#20`, `p100:#16..#18`, `p100:#18..#20`, while
|
|
49
|
+
the second viewer sat frozen at 1673.6 s. The placement follows the map, so the
|
|
50
|
+
machine spent six minutes on 77 encoder starts and 141 stops.
|
|
51
|
+
|
|
52
|
+
PRESENCE IS A SEPARATE FACT and it is the connection: a viewer is here until
|
|
53
|
+
something says otherwise. Silence never says it — a pause, a hidden tab whose
|
|
54
|
+
timers are throttled, and a full cushion are all silent and all still watching.
|
|
55
|
+
|
|
31
56
|
## Viewers become a map, and that is the whole crossing
|
|
32
57
|
|
|
33
58
|
```mermaid
|
|
34
59
|
flowchart TB
|
|
35
60
|
subgraph V["services/viewer — where people are"]
|
|
36
|
-
VW[Viewer<br/>position, playing, buffered, chosen track]
|
|
61
|
+
VW[Viewer<br/>position as a function of time, playing, buffered, chosen track]
|
|
37
62
|
VS[Viewers<br/>the relation, indexed from both ends]
|
|
38
63
|
end
|
|
39
64
|
|
package/docs/logs.md
CHANGED
|
@@ -1,51 +1,97 @@
|
|
|
1
|
-
# Logs — where to look
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
ssh do "docker logs infra-server-1 --tail
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
1
|
+
# Logs — where to look
|
|
2
|
+
|
|
3
|
+
No browser console copy-paste is needed: the page forwards its own log. But it
|
|
4
|
+
lands in TWO places depending on the phase of the session, and `docker logs` is
|
|
5
|
+
only one of them — see "Browser logs are in TWO places" below before concluding
|
|
6
|
+
anything from a half-session.
|
|
7
|
+
|
|
8
|
+
## HA proxy (aarch64, addon `b34a1737_torrent_tv_proxy`)
|
|
9
|
+
|
|
10
|
+
Image `b34a1737/aarch64-addon-torrent_tv_proxy:<version>` = `@torrent-tv/proxy` `<version>` (see `ha-addon/torrent_tv_proxy/config.yaml`).
|
|
11
|
+
|
|
12
|
+
- Console + file are the same: the proxy logs to both `stdout` and `/data/proxy.log` on start (`logging to /data/proxy.log as well as the console`).
|
|
13
|
+
- **Since 2.71.0 that is true of the torrent thread too, and it was not before.**
|
|
14
|
+
A worker thread loads its own instance of every module, so the logger's file
|
|
15
|
+
handle — set once, on the main thread — was null in the worker for the life of
|
|
16
|
+
the process. Measured over a whole 49 938-line file: zero lines from the piece
|
|
17
|
+
reader (`read "…"`, `supply "…"`, every wait and its cause) and zero from the
|
|
18
|
+
torrent pool, against 52 and 36 of them in `docker logs`, which every release
|
|
19
|
+
destroys. The worker now sends its lines to the main thread, which is the only
|
|
20
|
+
writer; a line from there reads `torrent-worker: …`.
|
|
21
|
+
- Via container:
|
|
22
|
+
```bash
|
|
23
|
+
ssh ha "sudo docker logs app_b34a1737_torrent_tv_proxy --tail 200"
|
|
24
|
+
ssh ha "sudo docker exec app_b34a1737_torrent_tv_proxy cat /data/proxy.log | tail -n 300"
|
|
25
|
+
```
|
|
26
|
+
- Filter by session: every transcode session logs its id, e.g. `003ed2fd-7c9b-4cd5-9d05-ff875ff2be23`, `hold segment-00068.mp4 failed`, `encode-run failed`, `EXITED_*`.
|
|
27
|
+
- Host file `/data/proxy.log` survives `docker logs` rotation; `core dumps: 1 present` line shows kept dump under `/data`.
|
|
28
|
+
|
|
29
|
+
SSH to HA requires `MACs hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,umac-128-etm@openssh.com` — server `OpenSSH_10.3` on `homeassistant.local` offers only `*-etm` (`Unable to negotiate` / `Corrupted MAC` otherwise). Already in `~/.ssh/config` `Host ha`.
|
|
30
|
+
|
|
31
|
+
## DO server (webauth.courses, `infra-server-1`)
|
|
32
|
+
|
|
33
|
+
- Server is `infra-server-1` (`ghcr.io/torrent-tv/server:latest`) on `do` (`206.189.97.152`).
|
|
34
|
+
- Frontend logs reach here only in the phases the proxy cannot be reached in — see "Browser logs are in TWO places" below. When they do, the server `console.log`s them with the prefix `[client <tag> <id> sig=<webrtcSessionId>]` (`server/routes/api/client-logs/post.js`), in the same container as its own lines.
|
|
35
|
+
- Via container:
|
|
36
|
+
```bash
|
|
37
|
+
ssh do "docker logs infra-server-1 --tail 200 | cat"
|
|
38
|
+
# filter a single viewing:
|
|
39
|
+
ssh do "docker logs infra-server-1 --tail 500 | grep 003ed2fd"
|
|
40
|
+
ssh do "docker logs infra-server-1 --tail 500 | grep '\[client'"
|
|
41
|
+
```
|
|
42
|
+
- No need to open eruda or copy the browser console on a phone — it is forwarded.
|
|
43
|
+
|
|
44
|
+
## Browser logs are in TWO places, split by phase — read both
|
|
45
|
+
|
|
46
|
+
Until 2026-09-13 the page's whole log went to the droplet's standard output,
|
|
47
|
+
which every release of the server destroys. A viewer reported a desync and a
|
|
48
|
+
frozen picture that day and the analysis ran on half the evidence, so the log
|
|
49
|
+
moved to the proxy — beside the proxy's own, on the same durable directory.
|
|
50
|
+
|
|
51
|
+
The split is by PHASE, and neither half is the whole session:
|
|
52
|
+
|
|
53
|
+
1. **Before a transport exists** — the page opening, the proxy being chosen, a
|
|
54
|
+
connection failing — there is nowhere else to send it, so it goes to the
|
|
55
|
+
SERVER (`infra-server-1`);
|
|
56
|
+
2. **From the moment the data channel is up**, the page's logger is given a
|
|
57
|
+
proxy sink (`loading.js`, `setProxySink`) and every batch goes to the PROXY
|
|
58
|
+
over that channel. This is the bulk of a viewing;
|
|
59
|
+
3. **At unload** it goes to the server again: `navigator.sendBeacon` is the only
|
|
60
|
+
thing a page that is going away can use, and a data channel is not;
|
|
61
|
+
4. **A batch that cannot reach the proxy** falls back to the server.
|
|
62
|
+
|
|
63
|
+
So a session that failed while connecting is on the droplet, a session that
|
|
64
|
+
failed while playing is on the addon host, and a complete reading of a long
|
|
65
|
+
failure needs both.
|
|
66
|
+
|
|
67
|
+
### The files on the proxy
|
|
68
|
+
|
|
69
|
+
One file per viewing session, in the proxy log's own directory (`/data` on the
|
|
70
|
+
addon host), named so the two halves join without guessing:
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
client-<YYYYMMDD-HHMMSS UTC of the session start>-<sessionId:8>-<torrent name:60>-<infoHash:8>.log
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
A session that has not chosen a torrent yet writes `no-torrent-yet` in that
|
|
77
|
+
last part — the file is keyed by the session id, not by the name, so choosing a
|
|
78
|
+
torrent later does NOT start a second file. Rotated at 16 MB to `<name>.log.1`.
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
ssh ha "sudo docker exec app_b34a1737_torrent_tv_proxy sh -c 'ls -la /data/client-*.log'"
|
|
82
|
+
ssh ha "sudo docker exec app_b34a1737_torrent_tv_proxy sh -c 'cat /data/client-20260913-*.log'"
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Implementation: `utils/client-log-file.js`, route `routes/api/client-logs/post.js`.
|
|
86
|
+
|
|
87
|
+
## Quick triage
|
|
88
|
+
|
|
89
|
+
- Rewind/seek bug (hold 0ms → 500): HA `hold ... failed after 0ms → 500` + `encode-run` lines for the same `<sessionId>`, and DO `[client ...] fragLoadError / levelLoadError 500` for same `sn`.
|
|
90
|
+
- Cushion / link budget: HA `memory: rss=... anon=...` and `cushion` lines; DO `[eta]` / `[cushion]` from client.
|
|
91
|
+
- Update check: `ssh ha "sudo docker exec hassio_cli ha apps info b34a1737_torrent_tv_proxy | grep version"` and `ssh ha "sudo docker ps --filter name=app_b34a1737_torrent_tv_proxy"`.
|
|
92
|
+
|
|
93
|
+
## Related
|
|
94
|
+
|
|
95
|
+
- `ha-addon/CLAUDE.md` — addon build/update detour via `hassio_cli`, cache-bust via `config.yaml` version.
|
|
96
|
+
- `docs/container-architecture.md` — what container/track classes log and where.
|
|
97
|
+
- `routes/api/client-logs/post.js` and `server/routes/api/client-logs/post.js` — the two receivers; both sanitize the same way (control chars → space, `MAX_LINES`, `MAX_MSG_LEN`).
|
package/package.json
CHANGED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Take the browser's own log and keep it beside the proxy's.
|
|
3
|
+
*
|
|
4
|
+
* The page has always forwarded its console somewhere; until now that was the
|
|
5
|
+
* SERVER's standard output on the droplet, which every release destroys. Both
|
|
6
|
+
* halves of a failure are needed to explain one — what the proxy did and what
|
|
7
|
+
* the page saw — and on 2026-09-13 an investigation ran on half of them for
|
|
8
|
+
* exactly that reason.
|
|
9
|
+
*
|
|
10
|
+
* Here the two land in one directory, on the host's durable disk, in files
|
|
11
|
+
* named so they join without guessing.
|
|
12
|
+
*
|
|
13
|
+
* The server's route stays and is not replaced: a page that has no proxy yet,
|
|
14
|
+
* or whose connection to one just failed, still has something to say, and those
|
|
15
|
+
* are the moments that matter most.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const MAX_LINES = 50;
|
|
19
|
+
const MAX_MSG_LEN = 2000;
|
|
20
|
+
const MAX_TAG_LEN = 40;
|
|
21
|
+
const MAX_SID_LEN = 16;
|
|
22
|
+
const MAX_NAME_LEN = 200;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {unknown} value
|
|
26
|
+
* @param {number} maxLen
|
|
27
|
+
* @returns {string}
|
|
28
|
+
*/
|
|
29
|
+
function safeString(value, maxLen) {
|
|
30
|
+
return (typeof value === "string" ? value : "").slice(0, maxLen);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Replace every control character with a space.
|
|
35
|
+
*
|
|
36
|
+
* These lines are written by a stranger's browser into a file a person will
|
|
37
|
+
* later read in a terminal: a newline would forge a log entry and an escape
|
|
38
|
+
* sequence would drive the terminal.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} value
|
|
41
|
+
* @returns {string}
|
|
42
|
+
*/
|
|
43
|
+
function sanitizeLine(value) {
|
|
44
|
+
let out = "";
|
|
45
|
+
for (const character of value) {
|
|
46
|
+
const code = character.codePointAt(0) ?? 0;
|
|
47
|
+
out += code < 0x20 || code === 0x7f ? " " : character;
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* `POST /api/client-logs`
|
|
54
|
+
*
|
|
55
|
+
* Body: `{ sessionId, startedAt, torrentName, infoHash, tag, signalSessionId,
|
|
56
|
+
* lines: [{ level, ts, msg }] }`.
|
|
57
|
+
*
|
|
58
|
+
* @param {import("fastify").FastifyRequest} req
|
|
59
|
+
* @param {import("fastify").FastifyReply} reply
|
|
60
|
+
* @param {{ clientLogs: { write: (session: object, lines: string[]) => void } | null }} deps
|
|
61
|
+
* @returns {Promise<unknown>}
|
|
62
|
+
*/
|
|
63
|
+
export async function handleApiClientLogsPost(req, reply, { clientLogs }) {
|
|
64
|
+
const body = req.body && typeof req.body === "object" && !Array.isArray(req.body) ? req.body : {};
|
|
65
|
+
const session = {
|
|
66
|
+
sessionId: safeString(body.sessionId, MAX_SID_LEN) || "unknown",
|
|
67
|
+
startedAt: safeString(body.startedAt, 30),
|
|
68
|
+
torrentName: sanitizeLine(safeString(body.torrentName, MAX_NAME_LEN)),
|
|
69
|
+
infoHash: safeString(body.infoHash, 40)
|
|
70
|
+
};
|
|
71
|
+
const tag = sanitizeLine(safeString(body.tag, MAX_TAG_LEN)) || "Unknown/Unknown";
|
|
72
|
+
const rows = Array.isArray(body.lines) ? body.lines.slice(0, MAX_LINES) : [];
|
|
73
|
+
|
|
74
|
+
const lines = rows.map((row) => {
|
|
75
|
+
const entry = row && typeof row === "object" ? row : {};
|
|
76
|
+
const level = safeString(entry.level, 8) || "log";
|
|
77
|
+
const at = sanitizeLine(safeString(entry.ts, 16));
|
|
78
|
+
const message = sanitizeLine(safeString(entry.msg, MAX_MSG_LEN));
|
|
79
|
+
return `[${tag}] ${at} ${level}: ${message}`;
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
if (lines.length > 0) {
|
|
83
|
+
clientLogs?.write(session, lines);
|
|
84
|
+
}
|
|
85
|
+
return reply.code(204).send();
|
|
86
|
+
}
|