Skip to content
UniKVS
Esc
↑↓navigate↵open⌘Jpreview
On this page

@unikvs/opfs

How to use Opfs in @unikvs/opfs, a storage that keeps bytes in OPFS.

Overview

@unikvs/opfs Browser only is a storage that keeps bytes in the Origin Private File System. Use it to persist files in the browser.

  • name is Opfs.
  • Requires open. No close.
  • Specify the root directory in the constructor.
  • This storage validates keys as file names.

Install with:

npm install @unikvs/opfs
pnpm add @unikvs/opfs
yarn add @unikvs/opfs
bun add @unikvs/opfs
nub add @unikvs/opfs
aube add @unikvs/opfs

Usage

Opfs implements IStorage. Pass it to UniKvs with persist().

import { Opfs } from "@unikvs/opfs";

Constructor arguments:

PropType
root?string | FileSystemDirectoryHandle

Root directory for data. Pass a name as a string or a directory handle directly.

Typestring | FileSystemDirectoryHandle
Default".unikvs"

The following example stores bytes and reads them back.

import { Opfs } from "@unikvs/opfs";
import * as unikvs from "unikvs";

const kvs = new unikvs.Builder<{
  image: unikvs.Value<Uint8Array>;
}>()
  .persist(new Opfs())
  .build();

await kvs.open();
await kvs.set("image", new Uint8Array([0, 1, 2]));

const bytes = await kvs.get("image");
console.log(bytes); // Uint8Array(3) [0, 1, 2]

await kvs.close();

Data types

Operation Supported data type
write/read Uint8Array<ArrayBuffer>
getWritable/getReadable chunks Uint8Array<ArrayBuffer>
  • Stores bytes only.
  • This storage validates keys as file names. Invalid keys throw InvalidFilenameError.
  • Streams from getWritable write directly to the OPFS write stream.
  • Streams from getReadable return the file’s read stream.

Errors

This package has no package-specific errors. Invalid keys throw InvalidFilenameError from @unikvs/utils. Reading a missing key throws a NotFoundError DOMException. Operating before opening throws a TypeError.

Was this page helpful?