- Notifications
You must be signed in to change notification settings - Fork0
trasherdk/zen-fs-core
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
ZenFS is a file system that emulates theNodeJS filesystem API.
It works using a system of backends, which are used by ZenFS to store and retrieve data. ZenFS can also integrate with other tools.
ZenFS is a fork ofBrowserFS. If you are using ZenFS in a research paper, you may want tocite BrowserFS.
ZenFS is modular and extensible. The core includes some built-in backends:
InMemory: Stores files in-memory. This is cleared when the runtime ends (e.g. a user navigating away from a web page or a Node process exiting)Overlay: Use read-only file system as read-write by overlaying a writable file system on top of it. (copy-on-write)Fetch: Downloads files over HTTP with thefetchAPI (readonly)Port: Interacts with a remote over aMessagePort-like interface (e.g. a worker)
ZenFS supports a number of other backends. Many are provided as separate packages under@zenfs. More backends can be defined by separate libraries by extending theFileSystem class and providing aBackend object.
As an added bonus, all ZenFS backends support syncrohnous operations. All of the backends included with the core are cross-platform.
For more information, see thedocs.
npm install @zenfs/core
Note
The examples are written in ESM.
If you are using CJS, you canrequire the package.
If using a browser environment without support fortype=module inscript tags, you can add ascript tag to your HTML pointing to thebrowser.min.js and use ZenFS with the globalZenFS object.
importfsfrom'@zenfs/core';// You can also use the named export, `fs`fs.writeFileSync('/test.txt','Cool, I can do this in any JS environment (including browsers)!');constcontents=fs.readFileSync('/test.txt','utf-8');console.log(contents);
A singleInMemory backend is created by default, mounted on/.
You can configure ZenFS to use a different backend and mount multiple backends. It is strongly recommended to do so using theconfigure function.
You can use multiple backends by passing an object toconfigure which maps paths to file systems.
The following example mounts a zip file to/zip, in-memory storage to/tmp, and IndexedDB to/home. Note that/ has the default in-memory backend.
import{configure,InMemory}from'@zenfs/core';import{IndexedDB}from'@zenfs/dom';import{Zip}from'@zenfs/zip';constres=awaitfetch('mydata.zip');awaitconfigure({mounts:{'/mnt/zip':{backend:Zip,data:awaitres.arrayBuffer()},'/tmp':InMemory,'/home':IndexedDB,}};
Tip
When configuring a mount point, you can pass in
- A
Backendobject, if the backend has no required options - An object that has the options accepted by the backend and a
backendproperty which is aBackendobject - A
FileSysteminstance
Here is an example that mounts theWebStorage backend from@zenfs/dom on/:
import{configureSingle,fs}from'@zenfs/core';import{WebStorage}from'@zenfs/dom';awaitconfigureSingle({backend:WebStorage});if(!fs.existsSync('/test.txt')){fs.writeFileSync('/test.txt','This will persist across reloads!');}constcontents=fs.readFileSync('/test.txt','utf-8');console.log(contents);
The FS promises API is exposed aspromises.
import{configureSingle}from'@zenfs/core';import{exists,writeFile}from'@zenfs/core/promises';import{IndexedDB}from'@zenfs/dom';awaitconfigureSingle({backend:IndexedDB});constexists=awaitexists('/myfile.txt');if(!exists){awaitwriteFile('/myfile.txt','Lots of persistant data');}
Note
You can import the promises API using:
- Exports from
@zenfs/core/promises - The
promisesexport from@zenfs/core fs.promiseson the exportedfsfrom@zenfs/core.
If you would like to create backends without configure (e.g. to do something dynamic at runtime), you may do so by importing the backend and callingresolveMountConfig with it.
You can then mount and unmount the backend instance by usingmount andumount.
import{configure,resolveMountConfig,InMemory}from'@zenfs/core';import{IndexedDB}from'@zenfs/dom';import{Zip}from'@zenfs/zip';awaitconfigure({mounts:{'/tmp':InMemory,'/home':IndexedDB,},});fs.mkdirSync('/mnt');constres=awaitfetch('mydata.zip');constzipfs=awaitresolveMountConfig({backend:Zip,data:awaitres.arrayBuffer()});fs.mount('/mnt/zip',zipfs);// do stuff with the mounted zipfs.umount('/mnt/zip');// finished using the zip
Warning
Instances of backends follow theinternal ZenFS API. You should never use a backend's methods unless you are extending a backend.
ZenFS exports a drop-in for Node'sfs module (up to the version of@types/node in package.json), so you can use it for your bundler of preference using the default export.
- Make sure you have Node and NPM installed. You must have Node v18 or newer.
- Install dependencies with
npm install - Build using
npm run build - You can find the built code in
dist.
Run unit tests withnpm test.
About
A filesystem, anywhere
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Languages
- TypeScript97.8%
- JavaScript2.2%