volume-localstorage.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ObjectStore = void 0;
  4. exports.createVolume = createVolume;
  5. const volume_1 = require("./volume");
  6. const node_1 = require("./node");
  7. class ObjectStore {
  8. constructor(obj) {
  9. this.obj = obj;
  10. }
  11. setItem(key, json) {
  12. this.obj[key] = JSON.stringify(json);
  13. }
  14. getItem(key) {
  15. const data = this.obj[key];
  16. if (typeof data === void 0)
  17. return void 0;
  18. return JSON.parse(data);
  19. }
  20. removeItem(key) {
  21. delete this.obj[key];
  22. }
  23. }
  24. exports.ObjectStore = ObjectStore;
  25. function createVolume(namespace, LS = localStorage) {
  26. const store = new ObjectStore(LS);
  27. const key = (type, id) => `memfs.${namespace}.${type}.${id}`;
  28. class NodeLocalStorage extends node_1.Node {
  29. get Key() {
  30. if (!this._key)
  31. this._key = key('ino', this.ino);
  32. return this._key;
  33. }
  34. sync() {
  35. store.setItem(this.Key, this.toJSON());
  36. }
  37. touch() {
  38. super.touch();
  39. this.sync();
  40. }
  41. del() {
  42. super.del();
  43. store.removeItem(this.Key);
  44. }
  45. }
  46. class LinkLocalStorage extends node_1.Link {
  47. get Key() {
  48. if (!this._key)
  49. this._key = key('link', this.getPath());
  50. return this._key;
  51. }
  52. sync() {
  53. store.setItem(this.Key, this.toJSON());
  54. }
  55. }
  56. return class VolumeLocalStorage extends volume_1.Volume {
  57. constructor() {
  58. super({
  59. Node: NodeLocalStorage,
  60. Link: LinkLocalStorage,
  61. });
  62. }
  63. createLink(parent, name, isDirectory, perm) {
  64. const link = super.createLink(parent, name, isDirectory, perm);
  65. store.setItem(key('link', link.getPath()), link.toJSON());
  66. return link;
  67. }
  68. deleteLink(link) {
  69. store.removeItem(key('link', link.getPath()));
  70. return super.deleteLink(link);
  71. }
  72. };
  73. }
  74. //# sourceMappingURL=volume-localstorage.js.map