molly-db 1.3.7 → 1.3.9

Sign up to get free protection for your applications and to get access to all the features.
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
  }
@@ -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
 
@@ -16,8 +15,8 @@ function Exit(code){ console.log(`worker exit code: ${code}`); }
16
15
 
17
16
  /*--────────────────────────────────────────────────────────────────────────────────────────────--*/
18
17
 
19
- function saveTimeout(data,db){
20
- setTimeout(() => {
18
+ function saveInterval(data,db){
19
+ setInterval(() => {
21
20
  if( db._update_ ){
22
21
  db._update_ = false;
23
22
  utils.saveAll(data,db);
@@ -36,9 +35,9 @@ module.exports = (arg)=>{ init( arg ).then((db)=>{
36
35
  name: 'molly-db', protocol: 'HTTP',
37
36
  port: arg.port, host: arg.host,
38
37
  }));
39
- }); saveTimeout( arg, db );
38
+ }); saveInterval( arg, db );
40
39
 
41
40
  }).catch((e)=>{ console.log(e) });
42
41
  }
43
42
 
44
- /*--────────────────────────────────────────────────────────────────────────────────────────────--*/
43
+ /*--────────────────────────────────────────────────────────────────────────────────────────────--*/
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "license": "MIT",
11
11
  "main": "main.js",
12
12
  "name": "molly-db",
13
- "version": "1.3.7",
13
+ "version": "1.3.9",
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
- }