webpeerjs 0.1.8 → 0.1.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/dist/esm/webpeerjs.js +1 -1
- package/dist/umd/webpeerjs.js +1 -1
- package/package.json +1 -1
- package/src/config.js +1 -1
- package/src/webpeerjs.js +109 -77
package/package.json
CHANGED
package/src/config.js
CHANGED
|
@@ -6,7 +6,7 @@ export const CONFIG_PROTOCOL = '/'+prefix+'/1.0.0'
|
|
|
6
6
|
export const CONFIG_BLOCKSTORE_PATH = prefix+'-blockstore'
|
|
7
7
|
export const CONFIG_DATASTORE_PATH = prefix+'-datastore'
|
|
8
8
|
export const CONFIG_DBSTORE_PATH = prefix+'-dbstore'
|
|
9
|
-
export const CONFIG_MAX_CONNECTIONS =
|
|
9
|
+
export const CONFIG_MAX_CONNECTIONS = 30
|
|
10
10
|
export const CONFIG_MIN_CONNECTIONS = 0
|
|
11
11
|
export const CONFIG_DISCOVER_RELAYS = 1
|
|
12
12
|
export const CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY = 'universal-connectivity-browser-peer-discovery'
|
package/src/webpeerjs.js
CHANGED
|
@@ -217,18 +217,22 @@ class webpeerjs{
|
|
|
217
217
|
let address = [addr]
|
|
218
218
|
|
|
219
219
|
if(this.#connectedPeers.has(id)){
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
220
|
+
if(this.#lastTimeReceiveData < 10*1000){
|
|
221
|
+
//reset this last seen
|
|
222
|
+
const now = new Date().getTime()
|
|
223
|
+
const metadata = {addrs:address,last:now}
|
|
224
|
+
this.#connectedPeers.set(id,metadata)
|
|
225
|
+
}
|
|
224
226
|
}
|
|
225
|
-
else
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
227
|
+
else {
|
|
228
|
+
if(this.#lastTimeReceiveData < 10*1000){
|
|
229
|
+
//add to connected webpeers
|
|
230
|
+
this.#onConnectFnUpdate(id)
|
|
231
|
+
const now = new Date().getTime()
|
|
232
|
+
const metadata = {addrs:address,last:now}
|
|
233
|
+
this.#connectedPeers.set(id,metadata)
|
|
234
|
+
this.#updatePeers()
|
|
235
|
+
}
|
|
232
236
|
}
|
|
233
237
|
|
|
234
238
|
}
|
|
@@ -273,20 +277,24 @@ class webpeerjs{
|
|
|
273
277
|
if(this.#webPeersId.includes(senderPeerId)){
|
|
274
278
|
|
|
275
279
|
if(this.#connectedPeers.has(senderPeerId)){
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
280
|
+
if(this.#lastTimeReceiveData < 10*1000){
|
|
281
|
+
//reset this last seen
|
|
282
|
+
const address = this.#connectedPeers.get(senderPeerId).addrs
|
|
283
|
+
const now = new Date().getTime()
|
|
284
|
+
const metadata = {addrs:address,last:now}
|
|
285
|
+
this.#connectedPeers.set(senderPeerId,metadata)
|
|
286
|
+
}
|
|
281
287
|
}
|
|
282
|
-
else
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
288
|
+
else {
|
|
289
|
+
if(this.#lastTimeReceiveData < 10*1000){
|
|
290
|
+
//add to connected webpeers
|
|
291
|
+
this.#onConnectFnUpdate(senderPeerId)
|
|
292
|
+
const address = this.#webPeersAddrs.get(senderPeerId)
|
|
293
|
+
const now = new Date().getTime()
|
|
294
|
+
const metadata = {addrs:address,last:now}
|
|
295
|
+
this.#connectedPeers.set(senderPeerId,metadata)
|
|
296
|
+
this.#updatePeers()
|
|
297
|
+
}
|
|
290
298
|
}
|
|
291
299
|
|
|
292
300
|
//dial if not connected
|
|
@@ -374,6 +382,13 @@ class webpeerjs{
|
|
|
374
382
|
}
|
|
375
383
|
this.#updatePeers()
|
|
376
384
|
}else{
|
|
385
|
+
//reset this last seen
|
|
386
|
+
let addr = this.#connectedPeers.get(id).addrs
|
|
387
|
+
if(address)addr = address
|
|
388
|
+
const now = new Date().getTime()
|
|
389
|
+
const metadata = {addrs:addr,last:now}
|
|
390
|
+
this.#connectedPeers.set(id,metadata)
|
|
391
|
+
//update address
|
|
377
392
|
if(address){
|
|
378
393
|
if(address.length > 0){
|
|
379
394
|
this.#webPeersAddrs.set(id,address)
|
|
@@ -1116,6 +1131,7 @@ class webpeerjs{
|
|
|
1116
1131
|
|
|
1117
1132
|
if(!navigator.onLine)return
|
|
1118
1133
|
if(!this.#isDialEnabled)return
|
|
1134
|
+
if(this.#connectedPeers.size > 5)return
|
|
1119
1135
|
|
|
1120
1136
|
for(const target of config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
|
|
1121
1137
|
if(!this.#isConnected(target) && !this.#connections.has(target)){
|
|
@@ -1150,7 +1166,7 @@ class webpeerjs{
|
|
|
1150
1166
|
|
|
1151
1167
|
//check the last seen in web peer
|
|
1152
1168
|
#trackLastSeen(){
|
|
1153
|
-
const timeout =
|
|
1169
|
+
const timeout = 20*1000
|
|
1154
1170
|
const forcetimeout = 60*1000
|
|
1155
1171
|
const now = new Date().getTime()
|
|
1156
1172
|
|
|
@@ -1160,7 +1176,7 @@ class webpeerjs{
|
|
|
1160
1176
|
const last = peer[1].last
|
|
1161
1177
|
const time = now-last
|
|
1162
1178
|
if((time>timeout && !this.#isConnected(id))||(time>forcetimeout)){
|
|
1163
|
-
|
|
1179
|
+
//console.log('time',time)
|
|
1164
1180
|
this.#connectedPeers.delete(id)
|
|
1165
1181
|
this.#updatePeers()
|
|
1166
1182
|
this.#onDisconnectFn(id)
|
|
@@ -1221,20 +1237,11 @@ class webpeerjs{
|
|
|
1221
1237
|
}
|
|
1222
1238
|
else{
|
|
1223
1239
|
if((nodePeerCount>(limitCount-5))||(allPeerCount>(config.CONFIG_MAX_CONNECTIONS-5))){
|
|
1224
|
-
|
|
1225
|
-
let peers = []
|
|
1226
|
-
for(const peer of this.#libp2p.getPeers()){
|
|
1227
|
-
peers.push(peer.toString())
|
|
1228
|
-
}
|
|
1229
|
-
const randomKey = Math.floor(Math.random() * peers.length)
|
|
1230
|
-
const randompeerid = peers[randomKey]
|
|
1231
|
-
if(!config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
|
|
1232
|
-
await this.#libp2p.hangUp(peerIdFromString(randompeerid))
|
|
1233
|
-
}
|
|
1240
|
+
return
|
|
1234
1241
|
}
|
|
1235
1242
|
}
|
|
1236
1243
|
|
|
1237
|
-
if(this.#webPeersId.includes(id) || config.
|
|
1244
|
+
if(this.#webPeersId.includes(id) || config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
|
|
1238
1245
|
this.#dialQueue.unshift(mddrs)
|
|
1239
1246
|
}
|
|
1240
1247
|
else{
|
|
@@ -1416,19 +1423,9 @@ class webpeerjs{
|
|
|
1416
1423
|
}
|
|
1417
1424
|
}
|
|
1418
1425
|
},45*1000)
|
|
1419
|
-
}
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
//track for good connection
|
|
1423
|
-
async #connectionTracker(){
|
|
1424
|
-
|
|
1425
|
-
for await (const { key, value } of this.#dbstore.query({})) {
|
|
1426
|
-
const id = key.toString().split('/')[1]
|
|
1427
|
-
const addr = new TextDecoder().decode(value)
|
|
1428
|
-
this.#dbstoreData.set(id,addr)
|
|
1429
|
-
}
|
|
1426
|
+
}
|
|
1430
1427
|
|
|
1431
|
-
|
|
1428
|
+
async #checkConnectionAll(){
|
|
1432
1429
|
|
|
1433
1430
|
//save peer address if connection is good
|
|
1434
1431
|
const connections = this.#libp2p.getConnections()
|
|
@@ -1456,12 +1453,26 @@ class webpeerjs{
|
|
|
1456
1453
|
|
|
1457
1454
|
}
|
|
1458
1455
|
|
|
1456
|
+
const shuffle = (array) => {
|
|
1457
|
+
for (let i = array.length - 1; i > 0; i--) {
|
|
1458
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
1459
|
+
[array[i], array[j]] = [array[j], array[i]];
|
|
1460
|
+
}
|
|
1461
|
+
return array;
|
|
1462
|
+
};
|
|
1459
1463
|
|
|
1460
1464
|
//connect to saved best peer address
|
|
1461
1465
|
//working great
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1466
|
+
const bestall = Array.from(this.#dbstoreData.keys())
|
|
1467
|
+
const bestshuffle = shuffle(bestall)
|
|
1468
|
+
const best = bestshuffle.slice(0, 10)
|
|
1469
|
+
for(const hybrid of config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
|
|
1470
|
+
if(best.includes(hybrid))continue
|
|
1471
|
+
if(!bestall.includes(hybrid))continue
|
|
1472
|
+
best.unshift(hybrid)
|
|
1473
|
+
}
|
|
1474
|
+
for(const id of best){
|
|
1475
|
+
const addr = this.#dbstoreData.get(id)
|
|
1465
1476
|
if(this.#isConnected(id)){
|
|
1466
1477
|
this.#connectionTrackerStore.set(id,0)
|
|
1467
1478
|
continue
|
|
@@ -1489,6 +1500,7 @@ class webpeerjs{
|
|
|
1489
1500
|
let mddrs = []
|
|
1490
1501
|
const mddr = multiaddr(addr)
|
|
1491
1502
|
mddrs.push(mddr)
|
|
1503
|
+
//console.log(id,addr)
|
|
1492
1504
|
this.#dialMultiaddress(mddrs)
|
|
1493
1505
|
}
|
|
1494
1506
|
}
|
|
@@ -1514,7 +1526,20 @@ class webpeerjs{
|
|
|
1514
1526
|
}
|
|
1515
1527
|
}
|
|
1516
1528
|
}
|
|
1517
|
-
|
|
1529
|
+
}
|
|
1530
|
+
//track for good connection
|
|
1531
|
+
async #connectionTracker(){
|
|
1532
|
+
|
|
1533
|
+
for await (const { key, value } of this.#dbstore.query({})) {
|
|
1534
|
+
const id = key.toString().split('/')[1]
|
|
1535
|
+
const addr = new TextDecoder().decode(value)
|
|
1536
|
+
this.#dbstoreData.set(id,addr)
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
this.#checkConnectionAll()
|
|
1540
|
+
|
|
1541
|
+
setInterval(async ()=>{
|
|
1542
|
+
this.#checkConnectionAll()
|
|
1518
1543
|
},15*1000)
|
|
1519
1544
|
}
|
|
1520
1545
|
|
|
@@ -1982,40 +2007,47 @@ class webpeerjs{
|
|
|
1982
2007
|
})
|
|
1983
2008
|
}
|
|
1984
2009
|
|
|
1985
|
-
let ice =
|
|
1986
|
-
|
|
1987
|
-
//console.log(ice)
|
|
1988
|
-
|
|
1989
|
-
//recheck ice
|
|
1990
|
-
if(!ice[0] && !ice[1]){
|
|
1991
|
-
ice = await checkice(stunurlsbackup,turnurlsbackup,turnusernamebackup,turncredentialbackup,5000)
|
|
1992
|
-
}else if (ice[0] && !ice[1]){
|
|
1993
|
-
ice = await checkice(stunurls,turnurlsbackup,turnusernamebackup,turncredentialbackup,5000)
|
|
1994
|
-
}else if (!ice[0] && ice[1]){
|
|
1995
|
-
ice = await checkice(stunurlsbackup,turnurls,turnusername,turncredential,5000)
|
|
1996
|
-
}
|
|
1997
|
-
|
|
1998
|
-
//console.log(ice)
|
|
1999
|
-
|
|
2000
|
-
//final ice remove false value
|
|
2001
|
-
ice.forEach(function(value, index) {
|
|
2002
|
-
if(!value){
|
|
2003
|
-
this.splice(index, 1)
|
|
2004
|
-
}
|
|
2005
|
-
}, ice);
|
|
2006
|
-
|
|
2007
|
-
//console.log(ice)
|
|
2010
|
+
let ice = []
|
|
2008
2011
|
|
|
2009
2012
|
let configuration = {}
|
|
2010
2013
|
|
|
2011
2014
|
if(arguments.length > 0){
|
|
2012
2015
|
configuration = arguments[0]
|
|
2013
|
-
}
|
|
2016
|
+
}
|
|
2017
|
+
|
|
2018
|
+
//set rtc configuration
|
|
2019
|
+
if(configuration.rtcConfiguration === undefined){
|
|
2020
|
+
|
|
2021
|
+
ice = await checkice(stunurls,turnurls,turnusername,turncredential,5000)
|
|
2022
|
+
|
|
2023
|
+
//console.log(ice)
|
|
2024
|
+
|
|
2025
|
+
//recheck ice
|
|
2026
|
+
if(!ice[0] && !ice[1]){
|
|
2027
|
+
ice = await checkice(stunurlsbackup,turnurlsbackup,turnusernamebackup,turncredentialbackup,5000)
|
|
2028
|
+
}else if (ice[0] && !ice[1]){
|
|
2029
|
+
ice = await checkice(stunurls,turnurlsbackup,turnusernamebackup,turncredentialbackup,5000)
|
|
2030
|
+
}else if (!ice[0] && ice[1]){
|
|
2031
|
+
ice = await checkice(stunurlsbackup,turnurls,turnusername,turncredential,5000)
|
|
2032
|
+
}
|
|
2033
|
+
|
|
2034
|
+
//console.log(ice)
|
|
2035
|
+
|
|
2036
|
+
//final ice remove false value
|
|
2037
|
+
ice.forEach(function(value, index) {
|
|
2038
|
+
if(!value){
|
|
2039
|
+
this.splice(index, 1)
|
|
2040
|
+
}
|
|
2041
|
+
}, ice);
|
|
2042
|
+
|
|
2043
|
+
//console.log(ice)
|
|
2044
|
+
|
|
2014
2045
|
configuration.rtcConfiguration = {
|
|
2015
2046
|
iceServers: ice,
|
|
2016
2047
|
}
|
|
2017
2048
|
}
|
|
2018
2049
|
|
|
2050
|
+
|
|
2019
2051
|
//create libp2p instance
|
|
2020
2052
|
const libp2p = await createLibp2p({
|
|
2021
2053
|
addresses: {
|