index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.memfs = exports.fs = exports.vol = exports.Volume = void 0;
  4. exports.createFsFromVolume = createFsFromVolume;
  5. const fs_node_1 = require("@jsonjoy.com/fs-node");
  6. Object.defineProperty(exports, "Volume", { enumerable: true, get: function () { return fs_node_1.Volume; } });
  7. const fs_node_utils_1 = require("@jsonjoy.com/fs-node-utils");
  8. const { F_OK, R_OK, W_OK, X_OK } = fs_node_utils_1.constants;
  9. // Default volume.
  10. exports.vol = new fs_node_1.Volume();
  11. function createFsFromVolume(vol) {
  12. const fs = { F_OK, R_OK, W_OK, X_OK, constants: fs_node_utils_1.constants, Stats: fs_node_1.Stats, Dirent: fs_node_1.Dirent };
  13. // Bind FS methods.
  14. for (const method of fs_node_1.fsSynchronousApiList)
  15. if (typeof vol[method] === 'function')
  16. fs[method] = vol[method].bind(vol);
  17. for (const method of fs_node_1.fsCallbackApiList)
  18. if (typeof vol[method] === 'function')
  19. fs[method] = vol[method].bind(vol);
  20. fs.StatWatcher = vol.StatWatcher;
  21. fs.FSWatcher = vol.FSWatcher;
  22. fs.WriteStream = vol.WriteStream;
  23. fs.ReadStream = vol.ReadStream;
  24. fs.promises = vol.promises;
  25. // Handle realpath and realpathSync with their .native properties
  26. if (typeof vol.realpath === 'function') {
  27. fs.realpath = vol.realpath.bind(vol);
  28. if (typeof vol.realpath.native === 'function') {
  29. fs.realpath.native = vol.realpath.native.bind(vol);
  30. }
  31. }
  32. if (typeof vol.realpathSync === 'function') {
  33. fs.realpathSync = vol.realpathSync.bind(vol);
  34. if (typeof vol.realpathSync.native === 'function') {
  35. fs.realpathSync.native = vol.realpathSync.native.bind(vol);
  36. }
  37. }
  38. fs._toUnixTimestamp = fs_node_1.toUnixTimestamp;
  39. fs.__vol = vol;
  40. return fs;
  41. }
  42. exports.fs = createFsFromVolume(exports.vol);
  43. /**
  44. * Creates a new file system instance.
  45. *
  46. * @param json File system structure expressed as a JSON object.
  47. * Use `null` for empty directories and empty string for empty files.
  48. * @param cwdOrOpts Current working directory (string) or options object.
  49. * The JSON structure will be created relative to the cwd path.
  50. * @returns A `memfs` file system instance, which is a drop-in replacement for
  51. * the `fs` module.
  52. */
  53. const memfs = (json = {}, cwdOrOpts = '/') => {
  54. const opts = typeof cwdOrOpts === 'string' ? { cwd: cwdOrOpts } : cwdOrOpts;
  55. // When no explicit cwd is given but a custom process is provided, let the
  56. // Superblock use that process's cwd(). Otherwise default to '/' so the
  57. // convenience function keeps its opinionated virtual-root default.
  58. const cwd = opts.cwd ?? (opts.process ? undefined : '/');
  59. const vol = fs_node_1.Volume.fromNestedJSON(json, cwd, { process: opts.process });
  60. const fs = createFsFromVolume(vol);
  61. return { fs, vol };
  62. };
  63. exports.memfs = memfs;
  64. module.exports = { ...module.exports, ...exports.fs };
  65. module.exports.semantic = true;
  66. //# sourceMappingURL=index.js.map