-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryFileSystem.js
197 lines (166 loc) · 5.29 KB
/
MemoryFileSystem.js
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
import MemoryFileSystemDirectory from "./Memory/MemoryFileSystemDirectory.js";
import MemoryFileSystemFile from "./Memory/MemoryFileSystemFile.js";
import DummyFsWatcher from "./DummyFsWatcher.js";
/**
* @implements {import("@spyglassmc/core").ExternalFileSystem}
*/
export default class MemoryFileSystem {
/** @type {MemoryFileSystemDirectory} */ root = new MemoryFileSystemDirectory();
/** @type {string} */ baseUri = 'file:///';
/**
* @param location
* @return {string[]}
*/
getPathParts(location) {
let str = location.toString();
if (!str.startsWith(this.baseUri)) {
throw new Error(`EACCES: ${str}`);
}
if (str.endsWith("/")) {
str = str.substring(0, str.length - 1);
}
let path = str.substring(this.baseUri.length);
return path.split("/");
}
/**
* @param location
* @param {boolean} parent If true, return the parent directory of the entry
* @return {Promise<MemoryFileSystemEntry>}
*/
async findEntry(location, parent = false) {
let parts = this.getPathParts(location);
if (parent) {
parts.pop();
}
let current = this.root;
for (let part of parts) {
if (!part.length) {
continue;
}
if (!(current instanceof MemoryFileSystemDirectory)) {
throw new Error(`ENOTDIR: ${location}`);
}
current = await current.getEntry(part);
if (current === null) {
throw new Error(`ENOENT: ${location}`);
}
}
return current;
}
/**
* @inheritDoc
*/
async chmod(_location, _mode) {
}
/**
* @inheritDoc
*/
async mkdir(location, options) {
let parts = this.getPathParts(location);
if (options.recursive) {
let current = this.root;
for (let part of parts) {
if (!part.length) {
continue;
}
let next = await current.getEntry(part);
if (next === null) {
next = new MemoryFileSystemDirectory();
await current.addEntry(part, next);
} else if (!(next instanceof MemoryFileSystemDirectory)) {
throw new Error(`EEXIST: ${location}`);
}
current = next;
}
}
let parent = await this.findEntry(location, true);
let basename = parts.pop();
if (!(parent instanceof MemoryFileSystemDirectory)) {
throw new Error(`ENOTDIR: ${location}`);
}
if (await parent.hasEntry(basename)) {
throw new Error(`EEXIST: ${location}`);
}
await parent.addEntry(basename, new MemoryFileSystemDirectory());
}
/**
* @inheritDoc
*/
async readdir(location) {
let directory = await this.findEntry(location);
if (!(directory instanceof MemoryFileSystemDirectory)) {
throw new Error(`ENOTDIR: ${location}`);
}
let result = [];
for (let [name, entry] of await directory.getEntries()) {
let isDirectory = entry instanceof MemoryFileSystemDirectory;
result.push({
name: name,
isDirectory: () => isDirectory,
isFile: () => !isDirectory,
isSymbolicLink: () => false
});
}
return result;
}
/**
* @inheritDoc
*/
async readFile(location) {
let entry = await this.findEntry(location);
if (!(entry instanceof MemoryFileSystemFile)) {
throw new Error(`EISDIR: ${location}`);
}
return await entry.getContent();
}
/**
* @inheritDoc
*/
async showFile(_path) {
throw new Error('showFile not supported on browser');
}
/**
* @inheritDoc
*/
async stat(location) {
let entry = await this.findEntry(location);
let isDirectory = entry instanceof MemoryFileSystemDirectory;
return {
isDirectory: () => isDirectory,
isFile: () => !isDirectory
};
}
/**
* @inheritDoc
*/
async unlink(location) {
let parts = this.getPathParts(location);
let parent = await this.findEntry(location, true);
let basename = parts.pop();
if (!(parent instanceof MemoryFileSystemDirectory) || !await parent.hasEntry(basename)) {
throw new Error(`ENOENT: ${location}`);
}
await parent.removeEntry(basename);
}
/**
* @inheritDoc
*/
watch(_locations) {
return new DummyFsWatcher();
}
/**
* @inheritDoc
*/
async writeFile(location, data, _options) {
let parts = this.getPathParts(location);
let parent = await this.findEntry(location, true);
let basename = parts.pop();
if (!(parent instanceof MemoryFileSystemDirectory)) {
throw new Error(`ENOENT: ${location}`);
}
if (await parent.hasEntry(basename) && !(await parent.getEntry(basename) instanceof MemoryFileSystemFile)) {
throw new Error(`EISDIR: ${location}`);
}
await parent.addEntry(basename, new MemoryFileSystemFile(data));
}
}