bitcore-wallet-service/lib/client/filestorage.js

31 lines
588 B
JavaScript
Raw Normal View History

2015-02-15 08:14:36 -08:00
var fs = require('fs')
function FileStorage(opts) {
if (!opts.filename) {
throw new Error('Please set the config filename');
}
this.filename = opts.filename;
2015-02-16 10:02:20 -08:00
this.fs = opts.fs || fs;
2015-02-15 08:14:36 -08:00
};
2015-02-16 15:23:25 -08:00
FileStorage.prototype.save = function(data, cb) {
this.fs.writeFile(this.filename, JSON.stringify(data), cb);
2015-02-15 08:14:36 -08:00
};
2015-02-16 15:23:25 -08:00
FileStorage.prototype.load = function(cb) {
this.fs.readFile(this.filename, 'utf8', function(err,data) {
if (err) return cb(err);
try {
data = JSON.parse(data);
} catch (e) {
}
return cb(null, data);
});
2015-02-15 08:14:36 -08:00
};
module.exports = FileStorage;