opfs-extra brings the user-friendly API to OPFS, just like fs-extra
npm i opfs-extraimport { OPFS } from "opfs-extra";
// Initialization
const opfs = await OPFS.open();
// Directory Creating
await opfs.mkdir("/data");
// File Writing
await opfs.writeFile("/data/hello.txt", "Hello World");
await opfs.writeJSON("/data/hello.json", { text: "Hello World" });
// File Reading
const text = await opfs.readText("/data/hello.txt"); // "Hello World"
const json = await opfs.readJSON("/data/hello.json"); // { text: "Hello World" }
// Directory Reading
const files = await opfs.readdir("/data"); // ["hello.txt", "hello.json"]
// Exists
await opfs.exists("/data/hello.json"); // true
// Remove
await opfs.remove("/data");- open - Opens access to the Origin Private File System.
- root - Gets the handle to the root directory of the Origin Private File System.
- estimate - Estimates storage usage and quota.
- getFileHandle - Gets a handle to a file.
- getDirectoryHandle - Gets a handle to a directory.
- readBinary - Reads a file as an ArrayBuffer.
- readText - Reads a file as plain text.
- readJSON - Reads a file as a JSON Object.
- readJSONL - Reads a JSONL file as a JSON Array.
These methods will create file and parent directories automatically if needed.
- writeFile - Writes data to a file.
- writeJSON - Writes JSON data to a file.
- appendFile - Appends data to a file.
- appendJSONL - Appends JSON data to a JSONL file.
- createAppendable - Creates a writable stream ready for high-performance appending.
- truncate - Truncates a file to a specified size.
- mkdir - Creates a directory (creates directories if needed).
- emptyDir - Empties a directory (creates directories if needed).
- readdir - Reads the directory contents as names.
- readdirHandles - Reads the directory contents as handles.