molly-db 1.3.6 → 1.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/main.js CHANGED
@@ -18,17 +18,14 @@ function config( _config ) {
18
18
 
19
19
  class molly_db{
20
20
  constructor( opt ){
21
- if(opt.pass) this.pass = opt.pass; this.port = opt.port || 27017;
22
21
  const dir = path.join(__dirname,'/module/worker_handler.js');
23
22
 
24
- if( !(new RegExp(process.cwd())).test(opt.path) )
25
- this.path = path.join(process.cwd(),opt.path);
26
- else this.path = opt.path;
27
-
28
- this.host = opt.host || '127.0.0.1';
29
- this.protocol = opt.protocol || 'http';
30
- this.time = opt.saveTime || .1;
31
- this.import = opt.import || '';
23
+ this.host = opt?.host || '127.0.0.1';
24
+ this.protocol = opt?.protocol || 'http';
25
+ this.port = opt?.port || 27017;
26
+ this.path = opt?.path || null;
27
+ this.pass = opt?.pass || null;
28
+ this.time = opt?.saveTime || .1;
32
29
 
33
30
  return require(dir)(this);
34
31
  }
@@ -28,15 +28,20 @@ module.exports = function(db,req,res,arg){
28
28
 
29
29
  req.on('data',(chunk)=>{ raw.push(chunk) });
30
30
  req.on('close',()=>{
31
- const data = JSON.parse(Buffer.concat(raw));
32
- send(db,arg,data).then(msg=>{
33
- const status = msg.match(/\d+/i)[0];
34
- res.writeHead(status,{'content-type': 'text/plain'});
35
- res.write(msg); res.end();
36
- }).catch(e=>{
31
+ try {
32
+ const data = JSON.parse(Buffer.concat(raw));
33
+ send(db,arg,data).then(msg=>{
34
+ const status = msg.match(/\d+/i)[0];
35
+ res.writeHead(status,{'content-type': 'text/plain'});
36
+ res.write(msg); res.end();
37
+ }).catch(e=>{
38
+ res.writeHead(404,{'content-type': 'text/plain'});
39
+ res.write(e); res.end();
40
+ })
41
+ } catch(e) {
37
42
  res.writeHead(404,{'content-type': 'text/plain'});
38
- res.write(e); res.end();
39
- })
43
+ res.write('unexpected body'); res.end();
44
+ }
40
45
  });
41
46
 
42
47
  }
@@ -1,4 +1,3 @@
1
- const download = require('./import_handler');
2
1
  const crypto = require('./crypto_handler');
3
2
  const fetch = require('molly-fetch');
4
3
  const readline = require('readline');
@@ -11,23 +10,19 @@ const db = new Object();
11
10
  function fillDB( _db, _table, _path, _pass ){
12
11
  return new Promise(async(response,reject)=>{
13
12
 
14
- let _itr = undefined;
13
+ let stream;
15
14
 
16
- if( (/^http/).test(_path) ){ try{
17
- const stream = await fetch(_path,{responseType:'stream'});
18
- _itr = readline.createInterface({ input: stream.data });
19
- } catch(e) { return response(`error reading ${_path}`); }}
15
+ if( fs.existsSync(_path) ) stream = fs.createReadStream(_path);
20
16
 
21
- else if( fs.existsSync(_path) )
22
- _itr = readline.createInterface({
23
- input: fs.createReadStream(_path)
24
- });
25
-
26
- else { return response(`error reading ${_path}`); }
27
-
28
- _itr.on('line',(line)=>{
29
- db[_db][_table].push(crypto.decrypt( line,_pass ));
30
- }); _itr.on('close',()=>{ response() });
17
+ else if( (/^http/).test(_path) )
18
+ stream = (await fetch(_path,{responseType:'stream'})).data;
19
+
20
+ try {
21
+ const _itr = readline.createInterface({ input: stream });
22
+ _itr.on('close',()=>{ response() }); _itr.on('line',(line)=>{
23
+ db[_db][_table].push(crypto.decrypt( line,_pass ));
24
+ });
25
+ } catch(e) { return response(`error: reading ${_path}`) }
31
26
 
32
27
  });
33
28
  }
@@ -38,19 +33,21 @@ module.exports = function(args){
38
33
  return new Promise(async(response,reject)=>{
39
34
  try {
40
35
 
41
- const dir = path.join(args.path,'_init_.json');
42
-
43
- db._buff_ = fs.readFileSync( dir );
44
- db._init_ = JSON.parse( db._buff_ );
45
- db._path_ = args.path; delete db._buff_;
36
+ const dir = `${args.path}/_init_.json`;
37
+
38
+ if( !(/^http/).test(args.path) )
39
+ db._init_ = JSON.parse(fs.readFileSync(dir));
40
+ else
41
+ db._init_ = (await fetch(dir)).data;
42
+ db._path_ = args.path;
46
43
 
47
- for( var i in db._init_.DB ){
44
+ for( let i in db._init_.DB ){
48
45
 
49
46
  const DB = db._init_.DB[i]; const name = DB.name;
50
47
  delete db[name]; db[name] = new Object();
51
48
 
52
- for( var j in DB.tables ){
53
- const table = DB.tables[j]; const dir = path.join(db._path_,`${table}.json`);
49
+ for( let j in DB.tables ){
50
+ const table = DB.tables[j]; const dir = `${db._path_}/${table}.json`;
54
51
  db[name][table] = new Array(); await fillDB( name,table,dir,args.pass );
55
52
  }
56
53
 
@@ -58,8 +55,9 @@ module.exports = function(args){
58
55
 
59
56
  } catch(e) {
60
57
  db._init_ = {DB:[]}; db._path_ = args.path;
61
- const dir = path.join(args.path,'_init_.json');
62
- fs.writeFileSync( dir,JSON.stringify(db._init_) );
58
+ const dir = `${args.path}/_init_.json`; try {
59
+ fs.writeFileSync( dir,JSON.stringify(db._init_) );
60
+ } catch(e) { }
63
61
  } response(db);
64
62
  });
65
63
  }
package/module/utils.js CHANGED
@@ -61,8 +61,8 @@ const encryptDB = function( param, db, _db, _table, _path ){
61
61
  const modifyDB = async function( data, db, _name, _table ){
62
62
  try{
63
63
 
64
- const dir = path.join(db._path_,`${_table}.json`);
65
- const init = path.join(db._path_,'_init_.json');
64
+ const init = `${db._path_}/_init_.json`;
65
+ const dir = `${db._path_}/${_table}.json`;
66
66
 
67
67
  fs.writeFileSync( init,JSON.stringify(db._init_) );
68
68
 
@@ -241,7 +241,7 @@ output.removeDB = function(data,db){
241
241
  for( var i in db._init_.DB ){
242
242
  if( db._init_.DB[i].name == data.db ){
243
243
  db._init_.DB[i].tables.map(x=>{
244
- const dir = path.join(data.path,`${x}.json`);
244
+ const dir = `${data.path}/${x}.json`;
245
245
  if( fs.existsSync(dir) ) fs.unlinkSync(dir);
246
246
  });
247
247
  const arr = db._init_.DB; arr.splice(i,1);
@@ -301,7 +301,7 @@ output.removeTable = function(data,db){
301
301
 
302
302
  for( var i in db._init_.DB ){
303
303
  if( db._init_.DB[i].name == data.db ){
304
- const dir = path.join(data.path,`${data.table}.json`);
304
+ const dir = `${data.path}/${data.table}.json`;
305
305
  const j = db._init_.DB[i].tables.indexOf(data.table);
306
306
  if( fs.existsSync(dir) ) fs.unlinkSync(dir);
307
307
  const arr = db._init_.DB[i].tables; arr.splice(j,1);
@@ -7,7 +7,6 @@ const app = require('./api_handler');
7
7
  const {Buffer} = require('buffer');
8
8
  const utils = require('./utils');
9
9
  const http = require('http');
10
- const path = require('path');
11
10
 
12
11
  /*--────────────────────────────────────────────────────────────────────────────────────────────--*/
13
12
 
@@ -33,7 +32,7 @@ module.exports = (arg)=>{ init( arg ).then((db)=>{
33
32
  const server = http.createServer((req,res)=>app(db,req,res,arg));
34
33
  server.listen( arg.port, arg.host, ()=>{
35
34
  console.log(JSON.stringify({
36
- name: 'molly-file', protocol: 'HTTP',
35
+ name: 'molly-db', protocol: 'HTTP',
37
36
  port: arg.port, host: arg.host,
38
37
  }));
39
38
  }); saveTimeout( arg, db );
package/package.json CHANGED
@@ -5,12 +5,12 @@
5
5
  "repository": "https://github.com/EDBC-REPO-NPM/Molly-db",
6
6
  "dependencies": {
7
7
  "crypto-js": "^4.1.1",
8
- "molly-fetch": "^1.1.2"
8
+ "molly-fetch": "^1.1.7"
9
9
  },
10
10
  "license": "MIT",
11
11
  "main": "main.js",
12
12
  "name": "molly-db",
13
- "version": "1.3.6",
13
+ "version": "1.3.8",
14
14
  "author": "bececrazy",
15
15
  "description": "Molly-db is a free and open source library for nodejs that allow you create a lightweight encrypted database using Json files",
16
16
  "keywords": [
@@ -1,3 +0,0 @@
1
- module.exports = function(){
2
- //FIXME: falta crear el importador DB
3
- }