molly-db 1.0.11 → 1.0.14

Sign up to get free protection for your applications and to get access to all the features.
package/main.js CHANGED
@@ -15,28 +15,31 @@ function config( _config ) {
15
15
 
16
16
  /* --------------------------------------------------------------------------------------- */
17
17
 
18
+ function startWorker(_path,_self){
19
+ return new Promise((response,reject)=>{
20
+ const wrk = new worker.Worker(
21
+ _path,{ workerData:_self,
22
+ env: worker.SHARE_ENV,
23
+ }); wrk.on('message',(msg)=>{
24
+ console.log(msg)
25
+ response(msg)
26
+ }); wrk.on('exit',(err)=>{ console.log(err);
27
+ response(startWorker(_path,_self));
28
+ });
29
+ });
30
+ }
31
+
32
+ /* --------------------------------------------------------------------------------------- */
33
+
18
34
  class molly_db{
19
35
 
20
36
  constructor( opt ){
21
- return new Promise((response,reject)=>{
22
- if( opt.pass )
23
- this.pass = opt.pass;
24
- this.port = opt.port || 27017;
25
- this.path = opt.path.replace(/^\./,process.cwd());
26
-
27
- if( this.worker ) return console.log(`server is running`);
28
-
29
- this.worker = new worker.Worker(
30
- `${__dirname}/module/_worker_.js`,{
31
- env: worker.SHARE_ENV,
32
- workerData: this
33
- }
34
- );
35
-
36
- this.worker.on('exit',(err)=>{ console.log(err); reject() });
37
- this.worker.on('error',(err)=>{ console.log(err); reject() });
38
- this.worker.on('message',(msg)=>{ console.log(msg); response() });
39
- });
37
+ if( opt.pass )
38
+ this.pass = opt.pass;
39
+ this.port = opt.port || 27017;
40
+ this.path = opt.path.replace(/^\./,process.cwd());
41
+ this.workerPath = `${__dirname}/module/_worker_.js`;
42
+ return startWorker( this.workerPath,this );
40
43
  }
41
44
 
42
45
  }
package/module/_init_.js CHANGED
@@ -4,21 +4,23 @@ function fillDB( _db, _table, _path ){
4
4
 
5
5
  let _itr = undefined;
6
6
 
7
- if( (/^http/).test(_path) ){
7
+ if( (/^http/).test(_path) ){ try{
8
8
  const stream = await fetch.get(_path,{responseType:'stream'});
9
9
  _itr = readline.createInterface({
10
10
  input: stream.data
11
- });
12
-
13
- } else
11
+ })
12
+ } catch(e) { console.log(`error reading ${_path}`); return response(); }}
13
+
14
+ else if( fs.existsSync(_path) )
14
15
  _itr = readline.createInterface({
15
16
  input: fs.createReadStream(_path)
16
17
  });
18
+
19
+ else { console.log(`error reading ${_path}`); return response(); }
17
20
 
18
- _itr.on('close',()=>{ response() });
19
21
  _itr.on('line',(line)=>{
20
22
  db[_db][_table].push(crypto.decrypt( line,query.pass ));
21
- });
23
+ }); _itr.on('close',()=>{ response() });
22
24
 
23
25
  });
24
26
  }
@@ -50,8 +52,7 @@ function fillDB( _db, _table, _path ){
50
52
  for( var j in DB.tables ){
51
53
  const table = DB.tables[j];
52
54
  const path = `${db._path_}/${table}.json`;
53
- db[name][table] = new Array();
54
- await fillDB( name,table,path );
55
+ db[name][table] = new Array(); await fillDB( name,table,path );
55
56
  }
56
57
 
57
58
  }
@@ -21,20 +21,13 @@ function json( _data ){
21
21
 
22
22
  function bodyParser( _data ){
23
23
 
24
- const date = Date.now();
25
- const result = new Array();
24
+ const date = Date.now(); _data = JSON.parse(_data);
25
+ const result = typeof _data != 'String' ? _data : [ _data ];
26
26
 
27
- if( typeof _data.length == 'number' ){
28
- for( var i in _data ){
29
- if(!_data[i]?.hash)
30
- _data[i].hash = crypto.hash( date,0 );
31
- result.push(JSON.stringify(_data[i]));
32
- }
33
- } else {
34
- if(!_data?.hash)
35
- _data.hash = crypto.hash( date,0 );
36
- result.push(JSON.stringify(_data));
37
- } return result;
27
+ return result.map(x=>{ if( !x?.hash )
28
+ x.hash = crypto.hash( date,Math.random() );
29
+ return JSON.stringify(x);
30
+ })
38
31
 
39
32
  }
40
33
 
@@ -45,9 +38,8 @@ function getBody(){
45
38
  const data = new Array();
46
39
  req.on('data',(chunk)=>{ data.push(chunk); });
47
40
  req.on('close',()=>{ try{
48
- const buff = Buffer.concat(data);
49
- const json = JSON.parse(buff);
50
- response( bodyParser(json) );
41
+ const buff = Buffer.concat(data);
42
+ response( bodyParser(buff) );
51
43
  } catch(e) { response(false) } });
52
44
  } else { response(true) }
53
45
  } catch(e) { response(false) }
@@ -141,50 +133,46 @@ function hash( _params ){
141
133
 
142
134
  /* --------------------------------------------------------------------------------------- */
143
135
 
144
- function shift( _params ){
145
- const result = db[_params.db][_params.table].shift(); save( _params ); return result;
136
+ async function shift( _params ){
137
+ const result = db[_params.db][_params.table].shift(); await save( _params ); return result;
146
138
  }
147
139
 
148
- function pop( _params ){
149
- const result = db[_params.db][_params.table].pop(); save( _params ); return result;
140
+ async function pop( _params ){
141
+ const result = db[_params.db][_params.table].pop(); await save( _params ); return result;
150
142
  }
151
143
 
152
144
  /* --------------------------------------------------------------------------------------- */
153
145
 
154
- async function push( _params ){
155
-
156
- db[_params.db][_params.table].push( ...body );
157
-
158
- save( _params ); return [{
159
- database: _params.db,
160
- table: _params.table,
161
- status: 'pushed'
162
- }];
163
-
146
+ function lengthOf( _params ){ return db[_params.db][_params.table].length; }
147
+ function indexOf( _params ){
148
+ return db[_params.db][_params.table].indexOf(x=>{
149
+ const regex = new RegExp(_params.target,'gi');
150
+ return regex.test(x);
151
+ });
164
152
  }
165
153
 
166
- async function splice( _params ){
154
+ /* --------------------------------------------------------------------------------------- */
155
+
156
+ async function unshift( _params ){
167
157
 
168
- db[_params.db][_params.table].splice(
169
- _params.offset,_params.length,...body
170
- );
158
+ db[_params.db][_params.table].unshift( ...body );
171
159
 
172
- save( _params ); return [{
160
+ await save( _params ); return [{
173
161
  database: _params.db,
174
162
  table: _params.table,
175
- status: 'spliced'
163
+ status: 'unshifted'
176
164
  }];
177
165
  }
178
166
 
179
- async function unshift( _params ){
180
-
181
- db[_params.db][_params.table].unshift( ...body );
167
+ async function push( _params ){
182
168
 
183
- save( _params ); return [{
169
+ db[_params.db][_params.table].push( ...body );
170
+ await save( _params ); return [{
184
171
  database: _params.db,
185
172
  table: _params.table,
186
- status: 'unshifted'
173
+ status: 'pushed'
187
174
  }];
175
+
188
176
  }
189
177
 
190
178
  /* --------------------------------------------------------------------------------------- */
@@ -199,7 +187,7 @@ async function update( _params ){
199
187
  if( !(index<0) )
200
188
  db[_params.db][_params.table].splice( index,1,...body );
201
189
 
202
- save( _params ); return [{
190
+ await save( _params ); return [{
203
191
  database: _params.db,
204
192
  table: _params.table,
205
193
  status: 'udated'
@@ -216,7 +204,7 @@ async function remove( _params ){
216
204
  if( !(index<0) )
217
205
  db[_params.db][_params.table].splice( index,1 );
218
206
 
219
- save( _params ); return [{
207
+ await save( _params ); return [{
220
208
  database: _params.db,
221
209
  table: _params.table,
222
210
  status: 'removed'
@@ -225,7 +213,7 @@ async function remove( _params ){
225
213
 
226
214
  /* --------------------------------------------------------------------------------------- */
227
215
 
228
- function addDB( _params ){
216
+ async function addDB( _params ){
229
217
  try{
230
218
 
231
219
  db._init_.DB.push({
@@ -233,7 +221,7 @@ function addDB( _params ){
233
221
  name: _params.db,
234
222
  }); db[_params.db] = new Array();
235
223
 
236
- save( _params ); return [{
224
+ await save( _params ); return [{
237
225
  database: _params.db,
238
226
  status: 'DB added'
239
227
  }];
@@ -241,7 +229,7 @@ function addDB( _params ){
241
229
  } catch(e) { }
242
230
  }
243
231
 
244
- function removeDB( _params ){
232
+ async function removeDB( _params ){
245
233
  try{
246
234
 
247
235
  const i = db._init_.DB.findIndex(x=>{
@@ -254,7 +242,7 @@ function removeDB( _params ){
254
242
  }); db._init_.DB.splice(i,1);
255
243
  db[_params.db] = new Array();
256
244
 
257
- save( _params ); return [{
245
+ await save( _params ); return [{
258
246
  database: _params.db,
259
247
  table: _params.table,
260
248
  status: 'DB deleted'
@@ -263,18 +251,19 @@ function removeDB( _params ){
263
251
  } catch(e) { }
264
252
  }
265
253
 
266
- function modifyDB( _name, _table ){
254
+ async function modifyDB( _name, _table ){
267
255
  try{
268
256
 
269
257
  const init = `${query.path}/_init_.json`;
270
258
  const path = `${query.path}/${_table}.json`;
271
-
272
259
  fs.writeFileSync( init,JSON.stringify(db._init_) );
273
260
 
274
261
  try {
275
262
  const length = db[_name][_table].length;
276
- if( length>0 ) encryptDB( _name, _table, path );
277
- else fs.writeFileSync( path,'' );
263
+ if( length>0 )
264
+ await encryptDB( _name, _table, path );
265
+ else
266
+ fs.writeFileSync( path,'' );
278
267
  } catch(e) {
279
268
  fs.unlinkSync( path );
280
269
  }
@@ -284,7 +273,7 @@ function modifyDB( _name, _table ){
284
273
 
285
274
  /* --------------------------------------------------------------------------------------- */
286
275
 
287
- function addTable( _params ){
276
+ async function addTable( _params ){
288
277
 
289
278
  if( db[_params.db][_params.table] )
290
279
  return {
@@ -299,7 +288,7 @@ function addTable( _params ){
299
288
 
300
289
  db[_params.db][_params.table] = new Array();
301
290
 
302
- save( _params ); return [{
291
+ await save( _params ); return [{
303
292
  database: _params.db,
304
293
  table: _params.table,
305
294
  status: 'table added'
@@ -307,7 +296,7 @@ function addTable( _params ){
307
296
 
308
297
  }
309
298
 
310
- function removeTable( _params ){
299
+ async function removeTable( _params ){
311
300
 
312
301
  const i = db._init_.DB.findIndex(x=>{
313
302
  return x.name == _params.db;
@@ -320,7 +309,7 @@ function removeTable( _params ){
320
309
  db._init_.DB[i].tables.splice(j,1);
321
310
  delete db[_params.db][_params.table];
322
311
 
323
- save( _params ); return [{
312
+ await save( _params ); return [{
324
313
  database: _params.db,
325
314
  table: _params.table,
326
315
  status: 'table removed'
@@ -337,8 +326,8 @@ function refresh( _params ){
337
326
  });
338
327
  }
339
328
 
340
- function save( _params ){
341
- modifyDB( _params.db,_params.table );
329
+ async function save( _params ){
330
+ await modifyDB( _params.db,_params.table );
342
331
  return [{
343
332
  database: _params.db,
344
333
  table: _params.table,
@@ -360,6 +349,9 @@ function save( _params ){
360
349
  else if( api.pathname == '/match' ) json( await match(params) )
361
350
  else if( api.pathname == '/update' ) json( await update(params) )
362
351
 
352
+ else if( api.pathname == '/index' ) json( await indexOf(params) )
353
+ else if( api.pathname == '/lengt' ) json( await lengthOf(params) )
354
+
363
355
  /* Save Api */
364
356
  else if( api.pathname == '/save' ) json( await save(params) )
365
357
  else if( api.pathname == '/remove' ) json( await remove(params) )
@@ -1,7 +1,7 @@
1
1
  const worker = require('worker_threads');
2
2
  const readline = require('readline');
3
3
  const crypto = require('./_crypto_');
4
- const {Buffer} = require('buffer');
4
+ const { Buffer } = require('buffer');
5
5
  const fetch = require('axios');
6
6
  const http = require('http');
7
7
  const url = require('url');
@@ -13,8 +13,7 @@ const db = new Object();
13
13
 
14
14
  function _init_(){
15
15
  return new Promise((response,reject)=>{
16
- try{
17
- eval( fs.readFileSync(`${__dirname}/_init_.js`).toString() );
16
+ try{ eval( fs.readFileSync(`${__dirname}/_init_.js`).toString() );
18
17
  } catch(e){ console.log(e); }
19
18
  });
20
19
  }
@@ -22,28 +21,21 @@ function _init_(){
22
21
  /* --------------------------------------------------------------------------------------- */
23
22
 
24
23
  function app(req,res){
25
- try{
26
- eval( fs.readFileSync(`${__dirname}/_server_.js`).toString() );
24
+ try{ eval( fs.readFileSync(`${__dirname}/_server_.js`).toString() );
27
25
  } catch(e) { console.log(e) }
28
26
  }
29
27
 
30
28
  /* --------------------------------------------------------------------------------------- */
31
29
 
32
30
  (()=>{
33
- try{
34
- http.createServer( app ).listen( query.port,()=>{
35
- console.log('molly-db cluster is running, please wait');
36
- _init_().then(()=>{
37
- worker.parentPort.postMessage({
38
- protocol: 'HTTP',
39
- status: 'started',
40
- workerID: process.pid,
41
- msg: 'molly-db is running',
42
- server: `http://localhost:${query.port}`,
43
- });
44
- }).catch(e=>{ process.exit(1); });
45
- });
46
- } catch(e) {}
31
+ http.createServer( app ).listen( query.port,()=>{
32
+ _init_().then(()=>{
33
+ worker.parentPort.postMessage({
34
+ protocol: 'HTTP', status: 'started',
35
+ workerID: process.pid, port: query.port,
36
+ });
37
+ }).catch(e=>{ console.log(e); process.exit(1); });
38
+ });
47
39
  })();
48
40
 
49
41
  /* --------------------------------------------------------------------------------------- */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "license": "MIT",
3
3
  "main": "main.js",
4
- "version": "1.0.11",
4
+ "version": "1.0.14",
5
5
  "name": "molly-db",
6
6
  "author": "bececrazy",
7
7
  "scripts": {