forked from dylan-sutton-chavez/edge-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidb.js
More file actions
80 lines (68 loc) · 2.4 KB
/
idb.js
File metadata and controls
80 lines (68 loc) · 2.4 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
/*
IndexedDB cache: `cas` (hash -> bytes) + `lockfile` (spec -> hash). Engine falls back to MemoryCache if `open()` rejects.
*/
const IDB_NAME = 'edgepython';
const IDB_VER = 1;
const VERSION_KEY = '\0v'; // '\0' isolates sentinel, canonical specs never contain null bytes
export class IdbCache {
constructor() {
this.db = null;
}
async open() {
this.db = await new Promise((resolve, reject) => {
const req = self.indexedDB.open(IDB_NAME, IDB_VER);
req.onupgradeneeded = () => {
const db = req.result;
if (!db.objectStoreNames.contains('cas')) db.createObjectStore('cas');
if (!db.objectStoreNames.contains('lockfile')) db.createObjectStore('lockfile');
};
req.onsuccess = () => resolve(req.result);
req.onerror = () => reject(req.error);
});
}
_tx(store, mode) {
return this.db.transaction(store, mode).objectStore(store);
}
_req(req) {
return new Promise((res, rej) => {
req.onsuccess = () => res(req.result);
req.onerror = () => rej(req.error);
});
}
getBytes(hash) {
return this._req(this._tx('cas', 'readonly').get(hash));
}
putBytes(hash, bytes) {
return this._req(this._tx('cas', 'readwrite').put(bytes, hash));
}
async loadLockfile() {
const out = new Map();
await new Promise((res, rej) => {
const r = this._tx('lockfile', 'readonly').openCursor();
r.onsuccess = () => {
const c = r.result;
if (!c) return res();
if (c.key !== VERSION_KEY) out.set(c.key, c.value);
c.continue();
};
r.onerror = () => rej(r.error);
});
return out;
}
async saveLockfile(entries) {
const s = this._tx('lockfile', 'readwrite');
let last;
for (const [k, v] of entries) last = s.put(v, k);
if (last) await this._req(last);
}
async clear() {
await this._req(this._tx('cas', 'readwrite').clear());
await this._req(this._tx('lockfile', 'readwrite').clear());
}
async setVersion(version) {
if (version) await this._req(this._tx('lockfile', 'readwrite').put(version, VERSION_KEY));
}
getVersion() {
return this._req(this._tx('lockfile', 'readonly').get(VERSION_KEY));
}
}