webpeerjs 0.0.8 → 0.0.9
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/README.md +15 -5
- package/dist/esm/webpeerjs.d.ts +3 -2
- package/dist/esm/webpeerjs.js +321 -89
- package/dist/umd/webpeerjs.js +15280 -15052
- package/package.json +11 -14
- package/src/config.js +1 -1
- package/src/utils.js +19 -8
- package/src/webpeerjs.js +320 -86
package/src/webpeerjs.js
CHANGED
|
@@ -9,7 +9,12 @@ import {
|
|
|
9
9
|
msgIdFnStrictNoSign,
|
|
10
10
|
metrics,
|
|
11
11
|
getDigest,
|
|
12
|
-
mkDebug
|
|
12
|
+
mkDebug,
|
|
13
|
+
multiaddr,
|
|
14
|
+
pipe,
|
|
15
|
+
lpStream,
|
|
16
|
+
lp,
|
|
17
|
+
map
|
|
13
18
|
} from './utils'
|
|
14
19
|
import { createDelegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-v1-http-api-client'
|
|
15
20
|
import { createLibp2p } from 'libp2p'
|
|
@@ -21,7 +26,6 @@ import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery'
|
|
|
21
26
|
import { circuitRelayTransport } from '@libp2p/circuit-relay-v2'
|
|
22
27
|
import { gossipsub } from '@chainsafe/libp2p-gossipsub'
|
|
23
28
|
import { identify, identifyPush } from '@libp2p/identify'
|
|
24
|
-
import { multiaddr } from '@multiformats/multiaddr'
|
|
25
29
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
26
30
|
import { kadDHT, removePrivateAddressesMapper } from '@libp2p/kad-dht'
|
|
27
31
|
import { simpleMetrics } from '@libp2p/simple-metrics'
|
|
@@ -84,6 +88,9 @@ class webpeerjs{
|
|
|
84
88
|
//message tracker avoid double
|
|
85
89
|
#msgIdtracker
|
|
86
90
|
|
|
91
|
+
//map of peer exchange data
|
|
92
|
+
#peerexchangedata
|
|
93
|
+
|
|
87
94
|
id
|
|
88
95
|
status
|
|
89
96
|
IPFS
|
|
@@ -112,6 +119,7 @@ class webpeerjs{
|
|
|
112
119
|
this.#dialQueue = []
|
|
113
120
|
this.#isDialEnabled = true
|
|
114
121
|
this.#msgIdtracker = []
|
|
122
|
+
this.#peerexchangedata = new Map()
|
|
115
123
|
|
|
116
124
|
this.peers = (function(f) {
|
|
117
125
|
return f
|
|
@@ -143,7 +151,7 @@ class webpeerjs{
|
|
|
143
151
|
const connect = connections.find((con)=>con.id == id)
|
|
144
152
|
const addr = connect.addr
|
|
145
153
|
|
|
146
|
-
if(config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id)
|
|
154
|
+
if(config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id)){
|
|
147
155
|
if(!this.#connections.has(id)&&addr.includes('webtransport')){
|
|
148
156
|
await this.#dbstore.put(new Key(id), new TextEncoder().encode(addr))
|
|
149
157
|
}
|
|
@@ -410,9 +418,8 @@ class webpeerjs{
|
|
|
410
418
|
count++
|
|
411
419
|
this.#trackDisconnect.set(id,count)
|
|
412
420
|
//console.log(this.#trackDisconnect)
|
|
413
|
-
if(count>
|
|
421
|
+
if(count>10){
|
|
414
422
|
if(this.#dbstoreData.has(id)){
|
|
415
|
-
//await this.#dbstore.delete(new Key(id))
|
|
416
423
|
this.#dbstoreData.delete(id)
|
|
417
424
|
}
|
|
418
425
|
|
|
@@ -425,6 +432,11 @@ class webpeerjs{
|
|
|
425
432
|
this.#trackDisconnect.set(id,0)
|
|
426
433
|
}
|
|
427
434
|
|
|
435
|
+
let peerexchangelist = []
|
|
436
|
+
for(const peer of this.#peerexchangedata.values()){
|
|
437
|
+
peerexchangelist.push(peer.id)
|
|
438
|
+
}
|
|
439
|
+
|
|
428
440
|
//if this disconnected peer is web peer redial it
|
|
429
441
|
if(this.#webPeersId.includes(id)){
|
|
430
442
|
const addr = this.#connections.get(id)
|
|
@@ -442,6 +454,14 @@ class webpeerjs{
|
|
|
442
454
|
mddrs.push(addrs)
|
|
443
455
|
this.#dialMultiaddress(mddrs)
|
|
444
456
|
}
|
|
457
|
+
|
|
458
|
+
else if(peerexchangelist.includes(id)){
|
|
459
|
+
const addr = this.#connections.get(id)
|
|
460
|
+
let mddrs = []
|
|
461
|
+
const addrs = multiaddr(addr)
|
|
462
|
+
mddrs.push(addrs)
|
|
463
|
+
this.#dialMultiaddress(mddrs)
|
|
464
|
+
}
|
|
445
465
|
|
|
446
466
|
//redial if this disconnected peer is regular peer
|
|
447
467
|
else{
|
|
@@ -474,34 +494,32 @@ class webpeerjs{
|
|
|
474
494
|
this.#libp2p.addEventListener('peer:identify', (evt) => {
|
|
475
495
|
//console.log('peer:identify '+evt.detail.peerId.toString(),evt.detail)
|
|
476
496
|
if(evt.detail.protocols.includes(config.CONFIG_PROTOCOL)){
|
|
477
|
-
//console.log('peer:identify '+evt.detail.peerId.toString(),evt
|
|
497
|
+
//console.log('peer:identify '+evt.detail.peerId.toString(),evt)
|
|
478
498
|
|
|
479
499
|
const id = evt.detail.peerId.toString()
|
|
480
500
|
let address = []
|
|
501
|
+
let mddrs = []
|
|
481
502
|
|
|
482
503
|
for(const addrs of evt.detail.listenAddrs){
|
|
483
504
|
const addr = addrs.toString()+'/p2p/'+id
|
|
505
|
+
const mddr = multiaddr(addr)
|
|
484
506
|
if(addr.includes('webtransport')){
|
|
485
507
|
address.push(addr)
|
|
508
|
+
mddrs.push(mddr)
|
|
486
509
|
}
|
|
487
510
|
}
|
|
488
511
|
|
|
489
|
-
if(!this.#webPeersId.includes(id))this.#webPeersId.push(id)
|
|
490
|
-
|
|
491
512
|
if(this.#connectedPeers.has(id)){
|
|
492
513
|
//reset this last seen
|
|
493
514
|
const now = new Date().getTime()
|
|
494
515
|
const metadata = {addrs:address,last:now}
|
|
495
516
|
this.#connectedPeers.set(id,metadata)
|
|
496
517
|
}
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
this.#connectedPeers.set(id,metadata)
|
|
503
|
-
this.#updatePeers()
|
|
504
|
-
}
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
const command = 'peer-exchange'
|
|
521
|
+
this.#dialProtocol(id,command)
|
|
522
|
+
|
|
505
523
|
|
|
506
524
|
}
|
|
507
525
|
})
|
|
@@ -582,14 +600,67 @@ class webpeerjs{
|
|
|
582
600
|
|
|
583
601
|
//Listen on new peer connection
|
|
584
602
|
#onConnectFn = () => {}
|
|
585
|
-
|
|
603
|
+
onConnect = f => (this.#onConnectFn = f)
|
|
586
604
|
|
|
587
605
|
|
|
588
606
|
//Listen on peer disconnect
|
|
589
607
|
#onDisconnectFn = () => {}
|
|
590
|
-
|
|
608
|
+
onDisconnect = f => (this.#onDisconnectFn = f)
|
|
591
609
|
|
|
610
|
+
joinRoom = room => {
|
|
611
|
+
if (this.#rooms[room]) {
|
|
612
|
+
return [
|
|
613
|
+
this.#rooms[room].sendMessage,
|
|
614
|
+
this.#rooms[room].listenMessage,
|
|
615
|
+
this.#rooms[room].onMembersChange
|
|
616
|
+
]
|
|
617
|
+
|
|
592
618
|
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
if (!room) {
|
|
622
|
+
throw mkErr('room is required')
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
//join room version 1 user pupsub via pupsub peer discovery
|
|
626
|
+
if(config.CONFIG_JOIN_ROOM_VERSION == 1){
|
|
627
|
+
|
|
628
|
+
const topics = config.CONFIG_PUBSUB_PEER_DISCOVERY
|
|
629
|
+
|
|
630
|
+
this.#rooms[room] = {
|
|
631
|
+
onMessage : () => {},
|
|
632
|
+
listenMessage : f => (this.#rooms[room] = {...this.#rooms[room], onMessage: f}),
|
|
633
|
+
sendMessage : async (message) => {
|
|
634
|
+
const msgId = (new Date()).getTime()
|
|
635
|
+
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,room,message,id:this.#libp2p.peerId.toString(),msgId})
|
|
636
|
+
const peer = {
|
|
637
|
+
publicKey: this.#libp2p.peerId.publicKey,
|
|
638
|
+
addrs: [uint8ArrayFromString(data)],
|
|
639
|
+
}
|
|
640
|
+
const encodedPeer = PBPeer.encode(peer)
|
|
641
|
+
for(const topic of topics){
|
|
642
|
+
await this.#libp2p.services.pubsub.publish(topic, encodedPeer)
|
|
643
|
+
}
|
|
644
|
+
},
|
|
645
|
+
members : [this.id],
|
|
646
|
+
onMembers : () => {},
|
|
647
|
+
onMembersChange : f => {this.#rooms[room] = {...this.#rooms[room], onMembers: f};this.#rooms[room].onMembers(this.#rooms[room].members);this.#ping()},
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
return [
|
|
652
|
+
this.#rooms[room].sendMessage,
|
|
653
|
+
this.#rooms[room].listenMessage,
|
|
654
|
+
this.#rooms[room].onMembersChange
|
|
655
|
+
]
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
dial(addr){
|
|
659
|
+
let mddrs = []
|
|
660
|
+
const mddr = multiaddr(addr)
|
|
661
|
+
mddrs.push(mddr)
|
|
662
|
+
this.#dialMultiaddress(mddrs)
|
|
663
|
+
}
|
|
593
664
|
|
|
594
665
|
|
|
595
666
|
/*
|
|
@@ -611,13 +682,96 @@ class webpeerjs{
|
|
|
611
682
|
}
|
|
612
683
|
|
|
613
684
|
async #registerProtocol(){
|
|
614
|
-
|
|
615
|
-
|
|
685
|
+
|
|
686
|
+
const handler = async ({ connection, stream, protocol }) => {
|
|
687
|
+
try{
|
|
688
|
+
const output = await pipe(
|
|
689
|
+
stream.source,
|
|
690
|
+
(source) => lp.decode(source),
|
|
691
|
+
(source) => map(source, (buf) => uint8ArrayToString(buf.subarray())),
|
|
692
|
+
async function (source) {
|
|
693
|
+
let string = ''
|
|
694
|
+
for await (const msg of source) {
|
|
695
|
+
string += msg.toString()
|
|
696
|
+
}
|
|
697
|
+
return string
|
|
698
|
+
}
|
|
699
|
+
)
|
|
700
|
+
|
|
701
|
+
const id = connection.remotePeer.toString()
|
|
702
|
+
|
|
703
|
+
let json = JSON.parse(output)
|
|
704
|
+
|
|
705
|
+
let jsonMessage = {
|
|
706
|
+
protocol:config.CONFIG_PROTOCOL,
|
|
707
|
+
command:null,
|
|
708
|
+
data:null
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
if(json.command === 'peer-exchange'){
|
|
712
|
+
|
|
713
|
+
if(json.protocol == config.CONFIG_PROTOCOL){
|
|
714
|
+
const address = [connection.remoteAddr.toString()]
|
|
715
|
+
if(this.#connectedPeers.has(id)){
|
|
716
|
+
//reset this last seen
|
|
717
|
+
const now = new Date().getTime()
|
|
718
|
+
const metadata = {addrs:address,last:now}
|
|
719
|
+
this.#connectedPeers.set(id,metadata)
|
|
720
|
+
}
|
|
721
|
+
else{
|
|
722
|
+
if(!this.#webPeersId.includes(id))this.#webPeersId.push(id)
|
|
723
|
+
|
|
724
|
+
//add to connected webpeers
|
|
725
|
+
this.#onConnectFn(id)
|
|
726
|
+
const now = new Date().getTime()
|
|
727
|
+
const metadata = {addrs:address,last:now}
|
|
728
|
+
this.#connectedPeers.set(id,metadata)
|
|
729
|
+
this.#updatePeers()
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
if(json.data != null){
|
|
734
|
+
this.#peerexchangedata.set(id,json.data)
|
|
735
|
+
let mddrs = []
|
|
736
|
+
const dataaddr = json.data.addr
|
|
737
|
+
const datamddr = multiaddr(dataaddr)
|
|
738
|
+
const dataid = json.data.id
|
|
739
|
+
mddrs.push(datamddr)
|
|
740
|
+
this.#dialMultiaddress(mddrs)
|
|
741
|
+
if(!this.#dbstoreData.has(dataid)){
|
|
742
|
+
//await this.#dbstore.put(new Key(dataid), new TextEncoder().encode(dataaddr))
|
|
743
|
+
//this.#dbstoreData.set(dataid,dataaddr)
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
const keys = Array.from(this.#dbstoreData.keys())
|
|
748
|
+
const randomKey = Math.floor(Math.random() * keys.length)
|
|
749
|
+
const key = keys[randomKey]
|
|
750
|
+
const addr = this.#dbstoreData.get(key)
|
|
751
|
+
|
|
752
|
+
jsonMessage.command = json.command
|
|
753
|
+
jsonMessage.data = {id:key,addr}
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
const message = JSON.stringify(jsonMessage)
|
|
757
|
+
//console.log('answer message '+id,message)
|
|
758
|
+
|
|
759
|
+
pipe(
|
|
760
|
+
message,
|
|
761
|
+
(source) => map(source, (string) => uint8ArrayFromString(string)),
|
|
762
|
+
(source) => lp.encode(source),
|
|
763
|
+
stream.sink
|
|
764
|
+
)
|
|
765
|
+
}
|
|
766
|
+
catch(err){
|
|
767
|
+
//console.warn(err)
|
|
768
|
+
}
|
|
616
769
|
}
|
|
617
770
|
|
|
618
771
|
await this.#libp2p.handle(config.CONFIG_PROTOCOL, handler, {
|
|
619
|
-
maxInboundStreams:
|
|
620
|
-
maxOutboundStreams:
|
|
772
|
+
maxInboundStreams: 50,
|
|
773
|
+
maxOutboundStreams: 50,
|
|
774
|
+
runOnTransientConnection:true
|
|
621
775
|
})
|
|
622
776
|
|
|
623
777
|
await this.#libp2p.register(config.CONFIG_PROTOCOL, {
|
|
@@ -633,6 +787,96 @@ class webpeerjs{
|
|
|
633
787
|
|
|
634
788
|
}
|
|
635
789
|
|
|
790
|
+
async #dialProtocol(id,command){
|
|
791
|
+
|
|
792
|
+
const connections = this.#libp2p.getConnections().map((con)=>{return {id:con.remotePeer.toString(),addr:con.remoteAddr.toString()}})
|
|
793
|
+
const connect = connections.find((con)=>con.id == id)
|
|
794
|
+
const addr = connect.addr
|
|
795
|
+
const mddr = multiaddr(addr)
|
|
796
|
+
|
|
797
|
+
let jsonMessage = {
|
|
798
|
+
protocol:config.CONFIG_PROTOCOL,
|
|
799
|
+
command:null,
|
|
800
|
+
data:null
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
if(command === 'peer-exchange'){
|
|
804
|
+
|
|
805
|
+
if(this.#peerexchangedata.has(id))return
|
|
806
|
+
|
|
807
|
+
const keys = Array.from(this.#dbstoreData.keys())
|
|
808
|
+
const randomKey = Math.floor(Math.random() * keys.length)
|
|
809
|
+
const key = keys[randomKey]
|
|
810
|
+
const addr = this.#dbstoreData.get(key)
|
|
811
|
+
|
|
812
|
+
jsonMessage.command = command
|
|
813
|
+
jsonMessage.data = {id:key,addr}
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
const message = JSON.stringify(jsonMessage)
|
|
817
|
+
//console.log('ask message '+id,message)
|
|
818
|
+
|
|
819
|
+
try{
|
|
820
|
+
|
|
821
|
+
const stream = await this.#libp2p.dialProtocol(mddr, config.CONFIG_PROTOCOL,{runOnTransientConnection:true})
|
|
822
|
+
|
|
823
|
+
const output = await pipe(
|
|
824
|
+
message,
|
|
825
|
+
(source) => map(source, (string) => uint8ArrayFromString(string)),
|
|
826
|
+
(source) => lp.encode(source),
|
|
827
|
+
stream,
|
|
828
|
+
(source) => lp.decode(source),
|
|
829
|
+
(source) => map(source, (buf) => uint8ArrayToString(buf.subarray())),
|
|
830
|
+
async function (source) {
|
|
831
|
+
let string = ''
|
|
832
|
+
for await (const msg of source) {
|
|
833
|
+
string += msg.toString()
|
|
834
|
+
}
|
|
835
|
+
return string
|
|
836
|
+
}
|
|
837
|
+
)
|
|
838
|
+
|
|
839
|
+
const json = JSON.parse(output)
|
|
840
|
+
if(json.protocol == config.CONFIG_PROTOCOL){
|
|
841
|
+
const address = [addr]
|
|
842
|
+
if(this.#connectedPeers.has(id)){
|
|
843
|
+
//reset this last seen
|
|
844
|
+
const now = new Date().getTime()
|
|
845
|
+
const metadata = {addrs:address,last:now}
|
|
846
|
+
this.#connectedPeers.set(id,metadata)
|
|
847
|
+
}
|
|
848
|
+
else{
|
|
849
|
+
if(!this.#webPeersId.includes(id))this.#webPeersId.push(id)
|
|
850
|
+
|
|
851
|
+
//add to connected webpeers
|
|
852
|
+
this.#onConnectFn(id)
|
|
853
|
+
const now = new Date().getTime()
|
|
854
|
+
const metadata = {addrs:address,last:now}
|
|
855
|
+
this.#connectedPeers.set(id,metadata)
|
|
856
|
+
this.#updatePeers()
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
if(json.command == 'peer-exchange'){
|
|
860
|
+
if(json.data != null){
|
|
861
|
+
this.#peerexchangedata.set(id,json.data)
|
|
862
|
+
let mddrs = []
|
|
863
|
+
const dataaddr = json.data.addr
|
|
864
|
+
const datamddr = multiaddr(dataaddr)
|
|
865
|
+
const dataid = json.data.id
|
|
866
|
+
mddrs.push(datamddr)
|
|
867
|
+
this.#dialMultiaddress(mddrs)
|
|
868
|
+
if(!this.#dbstoreData.has(dataid)){
|
|
869
|
+
//await this.#dbstore.put(new Key(dataid), new TextEncoder().encode(dataaddr))
|
|
870
|
+
//this.#dbstoreData.set(dataid,dataaddr)
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
catch(err){
|
|
876
|
+
//console.warn(err)
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
|
|
636
880
|
#findHybridPeer(){
|
|
637
881
|
setTimeout(async()=>{
|
|
638
882
|
for(const target of config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
|
|
@@ -719,13 +963,25 @@ class webpeerjs{
|
|
|
719
963
|
|
|
720
964
|
const id = mddrs[0].toString().split('/').pop()
|
|
721
965
|
|
|
722
|
-
const
|
|
966
|
+
const queueids = this.#dialQueue.map((arr)=> arr[0].toString().split('/').pop())
|
|
723
967
|
|
|
724
968
|
//if peer id is already in the queque cancel queque
|
|
725
|
-
if(
|
|
969
|
+
if(queueids.includes(id)){
|
|
726
970
|
return
|
|
727
971
|
}
|
|
728
972
|
|
|
973
|
+
const webPeerCount = this.#connectedPeers.size
|
|
974
|
+
const allPeerCount = this.#libp2p.getPeers().length
|
|
975
|
+
const nodePeerCount = allPeerCount - webPeerCount
|
|
976
|
+
const limitCount = config.CONFIG_MAX_CONNECTIONS / 2
|
|
977
|
+
|
|
978
|
+
if(this.#webPeersId.includes(id)){
|
|
979
|
+
if(webPeerCount>limitCount)return
|
|
980
|
+
}
|
|
981
|
+
else{
|
|
982
|
+
if(nodePeerCount>limitCount)return
|
|
983
|
+
}
|
|
984
|
+
|
|
729
985
|
if(this.#webPeersId.includes(id) || config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id) || config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
|
|
730
986
|
this.#dialQueue.unshift(mddrs)
|
|
731
987
|
}
|
|
@@ -739,7 +995,7 @@ class webpeerjs{
|
|
|
739
995
|
//dial multiaddr address in queue list
|
|
740
996
|
#dialQueueList(){
|
|
741
997
|
|
|
742
|
-
if(!this.#isDialEnabled)return
|
|
998
|
+
if(!this.#isDialEnabled || !navigator.onLine)return
|
|
743
999
|
|
|
744
1000
|
const mddrsToDial = 5
|
|
745
1001
|
|
|
@@ -804,55 +1060,6 @@ class webpeerjs{
|
|
|
804
1060
|
await this.#libp2p.services.pubsub.publish(topic, encodedPeer)
|
|
805
1061
|
}
|
|
806
1062
|
}
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
joinRoom = room => {
|
|
810
|
-
if (this.#rooms[room]) {
|
|
811
|
-
return [
|
|
812
|
-
this.#rooms[room].sendMessage,
|
|
813
|
-
this.#rooms[room].listenMessage,
|
|
814
|
-
this.#rooms[room].onMembersChange
|
|
815
|
-
]
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
}
|
|
819
|
-
|
|
820
|
-
if (!room) {
|
|
821
|
-
throw mkErr('room is required')
|
|
822
|
-
}
|
|
823
|
-
|
|
824
|
-
//join room version 1 user pupsub via pupsub peer discovery
|
|
825
|
-
if(config.CONFIG_JOIN_ROOM_VERSION == 1){
|
|
826
|
-
|
|
827
|
-
const topics = config.CONFIG_PUBSUB_PEER_DISCOVERY
|
|
828
|
-
|
|
829
|
-
this.#rooms[room] = {
|
|
830
|
-
onMessage : () => {},
|
|
831
|
-
listenMessage : f => (this.#rooms[room] = {...this.#rooms[room], onMessage: f}),
|
|
832
|
-
sendMessage : async (message) => {
|
|
833
|
-
const msgId = (new Date()).getTime()
|
|
834
|
-
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,room,message,id:this.#libp2p.peerId.toString(),msgId})
|
|
835
|
-
const peer = {
|
|
836
|
-
publicKey: this.#libp2p.peerId.publicKey,
|
|
837
|
-
addrs: [uint8ArrayFromString(data)],
|
|
838
|
-
}
|
|
839
|
-
const encodedPeer = PBPeer.encode(peer)
|
|
840
|
-
for(const topic of topics){
|
|
841
|
-
await this.#libp2p.services.pubsub.publish(topic, encodedPeer)
|
|
842
|
-
}
|
|
843
|
-
},
|
|
844
|
-
members : [this.id],
|
|
845
|
-
onMembers : () => {},
|
|
846
|
-
onMembersChange : f => {this.#rooms[room] = {...this.#rooms[room], onMembers: f};this.#rooms[room].onMembers(this.#rooms[room].members);this.#ping()},
|
|
847
|
-
}
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
return [
|
|
851
|
-
this.#rooms[room].sendMessage,
|
|
852
|
-
this.#rooms[room].listenMessage,
|
|
853
|
-
this.#rooms[room].onMembersChange
|
|
854
|
-
]
|
|
855
|
-
}
|
|
856
1063
|
|
|
857
1064
|
|
|
858
1065
|
//dial discovered peers
|
|
@@ -955,8 +1162,7 @@ class webpeerjs{
|
|
|
955
1162
|
if(besttime>bestlimit){
|
|
956
1163
|
const addr = remote.toString()
|
|
957
1164
|
const id = peer.toString()
|
|
958
|
-
if(!this.#webPeersId.includes(id) && !config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id) && !this.#dbstoreData.
|
|
959
|
-
//await this.#dbstore.delete(new Key(id))
|
|
1165
|
+
if(!this.#webPeersId.includes(id) && !config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id) && !this.#dbstoreData.has(id) && !addr.includes('p2p-circuit') && addr.includes('webtransport')){
|
|
960
1166
|
await this.#dbstore.put(new Key(id), new TextEncoder().encode(addr))
|
|
961
1167
|
this.#dbstoreData.set(id,addr)
|
|
962
1168
|
}
|
|
@@ -971,26 +1177,31 @@ class webpeerjs{
|
|
|
971
1177
|
}
|
|
972
1178
|
|
|
973
1179
|
|
|
974
|
-
let peers = []
|
|
975
|
-
for(const peer of this.#libp2p.getPeers()){
|
|
976
|
-
peers.push(peer.toString())
|
|
977
|
-
}
|
|
978
|
-
|
|
979
|
-
|
|
980
1180
|
//connect to saved best peer address
|
|
981
1181
|
//working great
|
|
982
1182
|
for(const peer of this.#dbstoreData){
|
|
983
1183
|
const id = peer[0]
|
|
984
1184
|
const addr = peer[1]
|
|
985
|
-
if(
|
|
1185
|
+
if(this.#isConnected(id)){
|
|
986
1186
|
this.#connectionTrackerStore.set(id,0)
|
|
987
1187
|
continue
|
|
988
1188
|
}else{
|
|
989
1189
|
if(this.#connectionTrackerStore.has(id)){
|
|
990
1190
|
let current = this.#connectionTrackerStore.get(id)
|
|
991
|
-
if(current>10)continue
|
|
992
1191
|
current++
|
|
993
1192
|
this.#connectionTrackerStore.set(id,current)
|
|
1193
|
+
if(current>5){
|
|
1194
|
+
if(!this.#connections.has(id) && !config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id) && navigator.onLine)
|
|
1195
|
+
{
|
|
1196
|
+
setTimeout(async ()=> {
|
|
1197
|
+
if(this.#dbstoreData.has(id) && !this.#connections.has(id)){
|
|
1198
|
+
this.#dbstoreData.delete(id)
|
|
1199
|
+
await this.#dbstore.delete(new Key(id))
|
|
1200
|
+
}
|
|
1201
|
+
},60*1000)
|
|
1202
|
+
}
|
|
1203
|
+
continue
|
|
1204
|
+
}
|
|
994
1205
|
}
|
|
995
1206
|
else{
|
|
996
1207
|
this.#connectionTrackerStore.set(id,0)
|
|
@@ -1005,7 +1216,7 @@ class webpeerjs{
|
|
|
1005
1216
|
//connect to good peer address if it is disconnected
|
|
1006
1217
|
const goods = Array.from(this.#dialedGoodPeers.keys())
|
|
1007
1218
|
for(const id of goods){
|
|
1008
|
-
if(
|
|
1219
|
+
if(this.#isConnected(id)){
|
|
1009
1220
|
this.#dialedGoodPeers.set(id,0)
|
|
1010
1221
|
continue
|
|
1011
1222
|
}
|
|
@@ -1078,6 +1289,10 @@ class webpeerjs{
|
|
|
1078
1289
|
|
|
1079
1290
|
//dial based on known bootsrap peers address
|
|
1080
1291
|
#dialKnownBootstrap(){
|
|
1292
|
+
|
|
1293
|
+
if(!navigator.onLine)return
|
|
1294
|
+
if(!this.#isDialEnabled)return
|
|
1295
|
+
|
|
1081
1296
|
const bootstrap = config.CONFIG_KNOWN_BOOTSTRAP_PEERS_ADDRS
|
|
1082
1297
|
for(const peer of bootstrap){
|
|
1083
1298
|
const address = peer.Peers[0].Addrs
|
|
@@ -1100,6 +1315,10 @@ class webpeerjs{
|
|
|
1100
1315
|
}
|
|
1101
1316
|
|
|
1102
1317
|
async #dialSavedKnownID(){
|
|
1318
|
+
|
|
1319
|
+
if(!navigator.onLine)return
|
|
1320
|
+
if(!this.#isDialEnabled)return
|
|
1321
|
+
|
|
1103
1322
|
let firsttime = true
|
|
1104
1323
|
for(const target of config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS){
|
|
1105
1324
|
if(this.#dbstoreData.has(target)){
|
|
@@ -1122,6 +1341,7 @@ class webpeerjs{
|
|
|
1122
1341
|
const api = config.CONFIG_DELEGATED_API
|
|
1123
1342
|
const delegatedClient = createDelegatedRoutingV1HttpApiClient(api)
|
|
1124
1343
|
const peer = await first(delegatedClient.getPeers(peerIdFromString(target)))
|
|
1344
|
+
if(!peer)continue
|
|
1125
1345
|
const address = peer.Addrs
|
|
1126
1346
|
const id = peer.ID
|
|
1127
1347
|
let mddrs = []
|
|
@@ -1142,16 +1362,26 @@ class webpeerjs{
|
|
|
1142
1362
|
}
|
|
1143
1363
|
|
|
1144
1364
|
async #dialUpdateSavedKnownID(){
|
|
1365
|
+
|
|
1366
|
+
if(!navigator.onLine)return
|
|
1367
|
+
if(!this.#isDialEnabled)return
|
|
1368
|
+
|
|
1145
1369
|
let firsttime = true
|
|
1146
1370
|
for(const target of config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS){
|
|
1147
1371
|
if(this.#dbstoreData.has(target)){
|
|
1148
1372
|
firsttime = false
|
|
1149
1373
|
}
|
|
1150
|
-
if(!this.#connections.has(target) &&
|
|
1374
|
+
if(!this.#connections.has(target) && (this.#dbstoreData.has(target) || firsttime)){
|
|
1151
1375
|
//console.log('#dialUpdateSavedKnownID()',target)
|
|
1152
1376
|
const api = config.CONFIG_DELEGATED_API
|
|
1153
1377
|
const delegatedClient = createDelegatedRoutingV1HttpApiClient(api)
|
|
1154
1378
|
const peer = await first(delegatedClient.getPeers(peerIdFromString(target)))
|
|
1379
|
+
if(!peer){
|
|
1380
|
+
if (navigator.onLine) {
|
|
1381
|
+
await this.#dbstore.delete(new Key(target))
|
|
1382
|
+
}
|
|
1383
|
+
continue
|
|
1384
|
+
}
|
|
1155
1385
|
const address = peer.Addrs
|
|
1156
1386
|
const id = peer.ID
|
|
1157
1387
|
let mddrs = []
|
|
@@ -1174,6 +1404,10 @@ class webpeerjs{
|
|
|
1174
1404
|
|
|
1175
1405
|
//dial based on known peers ID
|
|
1176
1406
|
async #dialKnownID(){
|
|
1407
|
+
|
|
1408
|
+
if(!navigator.onLine)return
|
|
1409
|
+
if(!this.#isDialEnabled)return
|
|
1410
|
+
|
|
1177
1411
|
//console.log('#dialKnownID()')
|
|
1178
1412
|
const api = config.CONFIG_DELEGATED_API
|
|
1179
1413
|
const delegatedClient = createDelegatedRoutingV1HttpApiClient(api)
|