UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

93 lines (92 loc) • 4.32 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var file_system_exports = {}; __export(file_system_exports, { FileSystem: () => FileSystem }); module.exports = __toCommonJS(file_system_exports); var import_classes = require("./classes.cjs"); var import_opfs = require("./opfs.cjs"); const operations = Object.freeze({ getDirectories: () => typeof window === "undefined" ? { documents: "", cache: "", applicationSupport: "", temporary: "" } : { ...import_opfs.directories }, getInfo: (uri) => typeof window === "undefined" ? Promise.resolve({ uri, exists: false, isDirectory: false }) : (0, import_opfs.info)(uri), readDirectory: async (uri) => { if (typeof window === "undefined") return []; const names = (0, import_opfs.parts)(uri), handle = await (0, import_opfs.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 = (0, import_opfs.parts)(uri); await (await (0, import_opfs.parent)(names, intermediates)).getDirectoryHandle(names.at(-1), { create: true }); }, writeFile: async (uri, contents, encoding = "utf8") => { if (typeof window === "undefined") return; const names = (0, import_opfs.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 (0, import_opfs.write)(await (await (0, import_opfs.parent)(names)).getFileHandle(names.at(-1), { create: true }), data); }, copy: async (from, to) => { if (typeof window !== "undefined") await (0, import_opfs.transfer)(from, to, false); }, move: async (from, to) => { if (typeof window !== "undefined") await (0, import_opfs.transfer)(from, to, true); }, delete: async (uri) => { if (typeof window === "undefined") return; const names = (0, import_opfs.parts)(uri); if (names.length === 1) throw new Error("FileSystem: app root directories are protected"); await (await (0, import_opfs.parent)(names)).removeEntry(names.at(-1), { recursive: true }); } }); const FileSystem = Object.freeze({ getDirectories: operations.getDirectories, getInfo: (uri) => (0, import_opfs.serial)(() => operations.getInfo(uri)), readDirectory: (uri) => (0, import_opfs.serial)(() => operations.readDirectory(uri)), makeDirectory: (uri, intermediates = true) => (0, import_opfs.serial)(() => operations.makeDirectory(uri, intermediates)), writeFile: (uri, contents, encoding = "utf8") => (0, import_opfs.serial)(() => operations.writeFile(uri, contents, encoding)), copy: (from, to) => (0, import_opfs.serial)(() => operations.copy(from, to)), move: (from, to) => (0, import_opfs.serial)(() => operations.move(from, to)), delete: (uri) => (0, import_opfs.serial)(() => operations.delete(uri)), File: import_classes.File, Directory: import_classes.Directory, Paths: import_classes.Paths, FileHandle: import_classes.FileHandle, FileMode: import_classes.FileMode });