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