Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
angeal185 committed Jun 8, 2020
1 parent 573c944 commit bc8e9b1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
40 changes: 13 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ tweekdb.prototype = {
if(!cb){
try {
data = fs.readFileSync(src);
data = utils.check_data(this, data, config);
data = utils.check_read(this, data, config);
utils.cl(vb,['status','db '+ src +' cached and ready.'],96);
return this.deserialize(data);
} catch(e) {
utils.cl(vb,['warning','unable to load data, creating new...'],91);
try {
data = fs.readFileSync(this.backup_pre + src +'.'+ this.backup_ext);
data = utils.check_data(this, data, config);
data = utils.check_read(this, data, config);
utils.cl(vb,['status','db '+ src +' backup cached and ready.'],96);
return data ? this.deserialize(data) : this.schema;
} catch (err) {
Expand All @@ -94,7 +94,7 @@ tweekdb.prototype = {
}
} else {
let $this = this;
fs.readFile(src, function(err,res){
fs.readFile(src, function(err,data){
if(err){
fs.readFile(this.backup_pre + src +'.'+ this.backup_ext, function(err, res){
if(err){
Expand All @@ -110,6 +110,8 @@ tweekdb.prototype = {
utils.cl(vb,['status','new db '+ src +' cached and ready.'],96);
return
}


if($this.gzip || this.gzip_backup){
res = zlib.unzipSync(res, config.gzip.settings);
}
Expand All @@ -119,30 +121,19 @@ tweekdb.prototype = {
}
cb(false, $this.deserialize(res));
utils.cl(vb,['status','db '+ src +' backup cached and ready.'],96);

});
} else {
if($this.gzip){
data = zlib.unzipSync(data, config.gzip.settings);
}
data = data.toString('utf8');
if($this.encryption){
res = enc.decrypt(res, $this.secret, $this.enc_cnf);
}
cb(false, $this.deserialize(res));
data = utils.check_read($this, res, config)
cb(false, $this.deserialize(data));
utils.cl(vb,['status','db '+ src +' cached and ready.'],96);
}
})
}
},
save: function(data, cb) {
let src = this.src;
data = this.serialize(data);
if(this.encryption){
data = enc.encrypt(data, this.secret, this.enc_cnf);
}
if(this.gzip){
data = zlib.gzipSync(data, config.gzip.settings);
}
data = utils.check_write(this, data, config);
if(!cb){
let res = fs.writeFileSync(src, data);
utils.write_backup(this, data, config);
Expand All @@ -169,16 +160,11 @@ tweekdb.prototype = {
}
},
set_backup: function(data, cb) {
data = this.serialize(data);

let dest = config.backup.pre + this.src +'.'+ config.backup.ext;

if(this.encryption){
data = enc.encrypt(data, this.secret, this.enc_cnf);
}
if(this.gzip){
data = zlib.gzipSync(data, config.gzip.settings);
}
data = utils.check_write(this, data, config);

if(!cb){
return fs.writeFileSync(dest, data);
}
Expand Down Expand Up @@ -226,10 +212,10 @@ if(config.fetch.enabled){
utils.req($this, 'fetch_config', config, function(err,data){
if(err){return cb(err)}
try {
if(obj.encryption){
if($this.encryption){
data = enc.decrypt(data, $this.secret, $this.enc_cnf);
}
data = obj.deserialize(data);
data = $this.deserialize(data);
cb(false, data);
utils.cl(vb,['status','db from '+ $this.fetch_config.hostname +' cached and ready.'],96);
} catch (err) {
Expand Down
12 changes: 11 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const utils = {
}

},
check_data: function(obj, data, config){
check_read: function(obj, data, config){
if(obj.gzip){
data = zlib.unzipSync(data, config.gzip.settings);
}
Expand All @@ -32,6 +32,16 @@ const utils = {
}
return data;
},
check_write: function(obj, data, config){
data = obj.serialize(data);
if(obj.encryption){
data = enc.encrypt(data, obj.secret, obj.enc_cnf);
}
if(obj.gzip){
data = zlib.gzipSync(data, config.gzip.settings);
}
return data
},
req: function(obj, dest, config, cb){
const req = https.request(obj[dest], function(res){

Expand Down

0 comments on commit bc8e9b1

Please sign in to comment.