
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
MockFS - Mocking FS module implementation for testing purpouses.
Basic idea is to declare file system contents via JSON spec, mount it, and use through real fs.*
functions like an ordinary one.
/**
* /
* file-buffer
* file-base64
* file-string
* file-alt
* dir/
* file-in-dir
*/
var fs = require('fs'),
MockFS = require('mockfs'),
spec, mfs, fd;
spec = {
time: 'Tue May 07 2013 17:09:57 GMT+0400' // global default time for any FS item, optional
ctime: new Date(), // creation time default, optional
/*atime: ..., // atime and ctime is not set
mtime: ...,*/ // value is taken from FS defaults (time)
items: {
'file-buffer': new Buffer('qwerty'), // specify content as Buffer
'file-base64': new Buffer('cXdlcnR5', 'base64'), // buffer with encoding
'file-string': 'qwerty', // or as string
'file-alt': { // alternative syntax
uid: 'johndoe', // owner user, as login name or id
gid: 300, // owner group
mode: 0766, // access mode
atime: new Date(), // Date instance
mtime: 1000255364, // timestamp
ctime: "-500" // number with a sign (+/-) - delta from fs default value
content: 'asobject' // file content
},
'dir': { // directory - always an object with items property (which is object too)
atime: 'Tue May 07 2013 17:09:57 GMT+0400' // Date as string
mtime: "+500", // stats, uid, gid, mode - on directories too
/*ctime*/ // ctime is not set, value taken from FS defaults
items: { // directory contents
'file-in-dir': 'inside directory'
}
}
};
mfs = new MockFS(spec);
mfs.mount('/mnt/mock');
fs.existsSync('/mnt/mock/file-buffer'); // true
fs.readFileSync('/mnt/mock/file-string').toString(); // "qwerty"
fs.readFile('/mnt/mock/dir/file-in-dir', function(e, r){
Buffer.isBuffer(r); // true
r.toString(); // "inside directory"
});
// file descriptors is also works
fs.open('/mnt/mock/file-base64', 'r', function(e, fd){
if(fd) {
var buf = new Buffer(100);
fs.read(fd, buf, 0, 100, null, function(e, bytesRead){
console.log(bytesRead); // 6
console.log(buf.toString('utf8', 0, bytesRead)); // qwerty
fs.closeSync(fd);
});
}
});
mfs.umount('/mnt/mock');
fs.existsSync('/mnt/mock/file-buffer'); // false
Implemented by wrapping bundled fs
module's basic functions (file descriptors handling, stat, rename/delete files/directories).
So, functions as createReadStream
, appendFileSync
and so are supported "out of the box" without any wrapping.
Currently, NodeJS v0.8+ is supported.
mirror
utility to create MockFS specs from real file systemsFAQs
Mocking FS module implementation for testing purpouses
We found that mockfs demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.