Skip to content
Source
Sign inSign up
Open History to see the last commit

deviceHostConnectionChecks.ts

68 lines2.1 KB

1import {
2 type DeviceHostSummary,
3 type DevicePlatformAvailability,
4 type EnvironmentId,
5 SshDeviceHostConfig,
6} from "@t3tools/contracts";
7import * as Schema from "effect/Schema";
8​
9export interface DeviceHostCheckTarget {
10 environmentId: EnvironmentId;
11 label: string;
12 connected: boolean;
14export type DeviceHostCheck =
15 | { status: "pending" }
16 | { status: "local" }
17 | {
18 status: "connected";
19 platforms: ReadonlyArray<DevicePlatformAvailability>;
20 tools?: DeviceHostSummary["tools"];
21 }
22 | { status: "failed"; error: string };
23​
24const decodeDeviceHostDraft = Schema.decodeUnknownOption(SshDeviceHostConfig);
25​
26export function parseDeviceHostDraft(host: SshDeviceHostConfig) {
27 const { identityFile, ...rest } = host;
28 return decodeDeviceHostDraft({
29 ...rest,
30 ...(identityFile?.trim() ? { identityFile: identityFile.trim() } : {}),
31 });
33​
34export function deviceHostConnectionKey(host: SshDeviceHostConfig) {
35 return JSON.stringify([host.target.trim(), host.port, host.identityFile?.trim() || undefined]);
37​
38export async function checkDeviceHostConnections(
39 targets: ReadonlyArray<DeviceHostCheckTarget>,
40 host: SshDeviceHostConfig,
41 probe: (environmentId: EnvironmentId, host: SshDeviceHostConfig) => Promise<DeviceHostSummary>,
42 report: (environmentId: EnvironmentId, result: DeviceHostCheck) => void,
43) {
44 await Promise.all(
45 targets.map(async (target) => {
46 report(target.environmentId, { status: "pending" });
47 try {
48 if (!target.connected) throw new Error("Environment disconnected");
49 const result = await probe(target.environmentId, host);
50 report(
51 target.environmentId,
52 result.kind === "local"
53 ? { status: "local" }
54 : {
55 status: "connected",
56 platforms: result.platforms,
57 ...(result.tools ? { tools: result.tools } : {}),
58 },
59 );
60 } catch (error) {
61 report(target.environmentId, {
62 status: "failed",
63 error: error instanceof Error ? error.message : String(error),
64 });
65 }
66 }),
67 );