@opfs.js/core is a modern JavaScript/TypeScript wrapper for the browser's native OPFS (Origin Private File System), providing a clean, user-friendly, and feature-rich API for working with the browser's private file system.
Install using npm, yarn, or pnpm:
# Using npm
npm install @opfs.js/core
# Using yarn
yarn add @opfs.js/core
# Using pnpm
pnpm add @opfs.js/core
import { file } from "@opfs.js/core";
// Create or open a file
const myFile = file("/documents/report.txt");
await myFile.create();
// Write content to the file
const rw = await myFile.open({ mode: "readwrite" });
await rw.write("Hello, OPFS!");
await rw.flush();
await rw.close();
// Read file content
const fileContent = await myFile.text();
console.log(fileContent); // 'Hello, OPFS!'
// Read as binary data
const buffer = await myFile.arrayBuffer();
// Delete the file
await myFile.remove();
import { dir } from "@opfs.js/core";
// Create or open a directory
const myDir = dir("/documents/work");
await myDir.create();
// List directory contents
const children = await myDir.children();
children.forEach((child) => {
console.log(`${child.kind}: ${child.fullPath}`);
});
// Copy directory
const destDir = dir("/backup");
await destDir.create();
await myDir.copyTo(destDir);
// Delete directory
await myDir.remove();
Every file system object (file or directory) has the following properties:
const myFile = file("/documents/report.txt");
console.log(myFile.fullPath); // '/documents/report.txt'
console.log(myFile.name); // 'report.txt'
console.log(myFile.parents); // ['documents']
console.log(myFile.kind); // 'file' (or 'directory')
import { file } from "@opfs.js/core";
const largeFile = file("/data/large.bin");
await largeFile.create();
// Write large data
const rw = await largeFile.open();
// Write at specific offset
await rw.write(new Uint8Array([1, 2, 3, 4, 5]), { at: 100 });
// Truncate file
await rw.truncate(1024 * 1024); // 1MB
await rw.close();
// Read as stream
const stream = await largeFile.stream();
const reader = stream.getReader();
// Read stream data...
// Check if file exists
const exists = await largeFile.exists();
// Move file
const newLocation = dir("/archive");
await newLocation.create();
await largeFile.moveTo(newLocation);
Full API documentation is available at https://atox996.github.io/opfs/.
create(): Promise<FileSystemFileHandle>
- Creates the fileexists(): Promise<boolean>
- Checks if the file existsremove(): Promise<void>
- Deletes the filecopyTo(dest): Promise<void>
- Copies the file to the destinationmoveTo(dest): Promise<void>
- Moves the file to the destinationopen(options?): Promise<FileRO | FileRW>
- Opens the file to get an access handletext(): Promise<string>
- Reads the file content as textarrayBuffer(): Promise<ArrayBuffer>
- Reads the file content as binary datastream(): Promise<ReadableStream<BufferSource>>
- Gets a readable stream for the filegetFile(): Promise<File | undefined>
- Gets the underlying File objectcreate(): Promise<FileSystemDirectoryHandle>
- Creates the directoryexists(): Promise<boolean>
- Checks if the directory existsremove(): Promise<void>
- Deletes the directory and its contentschildren(): Promise<(OPDir | OPFile)[]>
- Lists the immediate children of the directorycopyTo(dest): Promise<void>
- Copies the directory and its contents to the destinationmoveTo(dest): Promise<void>
- Moves the directory and its contents to the destinationread(size, options?): Promise<ArrayBuffer>
- Reads data from the filegetSize(): Promise<number>
- Gets the file sizeclose(): Promise<void>
- Closes the file handleExtends FileRO with these additional methods:
write(data, options?): Promise<number>
- Writes data to the filetruncate(newSize): Promise<void>
- Truncates the file to the specified sizeflush(): Promise<void>
- Flushes pending writes to storageThis library requires browsers that support:
If you want to contribute to this project, follow these steps:
git clone https://github.com/atox996/opfs.git
cd opfs
pnpm install
pnpm dev
pnpm build
pnpm lint
This project is licensed under the MIT License - see the LICENSE file for details
Contributions are welcome! Please submit Issues and Pull Requests to help improve this project. Before submitting, ensure your code adheres to the project's coding style and quality requirements.
This project is built on top of the browser's native Origin Private File System API, aiming to provide a cleaner, more user-friendly interface for working with the browser's private file system.