webpeerjs 0.1.5 → 0.1.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.
- package/README.md +4 -1
- package/dist/esm/webpeerjs.js +18 -18
- package/dist/umd/webpeerjs.js +19 -19
- package/package.json +1 -1
- package/src/utils.js +6 -5
- package/src/webpeerjs.js +53 -17
package/package.json
CHANGED
package/src/utils.js
CHANGED
|
@@ -27,7 +27,8 @@ const prefix = config.CONFIG_PREFIX
|
|
|
27
27
|
|
|
28
28
|
export const mkErr = msg => new Error(`${prefix}: ${msg}`)
|
|
29
29
|
|
|
30
|
-
export function mkDebug(
|
|
30
|
+
export function mkDebug(msg){
|
|
31
|
+
console.debug(msg)
|
|
31
32
|
return
|
|
32
33
|
}
|
|
33
34
|
|
|
@@ -167,14 +168,14 @@ export function metrics(data){
|
|
|
167
168
|
if ((fail-lastfailtreshold)>30){
|
|
168
169
|
if(isDialEnabled){
|
|
169
170
|
isDialEnabled = false
|
|
170
|
-
const str = JSON.stringify({isDialEnabled,fail,lastfailtreshold})
|
|
171
|
-
console.warn('dial disabled')
|
|
171
|
+
//const str = JSON.stringify({isDialEnabled,fail,lastfailtreshold})
|
|
172
|
+
console.warn('Peer dial is temporary disabled')
|
|
172
173
|
setTimeout(()=>{
|
|
173
174
|
if(!isDialEnabled){
|
|
174
175
|
isDialEnabled = true
|
|
175
176
|
lastfailtreshold = fail
|
|
176
|
-
const str = JSON.stringify({isDialEnabled,fail,lastfailtreshold})
|
|
177
|
-
console.warn('dial enabled')
|
|
177
|
+
//const str = JSON.stringify({isDialEnabled,fail,lastfailtreshold})
|
|
178
|
+
console.warn('Peer dial is enabled')
|
|
178
179
|
}
|
|
179
180
|
},6*60*1000)
|
|
180
181
|
}
|
package/src/webpeerjs.js
CHANGED
|
@@ -10,11 +10,10 @@ import {
|
|
|
10
10
|
Key,
|
|
11
11
|
msgIdFnStrictNoSign,
|
|
12
12
|
metrics,
|
|
13
|
-
getDigest,
|
|
13
|
+
//getDigest,
|
|
14
14
|
mkDebug,
|
|
15
15
|
multiaddr,
|
|
16
16
|
pipe,
|
|
17
|
-
lpStream,
|
|
18
17
|
lp,
|
|
19
18
|
map
|
|
20
19
|
} from './utils'
|
|
@@ -98,6 +97,7 @@ class webpeerjs{
|
|
|
98
97
|
|
|
99
98
|
//track last connect to webpeer network
|
|
100
99
|
#lastTimeConnectToNetwork
|
|
100
|
+
#lastTimeReceiveData
|
|
101
101
|
|
|
102
102
|
//arr to track on connect event
|
|
103
103
|
#onConnectQueue
|
|
@@ -132,6 +132,7 @@ class webpeerjs{
|
|
|
132
132
|
this.#msgIdtracker = []
|
|
133
133
|
this.#peerexchangedata = new Map()
|
|
134
134
|
this.#lastTimeConnectToNetwork = new Date().getTime()
|
|
135
|
+
this.#lastTimeReceiveData = new Date().getTime()
|
|
135
136
|
this.#onConnectQueue = []
|
|
136
137
|
|
|
137
138
|
this.peers = (function(f) {
|
|
@@ -162,7 +163,7 @@ class webpeerjs{
|
|
|
162
163
|
//listen to peer connect event
|
|
163
164
|
this.#libp2p.addEventListener("peer:connect",async (evt) => {
|
|
164
165
|
|
|
165
|
-
const connection = evt.detail;
|
|
166
|
+
//const connection = evt.detail;
|
|
166
167
|
const id = evt.detail.toString()
|
|
167
168
|
|
|
168
169
|
//console.log('peer:connect '+id,evt)
|
|
@@ -209,7 +210,7 @@ class webpeerjs{
|
|
|
209
210
|
const metadata = {addrs:address,last:now}
|
|
210
211
|
this.#connectedPeers.set(id,metadata)
|
|
211
212
|
}
|
|
212
|
-
else{
|
|
213
|
+
else if(this.#lastTimeReceiveData < 10*1000){
|
|
213
214
|
//add to connected webpeers
|
|
214
215
|
this.#onConnectFnUpdate(id)
|
|
215
216
|
const now = new Date().getTime()
|
|
@@ -240,6 +241,10 @@ class webpeerjs{
|
|
|
240
241
|
|
|
241
242
|
try{
|
|
242
243
|
|
|
244
|
+
if(topic === config.CONFIG_PUPSUB_PEER_DATA || config.CONFIG_PUBSUB_PEER_DISCOVERY_WEBPEER.includes(topic) || config.CONFIG_RUN_ON_TRANSIENT_CONNECTION){
|
|
245
|
+
this.#lastTimeReceiveData = new Date().getTime()
|
|
246
|
+
}
|
|
247
|
+
|
|
243
248
|
//track last connect to webpeer network
|
|
244
249
|
if(config.CONFIG_PUBSUB_PEER_DISCOVERY_WEBPEER.includes(topic)){
|
|
245
250
|
const now = new Date().getTime()
|
|
@@ -262,7 +267,7 @@ class webpeerjs{
|
|
|
262
267
|
const metadata = {addrs:address,last:now}
|
|
263
268
|
this.#connectedPeers.set(senderPeerId,metadata)
|
|
264
269
|
}
|
|
265
|
-
else{
|
|
270
|
+
else if(this.#lastTimeReceiveData < 10*1000){
|
|
266
271
|
//add to connected webpeers
|
|
267
272
|
this.#onConnectFnUpdate(senderPeerId)
|
|
268
273
|
const address = this.#webPeersAddrs.get(senderPeerId)
|
|
@@ -339,18 +344,29 @@ class webpeerjs{
|
|
|
339
344
|
//detect special webpeer identity
|
|
340
345
|
if(prefix === config.CONFIG_PREFIX){
|
|
341
346
|
|
|
347
|
+
this.#lastTimeReceiveData = new Date().getTime()
|
|
348
|
+
|
|
342
349
|
//add to webpeers id
|
|
343
350
|
if(!this.#webPeersId.includes(id))this.#webPeersId.push(id)
|
|
344
351
|
|
|
345
352
|
//add to connected webpeers
|
|
346
353
|
if(!this.#connectedPeers.has(id)){
|
|
347
354
|
this.#onConnectFnUpdate(id)
|
|
348
|
-
address = []
|
|
349
355
|
const now = new Date().getTime()
|
|
350
356
|
const metadata = {addrs:address,last:now}
|
|
351
357
|
this.#connectedPeers.set(id,metadata)
|
|
352
|
-
|
|
358
|
+
if(address){
|
|
359
|
+
if(address.length > 0){
|
|
360
|
+
this.#webPeersAddrs.set(id,address)
|
|
361
|
+
}
|
|
362
|
+
}
|
|
353
363
|
this.#updatePeers()
|
|
364
|
+
}else{
|
|
365
|
+
if(address){
|
|
366
|
+
if(address.length > 0){
|
|
367
|
+
this.#webPeersAddrs.set(id,address)
|
|
368
|
+
}
|
|
369
|
+
}
|
|
354
370
|
}
|
|
355
371
|
|
|
356
372
|
|
|
@@ -672,6 +688,7 @@ class webpeerjs{
|
|
|
672
688
|
setInterval(()=>{
|
|
673
689
|
this.#peerDiscoveryHybrid()
|
|
674
690
|
this.#trackHybridPeersConnection()
|
|
691
|
+
this.#trackWebpeerConnection()
|
|
675
692
|
},10e3)
|
|
676
693
|
|
|
677
694
|
|
|
@@ -792,6 +809,7 @@ class webpeerjs{
|
|
|
792
809
|
PRIVATE FUNCTION
|
|
793
810
|
*/
|
|
794
811
|
|
|
812
|
+
//track hybrid peer connection and try to dial if not connected at least 1
|
|
795
813
|
#trackHybridPeersConnection(){
|
|
796
814
|
let isConnectedToHybridPeers = false
|
|
797
815
|
for(const id of config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
|
|
@@ -811,6 +829,24 @@ class webpeerjs{
|
|
|
811
829
|
}
|
|
812
830
|
}
|
|
813
831
|
|
|
832
|
+
//track webpeer connection and try to dial if not connected to network using saved webpeer address
|
|
833
|
+
#trackWebpeerConnection(){
|
|
834
|
+
if(this.status === 'connected')return
|
|
835
|
+
if(this.#webPeersAddrs.size < 1)return
|
|
836
|
+
|
|
837
|
+
const keys = Array.from(this.#webPeersAddrs.keys())
|
|
838
|
+
const randomKey = Math.floor(Math.random() * keys.length)
|
|
839
|
+
const key = keys[randomKey]
|
|
840
|
+
const addrs = this.#webPeersAddrs.get(key)
|
|
841
|
+
|
|
842
|
+
let mddrs = []
|
|
843
|
+
for(const addr of addrs){
|
|
844
|
+
const mddr = multiaddr(addr)
|
|
845
|
+
mddrs.push(mddr)
|
|
846
|
+
}
|
|
847
|
+
this.#dialMultiaddress(mddrs)
|
|
848
|
+
}
|
|
849
|
+
|
|
814
850
|
//prevent double on connect event
|
|
815
851
|
#onConnectFnUpdate(id){
|
|
816
852
|
if(!this.#onConnectQueue.includes(id)){
|
|
@@ -842,7 +878,7 @@ class webpeerjs{
|
|
|
842
878
|
|
|
843
879
|
async #registerProtocol(){
|
|
844
880
|
|
|
845
|
-
const handler = async ({ connection, stream
|
|
881
|
+
const handler = async ({ connection, stream }) => {
|
|
846
882
|
try{
|
|
847
883
|
const output = await pipe(
|
|
848
884
|
stream.source,
|
|
@@ -923,7 +959,7 @@ class webpeerjs{
|
|
|
923
959
|
)
|
|
924
960
|
}
|
|
925
961
|
catch(err){
|
|
926
|
-
|
|
962
|
+
mkDebug(err)
|
|
927
963
|
}
|
|
928
964
|
}
|
|
929
965
|
|
|
@@ -934,14 +970,14 @@ class webpeerjs{
|
|
|
934
970
|
})
|
|
935
971
|
|
|
936
972
|
await this.#libp2p.register(config.CONFIG_PROTOCOL, {
|
|
937
|
-
onConnect: (peer, connection) => {
|
|
938
|
-
//
|
|
973
|
+
/*onConnect: (peer, connection) => {
|
|
974
|
+
//handle connect
|
|
939
975
|
//console.log('handle connect',peer)
|
|
940
976
|
},
|
|
941
977
|
onDisconnect: (peer, connection) => {
|
|
942
|
-
//
|
|
978
|
+
//handle disconnect
|
|
943
979
|
//console.log('handle disconnect',peer)
|
|
944
|
-
}
|
|
980
|
+
}*/
|
|
945
981
|
})
|
|
946
982
|
|
|
947
983
|
}
|
|
@@ -1032,7 +1068,7 @@ class webpeerjs{
|
|
|
1032
1068
|
}
|
|
1033
1069
|
}
|
|
1034
1070
|
catch(err){
|
|
1035
|
-
|
|
1071
|
+
mkDebug(err)
|
|
1036
1072
|
}
|
|
1037
1073
|
}
|
|
1038
1074
|
|
|
@@ -1074,7 +1110,7 @@ class webpeerjs{
|
|
|
1074
1110
|
|
|
1075
1111
|
//check the last seen in web peer
|
|
1076
1112
|
#trackLastSeen(){
|
|
1077
|
-
const timeout =
|
|
1113
|
+
const timeout = 10*1000
|
|
1078
1114
|
const forcetimeout = 60*1000
|
|
1079
1115
|
const now = new Date().getTime()
|
|
1080
1116
|
|
|
@@ -1892,7 +1928,7 @@ class webpeerjs{
|
|
|
1892
1928
|
};
|
|
1893
1929
|
|
|
1894
1930
|
pc.onicecandidateerror = (e) => {
|
|
1895
|
-
|
|
1931
|
+
console.debug(e);
|
|
1896
1932
|
};
|
|
1897
1933
|
|
|
1898
1934
|
pc.createDataChannel('webpeerjs');
|
|
@@ -1965,7 +2001,7 @@ class webpeerjs{
|
|
|
1965
2001
|
},
|
|
1966
2002
|
peerDiscovery: [
|
|
1967
2003
|
pubsubPeerDiscovery({
|
|
1968
|
-
interval:
|
|
2004
|
+
interval: 5_000,
|
|
1969
2005
|
topics: config.CONFIG_PUBSUB_PEER_DISCOVERY_WEBPEER,
|
|
1970
2006
|
listenOnly: false,
|
|
1971
2007
|
}),
|