All files / src/utils EnvironmentDiagnostics.ts

100% Statements 12/12
61.11% Branches 11/18
100% Functions 4/4
100% Lines 12/12

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                                1x 1x                       1x                     2x   2x 2x   2x 2x     2x                                     1x 1x 1x                
import { ISecurityStatus, ICapabilities } from "../types.js";
 
/**
 * 2026 Zenith Tier: 実行環境のセキュリティおよび能力を診断します。
 * 特に WASM Threads に必要な COOP/COEP ヘッダーの状態を検証します。
 */
export class EnvironmentDiagnostics {
  /**
   * 統合された環境レポートを取得します。
   */
  static async getReport(): Promise<{
    browser: string;
    capabilities: ICapabilities;
    security: ISecurityStatus;
  }> {
    const { EnvironmentDetector } =
      await import("../capabilities/EnvironmentDetector.js");
    return {
      browser:
        typeof navigator !== "undefined" ? navigator.userAgent : "unknown",
      capabilities: await EnvironmentDetector.detect(),
      security: this.getSecurityStatus(),
    };
  }
 
  /**
   * 高精度タイマーが利用可能かチェックします。
   */
  static hasHighResTimer(): boolean {
    return (
      typeof performance !== "undefined" &&
      typeof performance.now === "function"
    );
  }
 
  /**
   * 現在の環境のセキュリティ状態を取得します。
   */
  static getSecurityStatus(): ISecurityStatus {
    const isCrossOriginIsolated =
      typeof crossOriginIsolated !== "undefined" ? crossOriginIsolated : false;
 
    const missingHeaders: string[] = [];
    Eif (!isCrossOriginIsolated) {
      // 厳密な判定はサーバーサイドで行う必要があるが、クライアント側でも推測可能
      missingHeaders.push("Cross-Origin-Opener-Policy: same-origin");
      missingHeaders.push("Cross-Origin-Embedder-Policy: require-corp");
    }
 
    return {
      sriEnabled: true, // Core 側で強制
      coopCoepEnabled: isCrossOriginIsolated,
      sriSupported:
        typeof document !== "undefined" &&
        typeof document.createElement === "function"
          ? "integrity" in document.createElement("script")
          : false,
      canUseThreads:
        isCrossOriginIsolated && typeof SharedArrayBuffer !== "undefined",
      isCrossOriginIsolated,
      missingHeaders: missingHeaders.length > 0 ? missingHeaders : undefined,
    };
  }
 
  /**
   * 環境の能力が不足している場合に警告を出力します。
   */
  static warnIfSuboptimal(): void {
    const status = this.getSecurityStatus();
    Eif (!status.isCrossOriginIsolated) {
      console.warn(
        "[Zenith Tier] Environment is not cross-origin isolated. WASM Threads will be disabled. " +
          "Ensure your server sends COOP and COEP headers: ",
        status.missingHeaders,
      );
    }
  }
}