|
| 1 | +interface NnueStorage { |
| 2 | + id: string |
| 3 | + url: string |
| 4 | + data: Blob |
| 5 | + timestamp: number |
| 6 | + size: number |
| 7 | +} |
| 8 | + |
| 9 | +export class StockfishModelStorage { |
| 10 | + private dbName = 'StockfishModels' |
| 11 | + private storeName = 'models' |
| 12 | + private version = 1 |
| 13 | + private db: IDBDatabase | null = null |
| 14 | + |
| 15 | + async openDB(): Promise<IDBDatabase | null> { |
| 16 | + if (typeof indexedDB === 'undefined') { |
| 17 | + return null |
| 18 | + } |
| 19 | + |
| 20 | + if (this.db) return this.db |
| 21 | + |
| 22 | + return new Promise((resolve, reject) => { |
| 23 | + const request = indexedDB.open(this.dbName, this.version) |
| 24 | + |
| 25 | + request.onerror = () => reject(request.error) |
| 26 | + request.onsuccess = () => { |
| 27 | + this.db = request.result |
| 28 | + resolve(request.result) |
| 29 | + } |
| 30 | + |
| 31 | + request.onupgradeneeded = (event) => { |
| 32 | + const db = (event.target as IDBOpenDBRequest).result |
| 33 | + if (!db.objectStoreNames.contains(this.storeName)) { |
| 34 | + const store = db.createObjectStore(this.storeName, { keyPath: 'id' }) |
| 35 | + store.createIndex('timestamp', 'timestamp', { unique: false }) |
| 36 | + } |
| 37 | + } |
| 38 | + }) |
| 39 | + } |
| 40 | + |
| 41 | + async getModel(modelUrl: string): Promise<ArrayBuffer | null> { |
| 42 | + try { |
| 43 | + const db = await this.openDB() |
| 44 | + if (!db) return null |
| 45 | + |
| 46 | + const transaction = db.transaction([this.storeName], 'readonly') |
| 47 | + const store = transaction.objectStore(this.storeName) |
| 48 | + |
| 49 | + const modelData = await new Promise<NnueStorage | null>( |
| 50 | + (resolve, reject) => { |
| 51 | + const request = store.get(modelUrl) |
| 52 | + request.onsuccess = () => resolve(request.result || null) |
| 53 | + request.onerror = () => reject(request.error) |
| 54 | + }, |
| 55 | + ) |
| 56 | + |
| 57 | + if (!modelData) { |
| 58 | + return null |
| 59 | + } |
| 60 | + |
| 61 | + if (modelData.url !== modelUrl) { |
| 62 | + await this.deleteModel(modelUrl) |
| 63 | + return null |
| 64 | + } |
| 65 | + |
| 66 | + return modelData.data.arrayBuffer() |
| 67 | + } catch (error) { |
| 68 | + console.warn('Stockfish cache read failed:', error) |
| 69 | + return null |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + async storeModel(modelUrl: string, buffer: ArrayBuffer): Promise<void> { |
| 74 | + try { |
| 75 | + const db = await this.openDB() |
| 76 | + if (!db) return |
| 77 | + |
| 78 | + const transaction = db.transaction([this.storeName], 'readwrite') |
| 79 | + const store = transaction.objectStore(this.storeName) |
| 80 | + |
| 81 | + const modelData: NnueStorage = { |
| 82 | + id: modelUrl, |
| 83 | + url: modelUrl, |
| 84 | + data: new Blob([buffer]), |
| 85 | + timestamp: Date.now(), |
| 86 | + size: buffer.byteLength, |
| 87 | + } |
| 88 | + |
| 89 | + await new Promise<void>((resolve, reject) => { |
| 90 | + const request = store.put(modelData) |
| 91 | + request.onsuccess = () => resolve() |
| 92 | + request.onerror = () => reject(request.error) |
| 93 | + }) |
| 94 | + } catch (error) { |
| 95 | + console.warn('Stockfish cache write failed:', error) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + async deleteModel(modelUrl: string): Promise<void> { |
| 100 | + try { |
| 101 | + const db = await this.openDB() |
| 102 | + if (!db) return |
| 103 | + |
| 104 | + const transaction = db.transaction([this.storeName], 'readwrite') |
| 105 | + const store = transaction.objectStore(this.storeName) |
| 106 | + |
| 107 | + await new Promise<void>((resolve, reject) => { |
| 108 | + const request = store.delete(modelUrl) |
| 109 | + request.onsuccess = () => resolve() |
| 110 | + request.onerror = () => reject(request.error) |
| 111 | + }) |
| 112 | + } catch (error) { |
| 113 | + console.warn('Stockfish cache delete failed:', error) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + async requestPersistentStorage(): Promise<boolean> { |
| 118 | + try { |
| 119 | + if ( |
| 120 | + typeof navigator !== 'undefined' && |
| 121 | + 'storage' in navigator && |
| 122 | + 'persist' in navigator.storage |
| 123 | + ) { |
| 124 | + return navigator.storage.persist() |
| 125 | + } |
| 126 | + return false |
| 127 | + } catch (error) { |
| 128 | + console.warn('Failed to request persistent storage:', error) |
| 129 | + return false |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +export default StockfishModelStorage |
0 commit comments