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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
|
/**
* Virtual File System (VFS) using IndexedDB with Bash-like Commands
* (C) 2026 by Michael Peter Christen
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program in the file lgpl21.txt
* If not, see <http://www.gnu.org/licenses/>.
*/
/**
* This script implements a simple key-value store using IndexedDB, which is a low-level API
* for client-side storage of significant amounts of structured data, including files/blobs.
* This API uses indexes to enable high-performance searches of this data.
*
* The VFS provides a set of methods for storing, retrieving, and deleting key-value pairs in an
* IndexedDB object store, where keys represent paths in a file system-like structure.
* Paths are delimited by a slash (/) and must start with a leading slash.
* Paths ending with a slash are considered directories, while paths not ending with a slash are considered files.
*
* The VFS includes Bash-like commands for manipulating the virtual file system.
*
* The VFS is exposed as a global object (window.vfs) for easy access from other parts of the application.
*/
// Open a connection to the database
const openRequest = indexedDB.open('vfs', 1);
let vfsReadyResolve;
let vfsReadyReject;
window.vfsReady = new Promise((resolve, reject) => {
vfsReadyResolve = resolve;
vfsReadyReject = reject;
});
let db;
const applyUnifiedDiff = (originalText, diffText) => {
const originalEndsWithNewline = originalText.endsWith('\n');
const originalLines = originalText.split('\n');
const diffLines = diffText.split('\n');
const result = [];
let origIndex = 0;
let i = 0;
const hunkHeader = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
const collectHunkOps = (startIndex) => {
const ops = [];
let index = startIndex;
while (index < diffLines.length && !diffLines[index].startsWith('@@')) {
const hunkLine = diffLines[index];
if (hunkLine.startsWith(' ') || hunkLine.startsWith('-') || hunkLine.startsWith('+')) {
ops.push(hunkLine);
}
index += 1;
}
return { ops, nextIndex: index };
};
const hunkFitsAt = (startAt, ops) => {
let checkIndex = startAt;
for (const op of ops) {
if (op.startsWith(' ') || op.startsWith('-')) {
const content = op.slice(1);
if (originalLines[checkIndex] !== content) {
return false;
}
checkIndex += 1;
}
}
return true;
};
const findHunkStart = (fromIndex, ops, suggestedStart) => {
if (Number.isInteger(suggestedStart) && suggestedStart >= fromIndex) {
if (hunkFitsAt(suggestedStart, ops)) {
return suggestedStart;
}
}
const firstContext = ops.find((op) => op.startsWith(' '));
if (!firstContext) {
return fromIndex;
}
const needle = firstContext.slice(1);
for (let idx = fromIndex; idx < originalLines.length; idx += 1) {
if (originalLines[idx] === needle && hunkFitsAt(idx, ops)) {
return idx;
}
}
return -1;
};
while (i < diffLines.length) {
const line = diffLines[i];
if (
line.startsWith('diff --git') ||
line.startsWith('index ') ||
line.startsWith('--- ') ||
line.startsWith('+++ ')
) {
i += 1;
continue;
}
const match = line.match(hunkHeader);
if (!match) {
i += 1;
continue;
}
const oldStart = parseInt(match[1], 10) - 1;
const { ops, nextIndex } = collectHunkOps(i + 1);
const hunkStart = findHunkStart(origIndex, ops, oldStart);
if (hunkStart < 0) {
throw new Error('Patch context mismatch.');
}
while (origIndex < hunkStart && origIndex < originalLines.length) {
result.push(originalLines[origIndex]);
origIndex += 1;
}
for (const op of ops) {
if (op.startsWith(' ')) {
const content = op.slice(1);
if (originalLines[origIndex] !== content) {
throw new Error('Patch context mismatch.');
}
result.push(content);
origIndex += 1;
} else if (op.startsWith('-')) {
const content = op.slice(1);
if (originalLines[origIndex] !== content) {
throw new Error('Patch removal mismatch.');
}
origIndex += 1;
} else if (op.startsWith('+')) {
result.push(op.slice(1));
}
}
i = nextIndex;
}
while (origIndex < originalLines.length) {
result.push(originalLines[origIndex]);
origIndex += 1;
}
let patched = result.join('\n');
if (originalEndsWithNewline && !patched.endsWith('\n')) {
patched += '\n';
}
return patched;
};
// Handle the database upgrade event
openRequest.onupgradeneeded = function (event) {
const db = event.target.result;
// Check if the object store exists before creating it
if (!db.objectStoreNames.contains('keyValueStore')) {
db.createObjectStore('keyValueStore', { keyPath: 'id' });
}
};
// Handle the successful opening of the database
openRequest.onsuccess = function (event) {
db = event.target.result;
// Define the vfs object with methods to interact with the database
const vfs = {
put: function (key, value) {
return new Promise((resolve, reject) => {
const transaction = db.transaction(['keyValueStore'], 'readwrite');
const store = transaction.objectStore('keyValueStore');
const putRequest = store.put({ id: key, value });
// Handle the successful storage of a key-value pair
putRequest.onsuccess = function (event) {
console.log('Key-value pair stored successfully.');
resolve();
};
// Handle errors
putRequest.onerror = function (event) {
console.error('Error storing key-value pair:', event.target.errorCode);
reject(event.target.errorCode);
};
});
},
getasync: function (key) {
return new Promise((resolve, reject) => {
const transaction = db.transaction(['keyValueStore'], 'readonly');
const store = transaction.objectStore('keyValueStore');
const getRequest = store.get(key);
getRequest.onsuccess = function (event) {
// Check if the key exists before resolving the promise
if (event.target.result) {
resolve(event.target.result.value);
} else {
reject('Key not found');
}
};
getRequest.onerror = function (event) {
reject(event.target.errorCode);
};
});
},
get: async function (key) {
return await this.getasync(key);
},
rm: function (key) {
const transaction = db.transaction(['keyValueStore'], 'readwrite');
const store = transaction.objectStore('keyValueStore');
const deleteRequest = store.delete(key);
deleteRequest.onsuccess = function (event) {
console.log('Entry removed successfully.');
};
deleteRequest.onerror = function (event) {
console.error('Error removing entry:', event.target.errorCode);
};
},
// the following methods all consider that keys are paths and have their proper shape:
// - a path must have a leading slash
// - a path ending with a slash is considered a directory
// - a path not ending with a slash is considered a file
// - directories cannot be created or removed directly,
// - creating a file creates also the parent directory, removing all files removes the directory.
touch: function (path) {
// Create a file at the specified path.
if (!path.startsWith('/') || path === '') {
throw new Error('Invalid path');
}
const dirPath = path.substring(0, path.lastIndexOf('/')) + '/';
const fileName = path.substring(path.lastIndexOf('/') + 1);
const transaction = db.transaction(['keyValueStore'], 'readwrite');
const store = transaction.objectStore('keyValueStore');
const putRequest = store.put({ id: path, value: '' });
putRequest.onsuccess = function (event) {
console.log(`File created at ${path}`);
};
putRequest.onerror = function (event) {
console.error(`Error creating file at ${path}: ${event.target.errorCode}`);
};
},
rm: function (path) {
// Remove a file or directory at the specified path.
if (!path.startsWith('/') || path === '') {
throw new Error('Invalid path');
}
const transaction = db.transaction(['keyValueStore'], 'readwrite');
const store = transaction.objectStore('keyValueStore');
const deleteRequest = store.delete(path);
deleteRequest.onsuccess = function (event) {
console.log(`Entry removed at ${path}`);
};
deleteRequest.onerror = function (event) {
console.error(`Error removing entry at ${path}: ${event.target.errorCode}`);
};
},
cp: function (srcPath, destPath) {
// Copy a file or directory from one path to another.
if (!srcPath.startsWith('/') || !destPath.startsWith('/') || srcPath === '' || destPath === '') {
throw new Error('Invalid path');
}
const transaction = db.transaction(['keyValueStore'], 'readwrite');
const store = transaction.objectStore('keyValueStore');
const getRequest = store.get(srcPath);
getRequest.onsuccess = function (event) {
const value = event.target.result ? event.target.result.value : {};
const putRequest = store.put({ id: destPath, value });
putRequest.onsuccess = function (event) {
console.log(`Entry copied from ${srcPath} to ${destPath}`);
};
putRequest.onerror = function (event) {
console.error(`Error copying entry from ${srcPath} to ${destPath}: ${event.target.errorCode}`);
};
};
getRequest.onerror = function (event) {
console.error(`Error getting entry at ${srcPath}: ${event.target.errorCode}`);
};
},
mv: function (srcPath, destPath) {
// Move or rename a file or directory from one path to another.
if (!srcPath.startsWith('/') || !destPath.startsWith('/') || srcPath === '' || destPath === '') {
throw new Error('Invalid path');
}
const transaction = db.transaction(['keyValueStore'], 'readwrite');
const store = transaction.objectStore('keyValueStore');
const getRequest = store.get(srcPath);
getRequest.onsuccess = function (event) {
const value = event.target.result ? event.target.result.value : {};
const deleteRequest = store.delete(srcPath);
deleteRequest.onsuccess = function (event) {
const putRequest = store.put({ id: destPath, value });
putRequest.onsuccess = function (event) {
console.log(`Entry moved from ${srcPath} to ${destPath}`);
};
putRequest.onerror = function (event) {
console.error(`Error moving entry from ${srcPath} to ${destPath}: ${event.target.errorCode}`);
};
};
deleteRequest.onerror = function (event) {
console.error(`Error deleting entry at ${srcPath}: ${event.target.errorCode}`);
};
};
getRequest.onerror = function (event) {
console.error(`Error getting entry at ${srcPath}: ${event.target.errorCode}`);
};
},
applyDiff: async function (path, diffText) {
if (!path.startsWith('/') || path === '' || path.endsWith('/')) {
throw new Error('Invalid file path');
}
if (typeof diffText !== 'string') {
throw new Error('Invalid diff');
}
const current = await this.getasync(path);
const next = applyUnifiedDiff(String(current || ''), diffText);
await this.put(path, next);
return next;
},
ls: function (path) {
// List the contents of a directory at the specified path.
if (!path.endsWith('/')) {
throw new Error('Invalid directory path');
}
const transaction = db.transaction(['keyValueStore'], 'readonly');
const store = transaction.objectStore('keyValueStore');
const cursorRange = path === '/' ? IDBKeyRange.lowerBound(path) : IDBKeyRange.bound(path, path.substring(0, path.length - 1) + '\uffff', false, true);
return new Promise((resolve, reject) => {
const cursorRequest = store.openCursor(cursorRange);
const contents = [];
cursorRequest.onsuccess = function (event) {
const cursor = event.target.result;
if (cursor) {
if (cursor.key.startsWith(path)) {
contents.push(cursor.key.substring(path.length));
}
cursor.continue();
} else {
resolve(contents);
}
};
cursorRequest.onerror = function (event) {
reject(`Error listing directory at ${path}: ${event.target.errorCode}`);
};
});
},
normalizeDirPath: function (path) {
if (!path || path === '/') {
return '/';
}
if (!path.startsWith('/')) {
throw new Error('Invalid directory path');
}
return path.endsWith('/') ? path : `${path}/`;
},
parentDir: function (path) {
if (!path || path === '/') {
return '/';
}
if (!path.startsWith('/')) {
throw new Error('Invalid path');
}
if (path.endsWith('/')) {
return this.normalizeDirPath(path);
}
const index = path.lastIndexOf('/');
return index <= 0 ? '/' : path.substring(0, index + 1);
},
baseName: function (path) {
if (!path || path === '/') {
return '';
}
const trimmed = path.endsWith('/') ? path.slice(0, -1) : path;
const parts = trimmed.split('/').filter(Boolean);
return parts[parts.length - 1] || '';
},
mkdir: async function (path) {
const dirPath = this.normalizeDirPath(path);
await this.put(dirPath, '');
return dirPath;
},
deleteTree: async function (path) {
if (!path || !path.startsWith('/')) {
throw new Error('Invalid path');
}
if (path.endsWith('/')) {
const contents = await this.ls(path);
for (const entry of contents) {
if (!entry) continue;
await this.rm(`${path}${entry}`);
}
if (path !== '/') {
await this.rm(path);
}
return;
}
await this.rm(path);
},
moveTree: async function (srcPath, destDir) {
if (!srcPath || !srcPath.startsWith('/')) {
throw new Error('Invalid source path');
}
const targetDir = this.normalizeDirPath(destDir);
const isFolder = srcPath.endsWith('/');
const name = this.baseName(srcPath);
if (!name) {
return srcPath;
}
const destPath = `${targetDir}${name}${isFolder ? '/' : ''}`;
if (destPath === srcPath) {
return destPath;
}
if (isFolder && targetDir.startsWith(srcPath)) {
throw new Error('Cannot move a folder into itself.');
}
if (targetDir !== '/') {
await this.mkdir(targetDir);
}
if (isFolder) {
const contents = await this.ls(srcPath);
for (const entry of contents) {
if (!entry) continue;
await this.mv(`${srcPath}${entry}`, `${destPath}${entry}`);
}
}
await this.mv(srcPath, destPath);
return destPath;
},
cat: function (path) {
// Display the contents of a file at the specified path.
if (path.endsWith('/')) {
throw new Error('Invalid file path');
}
const transaction = db.transaction(['keyValueStore'], 'readonly');
const store = transaction.objectStore('keyValueStore');
const getRequest = store.get(path);
getRequest.onsuccess = function (event) {
console.log(event.target.result ? event.target.result.value : '');
};
getRequest.onerror = function (event) {
console.error(`Error getting file at ${path}: ${event.target.errorCode}`);
};
},
find: function (pattern) {
// Search for files or directories matching a specified pattern.
const transaction = db.transaction(['keyValueStore'], 'readonly');
const store = transaction.objectStore('keyValueStore');
const cursorRequest = store.openCursor();
const matches = [];
cursorRequest.onsuccess = function (event) {
const cursor = event.target.result;
if (cursor) {
if (new RegExp(pattern).test(cursor.key)) {
matches.push(cursor.key);
}
cursor.continue();
} else {
console.log(matches.join('\n'));
}
};
cursorRequest.onerror = function (event) {
console.error(`Error finding pattern ${pattern}: ${event.target.errorCode}`);
};
},
du: function () {
// Show the disk usage of files and directories.
const transaction = db.transaction(['keyValueStore'], 'readonly');
const store = transaction.objectStore('keyValueStore');
let totalSize = 0;
return new Promise((resolve, reject) => {
const cursorRequest = store.openCursor();
cursorRequest.onsuccess = function (event) {
const cursor = event.target.result;
if (cursor) {
const keyLength = cursor.key ? cursor.key.length : 0;
const valueLength = cursor.value ? cursor.value.length : 0;
if (!isNaN(keyLength) && !isNaN(valueLength)) {
totalSize += keyLength + valueLength;
}
cursor.continue();
} else {
resolve(totalSize);
}
};
cursorRequest.onerror = function (event) {
reject(`Error getting disk usage: ${event.target.errorCode}`);
};
});
},
df: function () {
// Show the amount of disk space used and available on the file system.
if (navigator.storage && navigator.storage.estimate) {
return navigator.storage.estimate().then((estimate) => {
const quota = Number.isFinite(estimate.quota) ? estimate.quota : 0;
const usage = Number.isFinite(estimate.usage) ? estimate.usage : 0;
const available = Math.max(0, quota - usage);
return { quota, usage, available };
});
}
return Promise.reject('Storage estimate not available');
},
grep: function (path, pattern) {
// Search for a pattern in file content at the specified path.
if (!path.startsWith('/') || path === '') {
throw new Error('Invalid path');
}
const transaction = db.transaction(['keyValueStore'], 'readonly');
const store = transaction.objectStore('keyValueStore');
const getRequest = store.get(path);
getRequest.onsuccess = function (event) {
const content = event.target.result ? event.target.result.value : '';
if (new RegExp(pattern).test(content)) {
console.log(`${path}: ${content}`);
}
};
getRequest.onerror = function (event) {
console.error(`Error getting file at ${path}: ${event.target.errorCode}`);
};
}
};
// Attach the vfs object to the window object
window.vfs = vfs;
if (vfsReadyResolve) vfsReadyResolve(vfs);
};
// Handle errors when opening the database
openRequest.onerror = function (event) {
console.error('Error opening database:', event.target.errorCode);
if (vfsReadyReject) vfsReadyReject(event.target.errorCode);
};
|