A simple, tiny, in-memory mock file system for all environments.
Installations
npm install micro-memfs
Developer Guide
Typescript
Yes
Module System
ESM
Node Version
18.12.1
NPM Version
8.16.0
Score
72.5
Supply Chain
89.5
Quality
75.7
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (98.56%)
JavaScript (1.44%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
awphi
Download Statistics
Total Downloads
1,079
Last Day
2
Last Week
3
Last Month
14
Last Year
99
GitHub Statistics
2 Stars
22 Commits
1 Watching
1 Branches
1 Contributors
Bundle Size
2.05 kB
Minified
967.00 B
Minified + Gzipped
Package Meta Information
Latest Version
1.2.0
Package Id
micro-memfs@1.2.0
Unpacked Size
24.97 kB
Size
6.87 kB
File Count
13
NPM Version
8.16.0
Node Version
18.12.1
Publised On
14 Feb 2023
Total Downloads
Cumulative downloads
Total Downloads
1,079
Last day
0%
2
Compared to previous day
Last week
-57.1%
3
Compared to previous week
Last month
0%
14
Compared to previous month
Last year
-89.9%
99
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
micro-memfs
micro-memfs
is a tiny in-memory "file system" for use in a simple applications that need a mocked file system i.e. unit testing, web apps or games.
- Small 860 bytes (minified + gzipped in empty esbuild project). Size Limit controls the size.
- Self-contained No dependencies & all utilities are managed internally (i.e. no reliance on
process.cwd()
) - Simple For quick, small fake file systems there's no need for a fully mocked test-focused library weighing multiple kilobytes.
- Flexible Supports "executables" to store/run JS and standard
path
-like path resolution to support absolute and relative paths.
API
The mock file system is made up of directories, files and "executables".
Files map a path to some text. This text can encode whatever you'd like. A file must have an extension - e.g. directory/file.txt
.
Executables are sync/async functions that will return some output. These are analogous to regular old binaries and thus their source code can be read with readFile()
too.
1type CommandFunc = (args: string[]) => string[] | Promise<string[]>; 2 3interface MicroFs { 4 /** Check if a path exists in the file system. */ 5 exists(path: string): boolean; 6 /** Check if a given path is a directory or a file/doesn't exist. This method also ensures the path exists. */ 7 isDirectory(path: string): boolean; 8 /** Read the text content of a file in the file system. */ 9 readFile(pth: string): string | undefined; 10 /** Read the contents of a directory in the file system. */ 11 readDir(pth: string): string[]; 12 /** Get or set the cwd (current working directory of this file system) */ 13 cwd(pth?: string): string; 14 /** 15 * Find an "executable" in the file system returning a callable function if found. 16 * Use this to prevent your linter going crazy if you try to `eval()` the output from `readFile() ` 17 * */ 18 findExecutable(prog: string): CommandFunc | null; 19}
Example
1import microfs from 'micro-memfs'; 2 3const fs = microfs({ 4 "usr/file.txt": "Some text content", 5}, 6{ 7 "usr/emphasize": (args) => args.concat(["!"]), 8}); 9 10fs.cwd() // '/' 11fs.exists('usr') // true 12fs.isDirectory('usr') // true 13fs.cwd('usr') // '/usr' 14fs.readFile('file.txt') // 'Some text content' 15fs.readFile('emphasize') // '(args) => args.concat(["!"])' 16const emphasize = fs.findExecutable("emphasize"); 17emphasize(['hello', 'world']) // -> ['hello', 'world'] 18fs.cwd('another-dir') // will throw an `Error` as /usr/another-dir doesn't exist
TODO
- Support for empty directories
- File/directory addition/deletion after instantiation of the file system
- Extend tests to cover all methods
Licenses
- nanopath: MIT © Fabio Spampinato
Contributions
Contributions are always welcome! Please submit them on GitHub :)
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No security vulnerabilities found.