|
| 1 | +# browser-fs |
| 2 | + |
| 3 | +A browser-based filesystem implementation for WebAssembly applications using Rust. This crate provides async filesystem operations similar to `std::fs` and `async-fs`, designed specifically for browser environments. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- 📂 Full filesystem operations (read, write, create, delete) |
| 8 | +- 📁 Directory management (create, list, remove) |
| 9 | +- 🔄 Async I/O support |
| 10 | +- 📊 File metadata handling |
| 11 | +- ⚡ High performance using Web Workers |
| 12 | +- 🛡️ Safe Rust implementation |
| 13 | +- 🌐 Browser-native using File System Access API |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +Add this to your `Cargo.toml`: |
| 18 | + |
| 19 | +```toml |
| 20 | +[dependencies] |
| 21 | +browser-fs = "0.1.0" |
| 22 | +``` |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +### Basic File Operations |
| 27 | + |
| 28 | +```rust |
| 29 | +use browser_fs::File; |
| 30 | +use futures_lite::io::AsyncWriteExt; |
| 31 | + |
| 32 | +// Create and write to a file |
| 33 | +async fn write_example() -> std::io::Result<()> { |
| 34 | + let mut file = File::create("example.txt").await?; |
| 35 | + file.write_all(b"Hello, world!").await?; |
| 36 | + file.flush().await?; |
| 37 | + Ok(()) |
| 38 | +} |
| 39 | + |
| 40 | +// Read from a file |
| 41 | +use futures_lite::io::AsyncReadExt; |
| 42 | + |
| 43 | +async fn read_example() -> std::io::Result<String> { |
| 44 | + let mut file = File::open("example.txt").await?; |
| 45 | + let mut contents = String::new(); |
| 46 | + file.read_to_string(&mut contents).await?; |
| 47 | + Ok(contents) |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +### Directory Operations |
| 52 | + |
| 53 | +```rust |
| 54 | +use browser_fs::{create_dir_all, read_dir}; |
| 55 | +use futures_lite::stream::StreamExt; |
| 56 | + |
| 57 | +async fn directory_example() -> std::io::Result<()> { |
| 58 | + // Create nested directories |
| 59 | + create_dir_all("/my/nested/directory").await?; |
| 60 | + |
| 61 | + // List directory contents |
| 62 | + let mut entries = read_dir("/my/nested").await?; |
| 63 | + while let Some(entry) = entries.try_next().await? { |
| 64 | + println!("{}", entry.file_name().to_string_lossy()); |
| 65 | + } |
| 66 | + Ok(()) |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +### Advanced File Operations |
| 71 | + |
| 72 | +```rust |
| 73 | +use browser_fs::OpenOptions; |
| 74 | + |
| 75 | +async fn advanced_file_example() -> std::io::Result<()> { |
| 76 | + let mut file = OpenOptions::new() |
| 77 | + .read(true) |
| 78 | + .write(true) |
| 79 | + .create(true) |
| 80 | + .open("advanced.txt") |
| 81 | + .await?; |
| 82 | + |
| 83 | + // File operations... |
| 84 | + Ok(()) |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +## Requirements |
| 89 | + |
| 90 | +- WebAssembly environment |
| 91 | +- Modern browser with File System Access API support |
| 92 | +- Must run in a Web Worker context |
| 93 | + |
| 94 | +## Limitations |
| 95 | + |
| 96 | +- Maximum file size of 4GB (due to wasm32 constraints) |
| 97 | +- Must run in a Web Worker (not available on main thread) |
| 98 | +- Browser security restrictions apply |
| 99 | +- Single file handle at a time per file |
| 100 | + |
| 101 | +## Contributing |
| 102 | + |
| 103 | +Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change. |
| 104 | + |
| 105 | +Please make sure to update tests as appropriate. |
| 106 | + |
| 107 | +## Testing |
| 108 | + |
| 109 | +Run the test suite: |
| 110 | + |
| 111 | +```bash |
| 112 | +wasm-pack test --headless --firefox |
| 113 | +``` |
| 114 | + |
| 115 | +## License |
| 116 | + |
| 117 | +MIT License |
| 118 | + |
| 119 | +## Credits |
| 120 | + |
| 121 | +This project is inspired by the excellent work done in: |
| 122 | +- [async-fs](https://docs.rs/async-fs/) |
| 123 | +- [web-fs](https://docs.rs/web-fs/) |
| 124 | + |
| 125 | +## Future Plans |
| 126 | + |
| 127 | +- [ ] Better concurrent file access |
| 128 | +- [ ] Better error messages and handling |
| 129 | + |
| 130 | +## Safety |
| 131 | + |
| 132 | +This crate uses `#![forbid(unsafe_code)]` to ensure 100% safe Rust. |
| 133 | + |
| 134 | +## Performance |
| 135 | + |
| 136 | +The crate is designed to be efficient while working within browser constraints. All operations are asynchronous and use modern browser APIs for optimal performance. |
0 commit comments