All files / src/storage index.ts

87.5% Statements 7/8
100% Branches 7/7
100% Functions 1/1
87.5% Lines 7/8

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                      3x       1x 1x             2x 1x     1x 1x                
import { ICapabilities, IFileStorage } from "../types.js";
import { OPFSStorage } from "./OPFSStorage.js";
import { IndexedDBStorage } from "./IndexedDBStorage.js";
import { NodeFSStorage } from "./NodeFSStorage.js";
import { MemoryStorage } from "./MemoryStorage.js";
 
/**
 * 実行環境に最適なストレージ実装を作成します。
 */
export function createFileStorage(capabilities: ICapabilities): IFileStorage {
  // Node.js/Bun 環境の場合 (CLI)
  if (
    typeof process !== "undefined" &&
    (process.versions?.node || process.versions?.bun)
  ) {
    try {
      return new NodeFSStorage();
    } catch {
      // フォールバック
    }
  }
 
  // Web ブラウザ環境
  if (capabilities.opfs) {
    return new OPFSStorage();
  }
 
  try {
    return new IndexedDBStorage();
  } catch {
    // 最終的なフォールバック
    return new MemoryStorage();
  }
}
 
export { OPFSStorage, IndexedDBStorage, NodeFSStorage, MemoryStorage };