You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.4 KiB

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