![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@zenfs/core
Advanced tools
ZenFS is a file system that emulates the NodeJS 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 of BrowserFS.
ZenFS is modular and extensible. The core includes two 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)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 the FileSystem
class and/or providing a Backend
object.
For more information, see the docs.
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.
import fs from '@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)!');
const contents = fs.readFileSync('/test.txt', 'utf-8');
console.log(contents);
A single InMemory
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 the configure
function.
You can use multiple backends by passing an object to configure
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';
const zipData = await (await fetch('mydata.zip')).arrayBuffer();
await configure({
'/mnt/zip': { backend: Zip, zipData },
'/tmp': InMemory,
'/home': IndexedDB,
};
[!TIP] When configuring a mount point, you can pass in
- A
Backend
object, if the backend has no required options- An object that has the options accepted by the backend and a
backend
property which is aBackend
object- A
FileSystem
instance (not recommended)
Here is an example that mounts the WebStorage
backend from @zenfs/dom
on /
:
import { configure, fs } from '@zenfs/core';
import { WebStorage } from '@zenfs/dom';
await configure({ backend: WebStorage });
if (!fs.existsSync('/test.txt')) {
fs.writeFileSync('/test.txt', 'This will persist across reloads!');
}
const contents = fs.readFileSync('/test.txt', 'utf-8');
console.log(contents);
The FS promises API is exposed as promises
.
import { configure } from '@zenfs/core';
import { exists, writeFile } from '@zenfs/core/promises';
import { IndexedDB } from '@zenfs/dom';
await configure({ '/': IndexedDB });
const exists = await exists('/myfile.txt');
if (!exists) {
await writeFile('/myfile.txt', 'Lots of persistant data');
}
[!NOTE] You can import the promises API using:
- Exports from
@zenfs/core/promises
- The
promises
export from@zenfs/core
fs.promises
on the exportedfs
from@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 calling resolveMountConfig
with it.
You can then mount and unmount the backend instance by using mount
and umount
.
import { configure, resolveMountConfig, InMemory } from '@zenfs/core';
import { IndexedDB } from '@zenfs/dom';
import { Zip } from '@zenfs/zip';
await configure({
'/tmp': InMemory,
'/home': IndexedDB,
};
fs.mkdirSync('/mnt');
const res = await fetch('mydata.zip');
const zipfs = await resolveMountConfig({ backend: Zip, zipData: await res.arrayBuffer() });
fs.mount('/mnt/zip', zipfs);
// do stuff with the mounted zip
fs.umount('/mnt/zip'); // finished using the zip
[!WARNING] Instances of backends follow the internal ZenFS API. You should never use a backend's methods unless you are extending a backend.
ZenFS exports a drop-in for Node's fs
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.
npm install
npm run build
dist
.Run unit tests with npm test
.
FAQs
A filesystem, anywhere
We found that @zenfs/core demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.