MCPcopy
hub / github.com/jvilk/BrowserFS / LocalStorageStore

Class LocalStorageStore

src/backend/LocalStorage.ts:31–78  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

29 * A synchronous key-value store backed by localStorage.
30 */
31export class LocalStorageStore implements SyncKeyValueStore, SimpleSyncStore {
32 public name(): string {
33 return LocalStorageFileSystem.Name;
34 }
35
36 public clear(): void {
37 global.localStorage.clear();
38 }
39
40 public beginTransaction(type: string): SyncKeyValueRWTransaction {
41 // No need to differentiate.
42 return new SimpleSyncRWTransaction(this);
43 }
44
45 public get(key: string): Buffer | undefined {
46 try {
47 const data = global.localStorage.getItem(key);
48 if (data !== null) {
49 return Buffer.from(data, binaryEncoding);
50 }
51 } catch (e) {
52 // Do nothing.
53 }
54 // Key doesn't exist, or a failure occurred.
55 return undefined;
56 }
57
58 public put(key: string, data: Buffer, overwrite: boolean): boolean {
59 try {
60 if (!overwrite && global.localStorage.getItem(key) !== null) {
61 // Don't want to overwrite the key!
62 return false;
63 }
64 global.localStorage.setItem(key, data.toString(binaryEncoding));
65 return true;
66 } catch (e) {
67 throw new ApiError(ErrorCode.ENOSPC, "LocalStorage is full.");
68 }
69 }
70
71 public del(key: string): void {
72 try {
73 global.localStorage.removeItem(key);
74 } catch (e) {
75 throw new ApiError(ErrorCode.EIO, "Unable to delete key " + key + ": " + e);
76 }
77 }
78}
79
80/**
81 * A synchronous file system backed by localStorage. Connects our

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected