Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
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.
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'));
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 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,
load Node's modules from memory. Or rewrite the whole fs
filesystem module.
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: () => { /* ... */ },
// etc...
}
fs-monkey
API
patchFs(vol[, fs])
- rewrites Node's filesystem module fs
with fs-like object vol
patchRequire(vol[, Module])
- rewrites require
function, patches Node's module
module to use a give fs-like object vol
for module loadingpatchFs(vol[, fs])
Rewrites Node's filesystem module fs
with fs-like object.
vol
- fs-like objectfs
(optional) - a filesystem to patch, defaults to require('fs')
import {patchFs} from 'fs-monkey';
const myfs = {
readFileSync: () => 'hello world',
};
patchFs(myfs);
console.log(require('fs').readFileSync('/foo/bar')); // hello world
You don't need to create fs-like objects yourself, use memfs
to create a virtual filesystem for you:
import {vol} from 'memfs';
import {patchFs} from 'fs-monkey';
vol.fromJSON({'/dir/foo': 'bar'});
patchFs(vol);
console.log(require('fs').readdirSync('/')); // [ 'dir' ]
patchRequire(vol[, unixifyPaths[, Module]])
Patches Node's module
module to use a given fs-like object vol
for module loading.
vol
- fs-like objectunixifyPaths
(optional) - whether to convert Windows paths to unix style paths, defaults to false
.Module
(optional) - a module to patch, defaults to require('module')
Monkey-patches the require
function in Node, this way you can make
Node.js to require modules from your custom filesystem.
It expects an object with three filesystem methods implemented that are
needed for the require
function to work.
let vol = {
readFileSync: () => {},
realpathSync: () => {},
statSync: () => {},
};
If you want to make Node.js to require your files from memory, you
don't need to implement those functions yourself, just use the
memfs
package:
import {vol} from 'memfs';
import {patchRequire} from 'fs-monkey';
vol.fromJSON({'/foo/bar.js': 'console.log("obi trice");'});
patchRequire(vol);
require('/foo/bar'); // obi trice
Now the require
function will only load the files from the vol
file
system, but not from the actual filesystem on the disk.
If you want the require
function to load modules from both file
systems, use the unionfs
package
to combine both filesystems into a union:
import {vol} from 'memfs';
import {patchRequire} from 'fs-monkey';
import {ufs} from 'unionfs';
import * as fs from 'fs';
vol.fromJSON({'/foo/bar.js': 'console.log("obi trice");'});
ufs
.use(vol)
.use(fs);
patchRequire(ufs);
require('/foo/bar.js'); // obi trice
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to http://unlicense.org/
FAQs
Monkey patches for file system related things.
The npm package fs-monkey receives a total of 5,067,374 weekly downloads. As such, fs-monkey popularity was classified as popular.
We found that fs-monkey demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.