Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
182f9b606d | ||
|
|
72e29731c2 | ||
|
|
aebe9dec3b | ||
|
|
bd901faf58 |
No files matched your search
@@ -25,6 +25,7 @@ import * as Effect from "effect/Effect";
|
|||||||
import * as FileSystem from "effect/FileSystem";
|
import * as FileSystem from "effect/FileSystem";
|
||||||
import * as Layer from "effect/Layer";
|
import * as Layer from "effect/Layer";
|
||||||
import * as Path from "effect/Path";
|
import * as Path from "effect/Path";
|
||||||
|
import * as Schema from "effect/Schema";
|
||||||
import { HttpClient } from "effect/unstable/http";
|
import { HttpClient } from "effect/unstable/http";
|
||||||
import { ChildProcessSpawner } from "effect/unstable/process";
|
import { ChildProcessSpawner } from "effect/unstable/process";
|
||||||
|
|
||||||
@@ -75,6 +76,8 @@ export interface DesktopSshEnvironmentLayerOptions {
|
|||||||
readonly resolveCliRunner?: Effect.Effect<RemoteT3RunnerOptions>;
|
readonly resolveCliRunner?: Effect.Effect<RemoteT3RunnerOptions>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isSshPasswordPromptError = Schema.is(SshPasswordPromptError);
|
||||||
|
|
||||||
function discoverDesktopSshHostsEffect(input?: { readonly homeDir?: string }) {
|
function discoverDesktopSshHostsEffect(input?: { readonly homeDir?: string }) {
|
||||||
return discoverSshHosts(input ?? {});
|
return discoverSshHosts(input ?? {});
|
||||||
}
|
}
|
||||||
@@ -83,7 +86,7 @@ export function isDesktopSshPasswordPromptCancellation(
|
|||||||
error: unknown,
|
error: unknown,
|
||||||
): error is SshPasswordPromptError {
|
): error is SshPasswordPromptError {
|
||||||
return (
|
return (
|
||||||
error instanceof SshPasswordPromptError &&
|
isSshPasswordPromptError(error) &&
|
||||||
DesktopSshPasswordPrompts.isDesktopSshPasswordPromptCancellation(error.cause)
|
DesktopSshPasswordPrompts.isDesktopSshPasswordPromptCancellation(error.cause)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,13 @@ import { assert, describe, it } from "@effect/vitest";
|
|||||||
import { SshHttpBridgeError } from "@t3tools/ssh/errors";
|
import { SshHttpBridgeError } from "@t3tools/ssh/errors";
|
||||||
import * as Effect from "effect/Effect";
|
import * as Effect from "effect/Effect";
|
||||||
import * as Layer from "effect/Layer";
|
import * as Layer from "effect/Layer";
|
||||||
|
import * as Schema from "effect/Schema";
|
||||||
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http";
|
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http";
|
||||||
|
|
||||||
import * as DesktopSshRemoteApi from "./DesktopSshRemoteApi.ts";
|
import * as DesktopSshRemoteApi from "./DesktopSshRemoteApi.ts";
|
||||||
|
|
||||||
|
const isSshHttpBridgeError = Schema.is(SshHttpBridgeError);
|
||||||
|
|
||||||
function jsonResponse(request: HttpClientRequest.HttpClientRequest, body: unknown, status = 200) {
|
function jsonResponse(request: HttpClientRequest.HttpClientRequest, body: unknown, status = 200) {
|
||||||
return HttpClientResponse.fromWeb(
|
return HttpClientResponse.fromWeb(
|
||||||
request,
|
request,
|
||||||
@@ -73,7 +76,7 @@ describe("DesktopSshRemoteApi", () => {
|
|||||||
|
|
||||||
assert.instanceOf(error, DesktopSshRemoteApi.DesktopSshRemoteApiError);
|
assert.instanceOf(error, DesktopSshRemoteApi.DesktopSshRemoteApiError);
|
||||||
assert.equal(error.operation, "fetch-environment-descriptor");
|
assert.equal(error.operation, "fetch-environment-descriptor");
|
||||||
assert.equal(error.cause instanceof SshHttpBridgeError, false);
|
assert.equal(isSshHttpBridgeError(error.cause), false);
|
||||||
}).pipe(Effect.provide(layer));
|
}).pipe(Effect.provide(layer));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
+53
-38
@@ -1,46 +1,61 @@
|
|||||||
import * as Data from "effect/Data";
|
import * as Schema from "effect/Schema";
|
||||||
|
|
||||||
export class SshHostDiscoveryError extends Data.TaggedError("SshHostDiscoveryError")<{
|
export class SshHostDiscoveryError extends Schema.TaggedErrorClass<SshHostDiscoveryError>()(
|
||||||
readonly message: string;
|
"SshHostDiscoveryError",
|
||||||
readonly cause: unknown;
|
{
|
||||||
}> {}
|
message: Schema.String,
|
||||||
|
cause: Schema.Defect,
|
||||||
|
},
|
||||||
|
) {}
|
||||||
|
|
||||||
export class SshInvalidTargetError extends Data.TaggedError("SshInvalidTargetError")<{
|
export class SshInvalidTargetError extends Schema.TaggedErrorClass<SshInvalidTargetError>()(
|
||||||
readonly message: string;
|
"SshInvalidTargetError",
|
||||||
}> {}
|
{
|
||||||
|
message: Schema.String,
|
||||||
|
},
|
||||||
|
) {}
|
||||||
|
|
||||||
export class SshCommandError extends Data.TaggedError("SshCommandError")<{
|
export class SshCommandError extends Schema.TaggedErrorClass<SshCommandError>()("SshCommandError", {
|
||||||
readonly message: string;
|
message: Schema.String,
|
||||||
readonly command: readonly string[];
|
command: Schema.Array(Schema.String),
|
||||||
readonly exitCode: number | null;
|
exitCode: Schema.NullOr(Schema.Number),
|
||||||
readonly stderr: string;
|
stderr: Schema.String,
|
||||||
readonly cause?: unknown;
|
cause: Schema.optional(Schema.Defect),
|
||||||
}> {}
|
}) {}
|
||||||
|
|
||||||
export class SshLaunchError extends Data.TaggedError("SshLaunchError")<{
|
export class SshLaunchError extends Schema.TaggedErrorClass<SshLaunchError>()("SshLaunchError", {
|
||||||
readonly message: string;
|
message: Schema.String,
|
||||||
readonly stdout: string;
|
stdout: Schema.String,
|
||||||
readonly cause?: unknown;
|
cause: Schema.optional(Schema.Defect),
|
||||||
}> {}
|
}) {}
|
||||||
|
|
||||||
export class SshPairingError extends Data.TaggedError("SshPairingError")<{
|
export class SshPairingError extends Schema.TaggedErrorClass<SshPairingError>()("SshPairingError", {
|
||||||
readonly message: string;
|
message: Schema.String,
|
||||||
readonly stdout: string;
|
stdout: Schema.String,
|
||||||
readonly cause?: unknown;
|
cause: Schema.optional(Schema.Defect),
|
||||||
}> {}
|
}) {}
|
||||||
|
|
||||||
export class SshHttpBridgeError extends Data.TaggedError("SshHttpBridgeError")<{
|
export class SshHttpBridgeError extends Schema.TaggedErrorClass<SshHttpBridgeError>()(
|
||||||
readonly message: string;
|
"SshHttpBridgeError",
|
||||||
readonly status?: number;
|
{
|
||||||
readonly cause?: unknown;
|
message: Schema.String,
|
||||||
}> {}
|
status: Schema.optional(Schema.Number),
|
||||||
|
cause: Schema.optional(Schema.Defect),
|
||||||
|
},
|
||||||
|
) {}
|
||||||
|
|
||||||
export class SshReadinessError extends Data.TaggedError("SshReadinessError")<{
|
export class SshReadinessError extends Schema.TaggedErrorClass<SshReadinessError>()(
|
||||||
readonly message: string;
|
"SshReadinessError",
|
||||||
readonly cause?: unknown;
|
{
|
||||||
}> {}
|
message: Schema.String,
|
||||||
|
cause: Schema.optional(Schema.Defect),
|
||||||
|
},
|
||||||
|
) {}
|
||||||
|
|
||||||
export class SshPasswordPromptError extends Data.TaggedError("SshPasswordPromptError")<{
|
export class SshPasswordPromptError extends Schema.TaggedErrorClass<SshPasswordPromptError>()(
|
||||||
readonly message: string;
|
"SshPasswordPromptError",
|
||||||
readonly cause?: unknown;
|
{
|
||||||
}> {}
|
message: Schema.String,
|
||||||
|
cause: Schema.optional(Schema.Defect),
|
||||||
|
},
|
||||||
|
) {}
|
||||||
@@ -167,6 +167,7 @@ const RemoteHttpError = Schema.Struct({
|
|||||||
const decodeRemoteLaunchResult = Schema.decodeEffect(fromLenientJson(RemoteLaunchResult));
|
const decodeRemoteLaunchResult = Schema.decodeEffect(fromLenientJson(RemoteLaunchResult));
|
||||||
const decodeRemotePairingResult = Schema.decodeEffect(fromLenientJson(RemotePairingResult));
|
const decodeRemotePairingResult = Schema.decodeEffect(fromLenientJson(RemotePairingResult));
|
||||||
const decodeRemoteHttpError = Schema.decodeEffect(Schema.fromJsonString(RemoteHttpError));
|
const decodeRemoteHttpError = Schema.decodeEffect(Schema.fromJsonString(RemoteHttpError));
|
||||||
|
const isSshReadinessError = Schema.is(SshReadinessError);
|
||||||
|
|
||||||
const decodeRemoteJsonOutput = <A, E>(
|
const decodeRemoteJsonOutput = <A, E>(
|
||||||
stdout: string,
|
stdout: string,
|
||||||
@@ -237,7 +238,7 @@ function applyScriptPlaceholders(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function describeReadinessCause(cause: unknown): unknown {
|
export function describeReadinessCause(cause: unknown): unknown {
|
||||||
if (cause instanceof SshReadinessError) {
|
if (isSshReadinessError(cause)) {
|
||||||
return {
|
return {
|
||||||
_tag: cause._tag,
|
_tag: cause._tag,
|
||||||
message: cause.message,
|
message: cause.message,
|
||||||
@@ -925,7 +926,7 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio
|
|||||||
});
|
});
|
||||||
}).pipe(
|
}).pipe(
|
||||||
Effect.mapError((cause) =>
|
Effect.mapError((cause) =>
|
||||||
cause instanceof SshReadinessError
|
isSshReadinessError(cause)
|
||||||
? cause
|
? cause
|
||||||
: new SshReadinessError({
|
: new SshReadinessError({
|
||||||
message: `Backend readiness probe failed at ${requestUrl}.`,
|
message: `Backend readiness probe failed at ${requestUrl}.`,
|
||||||
@@ -946,7 +947,7 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio
|
|||||||
|
|
||||||
const result = yield* readinessClient.execute(HttpClientRequest.get(requestUrl)).pipe(
|
const result = yield* readinessClient.execute(HttpClientRequest.get(requestUrl)).pipe(
|
||||||
Effect.mapError((cause) =>
|
Effect.mapError((cause) =>
|
||||||
cause instanceof SshReadinessError
|
isSshReadinessError(cause)
|
||||||
? cause
|
? cause
|
||||||
: new SshReadinessError({
|
: new SshReadinessError({
|
||||||
message: `Backend readiness probe failed at ${requestUrl}.`,
|
message: `Backend readiness probe failed at ${requestUrl}.`,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import * as Data from "effect/Data";
|
import * as Duration from "effect/Duration";
|
||||||
import * as Effect from "effect/Effect";
|
import * as Effect from "effect/Effect";
|
||||||
import * as Option from "effect/Option";
|
import * as Option from "effect/Option";
|
||||||
import * as Schema from "effect/Schema";
|
import * as Schema from "effect/Schema";
|
||||||
@@ -7,24 +7,36 @@ import { HttpClient, HttpClientRequest } from "effect/unstable/http";
|
|||||||
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
|
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
|
||||||
|
|
||||||
export const DEFAULT_TAILSCALE_SERVE_PORT = 443;
|
export const DEFAULT_TAILSCALE_SERVE_PORT = 443;
|
||||||
export const TAILSCALE_STATUS_TIMEOUT_MS = 1_500;
|
export const TAILSCALE_STATUS_TIMEOUT = Duration.millis(1_500);
|
||||||
export const TAILSCALE_SERVE_TIMEOUT_MS = 10_000;
|
export const TAILSCALE_SERVE_TIMEOUT = Duration.seconds(10);
|
||||||
export const TAILSCALE_PROBE_TIMEOUT_MS = 2_500;
|
export const TAILSCALE_PROBE_TIMEOUT = Duration.millis(2_500);
|
||||||
|
export const TAILSCALE_STATUS_TIMEOUT_MS = Duration.toMillis(TAILSCALE_STATUS_TIMEOUT);
|
||||||
|
export const TAILSCALE_SERVE_TIMEOUT_MS = Duration.toMillis(TAILSCALE_SERVE_TIMEOUT);
|
||||||
|
export const TAILSCALE_PROBE_TIMEOUT_MS = Duration.toMillis(TAILSCALE_PROBE_TIMEOUT);
|
||||||
|
|
||||||
export class TailscaleCommandError extends Data.TaggedError("TailscaleCommandError")<{
|
export class TailscaleCommandError extends Schema.TaggedErrorClass<TailscaleCommandError>()(
|
||||||
readonly command: readonly string[];
|
"TailscaleCommandError",
|
||||||
readonly message: string;
|
{
|
||||||
readonly exitCode: number | null;
|
command: Schema.Array(Schema.String),
|
||||||
readonly stderr: string;
|
message: Schema.String,
|
||||||
}> {}
|
exitCode: Schema.NullOr(Schema.Number),
|
||||||
|
stderr: Schema.String,
|
||||||
|
},
|
||||||
|
) {}
|
||||||
|
|
||||||
export class TailscaleStatusParseError extends Data.TaggedError("TailscaleStatusParseError")<{
|
export class TailscaleStatusParseError extends Schema.TaggedErrorClass<TailscaleStatusParseError>()(
|
||||||
readonly cause: unknown;
|
"TailscaleStatusParseError",
|
||||||
}> {}
|
{
|
||||||
|
cause: Schema.Defect,
|
||||||
|
},
|
||||||
|
) {}
|
||||||
|
|
||||||
export class TailscaleUnavailableError extends Data.TaggedError("TailscaleUnavailableError")<{
|
export class TailscaleUnavailableError extends Schema.TaggedErrorClass<TailscaleUnavailableError>()(
|
||||||
readonly reason: string;
|
"TailscaleUnavailableError",
|
||||||
}> {}
|
{
|
||||||
|
reason: Schema.String,
|
||||||
|
},
|
||||||
|
) {}
|
||||||
|
|
||||||
const TailscaleStatusSelf = Schema.Struct({
|
const TailscaleStatusSelf = Schema.Struct({
|
||||||
DNSName: Schema.optional(Schema.Unknown),
|
DNSName: Schema.optional(Schema.Unknown),
|
||||||
@@ -174,7 +186,7 @@ export const readTailscaleStatus: Effect.Effect<
|
|||||||
return yield* parseTailscaleStatus(stdout);
|
return yield* parseTailscaleStatus(stdout);
|
||||||
}).pipe(
|
}).pipe(
|
||||||
Effect.scoped,
|
Effect.scoped,
|
||||||
Effect.timeoutOption(TAILSCALE_STATUS_TIMEOUT_MS),
|
Effect.timeoutOption(TAILSCALE_STATUS_TIMEOUT),
|
||||||
Effect.flatMap((result) =>
|
Effect.flatMap((result) =>
|
||||||
Option.match(result, {
|
Option.match(result, {
|
||||||
onNone: () =>
|
onNone: () =>
|
||||||
@@ -206,7 +218,7 @@ const runTailscaleCommand = (
|
|||||||
readonly runMessage: string;
|
readonly runMessage: string;
|
||||||
readonly exitMessage: (exitCode: number) => string;
|
readonly exitMessage: (exitCode: number) => string;
|
||||||
readonly timeoutMessage: string;
|
readonly timeoutMessage: string;
|
||||||
readonly timeoutMs: number;
|
readonly timeout: Duration.Input;
|
||||||
},
|
},
|
||||||
): Effect.Effect<void, TailscaleCommandError, ChildProcessSpawner.ChildProcessSpawner> =>
|
): Effect.Effect<void, TailscaleCommandError, ChildProcessSpawner.ChildProcessSpawner> =>
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
@@ -243,7 +255,7 @@ const runTailscaleCommand = (
|
|||||||
}
|
}
|
||||||
}).pipe(
|
}).pipe(
|
||||||
Effect.scoped,
|
Effect.scoped,
|
||||||
Effect.timeoutOption(input.timeoutMs),
|
Effect.timeoutOption(input.timeout),
|
||||||
Effect.flatMap((result) =>
|
Effect.flatMap((result) =>
|
||||||
Option.match(result, {
|
Option.match(result, {
|
||||||
onNone: () => Effect.fail(tailscaleCommandError(args, input.timeoutMessage, null)),
|
onNone: () => Effect.fail(tailscaleCommandError(args, input.timeoutMessage, null)),
|
||||||
@@ -265,7 +277,7 @@ export const ensureTailscaleServe = (input: {
|
|||||||
runMessage: "Failed to run tailscale serve.",
|
runMessage: "Failed to run tailscale serve.",
|
||||||
exitMessage: (exitCode) => `Tailscale serve exited with code ${exitCode}.`,
|
exitMessage: (exitCode) => `Tailscale serve exited with code ${exitCode}.`,
|
||||||
timeoutMessage: "Tailscale serve timed out.",
|
timeoutMessage: "Tailscale serve timed out.",
|
||||||
timeoutMs: TAILSCALE_SERVE_TIMEOUT_MS,
|
timeout: TAILSCALE_SERVE_TIMEOUT,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -281,13 +293,13 @@ export const disableTailscaleServe = (
|
|||||||
runMessage: "Failed to run tailscale serve off.",
|
runMessage: "Failed to run tailscale serve off.",
|
||||||
exitMessage: (exitCode) => `Tailscale serve off exited with code ${exitCode}.`,
|
exitMessage: (exitCode) => `Tailscale serve off exited with code ${exitCode}.`,
|
||||||
timeoutMessage: "Tailscale serve off timed out.",
|
timeoutMessage: "Tailscale serve off timed out.",
|
||||||
timeoutMs: TAILSCALE_SERVE_TIMEOUT_MS,
|
timeout: TAILSCALE_SERVE_TIMEOUT,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
export const probeTailscaleHttpsEndpoint = (input: {
|
export const probeTailscaleHttpsEndpoint = (input: {
|
||||||
readonly baseUrl: string;
|
readonly baseUrl: string;
|
||||||
readonly timeoutMs?: number;
|
readonly timeout?: Duration.Input;
|
||||||
}): Effect.Effect<boolean, never, HttpClient.HttpClient> =>
|
}): Effect.Effect<boolean, never, HttpClient.HttpClient> =>
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
const client = yield* HttpClient.HttpClient;
|
const client = yield* HttpClient.HttpClient;
|
||||||
@@ -295,7 +307,7 @@ export const probeTailscaleHttpsEndpoint = (input: {
|
|||||||
const url = new URL("/.well-known/t3/environment", input.baseUrl);
|
const url = new URL("/.well-known/t3/environment", input.baseUrl);
|
||||||
const request = HttpClientRequest.get(url.toString());
|
const request = HttpClientRequest.get(url.toString());
|
||||||
return yield* client.execute(request);
|
return yield* client.execute(request);
|
||||||
}).pipe(Effect.timeoutOption(input.timeoutMs ?? TAILSCALE_PROBE_TIMEOUT_MS));
|
}).pipe(Effect.timeoutOption(input.timeout ?? TAILSCALE_PROBE_TIMEOUT));
|
||||||
|
|
||||||
return Option.match(response, {
|
return Option.match(response, {
|
||||||
onNone: () => false,
|
onNone: () => false,
|
||||||
|
|||||||
Reference in new issue
Block a user