-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfs.js
209 lines (196 loc) · 4.55 KB
/
fs.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
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
import memfs from 'memfs';
import volume from 'memfs/lib/volume.js';
const { vol, createFsFromVolume } = memfs;
import { Buffer } from './buffer.js';
import { fileURLToPath } from './url.js';
function unimplemented(name) {
throw new Error(`Node.js fs ${name} is not supported by JSPM core in the browser`);
}
vol.fromNestedJSON({
'/dev': { stdin: '', stdout: '', stderr: '' },
'/usr/bin': {},
'/home': {},
'/tmp': {},
});
vol.releasedFds = [2, 1, 0];
vol.openSync('/dev/stdin', 'w');
vol.openSync('/dev/stdout', 'r');
vol.openSync('/dev/stderr', 'r');
watchStdo('/dev/stdout', 1, console.log);
watchStdo('/dev/stderr', 2, console.error);
function watchStdo(path, fd, listener) {
let oldSize = 0;
const decoder = new TextDecoder();
vol.watch(path, 'utf8', () => {
const { size } = vol.fstatSync(fd);
const buf = Buffer.alloc(size - oldSize);
vol.readSync(fd, buf, 0, buf.length, oldSize);
oldSize = size;
listener(decoder.decode(buf, { stream: true }));
});
}
const fs = createFsFromVolume(vol);
fs.opendir = () => unimplemented('opendir');
fs.opendirSync = () => unimplemented('opendirSync');
fs.promises.opendir = () => unimplemented('promises.opendir');
fs.cp = () => unimplemented('cp');
fs.cpSync = () => unimplemented('cpSync');
fs.promises.cp = () => unimplemented('promises.cp');
fs.readv = () => unimplemented('readv');
fs.readvSync = () => unimplemented('readvSync');
fs.rm = () => unimplemented('rm');
fs.rmSync = () => unimplemented('rmSync');
fs.promises.rm = () => unimplemented('promises.rm');
fs.Dir = () => unimplemented('Dir');
fs.promises.watch = () => unimplemented('promises.watch');
fs.FileReadStream = fs.ReadStream;
fs.FileWriteStream = fs.WriteStream;
function handleFsUrl (url, isSync) {
if (url.protocol === 'file:')
return fileURLToPath(url);
if (url.protocol === 'https:' || url.protocol === 'http:') {
const path = '\\\\url\\' + url.href.replaceAll(/\//g, '\\\\');
if (existsSync(path))
return path;
if (isSync)
throw new Error(`Cannot sync request URL ${url} via FS. JSPM FS support for network URLs requires using async FS methods or priming the MemFS cache first with an async request before a sync request.`);
return (async () => {
const res = await fetch(url);
if (!res.ok)
throw new Error(`Unable to fetch ${url.href}, ${res.status}`);
const buf = await res.arrayBuffer();
writeFileSync(path, Buffer.from(buf));
return path;
})();
}
throw new Error('URL ' + url + ' not supported in JSPM FS implementation.');
}
function wrapFsSync (fn) {
return function (path, ...args) {
if (path instanceof URL)
return fn(handleFsUrl(path, true), ...args);
return fn(path, ...args);
};
}
function wrapFsPromise (fn) {
return async function (path, ...args) {
if (path instanceof URL)
return fn(await handleFsUrl(path), ...args);
return fn(path, ...args);
};
}
function wrapFsCallback (fn) {
return function (path, ...args) {
const cb = args[args.length - 1];
if (path instanceof URL && typeof cb === 'function') {
handleFsUrl(path).then(path => {
fn(path, ...args);
}, cb);
}
else {
fn(path, ...args);
}
};
}
fs.promises.readFile = wrapFsPromise(fs.promises.readFile);
fs.readFile = wrapFsCallback(fs.readFile);
fs.readFileSync = wrapFsSync(fs.readFileSync);
export const {
appendFile,
appendFileSync,
access,
accessSync,
chown,
chownSync,
chmod,
chmodSync,
close,
closeSync,
copyFile,
copyFileSync,
cp,
cpSync,
createReadStream,
createWriteStream,
exists,
existsSync,
fchown,
fchownSync,
fchmod,
fchmodSync,
fdatasync,
fdatasyncSync,
fstat,
fstatSync,
fsync,
fsyncSync,
ftruncate,
ftruncateSync,
futimes,
futimesSync,
lchown,
lchownSync,
lchmod,
lchmodSync,
link,
linkSync,
lstat,
lstatSync,
mkdir,
mkdirSync,
mkdtemp,
mkdtempSync,
open,
openSync,
opendir,
opendirSync,
readdir,
readdirSync,
read,
readSync,
readv,
readvSync,
readFile,
readFileSync,
readlink,
readlinkSync,
realpath,
realpathSync,
rename,
renameSync,
rm,
rmSync,
rmdir,
rmdirSync,
stat,
statSync,
symlink,
symlinkSync,
truncate,
truncateSync,
unwatchFile,
unlink,
unlinkSync,
utimes,
utimesSync,
watch,
watchFile,
writeFile,
writeFileSync,
write,
writeSync,
writev,
writevSync,
Dir,
Dirent,
Stats,
ReadStream,
WriteStream,
FileReadStream,
FileWriteStream,
_toUnixTimestamp,
constants: { F_OK, R_OK, W_OK, X_OK },
constants,
promises,
} = fs;
export default fs;