About
RenFS provides bindings for ZenFS in Rust, delegating to browser APIs like:
- localStorage,
- IndexedDB, and
- OPFS.
For convenience, std::fs is used on native platforms and works out-of-the-box on the web with webfs.
Usage
Please read the examples for guidance.
Install ZenFS
RenFS has a CLI to automatically install a bundled version of ZenFS directly into a project:
Otherwise, you can download it manually from https://github.com/egakareta/renfs_vendor.
Configure imports
In your HTML file:
{
"imports": {
"@zenfs/core": "./zenfs.js",
"@zenfs/dom": "./zenfs.js"
}
}
Configure a browser filesystem
For most use cases, configure ZenFS before Rust application initialization.
Trunk makes this easy with its data-initializer attribute:
where ./init.mjs looks like:
import from "@zenfs/core";
import from "@zenfs/dom";
await ;
export default ;
When you just need something simple
You can just not set up ZenFS. If the webfs feature is enabled (it is by default), RenFS can
run on the web without any additional setup with localStorage.
The obvious tradeoffs are that localStorage is not a very generous API in terms of storage size and that a lot of advanced filesystem APIs such as symlinking or file stats will error or no-op.
Q & A
Crates such as opfs, rexie, idb are locked into one browser API and call it a day. For a long time, this was the tradeoff developers had to make if they wanted to persist code in browsers.
RenFS delegates to ZenFS, one of the most versatile and powerful filesystem solutions available in browsers.
For synchronous APIs of some browser backends like IndexedDB and OPFS, no. If you write to a file then immediately refresh the browser window, there is no guarantee that your data has been saved. This is a fundamental limitation of ZenFS.
If you are using localStorage, you should be fine but this is subject to browser policy.
If you want durable operations, consider using asynchronous APIs such as write_file instead
of write_file_sync.
License
RenFS is licensed under the MIT License.
ZenFS is licensed under the LGPL 3.0 or later and COPYING.md. Copyright © James Prevett and other ZenFS contributors.