webpeerjs 0.0.10 → 0.1.0
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 -18
- package/dist/esm/webpeerjs.js +35 -23
- package/dist/umd/webpeerjs.js +2868 -723
- package/package.json +3 -1
- package/src/config.js +1 -0
- package/src/webpeerjs.js +33 -22
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webpeerjs",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Simple peer-to-peer with IPFS",
|
|
5
5
|
"main": "./dist/umd/webpeerjs.js",
|
|
6
6
|
"module": "./src/webpeerjs.js",
|
|
@@ -54,11 +54,13 @@
|
|
|
54
54
|
"@chainsafe/libp2p-yamux": "^6.0.2",
|
|
55
55
|
"@helia/delegated-routing-v1-http-api-client": "^3.0.1",
|
|
56
56
|
"@libp2p/circuit-relay-v2": "^1.0.24",
|
|
57
|
+
"@libp2p/dcutr": "^1.1.0",
|
|
57
58
|
"@libp2p/identify": "^2.0.2",
|
|
58
59
|
"@libp2p/kad-dht": "^12.0.17",
|
|
59
60
|
"@libp2p/peer-id": "^4.1.2",
|
|
60
61
|
"@libp2p/pubsub-peer-discovery": "^10.0.2",
|
|
61
62
|
"@libp2p/simple-metrics": "^1.0.2",
|
|
63
|
+
"@libp2p/webrtc": "^4.1.0",
|
|
62
64
|
"@libp2p/websockets": "^8.1.0",
|
|
63
65
|
"@libp2p/webtransport": "^4.0.32",
|
|
64
66
|
"datastore-idb": "^2.1.9",
|
package/src/config.js
CHANGED
|
@@ -11,6 +11,7 @@ export const CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY = 'universal-connectiv
|
|
|
11
11
|
export const CONFIG_PEER_DISCOVERY_GLOBAL = '_peer-discovery._p2p._pubsub'
|
|
12
12
|
export const CONFIG_PEER_DISCOVERY_WEBPEERJS= prefix+'-peer-discovery'
|
|
13
13
|
export const CONFIG_PUBSUB_PEER_DISCOVERY = [CONFIG_PEER_DISCOVERY_GLOBAL, CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY, CONFIG_PEER_DISCOVERY_WEBPEERJS]
|
|
14
|
+
export const CONFIG_PUPSUB_PEER_DATA = ['_'+prefix+'-peer-data_']
|
|
14
15
|
export const CONFIG_PUPSUB_TOPIC = prefix+'-room'
|
|
15
16
|
export const CONFIG_DELEGATED_API = 'https://delegated-ipfs.dev'
|
|
16
17
|
export const CONFIG_DNS_RESOLVER = 'https://dns.google/resolve'
|
package/src/webpeerjs.js
CHANGED
|
@@ -21,6 +21,8 @@ import { createLibp2p } from 'libp2p'
|
|
|
21
21
|
import { IDBDatastore } from 'datastore-idb'
|
|
22
22
|
import { webTransport } from '@libp2p/webtransport'
|
|
23
23
|
import { webSockets } from '@libp2p/websockets'
|
|
24
|
+
import { webRTC } from '@libp2p/webrtc'
|
|
25
|
+
import { dcutr } from '@libp2p/dcutr'
|
|
24
26
|
import { noise } from '@chainsafe/libp2p-noise'
|
|
25
27
|
import { yamux } from '@chainsafe/libp2p-yamux'
|
|
26
28
|
import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery'
|
|
@@ -139,6 +141,10 @@ class webpeerjs{
|
|
|
139
141
|
|
|
140
142
|
this.id = this.#libp2p.peerId.toString()
|
|
141
143
|
|
|
144
|
+
for(const topic of config.CONFIG_PUPSUB_PEER_DATA){
|
|
145
|
+
this.#libp2p.services.pubsub.subscribe(topic)
|
|
146
|
+
}
|
|
147
|
+
|
|
142
148
|
|
|
143
149
|
//listen to peer connect event
|
|
144
150
|
this.#libp2p.addEventListener("peer:connect",async (evt) => {
|
|
@@ -207,7 +213,7 @@ class webpeerjs{
|
|
|
207
213
|
if(config.CONFIG_JOIN_ROOM_VERSION == 1){
|
|
208
214
|
const topic = event.detail.topic
|
|
209
215
|
const senderPeerId = event.detail.from.toString()
|
|
210
|
-
|
|
216
|
+
|
|
211
217
|
try{
|
|
212
218
|
|
|
213
219
|
//if it is webpeer
|
|
@@ -243,15 +249,17 @@ class webpeerjs{
|
|
|
243
249
|
const addrs = this.#discoveredPeers.get(senderPeerId)
|
|
244
250
|
let mddrs = []
|
|
245
251
|
for(const addr of addrs){
|
|
252
|
+
if(!addr.includes('webrtc'))continue
|
|
246
253
|
const mddr = multiaddr(addr)
|
|
247
254
|
mddrs.push(mddr)
|
|
248
255
|
}
|
|
249
256
|
this.#dialMultiaddress(mddrs)
|
|
250
257
|
}
|
|
251
|
-
else{
|
|
258
|
+
else if(this.#connectedPeers.has(senderPeerId)){
|
|
252
259
|
const addrs = this.#connectedPeers.get(senderPeerId).addrs
|
|
253
260
|
let mddrs = []
|
|
254
261
|
for(const addr of addrs){
|
|
262
|
+
if(!addr.includes('webrtc'))continue
|
|
255
263
|
const mddr = multiaddr(addr)
|
|
256
264
|
mddrs.push(mddr)
|
|
257
265
|
}
|
|
@@ -300,8 +308,10 @@ class webpeerjs{
|
|
|
300
308
|
|
|
301
309
|
//update room members
|
|
302
310
|
if(!this.#rooms[room].members.includes(id)){
|
|
303
|
-
this.#
|
|
304
|
-
|
|
311
|
+
if(this.#connectedPeers.has(id)){
|
|
312
|
+
this.#rooms[room].members.push(id)
|
|
313
|
+
this.#rooms[room].onMembers(this.#rooms[room].members)
|
|
314
|
+
}
|
|
305
315
|
}
|
|
306
316
|
|
|
307
317
|
//inbound message
|
|
@@ -320,8 +330,10 @@ class webpeerjs{
|
|
|
320
330
|
for(const room of Object.keys(this.#rooms)){
|
|
321
331
|
//update room members
|
|
322
332
|
if(!this.#rooms[room].members.includes(id)){
|
|
323
|
-
this.#
|
|
324
|
-
|
|
333
|
+
if(this.#connectedPeers.has(id)){
|
|
334
|
+
this.#rooms[room].members.push(id)
|
|
335
|
+
this.#rooms[room].onMembers(this.#rooms[room].members)
|
|
336
|
+
}
|
|
325
337
|
}
|
|
326
338
|
}
|
|
327
339
|
}
|
|
@@ -330,7 +342,7 @@ class webpeerjs{
|
|
|
330
342
|
|
|
331
343
|
//repply announce with ping
|
|
332
344
|
if(signal == 'announce'){
|
|
333
|
-
setTimeout(()=>{this.#ping('
|
|
345
|
+
setTimeout(()=>{this.#ping('')},1000)
|
|
334
346
|
//console.log('rooms',rooms)
|
|
335
347
|
}
|
|
336
348
|
|
|
@@ -345,12 +357,7 @@ class webpeerjs{
|
|
|
345
357
|
//console.log('from '+event.detail.from.toString())
|
|
346
358
|
mkDebug(err)
|
|
347
359
|
}
|
|
348
|
-
|
|
349
|
-
const json = JSON.parse(topic)
|
|
350
|
-
const room = json.room
|
|
351
|
-
const message = new TextDecoder().decode(event.detail.data)
|
|
352
|
-
this.#rooms[room].onMessage(message)
|
|
353
|
-
}
|
|
360
|
+
|
|
354
361
|
}
|
|
355
362
|
|
|
356
363
|
})
|
|
@@ -391,6 +398,7 @@ class webpeerjs{
|
|
|
391
398
|
if(!this.#connections.has(id)){
|
|
392
399
|
let mddrs = []
|
|
393
400
|
for(const addr of addrs){
|
|
401
|
+
if(!addr.includes('webrtc'))continue
|
|
394
402
|
const mddr = multiaddr(addr)
|
|
395
403
|
mddrs.push(mddr)
|
|
396
404
|
}
|
|
@@ -484,7 +492,7 @@ class webpeerjs{
|
|
|
484
492
|
const mddrs = []
|
|
485
493
|
peer.addresses.forEach((addr)=>{
|
|
486
494
|
const maddr = addr.multiaddr.toString()+'/p2p/'+id
|
|
487
|
-
if(maddr.includes('webtransport') && maddr.includes('certhash')){
|
|
495
|
+
if(maddr.includes('webtransport') && maddr.includes('certhash') && maddr.includes('webrtc')){
|
|
488
496
|
mddrs.push(maddr)
|
|
489
497
|
}
|
|
490
498
|
})
|
|
@@ -648,7 +656,7 @@ class webpeerjs{
|
|
|
648
656
|
//join room version 1 user pupsub via pupsub peer discovery
|
|
649
657
|
if(config.CONFIG_JOIN_ROOM_VERSION == 1){
|
|
650
658
|
|
|
651
|
-
const topics = config.
|
|
659
|
+
const topics = config.CONFIG_PUPSUB_PEER_DATA
|
|
652
660
|
|
|
653
661
|
this.#rooms[room] = {
|
|
654
662
|
onMessage : () => {},
|
|
@@ -702,6 +710,7 @@ class webpeerjs{
|
|
|
702
710
|
else{
|
|
703
711
|
this.status = 'unconnected'
|
|
704
712
|
}
|
|
713
|
+
this.#ping()
|
|
705
714
|
}
|
|
706
715
|
|
|
707
716
|
async #registerProtocol(){
|
|
@@ -794,7 +803,7 @@ class webpeerjs{
|
|
|
794
803
|
await this.#libp2p.handle(config.CONFIG_PROTOCOL, handler, {
|
|
795
804
|
maxInboundStreams: 50,
|
|
796
805
|
maxOutboundStreams: 50,
|
|
797
|
-
runOnTransientConnection:
|
|
806
|
+
runOnTransientConnection:false
|
|
798
807
|
})
|
|
799
808
|
|
|
800
809
|
await this.#libp2p.register(config.CONFIG_PROTOCOL, {
|
|
@@ -841,7 +850,7 @@ class webpeerjs{
|
|
|
841
850
|
|
|
842
851
|
try{
|
|
843
852
|
|
|
844
|
-
const stream = await this.#libp2p.dialProtocol(mddr, config.CONFIG_PROTOCOL,{runOnTransientConnection:
|
|
853
|
+
const stream = await this.#libp2p.dialProtocol(mddr, config.CONFIG_PROTOCOL,{runOnTransientConnection:false})
|
|
845
854
|
|
|
846
855
|
const output = await pipe(
|
|
847
856
|
message,
|
|
@@ -858,7 +867,7 @@ class webpeerjs{
|
|
|
858
867
|
return string
|
|
859
868
|
}
|
|
860
869
|
)
|
|
861
|
-
|
|
870
|
+
//console.log(output)
|
|
862
871
|
const json = JSON.parse(output)
|
|
863
872
|
if(json.protocol == config.CONFIG_PROTOCOL){
|
|
864
873
|
const address = [addr]
|
|
@@ -1062,7 +1071,7 @@ class webpeerjs{
|
|
|
1062
1071
|
|
|
1063
1072
|
//announce and ping via pupsub peer discovery
|
|
1064
1073
|
async #announce(){
|
|
1065
|
-
const topics = config.
|
|
1074
|
+
const topics = config.CONFIG_PUPSUB_PEER_DATA
|
|
1066
1075
|
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'announce',id:this.#libp2p.peerId.toString(),address:this.address,rooms:this.#rooms})
|
|
1067
1076
|
const peer = {
|
|
1068
1077
|
publicKey: this.#libp2p.peerId.publicKey,
|
|
@@ -1074,7 +1083,7 @@ class webpeerjs{
|
|
|
1074
1083
|
}
|
|
1075
1084
|
}
|
|
1076
1085
|
async #ping(){
|
|
1077
|
-
const topics = config.
|
|
1086
|
+
const topics = config.CONFIG_PUPSUB_PEER_DATA
|
|
1078
1087
|
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'ping',id:this.#libp2p.peerId.toString(),address:this.address,rooms:this.#rooms})
|
|
1079
1088
|
const peer = {
|
|
1080
1089
|
publicKey: this.#libp2p.peerId.publicKey,
|
|
@@ -1622,11 +1631,13 @@ class webpeerjs{
|
|
|
1622
1631
|
const libp2p = await createLibp2p({
|
|
1623
1632
|
addresses: {
|
|
1624
1633
|
listen: [
|
|
1634
|
+
'/webrtc'
|
|
1625
1635
|
],
|
|
1626
1636
|
},
|
|
1627
1637
|
transports:[
|
|
1628
1638
|
webTransport(),
|
|
1629
1639
|
webSockets(),
|
|
1640
|
+
webRTC(),
|
|
1630
1641
|
circuitRelayTransport({
|
|
1631
1642
|
discoverRelays: config.CONFIG_DISCOVER_RELAYS,
|
|
1632
1643
|
reservationConcurrency: 1,
|
|
@@ -1691,7 +1702,7 @@ class webpeerjs{
|
|
|
1691
1702
|
allowPublishToZeroTopicPeers: true,
|
|
1692
1703
|
msgIdFn: msgIdFnStrictNoSign,
|
|
1693
1704
|
ignoreDuplicatePublishError: true,
|
|
1694
|
-
runOnTransientConnection:
|
|
1705
|
+
runOnTransientConnection:false,
|
|
1695
1706
|
}),
|
|
1696
1707
|
identify: identify(),
|
|
1697
1708
|
identifyPush: identifyPush(),
|
|
@@ -1700,7 +1711,7 @@ class webpeerjs{
|
|
|
1700
1711
|
peerInfoMapper: removePrivateAddressesMapper,
|
|
1701
1712
|
clientMode: false
|
|
1702
1713
|
}),
|
|
1703
|
-
|
|
1714
|
+
dcutr: dcutr()
|
|
1704
1715
|
},
|
|
1705
1716
|
peerStore: {
|
|
1706
1717
|
persistence: true,
|