-
Notifications
You must be signed in to change notification settings - Fork 6
/
memfs.ts
163 lines (154 loc) · 5.08 KB
/
memfs.ts
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
import type { FileSystemDriver, FileSystemNode } from './index';
import { asyncIterator2ReadableStream } from '../../utils';
type MemNode = MemFile | MemDirectory;
interface MemFile {
type: 'file';
content: Blob;
url: string | null;
lock: Promise<unknown>;
}
interface MemDirectory {
type: 'directory';
content: Record<string, MemNode>;
}
function expectedExists(node: MemNode | null): asserts node is MemNode {
if (!node)
throw new Error('ENOTFOUND');
}
function expectedDirectory(node: MemNode): asserts node is MemDirectory {
if (node.type !== 'directory')
throw new Error('ENOTADIR');
}
function expectedFile(node: MemNode): asserts node is MemFile {
if (node.type !== 'file')
throw new Error('EISDIR');
}
export class MemFS implements FileSystemDriver {
#filetree: MemDirectory = { type: 'directory', content: Object.create(null) };
async resolveUri(path: string[]): Promise<string> {
let resolved: MemNode = this.#filetree;
for (const segment of path) {
expectedDirectory(resolved);
const found: MemNode | null = resolved.content[segment] || null;
expectedExists(found);
resolved = found;
}
expectedFile(resolved);
return resolved.url ??= URL.createObjectURL(resolved.content);
}
async access(path: string[]): Promise<boolean> {
let resolved: MemNode = this.#filetree;
for (const segment of path) {
expectedDirectory(resolved);
const found: MemNode | null = resolved.content[segment] || null;
if (!found)
return false;
resolved = found;
}
return true;
}
async readDir(path: string[]): Promise<ReadableStream<FileSystemNode>> {
let resolved: MemNode = this.#filetree;
for (const segment of path) {
const found: MemNode | null = resolved.content[segment] || null;
expectedExists(found);
expectedDirectory(found);
resolved = found;
}
return asyncIterator2ReadableStream(
Object
.entries(resolved.content)
.map(([name, { type }]) => ({
type,
name
}))[Symbol.iterator]()
)
}
async readFile(path: string[], offset = 0, length?: number): Promise<ReadableStream<Uint8Array>> {
let resolved: MemNode = this.#filetree;
for (const segment of path) {
expectedDirectory(resolved);
const found: MemNode | null = resolved.content[segment] || null;
expectedExists(found);
resolved = found;
}
expectedFile(resolved);
const file = resolved;
const result = resolved.lock
.catch(() => (new Error().stack, null))
.then(() => file.content.slice(offset, length ? offset + length : undefined).stream());
resolved.lock = result;
return result;
}
async writeFile(path: string[], offset: 'before' | 'after' | 'override', create: boolean): Promise<WritableStream<Uint8Array>> {
let parent = this.#filetree, resolved: MemNode | null = parent;
for (let i = 0; i < path.length; i++) {
const segment = path[i]!;
if (resolved)
expectedDirectory(resolved);
else if (create)
resolved = parent.content[path[i - 1]!] = { type: 'directory', content: Object.create(null) };
else
expectedExists(resolved);
const found: MemNode | null = resolved.content[segment] || null;
parent = resolved;
resolved = found;
}
if (resolved)
expectedFile(resolved);
else if (create)
resolved = parent.content[path[path.length - 1]!] = {
type: 'file',
content: new Blob([]),
url: null,
lock: Promise.resolve()
};
else
expectedExists(resolved);
const strategy = new ByteLengthQueuingStrategy({ highWaterMark: 65535 });
const { readable, writable } = new TransformStream<Uint8Array, Uint8Array>({}, strategy, strategy);
const file = resolved;
const result = Promise.all([
resolved.lock.catch(() => null),
new Response(readable).blob()
])
.then(([, blob]) => {
let content;
switch (offset) {
case 'override':
content = blob;
break;
case 'before':
content = new Blob([blob, file.content]);
break;
case 'after':
content = new Blob([file.content, blob]);
break;
}
if (file.url)
URL.revokeObjectURL(file.url);
file.url = null;
file.content = content;
new Error().stack;
});
resolved.lock = result;
return Promise.resolve(writable);
}
async deleteNode(path: string[], recursive: boolean): Promise<void> {
let parent = this.#filetree, resolved: MemNode | null = this.#filetree;
for (const segment of path) {
if (!resolved)
resolved = parent.content[segment] = { type: 'directory', content: Object.create(null) };
else
expectedDirectory(resolved);
const found: MemNode | null = resolved.content[segment] || null;
parent = resolved;
resolved = found;
}
if (resolved)
if (parent === resolved)
throw new Error('EBUSY'); // cannot delete root
else
delete parent.content[path[path.length - 1]!];
}
}