All files / embed iframe.ts

100% Statements 11/11
45.45% Branches 5/11
100% Functions 1/1
100% Lines 10/10

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                      1x   1x 1x   1x 1x   1x       1x 1x                 1x     1x    
import { VaccineScheduleWidget } from './vaccine-schedule';
import type { EmbedMessage } from './protocol';
 
/**
 * Iframe variant: the same widget, rendered in a document served from the Cygnus origin.
 *
 * An iframe cannot size itself to its content — the host sets the height, and the host cannot see
 * inside a cross-origin frame to measure it. postMessage is how the frame reports what layout would
 * have told the host for free in the script-tag variant. That is the real cost of isolation here,
 * and it is why the `resize` message exists at all.
 */
const params = new URLSearchParams(window.location.search);
// Vercel uses /api; Docker may call the backend directly without a same-origin proxy.
const apiOrigin = params.get('apiOrigin') ?? import.meta.env.VITE_API_BASE_URL ?? '/api';
const limit = Number.parseInt(params.get('limit') ?? '', 10);
 
const mount = document.getElementById('root');
const theme = params.get('theme');
 
Eif (mount) {
  // `?theme=light|dark` pins the palette; without it the frame follows the visitor's OS, as the
  // script-tag variant does. The stylesheet reads the attribute from the shadow host, which here is
  // the mount.
  Eif (theme) mount.setAttribute('theme', theme);
  const widget = new VaccineScheduleWidget(mount, {
    apiOrigin,
    limit: Number.isNaN(limit) ? undefined : limit,
    // '*' as the target origin is correct here and deliberate: this widget is meant to be embedded
    // by sites this project does not know about, so there is no allowlist to check against. It is
    // safe because every message is public, non-personal data — the height of a box and a link that
    // is already printed in the markup. Nothing sensitive is ever posted, so nothing leaks to a
    // host we did not anticipate. If a message ever carries user data, this must become an
    // explicit allowlist on the same commit.
    emit: (message: EmbedMessage) => window.parent.postMessage(message, '*'),
  });
 
  void widget.render();
}