1
0
silverbullet/plugos/syscalls/fs.deno.ts

99 lines
2.9 KiB
TypeScript
Raw Normal View History

import type { SysCallMapping } from "../system.ts";
2022-10-12 09:47:13 +00:00
import { mime, path } from "../deps.ts";
2022-10-19 07:52:29 +00:00
import { base64DecodeDataUrl, base64Encode } from "../asset_bundle/base64.ts";
2022-10-12 09:47:13 +00:00
import { FileMeta } from "../../common/types.ts";
export default function fileSystemSyscalls(root = "/"): SysCallMapping {
function resolvedPath(p: string): string {
p = path.resolve(root, p);
if (!p.startsWith(root)) {
throw Error("Path outside root, not allowed");
}
return p;
}
return {
"fs.readFile": async (
_ctx,
filePath: string,
encoding: "utf8" | "dataurl" = "utf8",
2022-10-14 13:11:33 +00:00
): Promise<string> => {
const p = resolvedPath(filePath);
let text = "";
if (encoding === "utf8") {
text = await Deno.readTextFile(p);
} else {
text = `data:application/octet-stream,${
base64Encode(await Deno.readFile(p))
}`;
}
2022-10-14 13:11:33 +00:00
return text;
},
"fs.getFileMeta": async (_ctx, filePath: string): Promise<FileMeta> => {
const p = resolvedPath(filePath);
const s = await Deno.stat(p);
return {
name: filePath,
lastModified: s.mtime!.getTime(),
contentType: mime.getType(filePath) || "application/octet-stream",
size: s.size,
perm: "rw",
};
},
"fs.writeFile": async (
_ctx,
filePath: string,
text: string,
encoding: "utf8" | "dataurl" = "utf8",
): Promise<FileMeta> => {
const p = resolvedPath(filePath);
await Deno.mkdir(path.dirname(p), { recursive: true });
if (encoding === "utf8") {
await Deno.writeTextFile(p, text);
} else {
2022-10-19 07:52:29 +00:00
await Deno.writeFile(p, base64DecodeDataUrl(text));
}
const s = await Deno.stat(p);
return {
name: filePath,
lastModified: s.mtime!.getTime(),
contentType: mime.getType(filePath) || "application/octet-stream",
size: s.size,
perm: "rw",
};
},
"fs.deleteFile": async (_ctx, filePath: string): Promise<void> => {
await Deno.remove(resolvedPath(filePath));
},
"fs.listFiles": async (
_ctx,
dirPath: string,
recursive: boolean,
): Promise<FileMeta[]> => {
dirPath = resolvedPath(dirPath);
const allFiles: FileMeta[] = [];
async function walkPath(dir: string) {
const files = await Deno.readDir(dir);
for await (const file of files) {
const fullPath = path.join(dir, file.name);
const s = await Deno.stat(fullPath);
if (s.isDirectory && recursive) {
await walkPath(fullPath);
} else {
allFiles.push({
name: fullPath.substring(dirPath.length + 1),
lastModified: s.mtime!.getTime(),
contentType: mime.getType(fullPath) || "application/octet-stream",
size: s.size,
perm: "rw",
});
}
}
}
await walkPath(dirPath);
return allFiles;
},
};
}