Skip to content

Commit

Permalink
Implement MemoryFS for browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
SamyPesse committed Jun 20, 2016
1 parent 8759d2f commit fc8d1f9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -20,14 +20,14 @@ $ npm install gitkit

#### API Basics

State of the Git repository is represented as a single immutable `Repository` object. Read and write access to the repository is done using a `FS` driver, the implementation of the fs depends on the plaftrom (`NodeFS` for Node.js/Native, `LocalStorageFS` or `MemoryFS` for the browser).
State of the Git repository is represented as a single immutable `Repository` object. Read and write access to the repository is done using a `FS` driver, the implementation of the fs depends on the plaftrom (`NativeFS` for Node.js/Native, `LocalStorageFS` or `MemoryFS` for the browser).

```js
var GitKit = require('gitkit');
var NodeFS = require('gitkit/lib/fs/node');
var NativeFS = require('gitkit/lib/fs/native');

// Prepare the filesystem
var fs = NodeFS(process.cwd());
var fs = NativeFS(process.cwd());

// Create a repository instance
var repo = GitKit.Repository.createWithFS(fs, isBare);
Expand Down
12 changes: 12 additions & 0 deletions lib/fs/memory.js
@@ -0,0 +1,12 @@
var MemoryFileSystem = require('memory-fs');
var NodeFS = require('./node');

/**
* Create a in-mmeory filesystem that can be used in both Node.js and the browser
*/
function MemoryFS() {
var fs = new MemoryFileSystem();
return new NodeFS(fs, '/');
};

module.exports = MemoryFS;
4 changes: 4 additions & 0 deletions lib/fs/native.js
@@ -0,0 +1,4 @@
var fs = require('fs');
var NodeFS = require('./node');

module.exports = NodeFS.bind(null, fs);
23 changes: 14 additions & 9 deletions lib/fs/node.js
@@ -1,14 +1,17 @@
var Immutable = require('immutable');
var Promise = require('q');
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var bindAll = require('bind-all');

var File = require('../models/file');

function FS(root) {
if (!(this instanceof FS)) return new FS(root);
function FS(fs, root) {
if (!(this instanceof FS)) {
return new FS(fs, root);
}

this.fs = bindAll(fs);
this.root = root;
}

Expand All @@ -23,7 +26,7 @@ FS.prototype.path = function(filePath) {
* @return {Promise<List<String>>}
*/
FS.prototype.readDir = function readDir(dirpath) {
return Promise.nfcall(fs.readdir, this.path(dirpath))
return Promise.nfcall(this.fs.readdir, this.path(dirpath))
.then(Immutable.List);
};

Expand All @@ -33,7 +36,7 @@ FS.prototype.readDir = function readDir(dirpath) {
* @return {Promise<File>}
*/
FS.prototype.statFile = function statFile(filePath) {
return Promise.nfcall(fs.stat, this.path(filePath))
return Promise.nfcall(this.fs.stat, this.path(filePath))
.then(function(stat) {
return new File({
path: filePath,
Expand All @@ -50,7 +53,7 @@ FS.prototype.statFile = function statFile(filePath) {
* @return {Promise<Buffer>}
*/
FS.prototype.read = function read(filePath) {
return Promise.nfcall(fs.readFile, this.path(filePath));
return Promise.nfcall(this.fs.readFile, this.path(filePath));
};

/**
Expand All @@ -64,7 +67,7 @@ FS.prototype.write = function write(filePath, buf) {

return this.mkdir(path.dirname(filePath))
.then(function() {
return Promise.nfcall(fs.writeFile, that.path(filePath), buf);
return Promise.nfcall(this.fs.writeFile, that.path(filePath), buf);
});
};

Expand All @@ -74,7 +77,7 @@ FS.prototype.write = function write(filePath, buf) {
* @return {Promise}
*/
FS.prototype.unlink = function unlink(filePath) {
return Promise.nfcall(fs.unlink, this.path(filePath));
return Promise.nfcall(this.fs.unlink, this.path(filePath));
};

/**
Expand All @@ -83,7 +86,9 @@ FS.prototype.unlink = function unlink(filePath) {
* @return {Promise}
*/
FS.prototype.mkdir = function mkdir(filePath) {
return Promise.nfcall(mkdirp, this.path(filePath));
return Promise.nfcall(mkdirp, this.path(filePath), {
fs: this.fs
});
};

module.exports = FS;
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -13,6 +13,7 @@
"axios": "^0.9.1",
"bash-color": "0.0.3",
"binary": "^0.3.0",
"bind-all": "^1.0.0",
"buffer-to-uint8array": "^1.1.0",
"commander": "2.9.0",
"concentrate": "^0.2.3",
Expand All @@ -21,6 +22,7 @@
"ignore": "^3.1.1",
"immutable": "^3.7.6",
"is": "^3.1.0",
"memory-fs": "^0.3.0",
"mkdir": "0.0.2",
"pad": "^1.0.0",
"pako": "^1.0.1",
Expand Down

0 comments on commit fc8d1f9

Please sign in to comment.