pi-ext
bugabingas pi extensions
owned by bugabinga
Add ui network and error helpers
7175e82983d790b095a03936bf28aa0a0f7d1e23
Oliver Krylow <o.krylow@isp-insoft.de> ·
unsigned
Browse filesCompare with HEAD
Changed files (3)
extensions/ui/__tests__/server.test.ts
Addednew file mode 100644 1 +import { describe, expect, it }
from "vitest";
2 +import { humanError, redactToken, uiError }
from "../errors";
3 +import { chooseLanAddress, uiUrl }
from "../network";
5 +describe(
"ui network helpers", ()
=> {
6 + it(
"prefers non-internal IPv4 LAN addresses", ()
=> {
7 + const address
= chooseLanAddress({
8 + lo: [{ address:
"127.0.0.1", family:
"IPv4", internal:
true }
as never],
10 + { address:
"192.168.1.44", family:
"IPv4", internal:
false }
as never,
14 + expect(address).
toBe(
"192.168.1.44");
17 + it(
"falls back to 127.0.0.1 when no LAN address exists", ()
=> {
18 + const address
= chooseLanAddress({
19 + lo: [{ address:
"127.0.0.1", family:
"IPv4", internal:
true }
as never],
22 + expect(address).
toBe(
"127.0.0.1");
25 + it(
"builds token URLs and redacts tokens in diagnostics", ()
=> {
26 + const url
= uiUrl(
"10.0.0.9",
54321,
"abc");
28 + expect(url).
toBe(
"http://10.0.0.9:54321/?token=abc");
29 + expect(
redactToken(url)).
toBe(
"http://10.0.0.9:54321/?token=<redacted>");
33 +describe(
"ui errors", ()
=> {
34 + it(
"formats human-actionable errors", ()
=> {
35 + const err
= uiError(
"server_bind_failed",
"Could not bind LAN server", {
36 + action:
"Pick another port with /ui --port <port>.",
40 + expect(
humanError(err)).
toContain(
"Could not bind LAN server");
41 + expect(
humanError(err)).
toContain(
"Pick another port");
42 + expect(
humanError(err)).
toContain(
"Retryable: yes");
extensions/ui/errors.ts
Addednew file mode 100644 1 +export type UiErrorCode = 2 + | "server_bind_failed" 5 + | "runtime_not_active" 6 + | "runtime_already_active" 10 +export type UiError = Error & {
17 +export function uiError(
20 + options
: { action
: string; retryable
?: boolean; cause
?: unknown },
22 + const error
= new Error(message)
as UiError;
23 + error.
name = "UiError";
25 + error.
action = options.
action;
26 + error.retryable
= options.retryable
?? false;
27 + error.cause
= options.cause;
31 +export function humanError(error
: UiError)
: string {
33 + `${error.code}: ${error.message}`,
34 + `Action: ${error.action}`,
35 + `Retryable: ${error.retryable ? "yes" : "no"}`,
39 +export function errorMessage(error
: unknown)
: string {
40 + return error
instanceof Error ? error.message
: String(error);
43 +export function redactToken(value
: string)
: string {
44 + return value.
replace(
/([?&]token=)[^&#\s]+/g,
"$1<redacted>");
extensions/ui/network.ts
Addednew file mode 100644 1 +import { networkInterfaces, type NetworkInterfaceInfo }
from "node:os";
3 +export type InterfaceMap = Record<
string,
NetworkInterfaceInfo[]
| undefined>;
5 +export function chooseLanAddress(
6 + interfaces
: InterfaceMap = networkInterfaces(),
8 + const candidates
= Object.
values(interfaces)
9 + .
flatMap((items)
=> items
?? [])
10 + .
filter((item)
=> item.family
=== "IPv4" && !item.internal)
11 + .
map((item)
=> item.address)
12 + .
sort((a, b)
=> scoreAddress(b)
- scoreAddress(a));
14 + return candidates[
0]
?? "127.0.0.1";
17 +export function listenHostFor(address
: string)
: string {
18 + return address
=== "127.0.0.1" ? "127.0.0.1" : "0.0.0.0";
21 +export function uiUrl(address
: string, port
: number, token
: string)
: string {
22 + return `http://${address}:${port}/?token=${encodeURIComponent(token)}`;
25 +function scoreAddress(address
: string)
: number {
26 + if (address.
startsWith(
"192.168."))
return 30;
27 + if (address.
startsWith(
"10."))
return 20;
28 + if (
/^172\.(1[6-9]|2\d|3[01])\./.
test(address))
return 10;