All files / cli/src preview.ts

93.93% Statements 31/33
93.1% Branches 27/29
100% Functions 4/4
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64                                5x 5x 5x 3x 3x 5x 1x 2x 2x 2x 9x 9x 9x       9x 1x 1x   8x   9x             9x 2x 2x   6x 6x   2x 2x 2x 2x 2x     2x 2x 2x    
import { readFile } from 'node:fs/promises';
import { createServer, type Server } from 'node:http';
import { getPhotometricTypeSupport, parsePhotometry } from '@ies-tools/core';
import type { InputFormat } from './photometry.js';
 
export interface PreviewServer {
  server: Server;
  url: string;
}
 
/** Start a loopback-only preview server. Its input is captured, never resolved from request paths. */
export async function servePreview(
  text: string,
  port = 0,
  options: { format?: InputFormat; lampSetIndex?: number } = {},
): Promise<PreviewServer> {
  const document = parsePhotometry(text, options.format, { lampSetIndex: options.lampSetIndex });
  const support = getPhotometricTypeSupport(document.profile);
  Iif (!support.supported) throw new Error(support.message);
  const profilePath = `/profile.${document.sourceFormat}`;
  const selection = options.lampSetIndex === undefined ? '' : ` lamp-set-index="${options.lampSetIndex}"`;
  if (!Number.isInteger(port) || port < 0 || port > 65535)
    throw new RangeError('Port must be an integer from 0 to 65535');
  const bundle = await readFile(new URL('../media/light-viewer.js', import.meta.url));
  const html = `<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Photometric Light Viewer</title><style>html,body{margin:0;background:#000;color:#fff;font-family:system-ui}light-viewer{display:block;width:100vw;height:100vh}</style><light-viewer src="${profilePath}" format="${document.sourceFormat}"${selection}></light-viewer><script type="module" src="/light-viewer.js"></script></html>`;
  const server = createServer((request, response) => {
    response.setHeader('X-Content-Type-Options', 'nosniff');
    response.setHeader('Cache-Control', 'no-store');
    response.setHeader(
      'Content-Security-Policy',
      "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; connect-src 'self'",
    );
    if (request.method !== 'GET' && request.method !== 'HEAD') {
      response.writeHead(405, { Allow: 'GET, HEAD' }).end();
      return;
    }
    const path = request.url?.split('?')[0];
    const route =
      path === '/'
        ? ([html, 'text/html; charset=utf-8'] as const)
        : path === profilePath
          ? ([text, 'text/plain; charset=utf-8'] as const)
          : path === '/light-viewer.js'
            ? ([bundle, 'application/javascript'] as const)
            : null;
    if (!route) {
      response.writeHead(404).end('Not found');
      return;
    }
    response.writeHead(200, { 'Content-Type': route[1] });
    response.end(request.method === 'HEAD' ? undefined : route[0]);
  });
  await new Promise<void>((resolve, reject) => {
    server.once('error', reject);
    server.listen(port, '127.0.0.1', () => {
      server.off('error', reject);
      resolve();
    });
  });
  const address = server.address();
  Iif (!address || typeof address === 'string') throw new Error('Unable to determine preview port');
  return { server, url: `http://127.0.0.1:${address.port}/` };
}