molly-db 1.0.4 → 1.0.5

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/module/localDB.js DELETED
@@ -1,415 +0,0 @@
1
- const readline = require('readline');
2
- const crypto = require('crypto-js');
3
- const fs = require('fs');
4
-
5
- //TODO: Function ---------------------------------------------------------------------------------//
6
- function slugify(str){
7
- const map = {
8
- 'o' : 'ó|ò|ô|õ|ö|Ó|Ò|Ô|Õ|Ö',
9
- 'a' : 'á|à|ã|â|ä|À|Á|Ã|Â|Ä',
10
- 'e' : 'é|è|ê|ë|É|È|Ê|Ë',
11
- 'i' : 'í|ì|î|ï|Í|Ì|Î|Ï',
12
- 'u' : 'ú|ù|û|ü|Ú|Ù|Û|Ü',
13
- 'c' : 'ç|Ç','n':'ñ|Ñ',
14
- '' : /\s+|\W+/,
15
- };
16
- for (var pattern in map) {
17
- str=str.replace( new RegExp(map[pattern],'gi' ), pattern);
18
- } return str.toLowerCase();
19
- }
20
-
21
- const JsonFormatter = {
22
- 'stringify': function(cipherParams) {
23
- var jsonObj = { ct: cipherParams.ciphertext.toString(crypto.enc.Base64) };
24
- if (cipherParams.salt) jsonObj.s = cipherParams.salt.toString();
25
- if (cipherParams.iv) jsonObj.iv = cipherParams.iv.toString();
26
- return new Buffer(JSON.stringify(jsonObj)).toString('base64');
27
- },
28
-
29
- 'parse': function(jsonStr) {
30
- var jsonObj = JSON.parse( new Buffer(jsonStr,'base64').toString('UTF-8'));
31
- var cipherParams = crypto.lib.CipherParams.create({
32
- ciphertext: crypto.enc.Base64.parse(jsonObj.ct)
33
- });
34
- if (jsonObj.iv) cipherParams.iv = crypto.enc.Hex.parse(jsonObj.iv);
35
- if (jsonObj.s) cipherParams.salt = crypto.enc.Hex.parse(jsonObj.s);
36
- return cipherParams;
37
- }
38
- };
39
-
40
- //TODO: Optimization FUnctions -------------------------------------------------------------------//
41
-
42
- const init = function( _table,_config,_self ){
43
- return {
44
- _itr: readline.createInterface( createNewTable( `${_self.path}/${_table}.json` ) ),
45
- _cfg: !_config ? _self.default : _config,
46
- _tmp: `${_self.path}/${_table}_tmp.json`,
47
- _path: `${_self.path}/${_table}.json`,
48
- _res: new Array(),
49
- _i: 0,
50
- }
51
- };
52
-
53
- const lineConstrain = function( _i,_config ){
54
- if( _i >= parseInt(_config.length)+parseInt(_config.offset) ) return 1;
55
- else if ( _i < parseInt(_config.offset) ) return -1
56
- else return 0;
57
- }
58
-
59
- const createNewTable = function( _path ){
60
- if( !fs.existsSync( _path ) ) fs.writeFileSync( _path,"" );
61
- const table = fs.createReadStream( _path );
62
- return { debug: false, input: table };
63
- }
64
-
65
- const createNewHash = function( _object ){
66
- _object['_stamp'] = Date.now();
67
- const _base = JSON.stringify( _object );
68
- if( !_object.hash )
69
- _object.hash = crypto.SHA256( _base ).toString();
70
- return _object;
71
- }
72
-
73
- //TODO: localDB Class ----------------------------------------------------------------------------//
74
-
75
- class localDB{
76
-
77
- encrypted = false
78
- events = new Object()
79
- default = { offset: 0, length: 100 }
80
-
81
- constructor( object ){
82
- if( object.pass ){
83
- this.password = object.pass;
84
- this.encrypted = true;
85
- } this.path = object.path;
86
- }
87
-
88
- // TODO: Encription & Decription DATA //
89
- encrypt( _message,_password=this.password,_encrypted=this.encrypted ){
90
- try{
91
- if( _encrypted )
92
- return crypto.AES.encrypt( _message,_password,{
93
- format: JsonFormatter
94
- }).toString(); return _message;
95
- } catch(e) { return _message; }
96
- }
97
-
98
- decrypt( _message,_password=this.password,_encrypted=this.encrypted ){
99
- try{
100
- if( _encrypted )
101
- return crypto.AES.decrypt( _message,_password,{
102
- format: JsonFormatter
103
- }).toString( crypto.enc.Utf8 );
104
- return _message;
105
- } catch(e) { return _message; }
106
- }
107
-
108
- // TODO: Searching functions //
109
- list( _table, _config ){
110
- return new Promise( (res,rej)=>{ try{
111
-
112
- let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,_config,this );
113
-
114
- _itr.on( 'line',( encryptedLine )=>{ try{
115
- const line = this.decrypt( encryptedLine );
116
- const cns = lineConstrain( _i, _cfg );
117
- if( cns == 0 ) { _res.push( JSON.parse( line ) );
118
- } else if( cns == 1 ) _itr.close();
119
- } catch(e) { rej(`the db can be decripted: ${e}`) }
120
- _i++; });
121
-
122
- _itr.on( 'close',()=>{ res({
123
- table:_table,
124
- data:_res,
125
- }); });
126
-
127
- } catch(e) { rej( e ); } });
128
- }
129
-
130
- find( _table, _target, _config ){
131
- return new Promise( async(res,rej)=>{ try{
132
-
133
- let { _i,_cfg,_itr,_res } = await init( _table,_config,this );
134
-
135
- _itr.on( 'line',( encryptedLine )=>{
136
- try{
137
- const line = this.decrypt( encryptedLine );
138
- const keys = Object.keys( _target );
139
- const data = JSON.parse( line );
140
-
141
- const regex = ( x )=>{
142
- const target = slugify(_target[x].toString());
143
- const info = slugify(data[x].toString());
144
- const regex = new RegExp(target,'gi');
145
- return regex.test(info);
146
- }
147
-
148
- const every = keys.every( (x)=>{return regex(x)} );
149
- if( ( every ) ){
150
- const cns = lineConstrain( _i, _cfg );
151
- if( cns == 0 ) _res.push( data );
152
- else if( cns == 1 ) _itr.close();
153
- _i++;}
154
-
155
- } catch(e) { rej(`the db can be decripted: ${e}`) }
156
- });
157
-
158
- _itr.on( 'close',()=>{ res({
159
- table:_table,
160
- data:_res,
161
- }); });
162
-
163
- } catch(e) { rej( e ); } });
164
- }
165
-
166
- match( _table,_match,_config ){
167
- return new Promise( async(res,rej)=>{ try{
168
-
169
- let { _i,_cfg,_itr,_res } = await init( _table,_config,this );
170
-
171
- _itr.on( 'line',( encryptedLine )=>{ try{
172
- const regex = new RegExp(slugify(_match),'gi');
173
- const line = this.decrypt( encryptedLine );
174
- if( regex.test(slugify(line)) ){
175
- const cns = lineConstrain( _i, _cfg );
176
- if( cns == 0 ) _res.push( JSON.parse( line ) );
177
- else if( cns == 1 ) _itr.close();
178
- _i++;}
179
- } catch(e) { rej(`the db can be decripted: ${e}`) }
180
- });
181
-
182
- _itr.on( 'close',()=>{ res({
183
- table:_table,
184
- data:_res,
185
- }); });
186
-
187
- } catch(e) { rej( e ); } });
188
- }
189
-
190
- hash( _table, _hash ){
191
- return new Promise( (res,rej)=>{ try{
192
-
193
- let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
194
-
195
- _itr.on( 'line',( encryptedLine )=>{ try{
196
- const line = this.decrypt( encryptedLine );
197
- const data = JSON.parse( line );
198
- if( data.hash == _hash ){
199
- _res.push( data );
200
- _itr.close();
201
- }
202
- } catch(e) { rej(`the db can be decripted: ${e}`) }
203
- });
204
-
205
- _itr.on( 'close',()=>{ res({
206
- table:_table,
207
- data:_res,
208
- }); });
209
-
210
- } catch(e) { rej( e ); } });
211
- }
212
-
213
- // TODO: Saving functions //
214
- encryptTable( _table,_password ){
215
- return new Promise( (res,rej)=>{ try{
216
-
217
- let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
218
- const writable = fs.createWriteStream( _tmp );
219
-
220
- writable.on( 'finish',()=>{ fs.renameSync( _tmp, _path ); res(); });
221
-
222
- _itr.on( 'line',( encryptedLine )=>{
223
- try{
224
- const line = this.decrypt( encryptedLine );
225
- const encryptedData = this.encrypt( line,_password,true );
226
- writable.write( `${encryptedData}\n` );
227
- } catch(e) { rej(`the db can be decripted ${e}`) }
228
- });
229
-
230
- _itr.on( 'close',()=>{ writable.end(); res({
231
- table: _table,
232
- }); });
233
-
234
- } catch(e) { rej( e ); } });
235
- }
236
-
237
- decryptTable( _table ){
238
- return new Promise( (res,rej)=>{ try{
239
-
240
- let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
241
- const writable = fs.createWriteStream( _tmp );
242
-
243
- writable.on( 'finish',()=>{ fs.renameSync( _tmp, _path ); res(); });
244
- _itr.on( 'line',( encryptedLine )=>{
245
- try{
246
- const line = this.decrypt( encryptedLine );
247
- writable.write( `${line}\n` );
248
-
249
- } catch(e) { rej(`the db can be decripted: ${e}`) }
250
- });
251
-
252
- _itr.on( 'close',()=>{ writable.end(); res({
253
- table: _table,
254
- }); });
255
-
256
- } catch(e) { rej( e ); } });
257
- }
258
-
259
- // TODO: Saving functions //
260
- push( _table, ..._object ){
261
- return new Promise( (res,rej)=>{ try{
262
-
263
- let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
264
- const writable = fs.createWriteStream( _tmp );
265
-
266
- _itr.on( 'line',( line )=>{ writable.write(`${line}\n`); });
267
-
268
- _itr.on( 'close',()=>{
269
- _object.flat().forEach( item=>{
270
- item = createNewHash( item );
271
- const encryptedData = this.encrypt( JSON.stringify( item ) );
272
- writable.write( `${encryptedData}\n` );
273
- }); writable.end();
274
- });
275
-
276
- writable.on( 'finish',()=>{ fs.renameSync( _tmp, _path ); res({
277
- table: _table,
278
- }); });
279
-
280
- } catch(e) { rej(e); }
281
- }); }
282
-
283
- unshift( _table, ..._object ){
284
- return new Promise( (res,rej)=>{ try{
285
-
286
- let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
287
- const writable = fs.createWriteStream( _tmp );
288
-
289
- _itr.on( 'line',( line )=>{
290
- if( _i == 0 ){
291
- _object.flat().forEach( item=>{
292
- item = createNewHash( item );
293
- const encryptedData = this.encrypt( JSON.stringify( item ) );
294
- writable.write( `${encryptedData}\n` );
295
- });
296
- } writable.write( `${line}\n` );
297
- _i++;});
298
-
299
- _itr.on( 'close',()=>{ writable.end(); });
300
-
301
- writable.on( 'finish',()=>{ fs.renameSync( _tmp, _path ); res({
302
- table: _table,
303
- }); });
304
-
305
- } catch(e) { rej(e) }
306
- }); }
307
-
308
- place( _table, _line, ..._object ){
309
- return new Promise( (res,rej)=>{ try{
310
-
311
- let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
312
- const writable = fs.createWriteStream( _tmp );
313
-
314
- _itr.on( 'line',( line )=>{
315
- if( _i == _line ){
316
- _object.flat().forEach( item=>{
317
- item = createNewHash( item );
318
- const encryptedData = this.encrypt( JSON.stringify( item ) );
319
- writable.write( `${encryptedData}\n` );
320
- });
321
- } writable.write( `${line}\n` );
322
- _i++;});
323
-
324
- _itr.on( 'close',()=>{ writable.end(); });
325
-
326
- writable.on( 'finish',()=>{ fs.renameSync( _tmp, _path ); res({
327
- table: _table,
328
- }); });
329
-
330
- } catch(e) { rej(e) }
331
- }); }
332
-
333
- update( _table, _hash, ..._object ){
334
- return new Promise( (res,rej)=>{ try{
335
-
336
- let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
337
- const writable = fs.createWriteStream( _tmp );
338
-
339
- _itr.on( 'line',( encryptedLine )=>{ try{
340
- const line = this.decrypt( encryptedLine );
341
- const data = JSON.parse( line );
342
- if( data.hash == _hash ){
343
- _object.flat().forEach( item=>{
344
- item = createNewHash( item );
345
- const encryptedData = this.encrypt( JSON.stringify( item ) );
346
- writable.write( `${encryptedData}\n` );
347
- });
348
- } else
349
- writable.write( `${encryptedLine}\n` );
350
- } catch(e) { rej(`the db can be decripted: ${e}`) }
351
- });
352
-
353
- _itr.on( 'close',()=>{ writable.end(); });
354
-
355
- writable.on( 'finish',()=>{ fs.renameSync( _tmp, _path ); res({
356
- table: _table,
357
- }); });
358
-
359
- } catch(e) { rej(e) }
360
- }); }
361
-
362
- // TODO: Removing functions //
363
- remove( _table, _hash ){
364
- return new Promise( (res,rej)=>{ try{
365
-
366
- let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
367
- const writable = fs.createWriteStream( _tmp );
368
-
369
- _itr.on( 'line',( encryptedLine )=>{
370
- const line = this.decrypt( encryptedLine );
371
- const data = JSON.parse( line );
372
- if( data.hash != _hash )
373
- writable.write( `${encryptedLine}\n` );
374
- });
375
-
376
- _itr.on( 'close',()=>{ writable.end(); });
377
-
378
- writable.on( 'finish',()=>{ fs.renameSync( _tmp, _path ); res({
379
- table: _table,
380
- }); });
381
-
382
- } catch(e) { rej(e) }
383
- }); }
384
-
385
- removeTable( _table ){
386
- return new Promise( (res,rej)=>{
387
- const _path = `${this.path}/${_table}`;
388
- fs.unlink( _path,( err,data )=>{
389
- if(err) rej( err );
390
- res({ table: _table });
391
- });
392
- })
393
- }
394
-
395
- shift( _table ){
396
- return new Promise( (res,rej)=>{ try{
397
-
398
- let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
399
- const writable = fs.createWriteStream( _tmp );
400
-
401
- _itr.on( 'line',( line )=>{ if( _i != 0 ) writable.write( `${line}\n` ); _i++;});
402
-
403
- _itr.on( 'close',()=>{ writable.end(); });
404
-
405
- writable.on( 'finish',()=>{ fs.renameSync( _tmp, _path ); res({
406
- table: _table,
407
- }); });
408
-
409
- } catch(e) { rej(e) }
410
- }); }
411
-
412
- }
413
-
414
- //TODO: localDB Class ----------------------------------------------------------------------------//
415
- module.exports = localDB;
@@ -1,208 +0,0 @@
1
- const crypto = require('crypto-js');
2
- const {Buffer} = require('buffer');
3
- const output = new Object();
4
-
5
- /* -------------------------------------------------------------------------------------------------------- */
6
-
7
- const JsonFormatter = {
8
-
9
- 'stringify': function(cipherParams) {
10
- var jsonObj = { ct: cipherParams.ciphertext.toString(crypto.enc.Base64) };
11
- if (cipherParams.salt) jsonObj.s = cipherParams.salt.toString();
12
- if (cipherParams.iv) jsonObj.iv = cipherParams.iv.toString();
13
- return new Buffer.from(JSON.stringify(jsonObj)).toString('base64');
14
- },
15
-
16
- 'parse': function(jsonStr) {
17
- var jsonObj = JSON.parse( new Buffer.from(jsonStr,'base64').toString());
18
- var cipherParams = crypto.lib.CipherParams.create({
19
- ciphertext: crypto.enc.Base64.parse(jsonObj.ct)
20
- });
21
- if (jsonObj.iv) cipherParams.iv = crypto.enc.Hex.parse(jsonObj.iv);
22
- if (jsonObj.s) cipherParams.salt = crypto.enc.Hex.parse(jsonObj.s);
23
- return cipherParams;
24
- }
25
-
26
- };
27
-
28
- /* -------------------------------------------------------------------------------------------------------- */
29
-
30
- function slugify(str){
31
- const map = {
32
- 'c' : 'ç',
33
- 'n' : 'ñ',
34
- 'e' : 'é|è|ê|ë',
35
- 'i' : 'í|ì|î|ï',
36
- 'u' : 'ú|ù|û|ü',
37
- 'o' : 'ó|ò|ô|õ|ö',
38
- 'a' : 'á|à|ã|â|ä',
39
- '' : /\s+|\W+/,
40
- };
41
- for (var pattern in map) {
42
- str=str.replace( new RegExp(map[pattern],'gi' ), pattern);
43
- } return str.toLowerCase();
44
- }
45
-
46
- function encrypt( _message,_password ){
47
- try{if( _password )
48
- return crypto.AES.encrypt( _message,_password,{
49
- format: JsonFormatter
50
- }).toString(); return _message;
51
- } catch(e) { return _message; }
52
- }
53
-
54
- function decrypt( _message,_password ){
55
- try{if( _password )
56
- return crypto.AES.decrypt( _message,_password,{
57
- format: JsonFormatter
58
- }).toString( crypto.enc.Utf8 );
59
- return _message;
60
- } catch(e) { return _message; }
61
- }
62
-
63
- function processData( _chunk ){
64
- for( var i=_chunk.length; i--; ){
65
- if( _chunk[i]==10 || _chunk[i]==13 )
66
- return [
67
- _chunk.slice(0,i),
68
- _chunk.slice(i),
69
- ];
70
- } return [ null, _chunk ];
71
- }
72
-
73
- /* -------------------------------------------------------------------------------------------------------- */
74
-
75
- function dbDecrypt( _list,_self ){ return _list.map((x)=>{ return decrypt(x,_self?.password) }); }
76
-
77
- function dbEncrypt( _list,_self ){ return _list.map((x)=>{ return encrypt(x,_self?.password) }); }
78
-
79
- /* -------------------------------------------------------------------------------------------------------- */
80
-
81
- output.readLine = ( _self,_stream,_config,_data )=>{
82
- return new Promise((response,reject)=>{
83
-
84
- let offset = 0;
85
- const db = new Array();
86
- let prcd = new Array();
87
- let data = new Buffer(0);
88
-
89
- _stream.on('data',async(chunk)=>{
90
-
91
- data = Buffer.concat([ data,chunk ]);
92
- prcd = processData( data );
93
- data = prcd[1];
94
-
95
- if( prcd[0] ){
96
-
97
- const encrypted = prcd[0].toString().split('\n');
98
- const decrypted = dbDecrypt(encrypted,_self);
99
-
100
- const done = await _data( db,decrypted,_config );
101
- offset += done.length;
102
-
103
- if( db.length >= _config.length )
104
- _stream.destroy();
105
- else if ( offset > _config.offset && db.length != 0 )
106
- db.push( ...done );
107
- else if ( offset > _config.offset && db.length == 0 )
108
- db.push( ...done.slice( offset - done.length - _config.offset - 1 ) );
109
-
110
- }
111
-
112
- });
113
-
114
- _stream.on('close',async()=>{ response( db.slice( 0,_config.length ) ); });
115
-
116
- });
117
- }
118
-
119
- output.list = ( _self,_stream,_config )=>{
120
-
121
- const onData = ( db,data,_config )=>{
122
- return new Promise((response,reject)=>{
123
- try{
124
- const index = data.findIndex((x)=>x=='');
125
- data.splice( index,1 );
126
- } catch(e) {} response(data);
127
- });
128
- }
129
-
130
- return new Promise((response,reject)=>{
131
- response( output.readLine( _self,_stream,_config,onData ));
132
- });
133
-
134
- }
135
-
136
- output.match = ( _self,_stream,_target,_config )=>{
137
-
138
- const onData = ( db,data,_config )=>{
139
- return new Promise((response,reject)=>{
140
- const done = new Array();
141
- data.map(x=>{
142
- try{
143
- if( (new RegExp(slugify(_target),'gi')).test(slugify(x)) )
144
- done.push(x);
145
- } catch(e) {} });
146
- response(done);
147
- });
148
- }
149
-
150
- return new Promise((response,reject)=>{
151
- response(output.readLine( _self,_stream,_config,onData ));
152
- });
153
-
154
- }
155
-
156
- output.find = ( _self,_stream,_target,_config )=>{
157
-
158
- const onData = ( db,data,_config )=>{
159
- return new Promise((response,reject)=>{
160
- const done = new Array();
161
- data.map(x=>{
162
- try{
163
- const data = JSON.parse(x);
164
- const cond = Object.keys(_target)
165
- .every(y=>{
166
- return (_target[y]).test(data[y]);
167
- }); if( cond ) done.push(x);
168
- } catch(e) {}
169
- }); response(done);
170
- });
171
- }
172
-
173
- return new Promise((response,reject)=>{
174
- response(output.readLine( _self,_stream,_config,onData ));
175
- });
176
-
177
- }
178
-
179
- output.hash = ( _self,_stream,_target )=>{
180
-
181
- const onData = ( db,data,_config )=>{
182
- return new Promise((response,reject)=>{
183
- data.some((x)=>{
184
- try{
185
- const y = JSON.parse(x);
186
- if( y.hash == _target )
187
- response([x]);
188
- } catch(e) {}
189
- });
190
- });
191
- }
192
-
193
- const _config = { offset:0, length:1 };
194
- return new Promise((response,reject)=>{
195
- response(output.readLine( _self,_stream,_config,onData ));
196
- });
197
-
198
- }
199
-
200
- output.stream = ( _self,_stream,_config,_onData )=>{
201
- return new Promise((response,reject)=>{
202
- response(output.readLine( _self,_stream,_config,_onData ));
203
- });
204
- }
205
-
206
- /* -------------------------------------------------------------------------------------------------------- */
207
-
208
- module.exports = output;