@loksly/trcc-client 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +73 -2
- package/dist/client.d.ts +88 -0
- package/dist/client.js +271 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +40 -4
- package/src/client.ts +307 -0
- package/src/index.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Loksly
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,74 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @loksly/trcc-client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A small, dependency-free Node.js client for the REST API of `trcc serve`, from
|
|
4
|
+
[thermalright-trcc-linux](https://github.com/Lexonight1/thermalright-trcc-linux),
|
|
5
|
+
the Linux driver for Thermalright USB LCD panels (Trofeo Vision and others).
|
|
6
|
+
|
|
7
|
+
It comes with the lessons of running a dashboard on one of these panels 24/7:
|
|
8
|
+
|
|
9
|
+
- **No resending from the client.** The LY firmware drops the image after
|
|
10
|
+
~2-3 s without a frame, but `trcc serve` already resends the cached frame
|
|
11
|
+
every ~150 ms. The client sends a frame only when something changes, and
|
|
12
|
+
`pinFrame` is just a spaced health probe.
|
|
13
|
+
- **A canary for the keepalive contract.** `trcc` once changed the
|
|
14
|
+
keepalive's behaviour behind an identical `200 OK`, which turned clients
|
|
15
|
+
into silent busy loops. `checkKeepaliveContract` warns the first time the
|
|
16
|
+
answer stops looking like the one this client was written for.
|
|
17
|
+
- **A hand-built multipart body.** Sending `FormData` + `Blob` through Node's
|
|
18
|
+
`fetch` leaked every image (8 GB in nine days for a dashboard). `sendFrame`
|
|
19
|
+
builds the body in a single `Buffer`, and sends JPEG or PNG based on the
|
|
20
|
+
first bytes.
|
|
21
|
+
- **API tokens**, read from a file and re-read every 30 s, for a `trcc serve`
|
|
22
|
+
started with `--token`.
|
|
23
|
+
|
|
24
|
+
## Install
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
npm install @loksly/trcc-client
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Node.js 20 or newer (uses the built-in `fetch`). ESM only.
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { connectPanel, detectPanel, pinFrame, sendFrame, serverAlive } from '@loksly/trcc-client';
|
|
36
|
+
|
|
37
|
+
const trcc = { url: 'http://127.0.0.1:8099', tokenFile: `${process.env.HOME}/.trcc-token`, timeoutMs: 20_000 };
|
|
38
|
+
|
|
39
|
+
if (!(await serverAlive(trcc))) throw new Error('trcc serve is not running');
|
|
40
|
+
const panel = await detectPanel(trcc); // { key: '0416:5408', width: 1920, height: 462 }
|
|
41
|
+
if (!panel) throw new Error('no panel');
|
|
42
|
+
|
|
43
|
+
await connectPanel(trcc, panel.key);
|
|
44
|
+
await sendFrame(trcc, panel.key, pngOrJpegBuffer); // render at panel.width × panel.height
|
|
45
|
+
|
|
46
|
+
const stop = pinFrame(trcc, panel.key, 30, (msg) => console.warn(msg));
|
|
47
|
+
// … later
|
|
48
|
+
stop();
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Options
|
|
52
|
+
|
|
53
|
+
| Field | Meaning |
|
|
54
|
+
|---|---|
|
|
55
|
+
| `url` | Base URL of `trcc serve`. |
|
|
56
|
+
| `token` / `tokenFile` | API token, inline or from a file. Without either, no header is sent. |
|
|
57
|
+
| `timeoutMs` | Per-request timeout. |
|
|
58
|
+
| `deviceKey` | `VID:PID` of the panel to use; empty takes the first one listed. |
|
|
59
|
+
|
|
60
|
+
## Known limits
|
|
61
|
+
|
|
62
|
+
Two identical panels share a `VID:PID`, and `trcc serve` cannot yet be told to
|
|
63
|
+
leave one of them alone
|
|
64
|
+
([trcc#317](https://github.com/Lexonight1/thermalright-trcc-linux/issues/317)).
|
|
65
|
+
|
|
66
|
+
## Contributing
|
|
67
|
+
|
|
68
|
+
This package lives in the [aimonitor](https://github.com/Loksly/aimonitor)
|
|
69
|
+
repository under `packages/trcc-client`. Issues and pull requests are welcome
|
|
70
|
+
there. Run `npm test` from the package directory.
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
MIT
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
export interface TrccOptions {
|
|
2
|
+
/** Base URL of `trcc serve`, e.g. `http://127.0.0.1:8099`. */
|
|
3
|
+
url: string;
|
|
4
|
+
/** API token, if `trcc serve` runs with `--token`. */
|
|
5
|
+
token?: string;
|
|
6
|
+
/** File holding the token (re-read every 30 s). Ignored if `token` is set. */
|
|
7
|
+
tokenFile?: string;
|
|
8
|
+
/** Timeout for each request, in ms. */
|
|
9
|
+
timeoutMs: number;
|
|
10
|
+
/** `VID:PID` key of the panel to use; empty = the first one the server lists. */
|
|
11
|
+
deviceKey?: string;
|
|
12
|
+
/**
|
|
13
|
+
* USB serial number of the panel to use. With several identical panels,
|
|
14
|
+
* trcc ≥ 9.10.8 lists each as `VID:PID@<usb path>` (`0416:5408@3-11.1`), and
|
|
15
|
+
* the bare `VID:PID` means "the first one", which may be a twin another
|
|
16
|
+
* process holds. The serial is mapped to its USB path through sysfs.
|
|
17
|
+
*/
|
|
18
|
+
serial?: string;
|
|
19
|
+
/** Where to look the serial up; for tests. */
|
|
20
|
+
sysfsRoot?: string;
|
|
21
|
+
}
|
|
22
|
+
/** The USB path (`3-11.1`) of the device with this serial, from sysfs; null if not plugged in. */
|
|
23
|
+
export declare function usbPathForSerial(serial: string, root?: string): string | null;
|
|
24
|
+
export interface Panel {
|
|
25
|
+
/** `VID:PID` key of the device. */
|
|
26
|
+
key: string;
|
|
27
|
+
width: number;
|
|
28
|
+
height: number;
|
|
29
|
+
}
|
|
30
|
+
export declare function apiToken(o: Pick<TrccOptions, 'token' | 'tokenFile'>, now?: number): string;
|
|
31
|
+
/** Is `trcc serve` up? */
|
|
32
|
+
export declare function serverAlive(o: TrccOptions): Promise<boolean>;
|
|
33
|
+
/**
|
|
34
|
+
* Asks the server which panel there is and at which resolution. Render from
|
|
35
|
+
* this, not from a constant: the driver documents 1920×462 for the Trofeo
|
|
36
|
+
* Vision while shops advertise 1920×480.
|
|
37
|
+
*/
|
|
38
|
+
export declare function detectPanel(o: TrccOptions): Promise<Panel | null>;
|
|
39
|
+
/** Opens the device on the server. Without this, frames are stored but not shown. */
|
|
40
|
+
export declare function connectPanel(o: TrccOptions, key: string): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* `multipart/form-data` body with the image in the `image` field, built by hand.
|
|
43
|
+
*
|
|
44
|
+
* **Do not use `FormData` + `Blob` here.** With Node 24's `fetch`, every such
|
|
45
|
+
* request keeps a copy of the image alive forever: measured, 1500 requests of
|
|
46
|
+
* 81 KB retained 118 MB of `arrayBuffers` after a forced GC. With the body in a
|
|
47
|
+
* single `Buffer`, nothing. A dashboard sending on every change reached 8 GB
|
|
48
|
+
* in nine days.
|
|
49
|
+
*
|
|
50
|
+
* `trcc` picks the format from the file name's extension, so it is derived
|
|
51
|
+
* from the first bytes: JPEG starts with FF D8, anything else goes as PNG.
|
|
52
|
+
*/
|
|
53
|
+
export declare function multipartImage(image: Buffer, boundary?: string): {
|
|
54
|
+
body: Buffer;
|
|
55
|
+
contentType: string;
|
|
56
|
+
};
|
|
57
|
+
/** Uploads a PNG or JPEG and shows it. Throws if the server reports an error. */
|
|
58
|
+
export declare function sendFrame(o: TrccOptions, key: string, image: Buffer): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Checks that the keepalive contract is still the one `pinFrame` assumes.
|
|
61
|
+
*
|
|
62
|
+
* It exists because `trcc` once changed this endpoint's behaviour **without
|
|
63
|
+
* changing its response**: the same `200 OK` blocking for 30 s as returning in
|
|
64
|
+
* 3 ms, so a client written for the old contract silently degenerated into a
|
|
65
|
+
* busy loop (87% of a core, not a single error in any log). Any assumption
|
|
66
|
+
* about a foreign API that can flip silently deserves a cheap assertion. Here,
|
|
67
|
+
* the three `pinFrame` depends on:
|
|
68
|
+
*
|
|
69
|
+
* 1. the call does **not** block;
|
|
70
|
+
* 2. the call does **not** resend anything itself (`frames_resent === 0`),
|
|
71
|
+
* because the server's worker keeps the image;
|
|
72
|
+
* 3. the server says so in its message.
|
|
73
|
+
*
|
|
74
|
+
* It fixes nothing: it warns. A canary, not a cage.
|
|
75
|
+
*/
|
|
76
|
+
export declare function checkKeepaliveContract(body: string, elapsedMs: number, warn: (msg: string) => void): void;
|
|
77
|
+
/**
|
|
78
|
+
* Watches that the panel keeps its image. **It does not resend anything, and
|
|
79
|
+
* must not**: the server's `DeviceSender` resends the cached frame every
|
|
80
|
+
* ~150 ms while the process lives, and the keepalive call with `count >= 1`
|
|
81
|
+
* only checks that there is a frame and a connected device, returning in
|
|
82
|
+
* microseconds.
|
|
83
|
+
*
|
|
84
|
+
* What remains is a **health probe**: one call every `intervalS`, to find out
|
|
85
|
+
* that the panel was disconnected. Returns the function that stops it; the
|
|
86
|
+
* wait is interruptible, so stopping never takes `intervalS`.
|
|
87
|
+
*/
|
|
88
|
+
export declare function pinFrame(o: TrccOptions, key: string, intervalS: number, onError: (msg: string) => void): () => void;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client for the REST API of `trcc serve` (thermalright-trcc-linux), the
|
|
3
|
+
* process that owns a Thermalright USB LCD panel.
|
|
4
|
+
*
|
|
5
|
+
* Two facts about the hardware shape this module:
|
|
6
|
+
*
|
|
7
|
+
* - The LY firmware of panels such as the Trofeo Vision **drops the image**
|
|
8
|
+
* and falls back to the factory logo after ~2-3 s without a frame, so the
|
|
9
|
+
* image has to be resent several times per second.
|
|
10
|
+
* - The USB interface is exclusive: two `trcc` processes cannot talk to the
|
|
11
|
+
* same panel at once. So the resending cannot be a separate process.
|
|
12
|
+
*
|
|
13
|
+
* `trcc serve` solves both: one process owns the USB device, and its
|
|
14
|
+
* `DeviceSender` resends the cached frame every ~150 ms on its own. This
|
|
15
|
+
* client therefore only sends a frame when something changes (`sendFrame`)
|
|
16
|
+
* and runs a spaced health probe (`pinFrame`). It does not resend anything.
|
|
17
|
+
*/
|
|
18
|
+
import { readdirSync, readFileSync } from 'node:fs';
|
|
19
|
+
/** The USB path (`3-11.1`) of the device with this serial, from sysfs; null if not plugged in. */
|
|
20
|
+
export function usbPathForSerial(serial, root = '/sys/bus/usb/devices') {
|
|
21
|
+
let entries = [];
|
|
22
|
+
try {
|
|
23
|
+
entries = readdirSync(root);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
for (const name of entries) {
|
|
29
|
+
if (!/^\d+-[\d.]+$/.test(name))
|
|
30
|
+
continue;
|
|
31
|
+
try {
|
|
32
|
+
if (readFileSync(`${root}/${name}/serial`, 'utf8').trim() === serial)
|
|
33
|
+
return name;
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
// No serial (hubs and the like).
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
/** The token from its file, re-read every 30 s in case it changes or appears. */
|
|
42
|
+
let tokenCache = null;
|
|
43
|
+
export function apiToken(o, now = Date.now()) {
|
|
44
|
+
if (o.token)
|
|
45
|
+
return o.token;
|
|
46
|
+
const path = o.tokenFile ?? '';
|
|
47
|
+
if (tokenCache && tokenCache.path === path && now - tokenCache.at < 30_000)
|
|
48
|
+
return tokenCache.value;
|
|
49
|
+
let value = '';
|
|
50
|
+
try {
|
|
51
|
+
value = path ? readFileSync(path, 'utf8').trim() : '';
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
// No file, no token: a `trcc serve` started without `--token` accepts that.
|
|
55
|
+
}
|
|
56
|
+
tokenCache = { path, at: now, value };
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
/** Request headers, with the token when there is one. */
|
|
60
|
+
function headers(o, extra = {}) {
|
|
61
|
+
const token = apiToken(o);
|
|
62
|
+
return token ? { ...extra, 'X-API-Token': token } : extra;
|
|
63
|
+
}
|
|
64
|
+
function base(o) {
|
|
65
|
+
return o.url.replace(/\/+$/, '');
|
|
66
|
+
}
|
|
67
|
+
/** The key contains a colon, which has to be escaped in the path. */
|
|
68
|
+
function url(o, key, suffix) {
|
|
69
|
+
return `${base(o)}/devices/${encodeURIComponent(key)}${suffix}`;
|
|
70
|
+
}
|
|
71
|
+
async function postJson(o, target, body) {
|
|
72
|
+
const res = await fetch(target, {
|
|
73
|
+
method: 'POST',
|
|
74
|
+
headers: headers(o, { 'Content-Type': 'application/json' }),
|
|
75
|
+
body: JSON.stringify(body),
|
|
76
|
+
signal: AbortSignal.timeout(o.timeoutMs),
|
|
77
|
+
});
|
|
78
|
+
if (!res.ok)
|
|
79
|
+
throw new Error(`HTTP ${res.status} at ${target}: ${(await res.text()).slice(0, 200)}`);
|
|
80
|
+
return res.json();
|
|
81
|
+
}
|
|
82
|
+
/** Is `trcc serve` up? */
|
|
83
|
+
export async function serverAlive(o) {
|
|
84
|
+
try {
|
|
85
|
+
const res = await fetch(`${base(o)}/health`, { signal: AbortSignal.timeout(3000) });
|
|
86
|
+
return res.ok;
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Asks the server which panel there is and at which resolution. Render from
|
|
94
|
+
* this, not from a constant: the driver documents 1920×462 for the Trofeo
|
|
95
|
+
* Vision while shops advertise 1920×480.
|
|
96
|
+
*/
|
|
97
|
+
export async function detectPanel(o) {
|
|
98
|
+
try {
|
|
99
|
+
const res = await fetch(`${base(o)}/devices`, { headers: headers(o), signal: AbortSignal.timeout(o.timeoutMs) });
|
|
100
|
+
if (!res.ok)
|
|
101
|
+
return null;
|
|
102
|
+
const data = (await res.json());
|
|
103
|
+
const list = data.products ?? [];
|
|
104
|
+
let found = o.deviceKey ? list.find((p) => p.key === o.deviceKey) : list[0];
|
|
105
|
+
if (o.serial && !o.deviceKey) {
|
|
106
|
+
// Per-unit keys (trcc ≥ 9.10.8): the one at our panel's USB path. A
|
|
107
|
+
// server without them lists a single key, which is already ours.
|
|
108
|
+
const path = usbPathForSerial(o.serial, o.sysfsRoot);
|
|
109
|
+
const perUnit = list.filter((p) => p.key?.includes('@'));
|
|
110
|
+
if (perUnit.length)
|
|
111
|
+
found = path ? perUnit.find((p) => p.key.endsWith(`@${path}`)) : undefined;
|
|
112
|
+
}
|
|
113
|
+
if (!found?.key || !found.native_resolution)
|
|
114
|
+
return null;
|
|
115
|
+
const [width, height] = found.native_resolution;
|
|
116
|
+
return { key: found.key, width, height };
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/** Opens the device on the server. Without this, frames are stored but not shown. */
|
|
123
|
+
export async function connectPanel(o, key) {
|
|
124
|
+
await postJson(o, url(o, key, '/connect'), {});
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* `multipart/form-data` body with the image in the `image` field, built by hand.
|
|
128
|
+
*
|
|
129
|
+
* **Do not use `FormData` + `Blob` here.** With Node 24's `fetch`, every such
|
|
130
|
+
* request keeps a copy of the image alive forever: measured, 1500 requests of
|
|
131
|
+
* 81 KB retained 118 MB of `arrayBuffers` after a forced GC. With the body in a
|
|
132
|
+
* single `Buffer`, nothing. A dashboard sending on every change reached 8 GB
|
|
133
|
+
* in nine days.
|
|
134
|
+
*
|
|
135
|
+
* `trcc` picks the format from the file name's extension, so it is derived
|
|
136
|
+
* from the first bytes: JPEG starts with FF D8, anything else goes as PNG.
|
|
137
|
+
*/
|
|
138
|
+
export function multipartImage(image, boundary = `trcc${crypto.randomUUID().replace(/-/g, '')}`) {
|
|
139
|
+
const jpeg = image.length > 2 && image[0] === 0xff && image[1] === 0xd8;
|
|
140
|
+
const head = Buffer.from(`--${boundary}\r\n` +
|
|
141
|
+
`Content-Disposition: form-data; name="image"; filename="frame.${jpeg ? 'jpg' : 'png'}"\r\n` +
|
|
142
|
+
`Content-Type: image/${jpeg ? 'jpeg' : 'png'}\r\n\r\n`);
|
|
143
|
+
const tail = Buffer.from(`\r\n--${boundary}--\r\n`);
|
|
144
|
+
return { body: Buffer.concat([head, image, tail]), contentType: `multipart/form-data; boundary=${boundary}` };
|
|
145
|
+
}
|
|
146
|
+
/** Uploads a PNG or JPEG and shows it. Throws if the server reports an error. */
|
|
147
|
+
export async function sendFrame(o, key, image) {
|
|
148
|
+
const { body, contentType } = multipartImage(image);
|
|
149
|
+
const target = url(o, key, '/display/send-image');
|
|
150
|
+
const res = await fetch(target, {
|
|
151
|
+
method: 'POST',
|
|
152
|
+
headers: headers(o, { 'Content-Type': contentType }),
|
|
153
|
+
body,
|
|
154
|
+
signal: AbortSignal.timeout(o.timeoutMs),
|
|
155
|
+
});
|
|
156
|
+
if (!res.ok)
|
|
157
|
+
throw new Error(`HTTP ${res.status}: ${(await res.text()).slice(0, 200)}`);
|
|
158
|
+
const data = (await res.json());
|
|
159
|
+
if (data.ok === false)
|
|
160
|
+
throw new Error(data.message ?? 'the server rejected the frame');
|
|
161
|
+
// The server stores the frame even when the device is not open; then
|
|
162
|
+
// nothing was shown, and the caller has to know.
|
|
163
|
+
if (data.message && /not connected/i.test(data.message)) {
|
|
164
|
+
throw new Error('the device is not connected on the server');
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Checks that the keepalive contract is still the one `pinFrame` assumes.
|
|
169
|
+
*
|
|
170
|
+
* It exists because `trcc` once changed this endpoint's behaviour **without
|
|
171
|
+
* changing its response**: the same `200 OK` blocking for 30 s as returning in
|
|
172
|
+
* 3 ms, so a client written for the old contract silently degenerated into a
|
|
173
|
+
* busy loop (87% of a core, not a single error in any log). Any assumption
|
|
174
|
+
* about a foreign API that can flip silently deserves a cheap assertion. Here,
|
|
175
|
+
* the three `pinFrame` depends on:
|
|
176
|
+
*
|
|
177
|
+
* 1. the call does **not** block;
|
|
178
|
+
* 2. the call does **not** resend anything itself (`frames_resent === 0`),
|
|
179
|
+
* because the server's worker keeps the image;
|
|
180
|
+
* 3. the server says so in its message.
|
|
181
|
+
*
|
|
182
|
+
* It fixes nothing: it warns. A canary, not a cage.
|
|
183
|
+
*/
|
|
184
|
+
export function checkKeepaliveContract(body, elapsedMs, warn) {
|
|
185
|
+
if (elapsedMs > 5000) {
|
|
186
|
+
warn(`the keepalive took ${(elapsedMs / 1000).toFixed(1)} s: it looks like it blocks again. ` +
|
|
187
|
+
'The spaced probe in pinFrame assumes it answers at once.');
|
|
188
|
+
}
|
|
189
|
+
let data = {};
|
|
190
|
+
try {
|
|
191
|
+
data = JSON.parse(body);
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
warn('the keepalive no longer answers JSON; the contract has changed.');
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (typeof data.frames_resent === 'number' && data.frames_resent > 0) {
|
|
198
|
+
warn(`the keepalive says it resent ${data.frames_resent} frames. It used to resend none (the ` +
|
|
199
|
+
'worker did); if it now depends on this call, the probe is far too slow.');
|
|
200
|
+
}
|
|
201
|
+
if (data.message && !/send worker resends|keepalive active/i.test(data.message)) {
|
|
202
|
+
warn(`the keepalive answers something unexpected: "${data.message.slice(0, 80)}". Check the contract.`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Watches that the panel keeps its image. **It does not resend anything, and
|
|
207
|
+
* must not**: the server's `DeviceSender` resends the cached frame every
|
|
208
|
+
* ~150 ms while the process lives, and the keepalive call with `count >= 1`
|
|
209
|
+
* only checks that there is a frame and a connected device, returning in
|
|
210
|
+
* microseconds.
|
|
211
|
+
*
|
|
212
|
+
* What remains is a **health probe**: one call every `intervalS`, to find out
|
|
213
|
+
* that the panel was disconnected. Returns the function that stops it; the
|
|
214
|
+
* wait is interruptible, so stopping never takes `intervalS`.
|
|
215
|
+
*/
|
|
216
|
+
export function pinFrame(o, key, intervalS, onError) {
|
|
217
|
+
let stopped = false;
|
|
218
|
+
let contractChecked = false;
|
|
219
|
+
const controller = new AbortController();
|
|
220
|
+
const wait = (ms) => new Promise((resolve) => {
|
|
221
|
+
if (stopped)
|
|
222
|
+
return resolve();
|
|
223
|
+
const done = () => {
|
|
224
|
+
clearTimeout(timer);
|
|
225
|
+
controller.signal.removeEventListener('abort', done);
|
|
226
|
+
resolve();
|
|
227
|
+
};
|
|
228
|
+
const timer = setTimeout(done, ms);
|
|
229
|
+
controller.signal.addEventListener('abort', done, { once: true });
|
|
230
|
+
});
|
|
231
|
+
const loop = async () => {
|
|
232
|
+
while (!stopped) {
|
|
233
|
+
try {
|
|
234
|
+
const started = performance.now();
|
|
235
|
+
const res = await fetch(url(o, key, '/display/keepalive'), {
|
|
236
|
+
method: 'POST',
|
|
237
|
+
headers: headers(o, { 'Content-Type': 'application/json' }),
|
|
238
|
+
// Only `>= 1` matters: it picks the non-blocking branch. `count: 0`
|
|
239
|
+
// would make the server sleep inside the HTTP handler and pin a
|
|
240
|
+
// uvicorn worker forever.
|
|
241
|
+
body: JSON.stringify({ count: 1 }),
|
|
242
|
+
signal: controller.signal,
|
|
243
|
+
});
|
|
244
|
+
// Always consume the body: otherwise the connection stays busy and
|
|
245
|
+
// sockets pile up against the server.
|
|
246
|
+
const body = await res.text().catch(() => '');
|
|
247
|
+
if (res.ok && !contractChecked) {
|
|
248
|
+
contractChecked = true;
|
|
249
|
+
checkKeepaliveContract(body, performance.now() - started, onError);
|
|
250
|
+
}
|
|
251
|
+
if (!res.ok) {
|
|
252
|
+
onError(`the panel probe returned HTTP ${res.status}; retrying`);
|
|
253
|
+
await wait(2000);
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
await wait(intervalS * 1000);
|
|
257
|
+
}
|
|
258
|
+
catch (err) {
|
|
259
|
+
if (stopped)
|
|
260
|
+
return;
|
|
261
|
+
onError(`panel probe interrupted: ${err instanceof Error ? err.message : String(err)}`);
|
|
262
|
+
await wait(2000);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
void loop();
|
|
267
|
+
return () => {
|
|
268
|
+
stopped = true;
|
|
269
|
+
controller.abort();
|
|
270
|
+
};
|
|
271
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './client.ts';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./client.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loksly/trcc-client",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Dependency-free Node client for the REST API of `trcc serve` (Thermalright USB LCD panels on Linux).",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Loksly",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/Loksly/aimonitor.git",
|
|
11
|
+
"directory": "packages/trcc-client"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"thermalright",
|
|
15
|
+
"trcc",
|
|
16
|
+
"lcd",
|
|
17
|
+
"usb",
|
|
18
|
+
"trofeo-vision"
|
|
19
|
+
],
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"source": "./src/index.ts",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"default": "./dist/index.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"src",
|
|
30
|
+
"!src/**/*.test.ts",
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE"
|
|
33
|
+
],
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=20"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsc -p tsconfig.json",
|
|
39
|
+
"test": "node --conditions=source --import tsx --test 'src/**/*.test.ts'",
|
|
40
|
+
"check": "tsc -p tsconfig.json --noEmit"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client for the REST API of `trcc serve` (thermalright-trcc-linux), the
|
|
3
|
+
* process that owns a Thermalright USB LCD panel.
|
|
4
|
+
*
|
|
5
|
+
* Two facts about the hardware shape this module:
|
|
6
|
+
*
|
|
7
|
+
* - The LY firmware of panels such as the Trofeo Vision **drops the image**
|
|
8
|
+
* and falls back to the factory logo after ~2-3 s without a frame, so the
|
|
9
|
+
* image has to be resent several times per second.
|
|
10
|
+
* - The USB interface is exclusive: two `trcc` processes cannot talk to the
|
|
11
|
+
* same panel at once. So the resending cannot be a separate process.
|
|
12
|
+
*
|
|
13
|
+
* `trcc serve` solves both: one process owns the USB device, and its
|
|
14
|
+
* `DeviceSender` resends the cached frame every ~150 ms on its own. This
|
|
15
|
+
* client therefore only sends a frame when something changes (`sendFrame`)
|
|
16
|
+
* and runs a spaced health probe (`pinFrame`). It does not resend anything.
|
|
17
|
+
*/
|
|
18
|
+
import { readdirSync, readFileSync } from 'node:fs';
|
|
19
|
+
|
|
20
|
+
export interface TrccOptions {
|
|
21
|
+
/** Base URL of `trcc serve`, e.g. `http://127.0.0.1:8099`. */
|
|
22
|
+
url: string;
|
|
23
|
+
/** API token, if `trcc serve` runs with `--token`. */
|
|
24
|
+
token?: string;
|
|
25
|
+
/** File holding the token (re-read every 30 s). Ignored if `token` is set. */
|
|
26
|
+
tokenFile?: string;
|
|
27
|
+
/** Timeout for each request, in ms. */
|
|
28
|
+
timeoutMs: number;
|
|
29
|
+
/** `VID:PID` key of the panel to use; empty = the first one the server lists. */
|
|
30
|
+
deviceKey?: string;
|
|
31
|
+
/**
|
|
32
|
+
* USB serial number of the panel to use. With several identical panels,
|
|
33
|
+
* trcc ≥ 9.10.8 lists each as `VID:PID@<usb path>` (`0416:5408@3-11.1`), and
|
|
34
|
+
* the bare `VID:PID` means "the first one", which may be a twin another
|
|
35
|
+
* process holds. The serial is mapped to its USB path through sysfs.
|
|
36
|
+
*/
|
|
37
|
+
serial?: string;
|
|
38
|
+
/** Where to look the serial up; for tests. */
|
|
39
|
+
sysfsRoot?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The USB path (`3-11.1`) of the device with this serial, from sysfs; null if not plugged in. */
|
|
43
|
+
export function usbPathForSerial(serial: string, root = '/sys/bus/usb/devices'): string | null {
|
|
44
|
+
let entries: string[] = [];
|
|
45
|
+
try {
|
|
46
|
+
entries = readdirSync(root);
|
|
47
|
+
} catch {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
for (const name of entries) {
|
|
51
|
+
if (!/^\d+-[\d.]+$/.test(name)) continue;
|
|
52
|
+
try {
|
|
53
|
+
if (readFileSync(`${root}/${name}/serial`, 'utf8').trim() === serial) return name;
|
|
54
|
+
} catch {
|
|
55
|
+
// No serial (hubs and the like).
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface Panel {
|
|
62
|
+
/** `VID:PID` key of the device. */
|
|
63
|
+
key: string;
|
|
64
|
+
width: number;
|
|
65
|
+
height: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The token from its file, re-read every 30 s in case it changes or appears. */
|
|
69
|
+
let tokenCache: { path: string; at: number; value: string } | null = null;
|
|
70
|
+
export function apiToken(o: Pick<TrccOptions, 'token' | 'tokenFile'>, now = Date.now()): string {
|
|
71
|
+
if (o.token) return o.token;
|
|
72
|
+
const path = o.tokenFile ?? '';
|
|
73
|
+
if (tokenCache && tokenCache.path === path && now - tokenCache.at < 30_000) return tokenCache.value;
|
|
74
|
+
let value = '';
|
|
75
|
+
try {
|
|
76
|
+
value = path ? readFileSync(path, 'utf8').trim() : '';
|
|
77
|
+
} catch {
|
|
78
|
+
// No file, no token: a `trcc serve` started without `--token` accepts that.
|
|
79
|
+
}
|
|
80
|
+
tokenCache = { path, at: now, value };
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Request headers, with the token when there is one. */
|
|
85
|
+
function headers(o: TrccOptions, extra: Record<string, string> = {}): Record<string, string> {
|
|
86
|
+
const token = apiToken(o);
|
|
87
|
+
return token ? { ...extra, 'X-API-Token': token } : extra;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function base(o: TrccOptions): string {
|
|
91
|
+
return o.url.replace(/\/+$/, '');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The key contains a colon, which has to be escaped in the path. */
|
|
95
|
+
function url(o: TrccOptions, key: string, suffix: string): string {
|
|
96
|
+
return `${base(o)}/devices/${encodeURIComponent(key)}${suffix}`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function postJson(o: TrccOptions, target: string, body: unknown): Promise<unknown> {
|
|
100
|
+
const res = await fetch(target, {
|
|
101
|
+
method: 'POST',
|
|
102
|
+
headers: headers(o, { 'Content-Type': 'application/json' }),
|
|
103
|
+
body: JSON.stringify(body),
|
|
104
|
+
signal: AbortSignal.timeout(o.timeoutMs),
|
|
105
|
+
});
|
|
106
|
+
if (!res.ok) throw new Error(`HTTP ${res.status} at ${target}: ${(await res.text()).slice(0, 200)}`);
|
|
107
|
+
return res.json();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Is `trcc serve` up? */
|
|
111
|
+
export async function serverAlive(o: TrccOptions): Promise<boolean> {
|
|
112
|
+
try {
|
|
113
|
+
const res = await fetch(`${base(o)}/health`, { signal: AbortSignal.timeout(3000) });
|
|
114
|
+
return res.ok;
|
|
115
|
+
} catch {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Asks the server which panel there is and at which resolution. Render from
|
|
122
|
+
* this, not from a constant: the driver documents 1920×462 for the Trofeo
|
|
123
|
+
* Vision while shops advertise 1920×480.
|
|
124
|
+
*/
|
|
125
|
+
export async function detectPanel(o: TrccOptions): Promise<Panel | null> {
|
|
126
|
+
try {
|
|
127
|
+
const res = await fetch(`${base(o)}/devices`, { headers: headers(o), signal: AbortSignal.timeout(o.timeoutMs) });
|
|
128
|
+
if (!res.ok) return null;
|
|
129
|
+
const data = (await res.json()) as { products?: { key?: string; native_resolution?: [number, number] }[] };
|
|
130
|
+
const list = data.products ?? [];
|
|
131
|
+
let found = o.deviceKey ? list.find((p) => p.key === o.deviceKey) : list[0];
|
|
132
|
+
if (o.serial && !o.deviceKey) {
|
|
133
|
+
// Per-unit keys (trcc ≥ 9.10.8): the one at our panel's USB path. A
|
|
134
|
+
// server without them lists a single key, which is already ours.
|
|
135
|
+
const path = usbPathForSerial(o.serial, o.sysfsRoot);
|
|
136
|
+
const perUnit = list.filter((p) => p.key?.includes('@'));
|
|
137
|
+
if (perUnit.length) found = path ? perUnit.find((p) => p.key!.endsWith(`@${path}`)) : undefined;
|
|
138
|
+
}
|
|
139
|
+
if (!found?.key || !found.native_resolution) return null;
|
|
140
|
+
const [width, height] = found.native_resolution;
|
|
141
|
+
return { key: found.key, width, height };
|
|
142
|
+
} catch {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Opens the device on the server. Without this, frames are stored but not shown. */
|
|
148
|
+
export async function connectPanel(o: TrccOptions, key: string): Promise<void> {
|
|
149
|
+
await postJson(o, url(o, key, '/connect'), {});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* `multipart/form-data` body with the image in the `image` field, built by hand.
|
|
154
|
+
*
|
|
155
|
+
* **Do not use `FormData` + `Blob` here.** With Node 24's `fetch`, every such
|
|
156
|
+
* request keeps a copy of the image alive forever: measured, 1500 requests of
|
|
157
|
+
* 81 KB retained 118 MB of `arrayBuffers` after a forced GC. With the body in a
|
|
158
|
+
* single `Buffer`, nothing. A dashboard sending on every change reached 8 GB
|
|
159
|
+
* in nine days.
|
|
160
|
+
*
|
|
161
|
+
* `trcc` picks the format from the file name's extension, so it is derived
|
|
162
|
+
* from the first bytes: JPEG starts with FF D8, anything else goes as PNG.
|
|
163
|
+
*/
|
|
164
|
+
export function multipartImage(
|
|
165
|
+
image: Buffer,
|
|
166
|
+
boundary = `trcc${crypto.randomUUID().replace(/-/g, '')}`,
|
|
167
|
+
): { body: Buffer; contentType: string } {
|
|
168
|
+
const jpeg = image.length > 2 && image[0] === 0xff && image[1] === 0xd8;
|
|
169
|
+
const head = Buffer.from(
|
|
170
|
+
`--${boundary}\r\n` +
|
|
171
|
+
`Content-Disposition: form-data; name="image"; filename="frame.${jpeg ? 'jpg' : 'png'}"\r\n` +
|
|
172
|
+
`Content-Type: image/${jpeg ? 'jpeg' : 'png'}\r\n\r\n`,
|
|
173
|
+
);
|
|
174
|
+
const tail = Buffer.from(`\r\n--${boundary}--\r\n`);
|
|
175
|
+
return { body: Buffer.concat([head, image, tail]), contentType: `multipart/form-data; boundary=${boundary}` };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Uploads a PNG or JPEG and shows it. Throws if the server reports an error. */
|
|
179
|
+
export async function sendFrame(o: TrccOptions, key: string, image: Buffer): Promise<void> {
|
|
180
|
+
const { body, contentType } = multipartImage(image);
|
|
181
|
+
const target = url(o, key, '/display/send-image');
|
|
182
|
+
const res = await fetch(target, {
|
|
183
|
+
method: 'POST',
|
|
184
|
+
headers: headers(o, { 'Content-Type': contentType }),
|
|
185
|
+
body,
|
|
186
|
+
signal: AbortSignal.timeout(o.timeoutMs),
|
|
187
|
+
});
|
|
188
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}: ${(await res.text()).slice(0, 200)}`);
|
|
189
|
+
const data = (await res.json()) as { ok?: boolean; message?: string };
|
|
190
|
+
if (data.ok === false) throw new Error(data.message ?? 'the server rejected the frame');
|
|
191
|
+
// The server stores the frame even when the device is not open; then
|
|
192
|
+
// nothing was shown, and the caller has to know.
|
|
193
|
+
if (data.message && /not connected/i.test(data.message)) {
|
|
194
|
+
throw new Error('the device is not connected on the server');
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Checks that the keepalive contract is still the one `pinFrame` assumes.
|
|
200
|
+
*
|
|
201
|
+
* It exists because `trcc` once changed this endpoint's behaviour **without
|
|
202
|
+
* changing its response**: the same `200 OK` blocking for 30 s as returning in
|
|
203
|
+
* 3 ms, so a client written for the old contract silently degenerated into a
|
|
204
|
+
* busy loop (87% of a core, not a single error in any log). Any assumption
|
|
205
|
+
* about a foreign API that can flip silently deserves a cheap assertion. Here,
|
|
206
|
+
* the three `pinFrame` depends on:
|
|
207
|
+
*
|
|
208
|
+
* 1. the call does **not** block;
|
|
209
|
+
* 2. the call does **not** resend anything itself (`frames_resent === 0`),
|
|
210
|
+
* because the server's worker keeps the image;
|
|
211
|
+
* 3. the server says so in its message.
|
|
212
|
+
*
|
|
213
|
+
* It fixes nothing: it warns. A canary, not a cage.
|
|
214
|
+
*/
|
|
215
|
+
export function checkKeepaliveContract(body: string, elapsedMs: number, warn: (msg: string) => void): void {
|
|
216
|
+
if (elapsedMs > 5000) {
|
|
217
|
+
warn(
|
|
218
|
+
`the keepalive took ${(elapsedMs / 1000).toFixed(1)} s: it looks like it blocks again. ` +
|
|
219
|
+
'The spaced probe in pinFrame assumes it answers at once.',
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
let data: { message?: string; frames_resent?: number } = {};
|
|
223
|
+
try {
|
|
224
|
+
data = JSON.parse(body) as typeof data;
|
|
225
|
+
} catch {
|
|
226
|
+
warn('the keepalive no longer answers JSON; the contract has changed.');
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (typeof data.frames_resent === 'number' && data.frames_resent > 0) {
|
|
230
|
+
warn(
|
|
231
|
+
`the keepalive says it resent ${data.frames_resent} frames. It used to resend none (the ` +
|
|
232
|
+
'worker did); if it now depends on this call, the probe is far too slow.',
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
if (data.message && !/send worker resends|keepalive active/i.test(data.message)) {
|
|
236
|
+
warn(`the keepalive answers something unexpected: "${data.message.slice(0, 80)}". Check the contract.`);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Watches that the panel keeps its image. **It does not resend anything, and
|
|
242
|
+
* must not**: the server's `DeviceSender` resends the cached frame every
|
|
243
|
+
* ~150 ms while the process lives, and the keepalive call with `count >= 1`
|
|
244
|
+
* only checks that there is a frame and a connected device, returning in
|
|
245
|
+
* microseconds.
|
|
246
|
+
*
|
|
247
|
+
* What remains is a **health probe**: one call every `intervalS`, to find out
|
|
248
|
+
* that the panel was disconnected. Returns the function that stops it; the
|
|
249
|
+
* wait is interruptible, so stopping never takes `intervalS`.
|
|
250
|
+
*/
|
|
251
|
+
export function pinFrame(o: TrccOptions, key: string, intervalS: number, onError: (msg: string) => void): () => void {
|
|
252
|
+
let stopped = false;
|
|
253
|
+
let contractChecked = false;
|
|
254
|
+
const controller = new AbortController();
|
|
255
|
+
|
|
256
|
+
const wait = (ms: number) =>
|
|
257
|
+
new Promise<void>((resolve) => {
|
|
258
|
+
if (stopped) return resolve();
|
|
259
|
+
const done = () => {
|
|
260
|
+
clearTimeout(timer);
|
|
261
|
+
controller.signal.removeEventListener('abort', done);
|
|
262
|
+
resolve();
|
|
263
|
+
};
|
|
264
|
+
const timer = setTimeout(done, ms);
|
|
265
|
+
controller.signal.addEventListener('abort', done, { once: true });
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
const loop = async () => {
|
|
269
|
+
while (!stopped) {
|
|
270
|
+
try {
|
|
271
|
+
const started = performance.now();
|
|
272
|
+
const res = await fetch(url(o, key, '/display/keepalive'), {
|
|
273
|
+
method: 'POST',
|
|
274
|
+
headers: headers(o, { 'Content-Type': 'application/json' }),
|
|
275
|
+
// Only `>= 1` matters: it picks the non-blocking branch. `count: 0`
|
|
276
|
+
// would make the server sleep inside the HTTP handler and pin a
|
|
277
|
+
// uvicorn worker forever.
|
|
278
|
+
body: JSON.stringify({ count: 1 }),
|
|
279
|
+
signal: controller.signal,
|
|
280
|
+
});
|
|
281
|
+
// Always consume the body: otherwise the connection stays busy and
|
|
282
|
+
// sockets pile up against the server.
|
|
283
|
+
const body = await res.text().catch(() => '');
|
|
284
|
+
if (res.ok && !contractChecked) {
|
|
285
|
+
contractChecked = true;
|
|
286
|
+
checkKeepaliveContract(body, performance.now() - started, onError);
|
|
287
|
+
}
|
|
288
|
+
if (!res.ok) {
|
|
289
|
+
onError(`the panel probe returned HTTP ${res.status}; retrying`);
|
|
290
|
+
await wait(2000);
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
await wait(intervalS * 1000);
|
|
294
|
+
} catch (err) {
|
|
295
|
+
if (stopped) return;
|
|
296
|
+
onError(`panel probe interrupted: ${err instanceof Error ? err.message : String(err)}`);
|
|
297
|
+
await wait(2000);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
void loop();
|
|
303
|
+
return () => {
|
|
304
|
+
stopped = true;
|
|
305
|
+
controller.abort();
|
|
306
|
+
};
|
|
307
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './client.ts';
|