molly-db
Version:
Molly-db is a free and open source library for nodejs that allow you create a lightweight encrypted database using Json files
49 lines (40 loc) • 2.29 kB
JavaScript
const memory = require('./memory_handler');
const cluster = require('cluster');
const {Buffer} = require('buffer');
const output = new Object();
/*--────────────────────────────────────────────────────────────────────────────────────────────--*/
function send( db,arg,msg ){
const empty = '{ "status":"404", "message":"empty data" }';
const error = '{ "status":"404", "message":"error data" }';
return new Promise((response,reject)=>{
try {memory(arg,msg,db)
.then(x=> response(JSON.stringify(x)||empty) )
.catch(e=>response(JSON.stringify(e)||empty) )
} catch(e) { response(error) }
})
}
/*--────────────────────────────────────────────────────────────────────────────────────────────--*/
module.exports = function(db,req,res,arg){
if( req.method != 'POST' ){
res.writeHead(200,{'content-type': 'text/plain'});
return res.end('Only Post Method Avalilable');
} const raw = new Array();
req.on('data',(chunk)=>{ raw.push(chunk) });
req.on('close',()=>{
try {
const data = JSON.parse(Buffer.concat(raw));
send(db,arg,data).then(msg=>{
const status = msg.match(/\d+/i)[0];
res.writeHead(status,{'content-type': 'text/plain'});
res.write(msg); res.end();
}).catch(e=>{
res.writeHead(404,{'content-type': 'text/plain'});
res.write(e); res.end();
})
} catch(e) {
res.writeHead(404,{'content-type': 'text/plain'});
res.write('unexpected body'); res.end();
}
});
}
/*--────────────────────────────────────────────────────────────────────────────────────────────--*/