UNPKG

meta-log-db

Version:

Native database package for Meta-Log (ProLog, DataLog, R5RS)

176 lines 5.25 kB
"use strict"; /** * Browser File I/O Abstraction Layer * * Provides file loading from URLs/public directory and IndexedDB caching */ Object.defineProperty(exports, "__esModule", { value: true }); exports.BrowserFileIO = void 0; const indexeddb_storage_js_1 = require("./indexeddb-storage.js"); /** * Browser File I/O Manager */ class BrowserFileIO { constructor(config = {}) { this.cache = new Map(); this.storage = null; this.enableCache = config.enableCache !== false; this.cacheStrategy = config.cacheStrategy || 'both'; if (this.enableCache && (this.cacheStrategy === 'indexeddb' || this.cacheStrategy === 'both')) { this.storage = new indexeddb_storage_js_1.IndexedDBStorage({ dbName: config.indexedDBName }); } } /** * Initialize storage (async initialization) */ async init() { if (this.storage) { await this.storage.init(); } } /** * Load file from URL/public directory */ async loadFromURL(url) { try { const response = await fetch(url); if (!response.ok) { throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`); } const content = await response.text(); // Cache the content if (this.enableCache) { await this.saveToCache(url, content); } return content; } catch (error) { throw new Error(`Error loading file from URL ${url}: ${error}`); } } /** * Load file from IndexedDB cache */ async loadFromIndexedDB(key) { if (!this.storage) { return null; } try { const cached = await this.storage.get('files', key); return cached; } catch (error) { console.warn(`Failed to load from IndexedDB: ${error}`); return null; } } /** * Save file to IndexedDB cache */ async saveToIndexedDB(key, content) { if (!this.storage) { return; } try { await this.storage.set('files', key, content); } catch (error) { console.warn(`Failed to save to IndexedDB: ${error}`); } } /** * Check if file exists in IndexedDB cache */ async existsInIndexedDB(key) { if (!this.storage) { return false; } try { return await this.storage.has('files', key); } catch (error) { return false; } } /** * Load file with fallback strategy: * 1. Try IndexedDB cache * 2. Try fetch from URL/public directory * 3. Cache in IndexedDB if successful */ async loadFile(path, url) { const fileUrl = url || path; // Try cache first if enabled if (this.enableCache) { // Try memory cache if (this.cacheStrategy === 'memory' || this.cacheStrategy === 'both') { if (this.cache.has(path)) { return this.cache.get(path); } } // Try IndexedDB cache if (this.cacheStrategy === 'indexeddb' || this.cacheStrategy === 'both') { const cached = await this.loadFromIndexedDB(path); if (cached !== null) { // Also store in memory cache if using both strategy if (this.cacheStrategy === 'both') { this.cache.set(path, cached); } return cached; } } } // Fetch from URL const content = await this.loadFromURL(fileUrl); // Cache the content if (this.enableCache) { await this.saveToCache(path, content); } return content; } /** * Save to cache (memory and/or IndexedDB) */ async saveToCache(key, content) { if (this.cacheStrategy === 'memory' || this.cacheStrategy === 'both') { this.cache.set(key, content); } if ((this.cacheStrategy === 'indexeddb' || this.cacheStrategy === 'both') && this.storage) { await this.saveToIndexedDB(key, content); } } /** * Clear cache */ async clearCache() { this.cache.clear(); if (this.storage) { await this.storage.clear('files'); } } /** * Clear specific file from cache */ async clearFileCache(key) { this.cache.delete(key); if (this.storage) { await this.storage.delete('files', key); } } /** * Check if file is cached */ async isCached(key) { if (this.cacheStrategy === 'memory' || this.cacheStrategy === 'both') { if (this.cache.has(key)) { return true; } } if (this.cacheStrategy === 'indexeddb' || this.cacheStrategy === 'both') { return await this.existsInIndexedDB(key); } return false; } } exports.BrowserFileIO = BrowserFileIO; //# sourceMappingURL=io.js.map