import { Router, type Router as ExpressRouter } from "express"; import { z } from "zod"; import { setPreviewPort } from "../preview-sink.js"; export const previewRouter: ExpressRouter = Router(); const portSchema = z.object({ port: z.number().int().min(1).max(65535), }); // Called by the frontend after the LSP `tinymist.startDefaultPreview` command // returns its (random) data-plane port. The backend stores it so that the // `/preview` WS and `/preview-app` HTTP proxies know where to dial. previewRouter.post("/sink", (req, res) => { const parsed = portSchema.safeParse(req.body); if (!parsed.success) { res.status(400).json({ error: parsed.error.issues }); return; } setPreviewPort(parsed.data.port); res.json({ ok: true, port: parsed.data.port }); });