journaling-hash
Advanced tools
Comparing version 0.0.2 to 0.0.3
55
index.js
@@ -6,37 +6,7 @@ var ImmutableHash = require('immutable-hash'); | ||
var lineReader = function(){ | ||
var buffer = ''; | ||
return through2(function(chunk, enc, cb){ | ||
buffer += chunk.toString(); | ||
var lineReader = require('through2-linereader'); | ||
var lineWriter = require('through2-linewriter'); | ||
var JSONReader = require('through2-jsonreader'); | ||
var JSONWriter = require('through2-jsonwriter'); | ||
var lines = buffer.split('\n'); | ||
buffer = lines.pop(); | ||
var self = this; | ||
lines.forEach(function(line){ | ||
self.push(line); | ||
}); | ||
cb(); | ||
}, function(cb){ | ||
this.push(buffer); | ||
cb(); | ||
}); | ||
}; | ||
var JSONReader = function(){ | ||
return through2.obj(function(chunk, enc, cb){ | ||
chunk = chunk.toString(); | ||
var err = null; | ||
try { | ||
chunk = JSON.parse(chunk); | ||
} catch (error) { | ||
err = error; | ||
chunk = null; | ||
} | ||
cb(err, chunk); | ||
}); | ||
}; | ||
var readJournal = function(journalPath, callback){ | ||
@@ -50,3 +20,3 @@ | ||
}) | ||
.pipe(lineReader()) | ||
.pipe(lineReader(true)) | ||
.pipe(JSONReader()) | ||
@@ -75,9 +45,12 @@ .on('error', function(err){ | ||
self.journalWriteStream = fs.createWriteStream(self.journalPath, { | ||
flags: newFile ? 'w' : 'a', | ||
encoding: 'utf8' | ||
}); | ||
self.journalWriteStream = JSONWriter(); | ||
self.journalWriteStream | ||
.pipe(lineWriter()) | ||
.pipe(fs.createWriteStream(self.journalPath, { | ||
flags: newFile ? 'w' : 'a', | ||
encoding: 'utf8' | ||
})); | ||
if (newFile && hasData) { | ||
self.journalWriteStream.write(JSON.stringify(hash.toJSON())+'\n', 'utf8', function(){ | ||
self.journalWriteStream.write(hash.toJSON(), function(){ | ||
callback(self); | ||
@@ -106,3 +79,3 @@ }); | ||
promise = new Promise(function(resolve){ | ||
self.journalWriteStream.write(JSON.stringify(diff)+'\n', 'utf8', resolve); | ||
self.journalWriteStream.write(diff, resolve); | ||
}); | ||
@@ -109,0 +82,0 @@ } else { |
{ | ||
"name": "journaling-hash", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Immutable hash that can back itself up to a file and rebuild itself", | ||
@@ -27,4 +27,8 @@ "main": "index.js", | ||
"immutable-hash": "^0.1.6", | ||
"through2": "^0.6.3" | ||
"through2": "^0.6.3", | ||
"through2-jsonreader": "0.0.1", | ||
"through2-jsonwriter": "0.0.1", | ||
"through2-linereader": "0.0.1", | ||
"through2-linewriter": "0.0.1" | ||
} | ||
} |
7530
7
72
+ Addedthrough2-jsonreader@0.0.1
+ Addedthrough2-jsonwriter@0.0.1
+ Addedthrough2-linereader@0.0.1
+ Addedthrough2-linewriter@0.0.1
+ Addedreadable-stream@1.1.14(transitive)
+ Addedthrough2@1.1.1(transitive)
+ Addedthrough2-jsonreader@0.0.1(transitive)
+ Addedthrough2-jsonwriter@0.0.1(transitive)
+ Addedthrough2-linereader@0.0.1(transitive)
+ Addedthrough2-linewriter@0.0.1(transitive)