Files
webstudio/packages/cli/src/preview-server/controller.ts
T
Oleg Isonen 3c7dac7eb0 fix: resolve MCP preview and content graph reports (#6511)
## Summary

- Raise the document-reference graph budget independently of the 20-file
content hydration limit, and document the 2,000-document graph cap. This
lets the reported 29-post referenced query complete.
- Make iterative preview startup verify the generated project identity
endpoint instead of compiling the application route; exact route
validation remains with screenshot/capture.
- Keep production preview readiness tied to the application route even
when no CSS assets are present.
- Include a stable structured diagnostic when the browser DevTools
connection closes, while preserving the existing reconnect behavior.
- Document and test a safe leaves-first `convert-instance` migration for
required-part composite trees.

Closes #6508, #6515
Ref #6509, #6510

## Verification

- Content Engine suite: 656 tests passed.
- Vision suite: 85 tests passed.
- CLI preview/readiness/controller tests: 32 passed; project-build
instance conversion tests: 89 passed.
- CLI typecheck, targeted lint, formatting, and diff checks passed.
- The full CLI suite timed out in unrelated launcher/fixture tests under
parallel load. The preview-script and launcher tests passed with fewer
workers, and the MDX fixture test passed alone in 29 seconds. No full
local agent evaluation command was run.
- All required GitHub checks passed on latest commit `7f7b981` (29
successful; visual regression skipped).

## Remaining scope

- #6509 remains open: this PR improves diagnostics but does not
establish a stable Edge DevTools capture.
- #6510 remains open: this PR removes the initial route-compilation
dependency, but does not yet provide route-specific preparing/retry
status.

## Checklist

- [x] Review the change and preserve production preview route checks.
- [x] Add regression coverage for the review fix and required-part
conversion recipe.
- [x] Update MCP manual docs with the supported conversion order.
- [x] Run focused tests, typecheck, lint, and formatting.
- [ ] Run the full local agent evaluation suite (not run).
- [x] Confirm required CI checks pass on the latest commit.
2026-10-09 16:14:18 +02:00

310 lines
9.0 KiB
TypeScript

import { join } from "node:path";
import { previewBuildCacheMarker } from "./constants";
import { defaultPreviewServerDependencies } from "./dependencies";
import {
getPreviewCssAssetNames,
getPreviewProjectIdentity,
waitForPreviewReady,
} from "./readiness";
import {
getPreviewUrl,
runPreviewBuild,
startPreviewServer,
stopPreviewProcess,
} from "./server";
import type {
PreviewMode,
PreviewServerOptions,
PreviewServerResult,
} from "./types";
export type PreviewControllerResult = {
url?: string;
pid?: number;
running: boolean;
mode?: PreviewMode;
};
type PreviewControllerStartOptions = Partial<PreviewServerOptions> & {
restart?: boolean;
buildCacheKey?: string;
};
export const arePreviewImageDomainsEqual = (
left: string[] | undefined,
right: string[] | undefined
) =>
left === right ||
(left !== undefined &&
right !== undefined &&
left.length === right.length &&
left.every((value, index) => value === right[index]));
const formatPreviewServerStartupError = ({
message,
output,
url,
}: {
message: string;
output: string;
url: string;
}) => {
const portHint = output.includes("EADDRINUSE")
? `\n\nPort is already in use. Stop the existing preview server for ${url}, or start preview with a different port.`
: "";
return `${message}\n\nPreview server output:\n${output}${portHint}`;
};
export const createPreviewController = (
defaults: PreviewServerOptions,
dependencies = defaultPreviewServerDependencies,
{ manageProcessSignals = true }: { manageProcessSignals?: boolean } = {}
) => {
let server: PreviewServerResult | undefined;
let currentOptions = defaults;
let currentCwd = defaults.cwd;
let serverOutput = "";
const terminationSignals = ["SIGHUP", "SIGINT", "SIGTERM"] as const;
let isTerminating = false;
const terminationHandlers = new Map<NodeJS.Signals, () => void>();
const removeTerminationHandlers = () => {
for (const [signal, handler] of terminationHandlers) {
dependencies.parentProcess.off(signal, handler);
}
terminationHandlers.clear();
};
const installTerminationHandlers = () => {
if (manageProcessSignals === false || terminationHandlers.size > 0) {
return;
}
for (const signal of terminationSignals) {
const handler = () => {
if (isTerminating) {
return;
}
isTerminating = true;
const activeServer = server;
server = undefined;
removeTerminationHandlers();
if (activeServer === undefined) {
dependencies.parentProcess.kill(
dependencies.parentProcess.pid,
signal
);
return;
}
try {
stopPreviewProcess(activeServer.process);
} finally {
dependencies.parentProcess.kill(
dependencies.parentProcess.pid,
signal
);
}
};
terminationHandlers.set(signal, handler);
dependencies.parentProcess.once(signal, handler);
}
};
const appendServerOutput = (chunk: unknown) => {
serverOutput = `${serverOutput}${String(chunk)}`.slice(-4000);
};
const isRunning = () =>
server !== undefined &&
server.process.killed === false &&
server.process.exitCode === null &&
server.process.signalCode === null;
const getStatus = (): PreviewControllerResult => {
if (isRunning() === false) {
return { running: false };
}
return {
url: getPreviewUrl(currentOptions),
pid: server?.process.pid,
running: true,
mode: currentOptions.mode ?? "production",
};
};
const resolveOptions = (
options: PreviewControllerStartOptions
): PreviewServerOptions => {
const running = isRunning();
return {
host: options.host ?? (running ? currentOptions.host : defaults.host),
port: options.port ?? (running ? currentOptions.port : defaults.port),
cwd: options.cwd ?? (running ? currentCwd : defaults.cwd),
imageDomains:
options.imageDomains ??
(running ? currentOptions.imageDomains : defaults.imageDomains),
mode:
options.mode ??
(running ? currentOptions.mode : defaults.mode) ??
"production",
};
};
const canReuse = (options: PreviewControllerStartOptions = {}) => {
if (isRunning() === false) {
return false;
}
const nextOptions = resolveOptions(options);
return (
nextOptions.host === currentOptions.host &&
nextOptions.port === currentOptions.port &&
nextOptions.cwd === currentCwd &&
nextOptions.mode === (currentOptions.mode ?? "production") &&
arePreviewImageDomainsEqual(
nextOptions.imageDomains,
currentOptions.imageDomains
)
);
};
const stop = async () => {
if (server === undefined) {
return;
}
const process = server.process;
server = undefined;
removeTerminationHandlers();
if (
process.killed ||
process.exitCode !== null ||
process.signalCode !== null
) {
return;
}
const exited = new Promise<void>((resolve, reject) => {
process.once("error", reject);
process.once("exit", () => resolve());
});
if (stopPreviewProcess(process) === false) {
return;
}
await exited;
};
const start = async (
options: PreviewControllerStartOptions = {}
): Promise<PreviewControllerResult> => {
const running = isRunning();
const nextOptions = resolveOptions(options);
if (running) {
if (options.restart !== true) {
if (canReuse(options) === false) {
throw new Error(
`Preview server is already running at ${getPreviewUrl(currentOptions)}. Stop it before starting a different preview server.`
);
}
return getStatus();
}
await stop();
}
currentOptions = nextOptions;
currentCwd = nextOptions.cwd;
const cachedBuildKey =
options.buildCacheKey === undefined || currentCwd === undefined
? undefined
: await dependencies
.readFile(join(currentCwd, previewBuildCacheMarker), "utf8")
.catch(() => undefined);
if (
nextOptions.mode === "production" &&
(options.buildCacheKey === undefined ||
cachedBuildKey !== options.buildCacheKey)
) {
await runPreviewBuild(dependencies, currentCwd, [
"ignore",
"pipe",
"pipe",
]);
if (options.buildCacheKey !== undefined && currentCwd !== undefined) {
await dependencies.writeFile(
join(currentCwd, previewBuildCacheMarker),
options.buildCacheKey
);
}
}
serverOutput = "";
const startedServer = startPreviewServer(
{
...nextOptions,
detached: dependencies.platform !== "win32",
stdio: ["ignore", "pipe", "pipe"],
},
dependencies
);
server = startedServer;
installTerminationHandlers();
startedServer.process.stdout?.on("data", appendServerOutput);
startedServer.process.stderr?.on("data", appendServerOutput);
startedServer.process.once("exit", () => {
if (server === startedServer) {
server = undefined;
removeTerminationHandlers();
}
});
return getStatus();
};
return {
canReuse,
start,
async startAndWait(
options: PreviewControllerStartOptions = {}
): Promise<PreviewControllerResult> {
const nextCwd = options.cwd ?? (isRunning() ? currentCwd : defaults.cwd);
const requiredProject = await getPreviewProjectIdentity(
nextCwd,
dependencies
);
const result = await start(options);
const url = result.url ?? getPreviewUrl(currentOptions);
const requiredAssetNames =
result.mode === "production"
? await getPreviewCssAssetNames(currentCwd, dependencies)
: [];
try {
await waitForPreviewReady(
url,
{
isRunning,
requiredAssetNames,
requiredProject,
probeIdentityOnly: result.mode === "iterative",
},
dependencies
);
} catch (error) {
const output = serverOutput.trim();
if (output !== "" && error instanceof Error) {
error.message = formatPreviewServerStartupError({
message: error.message,
output,
url,
});
if (output.includes("EADDRINUSE")) {
Object.assign(error, {
code: "PREVIEW_PORT_IN_USE",
issues: [
{
code: "preview_port_in_use",
path: [],
message: "The selected preview port is already in use.",
constraint: "available_preview_port",
},
],
});
}
}
throw error;
}
return result;
},
async stop(): Promise<PreviewControllerResult> {
await stop();
return getStatus();
},
status: getStatus,
resolveUrl(path = "/") {
return getPreviewUrl({ ...currentOptions, path });
},
};
};