import { createServer, type IncomingMessage, type ServerResponse } from 'node:http' import type { UsageReport } from '@pandacss/compiler-shared' export interface StudioSource { path: string text: string } /** What the bridge page relays into Studio. Studio's `useStudioBridge` reads the same shapes. */ export type StudioMessage = | { type: 'system'; spec: string } | { type: 'report'; report: UsageReport } | { type: 'sources'; files: StudioSource[] } | { type: 'source'; path: string; text: string } | { type: 'removed'; path: string } export interface StudioServerOptions { port: number token: string /** Hosted Studio page the bridge frames, e.g. `https://studio.panda-css.com/view`. */ studioUrl: string /** Everything a newly connected page needs before live updates. */ snapshot(): StudioMessage[] onConnect?(): void /** A request with a wrong token, usually a tab left from an earlier run. */ onReject?(): void } export interface StudioServer { url: string port: number broadcast(message: StudioMessage): void close(): Promise } const LOCAL_HOST = /^(localhost|127\.0\.0\.1)(:\d+)?$/ const KEEPALIVE_MS = 15_000 export function startStudioServer(options: StudioServerOptions): Promise { const clients = new Set() const write = (res: ServerResponse, message: StudioMessage) => res.write(`data: ${JSON.stringify(message)}\n\n`) const server = createServer((req, res) => { // DNS rebinding: a page on another name that resolves to 127.0.0.1 still sends its own Host. if (!LOCAL_HOST.test(req.headers.host ?? '')) return void res.writeHead(403).end() const url = new URL(req.url ?? '/', 'http://localhost') const token = url.searchParams.get('token') const authorized = token === options.token if (url.pathname === '/' && req.method === 'GET') { if (token && !authorized) options.onReject?.() res.writeHead(authorized ? 200 : 401, { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store' }) return void res.end(authorized ? bridgePage(options.studioUrl, options.token) : tokenPage(Boolean(token))) } if (url.pathname === '/events' && req.method === 'GET') { if (!authorized) { options.onReject?.() return void res.writeHead(401).end() } openStream(req, res) options.onConnect?.() return } res.writeHead(404).end() }) const openStream = (req: IncomingMessage, res: ServerResponse) => { res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-store', connection: 'keep-alive', }) for (const message of options.snapshot()) write(res, message) clients.add(res) const keepalive = setInterval(() => res.write(': keepalive\n\n'), KEEPALIVE_MS) req.on('close', () => { clearInterval(keepalive) clients.delete(res) }) } return new Promise((resolve, reject) => { server.once('error', reject) server.listen(options.port, '127.0.0.1', () => { server.off('error', reject) const address = server.address() const port = typeof address === 'object' && address ? address.port : options.port resolve({ port, url: `http://localhost:${port}/?token=${options.token}`, broadcast(message) { for (const res of clients) write(res, message) }, close() { for (const res of clients) res.end() clients.clear() return new Promise((done) => server.close(() => done())) }, }) }) }) } export function bridgePage(studioUrl: string, token: string): string { const origin = new URL(studioUrl).origin return ` Panda Studio ` } const LOGO = `` const FAVICON = `data:image/svg+xml,${encodeURIComponent(LOGO)}` export function tokenPage(stale: boolean): string { const [heading, body] = stale ? [ 'This link is from an earlier run', 'Each run of panda studio prints a new link. Open the latest one from your terminal.', ] : ['Open Studio from your terminal', 'Use the link panda studio printed. It carries the access token.'] return ` Panda Studio
${LOGO}Panda Studio

${heading}

${body}

panda studio
` } function escapeAttribute(value: string): string { return value.replace(/&/g, '&').replace(/"/g, '"').replace(/