All files / worker-play/src cross-origin-isolation.ts

100% Statements 5/5
100% Branches 0/0
100% Functions 1/1
100% Lines 4/4

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                          1x             3x 6x 3x    
/**
 * The headers that make the player cross-origin isolated (`crossOriginIsolated`), set on every response this Worker
 * serves (docs/services.md, "Player").
 *
 * Isolation coarsens `performance.now()` and `Event.timeStamp` less (Chromium clamps them to 5 µs instead of 100 µs),
 * so key presses are timed and recorded in replays more finely. It also gives the page `SharedArrayBuffer`, which the
 * BGA transcode uses to hand frames to the WebCodecs encoder.
 *
 * `credentialless` rather than `require-corp`: the page loads cross-origin resources that send no
 * `Cross-Origin-Resource-Policy` (Google Fonts, avatars on the providers' CDNs), and `credentialless` lets those load
 * without cookies instead of blocking them. A browser that does not know `credentialless` ignores the header and simply
 * stays unisolated.
 */
export const CROSS_ORIGIN_ISOLATION_HEADERS: Readonly<Record<string, string>> = {
  'cross-origin-opener-policy': 'same-origin',
  'cross-origin-embedder-policy': 'credentialless',
};
 
/** `response` with {@link CROSS_ORIGIN_ISOLATION_HEADERS} set, as a new response whose headers can be changed. */
export function withCrossOriginIsolation(response: Response): Response {
  const served = new Response(response.body, response);
  for (const [name, value] of Object.entries(CROSS_ORIGIN_ISOLATION_HEADERS)) served.headers.set(name, value);
  return served;
}