forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInMemory.ts
More file actions
52 lines (43 loc) · 1.5 KB
/
InMemory.ts
File metadata and controls
52 lines (43 loc) · 1.5 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
import {BFSCallback, FileSystemOptions} from '../core/file_system';
import {SyncKeyValueStore, SimpleSyncStore, SimpleSyncRWTransaction, SyncKeyValueRWTransaction, SyncKeyValueFileSystem} from '../generic/key_value_filesystem';
/**
* A simple in-memory key-value store backed by a JavaScript object.
*/
export class InMemoryStore implements SyncKeyValueStore, SimpleSyncStore {
private store: { [key: string]: Buffer } = {};
public name() { return InMemoryFileSystem.Name; }
public clear() { this.store = {}; }
public beginTransaction(type: string): SyncKeyValueRWTransaction {
return new SimpleSyncRWTransaction(this);
}
public get(key: string): Buffer {
return this.store[key];
}
public put(key: string, data: Buffer, overwrite: boolean): boolean {
if (!overwrite && this.store.hasOwnProperty(key)) {
return false;
}
this.store[key] = data;
return true;
}
public del(key: string): void {
delete this.store[key];
}
}
/**
* A simple in-memory file system backed by an InMemoryStore.
* Files are not persisted across page loads.
*/
export default class InMemoryFileSystem extends SyncKeyValueFileSystem {
public static readonly Name = "InMemory";
public static readonly Options: FileSystemOptions = {};
/**
* Creates an InMemoryFileSystem instance.
*/
public static Create(options: any, cb: BFSCallback<InMemoryFileSystem>): void {
cb(null, new InMemoryFileSystem());
}
private constructor() {
super({ store: new InMemoryStore() });
}
}