2020-07-19 13:49:44 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
((window) => {
|
2020-09-16 16:22:43 -04:00
|
|
|
const core = window.Deno.core;
|
2020-07-19 13:49:44 -04:00
|
|
|
const { pathFromURL } = window.__bootstrap.util;
|
|
|
|
const build = window.__bootstrap.build.build;
|
|
|
|
|
|
|
|
function chmodSync(path, mode) {
|
2020-09-16 16:22:43 -04:00
|
|
|
core.jsonOpSync("op_chmod_sync", { path: pathFromURL(path), mode });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function chmod(path, mode) {
|
2020-09-16 16:22:43 -04:00
|
|
|
await core.jsonOpAsync("op_chmod_async", { path: pathFromURL(path), mode });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function chownSync(
|
|
|
|
path,
|
|
|
|
uid,
|
|
|
|
gid,
|
|
|
|
) {
|
2020-09-16 16:22:43 -04:00
|
|
|
core.jsonOpSync("op_chown_sync", { path: pathFromURL(path), uid, gid });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function chown(
|
|
|
|
path,
|
|
|
|
uid,
|
|
|
|
gid,
|
|
|
|
) {
|
2020-09-16 16:22:43 -04:00
|
|
|
await core.jsonOpAsync(
|
|
|
|
"op_chown_async",
|
|
|
|
{ path: pathFromURL(path), uid, gid },
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function copyFileSync(
|
|
|
|
fromPath,
|
|
|
|
toPath,
|
|
|
|
) {
|
2020-09-16 16:22:43 -04:00
|
|
|
core.jsonOpSync("op_copy_file_sync", {
|
2020-07-19 13:49:44 -04:00
|
|
|
from: pathFromURL(fromPath),
|
|
|
|
to: pathFromURL(toPath),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function copyFile(
|
|
|
|
fromPath,
|
|
|
|
toPath,
|
|
|
|
) {
|
2020-09-16 16:22:43 -04:00
|
|
|
await core.jsonOpAsync("op_copy_file_async", {
|
2020-07-19 13:49:44 -04:00
|
|
|
from: pathFromURL(fromPath),
|
|
|
|
to: pathFromURL(toPath),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function cwd() {
|
2020-09-16 16:22:43 -04:00
|
|
|
return core.jsonOpSync("op_cwd");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function chdir(directory) {
|
2020-09-16 16:22:43 -04:00
|
|
|
core.jsonOpSync("op_chdir", { directory });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeTempDirSync(options = {}) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return core.jsonOpSync("op_make_temp_dir_sync", options);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeTempDir(options = {}) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return core.jsonOpAsync("op_make_temp_dir_async", options);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeTempFileSync(options = {}) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return core.jsonOpSync("op_make_temp_file_sync", options);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeTempFile(options = {}) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return core.jsonOpAsync("op_make_temp_file_async", options);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function mkdirArgs(path, options) {
|
|
|
|
const args = { path, recursive: false };
|
|
|
|
if (options != null) {
|
|
|
|
if (typeof options.recursive == "boolean") {
|
|
|
|
args.recursive = options.recursive;
|
|
|
|
}
|
|
|
|
if (options.mode) {
|
|
|
|
args.mode = options.mode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
function mkdirSync(path, options) {
|
2020-09-16 16:22:43 -04:00
|
|
|
core.jsonOpSync("op_mkdir_sync", mkdirArgs(path, options));
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function mkdir(
|
|
|
|
path,
|
|
|
|
options,
|
|
|
|
) {
|
2020-09-16 16:22:43 -04:00
|
|
|
await core.jsonOpAsync("op_mkdir_async", mkdirArgs(path, options));
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function res(response) {
|
|
|
|
return response.entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
function readDirSync(path) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return res(
|
|
|
|
core.jsonOpSync("op_read_dir_sync", { path: pathFromURL(path) }),
|
|
|
|
)[
|
2020-07-19 13:49:44 -04:00
|
|
|
Symbol.iterator
|
|
|
|
]();
|
|
|
|
}
|
|
|
|
|
|
|
|
function readDir(path) {
|
2020-09-16 16:22:43 -04:00
|
|
|
const array = core.jsonOpAsync(
|
|
|
|
"op_read_dir_async",
|
|
|
|
{ path: pathFromURL(path) },
|
|
|
|
)
|
2020-08-28 11:08:24 -04:00
|
|
|
.then(
|
|
|
|
res,
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
return {
|
|
|
|
async *[Symbol.asyncIterator]() {
|
|
|
|
yield* await array;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function readLinkSync(path) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return core.jsonOpSync("op_read_link_sync", { path });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function readLink(path) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return core.jsonOpAsync("op_read_link_async", { path });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function realPathSync(path) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return core.jsonOpSync("op_realpath_sync", { path });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function realPath(path) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return core.jsonOpAsync("op_realpath_async", { path });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeSync(
|
|
|
|
path,
|
|
|
|
options = {},
|
|
|
|
) {
|
2020-09-16 16:22:43 -04:00
|
|
|
core.jsonOpSync("op_remove_sync", {
|
2020-07-19 13:49:44 -04:00
|
|
|
path: pathFromURL(path),
|
|
|
|
recursive: !!options.recursive,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function remove(
|
|
|
|
path,
|
|
|
|
options = {},
|
|
|
|
) {
|
2020-09-16 16:22:43 -04:00
|
|
|
await core.jsonOpAsync("op_remove_async", {
|
2020-07-19 13:49:44 -04:00
|
|
|
path: pathFromURL(path),
|
|
|
|
recursive: !!options.recursive,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function renameSync(oldpath, newpath) {
|
2020-09-16 16:22:43 -04:00
|
|
|
core.jsonOpSync("op_rename_sync", { oldpath, newpath });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function rename(oldpath, newpath) {
|
2020-09-16 16:22:43 -04:00
|
|
|
await core.jsonOpAsync("op_rename_async", { oldpath, newpath });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function parseFileInfo(response) {
|
|
|
|
const unix = build.os === "darwin" || build.os === "linux";
|
|
|
|
return {
|
|
|
|
isFile: response.isFile,
|
|
|
|
isDirectory: response.isDirectory,
|
|
|
|
isSymlink: response.isSymlink,
|
|
|
|
size: response.size,
|
|
|
|
mtime: response.mtime != null ? new Date(response.mtime) : null,
|
|
|
|
atime: response.atime != null ? new Date(response.atime) : null,
|
|
|
|
birthtime: response.birthtime != null
|
|
|
|
? new Date(response.birthtime)
|
|
|
|
: null,
|
|
|
|
// Only non-null if on Unix
|
|
|
|
dev: unix ? response.dev : null,
|
|
|
|
ino: unix ? response.ino : null,
|
|
|
|
mode: unix ? response.mode : null,
|
|
|
|
nlink: unix ? response.nlink : null,
|
|
|
|
uid: unix ? response.uid : null,
|
|
|
|
gid: unix ? response.gid : null,
|
|
|
|
rdev: unix ? response.rdev : null,
|
|
|
|
blksize: unix ? response.blksize : null,
|
|
|
|
blocks: unix ? response.blocks : null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function fstatSync(rid) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return parseFileInfo(core.jsonOpSync("op_fstat_sync", { rid }));
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function fstat(rid) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return parseFileInfo(await core.jsonOpAsync("op_fstat_async", { rid }));
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function lstat(path) {
|
2020-09-16 16:22:43 -04:00
|
|
|
const res = await core.jsonOpAsync("op_stat_async", {
|
2020-07-19 13:49:44 -04:00
|
|
|
path: pathFromURL(path),
|
|
|
|
lstat: true,
|
|
|
|
});
|
|
|
|
return parseFileInfo(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
function lstatSync(path) {
|
2020-09-16 16:22:43 -04:00
|
|
|
const res = core.jsonOpSync("op_stat_sync", {
|
2020-07-19 13:49:44 -04:00
|
|
|
path: pathFromURL(path),
|
|
|
|
lstat: true,
|
|
|
|
});
|
|
|
|
return parseFileInfo(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function stat(path) {
|
2020-09-16 16:22:43 -04:00
|
|
|
const res = await core.jsonOpAsync("op_stat_async", {
|
2020-07-19 13:49:44 -04:00
|
|
|
path: pathFromURL(path),
|
|
|
|
lstat: false,
|
|
|
|
});
|
|
|
|
return parseFileInfo(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
function statSync(path) {
|
2020-09-16 16:22:43 -04:00
|
|
|
const res = core.jsonOpSync("op_stat_sync", {
|
2020-07-19 13:49:44 -04:00
|
|
|
path: pathFromURL(path),
|
|
|
|
lstat: false,
|
|
|
|
});
|
|
|
|
return parseFileInfo(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
function coerceLen(len) {
|
|
|
|
if (len == null || len < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ftruncateSync(rid, len) {
|
2020-09-16 16:22:43 -04:00
|
|
|
core.jsonOpSync("op_ftruncate_sync", { rid, len: coerceLen(len) });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function ftruncate(rid, len) {
|
2020-09-16 16:22:43 -04:00
|
|
|
await core.jsonOpAsync("op_ftruncate_async", { rid, len: coerceLen(len) });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function truncateSync(path, len) {
|
2020-09-16 16:22:43 -04:00
|
|
|
core.jsonOpSync("op_truncate_sync", { path, len: coerceLen(len) });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function truncate(path, len) {
|
2020-09-16 16:22:43 -04:00
|
|
|
await core.jsonOpAsync("op_truncate_async", { path, len: coerceLen(len) });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function umask(mask) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return core.jsonOpSync("op_umask", { mask });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function linkSync(oldpath, newpath) {
|
2020-09-16 16:22:43 -04:00
|
|
|
core.jsonOpSync("op_link_sync", { oldpath, newpath });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function link(oldpath, newpath) {
|
2020-09-16 16:22:43 -04:00
|
|
|
await core.jsonOpAsync("op_link_async", { oldpath, newpath });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2020-09-01 10:03:07 -04:00
|
|
|
function toUnixTimeFromEpoch(value) {
|
|
|
|
if (value instanceof Date) {
|
|
|
|
const time = value.valueOf();
|
|
|
|
const seconds = Math.trunc(time / 1e3);
|
|
|
|
const nanoseconds = Math.trunc(time - (seconds * 1e3)) * 1e6;
|
|
|
|
|
|
|
|
return [
|
|
|
|
seconds,
|
|
|
|
nanoseconds,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
const seconds = value;
|
|
|
|
const nanoseconds = 0;
|
|
|
|
|
|
|
|
return [
|
|
|
|
seconds,
|
|
|
|
nanoseconds,
|
|
|
|
];
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2020-08-31 14:29:43 -04:00
|
|
|
function futimeSync(
|
|
|
|
rid,
|
|
|
|
atime,
|
|
|
|
mtime,
|
|
|
|
) {
|
2020-09-16 16:22:43 -04:00
|
|
|
core.jsonOpSync("op_futime_sync", {
|
2020-08-31 14:29:43 -04:00
|
|
|
rid,
|
2020-09-01 10:03:07 -04:00
|
|
|
atime: toUnixTimeFromEpoch(atime),
|
|
|
|
mtime: toUnixTimeFromEpoch(mtime),
|
2020-08-31 14:29:43 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function futime(
|
|
|
|
rid,
|
|
|
|
atime,
|
|
|
|
mtime,
|
|
|
|
) {
|
2020-09-16 16:22:43 -04:00
|
|
|
await core.jsonOpAsync("op_futime_async", {
|
2020-08-31 14:29:43 -04:00
|
|
|
rid,
|
2020-09-01 10:03:07 -04:00
|
|
|
atime: toUnixTimeFromEpoch(atime),
|
|
|
|
mtime: toUnixTimeFromEpoch(mtime),
|
2020-08-31 14:29:43 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
function utimeSync(
|
|
|
|
path,
|
|
|
|
atime,
|
|
|
|
mtime,
|
|
|
|
) {
|
2020-09-16 16:22:43 -04:00
|
|
|
core.jsonOpSync("op_utime_sync", {
|
2020-07-19 13:49:44 -04:00
|
|
|
path,
|
2020-09-01 10:03:07 -04:00
|
|
|
atime: toUnixTimeFromEpoch(atime),
|
|
|
|
mtime: toUnixTimeFromEpoch(mtime),
|
2020-07-19 13:49:44 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function utime(
|
|
|
|
path,
|
|
|
|
atime,
|
|
|
|
mtime,
|
|
|
|
) {
|
2020-09-16 16:22:43 -04:00
|
|
|
await core.jsonOpAsync("op_utime_async", {
|
2020-07-19 13:49:44 -04:00
|
|
|
path,
|
2020-09-01 10:03:07 -04:00
|
|
|
atime: toUnixTimeFromEpoch(atime),
|
|
|
|
mtime: toUnixTimeFromEpoch(mtime),
|
2020-07-19 13:49:44 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function symlinkSync(
|
|
|
|
oldpath,
|
|
|
|
newpath,
|
|
|
|
options,
|
|
|
|
) {
|
2020-09-16 16:22:43 -04:00
|
|
|
core.jsonOpSync("op_symlink_sync", { oldpath, newpath, options });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function symlink(
|
|
|
|
oldpath,
|
|
|
|
newpath,
|
|
|
|
options,
|
|
|
|
) {
|
2020-09-16 16:22:43 -04:00
|
|
|
await core.jsonOpAsync("op_symlink_async", { oldpath, newpath, options });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function fdatasyncSync(rid) {
|
2020-09-16 16:22:43 -04:00
|
|
|
core.jsonOpSync("op_fdatasync_sync", { rid });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function fdatasync(rid) {
|
2020-09-16 16:22:43 -04:00
|
|
|
await core.jsonOpAsync("op_fdatasync_async", { rid });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function fsyncSync(rid) {
|
2020-09-16 16:22:43 -04:00
|
|
|
core.jsonOpSync("op_fsync_sync", { rid });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function fsync(rid) {
|
2020-09-16 16:22:43 -04:00
|
|
|
await core.jsonOpAsync("op_fsync_async", { rid });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.fs = {
|
|
|
|
cwd,
|
|
|
|
chdir,
|
|
|
|
chmodSync,
|
|
|
|
chmod,
|
|
|
|
chown,
|
|
|
|
chownSync,
|
|
|
|
copyFile,
|
|
|
|
copyFileSync,
|
|
|
|
makeTempFile,
|
|
|
|
makeTempDir,
|
|
|
|
makeTempFileSync,
|
|
|
|
makeTempDirSync,
|
|
|
|
mkdir,
|
|
|
|
mkdirSync,
|
|
|
|
readDir,
|
|
|
|
readDirSync,
|
|
|
|
readLinkSync,
|
|
|
|
readLink,
|
|
|
|
realPathSync,
|
|
|
|
realPath,
|
|
|
|
remove,
|
|
|
|
removeSync,
|
|
|
|
renameSync,
|
|
|
|
rename,
|
|
|
|
fstatSync,
|
|
|
|
fstat,
|
|
|
|
lstat,
|
|
|
|
lstatSync,
|
|
|
|
stat,
|
|
|
|
statSync,
|
|
|
|
ftruncate,
|
|
|
|
ftruncateSync,
|
|
|
|
truncate,
|
|
|
|
truncateSync,
|
|
|
|
umask,
|
|
|
|
link,
|
|
|
|
linkSync,
|
2020-08-31 14:29:43 -04:00
|
|
|
futime,
|
|
|
|
futimeSync,
|
2020-07-19 13:49:44 -04:00
|
|
|
utime,
|
|
|
|
utimeSync,
|
|
|
|
symlink,
|
|
|
|
symlinkSync,
|
|
|
|
fdatasync,
|
|
|
|
fdatasyncSync,
|
|
|
|
fsync,
|
|
|
|
fsyncSync,
|
|
|
|
};
|
|
|
|
})(this);
|