All files / app/core/services local-storage.service.ts

100% Statements 16/16
100% Branches 1/1
100% Functions 7/7
100% Lines 15/15

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            1x 43x     44x       17x 17x 2x   15x       14x       4x       7x       3x       5x 5x 6x   5x    
import { Injectable } from '@angular/core';
import { IStorageProvider } from '../domain/interfaces/storage.interface';
 
@Injectable({
  providedIn: 'root'
})
export class LocalStorageService implements IStorageProvider {
  private readonly storage = new Map<string, Uint8Array>();
 
  async write(path: string, data: Uint8Array): Promise<void> {
    this.storage.set(path, data);
  }
 
  async read(path: string): Promise<Uint8Array> {
    const data = this.storage.get(path);
    if (!data) {
      throw new Error(`File not found: ${path}`);
    }
    return data;
  }
 
  async exists(path: string): Promise<boolean> {
    return this.storage.has(path);
  }
 
  async delete(path: string): Promise<void> {
    this.storage.delete(path);
  }
 
  async list(): Promise<string[]> {
    return Array.from(this.storage.keys());
  }
 
  async clear(): Promise<void> {
    this.storage.clear();
  }
 
  async getSize(): Promise<number> {
    let totalSize = 0;
    for (const data of this.storage.values()) {
      totalSize += data.length;
    }
    return totalSize;
  }
}