fix: support reverse-proxied dev origins

This commit is contained in:
Ben Davis committed 2026-07-24 21:11:47 -07:00
1 parent 1fa4219b48
commit 35ddbc4ab1
13 files changed
+298 -34

No files matched your search

+75
View File
@@ -61,6 +61,20 @@ const devServerInput = {
runArgs: ["--inspect", "secret-token-value"],
} as const;
const defaultCreateEnvInput = {
mode: "dev",
baseEnv: {},
serverOffset: 0,
webOffset: 0,
t3Home: undefined,
browser: undefined,
autoBootstrapProjectFromCwd: undefined,
logWebSocketEvents: undefined,
host: undefined,
port: undefined,
devUrl: undefined,
} as const;
it.layer(NodeServices.layer)("dev-runner", (it) => {
describe("getDevRunnerModeArgs", () => {
it.effect("lets Vite+ honor the desktop dev task graph", () =>
@@ -147,6 +161,67 @@ it.layer(NodeServices.layer)("dev-runner", (it) => {
}),
);
it.effect("configures remote dev targets and cookie isolation from one public URL", () =>
Effect.gen(function* () {
const env = yield* createDevRunnerEnv({
mode: "dev",
baseEnv: {},
serverOffset: 0,
webOffset: 0,
t3Home: undefined,
browser: undefined,
autoBootstrapProjectFromCwd: undefined,
logWebSocketEvents: undefined,
host: undefined,
port: undefined,
devUrl: undefined,
publicUrl: new URL("https://siva.example.test:7446"),
});
assert.equal(env.VITE_DEV_SERVER_URL, "https://siva.example.test:7446");
assert.equal(env.VITE_HTTP_URL, "https://siva.example.test:7446");
assert.equal(env.VITE_WS_URL, "wss://siva.example.test:7446");
assert.equal(env.T3CODE_DEV_PROXY_URL, "http://localhost:13773");
assert.equal(env.T3CODE_AUTH_COOKIE_PORT, "7446");
}),
);
it.effect("rejects public URLs that are not HTTP origins", () =>
Effect.gen(function* () {
const unsupportedProtocol = yield* Effect.flip(
createDevRunnerEnv({
...defaultCreateEnvInput,
publicUrl: new URL("ftp://siva.example.test:7446"),
}),
);
const pathUrl = yield* Effect.flip(
createDevRunnerEnv({
...defaultCreateEnvInput,
publicUrl: new URL("https://siva.example.test:7446/app"),
}),
);
assert.equal(unsupportedProtocol._tag, "DevRunnerPublicUrlError");
assert.equal(unsupportedProtocol.reason, "unsupported-protocol");
assert.equal(pathUrl._tag, "DevRunnerPublicUrlError");
assert.equal(pathUrl.reason, "not-an-origin");
}),
);
it.effect("rejects conflicting public and explicit dev URLs", () =>
Effect.gen(function* () {
const error = yield* Effect.flip(
createDevRunnerEnv({
...defaultCreateEnvInput,
devUrl: new URL("http://localhost:7331"),
publicUrl: new URL("https://siva.example.test:7446"),
}),
);
assert.equal(error._tag, "DevRunnerConflictingUrlOptionsError");
}),
);
it.effect("allows browser auto-open to be explicitly enabled", () =>
Effect.gen(function* () {
const env = yield* createDevRunnerEnv({
+97 -3
View File
@@ -81,6 +81,29 @@ export class DevRunnerInvalidPortOffsetError extends Schema.TaggedErrorClass<Dev
}
}
export class DevRunnerPublicUrlError extends Schema.TaggedErrorClass<DevRunnerPublicUrlError>()(
"DevRunnerPublicUrlError",
{
reason: Schema.Literals(["unsupported-protocol", "not-an-origin"]),
protocol: Schema.String,
},
) {
override get message(): string {
return this.reason === "unsupported-protocol"
? `Public URL must use HTTP or HTTPS; received protocol "${this.protocol}".`
: "Public URL must be an origin without credentials, a path, query parameters, or a fragment.";
}
}
export class DevRunnerConflictingUrlOptionsError extends Schema.TaggedErrorClass<DevRunnerConflictingUrlOptionsError>()(
"DevRunnerConflictingUrlOptionsError",
{},
) {
override get message(): string {
return "Use either --public-url or --dev-url, not both.";
}
}
export class DevRunnerPortExhaustedError extends Schema.TaggedErrorClass<DevRunnerPortExhaustedError>()(
"DevRunnerPortExhaustedError",
{
@@ -131,6 +154,8 @@ export class DevRunnerProcessExitError extends Schema.TaggedErrorClass<DevRunner
export const DevRunnerError = Schema.Union([
DevRunnerConfigurationError,
DevRunnerInvalidPortOffsetError,
DevRunnerPublicUrlError,
DevRunnerConflictingUrlOptionsError,
DevRunnerPortExhaustedError,
DevRunnerProcessError,
DevRunnerProcessExitError,
@@ -153,6 +178,11 @@ const optionalPortConfig = (name: string): Config.Config<number | undefined> =>
Config.option,
Config.map((value) => Option.getOrUndefined(value)),
);
const optionalUrlConfig = (name: string): Config.Config<URL | undefined> =>
Config.url(name).pipe(
Config.option,
Config.map((value) => Option.getOrUndefined(value)),
);
const optionalIntegerConfig = (name: string): Config.Config<number | undefined> =>
Config.int(name).pipe(
Config.option,
@@ -227,6 +257,7 @@ interface CreateDevRunnerEnvInput {
readonly host: string | undefined;
readonly port: number | undefined;
readonly devUrl: URL | undefined;
readonly publicUrl?: URL | undefined;
}
export function createDevRunnerEnv({
@@ -241,18 +272,68 @@ export function createDevRunnerEnv({
host,
port,
devUrl,
}: CreateDevRunnerEnvInput): Effect.Effect<NodeJS.ProcessEnv, never, Path.Path> {
publicUrl,
}: CreateDevRunnerEnvInput): Effect.Effect<
NodeJS.ProcessEnv,
DevRunnerPublicUrlError | DevRunnerConflictingUrlOptionsError,
Path.Path
> {
return Effect.gen(function* () {
const serverPort = port ?? BASE_SERVER_PORT + serverOffset;
const webPort = BASE_WEB_PORT + webOffset;
const configuredBaseDir = t3Home?.trim() || baseEnv.T3CODE_HOME?.trim() || undefined;
const resolvedBaseDir = yield* resolveBaseDir(configuredBaseDir);
const isDesktopMode = mode === "dev:desktop";
const configuredPublicUrl = isDesktopMode ? undefined : publicUrl;
if (configuredPublicUrl !== undefined && devUrl !== undefined) {
return yield* new DevRunnerConflictingUrlOptionsError();
}
if (
configuredPublicUrl !== undefined &&
configuredPublicUrl.protocol !== "http:" &&
configuredPublicUrl.protocol !== "https:"
) {
return yield* new DevRunnerPublicUrlError({
reason: "unsupported-protocol",
protocol: configuredPublicUrl.protocol,
});
}
if (
configuredPublicUrl !== undefined &&
(configuredPublicUrl.username !== "" ||
configuredPublicUrl.password !== "" ||
configuredPublicUrl.pathname !== "/" ||
configuredPublicUrl.search !== "" ||
configuredPublicUrl.hash !== "")
) {
return yield* new DevRunnerPublicUrlError({
reason: "not-an-origin",
protocol: configuredPublicUrl.protocol,
});
}
const publicHttpUrl = configuredPublicUrl?.origin;
const publicWsUrl =
configuredPublicUrl === undefined
? undefined
: (() => {
const url = new URL(configuredPublicUrl);
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
return url.origin;
})();
const publicPort =
configuredPublicUrl === undefined
? undefined
: Number(
configuredPublicUrl.port || (configuredPublicUrl.protocol === "https:" ? "443" : "80"),
);
const output: NodeJS.ProcessEnv = {
...baseEnv,
PORT: String(webPort),
VITE_DEV_SERVER_URL:
publicHttpUrl ??
devUrl?.toString() ??
`http://${isDesktopMode ? DESKTOP_DEV_LOOPBACK_HOST : "localhost"}:${webPort}`,
};
@@ -265,8 +346,12 @@ export function createDevRunnerEnv({
if (!isDesktopMode) {
output.T3CODE_PORT = String(serverPort);
output.VITE_HTTP_URL = `http://localhost:${serverPort}`;
output.VITE_WS_URL = `ws://localhost:${serverPort}`;
output.T3CODE_DEV_PROXY_URL = `http://localhost:${serverPort}`;
output.VITE_HTTP_URL = publicHttpUrl ?? `http://localhost:${serverPort}`;
output.VITE_WS_URL = publicWsUrl ?? `ws://localhost:${serverPort}`;
if (publicPort !== undefined) {
output.T3CODE_AUTH_COOKIE_PORT = String(publicPort);
}
} else {
output.T3CODE_PORT = String(serverPort);
output.VITE_HTTP_URL = `http://${DESKTOP_DEV_LOOPBACK_HOST}:${serverPort}`;
@@ -479,6 +564,7 @@ interface DevRunnerCliInput {
readonly host: string | undefined;
readonly port: number | undefined;
readonly devUrl: URL | undefined;
readonly publicUrl?: URL | undefined;
readonly dryRun: boolean;
readonly runArgs: ReadonlyArray<string>;
}
@@ -517,6 +603,7 @@ export function runDevRunnerWithInput(input: DevRunnerCliInput) {
host: input.host,
port: input.port,
devUrl: input.devUrl,
publicUrl: input.publicUrl,
});
const selectionSuffix =
@@ -627,6 +714,13 @@ const devRunnerCli = Command.make("dev-runner", {
Flag.optional,
Flag.map(Option.getOrUndefined),
),
publicUrl: Flag.string("public-url").pipe(
Flag.withSchema(Schema.URLFromString),
Flag.withDescription(
"Browser-facing HTTP(S) origin for remote dev access; also namespaces auth cookies by its public port.",
),
Flag.withFallbackConfig(optionalUrlConfig("T3CODE_PUBLIC_URL")),
),
dryRun: Flag.boolean("dry-run").pipe(
Flag.withDescription("Resolve mode/ports/env and print, but do not spawn Vite+."),
Flag.withDefault(false),