-
Notifications
You must be signed in to change notification settings - Fork 1
/
firegun2.ts
773 lines (724 loc) · 25.6 KB
/
firegun2.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
import Gun from 'gun';
import 'gun/sea';
import 'gun/lib/radix';
import 'gun/lib/radisk';
// import 'gun/lib/store';
import 'gun/lib/rindexed';
import { FiregunUser, Ack, common } from './common'
// @ts-ignore
import { IGunChainReference } from "gun/types/chain";
// @ts-ignore
import { IGunCryptoKeyPair } from "gun/types/types";
// @ts-ignore
import { IGunStatic } from 'gun/types/static';
function randomAlphaNumeric(length:number):string {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
export default class Firegun {
prefix : string;
dbname : string;
gun : IGunChainReference;
Gun : IGunStatic;
peers : string[];
user : FiregunUser;
ev : {
[key:string] : {
handler : any
}
}
/**
*
* --------------------------------------
* Create Firegun Instance
*
* @param peers list of gun Peers, default : []
* @param dbname dbName, default : "fireDB"
* @param localstorage whether to use localstorage or not (indexedDB)
* @param prefix node prefix, default : "" (no prefix)
* @param axe join axe network, default : false
* @param port multicast port, default : 8765
* @param gunInstance use an existing gunDB instance, default : null
*/
constructor(
option? : {
peers?: string[],
dbname?: string,
localstorage?: boolean,
prefix?: string,
axe?: boolean,
port?: number,
gunInstance?: (IGunChainReference | null),
},
) {
if (option) {
option.peers = option.peers || [],
option.dbname = option.dbname || "fireDB",
option.localstorage = option.localstorage || false,
option.prefix = option.prefix || "",
option.axe = option.axe || false,
option.port = option.port || 8765,
option.gunInstance = option.gunInstance || null
}
this.prefix = option?.prefix || "";
this.peers = option?.peers || [];
this.dbname = option?.dbname || "fireDB";
if (option?.gunInstance) {
this.gun = option.gunInstance;
} else {
// @ts-ignore
this.gun = Gun({
file : option?.dbname,
localStorage : option?.localstorage,
axe : option?.axe,
multicast : {
port : option?.port
},
peers : option?.peers
})
}
// @ts-ignore
this.Gun = Gun;
// Auto Login
this.user = {
alias : "",
pair : {
priv : "",
pub : "",
epriv : "",
epub : ""
}
};
if (typeof localStorage!== "undefined") {
let user = localStorage.getItem("fg.keypair");
user = user || ""
if (user)
try {
let autoLoginUser = JSON.parse(user);
this.loginPair(autoLoginUser.pair,autoLoginUser.alias)
.then(async ()=>{
console.log ("Checking Certificate...")
try {
await this.userGet("chat-cert");
console.log ("Checking Certificate...✔")
} catch (error) {
common.generatePublicCert(this);
}
})
} catch (error) {
}
}
this.ev = {};
}
/**
* Wait in ms
* @param ms duration of timeout in ms
* @returns
*/
async _timeout (ms : number) : Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* Delete On Subscription
* @param ev On subscription name, default : "default"
*/
async Off (ev = "default") {
if (this.ev[ev] && this.ev[ev].handler) {
this.ev[ev].handler.off();
} else {
this.ev[ev] = {
handler : null
}
}
}
/**
* Listen changes on path
*
* @param path node path
* @param callback callback
* @param prefix node prefix, default : ""
*/
async Listen (path: string,callback: (result:{[key:string] : any} | string | undefined) => void,prefix: string =this.prefix) : Promise<void> {
path = `${prefix}${path}`;
let paths = path.split("/");
let dataGun = this.gun;
paths.forEach(path => {
dataGun = dataGun.get(path);
});
dataGun.map().once((s:any)=>{
callback(s);
});
}
/**
* New subscription on Path. When data on Path changed, callback is called.
*
* @param path node path
* @param callback callback
* @param ev On name as identifier, to be called by Off when finished
* @param different Whether to fetch only differnce, or all of nodes
* @param prefix node prefix, default : ""
*/
async On (path: string,callback: (result:{[key:string] : any} | string | undefined) => void,ev: string = "default", different: boolean=true,prefix: string =this.prefix) : Promise<void> {
path = `${prefix}${path}`;
let paths = path.split("/");
let dataGun = this.gun;
paths.forEach(path => {
dataGun = dataGun.get(path);
});
let listenerHandler = (value : any, key : any , _msg : any, _ev : any) => {
if (false) {
console.log(key)
}
this.ev[ev] = {
handler : _ev
}
if (value)
callback(JSON.parse(JSON.stringify(value)))
}
// @ts-ignore
dataGun.on(listenerHandler,{ change : different});
}
/**
* ----------------------------------
* Insert CONTENT-ADDRESSING Readonly Data.
*
* dev note : Sebenarnya bisa tambah lagi searchable path dengan RAD,
* hanya saja RAD masih Memiliki BUG, dan tidak bekerja secara consistent
* @param key must begin with #
* @param data If object, it will be stringified automatically
* @returns
*/
async addContentAdressing (key: string,data: (string | {})): Promise<Ack> {
if (typeof data === "object") {
data = JSON.stringify(data);
}
let hash = await this.Gun.SEA.work(data, null, undefined, {name: "SHA-256"});
return new Promise((resolve) => {
if (hash)
this.gun.get(`${key}`).get(hash).put(<any>data,(s:any)=>{
resolve(<Ack>s);
});
});
}
/**
* Generate Key PAIR from SEA module
* @returns
*/
async generatePair (): Promise<IGunCryptoKeyPair | undefined> {
return new Promise(async (resolve) => {
resolve (await this.Gun.SEA.pair());
});
}
/**
*
* Login using SEA Pair Key, instead of using username and Password
*
* @param pair SEA Key Pair
* @param alias if ommited, the value is Anonymous
* @returns
*/
async loginPair (pair: IGunCryptoKeyPair ,alias: string=""): Promise<({ err: Error; } | FiregunUser )> {
if (alias === "") {
alias = pair.pub.slice(0,8);
}
return new Promise((resolve,reject)=>{
this.gun.user().auth(pair as any,((s:any)=>{
if ("err" in s) {
this.userLogout()
reject (s.err)
} else {
this.user = {
alias : alias,
pair : s.sea,
}
resolve(this.user);
localStorage.setItem("fg.keypair",JSON.stringify(this.user));
}
}));
});
}
/**
*
* Create a new user and Log him in
*
* @param username
* @param password
* @returns
*/
async userNew (username: string, password: string,alias:string = ""): Promise<{ err: string } | FiregunUser > {
return new Promise((resolve,reject)=>{
this.gun.user().create(username,password,async (s:any)=>{
if ("err" in s) {
reject(s);
} else {
this.gun.user().leave();
let user = await this.userLogin(username,password,alias);
if ("err" in user) {
reject (user);
} else {
resolve(this.user);
common.generatePublicCert(this)
}
}
});
})
}
/**
*
* Log a user in
*
* @param username
* @param password
* @param repeat time to repeat the login before give up. Because the nature of decentralization, just because the first time login is failed, doesn't mean the user / password pair doesn't exist in the network
* @returns
*/
async userLogin (username: string, password: string, alias:string = "", repeat: number=2): Promise<{err : string} | FiregunUser> {
return new Promise((resolve,reject)=>{
this.gun.user().auth(username,password,async (s:any)=>{
if ("err" in s) {
if (repeat>0) {
await this._timeout(1000);
this.userLogin(username,password,alias,repeat-1)
.then(s=>{
resolve(s)
})
.catch(s=>{
reject(s);
})
} else {
reject(s);
this.userLogout()
}
} else {
this.user = {
alias : alias || username,
pair : s.sea,
}
resolve(this.user);
if (typeof localStorage !== "undefined") {
localStorage.setItem("fg.keypair",JSON.stringify(this.user));
}
}
})
});
}
/**
* Log the user out
*/
async userLogout () {
this.gun.user().leave();
this.user = {
alias : "",
pair : {
priv : "",
pub : "",
epriv : "",
epub : ""
}
};
}
/**
*
* Fetch data from userspace
*
* @param path node path
* @param repeat time to repeat fetching before returning undefined
* @param prefix Database Prefix
* @returns
*/
async userGet (path: string,repeat: number = 1,prefix: string=this.prefix): Promise<string | {
[key: string]: {};
} | {
[key: string]: string;
} | undefined> {
if (this.user.alias) {
path = `~${this.user.pair.pub}/${path}`
return (await this.Get(path,repeat,prefix));
} else {
return undefined;
}
}
/**
* Load Multi Nested Data From Userspace
* @param path node path
* @param repeat time to repeat fetching before returning undefined
* @param prefix Database Prefix
* @returns
*/
async userLoad (path: string,async=false,repeat: number = 1,prefix: string=this.prefix) : Promise<{data : {[s:string] : any}, err : {path : string, err : string}[]}> {
if (this.user.alias) {
path = `~${this.user.pair.pub}/${path}`
return (await this.Load(path,async, repeat,prefix));
} else {
return {data : {}, err : [{path : path, err : "User not logged in"}]};
}
}
/**
*
* Fetching data
*
* @param {string} path node path
* @param {number} repeat time to repeat fetching before returning undefined
* @param {string} prefix Database Prefix
* @returns
*/
async Get (path: string,repeat: number = 1,prefix: string=this.prefix): Promise<undefined | string | {[key:string] : {}} | {[key:string] : string}> {
let path0 = path;
path = `${prefix}${path}`;
let paths = path.split("/");
let dataGun = this.gun;
paths.forEach(path => {
dataGun = dataGun.get(path);
});
return new Promise((resolve,reject) => {
setTimeout(() => {
reject({ err : "timeout", ket : `TIMEOUT, Possibly Data : ${path} is corrupt`, data : {}, "#" : path});
}, 5000);
dataGun.once(async (s:any)=>{
if (s) {
s = JSON.parse(JSON.stringify(s));
resolve(s);
} else {
if (repeat) {
await (this._timeout(1000))
try {
let data = await this.Get(path0,repeat-1,prefix)
resolve (data);
} catch (error) {
reject (error)
}
} else {
reject({ err : "notfound", ket : `Data Not Found, Data : ${path} is undefined`, data : {}, "#" : path});
}
}
})
});
}
/**
*
* Put data on userspace
*
* @param path
* @param data
* @returns
*/
async userPut (path: string,data: (string | {[key:string] : {}}),async=false, prefix=this.prefix): Promise<{data : Ack[],error : Ack[]}> {
return new Promise(async (resolve, reject) => {
if (this.user.alias) {
path = `~${this.user.pair.pub}/${path}`
resolve (await this.Put(path,data,async, prefix));
} else {
reject (<Ack>{err : new Error("User Belum Login") , ok : undefined});
}
});
}
/**
* Insert new Data into a node with a random key
*
* @param path
* @param data
* @param prefix
* @param opt
* @returns
*/
async Set (path: string,data: {[key : string] : {}} | {[key : string] : string} ,async=false, prefix=this.prefix,opt : undefined | { opt: { cert: string; }; }=undefined) : Promise<{data : Ack[],error : Ack[]}> {
return new Promise(async (resolve, reject) => {
var token = randomAlphaNumeric(30);
data.id = token;
this.Put(`${path}/${token}`,data,async,prefix,opt)
.then(s=>{
resolve(s);
})
.catch(err=>{
reject(err);
})
})
}
/**
* ----------------------------
* Put Data to the gunDB Node
*
* @param path node path
* @param data data to put
* @param prefix node prefix
* @param opt option (certificate)
* @returns
*/
async Put (path: string,data: (null | string | {[key:string] : {} | string}),async = false, prefix: string=this.prefix,opt:undefined | { opt : { cert : string} }=undefined): Promise<{data : Ack[],error : Ack[]}> {
path = `${prefix}${path}`;
// if (async) { console.log(path) }
let paths = path.split("/");
let dataGun = this.gun;
paths.forEach(path => {
dataGun = dataGun.get(path);
});
if (typeof data === "undefined") {
data = { "t" : "_" }
}
let promises : Promise<Ack>[] = [];
if (typeof data === "object")
var obj : {data : Ack[],error : Ack[]}
obj = { data: [] , error : []};
if (typeof data == "object")
for (const key in data) {
if (Object.hasOwnProperty.call(data, key)) {
const element = data[key];
if (typeof element === "object") {
delete data[key];
if (async) {
let s = await this.Put(`${path}/${key}`,element,async)
obj.data = [...obj.data, ...s.data]
obj.error = [...obj.error, ...s.error]
} else {
promises.push(
this.Put(`${path}/${key}`,element,async)
.then(s=>{
obj.data = [...obj.data, ...s.data]
obj.error = [...obj.error, ...s.error]
})
)
}
}
}
}
return new Promise((resolve,reject)=>{
Promise.allSettled(promises)
.then(()=>{
// Handle Empty Object
if (data && Object.keys(data).length === 0) {
resolve (obj)
} else {
setTimeout(() => {
obj.error.push({ err : Error("TIMEOUT, Failed to put Data"), ok : path});
resolve (obj);
}, 2000);
dataGun.put(<any>data,(ack:any)=>{
if (typeof obj === "undefined") {
obj = { data: [] , error : []};
}
if (ack.err === undefined) {
obj.data.push(ack);
} else {
obj.error.push({ err : Error(JSON.stringify(ack)), ok : path});
}
resolve(obj);
},opt)
}
})
.catch(s=>{
obj.error.push({ err : Error(JSON.stringify(s)), ok : path});
resolve(obj)
})
.catch((err)=>{
reject(err);
})
});
}
async purge(path:string) {
return new Promise(async (resolve,reject)=>{
let data = await this.Get(path);
let newData = JSON.parse(JSON.stringify(data));
if (typeof newData === "object"){
for (const key in newData) {
if (
key != "_" &&
key != ">" &&
key != "#" &&
key != ":"
)
newData[key] = null;
}
}
this.Put(path,newData)
.then(()=>{
resolve("OK")
})
.catch(err=>{
console.log(err);
reject(JSON.stringify(err));
})
})
}
/**
* Delete form user node
*
*
* @param path path to delete
* @param putNull
* - true (if you want to put null value)
* - false (if you want to delete the node with it's child)
* @returns
*/
async userDel (path : string, putNull:boolean=true) : Promise<{data : Ack[],error : Ack[]}> {
return new Promise(async (resolve, reject) => {
path = `~${this.user.pair.pub}/${path}`
this.Del(path,putNull)
.then(res=>{
resolve(res)
})
.catch(err=>{
reject(err)
})
});
}
/**
* Delete node Path. It's not really deleted. It's just detached (tombstone). Data without parent.
* @param path path to delete
* @param putNull
* - true (if you want to put null value)
* - false (if you want to delete the node with it's child)
*/
async Del (path : string, putNull:boolean=true,cert:string="") : Promise<{data : Ack[],error : Ack[]}> {
return new Promise(async (resolve, reject) => {
// var token = randomAlphaNumeric(50);
try {
let randomNode:any;
let paths = path.split("/");
let dataGun = this.gun;
// Check if path is user
if (putNull) {
randomNode = null
} else {
if (paths[0].indexOf("~")>=0) {
randomNode = this.gun.user().get('newNode').set({"t" : "_"});
} else {
randomNode = this.gun.get('newNode').set({"t" : "_"});
}
}
paths.forEach(path => {
dataGun = dataGun.get(path);
});
if (cert) {
dataGun.put(randomNode,(s:any)=>{
if (s.err === undefined) {
resolve({
data : [{
"ok" : "ok",
"err" : undefined
}],
error : []
})
} else {
reject({
data : [{
"ok" : "",
"err" : s.err
}],
error : []
})
}
},{
opt : {
cert : cert
}
})
} else {
dataGun.put(randomNode,(s:any)=>{
if (s.err === undefined) {
resolve({
data : [{
"ok" : "ok",
"err" : undefined
}],
error : []
})
} else {
reject({
data : [{
"ok" : "",
"err" : s.err
}],
error : []
})
}
})
}
} catch (error) {
reject(error);
}
})
}
/**
* Load Multi Nested Data
* @param path
* @param repeat time to repeat fetching before returning undefined
* @param prefix node Prefix
* @returns
*/
async Load (path: string,async=false,repeat: number = 1,prefix: string=this.prefix) : Promise<{data : {[s:string] : any}, err : {path : string, err : string}[]}> {
return new Promise((resolve, reject) => {
let promises :Promise<any>[] = []
let obj : {data : {[s:string] : {}}, err : {path : string, err : string}[]} = { data : {}, err : []};
this.Get(path,repeat,prefix)
.then(async (s)=>{
if (typeof s === "object")
for (const key in s) {
if ( key != "_" && key != "#" && key != ">" ) {
var element;
if (typeof s === "object") {
element = s[key];
} else {
element = s;
}
if (typeof element === "object") {
if (async) {
try {
let s = await <any>this.Load(`${path}/${key}`,async);
obj.data[key] = s;
} catch (error) {
(obj.err as any).push(error)
}
} else {
promises.push(this.Load(`${path}/${key}`,async).then(s=>{
obj.data[key] = s;
})
.catch(s=>{
obj.err.push(s)
})
);
}
} else {
obj.data[key] = element;
}
}
}
Promise.allSettled(promises)
.then(()=>{
resolve(obj);
})
.catch(s=>{
obj.err.push(s)
resolve(obj);
})
})
.catch(s=>{
obj.err.push(s)
resolve(obj);
})
.catch(err=>{
reject(err);
})
});
}
clearData() {
var req = indexedDB.deleteDatabase(this.dbname);
req.onsuccess = function () {
console.log("Deleted database successfully");
location.reload();
};
req.onerror = function () {
console.log("Couldn't delete database");
};
req.onblocked = function () {
console.log("Couldn't delete database due to the operation being blocked");
};
localStorage.removeItem("gun");
localStorage.removeItem("fireDB");
localStorage.removeItem("radata");
}
}