All files liveStream.ts

87.5% Statements 14/16
91.66% Branches 11/12
100% Functions 3/3
87.5% Lines 14/16

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            4x 4x         4x 1x     3x 1x     2x 2x   2x         2x 2x       1x         7x 7x    
import type { NEntry } from "../types/har";
import { HarParseError, parseHar } from "./parse";
 
/** validateDebugStreamURL restricts live inspection to the recorder's local-development threat model. */
export function validateDebugStreamURL(value: string): URL {
  let url: URL;
  try {
    url = new URL(value);
  } catch {
    throw new Error("Enter a valid absolute URL.");
  }
 
  if (url.protocol !== "http:" && url.protocol !== "https:") {
    throw new Error("The live stream URL must use HTTP or HTTPS.");
  }
 
  if (url.hostname !== "localhost" && url.hostname !== "127.0.0.1" && url.hostname !== "[::1]" && url.hostname !== "::1") {
    throw new Error("Live inspection accepts loopback URLs only.");
  }
 
  url.username = "";
  url.password = "";
 
  return url;
}
 
/** parseLiveEntry validates one entry event using the same rules as file imports. */
export function parseLiveEntry(data: string, id: number): NEntry {
  const loaded = parseHar(data);
  Iif (loaded.entries.length !== 1) {
    throw new HarParseError("A live entry event must contain exactly one HAR entry.");
  }
 
  return { ...loaded.entries[0], id };
}
 
/** liveReconnectDelay applies bounded exponential backoff without ever stopping retries. */
export function liveReconnectDelay(attempt: number): number {
  const boundedAttempt = Math.max(0, Math.min(Math.floor(attempt), 5));
  return Math.min(10_000, 500 * (2 ** boundedAttempt));
}