[codex] Structure browser target resolution errors (#3327)
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
c861bb86f7
commit
1fc31fa042
2 files changed
+125
-11
No files matched your search
@@ -28,12 +28,71 @@ describe("browser target resolver", () => {
|
||||
it("refuses public relay hosts until the authenticated gateway exists", async () => {
|
||||
readPreparedConnection.mockReturnValue({ httpBaseUrl: "https://relay.example.com" });
|
||||
const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
|
||||
expect(() =>
|
||||
|
||||
try {
|
||||
resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
|
||||
kind: "environment-port",
|
||||
port: 5173,
|
||||
}),
|
||||
).toThrow(/authenticated preview gateway/);
|
||||
});
|
||||
expect.unreachable("Expected public environment host resolution to fail");
|
||||
} catch (error) {
|
||||
expect(error).toMatchObject({
|
||||
_tag: "BrowserTargetPrivateNetworkRequiredError",
|
||||
environmentId: "environment-1",
|
||||
hostname: "relay.example.com",
|
||||
message:
|
||||
"Environment environment-1 host relay.example.com needs the planned authenticated preview gateway because it is not directly private-network reachable.",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it("identifies the disconnected environment", async () => {
|
||||
const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
|
||||
|
||||
try {
|
||||
resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
|
||||
kind: "environment-port",
|
||||
port: 5173,
|
||||
});
|
||||
expect.unreachable("Expected disconnected environment resolution to fail");
|
||||
} catch (error) {
|
||||
expect(error).toMatchObject({
|
||||
_tag: "BrowserTargetEnvironmentDisconnectedError",
|
||||
environmentId: "environment-1",
|
||||
message: "Environment environment-1 is not connected.",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it("preserves invalid environment URL causes with connection context", async () => {
|
||||
const sensitiveUrl =
|
||||
"https://user:password@[invalid-host]/private/workspace?access_token=secret#fragment";
|
||||
readPreparedConnection.mockReturnValue({ httpBaseUrl: sensitiveUrl });
|
||||
const { BrowserTargetEnvironmentUrlInvalidError, resolveBrowserNavigationTarget } =
|
||||
await import("./browserTargetResolver");
|
||||
|
||||
try {
|
||||
resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
|
||||
kind: "environment-port",
|
||||
port: 5173,
|
||||
});
|
||||
expect.unreachable("Expected browser target resolution to fail");
|
||||
} catch (error) {
|
||||
expect(error).toMatchObject({
|
||||
_tag: "BrowserTargetEnvironmentUrlInvalidError",
|
||||
environmentId: "environment-1",
|
||||
httpBaseUrlInputLength: sensitiveUrl.length,
|
||||
cause: expect.any(TypeError),
|
||||
message: `Environment environment-1 has an invalid HTTP base URL input of length ${sensitiveUrl.length}.`,
|
||||
});
|
||||
expect(error).toBeInstanceOf(BrowserTargetEnvironmentUrlInvalidError);
|
||||
expect(error).not.toHaveProperty("httpBaseUrl");
|
||||
expect(error).not.toHaveProperty("httpBaseUrlProtocol");
|
||||
expect(error).not.toHaveProperty("httpBaseUrlHostname");
|
||||
expect(String((error as Error).message)).not.toMatch(
|
||||
/user|password|private|workspace|access_token|secret|fragment/,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it("normalizes schemeless localhost server-picker values", async () => {
|
||||
|
||||
@@ -1,12 +1,52 @@
|
||||
import type {
|
||||
BrowserNavigationTarget,
|
||||
import {
|
||||
type BrowserNavigationTarget,
|
||||
EnvironmentId,
|
||||
PreviewUrlResolution,
|
||||
type PreviewUrlResolution,
|
||||
} from "@t3tools/contracts";
|
||||
import { isLoopbackHost, normalizePreviewUrl } from "@t3tools/shared/preview";
|
||||
import { getUrlDiagnostics } from "@t3tools/shared/urlDiagnostics";
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
import { readPreparedConnection } from "~/state/session";
|
||||
|
||||
export class BrowserTargetEnvironmentDisconnectedError extends Schema.TaggedErrorClass<BrowserTargetEnvironmentDisconnectedError>()(
|
||||
"BrowserTargetEnvironmentDisconnectedError",
|
||||
{
|
||||
environmentId: EnvironmentId,
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Environment ${this.environmentId} is not connected.`;
|
||||
}
|
||||
}
|
||||
|
||||
export class BrowserTargetEnvironmentUrlInvalidError extends Schema.TaggedErrorClass<BrowserTargetEnvironmentUrlInvalidError>()(
|
||||
"BrowserTargetEnvironmentUrlInvalidError",
|
||||
{
|
||||
environmentId: EnvironmentId,
|
||||
httpBaseUrlInputLength: Schema.Number,
|
||||
httpBaseUrlProtocol: Schema.optionalKey(Schema.String),
|
||||
httpBaseUrlHostname: Schema.optionalKey(Schema.String),
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Environment ${this.environmentId} has an invalid HTTP base URL input of length ${this.httpBaseUrlInputLength}.`;
|
||||
}
|
||||
}
|
||||
|
||||
export class BrowserTargetPrivateNetworkRequiredError extends Schema.TaggedErrorClass<BrowserTargetPrivateNetworkRequiredError>()(
|
||||
"BrowserTargetPrivateNetworkRequiredError",
|
||||
{
|
||||
environmentId: EnvironmentId,
|
||||
hostname: Schema.String,
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Environment ${this.environmentId} host ${this.hostname} needs the planned authenticated preview gateway because it is not directly private-network reachable.`;
|
||||
}
|
||||
}
|
||||
|
||||
const isPrivateNetworkHost = (host: string): boolean => {
|
||||
const normalized = host.toLowerCase().replace(/^\[|\]$/g, "");
|
||||
if (normalized === "localhost" || normalized === "::1" || normalized.endsWith(".local")) {
|
||||
@@ -37,12 +77,27 @@ export function resolveBrowserNavigationTarget(
|
||||
};
|
||||
}
|
||||
const connection = readPreparedConnection(environmentId);
|
||||
if (!connection) throw new Error(`Environment ${environmentId} is not connected.`);
|
||||
const environmentUrl = new URL(connection.httpBaseUrl);
|
||||
if (!connection) {
|
||||
throw new BrowserTargetEnvironmentDisconnectedError({ environmentId });
|
||||
}
|
||||
let environmentUrl: URL;
|
||||
try {
|
||||
environmentUrl = new URL(connection.httpBaseUrl);
|
||||
} catch (cause) {
|
||||
const diagnostics = getUrlDiagnostics(connection.httpBaseUrl);
|
||||
throw new BrowserTargetEnvironmentUrlInvalidError({
|
||||
environmentId,
|
||||
httpBaseUrlInputLength: diagnostics.inputLength,
|
||||
...(diagnostics.protocol === undefined ? {} : { httpBaseUrlProtocol: diagnostics.protocol }),
|
||||
...(diagnostics.hostname === undefined ? {} : { httpBaseUrlHostname: diagnostics.hostname }),
|
||||
cause,
|
||||
});
|
||||
}
|
||||
if (!isPrivateNetworkHost(environmentUrl.hostname)) {
|
||||
throw new Error(
|
||||
"This environment port needs the planned authenticated preview gateway; its server address is not directly private-network reachable.",
|
||||
);
|
||||
throw new BrowserTargetPrivateNetworkRequiredError({
|
||||
environmentId,
|
||||
hostname: environmentUrl.hostname,
|
||||
});
|
||||
}
|
||||
const protocol = target.protocol ?? "http";
|
||||
const path = target.path?.startsWith("/") ? target.path : `/${target.path ?? ""}`;
|
||||
|
||||
Reference in new issue
Block a user