forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMountableFileSystem.ts
More file actions
441 lines (404 loc) · 13.3 KB
/
MountableFileSystem.ts
File metadata and controls
441 lines (404 loc) · 13.3 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import {FileSystem, BaseFileSystem, BFSOneArgCallback, BFSCallback, FileSystemOptions} from '../core/file_system';
import InMemoryFileSystem from './InMemory';
import {ApiError, ErrorCode} from '../core/api_error';
import fs from '../core/node_fs';
import * as path from 'path';
import {mkdirpSync} from '../core/util';
/**
* Configuration options for the MountableFileSystem backend.
*/
export interface MountableFileSystemOptions {
// Locations of mount points. Can be empty.
[mountPoint: string]: FileSystem;
}
/**
* The MountableFileSystem allows you to mount multiple backend types or
* multiple instantiations of the same backend into a single file system tree.
* The file systems do not need to know about each other; all interactions are
* automatically facilitated through this interface.
*
* For example, if a file system is mounted at /mnt/blah, and a request came in
* for /mnt/blah/foo.txt, the file system would see a request for /foo.txt.
*
* You can mount file systems when you configure the file system:
* ```javascript
* BrowserFS.configure({
* fs: "MountableFileSystem",
* options: {
* '/data': { fs: 'HTTPRequest', options: { index: "http://mysite.com/files/index.json" } },
* '/home': { fs: 'LocalStorage' }
* }
* }, function(e) {
*
* });
* ```
*
* For advanced users, you can also mount file systems *after* MFS is constructed:
* ```javascript
* BrowserFS.FileSystem.HTTPRequest.Create({
* index: "http://mysite.com/files/index.json"
* }, function(e, xhrfs) {
* BrowserFS.FileSystem.MountableFileSystem.Create({
* '/data': xhrfs
* }, function(e, mfs) {
* BrowserFS.initialize(mfs);
*
* // Added after-the-fact...
* BrowserFS.FileSystem.LocalStorage.Create(function(e, lsfs) {
* mfs.mount('/home', lsfs);
* });
* });
* });
* ```
*
* Since MountableFileSystem simply proxies requests to mounted file systems, it supports all of the operations that the mounted file systems support.
*
* With no mounted file systems, `MountableFileSystem` acts as a simple `InMemory` filesystem.
*/
export default class MountableFileSystem extends BaseFileSystem implements FileSystem {
public static readonly Name = "MountableFileSystem";
public static readonly Options: FileSystemOptions = {};
/**
* Creates a MountableFileSystem instance with the given options.
*/
public static Create(opts: MountableFileSystemOptions, cb: BFSCallback<MountableFileSystem>): void {
InMemoryFileSystem.Create({}, (e, imfs?) => {
if (imfs) {
const fs = new MountableFileSystem(imfs);
try {
Object.keys(opts).forEach((mountPoint) => {
fs.mount(mountPoint, opts[mountPoint]);
});
} catch (e) {
return cb(e);
}
cb(null, fs);
} else {
cb(e);
}
});
}
public static isAvailable(): boolean {
return true;
}
private mntMap: {[path: string]: FileSystem};
// Contains the list of mount points in mntMap, sorted by string length in decreasing order.
// Ensures that we scan the most specific mount points for a match first, which lets us
// nest mount points.
private mountList: string[] = [];
private rootFs: FileSystem;
/**
* Creates a new, empty MountableFileSystem.
*/
private constructor(rootFs: FileSystem) {
super();
this.mntMap = {};
this.rootFs = rootFs;
}
/**
* Mounts the file system at the given mount point.
*/
public mount(mountPoint: string, fs: FileSystem): void {
if (mountPoint[0] !== '/') {
mountPoint = `/${mountPoint}`;
}
mountPoint = path.resolve(mountPoint);
if (this.mntMap[mountPoint]) {
throw new ApiError(ErrorCode.EINVAL, "Mount point " + mountPoint + " is already taken.");
}
mkdirpSync(mountPoint, 0x1ff, this.rootFs);
this.mntMap[mountPoint] = fs;
this.mountList.push(mountPoint);
this.mountList = this.mountList.sort((a, b) => b.length - a.length);
}
public umount(mountPoint: string): void {
if (mountPoint[0] !== '/') {
mountPoint = `/${mountPoint}`;
}
mountPoint = path.resolve(mountPoint);
if (!this.mntMap[mountPoint]) {
throw new ApiError(ErrorCode.EINVAL, "Mount point " + mountPoint + " is already unmounted.");
}
delete this.mntMap[mountPoint];
this.mountList.splice(this.mountList.indexOf(mountPoint), 1);
while (mountPoint !== '/') {
if (this.rootFs.readdirSync(mountPoint).length === 0) {
this.rootFs.rmdirSync(mountPoint);
mountPoint = path.dirname(mountPoint);
} else {
break;
}
}
}
/**
* Returns the file system that the path points to.
*/
public _getFs(path: string): {fs: FileSystem; path: string, mountPoint: string} {
const mountList = this.mountList, len = mountList.length;
for (let i = 0; i < len; i++) {
const mountPoint = mountList[i];
// We know path is normalized, so it is a substring of the mount point.
if (mountPoint.length <= path.length && path.indexOf(mountPoint) === 0) {
path = path.substr(mountPoint.length > 1 ? mountPoint.length : 0);
if (path === '') {
path = '/';
}
return {fs: this.mntMap[mountPoint], path: path, mountPoint: mountPoint};
}
}
// Query our root file system.
return {fs: this.rootFs, path: path, mountPoint: '/'};
}
// Global information methods
public getName(): string {
return MountableFileSystem.Name;
}
public diskSpace(path: string, cb: (total: number, free: number) => void): void {
cb(0, 0);
}
public isReadOnly(): boolean {
return false;
}
public supportsLinks(): boolean {
// I'm not ready for cross-FS links yet.
return false;
}
public supportsProps(): boolean {
return false;
}
public supportsSynch(): boolean {
return true;
}
/**
* Fixes up error messages so they mention the mounted file location relative
* to the MFS root, not to the particular FS's root.
* Mutates the input error, and returns it.
*/
public standardizeError(err: ApiError, path: string, realPath: string): ApiError {
const index = err.message.indexOf(path);
if (index !== -1) {
err.message = err.message.substr(0, index) + realPath + err.message.substr(index + path.length);
err.path = realPath;
}
return err;
}
// The following methods involve multiple file systems, and thus have custom
// logic.
// Note that we go through the Node API to use its robust default argument
// processing.
public rename(oldPath: string, newPath: string, cb: BFSOneArgCallback): void {
// Scenario 1: old and new are on same FS.
const fs1rv = this._getFs(oldPath);
const fs2rv = this._getFs(newPath);
if (fs1rv.fs === fs2rv.fs) {
return fs1rv.fs.rename(fs1rv.path, fs2rv.path, (e?: ApiError) => {
if (e) {
this.standardizeError(this.standardizeError(e, fs1rv.path, oldPath), fs2rv.path, newPath);
}
cb(e);
});
}
// Scenario 2: Different file systems.
// Read old file, write new file, delete old file.
return fs.readFile(oldPath, function(err: ApiError, data?: any) {
if (err) {
return cb(err);
}
fs.writeFile(newPath, data, function(err: ApiError) {
if (err) {
return cb(err);
}
fs.unlink(oldPath, cb);
});
});
}
public renameSync(oldPath: string, newPath: string): void {
// Scenario 1: old and new are on same FS.
const fs1rv = this._getFs(oldPath);
const fs2rv = this._getFs(newPath);
if (fs1rv.fs === fs2rv.fs) {
try {
return fs1rv.fs.renameSync(fs1rv.path, fs2rv.path);
} catch (e) {
this.standardizeError(this.standardizeError(e, fs1rv.path, oldPath), fs2rv.path, newPath);
throw e;
}
}
// Scenario 2: Different file systems.
const data = fs.readFileSync(oldPath);
fs.writeFileSync(newPath, data);
return fs.unlinkSync(oldPath);
}
public readdirSync(p: string): string[] {
const fsInfo = this._getFs(p);
// If null, rootfs did not have the directory
// (or the target FS is the root fs).
let rv: string[] | null = null;
// Mount points are all defined in the root FS.
// Ensure that we list those, too.
if (fsInfo.fs !== this.rootFs) {
try {
rv = this.rootFs.readdirSync(p);
} catch (e) {
// Ignore.
}
}
try {
const rv2 = fsInfo.fs.readdirSync(fsInfo.path);
if (rv === null) {
return rv2;
} else {
// Filter out duplicates.
return rv2.concat(rv.filter((val) => rv2.indexOf(val) === -1));
}
} catch (e) {
if (rv === null) {
throw this.standardizeError(e, fsInfo.path, p);
} else {
// The root FS had something.
return rv;
}
}
}
public readdir(p: string, cb: BFSCallback<string[]>): void {
const fsInfo = this._getFs(p);
fsInfo.fs.readdir(fsInfo.path, (err, files) => {
if (fsInfo.fs !== this.rootFs) {
try {
const rv = this.rootFs.readdirSync(p);
if (files) {
// Filter out duplicates.
files = files.concat(rv.filter((val) => files!.indexOf(val) === -1));
} else {
files = rv;
}
} catch (e) {
// Root FS and target FS did not have directory.
if (err) {
return cb(this.standardizeError(err, fsInfo.path, p));
}
}
} else if (err) {
// Root FS and target FS are the same, and did not have directory.
return cb(this.standardizeError(err, fsInfo.path, p));
}
cb(null, files);
});
}
public realpathSync(p: string, cache: {[path: string]: string}): string {
const fsInfo = this._getFs(p);
try {
const mountedPath = fsInfo.fs.realpathSync(fsInfo.path, {});
// resolve is there to remove any trailing slash that may be present
return path.resolve(path.join(fsInfo.mountPoint, mountedPath));
} catch (e) {
throw this.standardizeError(e, fsInfo.path, p);
}
}
public realpath(p: string, cache: {[path: string]: string}, cb: BFSCallback<string>): void {
const fsInfo = this._getFs(p);
fsInfo.fs.realpath(fsInfo.path, {}, (err, rv) => {
if (err) {
cb(this.standardizeError(err, fsInfo.path, p));
} else {
// resolve is there to remove any trailing slash that may be present
cb(null, path.resolve(path.join(fsInfo.mountPoint, rv!)));
}
});
}
public rmdirSync(p: string): void {
const fsInfo = this._getFs(p);
if (this._containsMountPt(p)) {
throw ApiError.ENOTEMPTY(p);
} else {
try {
fsInfo.fs.rmdirSync(fsInfo.path);
} catch (e) {
throw this.standardizeError(e, fsInfo.path, p);
}
}
}
public rmdir(p: string, cb: BFSOneArgCallback): void {
const fsInfo = this._getFs(p);
if (this._containsMountPt(p)) {
cb(ApiError.ENOTEMPTY(p));
} else {
fsInfo.fs.rmdir(fsInfo.path, (err?) => {
cb(err ? this.standardizeError(err, fsInfo.path, p) : null);
});
}
}
/**
* Returns true if the given path contains a mount point.
*/
private _containsMountPt(p: string): boolean {
const mountPoints = this.mountList, len = mountPoints.length;
for (let i = 0; i < len; i++) {
const pt = mountPoints[i];
if (pt.length >= p.length && pt.slice(0, p.length) === p) {
return true;
}
}
return false;
}
}
/**
* Tricky: Define all of the functions that merely forward arguments to the
* relevant file system, or return/throw an error.
* Take advantage of the fact that the *first* argument is always the path, and
* the *last* is the callback function (if async).
* @todo Can use numArgs to make proxying more efficient.
* @hidden
*/
function defineFcn(name: string, isSync: boolean, numArgs: number): (...args: any[]) => any {
if (isSync) {
return function(this: MountableFileSystem, ...args: any[]) {
const path = args[0];
const rv = this._getFs(path);
args[0] = rv.path;
try {
return (<any> rv.fs)[name].apply(rv.fs, args);
} catch (e) {
this.standardizeError(e, rv.path, path);
throw e;
}
};
} else {
return function(this: MountableFileSystem, ...args: any[]) {
const path = args[0];
const rv = this._getFs(path);
args[0] = rv.path;
if (typeof args[args.length - 1] === 'function') {
const cb = args[args.length - 1];
args[args.length - 1] = (...args: any[]) => {
if (args.length > 0 && args[0] instanceof ApiError) {
this.standardizeError(args[0], rv.path, path);
}
cb.apply(null, args);
};
}
return (<any> rv.fs)[name].apply(rv.fs, args);
};
}
}
/**
* @hidden
*/
const fsCmdMap = [
// 1 arg functions
['exists', 'unlink', 'readlink'],
// 2 arg functions
['stat', 'mkdir', 'truncate'],
// 3 arg functions
['open', 'readFile', 'chmod', 'utimes'],
// 4 arg functions
['chown'],
// 5 arg functions
['writeFile', 'appendFile']];
for (let i = 0; i < fsCmdMap.length; i++) {
const cmds = fsCmdMap[i];
for (const fnName of cmds) {
(<any> MountableFileSystem.prototype)[fnName] = defineFcn(fnName, false, i + 1);
(<any> MountableFileSystem.prototype)[fnName + 'Sync'] = defineFcn(fnName + 'Sync', true, i + 1);
}
}