All files / src/storage IndexedDBStorage.ts

73.46% Statements 72/98
20% Branches 5/25
61.76% Functions 21/34
77.01% Lines 67/87

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            6x 6x 15x           2x       2x 2x 2x 2x       2x 2x   2x 2x 2x                   9x 9x 9x 9x       9x 9x   9x 9x 9x                   1x 1x 1x 1x       1x 1x   1x 1x 1x                   9x 9x 9x 9x       9x 9x   9x 9x 9x                   1x 1x 1x 1x       1x 1x   1x 1x 1x                     24x 17x   17x 17x   1x       8x 8x 8x 7x 7x 7x     8x 8x 8x 1x   8x     8x   8x                                
import { IFileStorage } from "../types.js";
 
/**
 * 2026 Zenith Tier: IndexedDB を使用した物理ストレージ実装。
 */
export class IndexedDBStorage implements IFileStorage {
  private static readonly DB_NAME = "multi-game-engines";
  private static readonly STORE_NAME = "engine-cache";
  private db: IDBDatabase | null = null;
 
  /**
   * テスト用: 内部 DB インスタンスを取得します。
   */
  async getDB(): Promise<IDBDatabase> {
    return this.ensureDb();
  }
 
  async get(key: string): Promise<ArrayBuffer | null> {
    const db = await this.ensureDb();
    return new Promise((resolve, reject) => {
      try {
        const transaction = db.transaction(
          [IndexedDBStorage.STORE_NAME],
          "readonly",
        );
        const store = transaction.objectStore(IndexedDBStorage.STORE_NAME);
        const request = store.get(key);
 
        request.onsuccess = () => resolve(request.result || null);
        request.onerror = () => reject(request.error);
        transaction.onabort = () =>
          reject(transaction.error || new Error("Transaction aborted"));
      } catch (err) {
        this.handleDbError(err);
        reject(err);
      }
    });
  }
 
  async set(key: string, data: ArrayBuffer): Promise<void> {
    const db = await this.ensureDb();
    return new Promise((resolve, reject) => {
      try {
        const transaction = db.transaction(
          [IndexedDBStorage.STORE_NAME],
          "readwrite",
        );
        const store = transaction.objectStore(IndexedDBStorage.STORE_NAME);
        const request = store.put(data, key);
 
        request.onsuccess = () => resolve();
        request.onerror = () => reject(request.error);
        transaction.onabort = () =>
          reject(transaction.error || new Error("Transaction aborted"));
      } catch (err) {
        this.handleDbError(err);
        reject(err);
      }
    });
  }
 
  async delete(key: string): Promise<void> {
    const db = await this.ensureDb();
    return new Promise((resolve, reject) => {
      try {
        const transaction = db.transaction(
          [IndexedDBStorage.STORE_NAME],
          "readwrite",
        );
        const store = transaction.objectStore(IndexedDBStorage.STORE_NAME);
        const request = store.delete(key);
 
        request.onsuccess = () => resolve();
        request.onerror = () => reject(request.error);
        transaction.onabort = () =>
          reject(transaction.error || new Error("Transaction aborted"));
      } catch (err) {
        this.handleDbError(err);
        reject(err);
      }
    });
  }
 
  async has(key: string): Promise<boolean> {
    const db = await this.ensureDb();
    return new Promise((resolve, reject) => {
      try {
        const transaction = db.transaction(
          [IndexedDBStorage.STORE_NAME],
          "readonly",
        );
        const store = transaction.objectStore(IndexedDBStorage.STORE_NAME);
        const request = store.count(key);
 
        request.onsuccess = () => resolve(request.result > 0);
        request.onerror = () => reject(request.error);
        transaction.onabort = () =>
          reject(transaction.error || new Error("Transaction aborted"));
      } catch (err) {
        this.handleDbError(err);
        reject(err);
      }
    });
  }
 
  async clear(): Promise<void> {
    const db = await this.ensureDb();
    return new Promise((resolve, reject) => {
      try {
        const transaction = db.transaction(
          [IndexedDBStorage.STORE_NAME],
          "readwrite",
        );
        const store = transaction.objectStore(IndexedDBStorage.STORE_NAME);
        const request = store.clear();
 
        request.onsuccess = () => resolve();
        request.onerror = () => reject(request.error);
        transaction.onabort = () =>
          reject(transaction.error || new Error("Transaction aborted"));
      } catch (err) {
        this.handleDbError(err);
        reject(err);
      }
    });
  }
 
  private async ensureDb(): Promise<IDBDatabase> {
    // 物理的な接続ロスや InvalidState を検知
    if (this.db) {
      try {
        // ダミーのトランザクションで生存確認
        this.db.transaction([IndexedDBStorage.STORE_NAME], "readonly");
        return this.db;
      } catch {
        this.db = null;
      }
    }
 
    return new Promise((resolve, reject) => {
      const request = indexedDB.open(IndexedDBStorage.DB_NAME, 1);
      request.onupgradeneeded = () => {
        const db = request.result;
        Eif (!db.objectStoreNames.contains(IndexedDBStorage.STORE_NAME)) {
          db.createObjectStore(IndexedDBStorage.STORE_NAME);
        }
      };
      request.onsuccess = () => {
        this.db = request.result;
        this.db.onclose = () => {
          this.db = null;
        };
        this.db.onerror = () => {
          this.db = null;
        };
        resolve(this.db);
      };
      request.onerror = () => reject(request.error);
    });
  }
 
  private handleDbError(err: unknown): void {
    if (err && typeof err === "object" && "name" in err) {
      const error = err as { name: string };
      if (
        error.name === "InvalidStateError" ||
        error.name === "TransactionInactiveError"
      ) {
        this.db = null;
      }
    }
  }
}