All files / player-browser/src/browser compat.ts

83.72% Statements 36/43
88% Branches 44/50
100% Functions 20/20
90.62% Lines 29/32

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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245                                                                                    5x 5x                                                                                                                                                                                                                           19x 5x               3x 5x 2x 1x       5x 2x 2x     2x             5x     5x       5x 5x       5x       5x       5x       5x       5x 5x               5x       5x       5x       5x       5x       5x       5x    
/**
 * Feature-detection probe for the browser APIs the web player relies on. Run once at boot; the result drives the
 * readiness badge in the drop card so a user on an old browser sees *why* the player can't start instead of getting an
 * opaque WebGL / AudioContext error mid-load.
 *
 * The detection lives in `player-web` (not the player) so any host shell — the player, a future hosted build, an embedded
 * iframe — gets the same readiness verdict from a single source of truth as the actual runtime feature usage.
 */
 
/**
 * One row of the compatibility report. `id` is stable across versions so a host could persist user-dismissed warnings;
 * the `label` is the human-readable name shown in the badge.
 */
export interface BrowserCompatItem {
  id: string;
  label: string;
  supported: boolean;
  /**
   * `true` when the player can't function at all without this feature (rendering / audio / chart loading). Missing
   * required features flip {@link BrowserCompatReport.ok} to `false`.
   */
  required: boolean;
  /**
   * One-line note explaining what depends on this feature, shown as a tooltip / aria-description on the badge.
   */
  note: string;
}
 
export interface BrowserCompatReport {
  /**
   * `true` iff every `required: true` item is supported. The drop card uses this to swap "Drop to load" for the
   * unsupported message.
   */
  ok: boolean;
  items: ReadonlyArray<BrowserCompatItem>;
}
 
/**
 * Synchronously probes every feature. Each detector swallows its own exceptions so a single failed probe (e.g.
 * `getContext` on a locked-down canvas) can't crash the rest of the page.
 */
export function checkBrowserCompat(): BrowserCompatReport {
  const webgpu = detectWebGPU();
  const items: BrowserCompatItem[] = [
    // What the player cannot start without: something to draw with, something to play the sound through, and a way to
    // take a dropped folder.
    {
      id: 'gpu',
      label: 'WebGPU / WebGL2',
      required: true,
      supported: webgpu || detectWebGL2(),
      note: 'Draws the player: WebGPU where there is one, WebGL2 otherwise',
    },
    {
      id: 'web-audio',
      label: 'Web Audio',
      required: true,
      supported: detectAudioContext(),
      note: 'Keysound and BGM playback',
    },
    {
      id: 'file-api',
      label: 'Drag & Drop',
      required: true,
      supported: detectFileAPI(),
      note: 'Loads dropped BMS folders and skins',
    },
    // Each of these switches one feature off, or makes it slower, when it is missing.
    {
      id: 'webgpu',
      label: 'WebGPU',
      required: false,
      supported: webgpu,
      note: 'Preferred renderer; WebGL2 draws without it',
    },
    {
      id: 'worker',
      label: 'Workers',
      required: false,
      supported: detectWorker(),
      note: 'Parses charts and transcodes BGA off the page; charts still parse on the page without them',
    },
    {
      id: 'webassembly',
      label: 'WebAssembly',
      required: false,
      supported: detectWebAssembly(),
      note: 'Legacy BGA video (ffmpeg.wasm) and tempo changes that keep the pitch',
    },
    {
      id: 'audio-worklet',
      label: 'AudioWorklet',
      required: false,
      supported: detectAudioWorklet(),
      note: 'Tempo changes that keep the pitch, and the sound of a WebCodecs recording',
    },
    {
      id: 'webcodecs',
      label: 'WebCodecs',
      required: false,
      supported: detectWebCodecs(),
      note: 'Hardware-accelerated BGA transcoding and gameplay recording',
    },
    {
      id: 'media-recorder',
      label: 'MediaRecorder',
      required: false,
      supported: detectMediaRecorder(),
      note: 'Gameplay recording where WebCodecs is missing',
    },
    {
      id: 'offscreen-canvas',
      label: 'OffscreenCanvas',
      required: false,
      supported: detectOffscreenCanvas(),
      note: 'Resizing beatoraja skin textures',
    },
    {
      id: 'cross-origin-isolation',
      label: 'Cross-origin isolation',
      required: false,
      supported: detectCrossOriginIsolation(),
      note: 'Faster BGA transcoding (SharedArrayBuffer flow control)',
    },
    {
      id: 'web-crypto',
      label: 'Web Crypto',
      required: false,
      supported: detectWebCrypto(),
      note: 'Chart hashes that key records, replays and the Leaderboard',
    },
    {
      id: 'indexed-db',
      label: 'IndexedDB',
      required: false,
      supported: detectIndexedDB(),
      note: 'Saved courses and the local data viewer',
    },
    {
      id: 'compression-streams',
      label: 'Compression Streams',
      required: false,
      supported: detectCompressionStreams(),
      note: 'Leaderboard uploads and replay slots',
    },
    {
      id: 'service-worker',
      label: 'Service Worker',
      required: false,
      supported: detectServiceWorker(),
      note: 'Offline play and installing the player',
    },
  ];
  const ok = items.every((item) => !item.required || item.supported);
  return { ok, items };
}
 
/**
 * Compact summary of the missing features, suitable for a single status line. Returns `undefined` when every required
 * feature is supported (callers can branch on that to skip rendering).
 */
export function summarizeBrowserCompat(report: BrowserCompatReport): string | undefined {
  if (report.ok) return undefined;
  const missing = report.items.filter((item) => item.required && !item.supported).map((item) => item.label);
  if (missing.length === 0) return undefined;
  return `Missing required: ${missing.join(', ')}`;
}
 
function detectWebGL2(): boolean {
  if (typeof document === 'undefined') return false;
  try {
    const canvas = document.createElement('canvas');
    // `failIfMajorPerformanceCaveat: false` matches Pixi's default — we want to know whether ANY WebGL2 context is
    // reachable, even on integrated GPUs the browser flags as "slow".
    return canvas.getContext('webgl2') !== null;
  } catch {
    return false;
  }
}
 
function detectAudioContext(): boolean {
  Iif (typeof globalThis === 'undefined') return false;
  // Safari on iOS still ships only the prefixed `webkitAudioContext` for some versions; treat both as "Web Audio is
  // reachable".
  return 'AudioContext' in globalThis || 'webkitAudioContext' in (globalThis as unknown as Record<string, unknown>);
}
 
function detectFileAPI(): boolean {
  Iif (typeof globalThis === 'undefined') return false;
  return 'File' in globalThis && 'FileReader' in globalThis && 'DataTransfer' in globalThis;
}
 
function detectWebAssembly(): boolean {
  return typeof globalThis !== 'undefined' && 'WebAssembly' in globalThis;
}
 
function detectWorker(): boolean {
  return typeof globalThis !== 'undefined' && 'Worker' in globalThis;
}
 
function detectWebGPU(): boolean {
  return typeof navigator !== 'undefined' && 'gpu' in navigator;
}
 
function detectWebCodecs(): boolean {
  return typeof globalThis !== 'undefined' && 'VideoEncoder' in globalThis;
}
 
function detectMediaRecorder(): boolean {
  Iif (typeof globalThis === 'undefined') return false;
  Eif (!('MediaRecorder' in globalThis)) return false;
  // `captureStream` is the bridge between the canvas and the recorder. Without it, MediaRecorder existing alone doesn't
  // help us — we'd never feed it a video track.
  if (typeof HTMLCanvasElement === 'undefined') return false;
  return typeof HTMLCanvasElement.prototype.captureStream === 'function';
}
 
function detectOffscreenCanvas(): boolean {
  return typeof globalThis !== 'undefined' && 'OffscreenCanvas' in globalThis;
}
 
function detectAudioWorklet(): boolean {
  return typeof globalThis !== 'undefined' && 'AudioWorkletNode' in globalThis;
}
 
function detectCrossOriginIsolation(): boolean {
  return typeof SharedArrayBuffer !== 'undefined' && globalThis.crossOriginIsolated === true;
}
 
function detectWebCrypto(): boolean {
  return typeof crypto !== 'undefined' && crypto.subtle !== undefined;
}
 
function detectIndexedDB(): boolean {
  return typeof globalThis !== 'undefined' && 'indexedDB' in globalThis;
}
 
function detectCompressionStreams(): boolean {
  return typeof globalThis !== 'undefined' && 'CompressionStream' in globalThis && 'DecompressionStream' in globalThis;
}
 
function detectServiceWorker(): boolean {
  return typeof navigator !== 'undefined' && 'serviceWorker' in navigator;
}