Skip to content

Commit

Permalink
feat: started localstorage persistance
Browse files Browse the repository at this point in the history
  • Loading branch information
teagrinder committed Sep 5, 2017
1 parent 5effd95 commit 4e22286
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 8 deletions.
17 changes: 14 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@
"description": "In-memory file-system with Node's fs API.",
"main": "lib/index.js",
"keywords": [
"fs", "filesystem", "fs.js", "memory-fs", "memfs",
"file", "file system", "mount", "memory", "in-memory",
"virtual", "test", "testing", "mock"
"fs",
"filesystem",
"fs.js",
"memory-fs",
"memfs",
"file",
"file system",
"mount",
"memory",
"in-memory",
"virtual",
"test",
"testing",
"mock"
],
"repository": {
"type": "git",
Expand Down
20 changes: 20 additions & 0 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,26 @@ export class Node extends EventEmitter {

return false;
}

del() {
this.emit('delete', this);
}

toJSON() {
return {
ino: this.ino,
uid: this.uid,
gid: this.gid,
atime: this.atime.getTime(),
mtime: this.mtime.getTime(),
ctime: this.ctime.getTime(),
perm: this.perm,
mode: this.mode,
nlink: this.nlink,
symlink: this.symlink,
data: this.getString(),
};
}
}


Expand Down
78 changes: 78 additions & 0 deletions src/volume-localstorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {Volume} from "./volume";
import {Link, Node} from "./node";

const LS = {};

export class NodeLocalstorage extends Node {
key() {
return `memfs.ino.${this.ino}`;
}

sync() {
LS[this.key()] = this.toJSON();
}

touch() {
super.touch();
this.sync();
}

del() {
delete LS[this.key()];
}
}

export class LinkLocalstorage extends Link {

}

export class VolumeLocalstorage extends Volume {
constructor() {
super({
Node: NodeLocalstorage,
Link: LinkLocalstorage,
});


}
}

export function createVolume(namespace: string, LS = localStorage) {

const key = (type, id) => `memfs.${namespace}.${type}.${id}`;

export class NodeLocalstorage extends Node {
key() {
return key('ino', this.ino);
}

sync() {
LS[this.key()] = this.toJSON();
}

touch() {
super.touch();
this.sync();
}

del() {
super.del();
delete LS[this.key()];
}
}

export class LinkLocalstorage extends Link {

}

export class VolumeLocalstorage extends Volume {
constructor() {
super({
Node: NodeLocalstorage,
Link: LinkLocalstorage,
});


}
}
}
19 changes: 14 additions & 5 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,16 @@ export class Volume {
WriteStream: new (...args) => IWriteStream;
FSWatcher: new () => FSWatcher;

constructor() {
const root = new Link(this, null, '');
props: {
Node: new (...args) => Node,
Link: new (...args) => Link,
File: new (...File) => File,
};

constructor(props = {}) {
this.props = extend(props, {Node, Link, File});

const root = new this.props.Link(this, null, '');
root.setNode(this.createNode(true));

const self = this;
Expand Down Expand Up @@ -612,7 +620,7 @@ export class Volume {
}

createNode(isDirectory: boolean = false, perm?: number): Node {
const node = new Node(this.newInoNumber(), perm);
const node = new this.props.Node(this.newInoNumber(), perm);
if(isDirectory) node.setIsDirectory();
this.inodes[node.ino] = node;
return node;
Expand All @@ -623,6 +631,7 @@ export class Volume {
}

private deleteNode(node: Node) {
node.del();
delete this.inodes[node.ino];
this.releasedInos.push(node.ino);
}
Expand Down Expand Up @@ -815,7 +824,7 @@ export class Volume {
this.releasedFds = [];
this.openFiles = 0;

this.root = new Link(this, null, '');
this.root = new this.props.Link(this, null, '');
this.root.setNode(this.createNode(true));
}

Expand Down Expand Up @@ -848,7 +857,7 @@ export class Volume {

}

const file = new File(link, node, flagsNum, this.newFdNumber());
const file = new this.props.File(link, node, flagsNum, this.newFdNumber());
this.fds[file.fd] = file;
this.openFiles++;

Expand Down

0 comments on commit 4e22286

Please sign in to comment.