Wasm WNFS
This projects implements necessary JavaScript bindings for using the WebNative FileSystem (WNFS) Rust implementation in the browser.
WNFS is a versioned content-addressable distributed filesystem with private and public sub systems. The private filesystem is encrypted so that only users with the right keys can access its contents. It is designed to prevent inferring metadata like the structure of the file tree. The other part of the WNFS filesystem is a simpler public filesystem that is not encrypted and can be accessed by anyone with the right address.
WNFS also features collaborative editing of file trees, where multiple users can edit the same tree at the same time.
WNFS file trees can serialize and be deserialized from IPLD graphs with an extensible metadata section. This allows WNFS to be understood by other IPLD-based tools and systems.
Outline
Setting up the Project
-
Install
wasm-bindgen
-
Install dependencies
-
Install playwright binaries
-
Build project
Usage
WNFS does not have an opinion on where you want to persist your content or the file tree. Instead, the API takes any object that implements the asynchronous BlockStore
trait. The library also avoids including system function calls that could possibly tie it to a set of platforms. Operations like time and random number generation have to be passed in via the API. This allows the library to be used in a wide variety of environments. It particularly makes virtualisation easier.
Let's see an example of working with a public filesystem. We will use a user-provided in-memory block store.
import from "<custom>";
import from "wnfs";
const dir = ;
const store = ;
var = await dir.;
// Create a sample CIDv1.
const cid = Uint8Array.;
// Add a file to /pictures/cats.
var = await rootDir.;
// Create and add a file to /pictures/dogs directory.
var = await rootDir.;
// Delete /pictures/cats directory.
var = await rootDir.;
// List all files in /pictures directory.
var = await rootDir.;
console.log;
You may notice that we use the rootDir
s returned by each operation in subseqent operations. That is because WNFS internal state is immutable and every operation potentially returns a new root directory. This allows us to track and rollback changes when needed. It also makes collaborative editing easier to implement and reason about. There is a basic demo of the filesystem immutability here.
The private filesystem, on the other hand, is a bit more involved. Hash Array Mapped Trie (HAMT) is used as the intermediate format of private file tree before it is persisted to the blockstore. Our use of HAMTs obfuscate the file tree hierarchy.
import from "<custom>";
import from "wnfs";
const initialForest = ;
const rng = ;
const store = ;
const dir = ;
var = await root.;
// Add a file to /pictures/cats.
var = await rootDir.;
// Create and add a file to /pictures/dogs directory.
var = await rootDir.;
// Delete /pictures/cats directory.
var = await rootDir.;
// List all files in /pictures directory.
var = await rootDir.;
console.log;
Testing the Project
-
Run tests
Publishing Package
-
Build the project
-
Publish