-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathstockfishStorage.ts
More file actions
134 lines (113 loc) · 3.58 KB
/
stockfishStorage.ts
File metadata and controls
134 lines (113 loc) · 3.58 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
interface NnueStorage {
id: string
url: string
data: Blob
timestamp: number
size: number
}
export class StockfishModelStorage {
private dbName = 'StockfishModels'
private storeName = 'models'
private version = 1
private db: IDBDatabase | null = null
async openDB(): Promise<IDBDatabase | null> {
if (typeof indexedDB === 'undefined') {
return null
}
if (this.db) return this.db
return new Promise((resolve, reject) => {
const request = indexedDB.open(this.dbName, this.version)
request.onerror = () => reject(request.error)
request.onsuccess = () => {
this.db = request.result
resolve(request.result)
}
request.onupgradeneeded = (event) => {
const db = (event.target as IDBOpenDBRequest).result
if (!db.objectStoreNames.contains(this.storeName)) {
const store = db.createObjectStore(this.storeName, { keyPath: 'id' })
store.createIndex('timestamp', 'timestamp', { unique: false })
}
}
})
}
async getModel(modelUrl: string): Promise<ArrayBuffer | null> {
try {
const db = await this.openDB()
if (!db) return null
const transaction = db.transaction([this.storeName], 'readonly')
const store = transaction.objectStore(this.storeName)
const modelData = await new Promise<NnueStorage | null>(
(resolve, reject) => {
const request = store.get(modelUrl)
request.onsuccess = () => resolve(request.result || null)
request.onerror = () => reject(request.error)
},
)
if (!modelData) {
return null
}
if (modelData.url !== modelUrl) {
await this.deleteModel(modelUrl)
return null
}
return modelData.data.arrayBuffer()
} catch (error) {
console.warn('Stockfish cache read failed:', error)
return null
}
}
async storeModel(modelUrl: string, buffer: ArrayBuffer): Promise<void> {
try {
const db = await this.openDB()
if (!db) return
const transaction = db.transaction([this.storeName], 'readwrite')
const store = transaction.objectStore(this.storeName)
const modelData: NnueStorage = {
id: modelUrl,
url: modelUrl,
data: new Blob([buffer]),
timestamp: Date.now(),
size: buffer.byteLength,
}
await new Promise<void>((resolve, reject) => {
const request = store.put(modelData)
request.onsuccess = () => resolve()
request.onerror = () => reject(request.error)
})
} catch (error) {
console.warn('Stockfish cache write failed:', error)
}
}
async deleteModel(modelUrl: string): Promise<void> {
try {
const db = await this.openDB()
if (!db) return
const transaction = db.transaction([this.storeName], 'readwrite')
const store = transaction.objectStore(this.storeName)
await new Promise<void>((resolve, reject) => {
const request = store.delete(modelUrl)
request.onsuccess = () => resolve()
request.onerror = () => reject(request.error)
})
} catch (error) {
console.warn('Stockfish cache delete failed:', error)
}
}
async requestPersistentStorage(): Promise<boolean> {
try {
if (
typeof navigator !== 'undefined' &&
'storage' in navigator &&
'persist' in navigator.storage
) {
return navigator.storage.persist()
}
return false
} catch (error) {
console.warn('Failed to request persistent storage:', error)
return false
}
}
}
export default StockfishModelStorage