Prefer the dev server when several are live under one base dir
`findLiveServerRuntimeState` checked the configured state path first, which without --dev-url resolves to `userdata`. With both a userdata and a dev server running under the same base directory, `auth pairing url` would mint against the userdata server and print its origin — not the dev server this command exists to pair with. Rank candidates by the `devUrl` field instead, which only a server fronted by a web dev server records. Falls back to any live server when no dev server is running. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
1 parent
36fe3b31a3
commit
0555604ded
2 files changed
+156
-6
No files matched your search
@@ -0,0 +1,138 @@
|
|||||||
|
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||||
|
import { assert, describe, it } from "@effect/vitest";
|
||||||
|
import * as Effect from "effect/Effect";
|
||||||
|
import * as FileSystem from "effect/FileSystem";
|
||||||
|
import * as Option from "effect/Option";
|
||||||
|
import * as Path from "effect/Path";
|
||||||
|
|
||||||
|
import { persistServerRuntimeState } from "../serverRuntimeState.ts";
|
||||||
|
import { findLiveServerRuntimeState } from "./auth.ts";
|
||||||
|
|
||||||
|
/** A pid that cannot belong to a live process (above the usual pid_max). */
|
||||||
|
const DEAD_PID = 0x7ffffffe;
|
||||||
|
|
||||||
|
const writeRuntimeState = Effect.fn(function* (input: {
|
||||||
|
readonly baseDir: string;
|
||||||
|
readonly stateDir: "dev" | "userdata";
|
||||||
|
readonly pid: number;
|
||||||
|
readonly port: number;
|
||||||
|
readonly devUrl?: string;
|
||||||
|
}) {
|
||||||
|
const path = yield* Path.Path;
|
||||||
|
yield* persistServerRuntimeState({
|
||||||
|
path: path.join(input.baseDir, input.stateDir, "server-runtime.json"),
|
||||||
|
state: {
|
||||||
|
version: 1,
|
||||||
|
pid: input.pid,
|
||||||
|
port: input.port,
|
||||||
|
origin: `http://127.0.0.1:${String(input.port)}`,
|
||||||
|
...(input.devUrl ? { devUrl: input.devUrl } : {}),
|
||||||
|
startedAt: "2026-07-20T00:00:00.000Z",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const makeBaseDir = Effect.fn(function* () {
|
||||||
|
const fileSystem = yield* FileSystem.FileSystem;
|
||||||
|
const path = yield* Path.Path;
|
||||||
|
const baseDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-auth-cli-test-" });
|
||||||
|
return {
|
||||||
|
baseDir,
|
||||||
|
// What the flag heuristic resolves to without --dev-url: the userdata dir.
|
||||||
|
serverRuntimeStatePath: path.join(baseDir, "userdata", "server-runtime.json"),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("findLiveServerRuntimeState", () => {
|
||||||
|
it.effect("prefers the dev server when both state directories are live", () =>
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const path = yield* Path.Path;
|
||||||
|
const config = yield* makeBaseDir();
|
||||||
|
// The configured path points at userdata, but this command is about dev.
|
||||||
|
yield* writeRuntimeState({
|
||||||
|
baseDir: config.baseDir,
|
||||||
|
stateDir: "userdata",
|
||||||
|
pid: process.pid,
|
||||||
|
port: 16_601,
|
||||||
|
});
|
||||||
|
yield* writeRuntimeState({
|
||||||
|
baseDir: config.baseDir,
|
||||||
|
stateDir: "dev",
|
||||||
|
pid: process.pid,
|
||||||
|
port: 16_602,
|
||||||
|
devUrl: "http://localhost:8888/",
|
||||||
|
});
|
||||||
|
|
||||||
|
const found = yield* findLiveServerRuntimeState(config);
|
||||||
|
|
||||||
|
const live = Option.getOrThrow(found);
|
||||||
|
assert.equal(live.stateDir, path.join(config.baseDir, "dev"));
|
||||||
|
assert.equal(live.state.devUrl, "http://localhost:8888/");
|
||||||
|
}).pipe(Effect.provide(NodeServices.layer)),
|
||||||
|
);
|
||||||
|
|
||||||
|
it.effect("falls back to a live non-dev server when no dev server is running", () =>
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const path = yield* Path.Path;
|
||||||
|
const config = yield* makeBaseDir();
|
||||||
|
yield* writeRuntimeState({
|
||||||
|
baseDir: config.baseDir,
|
||||||
|
stateDir: "userdata",
|
||||||
|
pid: process.pid,
|
||||||
|
port: 16_601,
|
||||||
|
});
|
||||||
|
|
||||||
|
const found = yield* findLiveServerRuntimeState(config);
|
||||||
|
|
||||||
|
const live = Option.getOrThrow(found);
|
||||||
|
assert.equal(live.stateDir, path.join(config.baseDir, "userdata"));
|
||||||
|
assert.equal(live.state.port, 16_601);
|
||||||
|
}).pipe(Effect.provide(NodeServices.layer)),
|
||||||
|
);
|
||||||
|
|
||||||
|
// A server killed with SIGKILL never clears its state file.
|
||||||
|
it.effect("ignores state files whose process is gone", () =>
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const config = yield* makeBaseDir();
|
||||||
|
yield* writeRuntimeState({
|
||||||
|
baseDir: config.baseDir,
|
||||||
|
stateDir: "dev",
|
||||||
|
pid: DEAD_PID,
|
||||||
|
port: 16_602,
|
||||||
|
devUrl: "http://localhost:8888/",
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.isTrue(Option.isNone(yield* findLiveServerRuntimeState(config)));
|
||||||
|
}).pipe(Effect.provide(NodeServices.layer)),
|
||||||
|
);
|
||||||
|
|
||||||
|
it.effect("skips a stale dev server in favor of a live one elsewhere", () =>
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const path = yield* Path.Path;
|
||||||
|
const config = yield* makeBaseDir();
|
||||||
|
yield* writeRuntimeState({
|
||||||
|
baseDir: config.baseDir,
|
||||||
|
stateDir: "dev",
|
||||||
|
pid: DEAD_PID,
|
||||||
|
port: 16_602,
|
||||||
|
devUrl: "http://localhost:8888/",
|
||||||
|
});
|
||||||
|
yield* writeRuntimeState({
|
||||||
|
baseDir: config.baseDir,
|
||||||
|
stateDir: "userdata",
|
||||||
|
pid: process.pid,
|
||||||
|
port: 16_601,
|
||||||
|
});
|
||||||
|
|
||||||
|
const live = Option.getOrThrow(yield* findLiveServerRuntimeState(config));
|
||||||
|
assert.equal(live.stateDir, path.join(config.baseDir, "userdata"));
|
||||||
|
}).pipe(Effect.provide(NodeServices.layer)),
|
||||||
|
);
|
||||||
|
|
||||||
|
it.effect("reports nothing when no state files exist", () =>
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const config = yield* makeBaseDir();
|
||||||
|
assert.isTrue(Option.isNone(yield* findLiveServerRuntimeState(config)));
|
||||||
|
}).pipe(Effect.provide(NodeServices.layer)),
|
||||||
|
);
|
||||||
|
});
|
||||||
@@ -139,18 +139,23 @@ const pairingCreateCommand = Command.make("create", {
|
|||||||
* tokens into a database the running server never reads when guessed wrong.
|
* tokens into a database the running server never reads when guessed wrong.
|
||||||
* So check both, and let the live server decide which one is right.
|
* So check both, and let the live server decide which one is right.
|
||||||
*/
|
*/
|
||||||
const findLiveServerRuntimeState = Effect.fn("auth.findLiveServerRuntimeState")(function* (
|
export const findLiveServerRuntimeState = Effect.fn("auth.findLiveServerRuntimeState")(function* (
|
||||||
config: ServerConfig.ServerConfig["Service"],
|
config: Pick<ServerConfig.ServerConfig["Service"], "baseDir" | "serverRuntimeStatePath">,
|
||||||
) {
|
) {
|
||||||
const path = yield* Path.Path;
|
const path = yield* Path.Path;
|
||||||
const candidates = [
|
const candidatePaths = [
|
||||||
config.serverRuntimeStatePath,
|
config.serverRuntimeStatePath,
|
||||||
...(["dev", "userdata"] as const).map((stateDir) =>
|
...(["dev", "userdata"] as const).map((stateDir) =>
|
||||||
path.join(config.baseDir, stateDir, "server-runtime.json"),
|
path.join(config.baseDir, stateDir, "server-runtime.json"),
|
||||||
),
|
),
|
||||||
].filter((candidate, index, all) => all.indexOf(candidate) === index);
|
].filter((candidate, index, all) => all.indexOf(candidate) === index);
|
||||||
|
|
||||||
for (const statePath of candidates) {
|
const live: Array<{
|
||||||
|
readonly stateDir: string;
|
||||||
|
readonly state: PersistedServerRuntimeState;
|
||||||
|
}> = [];
|
||||||
|
|
||||||
|
for (const statePath of candidatePaths) {
|
||||||
const state = yield* readPersistedServerRuntimeState(statePath);
|
const state = yield* readPersistedServerRuntimeState(statePath);
|
||||||
if (Option.isNone(state)) {
|
if (Option.isNone(state)) {
|
||||||
continue;
|
continue;
|
||||||
@@ -159,11 +164,18 @@ const findLiveServerRuntimeState = Effect.fn("auth.findLiveServerRuntimeState")(
|
|||||||
// nothing is listening on. Minting against it produces a token the live
|
// nothing is listening on. Minting against it produces a token the live
|
||||||
// server rejects with `invalid_credential`.
|
// server rejects with `invalid_credential`.
|
||||||
if (yield* isPersistedServerRuntimeStateLive(state.value)) {
|
if (yield* isPersistedServerRuntimeStateLive(state.value)) {
|
||||||
return Option.some({ stateDir: path.dirname(statePath), state: state.value });
|
live.push({ stateDir: path.dirname(statePath), state: state.value });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Option.none<{ readonly stateDir: string; readonly state: PersistedServerRuntimeState }>();
|
// `devUrl` is only recorded by a server fronted by a web dev server, so it
|
||||||
|
// distinguishes the dev instance from a production-style one when both are
|
||||||
|
// running under the same base directory. This command is about dev, so a dev
|
||||||
|
// server wins regardless of which state directory the flags happened to
|
||||||
|
// resolve to.
|
||||||
|
return Option.fromNullishOr(
|
||||||
|
live.find((candidate) => candidate.state.devUrl !== undefined) ?? live[0],
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user