histoire
Version:
A fast story book powered by Vite
47 lines (46 loc) • 3.1 kB
JavaScript
import { randomBytes } from 'node:crypto';
import { getSandboxRelativeUrl, normalizePreviewBase, setFramePurpose } from '@histoire/shared';
import { renderPreviewScript } from './preview-script.js';
/** Expiring same-origin host registry, independent of Vite and MCP transports. */
export function createPreviewHostRegistry(options) {
const base = normalizePreviewBase(options.base);
const prefix = `${base}__histoire/preview/`;
const targets = new Map();
let closed = false;
return {
/** Dedicated prefix lets HTTP adapters reject expired routes before fallback. */
prefix,
/** Acquire a random route only after an operation reaches the execution lane. */
open(target) {
if (closed || !target.isActive())
throw new Error('Preview host is inactive');
const origin = new URL(target.origin);
if (!['http:', 'https:'].includes(origin.protocol) || origin.username || origin.password)
throw new Error('Preview host origin is invalid');
const nonce = randomBytes(32).toString('hex');
const path = `${prefix}${nonce}.html`;
targets.set(path, { ...target, globals: target.globals && { ...target.globals }, propsOverride: target.propsOverride && structuredClone(target.propsOverride), origin: origin.origin, nonce });
return { nonce, url: new URL(path, origin.origin).href, close: () => targets.delete(path) };
},
/** Match exact raw route; query, traversal and expired leases have no document. */
render(path) {
const target = targets.get(path);
if (!target?.isActive()) {
targets.delete(path);
return;
}
const sandboxUrl = new URL(getSandboxRelativeUrl({ base, storyId: target.storyId, variantId: target.variantId }), target.origin);
sandboxUrl.searchParams.set('mcpNonce', target.nonce);
sandboxUrl.searchParams.set('mcpEpoch', target.epoch);
sandboxUrl.searchParams.set('embed', 'true');
setFramePurpose(sandboxUrl, 'automation');
const script = renderPreviewScript({ authority: { origin: new URL(target.origin).origin, storyId: target.storyId, variantId: target.variantId, nonce: target.nonce, epoch: target.epoch, active: true }, sandboxUrl: sandboxUrl.href, settings: { responsiveWidth: target.width, responsiveHeight: target.height, rotate: false, backgroundColor: target.backgroundColor, checkerboard: false, textDirection: target.textDirection, globals: target.globals ?? {} }, colorScheme: target.colorScheme, propsOverride: target.propsOverride });
return `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"></head><body style="margin:0;overflow:hidden"><script>${script}</script></body></html>`;
},
/** All old capabilities cease serving immediately on generation release. */
close() {
closed = true;
targets.clear();
},
};
}