webpeerjs 0.1.4 → 0.1.5
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 +12 -6
- package/dist/esm/webpeerjs.js +12 -12
- package/dist/umd/webpeerjs.js +12 -12
- package/package.json +3 -3
- package/src/config.js +1 -1
- package/src/webpeerjs.js +102 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webpeerjs",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "WebPEER.js is decentralized P2P JS library for communication between applications in browser.",
|
|
5
5
|
"main": "./dist/umd/webpeerjs.js",
|
|
6
6
|
"module": "./src/webpeerjs.js",
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
"start": "npm run dev",
|
|
20
20
|
"dev": "vite serve test",
|
|
21
21
|
"demo": "vite serve demo",
|
|
22
|
-
"eslint": "eslint ./src",
|
|
23
|
-
"eslint:fix": "eslint ./src --fix",
|
|
22
|
+
"eslint": "eslint -c ./config/eslint.config.mjs ./src",
|
|
23
|
+
"eslint:fix": "eslint -c ./config/eslint.config.mjs ./src --fix",
|
|
24
24
|
"removedir": "node -e \"var fs = require('fs'); try{process.argv.slice(1).map((fpath) => fs.rmdirSync(fpath, { recursive: true }))}catch(err){console.log(`Dist not found`)}; process.exit(0);\"",
|
|
25
25
|
"build-all": "tsc -p config/tsconfig-rollup.json && rollup -c temp/config/rollup.config.build.js && echo {\"type\": \"commonjs\"}>dist\\umd\\package.json && echo {\"type\": \"module\"}>dist\\esm\\package.json",
|
|
26
26
|
"build-types": "tsc -p config/tsconfig-esm.json",
|
package/src/config.js
CHANGED
|
@@ -13,7 +13,7 @@ export const CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY = 'universal-connectiv
|
|
|
13
13
|
export const CONFIG_PEER_DISCOVERY_GLOBAL = '_peer-discovery._p2p._pubsub'
|
|
14
14
|
export const CONFIG_PEER_DISCOVERY_WEBPEERJS= prefix+'-peer-discovery'
|
|
15
15
|
export const CONFIG_PUBSUB_PEER_DISCOVERY_HYBRID = [CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY]
|
|
16
|
-
export const CONFIG_PUBSUB_PEER_DISCOVERY_WEBPEER = [
|
|
16
|
+
export const CONFIG_PUBSUB_PEER_DISCOVERY_WEBPEER = [CONFIG_PEER_DISCOVERY_WEBPEERJS]
|
|
17
17
|
export const CONFIG_PUPSUB_PEER_DATA = ['_'+prefix+'-peer-data_']
|
|
18
18
|
export const CONFIG_PUPSUB_TOPIC = prefix+'-room'
|
|
19
19
|
export const CONFIG_DELEGATED_API = 'https://delegated-ipfs.dev'
|
package/src/webpeerjs.js
CHANGED
|
@@ -96,6 +96,12 @@ class webpeerjs{
|
|
|
96
96
|
//map of peer exchange data
|
|
97
97
|
#peerexchangedata
|
|
98
98
|
|
|
99
|
+
//track last connect to webpeer network
|
|
100
|
+
#lastTimeConnectToNetwork
|
|
101
|
+
|
|
102
|
+
//arr to track on connect event
|
|
103
|
+
#onConnectQueue
|
|
104
|
+
|
|
99
105
|
id
|
|
100
106
|
status
|
|
101
107
|
IPFS
|
|
@@ -125,6 +131,8 @@ class webpeerjs{
|
|
|
125
131
|
this.#isDialEnabled = true
|
|
126
132
|
this.#msgIdtracker = []
|
|
127
133
|
this.#peerexchangedata = new Map()
|
|
134
|
+
this.#lastTimeConnectToNetwork = new Date().getTime()
|
|
135
|
+
this.#onConnectQueue = []
|
|
128
136
|
|
|
129
137
|
this.peers = (function(f) {
|
|
130
138
|
return f
|
|
@@ -185,6 +193,14 @@ class webpeerjs{
|
|
|
185
193
|
|
|
186
194
|
if(this.#webPeersId.includes(id)){
|
|
187
195
|
|
|
196
|
+
setTimeout(()=>{
|
|
197
|
+
this.#ping()
|
|
198
|
+
},10000)
|
|
199
|
+
|
|
200
|
+
setTimeout(()=>{
|
|
201
|
+
this.#ping()
|
|
202
|
+
},15000)
|
|
203
|
+
|
|
188
204
|
let address = [addr]
|
|
189
205
|
|
|
190
206
|
if(this.#connectedPeers.has(id)){
|
|
@@ -195,7 +211,7 @@ class webpeerjs{
|
|
|
195
211
|
}
|
|
196
212
|
else{
|
|
197
213
|
//add to connected webpeers
|
|
198
|
-
this.#
|
|
214
|
+
this.#onConnectFnUpdate(id)
|
|
199
215
|
const now = new Date().getTime()
|
|
200
216
|
const metadata = {addrs:address,last:now}
|
|
201
217
|
this.#connectedPeers.set(id,metadata)
|
|
@@ -217,12 +233,25 @@ class webpeerjs{
|
|
|
217
233
|
if (event.detail.type !== 'signed') {
|
|
218
234
|
return
|
|
219
235
|
}
|
|
236
|
+
|
|
220
237
|
if(config.CONFIG_JOIN_ROOM_VERSION == 1){
|
|
221
238
|
const topic = event.detail.topic
|
|
222
239
|
const senderPeerId = event.detail.from.toString()
|
|
223
240
|
|
|
224
241
|
try{
|
|
225
242
|
|
|
243
|
+
//track last connect to webpeer network
|
|
244
|
+
if(config.CONFIG_PUBSUB_PEER_DISCOVERY_WEBPEER.includes(topic)){
|
|
245
|
+
const now = new Date().getTime()
|
|
246
|
+
const limit = 15*1000
|
|
247
|
+
const lastConnectTime = now - this.#lastTimeConnectToNetwork
|
|
248
|
+
if(lastConnectTime > limit){
|
|
249
|
+
//console.log('need re announce')
|
|
250
|
+
this.#announce()
|
|
251
|
+
}
|
|
252
|
+
this.#lastTimeConnectToNetwork = now
|
|
253
|
+
}
|
|
254
|
+
|
|
226
255
|
//if it is webpeer
|
|
227
256
|
if(this.#webPeersId.includes(senderPeerId)){
|
|
228
257
|
|
|
@@ -235,7 +264,7 @@ class webpeerjs{
|
|
|
235
264
|
}
|
|
236
265
|
else{
|
|
237
266
|
//add to connected webpeers
|
|
238
|
-
this.#
|
|
267
|
+
this.#onConnectFnUpdate(senderPeerId)
|
|
239
268
|
const address = this.#webPeersAddrs.get(senderPeerId)
|
|
240
269
|
const now = new Date().getTime()
|
|
241
270
|
const metadata = {addrs:address,last:now}
|
|
@@ -279,6 +308,20 @@ class webpeerjs{
|
|
|
279
308
|
|
|
280
309
|
//parse the message over pupsub peer discovery
|
|
281
310
|
const peer = PBPeer.decode(event.detail.data)
|
|
311
|
+
|
|
312
|
+
//detect libp2p pupsub peer discovery
|
|
313
|
+
if(peer.addrs.length > 1 && this.#libp2p.getPeers().length < config.CONFIG_MAX_CONNECTIONS){
|
|
314
|
+
let mddrs = []
|
|
315
|
+
for(const uaddr of peer.addrs){
|
|
316
|
+
const mddr = multiaddr(uaddr)
|
|
317
|
+
if(mddr.toString().includes('webtransport') && mddr.toString().includes('certhash')){
|
|
318
|
+
mddrs.push(mddr)
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
this.#dialMultiaddress(mddrs)
|
|
322
|
+
//console.log('dial '+senderPeerId,mddrs.toString())
|
|
323
|
+
}
|
|
324
|
+
|
|
282
325
|
const msg = uint8ArrayToString(peer.addrs[0])
|
|
283
326
|
const json = JSON.parse(msg)
|
|
284
327
|
const prefix = json.prefix
|
|
@@ -288,9 +331,10 @@ class webpeerjs{
|
|
|
288
331
|
const msgId = json.msgId
|
|
289
332
|
const signal = json.signal
|
|
290
333
|
const id = json.id
|
|
291
|
-
|
|
334
|
+
const address = json.address
|
|
335
|
+
//console.log(`from ${id}:${signal} = ${msg}`)
|
|
336
|
+
|
|
292
337
|
if(id != senderPeerId)return
|
|
293
|
-
let address = json.address
|
|
294
338
|
|
|
295
339
|
//detect special webpeer identity
|
|
296
340
|
if(prefix === config.CONFIG_PREFIX){
|
|
@@ -300,7 +344,7 @@ class webpeerjs{
|
|
|
300
344
|
|
|
301
345
|
//add to connected webpeers
|
|
302
346
|
if(!this.#connectedPeers.has(id)){
|
|
303
|
-
this.#
|
|
347
|
+
this.#onConnectFnUpdate(id)
|
|
304
348
|
address = []
|
|
305
349
|
const now = new Date().getTime()
|
|
306
350
|
const metadata = {addrs:address,last:now}
|
|
@@ -366,13 +410,16 @@ class webpeerjs{
|
|
|
366
410
|
}
|
|
367
411
|
else if(prefix === 'hybrid'){
|
|
368
412
|
|
|
369
|
-
|
|
413
|
+
//console.log('hybrid' +id,address)
|
|
414
|
+
const limit = config.CONFIG_MAX_CONNECTIONS / 2
|
|
415
|
+
if(address.length>0 && ((!this.#connections.has(id))||(this.#connectedPeers.size < limit))){
|
|
370
416
|
let mddrs = []
|
|
371
417
|
for(const addr of address){
|
|
372
418
|
const mddr = multiaddr(addr)
|
|
373
419
|
mddrs.push(mddr)
|
|
374
420
|
}
|
|
375
421
|
this.#dialMultiaddress(mddrs)
|
|
422
|
+
//console.log('dial '+id,mddrs.toString())
|
|
376
423
|
}
|
|
377
424
|
|
|
378
425
|
}
|
|
@@ -451,7 +498,12 @@ class webpeerjs{
|
|
|
451
498
|
count++
|
|
452
499
|
this.#trackDisconnect.set(id,count)
|
|
453
500
|
//console.log(this.#trackDisconnect)
|
|
454
|
-
if(count>
|
|
501
|
+
if(count>5){
|
|
502
|
+
if(config.CONFIG_KNOWN_BOOTSTRAP_PUBLIC_IDS.includes(id)){
|
|
503
|
+
return
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
else if(count>10){
|
|
455
507
|
if(this.#dbstoreData.has(id)){
|
|
456
508
|
this.#dbstoreData.delete(id)
|
|
457
509
|
}
|
|
@@ -619,6 +671,7 @@ class webpeerjs{
|
|
|
619
671
|
|
|
620
672
|
setInterval(()=>{
|
|
621
673
|
this.#peerDiscoveryHybrid()
|
|
674
|
+
this.#trackHybridPeersConnection()
|
|
622
675
|
},10e3)
|
|
623
676
|
|
|
624
677
|
|
|
@@ -738,6 +791,39 @@ class webpeerjs{
|
|
|
738
791
|
/*
|
|
739
792
|
PRIVATE FUNCTION
|
|
740
793
|
*/
|
|
794
|
+
|
|
795
|
+
#trackHybridPeersConnection(){
|
|
796
|
+
let isConnectedToHybridPeers = false
|
|
797
|
+
for(const id of config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
|
|
798
|
+
if(this.#isConnected(id))isConnectedToHybridPeers = true
|
|
799
|
+
}
|
|
800
|
+
if(!isConnectedToHybridPeers){
|
|
801
|
+
for(const id of config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS){
|
|
802
|
+
if(this.status !== 'connected' && this.#connections.has(id))
|
|
803
|
+
{
|
|
804
|
+
let mddrs = []
|
|
805
|
+
const addr = this.#connections.get(id)
|
|
806
|
+
const mddr = multiaddr(addr)
|
|
807
|
+
mddrs.push(mddr)
|
|
808
|
+
this.#dialMultiaddress(mddrs)
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
//prevent double on connect event
|
|
815
|
+
#onConnectFnUpdate(id){
|
|
816
|
+
if(!this.#onConnectQueue.includes(id)){
|
|
817
|
+
this.#onConnectQueue.push(id)
|
|
818
|
+
this.#onConnectFn(id)
|
|
819
|
+
setTimeout(()=>{
|
|
820
|
+
const index = this.#onConnectQueue.indexOf(id)
|
|
821
|
+
if (index > -1) {
|
|
822
|
+
this.#onConnectQueue.splice(index, 1)
|
|
823
|
+
}
|
|
824
|
+
},5000)
|
|
825
|
+
}
|
|
826
|
+
}
|
|
741
827
|
|
|
742
828
|
#updatePeers(){
|
|
743
829
|
this.#connectedPeersArr.length = 0
|
|
@@ -795,7 +881,7 @@ class webpeerjs{
|
|
|
795
881
|
if(!this.#webPeersId.includes(id))this.#webPeersId.push(id)
|
|
796
882
|
|
|
797
883
|
//add to connected webpeers
|
|
798
|
-
this.#
|
|
884
|
+
this.#onConnectFnUpdate(id)
|
|
799
885
|
const now = new Date().getTime()
|
|
800
886
|
const metadata = {addrs:address,last:now}
|
|
801
887
|
this.#connectedPeers.set(id,metadata)
|
|
@@ -922,7 +1008,7 @@ class webpeerjs{
|
|
|
922
1008
|
if(!this.#webPeersId.includes(id))this.#webPeersId.push(id)
|
|
923
1009
|
|
|
924
1010
|
//add to connected webpeers
|
|
925
|
-
this.#
|
|
1011
|
+
this.#onConnectFnUpdate(id)
|
|
926
1012
|
const now = new Date().getTime()
|
|
927
1013
|
const metadata = {addrs:address,last:now}
|
|
928
1014
|
this.#connectedPeers.set(id,metadata)
|
|
@@ -1045,6 +1131,8 @@ class webpeerjs{
|
|
|
1045
1131
|
return
|
|
1046
1132
|
}
|
|
1047
1133
|
|
|
1134
|
+
if(this.status === 'connected' && config.CONFIG_KNOWN_BOOTSTRAP_PUBLIC_IDS.includes(id))return
|
|
1135
|
+
|
|
1048
1136
|
const webPeerCount = this.#connectedPeers.size
|
|
1049
1137
|
const allPeerCount = this.#libp2p.getPeers().length
|
|
1050
1138
|
const nodePeerCount = allPeerCount - webPeerCount
|
|
@@ -1056,7 +1144,7 @@ class webpeerjs{
|
|
|
1056
1144
|
}
|
|
1057
1145
|
}
|
|
1058
1146
|
else{
|
|
1059
|
-
if(nodePeerCount>limitCount){
|
|
1147
|
+
if((nodePeerCount>(limitCount-5))||(allPeerCount>(config.CONFIG_MAX_CONNECTIONS-5))){
|
|
1060
1148
|
//close random peers
|
|
1061
1149
|
let peers = []
|
|
1062
1150
|
for(const peer of this.#libp2p.getPeers()){
|
|
@@ -1064,7 +1152,9 @@ class webpeerjs{
|
|
|
1064
1152
|
}
|
|
1065
1153
|
const randomKey = Math.floor(Math.random() * peers.length)
|
|
1066
1154
|
const randompeerid = peers[randomKey]
|
|
1067
|
-
|
|
1155
|
+
if(!config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
|
|
1156
|
+
await this.#libp2p.hangUp(peerIdFromString(randompeerid))
|
|
1157
|
+
}
|
|
1068
1158
|
}
|
|
1069
1159
|
}
|
|
1070
1160
|
|
|
@@ -1081,7 +1171,7 @@ class webpeerjs{
|
|
|
1081
1171
|
//dial multiaddr address in queue list
|
|
1082
1172
|
#dialQueueList(){
|
|
1083
1173
|
|
|
1084
|
-
if(!this.#isDialEnabled || !navigator.onLine)return
|
|
1174
|
+
if(!this.#isDialEnabled || !navigator.onLine || (document.visibilityState === 'hidden' && ('ontouchstart' in document.documentElement)) )return
|
|
1085
1175
|
|
|
1086
1176
|
const mddrsToDial = 5
|
|
1087
1177
|
|