forked from jcubic/fake-linux-terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryDB.js
More file actions
134 lines (129 loc) · 3.57 KB
/
MemoryDB.js
File metadata and controls
134 lines (129 loc) · 3.57 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
// copied from lighting-fs
class IdbBackend {
constructor(dbname, storename) {
this._database = dbname;
this._storename = storename;
this._ready = import('https://cdn.jsdelivr.net/npm/@isomorphic-git/idb-keyval@3.3.2/dist/idb-keyval.mjs').then(idb => {
this._idb = idb;
this._store = new idb.Store(this._database, this._storename);
});
}
async saveSuperblock(superblock) {
await this._ready;
return this._idb.set("!root", superblock, this._store);
}
async loadSuperblock() {
await this._ready;
return this._idb.get("!root", this._store);
}
readFile(inode) {
return this._idb.get(inode, this._store)
}
writeFile(inode, data) {
return this._idb.set(inode, data, this._store)
}
unlink(inode) {
return this._idb.del(inode, this._store)
}
async wipe() {
await this._ready;
return this._idb.clear(this._store)
}
close() {
return this._idb.close(this._store)
}
}
class BackendDB {
constructor(data) {
if (typeof data === 'string') {
this._idb = new IdbBackend(data, data + '_files');
} else if (data) {
this.load(data);
} else {
this._store = new Map();
}
}
saveSuperblock(superblock) {
if (this._idb) {
return this._idb.saveSuperblock(superblock);
}
this._store.set('!root', superblock);
}
loadSuperblock() {
if (this._idb) {
return this._idb.loadSuperblock();
}
return this._store.get('!root');
}
readFile(inode) {
if (this._idb) {
return this._idb.readFile(inode);
}
return this._store.get(inode);
}
writeFile(inode, data) {
if (this._idb) {
return this._idb.writeFile(inode, data);
}
this._store.set(inode, data);
}
unlink(inode) {
if (this._idb) {
return this._idb.unlink(inode);
}
this._store.delete(inode);
}
wipe() {
if (this._idb) {
return this._idb.wipe();
}
this._store = new Map();
}
close() {
if (this._idb) {
return this._idb.close();
}
}
dump() {
const sb = this._store.get('!root');
const result = {
superBlock: seralizeSuperBlock(sb),
inodes: Array.from(this._store.entries()).filter(([key, value]) => {
return key !== '!root';
}).map(([key, value]) => {
return [key, Array.from(value)];
})
};
return result;
}
async load(data) {
await this.wipe();
await this.saveSuperblock(unserializeSuperBlock(data.superBlock));
await Promise.all(data.inodes.map(([inode, data]) => {
return this.writeFile(inode, Uint8Array.from(data));
}));
}
persistent(name) {
const dump = this.dump();
this._idb = new IdbBackend(name, name + '_files');
this.load(dump);
}
}
function seralizeSuperBlock(sb) {
const arr = Array.from(sb.entries());
return arr.map(([key, value]) => {
if (value instanceof Map) {
return [key, seralizeSuperBlock(value)];
}
return [key, value];
});
}
function unserializeSuperBlock(arr) {
return new Map(arr.map(([key, value]) => {
if (value instanceof Array) {
return [key, unserializeSuperBlock(value)];
}
return [key, value];
}));
}
export default BackendDB;