webpeerjs 0.0.7 → 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 +42 -22
- package/dist/esm/webpeerjs.d.ts +4 -3
- package/dist/esm/webpeerjs.js +356 -131
- package/dist/umd/webpeerjs.js +15381 -15160
- package/package.json +11 -14
- package/src/config.js +8 -7
- package/src/utils.js +19 -8
- package/src/webpeerjs.js +348 -122
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
|
|
@@ -120,6 +128,8 @@ class webpeerjs{
|
|
|
120
128
|
this.status = (function(libp2p) {
|
|
121
129
|
return libp2p.status
|
|
122
130
|
})(this.#libp2p);
|
|
131
|
+
|
|
132
|
+
this.status = 'unconnected'
|
|
123
133
|
|
|
124
134
|
this.IPFS = (function(libp2p,discoveredPeers) {
|
|
125
135
|
const obj = {libp2p,discoveredPeers}
|
|
@@ -141,7 +151,7 @@ class webpeerjs{
|
|
|
141
151
|
const connect = connections.find((con)=>con.id == id)
|
|
142
152
|
const addr = connect.addr
|
|
143
153
|
|
|
144
|
-
if(config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id)
|
|
154
|
+
if(config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id)){
|
|
145
155
|
if(!this.#connections.has(id)&&addr.includes('webtransport')){
|
|
146
156
|
await this.#dbstore.put(new Key(id), new TextEncoder().encode(addr))
|
|
147
157
|
}
|
|
@@ -175,11 +185,7 @@ class webpeerjs{
|
|
|
175
185
|
const now = new Date().getTime()
|
|
176
186
|
const metadata = {addrs:address,last:now}
|
|
177
187
|
this.#connectedPeers.set(id,metadata)
|
|
178
|
-
this.#
|
|
179
|
-
for(const peer of this.#connectedPeers){
|
|
180
|
-
const item = {id:peer[0],address:peer[1].addrs}
|
|
181
|
-
this.#connectedPeersArr.push(item)
|
|
182
|
-
}
|
|
188
|
+
this.#updatePeers()
|
|
183
189
|
}
|
|
184
190
|
|
|
185
191
|
}
|
|
@@ -220,11 +226,7 @@ class webpeerjs{
|
|
|
220
226
|
const now = new Date().getTime()
|
|
221
227
|
const metadata = {addrs:address,last:now}
|
|
222
228
|
this.#connectedPeers.set(senderPeerId,metadata)
|
|
223
|
-
this.#
|
|
224
|
-
for(const peer of this.#connectedPeers){
|
|
225
|
-
const item = {id:peer[0],address:peer[1].addrs}
|
|
226
|
-
this.#connectedPeersArr.push(item)
|
|
227
|
-
}
|
|
229
|
+
this.#updatePeers()
|
|
228
230
|
}
|
|
229
231
|
|
|
230
232
|
//dial if not connected
|
|
@@ -288,11 +290,7 @@ class webpeerjs{
|
|
|
288
290
|
const metadata = {addrs:address,last:now}
|
|
289
291
|
this.#connectedPeers.set(id,metadata)
|
|
290
292
|
this.#webPeersAddrs.set(id,address)
|
|
291
|
-
this.#
|
|
292
|
-
for(const peer of this.#connectedPeers){
|
|
293
|
-
const item = {id:peer[0],address:peer[1].addrs}
|
|
294
|
-
this.#connectedPeersArr.push(item)
|
|
295
|
-
}
|
|
293
|
+
this.#updatePeers()
|
|
296
294
|
}
|
|
297
295
|
|
|
298
296
|
|
|
@@ -338,17 +336,6 @@ class webpeerjs{
|
|
|
338
336
|
if(signal == 'ping'){
|
|
339
337
|
//console.log('rooms',rooms)
|
|
340
338
|
}
|
|
341
|
-
|
|
342
|
-
//update connected webpeers
|
|
343
|
-
/*const now = new Date().getTime()
|
|
344
|
-
const metadata = {addrs:address,last:now}
|
|
345
|
-
this.#connectedPeers.set(id,metadata)
|
|
346
|
-
this.#webPeersAddrs.set(id,address)
|
|
347
|
-
this.#connectedPeersArr.length = 0
|
|
348
|
-
for(const peer of this.#connectedPeers){
|
|
349
|
-
const item = {id:peer[0],address:peer[1].addrs}
|
|
350
|
-
this.#connectedPeersArr.push(item)
|
|
351
|
-
}*/
|
|
352
339
|
|
|
353
340
|
}
|
|
354
341
|
}
|
|
@@ -431,9 +418,8 @@ class webpeerjs{
|
|
|
431
418
|
count++
|
|
432
419
|
this.#trackDisconnect.set(id,count)
|
|
433
420
|
//console.log(this.#trackDisconnect)
|
|
434
|
-
if(count>
|
|
421
|
+
if(count>10){
|
|
435
422
|
if(this.#dbstoreData.has(id)){
|
|
436
|
-
//await this.#dbstore.delete(new Key(id))
|
|
437
423
|
this.#dbstoreData.delete(id)
|
|
438
424
|
}
|
|
439
425
|
|
|
@@ -446,6 +432,11 @@ class webpeerjs{
|
|
|
446
432
|
this.#trackDisconnect.set(id,0)
|
|
447
433
|
}
|
|
448
434
|
|
|
435
|
+
let peerexchangelist = []
|
|
436
|
+
for(const peer of this.#peerexchangedata.values()){
|
|
437
|
+
peerexchangelist.push(peer.id)
|
|
438
|
+
}
|
|
439
|
+
|
|
449
440
|
//if this disconnected peer is web peer redial it
|
|
450
441
|
if(this.#webPeersId.includes(id)){
|
|
451
442
|
const addr = this.#connections.get(id)
|
|
@@ -463,6 +454,14 @@ class webpeerjs{
|
|
|
463
454
|
mddrs.push(addrs)
|
|
464
455
|
this.#dialMultiaddress(mddrs)
|
|
465
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
|
+
}
|
|
466
465
|
|
|
467
466
|
//redial if this disconnected peer is regular peer
|
|
468
467
|
else{
|
|
@@ -495,38 +494,32 @@ class webpeerjs{
|
|
|
495
494
|
this.#libp2p.addEventListener('peer:identify', (evt) => {
|
|
496
495
|
//console.log('peer:identify '+evt.detail.peerId.toString(),evt.detail)
|
|
497
496
|
if(evt.detail.protocols.includes(config.CONFIG_PROTOCOL)){
|
|
498
|
-
//console.log('peer:identify '+evt.detail.peerId.toString(),evt
|
|
497
|
+
//console.log('peer:identify '+evt.detail.peerId.toString(),evt)
|
|
499
498
|
|
|
500
499
|
const id = evt.detail.peerId.toString()
|
|
501
500
|
let address = []
|
|
501
|
+
let mddrs = []
|
|
502
502
|
|
|
503
503
|
for(const addrs of evt.detail.listenAddrs){
|
|
504
504
|
const addr = addrs.toString()+'/p2p/'+id
|
|
505
|
+
const mddr = multiaddr(addr)
|
|
505
506
|
if(addr.includes('webtransport')){
|
|
506
507
|
address.push(addr)
|
|
508
|
+
mddrs.push(mddr)
|
|
507
509
|
}
|
|
508
510
|
}
|
|
509
511
|
|
|
510
|
-
if(!this.#webPeersId.includes(id))this.#webPeersId.push(id)
|
|
511
|
-
|
|
512
512
|
if(this.#connectedPeers.has(id)){
|
|
513
513
|
//reset this last seen
|
|
514
514
|
const now = new Date().getTime()
|
|
515
515
|
const metadata = {addrs:address,last:now}
|
|
516
516
|
this.#connectedPeers.set(id,metadata)
|
|
517
517
|
}
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
this.#connectedPeers.set(id,metadata)
|
|
524
|
-
this.#connectedPeersArr.length = 0
|
|
525
|
-
for(const peer of this.#connectedPeers){
|
|
526
|
-
const item = {id:peer[0],address:peer[1].addrs}
|
|
527
|
-
this.#connectedPeersArr.push(item)
|
|
528
|
-
}
|
|
529
|
-
}
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
const command = 'peer-exchange'
|
|
521
|
+
this.#dialProtocol(id,command)
|
|
522
|
+
|
|
530
523
|
|
|
531
524
|
}
|
|
532
525
|
})
|
|
@@ -607,28 +600,178 @@ class webpeerjs{
|
|
|
607
600
|
|
|
608
601
|
//Listen on new peer connection
|
|
609
602
|
#onConnectFn = () => {}
|
|
610
|
-
|
|
603
|
+
onConnect = f => (this.#onConnectFn = f)
|
|
611
604
|
|
|
612
605
|
|
|
613
606
|
//Listen on peer disconnect
|
|
614
607
|
#onDisconnectFn = () => {}
|
|
615
|
-
|
|
608
|
+
onDisconnect = f => (this.#onDisconnectFn = f)
|
|
616
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
|
+
|
|
617
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
|
+
}
|
|
618
664
|
|
|
619
665
|
|
|
620
666
|
/*
|
|
621
667
|
PRIVATE FUNCTION
|
|
622
668
|
*/
|
|
669
|
+
|
|
670
|
+
#updatePeers(){
|
|
671
|
+
this.#connectedPeersArr.length = 0
|
|
672
|
+
for(const peer of this.#connectedPeers){
|
|
673
|
+
const item = {id:peer[0],address:peer[1].addrs}
|
|
674
|
+
this.#connectedPeersArr.push(item)
|
|
675
|
+
}
|
|
676
|
+
if(this.#connectedPeers.size > 0){
|
|
677
|
+
this.status = 'connected'
|
|
678
|
+
}
|
|
679
|
+
else{
|
|
680
|
+
this.status = 'unconnected'
|
|
681
|
+
}
|
|
682
|
+
}
|
|
623
683
|
|
|
624
684
|
async #registerProtocol(){
|
|
625
|
-
|
|
626
|
-
|
|
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
|
+
}
|
|
627
769
|
}
|
|
628
770
|
|
|
629
771
|
await this.#libp2p.handle(config.CONFIG_PROTOCOL, handler, {
|
|
630
|
-
maxInboundStreams:
|
|
631
|
-
maxOutboundStreams:
|
|
772
|
+
maxInboundStreams: 50,
|
|
773
|
+
maxOutboundStreams: 50,
|
|
774
|
+
runOnTransientConnection:true
|
|
632
775
|
})
|
|
633
776
|
|
|
634
777
|
await this.#libp2p.register(config.CONFIG_PROTOCOL, {
|
|
@@ -644,6 +787,96 @@ class webpeerjs{
|
|
|
644
787
|
|
|
645
788
|
}
|
|
646
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
|
+
|
|
647
880
|
#findHybridPeer(){
|
|
648
881
|
setTimeout(async()=>{
|
|
649
882
|
for(const target of config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
|
|
@@ -681,6 +914,7 @@ class webpeerjs{
|
|
|
681
914
|
//check the last seen in web peer
|
|
682
915
|
#trackLastSeen(){
|
|
683
916
|
const timeout = 25*1000
|
|
917
|
+
const forcetimeout = 60*1000
|
|
684
918
|
const now = new Date().getTime()
|
|
685
919
|
|
|
686
920
|
//if webpeer last seen grather then timeout send onDisconnect
|
|
@@ -688,13 +922,10 @@ class webpeerjs{
|
|
|
688
922
|
const id = peer[0]
|
|
689
923
|
const last = peer[1].last
|
|
690
924
|
const time = now-last
|
|
691
|
-
if(time>timeout){
|
|
925
|
+
if((time>timeout && !this.#isConnected(id))||(time>forcetimeout)){
|
|
926
|
+
|
|
692
927
|
this.#connectedPeers.delete(id)
|
|
693
|
-
this.#
|
|
694
|
-
for(const peer of this.#connectedPeers){
|
|
695
|
-
const item = {id:peer[0],address:peer[1].addrs}
|
|
696
|
-
this.#connectedPeersArr.push(item)
|
|
697
|
-
}
|
|
928
|
+
this.#updatePeers()
|
|
698
929
|
this.#onDisconnectFn(id)
|
|
699
930
|
|
|
700
931
|
//remove id from room member
|
|
@@ -706,6 +937,7 @@ class webpeerjs{
|
|
|
706
937
|
this.#rooms[room].onMembers(this.#rooms[room].members)
|
|
707
938
|
}
|
|
708
939
|
}
|
|
940
|
+
|
|
709
941
|
}
|
|
710
942
|
}
|
|
711
943
|
}
|
|
@@ -731,13 +963,25 @@ class webpeerjs{
|
|
|
731
963
|
|
|
732
964
|
const id = mddrs[0].toString().split('/').pop()
|
|
733
965
|
|
|
734
|
-
const
|
|
966
|
+
const queueids = this.#dialQueue.map((arr)=> arr[0].toString().split('/').pop())
|
|
735
967
|
|
|
736
968
|
//if peer id is already in the queque cancel queque
|
|
737
|
-
if(
|
|
969
|
+
if(queueids.includes(id)){
|
|
738
970
|
return
|
|
739
971
|
}
|
|
740
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
|
+
|
|
741
985
|
if(this.#webPeersId.includes(id) || config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id) || config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
|
|
742
986
|
this.#dialQueue.unshift(mddrs)
|
|
743
987
|
}
|
|
@@ -751,7 +995,7 @@ class webpeerjs{
|
|
|
751
995
|
//dial multiaddr address in queue list
|
|
752
996
|
#dialQueueList(){
|
|
753
997
|
|
|
754
|
-
if(!this.#isDialEnabled)return
|
|
998
|
+
if(!this.#isDialEnabled || !navigator.onLine)return
|
|
755
999
|
|
|
756
1000
|
const mddrsToDial = 5
|
|
757
1001
|
|
|
@@ -816,55 +1060,6 @@ class webpeerjs{
|
|
|
816
1060
|
await this.#libp2p.services.pubsub.publish(topic, encodedPeer)
|
|
817
1061
|
}
|
|
818
1062
|
}
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
joinRoom = room => {
|
|
822
|
-
if (this.#rooms[room]) {
|
|
823
|
-
return [
|
|
824
|
-
this.#rooms[room].sendMessage,
|
|
825
|
-
this.#rooms[room].listenMessage,
|
|
826
|
-
this.#rooms[room].onMembersChange
|
|
827
|
-
]
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
}
|
|
831
|
-
|
|
832
|
-
if (!room) {
|
|
833
|
-
throw mkErr('room is required')
|
|
834
|
-
}
|
|
835
|
-
|
|
836
|
-
//join room version 1 user pupsub via pupsub peer discovery
|
|
837
|
-
if(config.CONFIG_JOIN_ROOM_VERSION == 1){
|
|
838
|
-
|
|
839
|
-
const topics = config.CONFIG_PUBSUB_PEER_DISCOVERY
|
|
840
|
-
|
|
841
|
-
this.#rooms[room] = {
|
|
842
|
-
onMessage : () => {},
|
|
843
|
-
listenMessage : f => (this.#rooms[room] = {...this.#rooms[room], onMessage: f}),
|
|
844
|
-
sendMessage : async (message) => {
|
|
845
|
-
const msgId = (new Date()).getTime()
|
|
846
|
-
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,room,message,id:this.#libp2p.peerId.toString(),msgId})
|
|
847
|
-
const peer = {
|
|
848
|
-
publicKey: this.#libp2p.peerId.publicKey,
|
|
849
|
-
addrs: [uint8ArrayFromString(data)],
|
|
850
|
-
}
|
|
851
|
-
const encodedPeer = PBPeer.encode(peer)
|
|
852
|
-
for(const topic of topics){
|
|
853
|
-
await this.#libp2p.services.pubsub.publish(topic, encodedPeer)
|
|
854
|
-
}
|
|
855
|
-
},
|
|
856
|
-
members : [this.id],
|
|
857
|
-
onMembers : () => {},
|
|
858
|
-
onMembersChange : f => {this.#rooms[room] = {...this.#rooms[room], onMembers: f};this.#rooms[room].onMembers(this.#rooms[room].members);this.#ping()},
|
|
859
|
-
}
|
|
860
|
-
}
|
|
861
|
-
|
|
862
|
-
return [
|
|
863
|
-
this.#rooms[room].sendMessage,
|
|
864
|
-
this.#rooms[room].listenMessage,
|
|
865
|
-
this.#rooms[room].onMembersChange
|
|
866
|
-
]
|
|
867
|
-
}
|
|
868
1063
|
|
|
869
1064
|
|
|
870
1065
|
//dial discovered peers
|
|
@@ -967,8 +1162,7 @@ class webpeerjs{
|
|
|
967
1162
|
if(besttime>bestlimit){
|
|
968
1163
|
const addr = remote.toString()
|
|
969
1164
|
const id = peer.toString()
|
|
970
|
-
if(!this.#webPeersId.includes(id) && !config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id) && !this.#dbstoreData.
|
|
971
|
-
//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')){
|
|
972
1166
|
await this.#dbstore.put(new Key(id), new TextEncoder().encode(addr))
|
|
973
1167
|
this.#dbstoreData.set(id,addr)
|
|
974
1168
|
}
|
|
@@ -983,26 +1177,31 @@ class webpeerjs{
|
|
|
983
1177
|
}
|
|
984
1178
|
|
|
985
1179
|
|
|
986
|
-
let peers = []
|
|
987
|
-
for(const peer of this.#libp2p.getPeers()){
|
|
988
|
-
peers.push(peer.toString())
|
|
989
|
-
}
|
|
990
|
-
|
|
991
|
-
|
|
992
1180
|
//connect to saved best peer address
|
|
993
1181
|
//working great
|
|
994
1182
|
for(const peer of this.#dbstoreData){
|
|
995
1183
|
const id = peer[0]
|
|
996
1184
|
const addr = peer[1]
|
|
997
|
-
if(
|
|
1185
|
+
if(this.#isConnected(id)){
|
|
998
1186
|
this.#connectionTrackerStore.set(id,0)
|
|
999
1187
|
continue
|
|
1000
1188
|
}else{
|
|
1001
1189
|
if(this.#connectionTrackerStore.has(id)){
|
|
1002
1190
|
let current = this.#connectionTrackerStore.get(id)
|
|
1003
|
-
if(current>10)continue
|
|
1004
1191
|
current++
|
|
1005
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
|
+
}
|
|
1006
1205
|
}
|
|
1007
1206
|
else{
|
|
1008
1207
|
this.#connectionTrackerStore.set(id,0)
|
|
@@ -1017,7 +1216,7 @@ class webpeerjs{
|
|
|
1017
1216
|
//connect to good peer address if it is disconnected
|
|
1018
1217
|
const goods = Array.from(this.#dialedGoodPeers.keys())
|
|
1019
1218
|
for(const id of goods){
|
|
1020
|
-
if(
|
|
1219
|
+
if(this.#isConnected(id)){
|
|
1021
1220
|
this.#dialedGoodPeers.set(id,0)
|
|
1022
1221
|
continue
|
|
1023
1222
|
}
|
|
@@ -1090,6 +1289,10 @@ class webpeerjs{
|
|
|
1090
1289
|
|
|
1091
1290
|
//dial based on known bootsrap peers address
|
|
1092
1291
|
#dialKnownBootstrap(){
|
|
1292
|
+
|
|
1293
|
+
if(!navigator.onLine)return
|
|
1294
|
+
if(!this.#isDialEnabled)return
|
|
1295
|
+
|
|
1093
1296
|
const bootstrap = config.CONFIG_KNOWN_BOOTSTRAP_PEERS_ADDRS
|
|
1094
1297
|
for(const peer of bootstrap){
|
|
1095
1298
|
const address = peer.Peers[0].Addrs
|
|
@@ -1112,6 +1315,10 @@ class webpeerjs{
|
|
|
1112
1315
|
}
|
|
1113
1316
|
|
|
1114
1317
|
async #dialSavedKnownID(){
|
|
1318
|
+
|
|
1319
|
+
if(!navigator.onLine)return
|
|
1320
|
+
if(!this.#isDialEnabled)return
|
|
1321
|
+
|
|
1115
1322
|
let firsttime = true
|
|
1116
1323
|
for(const target of config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS){
|
|
1117
1324
|
if(this.#dbstoreData.has(target)){
|
|
@@ -1134,6 +1341,7 @@ class webpeerjs{
|
|
|
1134
1341
|
const api = config.CONFIG_DELEGATED_API
|
|
1135
1342
|
const delegatedClient = createDelegatedRoutingV1HttpApiClient(api)
|
|
1136
1343
|
const peer = await first(delegatedClient.getPeers(peerIdFromString(target)))
|
|
1344
|
+
if(!peer)continue
|
|
1137
1345
|
const address = peer.Addrs
|
|
1138
1346
|
const id = peer.ID
|
|
1139
1347
|
let mddrs = []
|
|
@@ -1154,12 +1362,26 @@ class webpeerjs{
|
|
|
1154
1362
|
}
|
|
1155
1363
|
|
|
1156
1364
|
async #dialUpdateSavedKnownID(){
|
|
1365
|
+
|
|
1366
|
+
if(!navigator.onLine)return
|
|
1367
|
+
if(!this.#isDialEnabled)return
|
|
1368
|
+
|
|
1369
|
+
let firsttime = true
|
|
1157
1370
|
for(const target of config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS){
|
|
1158
|
-
if(
|
|
1371
|
+
if(this.#dbstoreData.has(target)){
|
|
1372
|
+
firsttime = false
|
|
1373
|
+
}
|
|
1374
|
+
if(!this.#connections.has(target) && (this.#dbstoreData.has(target) || firsttime)){
|
|
1159
1375
|
//console.log('#dialUpdateSavedKnownID()',target)
|
|
1160
1376
|
const api = config.CONFIG_DELEGATED_API
|
|
1161
1377
|
const delegatedClient = createDelegatedRoutingV1HttpApiClient(api)
|
|
1162
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
|
+
}
|
|
1163
1385
|
const address = peer.Addrs
|
|
1164
1386
|
const id = peer.ID
|
|
1165
1387
|
let mddrs = []
|
|
@@ -1182,6 +1404,10 @@ class webpeerjs{
|
|
|
1182
1404
|
|
|
1183
1405
|
//dial based on known peers ID
|
|
1184
1406
|
async #dialKnownID(){
|
|
1407
|
+
|
|
1408
|
+
if(!navigator.onLine)return
|
|
1409
|
+
if(!this.#isDialEnabled)return
|
|
1410
|
+
|
|
1185
1411
|
//console.log('#dialKnownID()')
|
|
1186
1412
|
const api = config.CONFIG_DELEGATED_API
|
|
1187
1413
|
const delegatedClient = createDelegatedRoutingV1HttpApiClient(api)
|