Skip to content

nonoroazoro/opfs-extra

Repository files navigation

opfs-extra

opfs-extra brings the user-friendly API to OPFS, just like fs-extra

NPM License NPM Downloads

Installation

npm i opfs-extra

Usage

import { 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");

API Reference

Initialization

  • open - Opens access to the Origin Private File System.

Storage

  • root - Gets the handle to the root directory of the Origin Private File System.
  • estimate - Estimates storage usage and quota.

Handles

File Reading

  • 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.

File Writing

These methods will create file and parent directories automatically if needed.

Directory

  • 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.

Path

  • exists - Tests whether a file or directory exists.
  • remove - Removes a file or directory recursively.

About

💾 opfs-extra brings the user-friendly API to OPFS, just like fs-extra.

Topics

Resources

License

Stars

Watchers

Forks