-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonFileSystem.js
149 lines (130 loc) · 3.33 KB
/
JsonFileSystem.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
import DummyFsWatcher from "./DummyFsWatcher.js";
import Base64 from "../Util/Base64.js";
/**
* A file system that reads from a JSON predefined object.
*
* @implements {import("@spyglassmc/core").ExternalFileSystem}
*/
export default class JsonFileSystem {
/** @type {string} */ baseUri = 'file:///';
/** @type {Object} */ entries;
/**
* @param {Object} entries
*/
constructor(entries) {
this.entries = entries;
}
/**
* @param location
* @return {string|Object}
*/
findEntry(location) {
let str = location.toString();
if (!str.startsWith(this.baseUri)) {
throw new Error(`EACCES: ${str}`);
}
let path = str.substring(this.baseUri.length);
let parts = path.split("/");
let current = this.entries;
for (let part of parts) {
if (!part.length) {
continue;
}
current = current[part];
if (typeof current === 'undefined') {
throw new Error(`ENOENT: ${location}`);
}
}
return current;
}
/**
* @inheritDoc
*/
async chmod(_location, _mode) {
throw new Error('EPERM: Operation not permitted');
}
/**
* @inheritDoc
*/
async mkdir(_location, _options) {
throw new Error('EPERM: Operation not permitted');
}
/**
* @inheritDoc
*/
async readdir(location) {
let entry = this.findEntry(location);
if (typeof entry !== 'object') {
throw new Error(`ENOTDIR: ${location}`);
}
let result = [];
for (let [key, value] of Object.entries(entry)) {
if (typeof value === 'object') {
result.push({
name: key,
isDirectory: () => true,
isFile: () => false,
isSymbolicLink: () => false
});
} else {
result.push({
name: key,
isDirectory: () => false,
isFile: () => true,
isSymbolicLink: () => false
});
}
}
return result;
}
/**
* @inheritDoc
*/
async readFile(location) {
let entry = this.findEntry(location);
if (typeof entry !== 'string') {
throw new Error(`EISDIR: ${location}`);
}
return Base64.decode(entry);
}
/**
* @inheritDoc
*/
async showFile(_path) {
throw new Error('showFile not supported on browser');
}
/**
* @inheritDoc
*/
async stat(location) {
let entry = this.findEntry(location);
if (typeof entry === 'object') {
return {
isDirectory: () => true,
isFile: () => false
};
}
return {
isDirectory: () => false,
isFile: () => true
};
}
/**
* @inheritDoc
*/
async unlink(_location) {
throw new Error('EPERM: Operation not permitted');
}
/**
* @inheritDoc
*/
watch(_locations) {
return new DummyFsWatcher();
}
/**
* @inheritDoc
*/
async writeFile(_location, _data, _options) {
throw new Error('EPERM: Operation not permitted');
}
}