webpeerjs 0.0.5 → 0.0.6

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.
@@ -25,28 +25,32 @@ const CONFIG_MIN_CONNECTIONS = 0;
25
25
  const CONFIG_DISCOVER_RELAYS = 1;
26
26
  const CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY = 'universal-connectivity-browser-peer-discovery';
27
27
  const CONFIG_PEER_DISCOVERY_GLOBAL = '_peer-discovery._p2p._pubsub';
28
- const CONFIG_PUBSUB_PEER_DISCOVERY = [CONFIG_PEER_DISCOVERY_GLOBAL,CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY,prefix$1+'-peer-discovery'];
28
+ const CONFIG_PEER_DISCOVERY_WEBPEERJS= prefix$1+'-peer-discovery';
29
+ const CONFIG_PUBSUB_PEER_DISCOVERY = [CONFIG_PEER_DISCOVERY_GLOBAL, CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY, CONFIG_PEER_DISCOVERY_WEBPEERJS];
29
30
  const CONFIG_DELEGATED_API = 'https://delegated-ipfs.dev';
30
31
 
31
32
  const CONFIG_KNOWN_BOOTSTRAP_PEERS_ADDRS = [
32
33
  ];
33
34
 
34
- const CONFIG_KNOWN_BOOTSTRAP_PEER_IDS = [
35
- '12D3KooWFhXabKDwALpzqMbto94sB7rvmZ6M28hs9Y9xSopDKwQr',
36
- '12D3KooWPEDBmt7vm6FNNYuqaA4n2qMUZ6wPK5NcRc8t6KpqgRkV',
37
- '12D3KooWSHbugDEQeWm2LjtRRMpNgLu6oZ8zkX8XcTwYMAewVekP',
38
- '12D3KooWASoxFpwwy8JDdu4Tm57mhESsnbFPogam9VVmhR95FGXr',
35
+ const CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS = [
39
36
  'QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN',
40
- 'QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa',
41
37
  'QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb',
42
38
  'QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt',
43
39
  'QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
40
+ '12D3KooWFhXabKDwALpzqMbto94sB7rvmZ6M28hs9Y9xSopDKwQr',
41
+ '12D3KooWPEDBmt7vm6FNNYuqaA4n2qMUZ6wPK5NcRc8t6KpqgRkV',
42
+ '12D3KooWSHbugDEQeWm2LjtRRMpNgLu6oZ8zkX8XcTwYMAewVekP',
43
+ '12D3KooWASoxFpwwy8JDdu4Tm57mhESsnbFPogam9VVmhR95FGXr',
44
44
  '12D3KooWHh98YpAkJsn3ULjMjK1n9QVkXmi8Sb3gTDMatHxCmDP5',
45
45
  '12D3KooWS79EhkPU7ESUwgG4vyHHzW9FDNZLoWVth9b5N5NSrvaj',
46
46
  '12D3KooWBbkCD5MpJhMc1mfPAVGEyVkQnyxPKGS7AHwDqQM2JUsk',
47
47
  '12D3KooWKLdecs31Zmo2pLBjR9HY2vWo3VwM4eBm21Czeucbe6FL',
48
48
  '12D3KooWBdF3g6vSJFRPoZQo7BNnkNzaWb59gpyaVzsgtNTVeu8H'
49
- ];
49
+ ];
50
+
51
+ const CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS = [
52
+ '12D3KooWFhXabKDwALpzqMbto94sB7rvmZ6M28hs9Y9xSopDKwQr'
53
+ ];
50
54
 
51
55
  /* eslint-disable import/export */
52
56
  /* eslint-disable complexity */
@@ -181,6 +185,7 @@ let lastStats = {
181
185
 
182
186
  let isDialEnabled = true;
183
187
  let lastfailtreshold = 0;
188
+ let fail = 0;
184
189
 
185
190
  function metrics(data){
186
191
  try{
@@ -233,7 +238,7 @@ function metrics(data){
233
238
 
234
239
  lastStats = webTransportEvents;
235
240
 
236
- const fail = errors+timeouts;
241
+ fail = errors+timeouts;
237
242
  const treshold = errors+timeouts+stats.open+stats.pending;
238
243
 
239
244
  if(treshold>60){
@@ -249,13 +254,13 @@ function metrics(data){
249
254
  if(isDialEnabled){
250
255
  isDialEnabled = false;
251
256
  const str = JSON.stringify({isDialEnabled,fail,lastfailtreshold});
252
- console.warn(str);
257
+ console.warn('dial disabled');
253
258
  setTimeout(()=>{
254
259
  if(!isDialEnabled){
255
260
  isDialEnabled = true;
256
261
  lastfailtreshold = fail;
257
262
  const str = JSON.stringify({isDialEnabled,fail,lastfailtreshold});
258
- console.warn(str);
263
+ console.warn('dial enabled');
259
264
  }
260
265
  },6*60*1000);
261
266
  }
@@ -372,20 +377,27 @@ class webpeerjs{
372
377
 
373
378
 
374
379
  //listen to peer connect event
375
- this.#libp2p.addEventListener("peer:connect", (evt) => {
380
+ this.#libp2p.addEventListener("peer:connect",async (evt) => {
376
381
 
377
382
  //console.log(`Connected to ${connection.toString()}`);
378
383
 
379
- const connection = evt.detail;
384
+ evt.detail;
380
385
  const id = evt.detail.toString();
381
386
 
382
387
  const connections = this.#libp2p.getConnections().map((con)=>{return {id:con.remotePeer.toString(),addr:con.remoteAddr.toString()}});
383
388
  const connect = connections.find((con)=>con.id == id);
384
389
  const addr = connect.addr;
390
+
391
+ if(CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id) || CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
392
+ if(!this.#connections.has(id)&&addr.includes('webtransport')){
393
+ await this.#dbstore.put(new Key(id), new TextEncoder().encode(addr));
394
+ }
395
+ }
396
+
385
397
  this.#connections.set(id,addr);
386
398
 
387
399
  //required by joinRoom version 1 to announce via universal connectivity
388
- if(connection.toString() === CONFIG_KNOWN_BOOTSTRAP_PEER_IDS[0]){
400
+ if(CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
389
401
  setTimeout(()=>{
390
402
  this.#announce();
391
403
  setTimeout(()=>{
@@ -430,6 +442,11 @@ class webpeerjs{
430
442
  const now = new Date().getTime();
431
443
  const metadata = {addrs:address,last:now};
432
444
  this.#connectedPeers.set(senderPeerId,metadata);
445
+ this.#connectedPeersArr.length = 0;
446
+ for(const peer of this.#connectedPeers){
447
+ const item = {id:peer[0],address:peer[1].addrs};
448
+ this.#connectedPeersArr.push(item);
449
+ }
433
450
  }
434
451
 
435
452
  //dial if not connected
@@ -559,8 +576,6 @@ class webpeerjs{
559
576
  }
560
577
 
561
578
  }catch(err){
562
- //console.log('from '+event.detail.from.toString())
563
- console.debug(err);
564
579
  }
565
580
  }else {
566
581
  const json = JSON.parse(topic);
@@ -705,6 +720,7 @@ class webpeerjs{
705
720
 
706
721
  //dial random discovered peers
707
722
  //this.#dialdiscoveredpeers()
723
+
708
724
 
709
725
  onMetrics((data)=>{
710
726
  const signal = metrics(data);
@@ -724,16 +740,7 @@ class webpeerjs{
724
740
  setInterval(()=>{
725
741
  this.#trackLastSeen();
726
742
  },5e3);
727
-
728
- setInterval(async()=>{
729
- /*const peerId = peerIdFromString('QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN')
730
- //const peerInfo = await this.#libp2p.services.aminoDHT.findPeer(peerId)
731
-
732
- //console.info(peerInfo)
733
- for await (const event of this.#libp2p.services.aminoDHT.findPeer(peerId)){
734
- console.info(event)
735
- }*/
736
- },5e3);
743
+
737
744
 
738
745
  /*setTimeout(async()=>{
739
746
  try{
@@ -779,6 +786,39 @@ class webpeerjs{
779
786
  PRIVATE FUNCTION
780
787
  */
781
788
 
789
+ #findHybridPeer(){
790
+ setTimeout(async()=>{
791
+ for(const target of CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
792
+ if(!this.#isConnected(target)){
793
+ //console.log('findPeer',target)
794
+ const peerId = peerIdFromString(target);
795
+ //const peerInfo = await this.#libp2p.services.aminoDHT.findPeer(peerId)
796
+
797
+ //console.info(peerInfo)
798
+ for await (const event of this.#libp2p.services.aminoDHT.findPeer(peerId)){
799
+ //console.info('findPeer',event)
800
+ if (event.name === 'FINAL_PEER'){
801
+ //console.log(event.peer.id.toString(),event.peer.multiaddrs.toString())
802
+ let mddrs = [];
803
+ let addrs = [];
804
+ const id = event.peer.id.toString();
805
+ for(const mddr of event.peer.multiaddrs){
806
+ const peeraddr = mddr.toString()+'/p2p/'+id;
807
+ const peermddr = multiaddr(peeraddr);
808
+ addrs.push(peeraddr);
809
+ mddrs.push(peermddr);
810
+ }
811
+ this.#dialedKnownBootstrap.set(id,addrs);
812
+ if(!this.#isConnected(id)){
813
+ this.#dialMultiaddress(mddrs);
814
+ }
815
+ }
816
+ }
817
+ }
818
+ }
819
+ },30e3);
820
+ }
821
+
782
822
 
783
823
  //check the last seen in web peer
784
824
  #trackLastSeen(){
@@ -840,7 +880,7 @@ class webpeerjs{
840
880
  return
841
881
  }
842
882
 
843
- if(this.#webPeersId.includes(id) || id == CONFIG_KNOWN_BOOTSTRAP_PEER_IDS[0] ){
883
+ if(this.#webPeersId.includes(id) || CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id) || CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
844
884
  this.#dialQueue.unshift(mddrs);
845
885
  }
846
886
  else {
@@ -992,13 +1032,15 @@ class webpeerjs{
992
1032
  #dialRandomBootstrap(){
993
1033
  setInterval(()=>{
994
1034
  //const keys = Array.from(this.#dialedKnownBootstrap.keys())
995
- const keys = CONFIG_KNOWN_BOOTSTRAP_PEER_IDS;
1035
+ const keys = CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS;
996
1036
  const randomKey = Math.floor(Math.random() * keys.length);
997
1037
  let ids = [];
998
1038
  ids.push(keys[randomKey]);
999
1039
 
1000
1040
  //universal connectivity id for webpeer discovery and joinRoom version 1 to work
1001
- ids.push(CONFIG_KNOWN_BOOTSTRAP_PEER_IDS[0]);
1041
+ for(const id of CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
1042
+ ids.push(id);
1043
+ }
1002
1044
 
1003
1045
  for(const id of ids){
1004
1046
  if(id == undefined)continue
@@ -1065,7 +1107,7 @@ class webpeerjs{
1065
1107
  if(besttime>bestlimit){
1066
1108
  const addr = remote.toString();
1067
1109
  const id = peer.toString();
1068
- if(!this.#webPeersId.includes(id) && !CONFIG_KNOWN_BOOTSTRAP_PEER_IDS.includes(id) && !this.#dbstoreData.get(id) && !addr.includes('p2p-circuit')){
1110
+ if(!this.#webPeersId.includes(id) && !CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id) && !this.#dbstoreData.get(id) && !addr.includes('p2p-circuit') && addr.includes('webtransport')){
1069
1111
  //await this.#dbstore.delete(new Key(id))
1070
1112
  await this.#dbstore.put(new Key(id), new TextEncoder().encode(addr));
1071
1113
  this.#dbstoreData.set(id,addr);
@@ -1134,7 +1176,7 @@ class webpeerjs{
1134
1176
  }
1135
1177
  }
1136
1178
 
1137
- },30*1000);
1179
+ },15*1000);
1138
1180
  }
1139
1181
 
1140
1182
 
@@ -1157,23 +1199,28 @@ class webpeerjs{
1157
1199
  //dial to all known bootstrap peers and DNS
1158
1200
  #dialKnownPeers(){
1159
1201
  //this.#dialKnownBootstrap()
1160
- //setTimeout(()=>{
1161
- //const peers = this.#libp2p.getPeers().length
1162
- //if(peers == 0){
1163
- //currently not needed
1164
- this.#dialKnownID();
1165
- setTimeout(()=>{
1166
- const peers = this.#libp2p.getPeers().length;
1167
- if(peers == 0){
1168
- //currently not needed
1169
- //this.#dialKnownDNS()
1170
- setTimeout(()=>{
1171
- this.#libp2p.getPeers().length;
1172
- },15000);
1173
- }
1174
- },15000);
1175
- //}
1176
- //},15000)
1202
+ setTimeout(()=>{
1203
+ this.#dialSavedKnownID();
1204
+ this.#findHybridPeer();
1205
+ setTimeout(()=>{this.#dialUpdateSavedKnownID();},60000);
1206
+ setTimeout(()=>{
1207
+ const peers = this.#libp2p.getPeers().length;
1208
+ if(peers == 0){
1209
+ this.#dialKnownID();
1210
+ this.#findHybridPeer();
1211
+ setTimeout(()=>{
1212
+ const peers = this.#libp2p.getPeers().length;
1213
+ if(peers == 0){
1214
+ //currently not needed
1215
+ //this.#dialKnownDNS()
1216
+ setTimeout(()=>{
1217
+ this.#libp2p.getPeers().length;
1218
+ },15000);
1219
+ }
1220
+ },15000);
1221
+ }
1222
+ },15000);
1223
+ },5000);
1177
1224
  }
1178
1225
 
1179
1226
 
@@ -1200,12 +1247,80 @@ class webpeerjs{
1200
1247
  }
1201
1248
  }
1202
1249
 
1250
+ async #dialSavedKnownID(){
1251
+ let firsttime = true;
1252
+ for(const target of CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS){
1253
+ if(this.#dbstoreData.has(target)){
1254
+ firsttime = false;
1255
+ let mddrs = [];
1256
+ let addrs = [];
1257
+ const id = target;
1258
+ const peeraddr = this.#dbstoreData.get(target);
1259
+ const peermddr = multiaddr(peeraddr);
1260
+ addrs.push(peeraddr);
1261
+ mddrs.push(peermddr);
1262
+ this.#dialedKnownBootstrap.set(id,addrs);
1263
+ if(!this.#isConnected(id)){
1264
+ this.#dialMultiaddress(mddrs);
1265
+ }
1266
+ }
1267
+ }
1268
+ if(firsttime){
1269
+ for(const target of CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS){
1270
+ const api = CONFIG_DELEGATED_API;
1271
+ const delegatedClient = createDelegatedRoutingV1HttpApiClient(api);
1272
+ const peer = await first(delegatedClient.getPeers(peerIdFromString(target)));
1273
+ const address = peer.Addrs;
1274
+ const id = peer.ID;
1275
+ let mddrs = [];
1276
+ let addrs = [];
1277
+ for(const addr of address){
1278
+ const peeraddr = addr.toString()+'/p2p/'+id.toString();
1279
+ const peermddr = multiaddr(peeraddr);
1280
+ addrs.push(peeraddr);
1281
+ mddrs.push(peermddr);
1282
+ }
1283
+
1284
+ this.#dialedKnownBootstrap.set(id,addrs);
1285
+ if(!this.#isConnected(id)){
1286
+ this.#dialMultiaddress(mddrs);
1287
+ }
1288
+ }
1289
+ }
1290
+ }
1291
+
1292
+ async #dialUpdateSavedKnownID(){
1293
+ for(const target of CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS){
1294
+ if(!this.#connections.has(target)){
1295
+ const api = CONFIG_DELEGATED_API;
1296
+ const delegatedClient = createDelegatedRoutingV1HttpApiClient(api);
1297
+ const peer = await first(delegatedClient.getPeers(peerIdFromString(target)));
1298
+ const address = peer.Addrs;
1299
+ const id = peer.ID;
1300
+ let mddrs = [];
1301
+ let addrs = [];
1302
+ for(const addr of address){
1303
+ const peeraddr = addr.toString()+'/p2p/'+id.toString();
1304
+ const peermddr = multiaddr(peeraddr);
1305
+ addrs.push(peeraddr);
1306
+ mddrs.push(peermddr);
1307
+ }
1308
+
1309
+ this.#dialedKnownBootstrap.set(id,addrs);
1310
+ if(!this.#isConnected(id)){
1311
+ this.#dialMultiaddress(mddrs);
1312
+ }
1313
+ }
1314
+ }
1315
+ }
1316
+
1203
1317
 
1204
1318
  //dial based on known peers ID
1205
1319
  async #dialKnownID(){
1320
+ //console.log('#dialKnownID()')
1206
1321
  const api = CONFIG_DELEGATED_API;
1207
1322
  const delegatedClient = createDelegatedRoutingV1HttpApiClient(api);
1208
- const BOOTSTRAP_PEER_IDS = CONFIG_KNOWN_BOOTSTRAP_PEER_IDS;
1323
+ const BOOTSTRAP_PEER_IDS = CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS;
1209
1324
  const peers = await Promise.all(
1210
1325
  BOOTSTRAP_PEER_IDS.map((peerId) => first(delegatedClient.getPeers(peerIdFromString(peerId)))),
1211
1326
  );
@@ -1322,8 +1437,6 @@ class webpeerjs{
1322
1437
  await this.#libp2p.dial(addr);
1323
1438
  return // if we succeed dialing the peer, no need to try another address
1324
1439
  } catch (error) {
1325
- //console.log(`failed to dial webtransport multiaddr: %o`, addr.toString())
1326
- console.debug(error);
1327
1440
  }
1328
1441
  }
1329
1442
  }
@@ -1344,8 +1457,6 @@ class webpeerjs{
1344
1457
  await this.#libp2p.dial(addr);
1345
1458
  return // if we succeed dialing the peer, no need to try another address
1346
1459
  } catch (error) {
1347
- //console.log(`failed to dial websocket multiaddr: %o`, addr)
1348
- console.debug(error);
1349
1460
  }
1350
1461
  }
1351
1462
  }
@@ -1355,7 +1466,7 @@ class webpeerjs{
1355
1466
  static async createWebpeer(){
1356
1467
 
1357
1468
  // all libp2p debug logs
1358
- localStorage.setItem('debug', 'libp2p:*');
1469
+ //localStorage.setItem('debug', 'libp2p:*')
1359
1470
 
1360
1471
  const dbstore = new IDBDatastore(CONFIG_DBSTORE_PATH);
1361
1472
  await dbstore.open();
@@ -12,28 +12,32 @@
12
12
  const CONFIG_DISCOVER_RELAYS = 1;
13
13
  const CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY = 'universal-connectivity-browser-peer-discovery';
14
14
  const CONFIG_PEER_DISCOVERY_GLOBAL = '_peer-discovery._p2p._pubsub';
15
- const CONFIG_PUBSUB_PEER_DISCOVERY = [CONFIG_PEER_DISCOVERY_GLOBAL,CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY,prefix$1+'-peer-discovery'];
15
+ const CONFIG_PEER_DISCOVERY_WEBPEERJS= prefix$1+'-peer-discovery';
16
+ const CONFIG_PUBSUB_PEER_DISCOVERY = [CONFIG_PEER_DISCOVERY_GLOBAL, CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY, CONFIG_PEER_DISCOVERY_WEBPEERJS];
16
17
  const CONFIG_DELEGATED_API = 'https://delegated-ipfs.dev';
17
18
 
18
19
  const CONFIG_KNOWN_BOOTSTRAP_PEERS_ADDRS = [
19
20
  ];
20
21
 
21
- const CONFIG_KNOWN_BOOTSTRAP_PEER_IDS = [
22
- '12D3KooWFhXabKDwALpzqMbto94sB7rvmZ6M28hs9Y9xSopDKwQr',
23
- '12D3KooWPEDBmt7vm6FNNYuqaA4n2qMUZ6wPK5NcRc8t6KpqgRkV',
24
- '12D3KooWSHbugDEQeWm2LjtRRMpNgLu6oZ8zkX8XcTwYMAewVekP',
25
- '12D3KooWASoxFpwwy8JDdu4Tm57mhESsnbFPogam9VVmhR95FGXr',
22
+ const CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS = [
26
23
  'QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN',
27
- 'QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa',
28
24
  'QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb',
29
25
  'QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt',
30
26
  'QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
27
+ '12D3KooWFhXabKDwALpzqMbto94sB7rvmZ6M28hs9Y9xSopDKwQr',
28
+ '12D3KooWPEDBmt7vm6FNNYuqaA4n2qMUZ6wPK5NcRc8t6KpqgRkV',
29
+ '12D3KooWSHbugDEQeWm2LjtRRMpNgLu6oZ8zkX8XcTwYMAewVekP',
30
+ '12D3KooWASoxFpwwy8JDdu4Tm57mhESsnbFPogam9VVmhR95FGXr',
31
31
  '12D3KooWHh98YpAkJsn3ULjMjK1n9QVkXmi8Sb3gTDMatHxCmDP5',
32
32
  '12D3KooWS79EhkPU7ESUwgG4vyHHzW9FDNZLoWVth9b5N5NSrvaj',
33
33
  '12D3KooWBbkCD5MpJhMc1mfPAVGEyVkQnyxPKGS7AHwDqQM2JUsk',
34
34
  '12D3KooWKLdecs31Zmo2pLBjR9HY2vWo3VwM4eBm21Czeucbe6FL',
35
35
  '12D3KooWBdF3g6vSJFRPoZQo7BNnkNzaWb59gpyaVzsgtNTVeu8H'
36
- ];
36
+ ];
37
+
38
+ const CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS = [
39
+ '12D3KooWFhXabKDwALpzqMbto94sB7rvmZ6M28hs9Y9xSopDKwQr'
40
+ ];
37
41
 
38
42
  /**
39
43
  * Returns a `Uint8Array` of the requested size. Referenced memory will
@@ -3290,6 +3294,7 @@
3290
3294
 
3291
3295
  let isDialEnabled = true;
3292
3296
  let lastfailtreshold = 0;
3297
+ let fail = 0;
3293
3298
 
3294
3299
  function metrics(data){
3295
3300
  try{
@@ -3342,7 +3347,7 @@
3342
3347
 
3343
3348
  lastStats = webTransportEvents;
3344
3349
 
3345
- const fail = errors+timeouts;
3350
+ fail = errors+timeouts;
3346
3351
  const treshold = errors+timeouts+stats.open+stats.pending;
3347
3352
 
3348
3353
  if(treshold>60){
@@ -3358,13 +3363,13 @@
3358
3363
  if(isDialEnabled){
3359
3364
  isDialEnabled = false;
3360
3365
  const str = JSON.stringify({isDialEnabled,fail,lastfailtreshold});
3361
- console.warn(str);
3366
+ console.warn('dial disabled');
3362
3367
  setTimeout(()=>{
3363
3368
  if(!isDialEnabled){
3364
3369
  isDialEnabled = true;
3365
3370
  lastfailtreshold = fail;
3366
3371
  const str = JSON.stringify({isDialEnabled,fail,lastfailtreshold});
3367
- console.warn(str);
3372
+ console.warn('dial enabled');
3368
3373
  }
3369
3374
  },6*60*1000);
3370
3375
  }
@@ -46157,20 +46162,27 @@
46157
46162
 
46158
46163
 
46159
46164
  //listen to peer connect event
46160
- this.#libp2p.addEventListener("peer:connect", (evt) => {
46165
+ this.#libp2p.addEventListener("peer:connect",async (evt) => {
46161
46166
 
46162
46167
  //console.log(`Connected to ${connection.toString()}`);
46163
46168
 
46164
- const connection = evt.detail;
46169
+ evt.detail;
46165
46170
  const id = evt.detail.toString();
46166
46171
 
46167
46172
  const connections = this.#libp2p.getConnections().map((con)=>{return {id:con.remotePeer.toString(),addr:con.remoteAddr.toString()}});
46168
46173
  const connect = connections.find((con)=>con.id == id);
46169
46174
  const addr = connect.addr;
46175
+
46176
+ if(CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id) || CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
46177
+ if(!this.#connections.has(id)&&addr.includes('webtransport')){
46178
+ await this.#dbstore.put(new Key(id), new TextEncoder().encode(addr));
46179
+ }
46180
+ }
46181
+
46170
46182
  this.#connections.set(id,addr);
46171
46183
 
46172
46184
  //required by joinRoom version 1 to announce via universal connectivity
46173
- if(connection.toString() === CONFIG_KNOWN_BOOTSTRAP_PEER_IDS[0]){
46185
+ if(CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
46174
46186
  setTimeout(()=>{
46175
46187
  this.#announce();
46176
46188
  setTimeout(()=>{
@@ -46215,6 +46227,11 @@
46215
46227
  const now = new Date().getTime();
46216
46228
  const metadata = {addrs:address,last:now};
46217
46229
  this.#connectedPeers.set(senderPeerId,metadata);
46230
+ this.#connectedPeersArr.length = 0;
46231
+ for(const peer of this.#connectedPeers){
46232
+ const item = {id:peer[0],address:peer[1].addrs};
46233
+ this.#connectedPeersArr.push(item);
46234
+ }
46218
46235
  }
46219
46236
 
46220
46237
  //dial if not connected
@@ -46344,8 +46361,6 @@
46344
46361
  }
46345
46362
 
46346
46363
  }catch(err){
46347
- //console.log('from '+event.detail.from.toString())
46348
- console.debug(err);
46349
46364
  }
46350
46365
  }else {
46351
46366
  const json = JSON.parse(topic);
@@ -46490,6 +46505,7 @@
46490
46505
 
46491
46506
  //dial random discovered peers
46492
46507
  //this.#dialdiscoveredpeers()
46508
+
46493
46509
 
46494
46510
  onMetrics((data)=>{
46495
46511
  const signal = metrics(data);
@@ -46509,16 +46525,7 @@
46509
46525
  setInterval(()=>{
46510
46526
  this.#trackLastSeen();
46511
46527
  },5e3);
46512
-
46513
- setInterval(async()=>{
46514
- /*const peerId = peerIdFromString('QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN')
46515
- //const peerInfo = await this.#libp2p.services.aminoDHT.findPeer(peerId)
46516
-
46517
- //console.info(peerInfo)
46518
- for await (const event of this.#libp2p.services.aminoDHT.findPeer(peerId)){
46519
- console.info(event)
46520
- }*/
46521
- },5e3);
46528
+
46522
46529
 
46523
46530
  /*setTimeout(async()=>{
46524
46531
  try{
@@ -46564,6 +46571,39 @@
46564
46571
  PRIVATE FUNCTION
46565
46572
  */
46566
46573
 
46574
+ #findHybridPeer(){
46575
+ setTimeout(async()=>{
46576
+ for(const target of CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
46577
+ if(!this.#isConnected(target)){
46578
+ //console.log('findPeer',target)
46579
+ const peerId = peerIdFromString(target);
46580
+ //const peerInfo = await this.#libp2p.services.aminoDHT.findPeer(peerId)
46581
+
46582
+ //console.info(peerInfo)
46583
+ for await (const event of this.#libp2p.services.aminoDHT.findPeer(peerId)){
46584
+ //console.info('findPeer',event)
46585
+ if (event.name === 'FINAL_PEER'){
46586
+ //console.log(event.peer.id.toString(),event.peer.multiaddrs.toString())
46587
+ let mddrs = [];
46588
+ let addrs = [];
46589
+ const id = event.peer.id.toString();
46590
+ for(const mddr of event.peer.multiaddrs){
46591
+ const peeraddr = mddr.toString()+'/p2p/'+id;
46592
+ const peermddr = multiaddr(peeraddr);
46593
+ addrs.push(peeraddr);
46594
+ mddrs.push(peermddr);
46595
+ }
46596
+ this.#dialedKnownBootstrap.set(id,addrs);
46597
+ if(!this.#isConnected(id)){
46598
+ this.#dialMultiaddress(mddrs);
46599
+ }
46600
+ }
46601
+ }
46602
+ }
46603
+ }
46604
+ },30e3);
46605
+ }
46606
+
46567
46607
 
46568
46608
  //check the last seen in web peer
46569
46609
  #trackLastSeen(){
@@ -46625,7 +46665,7 @@
46625
46665
  return
46626
46666
  }
46627
46667
 
46628
- if(this.#webPeersId.includes(id) || id == CONFIG_KNOWN_BOOTSTRAP_PEER_IDS[0] ){
46668
+ if(this.#webPeersId.includes(id) || CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id) || CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
46629
46669
  this.#dialQueue.unshift(mddrs);
46630
46670
  }
46631
46671
  else {
@@ -46777,13 +46817,15 @@
46777
46817
  #dialRandomBootstrap(){
46778
46818
  setInterval(()=>{
46779
46819
  //const keys = Array.from(this.#dialedKnownBootstrap.keys())
46780
- const keys = CONFIG_KNOWN_BOOTSTRAP_PEER_IDS;
46820
+ const keys = CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS;
46781
46821
  const randomKey = Math.floor(Math.random() * keys.length);
46782
46822
  let ids = [];
46783
46823
  ids.push(keys[randomKey]);
46784
46824
 
46785
46825
  //universal connectivity id for webpeer discovery and joinRoom version 1 to work
46786
- ids.push(CONFIG_KNOWN_BOOTSTRAP_PEER_IDS[0]);
46826
+ for(const id of CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
46827
+ ids.push(id);
46828
+ }
46787
46829
 
46788
46830
  for(const id of ids){
46789
46831
  if(id == undefined)continue
@@ -46850,7 +46892,7 @@
46850
46892
  if(besttime>bestlimit){
46851
46893
  const addr = remote.toString();
46852
46894
  const id = peer.toString();
46853
- if(!this.#webPeersId.includes(id) && !CONFIG_KNOWN_BOOTSTRAP_PEER_IDS.includes(id) && !this.#dbstoreData.get(id) && !addr.includes('p2p-circuit')){
46895
+ if(!this.#webPeersId.includes(id) && !CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id) && !this.#dbstoreData.get(id) && !addr.includes('p2p-circuit') && addr.includes('webtransport')){
46854
46896
  //await this.#dbstore.delete(new Key(id))
46855
46897
  await this.#dbstore.put(new Key(id), new TextEncoder().encode(addr));
46856
46898
  this.#dbstoreData.set(id,addr);
@@ -46919,7 +46961,7 @@
46919
46961
  }
46920
46962
  }
46921
46963
 
46922
- },30*1000);
46964
+ },15*1000);
46923
46965
  }
46924
46966
 
46925
46967
 
@@ -46942,23 +46984,28 @@
46942
46984
  //dial to all known bootstrap peers and DNS
46943
46985
  #dialKnownPeers(){
46944
46986
  //this.#dialKnownBootstrap()
46945
- //setTimeout(()=>{
46946
- //const peers = this.#libp2p.getPeers().length
46947
- //if(peers == 0){
46948
- //currently not needed
46949
- this.#dialKnownID();
46950
- setTimeout(()=>{
46951
- const peers = this.#libp2p.getPeers().length;
46952
- if(peers == 0){
46953
- //currently not needed
46954
- //this.#dialKnownDNS()
46955
- setTimeout(()=>{
46956
- this.#libp2p.getPeers().length;
46957
- },15000);
46958
- }
46959
- },15000);
46960
- //}
46961
- //},15000)
46987
+ setTimeout(()=>{
46988
+ this.#dialSavedKnownID();
46989
+ this.#findHybridPeer();
46990
+ setTimeout(()=>{this.#dialUpdateSavedKnownID();},60000);
46991
+ setTimeout(()=>{
46992
+ const peers = this.#libp2p.getPeers().length;
46993
+ if(peers == 0){
46994
+ this.#dialKnownID();
46995
+ this.#findHybridPeer();
46996
+ setTimeout(()=>{
46997
+ const peers = this.#libp2p.getPeers().length;
46998
+ if(peers == 0){
46999
+ //currently not needed
47000
+ //this.#dialKnownDNS()
47001
+ setTimeout(()=>{
47002
+ this.#libp2p.getPeers().length;
47003
+ },15000);
47004
+ }
47005
+ },15000);
47006
+ }
47007
+ },15000);
47008
+ },5000);
46962
47009
  }
46963
47010
 
46964
47011
 
@@ -46985,12 +47032,80 @@
46985
47032
  }
46986
47033
  }
46987
47034
 
47035
+ async #dialSavedKnownID(){
47036
+ let firsttime = true;
47037
+ for(const target of CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS){
47038
+ if(this.#dbstoreData.has(target)){
47039
+ firsttime = false;
47040
+ let mddrs = [];
47041
+ let addrs = [];
47042
+ const id = target;
47043
+ const peeraddr = this.#dbstoreData.get(target);
47044
+ const peermddr = multiaddr(peeraddr);
47045
+ addrs.push(peeraddr);
47046
+ mddrs.push(peermddr);
47047
+ this.#dialedKnownBootstrap.set(id,addrs);
47048
+ if(!this.#isConnected(id)){
47049
+ this.#dialMultiaddress(mddrs);
47050
+ }
47051
+ }
47052
+ }
47053
+ if(firsttime){
47054
+ for(const target of CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS){
47055
+ const api = CONFIG_DELEGATED_API;
47056
+ const delegatedClient = createDelegatedRoutingV1HttpApiClient(api);
47057
+ const peer = await first$1(delegatedClient.getPeers(peerIdFromString(target)));
47058
+ const address = peer.Addrs;
47059
+ const id = peer.ID;
47060
+ let mddrs = [];
47061
+ let addrs = [];
47062
+ for(const addr of address){
47063
+ const peeraddr = addr.toString()+'/p2p/'+id.toString();
47064
+ const peermddr = multiaddr(peeraddr);
47065
+ addrs.push(peeraddr);
47066
+ mddrs.push(peermddr);
47067
+ }
47068
+
47069
+ this.#dialedKnownBootstrap.set(id,addrs);
47070
+ if(!this.#isConnected(id)){
47071
+ this.#dialMultiaddress(mddrs);
47072
+ }
47073
+ }
47074
+ }
47075
+ }
47076
+
47077
+ async #dialUpdateSavedKnownID(){
47078
+ for(const target of CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS){
47079
+ if(!this.#connections.has(target)){
47080
+ const api = CONFIG_DELEGATED_API;
47081
+ const delegatedClient = createDelegatedRoutingV1HttpApiClient(api);
47082
+ const peer = await first$1(delegatedClient.getPeers(peerIdFromString(target)));
47083
+ const address = peer.Addrs;
47084
+ const id = peer.ID;
47085
+ let mddrs = [];
47086
+ let addrs = [];
47087
+ for(const addr of address){
47088
+ const peeraddr = addr.toString()+'/p2p/'+id.toString();
47089
+ const peermddr = multiaddr(peeraddr);
47090
+ addrs.push(peeraddr);
47091
+ mddrs.push(peermddr);
47092
+ }
47093
+
47094
+ this.#dialedKnownBootstrap.set(id,addrs);
47095
+ if(!this.#isConnected(id)){
47096
+ this.#dialMultiaddress(mddrs);
47097
+ }
47098
+ }
47099
+ }
47100
+ }
47101
+
46988
47102
 
46989
47103
  //dial based on known peers ID
46990
47104
  async #dialKnownID(){
47105
+ //console.log('#dialKnownID()')
46991
47106
  const api = CONFIG_DELEGATED_API;
46992
47107
  const delegatedClient = createDelegatedRoutingV1HttpApiClient(api);
46993
- const BOOTSTRAP_PEER_IDS = CONFIG_KNOWN_BOOTSTRAP_PEER_IDS;
47108
+ const BOOTSTRAP_PEER_IDS = CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS;
46994
47109
  const peers = await Promise.all(
46995
47110
  BOOTSTRAP_PEER_IDS.map((peerId) => first$1(delegatedClient.getPeers(peerIdFromString(peerId)))),
46996
47111
  );
@@ -47107,8 +47222,6 @@
47107
47222
  await this.#libp2p.dial(addr);
47108
47223
  return // if we succeed dialing the peer, no need to try another address
47109
47224
  } catch (error) {
47110
- //console.log(`failed to dial webtransport multiaddr: %o`, addr.toString())
47111
- console.debug(error);
47112
47225
  }
47113
47226
  }
47114
47227
  }
@@ -47129,8 +47242,6 @@
47129
47242
  await this.#libp2p.dial(addr);
47130
47243
  return // if we succeed dialing the peer, no need to try another address
47131
47244
  } catch (error) {
47132
- //console.log(`failed to dial websocket multiaddr: %o`, addr)
47133
- console.debug(error);
47134
47245
  }
47135
47246
  }
47136
47247
  }
@@ -47140,7 +47251,7 @@
47140
47251
  static async createWebpeer(){
47141
47252
 
47142
47253
  // all libp2p debug logs
47143
- localStorage.setItem('debug', 'libp2p:*');
47254
+ //localStorage.setItem('debug', 'libp2p:*')
47144
47255
 
47145
47256
  const dbstore = new IDBDatastore(CONFIG_DBSTORE_PATH);
47146
47257
  await dbstore.open();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webpeerjs",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "Simple peer-to-peer with IPFS",
5
5
  "main": "./dist/umd/webpeerjs.js",
6
6
  "module": "./dist/esm/webpeerjs.js",
package/src/config.js CHANGED
@@ -8,7 +8,8 @@ export const CONFIG_MIN_CONNECTIONS = 0
8
8
  export const CONFIG_DISCOVER_RELAYS = 1
9
9
  export const CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY = 'universal-connectivity-browser-peer-discovery'
10
10
  export const CONFIG_PEER_DISCOVERY_GLOBAL = '_peer-discovery._p2p._pubsub'
11
- export const CONFIG_PUBSUB_PEER_DISCOVERY = [CONFIG_PEER_DISCOVERY_GLOBAL,CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY,prefix+'-peer-discovery']
11
+ export const CONFIG_PEER_DISCOVERY_WEBPEERJS= prefix+'-peer-discovery'
12
+ export const CONFIG_PUBSUB_PEER_DISCOVERY = [CONFIG_PEER_DISCOVERY_GLOBAL, CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY, CONFIG_PEER_DISCOVERY_WEBPEERJS]
12
13
  export const CONFIG_PUPSUB_TOPIC = prefix+'-room'
13
14
  export const CONFIG_DELEGATED_API = 'https://delegated-ipfs.dev'
14
15
  export const CONFIG_DNS_RESOLVER = 'https://dns.google/resolve'
@@ -18,19 +19,22 @@ export const CONFIG_JOIN_ROOM_VERSION = 1
18
19
  export const CONFIG_KNOWN_BOOTSTRAP_PEERS_ADDRS = [
19
20
  ]
20
21
 
21
- export const CONFIG_KNOWN_BOOTSTRAP_PEER_IDS = [
22
- '12D3KooWFhXabKDwALpzqMbto94sB7rvmZ6M28hs9Y9xSopDKwQr',
23
- '12D3KooWPEDBmt7vm6FNNYuqaA4n2qMUZ6wPK5NcRc8t6KpqgRkV',
24
- '12D3KooWSHbugDEQeWm2LjtRRMpNgLu6oZ8zkX8XcTwYMAewVekP',
25
- '12D3KooWASoxFpwwy8JDdu4Tm57mhESsnbFPogam9VVmhR95FGXr',
22
+ export const CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS = [
26
23
  'QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN',
27
- 'QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa',
28
24
  'QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb',
29
25
  'QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt',
30
26
  'QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
27
+ '12D3KooWFhXabKDwALpzqMbto94sB7rvmZ6M28hs9Y9xSopDKwQr',
28
+ '12D3KooWPEDBmt7vm6FNNYuqaA4n2qMUZ6wPK5NcRc8t6KpqgRkV',
29
+ '12D3KooWSHbugDEQeWm2LjtRRMpNgLu6oZ8zkX8XcTwYMAewVekP',
30
+ '12D3KooWASoxFpwwy8JDdu4Tm57mhESsnbFPogam9VVmhR95FGXr',
31
31
  '12D3KooWHh98YpAkJsn3ULjMjK1n9QVkXmi8Sb3gTDMatHxCmDP5',
32
32
  '12D3KooWS79EhkPU7ESUwgG4vyHHzW9FDNZLoWVth9b5N5NSrvaj',
33
33
  '12D3KooWBbkCD5MpJhMc1mfPAVGEyVkQnyxPKGS7AHwDqQM2JUsk',
34
34
  '12D3KooWKLdecs31Zmo2pLBjR9HY2vWo3VwM4eBm21Czeucbe6FL',
35
35
  '12D3KooWBdF3g6vSJFRPoZQo7BNnkNzaWb59gpyaVzsgtNTVeu8H'
36
36
  ]
37
+
38
+ export const CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS = [
39
+ '12D3KooWFhXabKDwALpzqMbto94sB7rvmZ6M28hs9Y9xSopDKwQr'
40
+ ]
package/src/utils.js CHANGED
@@ -12,6 +12,10 @@ const prefix = config.CONFIG_PREFIX
12
12
 
13
13
  export const mkErr = msg => new Error(`${prefix}: ${msg}`)
14
14
 
15
+ export function mkDebug(error){
16
+ return
17
+ }
18
+
15
19
  export {PBPeer}
16
20
 
17
21
  export function uint8ArrayToString(uint8Array){
@@ -82,6 +86,7 @@ let lastStats = {
82
86
 
83
87
  let isDialEnabled = true
84
88
  let lastfailtreshold = 0
89
+ let fail = 0
85
90
 
86
91
  export function metrics(data){
87
92
  try{
@@ -134,7 +139,7 @@ export function metrics(data){
134
139
 
135
140
  lastStats = webTransportEvents
136
141
 
137
- const fail = errors+timeouts
142
+ fail = errors+timeouts
138
143
  const treshold = errors+timeouts+stats.open+stats.pending
139
144
 
140
145
  if(treshold>60){
@@ -150,13 +155,13 @@ export function metrics(data){
150
155
  if(isDialEnabled){
151
156
  isDialEnabled = false
152
157
  const str = JSON.stringify({isDialEnabled,fail,lastfailtreshold})
153
- console.warn(str)
158
+ console.warn('dial disabled')
154
159
  setTimeout(()=>{
155
160
  if(!isDialEnabled){
156
161
  isDialEnabled = true
157
162
  lastfailtreshold = fail
158
163
  const str = JSON.stringify({isDialEnabled,fail,lastfailtreshold})
159
- console.warn(str)
164
+ console.warn('dial enabled')
160
165
  }
161
166
  },6*60*1000)
162
167
  }
package/src/webpeerjs.js CHANGED
@@ -8,7 +8,8 @@ import {
8
8
  Key,
9
9
  msgIdFnStrictNoSign,
10
10
  metrics,
11
- getDigest
11
+ getDigest,
12
+ mkDebug
12
13
  } from './utils'
13
14
  import { createDelegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-v1-http-api-client'
14
15
  import { createLibp2p } from 'libp2p'
@@ -129,7 +130,7 @@ class webpeerjs{
129
130
 
130
131
 
131
132
  //listen to peer connect event
132
- this.#libp2p.addEventListener("peer:connect", (evt) => {
133
+ this.#libp2p.addEventListener("peer:connect",async (evt) => {
133
134
 
134
135
  //console.log(`Connected to ${connection.toString()}`);
135
136
 
@@ -139,10 +140,17 @@ class webpeerjs{
139
140
  const connections = this.#libp2p.getConnections().map((con)=>{return {id:con.remotePeer.toString(),addr:con.remoteAddr.toString()}})
140
141
  const connect = connections.find((con)=>con.id == id)
141
142
  const addr = connect.addr
143
+
144
+ if(config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id) || config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
145
+ if(!this.#connections.has(id)&&addr.includes('webtransport')){
146
+ await this.#dbstore.put(new Key(id), new TextEncoder().encode(addr))
147
+ }
148
+ }
149
+
142
150
  this.#connections.set(id,addr)
143
151
 
144
152
  //required by joinRoom version 1 to announce via universal connectivity
145
- if(connection.toString() === config.CONFIG_KNOWN_BOOTSTRAP_PEER_IDS[0]){
153
+ if(config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
146
154
  setTimeout(()=>{
147
155
  this.#announce()
148
156
  setTimeout(()=>{
@@ -187,6 +195,11 @@ class webpeerjs{
187
195
  const now = new Date().getTime()
188
196
  const metadata = {addrs:address,last:now}
189
197
  this.#connectedPeers.set(senderPeerId,metadata)
198
+ this.#connectedPeersArr.length = 0
199
+ for(const peer of this.#connectedPeers){
200
+ const item = {id:peer[0],address:peer[1].addrs}
201
+ this.#connectedPeersArr.push(item)
202
+ }
190
203
  }
191
204
 
192
205
  //dial if not connected
@@ -317,7 +330,7 @@ class webpeerjs{
317
330
 
318
331
  }catch(err){
319
332
  //console.log('from '+event.detail.from.toString())
320
- console.debug(err)
333
+ mkDebug(err)
321
334
  }
322
335
  }else{
323
336
  const json = JSON.parse(topic)
@@ -468,6 +481,7 @@ class webpeerjs{
468
481
 
469
482
  //dial random discovered peers
470
483
  //this.#dialdiscoveredpeers()
484
+
471
485
 
472
486
  onMetrics((data)=>{
473
487
  const signal = metrics(data)
@@ -487,16 +501,7 @@ class webpeerjs{
487
501
  setInterval(()=>{
488
502
  this.#trackLastSeen()
489
503
  },5e3)
490
-
491
- setInterval(async()=>{
492
- /*const peerId = peerIdFromString('QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN')
493
- //const peerInfo = await this.#libp2p.services.aminoDHT.findPeer(peerId)
494
-
495
- //console.info(peerInfo)
496
- for await (const event of this.#libp2p.services.aminoDHT.findPeer(peerId)){
497
- console.info(event)
498
- }*/
499
- },5e3)
504
+
500
505
 
501
506
  /*setTimeout(async()=>{
502
507
  try{
@@ -542,6 +547,39 @@ class webpeerjs{
542
547
  PRIVATE FUNCTION
543
548
  */
544
549
 
550
+ #findHybridPeer(){
551
+ setTimeout(async()=>{
552
+ for(const target of config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
553
+ if(!this.#isConnected(target)){
554
+ //console.log('findPeer',target)
555
+ const peerId = peerIdFromString(target)
556
+ //const peerInfo = await this.#libp2p.services.aminoDHT.findPeer(peerId)
557
+
558
+ //console.info(peerInfo)
559
+ for await (const event of this.#libp2p.services.aminoDHT.findPeer(peerId)){
560
+ //console.info('findPeer',event)
561
+ if (event.name === 'FINAL_PEER'){
562
+ //console.log(event.peer.id.toString(),event.peer.multiaddrs.toString())
563
+ let mddrs = []
564
+ let addrs = []
565
+ const id = event.peer.id.toString()
566
+ for(const mddr of event.peer.multiaddrs){
567
+ const peeraddr = mddr.toString()+'/p2p/'+id
568
+ const peermddr = multiaddr(peeraddr)
569
+ addrs.push(peeraddr)
570
+ mddrs.push(peermddr)
571
+ }
572
+ this.#dialedKnownBootstrap.set(id,addrs)
573
+ if(!this.#isConnected(id)){
574
+ this.#dialMultiaddress(mddrs)
575
+ }
576
+ }
577
+ }
578
+ }
579
+ }
580
+ },30e3)
581
+ }
582
+
545
583
 
546
584
  //check the last seen in web peer
547
585
  #trackLastSeen(){
@@ -603,7 +641,7 @@ class webpeerjs{
603
641
  return
604
642
  }
605
643
 
606
- if(this.#webPeersId.includes(id) || id == config.CONFIG_KNOWN_BOOTSTRAP_PEER_IDS[0] ){
644
+ if(this.#webPeersId.includes(id) || config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id) || config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
607
645
  this.#dialQueue.unshift(mddrs)
608
646
  }
609
647
  else{
@@ -755,13 +793,15 @@ class webpeerjs{
755
793
  #dialRandomBootstrap(){
756
794
  setInterval(()=>{
757
795
  //const keys = Array.from(this.#dialedKnownBootstrap.keys())
758
- const keys = config.CONFIG_KNOWN_BOOTSTRAP_PEER_IDS
796
+ const keys = config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS
759
797
  const randomKey = Math.floor(Math.random() * keys.length)
760
798
  let ids = []
761
799
  ids.push(keys[randomKey])
762
800
 
763
801
  //universal connectivity id for webpeer discovery and joinRoom version 1 to work
764
- ids.push(config.CONFIG_KNOWN_BOOTSTRAP_PEER_IDS[0])
802
+ for(const id of config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
803
+ ids.push(id)
804
+ }
765
805
 
766
806
  for(const id of ids){
767
807
  if(id == undefined)continue
@@ -828,7 +868,7 @@ class webpeerjs{
828
868
  if(besttime>bestlimit){
829
869
  const addr = remote.toString()
830
870
  const id = peer.toString()
831
- if(!this.#webPeersId.includes(id) && !config.CONFIG_KNOWN_BOOTSTRAP_PEER_IDS.includes(id) && !this.#dbstoreData.get(id) && !addr.includes('p2p-circuit')){
871
+ if(!this.#webPeersId.includes(id) && !config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id) && !this.#dbstoreData.get(id) && !addr.includes('p2p-circuit') && addr.includes('webtransport')){
832
872
  //await this.#dbstore.delete(new Key(id))
833
873
  await this.#dbstore.put(new Key(id), new TextEncoder().encode(addr))
834
874
  this.#dbstoreData.set(id,addr)
@@ -897,7 +937,7 @@ class webpeerjs{
897
937
  }
898
938
  }
899
939
 
900
- },30*1000)
940
+ },15*1000)
901
941
  }
902
942
 
903
943
 
@@ -920,27 +960,32 @@ class webpeerjs{
920
960
  //dial to all known bootstrap peers and DNS
921
961
  #dialKnownPeers(){
922
962
  //this.#dialKnownBootstrap()
923
- //setTimeout(()=>{
924
- //const peers = this.#libp2p.getPeers().length
925
- //if(peers == 0){
926
- //currently not needed
927
- this.#dialKnownID()
928
- setTimeout(()=>{
929
- const peers = this.#libp2p.getPeers().length
930
- if(peers == 0){
931
- //currently not needed
932
- //this.#dialKnownDNS()
933
- setTimeout(()=>{
934
- const peers = this.#libp2p.getPeers().length
935
- if(peers == 0){
936
- //currently not needed
937
- //this.#dialKnownDNSonly()
938
- }
939
- },15000)
940
- }
941
- },15000)
942
- //}
943
- //},15000)
963
+ setTimeout(()=>{
964
+ this.#dialSavedKnownID()
965
+ this.#findHybridPeer()
966
+ setTimeout(()=>{this.#dialUpdateSavedKnownID()},60000)
967
+ setTimeout(()=>{
968
+ const peers = this.#libp2p.getPeers().length
969
+ if(peers == 0){
970
+ this.#dialKnownID()
971
+ this.#findHybridPeer()
972
+ setTimeout(()=>{
973
+ const peers = this.#libp2p.getPeers().length
974
+ if(peers == 0){
975
+ //currently not needed
976
+ //this.#dialKnownDNS()
977
+ setTimeout(()=>{
978
+ const peers = this.#libp2p.getPeers().length
979
+ if(peers == 0){
980
+ //currently not needed
981
+ //this.#dialKnownDNSonly()
982
+ }
983
+ },15000)
984
+ }
985
+ },15000)
986
+ }
987
+ },15000)
988
+ },5000)
944
989
  }
945
990
 
946
991
 
@@ -967,12 +1012,80 @@ class webpeerjs{
967
1012
  }
968
1013
  }
969
1014
 
1015
+ async #dialSavedKnownID(){
1016
+ let firsttime = true
1017
+ for(const target of config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS){
1018
+ if(this.#dbstoreData.has(target)){
1019
+ firsttime = false
1020
+ let mddrs = []
1021
+ let addrs = []
1022
+ const id = target
1023
+ const peeraddr = this.#dbstoreData.get(target)
1024
+ const peermddr = multiaddr(peeraddr)
1025
+ addrs.push(peeraddr)
1026
+ mddrs.push(peermddr)
1027
+ this.#dialedKnownBootstrap.set(id,addrs)
1028
+ if(!this.#isConnected(id)){
1029
+ this.#dialMultiaddress(mddrs)
1030
+ }
1031
+ }
1032
+ }
1033
+ if(firsttime){
1034
+ for(const target of config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS){
1035
+ const api = config.CONFIG_DELEGATED_API
1036
+ const delegatedClient = createDelegatedRoutingV1HttpApiClient(api)
1037
+ const peer = await first(delegatedClient.getPeers(peerIdFromString(target)))
1038
+ const address = peer.Addrs
1039
+ const id = peer.ID
1040
+ let mddrs = []
1041
+ let addrs = []
1042
+ for(const addr of address){
1043
+ const peeraddr = addr.toString()+'/p2p/'+id.toString()
1044
+ const peermddr = multiaddr(peeraddr)
1045
+ addrs.push(peeraddr)
1046
+ mddrs.push(peermddr)
1047
+ }
1048
+
1049
+ this.#dialedKnownBootstrap.set(id,addrs)
1050
+ if(!this.#isConnected(id)){
1051
+ this.#dialMultiaddress(mddrs)
1052
+ }
1053
+ }
1054
+ }
1055
+ }
1056
+
1057
+ async #dialUpdateSavedKnownID(){
1058
+ for(const target of config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS){
1059
+ if(!this.#connections.has(target)){
1060
+ const api = config.CONFIG_DELEGATED_API
1061
+ const delegatedClient = createDelegatedRoutingV1HttpApiClient(api)
1062
+ const peer = await first(delegatedClient.getPeers(peerIdFromString(target)))
1063
+ const address = peer.Addrs
1064
+ const id = peer.ID
1065
+ let mddrs = []
1066
+ let addrs = []
1067
+ for(const addr of address){
1068
+ const peeraddr = addr.toString()+'/p2p/'+id.toString()
1069
+ const peermddr = multiaddr(peeraddr)
1070
+ addrs.push(peeraddr)
1071
+ mddrs.push(peermddr)
1072
+ }
1073
+
1074
+ this.#dialedKnownBootstrap.set(id,addrs)
1075
+ if(!this.#isConnected(id)){
1076
+ this.#dialMultiaddress(mddrs)
1077
+ }
1078
+ }
1079
+ }
1080
+ }
1081
+
970
1082
 
971
1083
  //dial based on known peers ID
972
1084
  async #dialKnownID(){
1085
+ //console.log('#dialKnownID()')
973
1086
  const api = config.CONFIG_DELEGATED_API
974
1087
  const delegatedClient = createDelegatedRoutingV1HttpApiClient(api)
975
- const BOOTSTRAP_PEER_IDS = config.CONFIG_KNOWN_BOOTSTRAP_PEER_IDS
1088
+ const BOOTSTRAP_PEER_IDS = config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS
976
1089
  const peers = await Promise.all(
977
1090
  BOOTSTRAP_PEER_IDS.map((peerId) => first(delegatedClient.getPeers(peerIdFromString(peerId)))),
978
1091
  )
@@ -1090,7 +1203,7 @@ class webpeerjs{
1090
1203
  return // if we succeed dialing the peer, no need to try another address
1091
1204
  } catch (error) {
1092
1205
  //console.log(`failed to dial webtransport multiaddr: %o`, addr.toString())
1093
- console.debug(error)
1206
+ mkDebug(error)
1094
1207
  }
1095
1208
  }
1096
1209
  }
@@ -1112,7 +1225,7 @@ class webpeerjs{
1112
1225
  return // if we succeed dialing the peer, no need to try another address
1113
1226
  } catch (error) {
1114
1227
  //console.log(`failed to dial websocket multiaddr: %o`, addr)
1115
- console.debug(error)
1228
+ mkDebug(error)
1116
1229
  }
1117
1230
  }
1118
1231
  }
@@ -1122,7 +1235,7 @@ class webpeerjs{
1122
1235
  static async createWebpeer(){
1123
1236
 
1124
1237
  // all libp2p debug logs
1125
- localStorage.setItem('debug', 'libp2p:*')
1238
+ //localStorage.setItem('debug', 'libp2p:*')
1126
1239
 
1127
1240
  const dbstore = new IDBDatastore(config.CONFIG_DBSTORE_PATH)
1128
1241
  await dbstore.open()