1 | # memfs
|
2 |
|
3 | [![][chat-badge]][chat] [![][npm-badge]][npm-url] [![][travis-badge]][travis-url]
|
4 |
|
5 | In-memory file-system with [Node's `fs` API](https://nodejs.org/api/fs.html).
|
6 |
|
7 | - Node's `fs` API implemented, see [_API Status_](./docs/api-status.md)
|
8 | - Stores files in memory, in `Buffer`s
|
9 | - Throws sameish\* errors as Node.js
|
10 | - Has concept of _i-nodes_
|
11 | - Implements _hard links_
|
12 | - Implements _soft links_ (aka symlinks, symbolic links)
|
13 | - Permissions may\* be implemented in the future
|
14 | - Can be used in browser, see [`memfs-webpack`](https://github.com/streamich/memfs-webpack)
|
15 |
|
16 | ### Install
|
17 |
|
18 | ```shell
|
19 | npm install --save memfs
|
20 | ```
|
21 |
|
22 | ## Usage
|
23 |
|
24 | ```js
|
25 | import { fs } from 'memfs';
|
26 |
|
27 | fs.writeFileSync('/hello.txt', 'World!');
|
28 | fs.readFileSync('/hello.txt', 'utf8'); // World!
|
29 | ```
|
30 |
|
31 | Create a file system from a plain JSON:
|
32 |
|
33 | ```js
|
34 | import { fs, vol } from 'memfs';
|
35 |
|
36 | const json = {
|
37 | './README.md': '1',
|
38 | './src/index.js': '2',
|
39 | './node_modules/debug/index.js': '3',
|
40 | };
|
41 | vol.fromJSON(json, '/app');
|
42 |
|
43 | fs.readFileSync('/app/README.md', 'utf8'); // 1
|
44 | vol.readFileSync('/app/src/index.js', 'utf8'); // 2
|
45 | ```
|
46 |
|
47 | Export to JSON:
|
48 |
|
49 | ```js
|
50 | vol.writeFileSync('/script.sh', 'sudo rm -rf *');
|
51 | vol.toJSON(); // {"/script.sh": "sudo rm -rf *"}
|
52 | ```
|
53 |
|
54 | Use it for testing:
|
55 |
|
56 | ```js
|
57 | vol.writeFileSync('/foo', 'bar');
|
58 | expect(vol.toJSON()).toEqual({ '/foo': 'bar' });
|
59 | ```
|
60 |
|
61 | Create as many filesystem volumes as you need:
|
62 |
|
63 | ```js
|
64 | import { Volume } from 'memfs';
|
65 |
|
66 | const vol = Volume.fromJSON({ '/foo': 'bar' });
|
67 | vol.readFileSync('/foo'); // bar
|
68 |
|
69 | const vol2 = Volume.fromJSON({ '/foo': 'bar 2' });
|
70 | vol2.readFileSync('/foo'); // bar 2
|
71 | ```
|
72 |
|
73 | Use `memfs` together with [`unionfs`][unionfs] to create one filesystem
|
74 | from your in-memory volumes and the real disk filesystem:
|
75 |
|
76 | ```js
|
77 | import * as fs from 'fs';
|
78 | import { ufs } from 'unionfs';
|
79 |
|
80 | ufs.use(fs).use(vol);
|
81 |
|
82 | ufs.readFileSync('/foo'); // bar
|
83 | ```
|
84 |
|
85 | Use [`fs-monkey`][fs-monkey] to monkey-patch Node's `require` function:
|
86 |
|
87 | ```js
|
88 | import { patchRequire } from 'fs-monkey';
|
89 |
|
90 | vol.writeFileSync('/index.js', 'console.log("hi world")');
|
91 | patchRequire(vol);
|
92 | require('/index'); // hi world
|
93 | ```
|
94 |
|
95 | ## Docs
|
96 |
|
97 | - [Reference](./docs/reference.md)
|
98 | - [Relative paths](./docs/relative-paths.md)
|
99 | - [API status](./docs/api-status.md)
|
100 | - [Dependencies](./docs/dependencies.md)
|
101 |
|
102 | ## See also
|
103 |
|
104 | - [`spyfs`][spyfs] - spies on filesystem actions
|
105 | - [`unionfs`][unionfs] - creates a union of multiple filesystem volumes
|
106 | - [`linkfs`][linkfs] - redirects filesystem paths
|
107 | - [`fs-monkey`][fs-monkey] - monkey-patches Node's `fs` module and `require` function
|
108 | - [`libfs`](https://github.com/streamich/full-js/blob/master/src/lib/fs.ts) - real filesystem (that executes UNIX system calls) implemented in JavaScript
|
109 |
|
110 | [chat]: https://onp4.com/@vadim/~memfs
|
111 | [chat-badge]: https://img.shields.io/badge/Chat-%F0%9F%92%AC-green?style=flat&logo=chat&link=https://onp4.com/@vadim/~memfs
|
112 | [npm-url]: https://www.npmjs.com/package/memfs
|
113 | [npm-badge]: https://img.shields.io/npm/v/memfs.svg
|
114 | [travis-url]: https://travis-ci.org/streamich/memfs
|
115 | [travis-badge]: https://travis-ci.org/streamich/memfs.svg?branch=master
|
116 | [memfs]: https://github.com/streamich/memfs
|
117 | [unionfs]: https://github.com/streamich/unionfs
|
118 | [linkfs]: https://github.com/streamich/linkfs
|
119 | [spyfs]: https://github.com/streamich/spyfs
|
120 | [fs-monkey]: https://github.com/streamich/fs-monkey
|
121 |
|
122 | ## License
|
123 |
|
124 | [Unlicense](./LICENSE) - public domain.
|