2020-07-19 13:49:44 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
((window) => {
|
|
|
|
const { sendSync, sendAsync } = window.__bootstrap.dispatchJson;
|
|
|
|
const { pathFromURL } = window.__bootstrap.util;
|
|
|
|
const build = window.__bootstrap.build.build;
|
|
|
|
|
|
|
|
function chmodSync(path, mode) {
|
2020-08-28 11:08:24 -04:00
|
|
|
sendSync("op_chmod_sync", { path: pathFromURL(path), mode });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function chmod(path, mode) {
|
2020-08-28 11:08:24 -04:00
|
|
|
await sendAsync("op_chmod_async", { path: pathFromURL(path), mode });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function chownSync(
|
|
|
|
path,
|
|
|
|
uid,
|
|
|
|
gid,
|
|
|
|
) {
|
2020-08-28 11:08:24 -04:00
|
|
|
sendSync("op_chown_sync", { path: pathFromURL(path), uid, gid });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function chown(
|
|
|
|
path,
|
|
|
|
uid,
|
|
|
|
gid,
|
|
|
|
) {
|
2020-08-28 11:08:24 -04:00
|
|
|
await sendAsync("op_chown_async", { path: pathFromURL(path), uid, gid });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function copyFileSync(
|
|
|
|
fromPath,
|
|
|
|
toPath,
|
|
|
|
) {
|
2020-08-28 11:08:24 -04:00
|
|
|
sendSync("op_copy_file_sync", {
|
2020-07-19 13:49:44 -04:00
|
|
|
from: pathFromURL(fromPath),
|
|
|
|
to: pathFromURL(toPath),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function copyFile(
|
|
|
|
fromPath,
|
|
|
|
toPath,
|
|
|
|
) {
|
2020-08-28 11:08:24 -04:00
|
|
|
await sendAsync("op_copy_file_async", {
|
2020-07-19 13:49:44 -04:00
|
|
|
from: pathFromURL(fromPath),
|
|
|
|
to: pathFromURL(toPath),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function cwd() {
|
|
|
|
return sendSync("op_cwd");
|
|
|
|
}
|
|
|
|
|
|
|
|
function chdir(directory) {
|
|
|
|
sendSync("op_chdir", { directory });
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeTempDirSync(options = {}) {
|
2020-08-28 11:08:24 -04:00
|
|
|
return sendSync("op_make_temp_dir_sync", options);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeTempDir(options = {}) {
|
2020-08-28 11:08:24 -04:00
|
|
|
return sendAsync("op_make_temp_dir_async", options);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeTempFileSync(options = {}) {
|
2020-08-28 11:08:24 -04:00
|
|
|
return sendSync("op_make_temp_file_sync", options);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeTempFile(options = {}) {
|
2020-08-28 11:08:24 -04:00
|
|
|
return sendAsync("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-08-28 11:08:24 -04:00
|
|
|
sendSync("op_mkdir_sync", mkdirArgs(path, options));
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function mkdir(
|
|
|
|
path,
|
|
|
|
options,
|
|
|
|
) {
|
2020-08-28 11:08:24 -04:00
|
|
|
await sendAsync("op_mkdir_async", mkdirArgs(path, options));
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function res(response) {
|
|
|
|
return response.entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
function readDirSync(path) {
|
2020-08-28 11:08:24 -04:00
|
|
|
return res(sendSync("op_read_dir_sync", { path: pathFromURL(path) }))[
|
2020-07-19 13:49:44 -04:00
|
|
|
Symbol.iterator
|
|
|
|
]();
|
|
|
|
}
|
|
|
|
|
|
|
|
function readDir(path) {
|
2020-08-28 11:08:24 -04:00
|
|
|
const array = sendAsync("op_read_dir_async", { path: pathFromURL(path) })
|
|
|
|
.then(
|
|
|
|
res,
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
return {
|
|
|
|
async *[Symbol.asyncIterator]() {
|
|
|
|
yield* await array;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function readLinkSync(path) {
|
2020-08-28 11:08:24 -04:00
|
|
|
return sendSync("op_read_link_sync", { path });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function readLink(path) {
|
2020-08-28 11:08:24 -04:00
|
|
|
return sendAsync("op_read_link_async", { path });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function realPathSync(path) {
|
2020-08-28 11:08:24 -04:00
|
|
|
return sendSync("op_realpath_sync", { path });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function realPath(path) {
|
2020-08-28 11:08:24 -04:00
|
|
|
return sendAsync("op_realpath_async", { path });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeSync(
|
|
|
|
path,
|
|
|
|
options = {},
|
|
|
|
) {
|
2020-08-28 11:08:24 -04:00
|
|
|
sendSync("op_remove_sync", {
|
2020-07-19 13:49:44 -04:00
|
|
|
path: pathFromURL(path),
|
|
|
|
recursive: !!options.recursive,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function remove(
|
|
|
|
path,
|
|
|
|
options = {},
|
|
|
|
) {
|
2020-08-28 11:08:24 -04:00
|
|
|
await sendAsync("op_remove_async", {
|
2020-07-19 13:49:44 -04:00
|
|
|
path: pathFromURL(path),
|
|
|
|
recursive: !!options.recursive,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function renameSync(oldpath, newpath) {
|
2020-08-28 11:08:24 -04:00
|
|
|
sendSync("op_rename_sync", { oldpath, newpath });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function rename(oldpath, newpath) {
|
2020-08-28 11:08:24 -04:00
|
|
|
await sendAsync("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-08-28 11:08:24 -04:00
|
|
|
return parseFileInfo(sendSync("op_fstat_sync", { rid }));
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function fstat(rid) {
|
2020-08-28 11:08:24 -04:00
|
|
|
return parseFileInfo(await sendAsync("op_fstat_async", { rid }));
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function lstat(path) {
|
2020-08-28 11:08:24 -04:00
|
|
|
const res = await sendAsync("op_stat_async", {
|
2020-07-19 13:49:44 -04:00
|
|
|
path: pathFromURL(path),
|
|
|
|
lstat: true,
|
|
|
|
});
|
|
|
|
return parseFileInfo(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
function lstatSync(path) {
|
2020-08-28 11:08:24 -04:00
|
|
|
const res = sendSync("op_stat_sync", {
|
2020-07-19 13:49:44 -04:00
|
|
|
path: pathFromURL(path),
|
|
|
|
lstat: true,
|
|
|
|
});
|
|
|
|
return parseFileInfo(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function stat(path) {
|
2020-08-28 11:08:24 -04:00
|
|
|
const res = await sendAsync("op_stat_async", {
|
2020-07-19 13:49:44 -04:00
|
|
|
path: pathFromURL(path),
|
|
|
|
lstat: false,
|
|
|
|
});
|
|
|
|
return parseFileInfo(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
function statSync(path) {
|
2020-08-28 11:08:24 -04:00
|
|
|
const res = sendSync("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-08-28 11:08:24 -04:00
|
|
|
sendSync("op_ftruncate_sync", { rid, len: coerceLen(len) });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function ftruncate(rid, len) {
|
2020-08-28 11:08:24 -04:00
|
|
|
await sendAsync("op_ftruncate_async", { rid, len: coerceLen(len) });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function truncateSync(path, len) {
|
2020-08-28 11:08:24 -04:00
|
|
|
sendSync("op_truncate_sync", { path, len: coerceLen(len) });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function truncate(path, len) {
|
2020-08-28 11:08:24 -04:00
|
|
|
await sendAsync("op_truncate_async", { path, len: coerceLen(len) });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function umask(mask) {
|
|
|
|
return sendSync("op_umask", { mask });
|
|
|
|
}
|
|
|
|
|
|
|
|
function linkSync(oldpath, newpath) {
|
2020-08-28 11:08:24 -04:00
|
|
|
sendSync("op_link_sync", { oldpath, newpath });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function link(oldpath, newpath) {
|
2020-08-28 11:08:24 -04:00
|
|
|
await sendAsync("op_link_async", { oldpath, newpath });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function toSecondsFromEpoch(v) {
|
|
|
|
return v instanceof Date ? Math.trunc(v.valueOf() / 1000) : v;
|
|
|
|
}
|
|
|
|
|
|
|
|
function utimeSync(
|
|
|
|
path,
|
|
|
|
atime,
|
|
|
|
mtime,
|
|
|
|
) {
|
2020-08-28 11:08:24 -04:00
|
|
|
sendSync("op_utime_sync", {
|
2020-07-19 13:49:44 -04:00
|
|
|
path,
|
|
|
|
// TODO(ry) split atime, mtime into [seconds, nanoseconds] tuple
|
|
|
|
atime: toSecondsFromEpoch(atime),
|
|
|
|
mtime: toSecondsFromEpoch(mtime),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function utime(
|
|
|
|
path,
|
|
|
|
atime,
|
|
|
|
mtime,
|
|
|
|
) {
|
2020-08-28 11:08:24 -04:00
|
|
|
await sendAsync("op_utime_async", {
|
2020-07-19 13:49:44 -04:00
|
|
|
path,
|
|
|
|
// TODO(ry) split atime, mtime into [seconds, nanoseconds] tuple
|
|
|
|
atime: toSecondsFromEpoch(atime),
|
|
|
|
mtime: toSecondsFromEpoch(mtime),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function symlinkSync(
|
|
|
|
oldpath,
|
|
|
|
newpath,
|
|
|
|
options,
|
|
|
|
) {
|
2020-08-28 11:08:24 -04:00
|
|
|
sendSync("op_symlink_sync", { oldpath, newpath, options });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function symlink(
|
|
|
|
oldpath,
|
|
|
|
newpath,
|
|
|
|
options,
|
|
|
|
) {
|
2020-08-28 11:08:24 -04:00
|
|
|
await sendAsync("op_symlink_async", { oldpath, newpath, options });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function fdatasyncSync(rid) {
|
2020-08-28 11:08:24 -04:00
|
|
|
sendSync("op_fdatasync_sync", { rid });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function fdatasync(rid) {
|
2020-08-28 11:08:24 -04:00
|
|
|
await sendAsync("op_fdatasync_async", { rid });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function fsyncSync(rid) {
|
2020-08-28 11:08:24 -04:00
|
|
|
sendSync("op_fsync_sync", { rid });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function fsync(rid) {
|
2020-08-28 11:08:24 -04:00
|
|
|
await sendAsync("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,
|
|
|
|
utime,
|
|
|
|
utimeSync,
|
|
|
|
symlink,
|
|
|
|
symlinkSync,
|
|
|
|
fdatasync,
|
|
|
|
fdatasyncSync,
|
|
|
|
fsync,
|
|
|
|
fsyncSync,
|
|
|
|
};
|
|
|
|
})(this);
|