molly-db 1.0.13 → 1.0.16

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
@@ -15,28 +15,30 @@ 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('exit',(err)=>{ console.log(err);
24
+ response(startWorker(_path,_self));
25
+ }); wrk.on('message',(msg)=>{
26
+ console.log(msg); response(msg)
27
+ });
28
+ });
29
+ }
30
+
31
+ /* --------------------------------------------------------------------------------------- */
32
+
18
33
  class molly_db{
19
34
 
20
35
  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(err) });
37
- this.worker.on('error',(err)=>{ console.log(err); reject(err) });
38
- this.worker.on('message',(msg)=>{ console.log(msg); response(msg) });
39
- });
36
+ if( opt.pass )
37
+ this.pass = opt.pass;
38
+ this.port = opt.port || 27017;
39
+ this.path = opt.path.replace(/^\./,process.cwd());
40
+ this.workerPath = `${__dirname}/module/_worker_.js`;
41
+ return startWorker( this.workerPath,this );
40
42
  }
41
43
 
42
44
  }
package/module/_init_.js CHANGED
@@ -4,19 +4,19 @@ 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
-
11
+ })
12
+ } catch(e) { console.log(`error reading ${_path}`); return response(); }}
13
+
14
14
  else if( fs.existsSync(_path) )
15
15
  _itr = readline.createInterface({
16
16
  input: fs.createReadStream(_path)
17
17
  });
18
18
 
19
- else return response();
19
+ else { console.log(`error reading ${_path}`); return response(); }
20
20
 
21
21
  _itr.on('line',(line)=>{
22
22
  db[_db][_table].push(crypto.decrypt( line,query.pass ));
@@ -27,7 +27,7 @@ function fillDB( _db, _table, _path ){
27
27
 
28
28
  /* --------------------------------------------------------------------------------------- */
29
29
 
30
- (async()=>{
30
+ (()=>{ return new Promise(async(response,reject)=>{
31
31
  try {
32
32
 
33
33
  const path = `${query.path}/_init_.json`;
@@ -65,4 +65,6 @@ function fillDB( _db, _table, _path ){
65
65
  fs.writeFileSync( path,JSON.stringify(db._init_) );
66
66
 
67
67
  } response();
68
- })();
68
+
69
+
70
+ }); })();
@@ -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 = _data.length ? _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,12 +133,12 @@ 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
  /* --------------------------------------------------------------------------------------- */
@@ -161,40 +153,26 @@ function indexOf( _params ){
161
153
 
162
154
  /* --------------------------------------------------------------------------------------- */
163
155
 
164
- async function push( _params ){
156
+ async function unshift( _params ){
165
157
 
166
- db[_params.db][_params.table].push( ...body );
158
+ db[_params.db][_params.table].unshift( ...body );
167
159
 
168
- save( _params ); return [{
160
+ await save( _params ); return [{
169
161
  database: _params.db,
170
162
  table: _params.table,
171
- status: 'pushed'
163
+ status: 'unshifted'
172
164
  }];
173
-
174
165
  }
175
166
 
176
- async function splice( _params ){
177
-
178
- db[_params.db][_params.table].splice(
179
- _params.offset,_params.length,...body
180
- );
167
+ async function push( _params ){
181
168
 
182
- save( _params ); return [{
169
+ db[_params.db][_params.table].push( ...body );
170
+ await save( _params ); return [{
183
171
  database: _params.db,
184
172
  table: _params.table,
185
- status: 'spliced'
173
+ status: 'pushed'
186
174
  }];
187
- }
188
-
189
- async function unshift( _params ){
190
-
191
- db[_params.db][_params.table].unshift( ...body );
192
175
 
193
- save( _params ); return [{
194
- database: _params.db,
195
- table: _params.table,
196
- status: 'unshifted'
197
- }];
198
176
  }
199
177
 
200
178
  /* --------------------------------------------------------------------------------------- */
@@ -209,7 +187,7 @@ async function update( _params ){
209
187
  if( !(index<0) )
210
188
  db[_params.db][_params.table].splice( index,1,...body );
211
189
 
212
- save( _params ); return [{
190
+ await save( _params ); return [{
213
191
  database: _params.db,
214
192
  table: _params.table,
215
193
  status: 'udated'
@@ -226,7 +204,7 @@ async function remove( _params ){
226
204
  if( !(index<0) )
227
205
  db[_params.db][_params.table].splice( index,1 );
228
206
 
229
- save( _params ); return [{
207
+ await save( _params ); return [{
230
208
  database: _params.db,
231
209
  table: _params.table,
232
210
  status: 'removed'
@@ -235,7 +213,7 @@ async function remove( _params ){
235
213
 
236
214
  /* --------------------------------------------------------------------------------------- */
237
215
 
238
- function addDB( _params ){
216
+ async function addDB( _params ){
239
217
  try{
240
218
 
241
219
  db._init_.DB.push({
@@ -243,7 +221,7 @@ function addDB( _params ){
243
221
  name: _params.db,
244
222
  }); db[_params.db] = new Array();
245
223
 
246
- save( _params ); return [{
224
+ await save( _params ); return [{
247
225
  database: _params.db,
248
226
  status: 'DB added'
249
227
  }];
@@ -251,7 +229,7 @@ function addDB( _params ){
251
229
  } catch(e) { }
252
230
  }
253
231
 
254
- function removeDB( _params ){
232
+ async function removeDB( _params ){
255
233
  try{
256
234
 
257
235
  const i = db._init_.DB.findIndex(x=>{
@@ -264,7 +242,7 @@ function removeDB( _params ){
264
242
  }); db._init_.DB.splice(i,1);
265
243
  db[_params.db] = new Array();
266
244
 
267
- save( _params ); return [{
245
+ await save( _params ); return [{
268
246
  database: _params.db,
269
247
  table: _params.table,
270
248
  status: 'DB deleted'
@@ -273,18 +251,19 @@ function removeDB( _params ){
273
251
  } catch(e) { }
274
252
  }
275
253
 
276
- function modifyDB( _name, _table ){
254
+ async function modifyDB( _name, _table ){
277
255
  try{
278
256
 
279
257
  const init = `${query.path}/_init_.json`;
280
258
  const path = `${query.path}/${_table}.json`;
281
-
282
259
  fs.writeFileSync( init,JSON.stringify(db._init_) );
283
260
 
284
261
  try {
285
262
  const length = db[_name][_table].length;
286
- if( length>0 ) encryptDB( _name, _table, path );
287
- else fs.writeFileSync( path,'' );
263
+ if( length>0 )
264
+ await encryptDB( _name, _table, path );
265
+ else
266
+ fs.writeFileSync( path,'' );
288
267
  } catch(e) {
289
268
  fs.unlinkSync( path );
290
269
  }
@@ -294,7 +273,7 @@ function modifyDB( _name, _table ){
294
273
 
295
274
  /* --------------------------------------------------------------------------------------- */
296
275
 
297
- function addTable( _params ){
276
+ async function addTable( _params ){
298
277
 
299
278
  if( db[_params.db][_params.table] )
300
279
  return {
@@ -309,7 +288,7 @@ function addTable( _params ){
309
288
 
310
289
  db[_params.db][_params.table] = new Array();
311
290
 
312
- save( _params ); return [{
291
+ await save( _params ); return [{
313
292
  database: _params.db,
314
293
  table: _params.table,
315
294
  status: 'table added'
@@ -317,7 +296,7 @@ function addTable( _params ){
317
296
 
318
297
  }
319
298
 
320
- function removeTable( _params ){
299
+ async function removeTable( _params ){
321
300
 
322
301
  const i = db._init_.DB.findIndex(x=>{
323
302
  return x.name == _params.db;
@@ -330,7 +309,7 @@ function removeTable( _params ){
330
309
  db._init_.DB[i].tables.splice(j,1);
331
310
  delete db[_params.db][_params.table];
332
311
 
333
- save( _params ); return [{
312
+ await save( _params ); return [{
334
313
  database: _params.db,
335
314
  table: _params.table,
336
315
  status: 'table removed'
@@ -347,8 +326,8 @@ function refresh( _params ){
347
326
  });
348
327
  }
349
328
 
350
- function save( _params ){
351
- modifyDB( _params.db,_params.table );
329
+ async function save( _params ){
330
+ await modifyDB( _params.db,_params.table );
352
331
  return [{
353
332
  database: _params.db,
354
333
  table: _params.table,
@@ -400,4 +379,4 @@ function save( _params ){
400
379
  } catch(e) { error(e?.message||e); }
401
380
  })();
402
381
 
403
- /* --------------------------------------------------------------------------------------- */
382
+ /* --------------------------------------------------------------------------------------- */
@@ -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');
@@ -12,34 +12,24 @@ const db = new Object();
12
12
  /* --------------------------------------------------------------------------------------- */
13
13
 
14
14
  function _init_(){
15
- return new Promise((response,reject)=>{
16
- try{
17
- eval( fs.readFileSync(`${__dirname}/_init_.js`).toString() );
18
- } catch(e){ console.log(e); }
19
- });
15
+ try{ return eval( fs.readFileSync(`${__dirname}/_init_.js`).toString() );
16
+ } catch(e){ console.log(e); }
20
17
  }
21
18
 
22
19
  /* --------------------------------------------------------------------------------------- */
23
20
 
24
21
  function app(req,res){
25
- try{
26
- eval( fs.readFileSync(`${__dirname}/_server_.js`).toString() );
22
+ try{ eval( fs.readFileSync(`${__dirname}/_server_.js`).toString() );
27
23
  } catch(e) { console.log(e) }
28
24
  }
29
25
 
30
26
  /* --------------------------------------------------------------------------------------- */
31
27
 
32
- (()=>{
33
- try{
34
- http.createServer( app ).listen( query.port,()=>{
35
- _init_().then(()=>{
36
- worker.parentPort.postMessage({
37
- protocol: 'HTTP', status: 'started',
38
- workerID: process.pid, port: query.port,
39
- });
40
- }).catch(e=>{ process.exit(1); });
41
- });
42
- } catch(e) {}
43
- })();
28
+ (()=>{ http.createServer( app ).listen( query.port,()=>{
29
+ _init_().then(()=>{ worker.parentPort.postMessage({
30
+ workerID: process.pid, port: query.port,
31
+ protocol: 'HTTP', status: 'started',
32
+ }); }).catch(e=>{ process.exit(1); });
33
+ }); })();
44
34
 
45
35
  /* --------------------------------------------------------------------------------------- */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "license": "MIT",
3
3
  "main": "main.js",
4
- "version": "1.0.13",
4
+ "version": "1.0.16",
5
5
  "name": "molly-db",
6
6
  "author": "bececrazy",
7
7
  "scripts": {