-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodefs.ts
715 lines (606 loc) · 20 KB
/
nodefs.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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/// <reference path="typings/tsd.d.ts" />
// We should not have a dependency on `fs` module. Rather the correct `fs` module is injected into `Drive.attach()`.
//var fs = require('fs');
/**
* path.resolve
* path.sep
* path.relative
*/
var path = require('path');
var time = new Date;
class LNode {
static fd = -1;
// Relative path inside `Layer`.
path: string;
// Layer where file is to be found.
layer: Layer;
// File descriptor, negative, because a real file descriptors cannot be negative.
fd: number = LNode.fd--;
constructor(layer, path) {
this.layer = layer;
this.path = path;
}
getData(): string {
return '';
}
setData(data: string|Buffer) {
}
getPath() {
return this.layer.mountpoint + this.path;
}
stats() {
return this.layer.drive.Stats.build(this);
}
rename(new_name) {
new_name = this.layer.getRelativePath(new_name);
var old_name = this.path;
this.path = new_name;
this.layer.nodes[new_name] = this;
delete this.layer.nodes[old_name];
return new_name;
}
}
class LFile extends LNode {
getData(): string {
return this.layer.files[this.path];
}
setData(data: string|Buffer) {
this.layer.files[this.path] = data.toString();
}
rename(new_name) {
var old_name = this.path;
new_name = super.rename(new_name);
this.layer.files[new_name] = this.layer.files[old_name];
delete this.layer.files[old_name];
}
}
class LDirectory extends LNode {
}
/**
* A single `JSON` file of data mounted to a single mount point.
*/
class Layer {
/**
* Array of directory steps to the `mountpoint`.
*/
steps = [];
/**
* The root directory at which this layer was mounted.
*/
mountpoint;
/**
* A map of relative file names to file contents 'string'.
* {
* "test.txt": "...."
* "some/path/hello.txt": "world ..."
* }
*/
files = {};
/**
* Relative path mapping to `LNode` objects.
*/
nodes = {};
/**
* A map of pseudo 'file descriptors' to LNodes.
*/
fds = {};
// a.k.a `fs`
drive;
constructor(mountpoint) {
this.mountpoint = path.resolve(mountpoint);
this.steps = this.mountpoint.split(path.sep);
this.mountpoint += path.sep;
}
getRelativePath(filepath: string) {
return path.relative(this.mountpoint, filepath);
}
getNode(p: string): LNode {
var relative = this.getRelativePath(p);
if(this.nodes[relative]) return this.nodes[relative];
else return null;
}
getFile(p: string): LFile {
var node = this.getNode(p);
return node instanceof LFile ? node : null;
}
getDirectory(p: string): LDirectory {
var node = this.getNode(p);
return node instanceof LDirectory ? node : null;
}
getByFd(fd: number): LNode {
return this.fds[fd];
}
addNode(node: LNode) {
if(node instanceof LFile) {
this.nodes[node.path] = node;
}
this.fds[node.fd] = node;
var parts = node.path.split(path.sep);
if(parts.length > 1) {
var p = parts[0];
for (var i = 1; i < parts.length; i++) {
this.nodes[p] = new LDirectory(this, p);
p += path.sep + parts[i];
}
}
}
generateNodes(archive = {}) {
this.files = archive;
for(var filepath in this.files) {
var node = new LFile(this, filepath);
this.addNode(node);
}
}
/**
* Return relative path if the path is insided the mounting point of this layer.
* @param p
*/
relativePathIfInMp(p: string) {
var rel = path.relative(this.mountpoint, p);
console.log(rel);
if((rel[0] == '.') && (rel[1] == '.')) return false;
else return rel;
}
/**
* Path points to above or to mount point.
* @param abs_path
* @returns {boolean}
*/
isAboveMp(abs_path: string): boolean {
return abs_path == this.mountpoint.substr(0, abs_path.length);
}
/**
* Path points inside or to mount point.
* @param abs_path
* @returns {boolean}
*/
isInMp(abs_path: string): boolean {
return this.mountpoint == abs_path.substr(0, this.mountpoint.length);
}
stepsMatch(steps: string[]) {
var min = Math.min(steps.length, this.steps.length);
for(var i = 0; i < min; i++) {
if(steps[i] != this.steps[i]) break;
}
return i;
}
readdir(abs_path) {
if(!abs_path) return [];
// Edge case when we have '/'.
if(abs_path[abs_path.length - 1] == path.sep)
abs_path = abs_path.substr(0, abs_path.length - 1);
var steps = abs_path.split(path.sep);
var steps_match = this.stepsMatch(steps);
var points_to_mountpoint = this.steps.length == steps_match;
// Is not pointing to or inside the mount point.
if(!points_to_mountpoint) {
if(steps_match == steps.length) {
return this.steps[steps_match];
} else { // Points to a diverging directory.
return [];
}
}
// Points to or inside the mount point.
var rel = path.relative(this.mountpoint, abs_path);
var expr = "^" + (rel ? rel + path.sep : '') + "([^" + path.sep + "]*)(" + path.sep + ")?";
var files = [];
var regex = new RegExp(expr);
for(var npath in this.nodes) {
var match = npath.match(regex);
if(match) {
files.push(match[1]);
}
}
return files;
}
}
/**
* A collection of layers, we have this, so that we override functions with `.attach()` only once.
*/
class Drive {
/**
* A `fs.Stats` class.
*/
Stats;
/**
* `fs` object that we have extended.
*/
fs;
/**
* Collection of file layers, where the top ones owerride the bottom ones.
*/
layers: Layer[] = [];
/**
* `fs` overrides already attached.
*/
attached = false;
readFileSync: (filename, options?) => Buffer|string;
readFile: (filename, options?, callback?) => void;
realpathSync;
realpath;
statSync;
lstatSync;
renameSync;
rename;
fstatSync;
fstat;
writeFileSync;
writeFile;
existsSync;
exists;
readdirSync;
readdir;
appendFileSync;
appendFile;
/**
* Create our `Stats` class that extends (or does not) the `fs.Stats`.
* @param fs
*/
createStatsClass(fs?) {
function Stats() {
this.uid = process.getuid();
this.gid = process.getgid();
this.rdev = 0;
this.blksize = 4096;
this.ino = 0;
this.size = 0;
this.blocks = 1;
this.atime = time;
this.mtime = time;
this.ctime = time;
this.birthtime = time;
this.dev = 0;
this.mode = 0;
this.nlink = 0;
this._isFile = false;
this._isDirectory = false;
}
if(fs) {
var tmp = function () {};
tmp.prototype = fs.Stats.prototype;
Stats.prototype = new tmp();
Stats.prototype.constructor = Stats;
}
Stats.prototype.isFile = function() {
return this._isFile;
};
Stats.prototype.isDirectory = function() {
return this._isDirectory;
};
Stats.prototype.isSymbolicLink = function() {
return false;
};
Stats.build = (node: LNode|LFile|LDirectory) => {
var stats = new Stats;
if(node instanceof LDirectory) {
stats._isDirectory = true;
} else if(node instanceof LFile) {
var data = node.getData();
stats.size = data.length;
stats._isFile = true;
}
return stats;
};
this.Stats = Stats;
}
/**
* Attach this drive to `fs`.
*/
attach(fs?) {
if(!arguments.length) fs = require('fs');
this.createStatsClass(fs);
var self = this;
fs = fs || {};
// fs.readFileSync(filename[, options])
var readFileSync = fs.readFileSync;
this.readFileSync = fs.readFileSync = (file, opts) => {
var f = self.getFile(file);
if(f) return opts ? f.getData() : new Buffer(f.getData());
else return readFileSync.apply(fs, arguments);
};
// fs.readFile(filename[, options], callback)
var readFile = fs.readFile;
this.readFile = fs.readFile = (file, opts, cb) => {
if(typeof opts == "function") {
cb = opts;
opts = {};
}
var f = self.getFile(file);
if(f) {
process.nextTick(() => {
if((typeof opts == "object") && opts.encoding) {
cb(null, f.getData());
} else {
cb(null, new Buffer(f.getData()));
}
});
}
else return readFile.apply(fs, arguments);
};
// fs.realpathSync(path[, cache])
var realpathSync = fs.realpathSync;
this.realpathSync = fs.realpathSync = (file, opts) => {
var node = self.getNode(file);
if(node) return node.getPath();
else return realpathSync.apply(fs, arguments);
};
// fs.realpath(path[, cache], callback)
var realpath = fs.realpath;
this.realpath = fs.realpath = (filepath, cache, callback) => {
if(typeof cache == "function") callback = cache;
var node = self.getNode(filepath);
if(node) {
process.nextTick(() => { callback(null, node.getPath()); });
} else realpath.apply(fs, arguments);
};
// fs.statSync(path)
var statSync = fs.statSync;
this.statSync = fs.statSync = function(p) {
//console.log('statSync', p);
var f = self.getNode(p);
return f ? f.stats() : statSync.apply(fs, arguments);
};
// fs.lstatSync(path)
var lstatSync = fs.lstatSync;
this.lstatSync = fs.lstatSync = function(p) {
var f = self.getNode(p);
return f ? f.stats() : lstatSync.apply(fs, arguments);
};
//fs.renameSync(oldPath, newPath)
var renameSync = fs.renameSync;
this.renameSync = fs.renameSync = (oldPath, newPath) => {
var n = self.getNode(oldPath);
if(n) n.rename(newPath);
else return renameSync.apply(fs, arguments);
};
//fs.renameSync(oldPath, newPath)
var rename = fs.rename;
this.rename = fs.rename = (oldPath, newPath, cb) => {
var n = self.getNode(oldPath);
if(n) {
n.rename(newPath);
process.nextTick(cb);
}
else return rename.apply(fs, arguments);
};
//fs.fstatSync(fd)
var fstatSync = fs.fstatSync;
this.fstatSync = fs.fstatSync = (fd) => {
var n = self.getByFd(fd);
return n ? n.stats() : fstatSync.apply(fs, arguments);
};
// fs.fstat(fd, callback)
var fstat = fs.fstat;
this.fstat = fs.fstat = (fd, callback) => {
var n = self.getByFd(fd);
if(n) process.nextTick(() => { callback(null, n.stats()); });
else fstat.apply(fs, arguments);
};
// fs.writeFileSync(filename, data[, options])
var writeFileSync = fs.writeFileSync;
this.writeFileSync = fs.writeFileSync = (filename, data, options?: any) => {
var n = self.getFile(filename);
if(n) {
n.setData(data);
return undefined;
} else {
return writeFileSync.apply(fs, arguments);
}
};
// fs.writeFile(filename, data[, options], callback)
var writeFile = fs.writeFile;
this.writeFile = fs.writeFile = (filename, data, options, callback) => {
if(typeof options == "function") {
callback = options;
}
var n = self.getFile(filename);
if(n) {
n.setData(data);
if(callback) process.nextTick(callback);
} else {
writeFile.apply(fs, arguments);
}
};
// fs.existsSync(filename)
var existsSync = fs.existsSync;
this.existsSync = fs.existsSync = (filename) => {
var n = self.getFile(filename);
return n ? true : existsSync.apply(fs, filename);
};
// fs.exists(filename, callback)
var exists = fs.exists;
this.exists = fs.exists = (filename, callback) => {
var n = self.getFile(filename);
if(n) {
if(callback) process.nextTick(() => { callback(true); });
} else {
writeFile.apply(fs, arguments);
}
};
// Remove duplicates.
function removeDupes(arr) {
return arr.sort().filter(function(item, pos, ary) {
return !pos || item != ary[pos - 1];
});
}
// fs.readdirSync(path)
var _readdirSync = function (p) {
var files = [];
p = path.resolve(p);
for(var i = 0; i < this.layers.length; i++) {
var layer = this.layers[i];
files = files.concat(layer.readdir(p));
}
return files;
}.bind(this);
var readdirSync = fs.readdirSync;
this.readdirSync = fs.readdirSync = (p) => {
var files = _readdirSync(p);
if(readdirSync) {
try {
files = files.concat(readdirSync.apply(fs, arguments));
} catch(e) {
if(!files.length) throw e;
}
}
return removeDupes(files);
};
// fs.readdir(path, callback)
var readdir = fs.readdir;
this.readdir = fs.readdir = (p, callback) => {
var files = _readdirSync(p);
if(readdir) {
readdir.call(fs, p, (err, more_files) => {
if (err) {
if (!files.length) {
return callback(err);
} else {
callback(null, files);
}
}
files = files.concat(more_files);
callback(null, removeDupes(files));
});
} else {
process.nextTick(() => {
callback(null, removeDupes(files));
});
}
};
// fs.appendFileSync(filename, data[, options])
var appendFileSync = fs.appendFileSync;
this.appendFileSync = fs.appendFileSync = (file, data: string|Buffer) => {
var f = self.getFile(file);
if(f) f.setData(f.getData() + data.toString());
else appendFileSync.apply(fs, arguments);
return undefined;
};
//fs.appendFile(filename, data[, options], callback)
var appendFile = fs.appendFile;
// TODO: This should create file in mounted drive if path resolves to inside the mounting point.
this.appendFile = fs.appendFile = (file, data: string|Buffer, opts, callback?) => {
if(typeof opts == 'function') {
callback = opts;
}
var f = self.getFile(file);
if(f) {
process.nextTick(() => {
f.setData(f.getData() + data.toString());
if(callback) callback();
});
}
else appendFile.apply(fs, arguments);
};
//fs.unlink(path, callback)
//fs.unlinkSync(path)
//fs.ftruncate(fd, len, callback)
//fs.ftruncateSync(fd, len)
//fs.truncate(path, len, callback)
//fs.truncateSync(path, len)
//fs.chown(path, uid, gid, callback)
//fs.chownSync(path, uid, gid)
//fs.fchown(fd, uid, gid, callback)
//fs.fchownSync(fd, uid, gid)
//fs.lchown(path, uid, gid, callback)
//fs.lchownSync(path, uid, gid)
//fs.chmod(path, mode, callback)
//fs.chmodSync(path, mode)
//fs.fchmod(fd, mode, callback)
//fs.fchmodSync(fd, mode)
//fs.lchmod(path, mode, callback)
//fs.lchmodSync(path, mode)
//fs.stat(path, callback)
//fs.lstat(path, callback)
//fs.link(srcpath, dstpath, callback)
//fs.linkSync(srcpath, dstpath)
//fs.symlink(srcpath, dstpath[, type], callback)
//fs.symlinkSync(srcpath, dstpath[, type])
//fs.readlink(path, callback)
//fs.readlinkSync(path)
//fs.rmdir(path, callback)
//fs.rmdirSync(path)
//fs.mkdir(path[, mode], callback)
//fs.mkdirSync(path[, mode])
//fs.close(fd, callback)
//fs.closeSync(fd)
//fs.open(path, flags[, mode], callback)
//fs.openSync(path, flags[, mode])
//fs.utimes(path, atime, mtime, callback)
//fs.utimesSync(path, atime, mtime)
//fs.futimes(fd, atime, mtime, callback)
//fs.futimesSync(fd, atime, mtime)
//fs.fsync(fd, callback)
//fs.fsyncSync(fd)
//fs.write(fd, buffer, offset, length[, position], callback)
//fs.write(fd, data[, position[, encoding]], callback)
//fs.writeSync(fd, buffer, offset, length[, position])
//fs.writeSync(fd, data[, position[, encoding]])
//fs.read(fd, buffer, offset, length, position, callback)
//fs.readSync(fd, buffer, offset, length, position)
//fs.watchFile(filename[, options], listener)
//fs.unwatchFile(filename[, listener])
//fs.watch(filename[, options][, listener])
//fs.access(path[, mode], callback)
//fs.accessSync(path[, mode])
//fs.createReadStream(path[, options])
//fs.createWriteStream(path[, options])
}
addLayer(layer) {
this.layers.push(layer);
layer.drive = this;
}
getFilePath(p: string) {
var filepath = path.resolve(p);
var node = this.getNode(filepath);
return node ? node : null;
}
getNode(p: string): LNode {
var filepath = path.resolve(p);
for(var i = 0; i < this.layers.length; i++) {
var n = this.layers[i].getNode(filepath);
if(n) return n;
}
return null;
}
getFile(p: string): LFile {
var node = this.getNode(p);
return node instanceof LFile ? node : null;
}
getDirectory(p: string): LDirectory {
var node = this.getNode(p);
return node instanceof LFile ? node : null;
}
getByFd(fd: number): LNode {
for(var i = 0; i < this.layers.length; i++) {
var n = this.layers[i].getByFd(fd);
if(n) return n;
}
return null;
}
// TODO: Mount from URL:
// TODO: `mount('/usr/lib', 'http://example.com/volumes/usr/lib.json', callback)`
// TODO: ...also cache that it has been loaded...
mount(mountpoint: string, archive: any = {}, fs?) {
var layer = new Layer(mountpoint);
layer.generateNodes(archive);
this.addLayer(layer);
if(!this.attached) {
this.attach(fs || (fs === null) ? fs : require('fs'));
this.attached = true;
}
}
}
var nodefs: any = function nodefs() {
};
nodefs.LNode = LNode;
nodefs.LFile = LFile;
nodefs.LDirectory = LDirectory;
nodefs.Layer = Layer;
nodefs.Drive = Drive;
nodefs.volume = new Drive;
nodefs.mount = function(mountpoint: string, archive: any = {}, fs: any = null) {
var drive = new Drive;
drive.mount(mountpoint, archive, fs);
return drive;
};
export = nodefs;