Files
hyperframes/packages/studio/vite.preview-config.ts
T
Miguel Ángel 846cce4891 fix(cli): the CLI trusts a preview only once it proves this user started it (#7899)
* fix(cli): the CLI trusts a preview only once it proves this user started it

* test(cli): a listener that cannot prove it is the user's preview is never trusted

* test(cli): stand-in previews answer the challenge as the user's own do

* style(cli): format the preview proof changes

* fix(cli): a preview's proof names its own endpoint and config, and the token file must be private

* style(cli): format the proof changes

* test(studio): the fake request in the host check has a socket, as every real one does

* fix(studio): a Studio started by hand proves it is the user's, so the CLI still finds it

* fix(cli): the challenge travels in a header, so Studio's routes keep their exact paths

* style(studio): keep the config producer compact
2026-11-16 03:15:26 +00:00

35 lines
1.3 KiB
TypeScript

import { PREVIEW_NONCE_HEADER, previewProof } from "@hyperframes/core/preview-proof";
export type PreviewConfigEnv = Record<string, string | undefined>;
type ConfigRequest = Pick<import("node:http").IncomingMessage, "headers" | "socket">;
function signed(config: Record<string, unknown>, req?: ConfigRequest) {
const { localAddress: address, localPort: port } = req?.socket ?? {};
const nonce = req?.headers[PREVIEW_NONCE_HEADER];
const proof = address && port ? previewProof(nonce, { address, port }, config) : null;
return proof ? { ...config, proof } : null;
}
export function previewConfigPayload(
env: PreviewConfigEnv,
pid = process.pid,
version = "dev",
req?: ConfigRequest,
): Record<string, unknown> | null {
const projectDir = env.HYPERFRAMES_PREVIEW_PROJECT_DIR;
const projectName = env.HYPERFRAMES_PREVIEW_PROJECT_NAME;
// A Studio started by hand serves many projects: it proves only that it is this user's, never a project.
if (!projectDir || !projectName) return signed({ isStudio: true }, req);
const browserGpuMode = env.HYPERFRAMES_PREVIEW_BROWSER_GPU_MODE;
const config = {
isHyperframes: true,
pid,
projectName,
projectDir,
serverBuildSignature: null,
...(browserGpuMode ? { browserGpuMode } : {}),
version,
};
return signed(config, req) ?? config;
}