repositories / pi-ext

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

Added
new 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],
9 + wifi: [
10 + { address: "192.168.1.44", family: "IPv4", internal: false } as never,
11 + ],
12 + });
14 + expect(address).toBe("192.168.1.44");
15 + });
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],
20 + });
22 + expect(address).toBe("127.0.0.1");
23 + });
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>");
30 + });
31 +});
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>.",
37 + retryable: true,
38 + });
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");
43 + });
44 +});

extensions/ui/errors.ts

Added
new file mode 100644
1 +export type UiErrorCode =
2 + | "server_bind_failed"
3 + | "no_lan_address"
4 + | "auth_failed"
5 + | "runtime_not_active"
6 + | "runtime_already_active"
7 + | "open_failed"
8 + | "qr_failed";
10 +export type UiError = Error & {
11 + code: UiErrorCode;
12 + action: string;
13 + retryable: boolean;
14 + cause?: unknown;
15 +};
17 +export function uiError(
18 + code: UiErrorCode,
19 + message: string,
20 + options: { action: string; retryable?: boolean; cause?: unknown },
21 +): UiError {
22 + const error = new Error(message) as UiError;
23 + error.name = "UiError";
24 + error.code = code;
25 + error.action = options.action;
26 + error.retryable = options.retryable ?? false;
27 + error.cause = options.cause;
28 + return error;
31 +export function humanError(error: UiError): string {
32 + return [
33 + `${error.code}: ${error.message}`,
34 + `Action: ${error.action}`,
35 + `Retryable: ${error.retryable ? "yes" : "no"}`,
36 + ].join("\n");
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

Added
new 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(),
7 +): string {
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;
29 + return 1;