@howaboua/pi-browser 0.0.1
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 +9 -0
- package/LICENSE +22 -0
- package/README.md +47 -0
- package/biome.json +31 -0
- package/changelog.ts +337 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +82 -0
- package/dist/remote-worker.d.ts +2 -0
- package/dist/remote-worker.js +3005 -0
- package/dist/src/browser/artifacts.d.ts +13 -0
- package/dist/src/browser/artifacts.js +133 -0
- package/dist/src/browser/bounds.d.ts +4 -0
- package/dist/src/browser/bounds.js +129 -0
- package/dist/src/browser/config.d.ts +11 -0
- package/dist/src/browser/config.js +106 -0
- package/dist/src/browser/help.d.ts +1 -0
- package/dist/src/browser/help.js +27 -0
- package/dist/src/browser/launcher.d.ts +8 -0
- package/dist/src/browser/launcher.js +169 -0
- package/dist/src/browser/operation-executor.d.ts +9 -0
- package/dist/src/browser/operation-executor.js +112 -0
- package/dist/src/browser/operation.d.ts +92 -0
- package/dist/src/browser/operation.js +20 -0
- package/dist/src/browser/parse-operation.d.ts +6 -0
- package/dist/src/browser/parse-operation.js +248 -0
- package/dist/src/browser/remote-helper.d.ts +3 -0
- package/dist/src/browser/remote-helper.js +114 -0
- package/dist/src/browser/remote-process.d.ts +7 -0
- package/dist/src/browser/remote-process.js +78 -0
- package/dist/src/browser/remote.d.ts +4 -0
- package/dist/src/browser/remote.js +73 -0
- package/dist/src/browser/request.d.ts +8 -0
- package/dist/src/browser/request.js +80 -0
- package/dist/src/browser/routes.d.ts +16 -0
- package/dist/src/browser/routes.js +40 -0
- package/dist/src/browser/runtime.d.ts +17 -0
- package/dist/src/browser/runtime.js +61 -0
- package/dist/src/browser/settings.d.ts +2 -0
- package/dist/src/browser/settings.js +164 -0
- package/dist/src/browser/worker-server.d.ts +2 -0
- package/dist/src/browser/worker-server.js +241 -0
- package/dist/src/browser-parameters.d.ts +42 -0
- package/dist/src/browser-parameters.js +113 -0
- package/dist/src/browser-tool.d.ts +48 -0
- package/dist/src/browser-tool.js +71 -0
- package/dist/src/cdp/actions/click.d.ts +4 -0
- package/dist/src/cdp/actions/click.js +56 -0
- package/dist/src/cdp/actions/element.d.ts +22 -0
- package/dist/src/cdp/actions/element.js +126 -0
- package/dist/src/cdp/actions/page.d.ts +7 -0
- package/dist/src/cdp/actions/page.js +127 -0
- package/dist/src/cdp/actions/screenshot.d.ts +10 -0
- package/dist/src/cdp/actions/screenshot.js +96 -0
- package/dist/src/cdp/actions/selector.d.ts +2 -0
- package/dist/src/cdp/actions/selector.js +41 -0
- package/dist/src/cdp/actions/type.d.ts +3 -0
- package/dist/src/cdp/actions/type.js +138 -0
- package/dist/src/cdp/client.d.ts +20 -0
- package/dist/src/cdp/client.js +248 -0
- package/dist/src/cdp/discovery.d.ts +7 -0
- package/dist/src/cdp/discovery.js +153 -0
- package/dist/src/cdp/evaluate.d.ts +3 -0
- package/dist/src/cdp/evaluate.js +31 -0
- package/dist/src/cdp/pages.d.ts +3 -0
- package/dist/src/cdp/pages.js +40 -0
- package/dist/src/cdp/serial.d.ts +1 -0
- package/dist/src/cdp/serial.js +18 -0
- package/dist/src/cdp/session.d.ts +25 -0
- package/dist/src/cdp/session.js +246 -0
- package/dist/src/cdp/snapshot-contract.d.ts +32 -0
- package/dist/src/cdp/snapshot-contract.js +23 -0
- package/dist/src/cdp/snapshot.d.ts +3 -0
- package/dist/src/cdp/snapshot.js +205 -0
- package/dist/src/cdp/types.d.ts +20 -0
- package/dist/src/cdp/types.js +14 -0
- package/dist/src/cdp/url.d.ts +1 -0
- package/dist/src/cdp/url.js +12 -0
- package/index.ts +115 -0
- package/package.json +93 -0
- package/remote-worker.ts +58 -0
- package/src/browser/artifacts.ts +178 -0
- package/src/browser/bounds.ts +159 -0
- package/src/browser/config.ts +162 -0
- package/src/browser/help.ts +30 -0
- package/src/browser/launcher.ts +208 -0
- package/src/browser/operation-executor.ts +251 -0
- package/src/browser/operation.ts +105 -0
- package/src/browser/parse-operation.ts +272 -0
- package/src/browser/remote-helper.ts +137 -0
- package/src/browser/remote-process.ts +85 -0
- package/src/browser/remote.ts +120 -0
- package/src/browser/request.ts +95 -0
- package/src/browser/routes.ts +69 -0
- package/src/browser/runtime.ts +84 -0
- package/src/browser/settings.ts +219 -0
- package/src/browser/worker-server.ts +274 -0
- package/src/browser-parameters.ts +118 -0
- package/src/browser-tool.ts +88 -0
- package/src/cdp/actions/click.ts +126 -0
- package/src/cdp/actions/element.ts +234 -0
- package/src/cdp/actions/page.ts +219 -0
- package/src/cdp/actions/screenshot.ts +176 -0
- package/src/cdp/actions/selector.ts +63 -0
- package/src/cdp/actions/type.ts +224 -0
- package/src/cdp/client.ts +286 -0
- package/src/cdp/discovery.ts +198 -0
- package/src/cdp/evaluate.ts +55 -0
- package/src/cdp/pages.ts +60 -0
- package/src/cdp/serial.ts +23 -0
- package/src/cdp/session.ts +345 -0
- package/src/cdp/snapshot-contract.ts +63 -0
- package/src/cdp/snapshot.ts +273 -0
- package/src/cdp/types.ts +49 -0
- package/src/cdp/url.ts +11 -0
- package/tsconfig.json +7 -0
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { BrowserOperation } from "./operation.js";
|
|
2
|
+
import type { BrowserRoute } from "./routes.js";
|
|
3
|
+
export { remoteNodeCommand } from "./remote-helper.js";
|
|
4
|
+
export declare function executeRemoteBrowser(route: BrowserRoute, operations: BrowserOperation[], signal?: AbortSignal): Promise<Record<string, unknown>>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { mkdir, rm } from "node:fs/promises";
|
|
2
|
+
import { basename, join } from "node:path";
|
|
3
|
+
import { artifactDirectory } from "./artifacts.js";
|
|
4
|
+
import { ensureRemoteHelper, remoteNodeCommand } from "./remote-helper.js";
|
|
5
|
+
import { runProgram } from "./remote-process.js";
|
|
6
|
+
const SAFE_REMOTE_FILE = /^\/[A-Za-z0-9_./-]+$/;
|
|
7
|
+
export { remoteNodeCommand } from "./remote-helper.js";
|
|
8
|
+
function isRecord(value) {
|
|
9
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
10
|
+
}
|
|
11
|
+
async function copyRemoteArtifact(host, remoteFile, signal) {
|
|
12
|
+
if (!SAFE_REMOTE_FILE.test(remoteFile)) {
|
|
13
|
+
throw new Error(`remote browser returned an unsafe artifact path: ${remoteFile}`);
|
|
14
|
+
}
|
|
15
|
+
await mkdir(artifactDirectory(), { recursive: true, mode: 0o700 });
|
|
16
|
+
const localFile = join(artifactDirectory(), `${host}-${basename(remoteFile)}`);
|
|
17
|
+
const copied = await runProgram("scp", ["-q", `${host}:${remoteFile}`, localFile], undefined, signal);
|
|
18
|
+
if (copied.code !== 0) {
|
|
19
|
+
throw new Error(copied.stderr || copied.stdout || "could not copy remote screenshot");
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const removed = await runProgram("ssh", [host, `/usr/bin/rm -- ${remoteFile}`], undefined, signal);
|
|
23
|
+
if (removed.code !== 0) {
|
|
24
|
+
throw new Error(removed.stderr ||
|
|
25
|
+
removed.stdout ||
|
|
26
|
+
"copied screenshot but could not remove remote artifact");
|
|
27
|
+
}
|
|
28
|
+
return localFile;
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
await rm(localFile, { force: true });
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async function localizeScreenshots(host, operations, result, signal) {
|
|
36
|
+
for (const [index, operation] of operations.entries()) {
|
|
37
|
+
if (operation.action !== "screenshot")
|
|
38
|
+
continue;
|
|
39
|
+
const item = operations.length === 1
|
|
40
|
+
? result
|
|
41
|
+
: Array.isArray(result["results"])
|
|
42
|
+
? result["results"][index]
|
|
43
|
+
: undefined;
|
|
44
|
+
if (!isRecord(item) || typeof item["file"] !== "string") {
|
|
45
|
+
throw new Error(`remote screenshot result ${index} has no file path`);
|
|
46
|
+
}
|
|
47
|
+
item["file"] = await copyRemoteArtifact(host, item["file"], signal);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export async function executeRemoteBrowser(route, operations, signal) {
|
|
51
|
+
if (!route.remote) {
|
|
52
|
+
throw new Error(`Browser host ${route.name} has no remote command`);
|
|
53
|
+
}
|
|
54
|
+
await ensureRemoteHelper(route.name, route.remote, signal);
|
|
55
|
+
const executed = await runProgram("ssh", [route.name, remoteNodeCommand(route.remote)], JSON.stringify({ operations }), signal);
|
|
56
|
+
if (executed.code !== 0) {
|
|
57
|
+
throw new Error(executed.stderr ||
|
|
58
|
+
executed.stdout ||
|
|
59
|
+
`remote browser exited with code ${executed.code}`);
|
|
60
|
+
}
|
|
61
|
+
let value;
|
|
62
|
+
try {
|
|
63
|
+
value = JSON.parse(executed.stdout);
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
throw new Error(`remote browser returned invalid JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
67
|
+
}
|
|
68
|
+
if (!isRecord(value)) {
|
|
69
|
+
throw new Error("remote browser result is not an object");
|
|
70
|
+
}
|
|
71
|
+
await localizeScreenshots(route.name, operations, value, signal);
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { OPERATION_ORDER } from "./operation.js";
|
|
2
|
+
import { isRecordValue, parseActionRequest } from "./parse-operation.js";
|
|
3
|
+
const BATCH_FIELDS = new Set(["host", "response_length", ...OPERATION_ORDER]);
|
|
4
|
+
function requestHost(value) {
|
|
5
|
+
if (value === undefined)
|
|
6
|
+
return undefined;
|
|
7
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
8
|
+
throw new Error("host must be a non-empty string");
|
|
9
|
+
}
|
|
10
|
+
return value.trim();
|
|
11
|
+
}
|
|
12
|
+
export function parseBrowserRequest(input) {
|
|
13
|
+
let value = input;
|
|
14
|
+
if (typeof input === "string") {
|
|
15
|
+
const text = input.trim();
|
|
16
|
+
if (text === "help")
|
|
17
|
+
return { help: true };
|
|
18
|
+
try {
|
|
19
|
+
value = JSON.parse(text);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
throw new Error(`input must be "help" or valid JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (!isRecordValue(value)) {
|
|
26
|
+
throw new Error("input must be a JSON object");
|
|
27
|
+
}
|
|
28
|
+
if (Object.hasOwn(value, "action")) {
|
|
29
|
+
const host = requestHost(value["host"]);
|
|
30
|
+
const { host: _host, ...actionValue } = value;
|
|
31
|
+
const operation = parseActionRequest(actionValue);
|
|
32
|
+
return operation.action === "help"
|
|
33
|
+
? { help: true }
|
|
34
|
+
: { operations: [operation], ...(host ? { host } : {}) };
|
|
35
|
+
}
|
|
36
|
+
const unknown = Object.keys(value).filter((key) => !BATCH_FIELDS.has(key));
|
|
37
|
+
if (unknown.length > 0) {
|
|
38
|
+
throw new Error(`unknown browser field(s): ${unknown.join(", ")}`);
|
|
39
|
+
}
|
|
40
|
+
const host = requestHost(value["host"]);
|
|
41
|
+
const responseLength = value["response_length"] ?? "medium";
|
|
42
|
+
if (responseLength !== "short" &&
|
|
43
|
+
responseLength !== "medium" &&
|
|
44
|
+
responseLength !== "long") {
|
|
45
|
+
throw new Error("response_length must be one of: short, medium, long");
|
|
46
|
+
}
|
|
47
|
+
const operations = [];
|
|
48
|
+
for (const action of OPERATION_ORDER) {
|
|
49
|
+
const items = value[action];
|
|
50
|
+
if (items === undefined)
|
|
51
|
+
continue;
|
|
52
|
+
if (!Array.isArray(items) || items.length === 0) {
|
|
53
|
+
throw new Error(`${action} must be a non-empty array`);
|
|
54
|
+
}
|
|
55
|
+
for (const [index, item] of items.entries()) {
|
|
56
|
+
if (!isRecordValue(item)) {
|
|
57
|
+
throw new Error(`${action}[${index}] must be an object`);
|
|
58
|
+
}
|
|
59
|
+
const reserved = ["action", "host", "response_length"].filter((key) => Object.hasOwn(item, key));
|
|
60
|
+
if (reserved.length > 0) {
|
|
61
|
+
throw new Error(`${action}[${index}] has top-level field(s): ${reserved.join(", ")}`);
|
|
62
|
+
}
|
|
63
|
+
const parsed = parseActionRequest({
|
|
64
|
+
action,
|
|
65
|
+
...item,
|
|
66
|
+
...(action === "open" || action === "find"
|
|
67
|
+
? { response_length: responseLength }
|
|
68
|
+
: {}),
|
|
69
|
+
});
|
|
70
|
+
if (parsed.action === "help") {
|
|
71
|
+
throw new Error("help is not a batch operation");
|
|
72
|
+
}
|
|
73
|
+
operations.push(parsed);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (operations.length === 0) {
|
|
77
|
+
throw new Error(`provide at least one operation: ${OPERATION_ORDER.join(", ")}`);
|
|
78
|
+
}
|
|
79
|
+
return { operations, ...(host ? { host } : {}) };
|
|
80
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface BrowserRemoteCommand {
|
|
2
|
+
nodePath: string;
|
|
3
|
+
}
|
|
4
|
+
export interface BrowserRoute {
|
|
5
|
+
local: boolean;
|
|
6
|
+
name: string;
|
|
7
|
+
remote?: BrowserRemoteCommand;
|
|
8
|
+
}
|
|
9
|
+
export declare class BrowserRoutes {
|
|
10
|
+
readonly names: readonly string[];
|
|
11
|
+
private readonly routes;
|
|
12
|
+
constructor(routes?: ReadonlyMap<string, BrowserRoute>);
|
|
13
|
+
resolve(name: string): BrowserRoute;
|
|
14
|
+
}
|
|
15
|
+
export declare function parseBrowserRoutes(value: unknown, currentHostname?: string): BrowserRoutes;
|
|
16
|
+
export declare function loadBrowserRoutes(path?: string, currentHostname?: string): BrowserRoutes;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { hostname } from "node:os";
|
|
2
|
+
import { normalizeBrowserRouteConfig, readBrowserRouteConfig, } from "./config.js";
|
|
3
|
+
export class BrowserRoutes {
|
|
4
|
+
names;
|
|
5
|
+
routes;
|
|
6
|
+
constructor(routes = new Map()) {
|
|
7
|
+
this.routes = routes;
|
|
8
|
+
this.names = [...routes.keys()];
|
|
9
|
+
}
|
|
10
|
+
resolve(name) {
|
|
11
|
+
const route = this.routes.get(name);
|
|
12
|
+
if (route)
|
|
13
|
+
return route;
|
|
14
|
+
if (this.names.length === 0) {
|
|
15
|
+
throw new Error("Browser host routing is disabled");
|
|
16
|
+
}
|
|
17
|
+
throw new Error(`browser host must be one of: ${this.names.join(", ")}`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function routesFromConfig(config, currentHostname) {
|
|
21
|
+
const shortHostname = currentHostname.split(".")[0] ?? currentHostname;
|
|
22
|
+
const currentName = config.aliases[currentHostname] ??
|
|
23
|
+
config.aliases[shortHostname] ??
|
|
24
|
+
shortHostname;
|
|
25
|
+
const remote = {
|
|
26
|
+
nodePath: config.remoteNodePath,
|
|
27
|
+
};
|
|
28
|
+
const routes = new Map();
|
|
29
|
+
for (const name of config.hosts) {
|
|
30
|
+
const local = currentName === name;
|
|
31
|
+
routes.set(name, local ? { name, local } : { name, local, remote });
|
|
32
|
+
}
|
|
33
|
+
return new BrowserRoutes(routes);
|
|
34
|
+
}
|
|
35
|
+
export function parseBrowserRoutes(value, currentHostname = hostname()) {
|
|
36
|
+
return routesFromConfig(normalizeBrowserRouteConfig(value), currentHostname);
|
|
37
|
+
}
|
|
38
|
+
export function loadBrowserRoutes(path, currentHostname = hostname()) {
|
|
39
|
+
return routesFromConfig(readBrowserRouteConfig(path), currentHostname);
|
|
40
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { BrowserOperation } from "./operation.js";
|
|
2
|
+
import type { BrowserRequest } from "./request.js";
|
|
3
|
+
import { BrowserRoutes } from "./routes.js";
|
|
4
|
+
export interface BrowserExecutionOptions {
|
|
5
|
+
signal?: AbortSignal | undefined;
|
|
6
|
+
onOperation?(operation: BrowserOperation, index: number, total: number): void;
|
|
7
|
+
}
|
|
8
|
+
export declare class BrowserRuntime {
|
|
9
|
+
readonly hosts: readonly string[];
|
|
10
|
+
private readonly cdp;
|
|
11
|
+
private readonly operations;
|
|
12
|
+
private readonly routes;
|
|
13
|
+
constructor(routes?: BrowserRoutes);
|
|
14
|
+
execute(request: BrowserRequest, options?: BrowserExecutionOptions): Promise<Record<string, unknown>>;
|
|
15
|
+
close(): void;
|
|
16
|
+
private executeLocal;
|
|
17
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { BrowserCdpSession } from "../cdp/session.js";
|
|
2
|
+
import { pruneArtifacts } from "./artifacts.js";
|
|
3
|
+
import { browserHelp } from "./help.js";
|
|
4
|
+
import { BrowserOperationExecutor } from "./operation-executor.js";
|
|
5
|
+
import { executeRemoteBrowser } from "./remote.js";
|
|
6
|
+
import { BrowserRoutes, loadBrowserRoutes } from "./routes.js";
|
|
7
|
+
export class BrowserRuntime {
|
|
8
|
+
hosts;
|
|
9
|
+
cdp = new BrowserCdpSession();
|
|
10
|
+
operations = new BrowserOperationExecutor(this.cdp);
|
|
11
|
+
routes;
|
|
12
|
+
constructor(routes = loadBrowserRoutes()) {
|
|
13
|
+
this.routes = routes;
|
|
14
|
+
this.hosts = routes.names;
|
|
15
|
+
}
|
|
16
|
+
async execute(request, options = {}) {
|
|
17
|
+
options.signal?.throwIfAborted();
|
|
18
|
+
await pruneArtifacts();
|
|
19
|
+
if ("help" in request)
|
|
20
|
+
return browserHelp(this.hosts);
|
|
21
|
+
if (request.host) {
|
|
22
|
+
const route = this.routes.resolve(request.host);
|
|
23
|
+
if (!route.local) {
|
|
24
|
+
const first = request.operations[0];
|
|
25
|
+
if (first) {
|
|
26
|
+
options.onOperation?.(first, 0, request.operations.length);
|
|
27
|
+
}
|
|
28
|
+
const result = await executeRemoteBrowser(route, request.operations, options.signal);
|
|
29
|
+
return { host: request.host, ...result };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const result = await this.executeLocal(request.operations, options);
|
|
33
|
+
return request.host ? { host: request.host, ...result } : result;
|
|
34
|
+
}
|
|
35
|
+
close() {
|
|
36
|
+
this.cdp.close();
|
|
37
|
+
}
|
|
38
|
+
async executeLocal(operations, options) {
|
|
39
|
+
const results = [];
|
|
40
|
+
for (const [index, operation] of operations.entries()) {
|
|
41
|
+
try {
|
|
42
|
+
options.signal?.throwIfAborted();
|
|
43
|
+
options.onOperation?.(operation, index, operations.length);
|
|
44
|
+
results.push(await this.operations.execute(operation, options.signal));
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
throw new Error(`batch failed at ${operation.action}[${index}] after ${results.length} completed operation(s): ${error instanceof Error ? error.message : String(error)}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const first = results[0];
|
|
51
|
+
if (results.length === 1 && first)
|
|
52
|
+
return first;
|
|
53
|
+
return {
|
|
54
|
+
results: results.map((result, index) => ({
|
|
55
|
+
operation: operations[index]?.action ?? "unknown",
|
|
56
|
+
index,
|
|
57
|
+
...result,
|
|
58
|
+
})),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { hostname } from "node:os";
|
|
2
|
+
import { browserConfigPath, defaultBrowserRouteConfig, normalizeBrowserHostName, normalizeBrowserRouteConfig, readBrowserRouteConfig, writeBrowserRouteConfig, } from "./config.js";
|
|
3
|
+
const BACK = "Back";
|
|
4
|
+
const CANCEL = "Cancel";
|
|
5
|
+
const SAVE = "Save and reload";
|
|
6
|
+
function currentMachineRoute(config) {
|
|
7
|
+
const full = hostname();
|
|
8
|
+
const short = full.split(".")[0] ?? full;
|
|
9
|
+
return (config.aliases[full] ??
|
|
10
|
+
config.aliases[short] ??
|
|
11
|
+
(config.hosts.includes(short) ? short : undefined));
|
|
12
|
+
}
|
|
13
|
+
function setCurrentMachineRoute(config, name) {
|
|
14
|
+
const full = hostname();
|
|
15
|
+
const short = full.split(".")[0] ?? full;
|
|
16
|
+
const aliases = { ...config.aliases };
|
|
17
|
+
delete aliases[full];
|
|
18
|
+
delete aliases[short];
|
|
19
|
+
if (name && name !== short)
|
|
20
|
+
aliases[short] = name;
|
|
21
|
+
return { ...config, aliases };
|
|
22
|
+
}
|
|
23
|
+
function compactPath(value) {
|
|
24
|
+
return value.length <= 48 ? value : `…${value.slice(-47)}`;
|
|
25
|
+
}
|
|
26
|
+
async function manageHosts(ctx, initial) {
|
|
27
|
+
let config = initial;
|
|
28
|
+
while (true) {
|
|
29
|
+
const removals = new Map(config.hosts.map((host) => [`Remove · ${host}`, host]));
|
|
30
|
+
const selected = await ctx.ui.select("Browser hosts", [
|
|
31
|
+
"Add SSH host",
|
|
32
|
+
...removals.keys(),
|
|
33
|
+
BACK,
|
|
34
|
+
]);
|
|
35
|
+
if (!selected || selected === BACK)
|
|
36
|
+
return config;
|
|
37
|
+
if (selected === "Add SSH host") {
|
|
38
|
+
const input = (await ctx.ui.input("SSH host name", "laptop"))?.trim();
|
|
39
|
+
if (!input)
|
|
40
|
+
continue;
|
|
41
|
+
try {
|
|
42
|
+
const host = normalizeBrowserHostName(input, "SSH host name");
|
|
43
|
+
if (config.hosts.includes(host)) {
|
|
44
|
+
ctx.ui.notify(`Browser host ${host} already exists`, "warning");
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
config = { ...config, hosts: [...config.hosts, host] };
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
ctx.ui.notify(error instanceof Error ? error.message : String(error), "error");
|
|
51
|
+
}
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const host = removals.get(selected);
|
|
55
|
+
if (!host)
|
|
56
|
+
continue;
|
|
57
|
+
if (!(await ctx.ui.confirm("Remove browser host", `Remove ${host} from browser routing?`))) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const aliases = Object.fromEntries(Object.entries(config.aliases).filter(([, target]) => target !== host));
|
|
61
|
+
config = {
|
|
62
|
+
...config,
|
|
63
|
+
aliases,
|
|
64
|
+
hosts: config.hosts.filter((candidate) => candidate !== host),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async function manageAdvanced(ctx, initial) {
|
|
69
|
+
let config = initial;
|
|
70
|
+
while (true) {
|
|
71
|
+
const node = `Remote Node · ${compactPath(config.remoteNodePath)}`;
|
|
72
|
+
const selected = await ctx.ui.select("Advanced browser routing", [
|
|
73
|
+
node,
|
|
74
|
+
BACK,
|
|
75
|
+
]);
|
|
76
|
+
if (!selected || selected === BACK)
|
|
77
|
+
return config;
|
|
78
|
+
const value = (await ctx.ui.input("Remote Node path", config.remoteNodePath))?.trim();
|
|
79
|
+
if (!value)
|
|
80
|
+
continue;
|
|
81
|
+
try {
|
|
82
|
+
config = normalizeBrowserRouteConfig({
|
|
83
|
+
...config,
|
|
84
|
+
remoteNodePath: value,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
ctx.ui.notify(error instanceof Error ? error.message : String(error), "error");
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async function initialConfig(ctx) {
|
|
93
|
+
try {
|
|
94
|
+
return readBrowserRouteConfig();
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
98
|
+
const reset = await ctx.ui.confirm("Invalid browser settings", `${message}\n\nReset them?`);
|
|
99
|
+
return reset ? defaultBrowserRouteConfig() : undefined;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async function openBrowserSettings(ctx) {
|
|
103
|
+
let config = await initialConfig(ctx);
|
|
104
|
+
if (!config)
|
|
105
|
+
return;
|
|
106
|
+
while (true) {
|
|
107
|
+
const hosts = `Hosts · ${config.hosts.length ? config.hosts.join(", ") : "local only"}`;
|
|
108
|
+
const machine = `This machine · ${currentMachineRoute(config) ?? "unnamed local"}`;
|
|
109
|
+
const selected = await ctx.ui.select("Browser settings", [
|
|
110
|
+
hosts,
|
|
111
|
+
machine,
|
|
112
|
+
"Advanced",
|
|
113
|
+
SAVE,
|
|
114
|
+
CANCEL,
|
|
115
|
+
]);
|
|
116
|
+
if (!selected || selected === CANCEL)
|
|
117
|
+
return;
|
|
118
|
+
if (selected === hosts) {
|
|
119
|
+
config = await manageHosts(ctx, config);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (selected === machine) {
|
|
123
|
+
const route = await ctx.ui.select("This machine", [
|
|
124
|
+
"Unnamed local browser",
|
|
125
|
+
...config.hosts,
|
|
126
|
+
]);
|
|
127
|
+
if (route) {
|
|
128
|
+
config = setCurrentMachineRoute(config, route === "Unnamed local browser" ? undefined : route);
|
|
129
|
+
}
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (selected === "Advanced") {
|
|
133
|
+
config = await manageAdvanced(ctx, config);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
writeBrowserRouteConfig(config);
|
|
138
|
+
ctx.ui.notify("Browser settings saved; reloading Pi", "info");
|
|
139
|
+
await ctx.reload();
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
ctx.ui.notify(`Could not save browser settings: ${error instanceof Error ? error.message : String(error)}`, "error");
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
export function registerBrowserCommand(pi) {
|
|
148
|
+
pi.registerCommand("browser", {
|
|
149
|
+
description: "Configure browser host routing",
|
|
150
|
+
handler: async (_args, ctx) => {
|
|
151
|
+
if (!ctx.hasUI || ctx.mode !== "tui") {
|
|
152
|
+
try {
|
|
153
|
+
const config = readBrowserRouteConfig();
|
|
154
|
+
ctx.ui.notify(`Browser: ${config.hosts.length ? config.hosts.join(", ") : "local only"}\nConfig: ${browserConfigPath()}`, "info");
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
ctx.ui.notify(error instanceof Error ? error.message : String(error), "error");
|
|
158
|
+
}
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
await openBrowserSettings(ctx);
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
}
|