Repository navigation
Expand file tree
/
Copy pathFileSystem.js
More file actions
409 lines (409 loc) · 14.1 KB
/
Copy pathFileSystem.js
File metadata and controls
409 lines (409 loc) · 14.1 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
import * as fs from "fs/promises";
import * as path from "path";
import * as os from "os";
import { glob as matchGlob } from "glob";
function resolvePath(ctx, p) {
if (!p)
return "";
if (path.isAbsolute(p))
return p;
return path.resolve(ctx?.cwd || process.cwd(), p);
}
export class FileSystemRuntime {
static async fileowner(ctx, filenameArg) {
try {
const filename = resolvePath(ctx, String(await filenameArg?.get() ?? ""));
return (await fs.stat(filename)).uid;
}
catch {
return false;
}
}
static async fileperms(ctx, filenameArg) {
try {
const filename = resolvePath(ctx, String(await filenameArg?.get() ?? ""));
return (await fs.stat(filename)).mode;
}
catch {
return false;
}
}
static async glob(ctx, patternArg, flagsArg) {
try {
const pattern = String(await patternArg?.get() ?? "");
const flags = Number(await flagsArg?.get()) || 0;
const cwd = ctx.cwd;
const normalizedPattern = process.platform === "win32" ? pattern.replace(/\\/g, "/") : pattern;
let matches = await matchGlob(normalizedPattern, {
cwd,
mark: Boolean(flags & 2),
nobrace: !(flags & 1024),
noext: true,
windowsPathsNoEscape: process.platform === "win32" || Boolean(flags & 64),
});
if (flags & 8192) {
const directories = await Promise.all(matches.map(async (entry) => {
try {
return (await fs.stat(path.resolve(cwd, entry))).isDirectory();
}
catch {
return false;
}
}));
matches = matches.filter((entry, index) => directories[index]);
}
matches = matches.map((entry) => entry.replace(/\\/g, "/"));
if (!(flags & 4))
matches.sort();
return matches.length === 0 && (flags & 16) ? [pattern] : matches;
}
catch {
return false;
}
}
static async file(ctx, filepathArg, flagsArg) {
try {
const filepath = resolvePath(ctx, String(await filepathArg?.get() ?? ""));
const flags = Number(await flagsArg?.get()) || 0;
const content = await fs.readFile(filepath, "utf8");
const lines = content.split("\n").map((line, idx, arr) => (idx < arr.length - 1 ? line + "\n" : line));
if (flags & 2) { // FILE_SKIP_EMPTY_LINES
return lines.filter((l) => l.trim().length > 0);
}
return lines;
}
catch {
return false;
}
}
static async file_get_contents(ctx, filepathArg) {
try {
const strPath = await ctx.str(await ctx.toVal(filepathArg));
if (strPath.startsWith("http://") || strPath.startsWith("https://")) {
try {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 1000);
if (typeof timer.unref === "function")
timer.unref();
try {
const res = await fetch(strPath, { signal: controller.signal });
clearTimeout(timer);
return await res.text();
}
catch {
clearTimeout(timer);
return false;
}
}
catch {
return false;
}
}
const filepath = resolvePath(ctx, strPath);
return await fs.readFile(filepath, "utf8");
}
catch {
return false;
}
}
static async file_put_contents(ctx, filepathArg, dataArg, flagsArg) {
try {
const filepath = resolvePath(ctx, String(await filepathArg?.get() ?? ""));
const data = await dataArg?.get();
const flags = Number(await flagsArg?.get()) || 0;
const str = typeof data === "string" || Buffer.isBuffer(data) ? data : String(data ?? "");
if (flags & 8) { // FILE_APPEND = 8
await fs.appendFile(filepath, str);
}
else {
await fs.writeFile(filepath, str);
}
return typeof str === "string" ? Buffer.byteLength(str) : str.length;
}
catch {
return false;
}
}
static async file_exists(ctx, filepathArg) {
try {
const filepath = resolvePath(ctx, String(await filepathArg?.get() ?? ""));
await fs.access(filepath);
return true;
}
catch {
return false;
}
}
static async is_dir(ctx, filepathArg) {
try {
const filepath = resolvePath(ctx, String(await filepathArg?.get() ?? ""));
const stat = await fs.stat(filepath);
return stat.isDirectory();
}
catch {
return false;
}
}
static async is_file(ctx, filepathArg) {
try {
const filepath = resolvePath(ctx, String(await filepathArg?.get() ?? ""));
const stat = await fs.stat(filepath);
return stat.isFile();
}
catch {
return false;
}
}
static async is_readable(ctx, filepathArg) {
try {
const filepath = resolvePath(ctx, String(await filepathArg?.get() ?? ""));
await fs.access(filepath, fs.constants.R_OK);
return true;
}
catch {
return false;
}
}
static async is_writable(ctx, filepathArg) {
try {
const filepath = resolvePath(ctx, String(await filepathArg?.get() ?? ""));
await fs.access(filepath, fs.constants.W_OK);
return true;
}
catch {
try {
const filepath = resolvePath(ctx, String(await filepathArg?.get() ?? ""));
await fs.access(filepath, fs.constants.F_OK);
return true;
}
catch {
return false;
}
}
}
static async filesize(ctx, filepathArg) {
try {
const filepath = resolvePath(ctx, String(await filepathArg?.get() ?? ""));
const stat = await fs.stat(filepath);
return stat.size;
}
catch {
return false;
}
}
static async filemtime(ctx, filepathArg) {
try {
const filepath = resolvePath(ctx, String(await filepathArg?.get() ?? ""));
const stat = await fs.stat(filepath);
return Math.floor(stat.mtimeMs / 1000);
}
catch {
return false;
}
}
static async realpath(ctx, filepathArg) {
try {
const filepath = resolvePath(ctx, String(await filepathArg?.get() ?? ""));
return await fs.realpath(filepath);
}
catch {
return false;
}
}
static async basename(ctx, filepathArg, suffixArg) {
const filepath = String(await ctx.toVal(filepathArg) ?? "");
const suffixVal = await ctx.toVal(suffixArg);
const suffix = suffixVal ? String(suffixVal) : undefined;
const normalized = filepath.replace(/\\/g, "/");
let base = path.basename(normalized);
if (suffix && base.endsWith(suffix)) {
base = base.substring(0, base.length - suffix.length);
}
return base;
}
static async dirname(ctx, filepathArg) {
const filepath = String(await ctx.toVal(filepathArg) ?? "");
return path.dirname(filepath);
}
static async pathinfo(ctx, filepathArg, flagsArg) {
const filepath = String(await ctx.toVal(filepathArg) ?? "");
const parsed = path.parse(filepath);
return {
dirname: parsed.dir,
basename: parsed.base,
extension: parsed.ext ? parsed.ext.substring(1) : "",
filename: parsed.name,
};
}
static async mkdir(ctx, dirpathArg, modeArg, recursiveArg) {
try {
const dirpath = resolvePath(ctx, String(await dirpathArg?.get() ?? ""));
const mode = Number(await modeArg?.get()) || 0o777;
const recursive = Boolean(await recursiveArg?.get());
await fs.mkdir(dirpath, { recursive, mode });
return true;
}
catch {
return false;
}
}
static async chmod(ctx, filenameArg, modeArg) {
try {
const filename = resolvePath(ctx, String(await filenameArg?.get() ?? ""));
const mode = Number(await modeArg?.get()) || 0o666;
await fs.chmod(filename, mode);
return true;
}
catch {
return true;
}
}
static async rmdir(ctx, dirpathArg) {
try {
const dirpath = resolvePath(ctx, String(await dirpathArg?.get() ?? ""));
await fs.rmdir(dirpath);
return true;
}
catch {
return false;
}
}
static async unlink(ctx, filepathArg) {
try {
const filepath = resolvePath(ctx, String(await filepathArg?.get() ?? ""));
await fs.unlink(filepath);
return true;
}
catch {
return false;
}
}
static async rename(ctx, oldnameArg, newnameArg) {
try {
const oldname = resolvePath(ctx, String(await oldnameArg?.get() ?? ""));
const newname = resolvePath(ctx, String(await newnameArg?.get() ?? ""));
await fs.rename(oldname, newname);
return true;
}
catch {
return false;
}
}
static async copy(ctx, sourceArg, destArg) {
try {
const source = resolvePath(ctx, String(await sourceArg?.get() ?? ""));
const dest = resolvePath(ctx, String(await destArg?.get() ?? ""));
await fs.copyFile(source, dest);
return true;
}
catch {
return false;
}
}
static async tempnam(ctx, dirArg, prefixArg) {
try {
const dir = resolvePath(ctx, String(await dirArg?.get() ?? ""));
const prefix = String(await prefixArg?.get() ?? "");
const name = path.join(dir, `${prefix}${Math.random().toString(36).substring(2)}`);
await fs.writeFile(name, "");
return name;
}
catch {
return false;
}
}
static async sys_get_temp_dir(ctx) {
return os.tmpdir();
}
static async scandir(ctx, dirpathArg) {
try {
const dirpath = resolvePath(ctx, String(await dirpathArg?.get() ?? ""));
return await fs.readdir(dirpath);
}
catch {
return false;
}
}
static async opendir(ctx, pathArg) {
const rawPath = String(await ctx.toVal(pathArg) ?? "");
const resolved = resolvePath(ctx, rawPath);
try {
if (!(await fs.stat(resolved)).isDirectory())
return false;
const entries = await fs.readdir(resolved);
return { isResource: true, resourceType: "dir", path: resolved, entries, ptr: 0 };
}
catch {
return false;
}
}
static async readdir(ctx, handleArg) {
const handle = await ctx.toVal(handleArg);
if (!handle || typeof handle !== "object" || !Array.isArray(handle.entries))
return false;
if (handle.ptr >= handle.entries.length)
return false;
return handle.entries[handle.ptr++];
}
static async closedir(ctx, handleArg) {
const handle = await ctx.toVal(handleArg);
if (handle && typeof handle === "object")
handle.entries = [];
return true;
}
static async rewinddir(ctx, handleArg) {
const handle = await ctx.toVal(handleArg);
if (handle && typeof handle === "object")
handle.ptr = 0;
}
static constants = {
glob_err: 1,
glob_mark: 2,
glob_nosort: 4,
glob_nocheck: 16,
glob_noescape: 64,
glob_brace: 1024,
glob_onlydir: 8192,
file_use_include_path: 1,
file_ignore_new_lines: 2,
file_skip_empty_lines: 4,
file_append: 8,
};
static functions = {
"fileowner": FileSystemRuntime.fileowner,
"fileperms": FileSystemRuntime.fileperms,
"glob": FileSystemRuntime.glob,
"file": FileSystemRuntime.file,
"file_get_contents": FileSystemRuntime.file_get_contents,
"file_put_contents": FileSystemRuntime.file_put_contents,
"file_exists": FileSystemRuntime.file_exists,
"is_dir": FileSystemRuntime.is_dir,
"is_file": FileSystemRuntime.is_file,
"is_readable": FileSystemRuntime.is_readable,
"is_writable": FileSystemRuntime.is_writable,
"filesize": FileSystemRuntime.filesize,
"filemtime": FileSystemRuntime.filemtime,
"realpath": FileSystemRuntime.realpath,
"basename": FileSystemRuntime.basename,
"dirname": FileSystemRuntime.dirname,
"pathinfo": FileSystemRuntime.pathinfo,
"mkdir": FileSystemRuntime.mkdir,
"opendir": FileSystemRuntime.opendir,
"readdir": FileSystemRuntime.readdir,
"closedir": FileSystemRuntime.closedir,
"rewinddir": FileSystemRuntime.rewinddir,
"chmod": FileSystemRuntime.chmod,
"rmdir": FileSystemRuntime.rmdir,
"unlink": FileSystemRuntime.unlink,
"rename": FileSystemRuntime.rename,
"copy": FileSystemRuntime.copy,
"tempnam": FileSystemRuntime.tempnam,
"sys_get_temp_dir": FileSystemRuntime.sys_get_temp_dir,
"scandir": FileSystemRuntime.scandir,
};
static register(engine) {
engine.registerConstants(FileSystemRuntime.constants);
engine.registerFunctions(FileSystemRuntime.functions);
}
}
//# sourceMappingURL=FileSystem.js.map