Code Monkey home page Code Monkey logo

memfs's Introduction

memfs

In-memory file-system with Node's fs API.

  • Node's fs API implemented, see API Status
  • Stores files in memory, in Buffers
  • Throws sameish* errors as Node.js
  • Has concept of i-nodes
  • Implements hard links
  • Implements soft links (aka symlinks, symbolic links)
  • Permissions may* be implemented in the future
  • Can be used in browser, see memfs-webpack

Install

npm install --save memfs

Usage

import { fs } from 'memfs';

fs.writeFileSync('/hello.txt', 'World!');
fs.readFileSync('/hello.txt', 'utf8'); // World!

Create a file system from a plain JSON:

import { fs, vol } from 'memfs';

const json = {
  './README.md': '1',
  './src/index.js': '2',
  './node_modules/debug/index.js': '3',
};
vol.fromJSON(json, '/app');

fs.readFileSync('/app/README.md', 'utf8'); // 1
vol.readFileSync('/app/src/index.js', 'utf8'); // 2

Export to JSON:

vol.writeFileSync('/script.sh', 'sudo rm -rf *');
vol.toJSON(); // {"/script.sh": "sudo rm -rf *"}

Use it for testing:

vol.writeFileSync('/foo', 'bar');
expect(vol.toJSON()).toEqual({ '/foo': 'bar' });

Create as many filesystem volumes as you need:

import { Volume } from 'memfs';

const vol = Volume.fromJSON({ '/foo': 'bar' });
vol.readFileSync('/foo'); // bar

const vol2 = Volume.fromJSON({ '/foo': 'bar 2' });
vol2.readFileSync('/foo'); // bar 2

Use memfs together with unionfs to create one filesystem from your in-memory volumes and the real disk filesystem:

import * as fs from 'fs';
import { ufs } from 'unionfs';

ufs.use(fs).use(vol);

ufs.readFileSync('/foo'); // bar

Use fs-monkey to monkey-patch Node's require function:

import { patchRequire } from 'fs-monkey';

vol.writeFileSync('/index.js', 'console.log("hi world")');
patchRequire(vol);
require('/index'); // hi world

Docs

See also

  • spyfs - spies on filesystem actions
  • unionfs - creates a union of multiple filesystem volumes
  • linkfs - redirects filesystem paths
  • fs-monkey - monkey-patches Node's fs module and require function
  • libfs - real filesystem (that executes UNIX system calls) implemented in JavaScript

License

Unlicense - public domain.

memfs's People

Contributors

ajafff avatar corwin-of-amber avatar dependabot[bot] avatar fearthecowboy avatar florianloch avatar g-rath avatar imsnif avatar jiacheng9 avatar jonathantneal avatar kirilknysh avatar opichals avatar pizzafroide avatar reinaldoarrosi avatar renovate-bot avatar renovate[bot] avatar saboya avatar sapegin avatar semantic-release-bot avatar sodatea avatar streamich avatar teagrinder avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.