-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CodeSandboxFS.ts
292 lines (238 loc) · 7.15 KB
/
CodeSandboxFS.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import * as path from "path";
import {
SynchronousFileSystem,
FileSystem,
BFSOneArgCallback,
BFSCallback,
FileSystemOptions
} from "../core/file_system";
import { File } from "../core/file";
import { FileFlag } from "../core/file_flag";
import { default as Stats, FileType } from "../core/node_fs_stats";
import PreloadFile from "../generic/preload_file";
import { ErrorCode, ApiError } from "../core/api_error";
export interface IModule {
path?: string;
code: string | undefined;
}
export interface IManager {
getTranspiledModules: () => {
[path: string]: {
module: IModule;
};
};
addModule(module: IModule): void;
removeModule(module: IModule): void;
moveModule(module: IModule, newPath: string): void;
updateModule(module: IModule): void;
}
class CodeSandboxFile extends PreloadFile<CodeSandboxFS> implements File {
constructor(
_fs: CodeSandboxFS,
_path: string,
_flag: FileFlag,
_stat: Stats,
contents?: Buffer
) {
super(_fs, _path, _flag, _stat, contents);
}
public sync(cb: BFSOneArgCallback): void {
if (this.isDirty()) {
const buffer = this.getBuffer();
this._fs._sync(
this.getPath(),
buffer,
(e: ApiError | undefined | null, stat?: Stats) => {
if (!e) {
this.resetDirty();
}
cb(e);
}
);
} else {
cb();
}
}
public close(cb: BFSOneArgCallback): void {
this.sync(cb);
}
public syncSync(): void {
if (this.isDirty()) {
this._fs._syncSync(this.getPath(), this.getBuffer());
this.resetDirty();
}
}
public closeSync(): void {
this.syncSync();
}
}
export interface ICodeSandboxFileSystemOptions {
manager: IManager;
}
export default class CodeSandboxFS extends SynchronousFileSystem
implements FileSystem {
public static readonly Name = "CodeSandboxFS";
public static readonly Options: FileSystemOptions = {
manager: {
type: "object",
description: "The CodeSandbox Manager",
validator: (opt: IManager, cb: BFSOneArgCallback): void => {
if (opt) {
cb();
} else {
cb(new ApiError(ErrorCode.EINVAL, `Manager is invalid`));
}
}
}
};
/**
* Creates an InMemoryFileSystem instance.
*/
public static Create(
options: ICodeSandboxFileSystemOptions,
cb: BFSCallback<CodeSandboxFS>
): void {
cb(null, new CodeSandboxFS(options.manager));
}
public static isAvailable(): boolean {
return true;
}
private manager: IManager;
constructor(manager: IManager) {
super();
this.manager = manager;
}
public getName(): string {
return "CodeSandboxFS";
}
public isReadOnly(): boolean {
return false;
}
public supportsProps(): boolean {
return false;
}
public supportsSynch(): boolean {
return true;
}
public empty(mainCb: BFSOneArgCallback): void {
const tModules = this.manager.getTranspiledModules();
Object.keys(tModules).forEach((pa: string) => {
this.manager.removeModule(tModules[pa].module);
});
mainCb();
}
public renameSync(oldPath: string, newPath: string) {
const tModules = this.manager.getTranspiledModules();
const modulesWithPath = Object.keys(tModules).filter(
(p: string) => p.startsWith(oldPath) + "/" || p === oldPath
);
if (modulesWithPath.length === 0) {
throw ApiError.FileError(ErrorCode.ENOENT, oldPath);
}
modulesWithPath
.map((p: string) => ({ path: p, moduleInfo: tModules[p] }))
.forEach(({ path, moduleInfo }) => {
const { module } = moduleInfo;
this.manager.moveModule(module, path.replace(oldPath, newPath));
});
}
public statSync(p: string, isLstate: boolean): Stats {
const tModules = this.manager.getTranspiledModules();
const moduleInfo = tModules[p];
if (!moduleInfo) {
const modulesStartingWithPath = Object.keys(tModules).filter(
(pa: string) => pa.startsWith(p.endsWith("/") ? p : p + "/") || pa === p
);
if (modulesStartingWithPath.length > 0) {
return new Stats(FileType.DIRECTORY, 0);
} else {
throw ApiError.FileError(ErrorCode.ENOENT, p);
}
}
const stats = new Stats(
FileType.FILE,
Buffer.byteLength(moduleInfo.module.code || '', 'utf8')
);
return stats;
}
public createFileSync(p: string, flag: FileFlag, mode: number): File {
if (p === "/") {
throw ApiError.EEXIST(p);
}
if (this.manager.getTranspiledModules()[p]) {
throw ApiError.EEXIST(p);
}
const module = {
path: p,
code: ""
};
this.manager.addModule(module);
const buffer = Buffer.from(module.code || "");
const stats = new Stats(FileType.FILE, buffer.length);
return new CodeSandboxFile(this, p, flag, stats, buffer);
}
public openFileSync(p: string, flag: FileFlag, mode: number): File {
const moduleInfo = this.manager.getTranspiledModules()[p];
if (!moduleInfo) {
throw ApiError.ENOENT(p);
}
const { code = "" } = moduleInfo.module;
const buffer = Buffer.from(code || "");
const stats = new Stats(FileType.FILE, buffer.length);
return new CodeSandboxFile(this, p, flag, stats, buffer);
}
public rmdirSync(p: string) {
const tModules = this.manager.getTranspiledModules();
Object.keys(tModules)
.filter((pa: string) => pa.startsWith(p + "/") || p === pa)
.forEach((pa: string) => {
const { module } = tModules[pa];
this.manager.removeModule(module);
});
}
public mkdirSync(p: string) {
// CodeSandbox Manager doesn't have the concept of directories, like git.
// For now we will do nothing, as we pretend that every directory already exists.
}
public readdirSync(path: string): string[] {
const paths = Object.keys(this.manager.getTranspiledModules());
const p = path.endsWith("/") ? path : path + "/";
const pathsInDir = paths.filter((secondP: string) => secondP.startsWith(p));
if (pathsInDir.length === 0) {
return [];
}
const directChildren: Set<string> = new Set();
const currentPathLength = p.split("/").length;
pathsInDir
.filter((np: string) => np.split("/").length >= currentPathLength)
.forEach((np: string) => {
const parts = np.split("/");
parts.length = currentPathLength;
directChildren.add(parts.join("/"));
});
const pathArray = Array.from(directChildren).map(pa => pa.replace(p, ""));
return pathArray;
}
public _sync(p: string, data: Buffer, cb: BFSCallback<Stats>): void {
const parent = path.dirname(p);
this.stat(
parent,
false,
(error: ApiError | undefined | null, stat?: Stats): void => {
if (error) {
cb(ApiError.FileError(ErrorCode.ENOENT, parent));
} else {
const module = this.manager.getTranspiledModules()[p].module;
this.manager.updateModule(module);
cb(null);
}
}
);
}
public _syncSync(p: string, data: Buffer): void {
const parent = path.dirname(p);
this.statSync(parent, false);
const module = this.manager.getTranspiledModules()[p].module;
this.manager.updateModule(module);
}
}