What is fs-monkey?
The fs-monkey package is a utility library for Node.js that allows you to patch or virtualize the Node.js file system (fs) module. This can be particularly useful for testing purposes, where you might want to avoid performing I/O operations on the actual file system. It provides functionalities to patch the global fs module, or to create a virtual file system that can be used independently.
What are fs-monkey's main functionalities?
Patching the global fs module
This feature allows you to patch the global fs module with a virtual file system. In the code sample, a virtual file system is created with a single file '/foo.txt' containing the text 'bar'. The global fs module is then patched, so any subsequent fs operations in the process will interact with this virtual file system instead of the real one.
const {patchFs} = require('fs-monkey');
const {Volume} = require('memfs');
const vol = Volume.fromJSON({'/foo.txt': 'bar'});
patchFs(vol);
Creating a union file system
This feature allows you to create a union file system, which combines multiple file systems into a single coherent file system interface. In the code sample, a virtual file system is created and then combined with the real fs module using the union file system. This allows for fallback to the real file system for files not found in the virtual file system.
const {ufs} = require('fs-monkey');
const {Volume} = require('memfs');
const vol = Volume.fromJSON({'/foo.txt': 'bar'});
ufs.use(vol);
ufs.use(require('fs'));
Other packages similar to fs-monkey
memfs
Similar to fs-monkey, memfs provides an in-memory file system that mimics the Node.js fs module. While fs-monkey offers utilities to patch or virtualize the fs module, memfs focuses more on creating an in-memory file system that can be used directly or with fs-monkey for patching the global fs.
unionfs
Unionfs is another package that is closely related to fs-monkey, specifically to its union file system functionality. It allows you to transparently overlay multiple file systems into one cohesive file system. While fs-monkey provides this functionality through its ufs feature, unionfs is dedicated solely to creating and managing union file systems.
fs-monkey
Monkey-patches for filesystem related things.
- Rewrite
require
function to load Node's modules from memory. - Or rewrite the whole
fs
filesystem module.
Install
npm install --save fs-monkey
Terms
An fs-like object is an object that implements methods of Node's
filesystem API.
It is denoted as vol
:
let vol = {
readFile: () => { },
readFileSync: () => { },
}
Reference
patchFs
- rewrites Node's filesystem module fs
with fs-like object vol
patchRequire
- rewrites require
function, patches Node's module
module to use a given fs-like object for module loading
License
Unlicense - public domain.