0.1.4 • Published 2 years ago

@tbbjs/vfs v0.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@tbbjs/vfs

GitHub license PRs Welcome Downloads npm version Discord

A virtual file system(vfs) for a fully browser-enabled, multi-process (Worker) environment.

The bottom layer is all based on SharedArrayBuffer and can be used in Worker. Other worker environments can create a new vfs instance by passing the parent buffer.

Installation

$ npm install @tbbjs/vfs

Usage

Mounting

import { FS } from '@tbbjs/vfs';

const vfs = new FS({files_struct: {}, fs_struct: {}});
vfs.writeFile("/test.txt", new TextEncoder().encode("Hello World!"));

const buffer = vfs.readFile("/test.txt");
console.log("/test.txt:", new TextDecoder().decode(buffer));
// Will out:
// /test.txt: Hello World!

vfs.load({
    '/bin/node': {
        text: 'function main(args) { console.log("Hello World!"); }'
    },
})
console.log(JSON.stringify(vfs.dump(), null, 2))
// Will out:
// {
//  "/test.txt": "Hello World!",
//  "/bin/node": "function main(args) { console.log(\"Hello World!\"); }"
// }

Cross Environment(Main-Worker)

Data sharing based on SharedArrayBuffer;

Main(/main.js)

import { FS } from '@tbbjs/vfs';

const vfs = new FS({files_struct: {}, fs_struct: {}});

fs.writeFile("/test.txt", new TextEncoder().encode("Hello World!"));
vfs.load({
    '/bin/node': {
        text: 'function main(args) { console.log("Hello World!"); }'
    },
})

const worker = new Worker('/worker.js');
worker.postMessage({
    type: 'mount',
    data: {
        buffer: vfs.fs_buffer,
        extra: vfs.fs_extra
    },
});

Worker(/worker.js)

import { FS } from '@tbbjs/vfs';

self.onmessage = (event) => {
    const message = event.data;
    if (message.type === 'mount') {
        const vfs = new FS({
            files_struct: {},
            fs_struct: {
                buffer: message.data.buffer,
                extra: message.data.extra
            },
        });
        const buffer = vfs.readFile("/test.txt");
        console.log("/test.txt:", new TextDecoder().decode(buffer));
        // Will out:
        // /test.txt: Hello World!
        console.log(JSON.stringify(vfs.dump(), null, 2));
        // Will out:
        // {
        //  "/test.txt": "Hello World!",
        //  "/bin/node": "function main(args) { console.log(\"Hello World!\"); }"
        // }
    }
}

Benchmark

pnpx esno benchmark-files

# example
pnpx esno benchmark/file-write.ts

benchmark

License

MIT

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.0.1

2 years ago

0.1.0

2 years ago