| Crates.io | opfs |
| lib.rs | opfs |
| version | 0.1.4 |
| created_at | 2025-06-07 02:30:10.382844+00 |
| updated_at | 2025-08-29 01:40:10.843645+00 |
| description | A Rust implementation of the Origin Private File System browser API. |
| homepage | |
| repository | https://github.com/anchpop/opfs.git |
| max_upload_size | |
| id | 1703669 |
| size | 87,815 |
Rust wrapper for the the Origin Private File System browser API. (This is an API that gives webapps limited access to the native file system.)
This library mostly exists because using the OPFS from Rust is very painful. As a bonus, it also gives you support for native platforms for free - when compiling to native platforms, it will use tokio::fs instead of browser APIs.
This crate provides an API for file system operations that automatically uses the appropriate implementation based on the target platform:
tokio::fsAn in-memory filesystem is also provided for use in tests (or when persistence isn't necessary)
cargo add opfs
use opfs::persistent::{DirectoryHandle, FileHandle, WritableFileStream, app_specific_dir};
use opfs::{GetFileHandleOptions, CreateWritableOptions};
use opfs::persistent;
// you must import the traits to call methods on the types
use opfs::{DirectoryHandle as _, FileHandle as _, WritableFileStream as _};
// This code works on both native and web platforms
async fn example(dir: DirectoryHandle) -> persistent::Result<()> {
let options = GetFileHandleOptions { create: true };
let mut file = dir.get_file_handle_with_options("example.txt", &options).await?;
let write_options = CreateWritableOptions { keep_existing_data: false };
let mut writer = file.create_writable_with_options(&write_options).await?;
writer.write_at_cursor_pos(b"Hello, world!".to_vec()).await?;
writer.close().await?;
let data = file.read().await?;
println!("File contents: {:?}", String::from_utf8(data));
Ok(())
}
async fn use_example() -> persistent::Result<()> {
let directory: DirectoryHandle = app_specific_dir().await?;
example(directory).await?;
Ok(())
}