one
Version:
One is a React framework for web, iOS, and Android apps.
74 lines (71 loc) • 3.09 kB
JavaScript
import { Directory, File, FileHandle, FileMode, Paths } from "./classes.mjs";
import { directories, entry, info, parent, parts, serial, transfer, write } from "./opfs.mjs";
const operations = Object.freeze({
getDirectories: () => typeof window === "undefined" ? {
documents: "",
cache: "",
applicationSupport: "",
temporary: ""
} : { ...directories },
getInfo: (uri) => typeof window === "undefined" ? Promise.resolve({
uri,
exists: false,
isDirectory: false
}) : info(uri),
readDirectory: async (uri) => {
if (typeof window === "undefined") return [];
const names = parts(uri), handle = await entry(names);
if (handle.kind !== "directory") throw new Error("FileSystem: expected a directory");
const entries = [];
for await (const [name, child] of handle.entries()) entries.push({
name,
uri: `opfs://one/${[...names, name].map(encodeURIComponent).join("/")}${child.kind === "directory" ? "/" : ""}`,
isDirectory: child.kind === "directory"
});
return entries.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
},
makeDirectory: async (uri, intermediates = true) => {
if (typeof window === "undefined") return;
const names = parts(uri);
await (await parent(names, intermediates)).getDirectoryHandle(names.at(-1), { create: true });
},
writeFile: async (uri, contents, encoding = "utf8") => {
if (typeof window === "undefined") return;
const names = parts(uri);
let data = contents;
if (encoding === "base64") {
if (!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(contents)) throw new Error("FileSystem: invalid base64 contents");
data = Uint8Array.from(atob(contents), (char) => char.charCodeAt(0));
} else if (encoding !== "utf8") throw new Error("FileSystem: invalid encoding");
await write(await (await parent(names)).getFileHandle(names.at(-1), { create: true }), data);
},
copy: async (from, to) => {
if (typeof window !== "undefined") await transfer(from, to, false);
},
move: async (from, to) => {
if (typeof window !== "undefined") await transfer(from, to, true);
},
delete: async (uri) => {
if (typeof window === "undefined") return;
const names = parts(uri);
if (names.length === 1) throw new Error("FileSystem: app root directories are protected");
await (await parent(names)).removeEntry(names.at(-1), { recursive: true });
}
});
const FileSystem = Object.freeze({
getDirectories: operations.getDirectories,
getInfo: (uri) => serial(() => operations.getInfo(uri)),
readDirectory: (uri) => serial(() => operations.readDirectory(uri)),
makeDirectory: (uri, intermediates = true) => serial(() => operations.makeDirectory(uri, intermediates)),
writeFile: (uri, contents, encoding = "utf8") => serial(() => operations.writeFile(uri, contents, encoding)),
copy: (from, to) => serial(() => operations.copy(from, to)),
move: (from, to) => serial(() => operations.move(from, to)),
delete: (uri) => serial(() => operations.delete(uri)),
File,
Directory,
Paths,
FileHandle,
FileMode
});
export { FileSystem };
//# sourceMappingURL=index.mjs.map