-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvfs.js
165 lines (134 loc) · 3.54 KB
/
vfs.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
/**
* @module vfs
*
* Virtual File System
*
* Provides a framework to abstract out operating against the local file system or a remote file system. This is useful
* in testing, distributed portability, or vendor independence.
*/
/**********************************************************
* Local VFS
**********************************************************/
const fs = require("fs");
const {
exists,
unlink,
readFile,
writeFile
} = require("./fs");
class LocalFileSystem {
async exists( file ){
return await exists(file);
}
async unlink( file ){
return await unlink(file);
}
async createReadableStream( file ){
return fs.createReadStream(file);
}
async createWritableStream( file ){
return fs.createWriteStream(file);
}
async asBytes( file ){
return await readFile( file );
}
async putBytes( file, bytes, encoding ){
await writeFile(file, bytes, {encoding});
}
}
/**********************************************************
* In Memory VFS
**********************************************************/
const {
MemoryReadable,
MemoryWritable
} = require("./streams");
class InMemoryVFS {
constructor() {
this.files = {};
}
async exists( file ){
return !!this.files[file];
}
async _expectFile( file ){
if( !this.files[file]){
throw new Error("No such file " + file);
}
}
async unlink( file ){
if( this.files[file] ){
delete this.files[file];
}
}
async createReadableStream( file ){
await this._expectFile(file);
return new MemoryReadable(this.files[file]);
}
async asBytes( file ){
await this._expectFile(file);
return Buffer.from(this.files[file]);
}
async putBytes( file, bytes, encoding ){
this.files[file] = Buffer.from(bytes, encoding);
}
async createWritableStream( file ){
const writable = new MemoryWritable();
writable.on("finish", () => {
this.files[file] = writable.bytes;
});
return writable;
}
}
/**********************************************************
*
**********************************************************/
const path = require("path");
function jailedPath( root, relative ){
const relativeNormalized = path.normalize(relative);
const resolvedPath = path.resolve(root, relativeNormalized);
const relativeResult = path.relative(root, resolvedPath);
const actualParts = relativeResult.split(path.sep).filter((c) => c != "..");
return [root].concat(actualParts).join(path.sep);
}
/**********************************************************
*
**********************************************************/
class JailedVFS {
constructor(root, vfs) {
this.root = root;
this.vfs = vfs;
}
async exists( file ){
const fileName = jailedPath(this.root, file);
return await this.vfs.exists(fileName);
}
async unlink( file ){
const fileName = jailedPath(this.root, file);
return await this.vfs.unlink(fileName);
}
async createReadableStream( file ){
const fileName = jailedPath(this.root, file);
return await this.vfs.createReadableStream(fileName);
}
async asBytes( file ){
const fileName = jailedPath(this.root, file);
return await this.vfs.asBytes(fileName);
}
async putBytes( file, bytes, encoding ){
const fileName = jailedPath(this.root, file);
return await this.vfs.putBytes(fileName);
}
async createWritableStream( file ){
const fileName = jailedPath(this.root, file);
return await this.vfs.createWritableStream(fileName);
}
}
/**********************************************************
*
**********************************************************/
module.exports = {
InMemoryVFS,
jailedPath,
JailedVFS,
LocalFileSystem
}