All files / src/storage MemoryStorage.ts

100% Statements 12/12
100% Branches 2/2
100% Functions 6/6
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              5x     2x 2x         5x 5x       1x       5x       1x       1x 1x 1x     1x      
import { IFileStorage } from "../types.js";
 
/**
 * 揮発性のオンメモリストレージ。
 * キャッシュ制限が厳しい環境や、プライベートブラウジング等で使用される。
 */
export class MemoryStorage implements IFileStorage {
  private cache = new Map<string, ArrayBuffer>();
 
  async get(key: string): Promise<ArrayBuffer | null> {
    const data = this.cache.get(key);
    return data || null;
  }
 
  async set(key: string, data: ArrayBuffer): Promise<void> {
    // 2026: Copy the data buffer to avoid mutation
    const copy = data.slice(0);
    this.cache.set(key, copy);
  }
 
  async delete(key: string): Promise<void> {
    this.cache.delete(key);
  }
 
  async has(key: string): Promise<boolean> {
    return this.cache.has(key);
  }
 
  async clear(): Promise<void> {
    this.cache.clear();
  }
 
  async getQuota(): Promise<{ usage: number; quota: number }> {
    let usage = 0;
    for (const data of this.cache.values()) {
      usage += data.byteLength;
    }
    // メモリ上限は安全な 128MB 程度
    return { usage, quota: 128 * 1024 * 1024 };
  }
}