webpeerjs 0.1.1 → 0.1.3
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 +7 -3
- package/dist/esm/webpeerjs.js +22 -18
- package/dist/umd/webpeerjs.js +24 -18
- package/package.json +10 -4
- package/src/config.js +4 -2
- package/src/peer.js +2 -0
- package/src/umd.js +1 -0
- package/src/utils.js +1 -0
- package/src/webpeerjs.js +94 -20
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webpeerjs",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.3",
|
|
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",
|
|
7
7
|
"exports": {
|
|
@@ -34,10 +34,16 @@
|
|
|
34
34
|
"keywords": [
|
|
35
35
|
"p2p",
|
|
36
36
|
"ipfs",
|
|
37
|
+
"libp2p",
|
|
37
38
|
"peer",
|
|
38
|
-
"peer
|
|
39
|
+
"peer-to-peer",
|
|
39
40
|
"decentralized",
|
|
40
|
-
"browser
|
|
41
|
+
"browser-to-browser",
|
|
42
|
+
"dapp",
|
|
43
|
+
"distributed",
|
|
44
|
+
"decentralized-web",
|
|
45
|
+
"distributed-web",
|
|
46
|
+
"serverless"
|
|
41
47
|
],
|
|
42
48
|
"author": {
|
|
43
49
|
"name": "Nuzulul Zulkarnain",
|
package/src/config.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//!
|
|
1
|
+
//! WebPEER.js -- https://github.com/nuzulul/webpeerjs
|
|
2
2
|
const prefix = 'webpeerjs'
|
|
3
3
|
export const CONFIG_PREFIX = prefix
|
|
4
4
|
export const CONFIG_PROTOCOL = '/'+prefix+'/1.0.0'
|
|
@@ -11,7 +11,8 @@ export const CONFIG_DISCOVER_RELAYS = 1
|
|
|
11
11
|
export const CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY = 'universal-connectivity-browser-peer-discovery'
|
|
12
12
|
export const CONFIG_PEER_DISCOVERY_GLOBAL = '_peer-discovery._p2p._pubsub'
|
|
13
13
|
export const CONFIG_PEER_DISCOVERY_WEBPEERJS= prefix+'-peer-discovery'
|
|
14
|
-
export const
|
|
14
|
+
export const CONFIG_PUBSUB_PEER_DISCOVERY_HYBRID = [CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY]
|
|
15
|
+
export const CONFIG_PUBSUB_PEER_DISCOVERY_WEBPEER = [CONFIG_PEER_DISCOVERY_GLOBAL, CONFIG_PEER_DISCOVERY_WEBPEERJS]
|
|
15
16
|
export const CONFIG_PUPSUB_PEER_DATA = ['_'+prefix+'-peer-data_']
|
|
16
17
|
export const CONFIG_PUPSUB_TOPIC = prefix+'-room'
|
|
17
18
|
export const CONFIG_DELEGATED_API = 'https://delegated-ipfs.dev'
|
|
@@ -19,6 +20,7 @@ export const CONFIG_DNS_RESOLVER = 'https://dns.google/resolve'
|
|
|
19
20
|
export const CONFIG_KNOWN_BOOTSTRAP_DNS = '_dnsaddr.bootstrap.libp2p.io'
|
|
20
21
|
export const CONFIG_JOIN_ROOM_VERSION = 1
|
|
21
22
|
export const CONFIG_TIMEOUT_DIAL_KNOWN_PEERS = 15000
|
|
23
|
+
export const CONFIG_RUN_ON_TRANSIENT_CONNECTION = true
|
|
22
24
|
|
|
23
25
|
// this list comes from https://github.com/ipfs/kubo/blob/196887cbe5fbcd41243c1dfb0db681a1cc2914ff/config/bootstrap_peers.go
|
|
24
26
|
export const CONFIG_KNOWN_DEFAULT_BOOTSTRAP_ADDRESSES = [
|
package/src/peer.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
//! WebPEER.js -- https://github.com/nuzulul/webpeerjs
|
|
2
|
+
|
|
1
3
|
//this code comes from https://github.com/libp2p/js-libp2p-pubsub-peer-discovery/blob/9d0da565f70e9b2403251c9d11dfc0b9b52babfa/src/peer.ts
|
|
2
4
|
|
|
3
5
|
import { decodeMessage, encodeMessage, message } from 'protons-runtime';
|
package/src/umd.js
CHANGED
package/src/utils.js
CHANGED
package/src/webpeerjs.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//!
|
|
1
|
+
//! WebPEER.js -- https://github.com/nuzulul/webpeerjs
|
|
2
2
|
import * as config from './config'
|
|
3
3
|
import {
|
|
4
4
|
mkErr,
|
|
@@ -145,7 +145,10 @@ class webpeerjs{
|
|
|
145
145
|
for(const topic of config.CONFIG_PUPSUB_PEER_DATA){
|
|
146
146
|
this.#libp2p.services.pubsub.subscribe(topic)
|
|
147
147
|
}
|
|
148
|
-
|
|
148
|
+
|
|
149
|
+
for(const topic of config.CONFIG_PUBSUB_PEER_DISCOVERY_HYBRID){
|
|
150
|
+
this.#libp2p.services.pubsub.subscribe(topic)
|
|
151
|
+
}
|
|
149
152
|
|
|
150
153
|
//listen to peer connect event
|
|
151
154
|
this.#libp2p.addEventListener("peer:connect",async (evt) => {
|
|
@@ -170,8 +173,10 @@ class webpeerjs{
|
|
|
170
173
|
//required by joinRoom version 1 to announce via universal connectivity
|
|
171
174
|
if(config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
|
|
172
175
|
setTimeout(()=>{
|
|
176
|
+
this.#peerDiscoveryHybrid()
|
|
173
177
|
this.#announce()
|
|
174
178
|
setTimeout(()=>{
|
|
179
|
+
this.#peerDiscoveryHybrid()
|
|
175
180
|
this.#announce()
|
|
176
181
|
},5000)
|
|
177
182
|
},1000)
|
|
@@ -358,6 +363,18 @@ class webpeerjs{
|
|
|
358
363
|
|
|
359
364
|
}
|
|
360
365
|
}
|
|
366
|
+
else if(prefix === 'hybrid'){
|
|
367
|
+
|
|
368
|
+
if(address.length>0 && !this.#connections.has(id)){
|
|
369
|
+
let mddrs = []
|
|
370
|
+
for(const addr of address){
|
|
371
|
+
const mddr = multiaddr(addr)
|
|
372
|
+
mddrs.push(mddr)
|
|
373
|
+
}
|
|
374
|
+
this.#dialMultiaddress(mddrs)
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
}
|
|
361
378
|
|
|
362
379
|
}catch(err){
|
|
363
380
|
//console.log('from '+event.detail.from.toString())
|
|
@@ -498,13 +515,14 @@ class webpeerjs{
|
|
|
498
515
|
const mddrs = []
|
|
499
516
|
peer.addresses.forEach((addr)=>{
|
|
500
517
|
const maddr = addr.multiaddr.toString()+'/p2p/'+id
|
|
501
|
-
if(maddr.includes('webtransport') && maddr.includes('certhash')
|
|
518
|
+
if(maddr.includes('webtransport') && maddr.includes('certhash')){
|
|
502
519
|
mddrs.push(maddr)
|
|
503
520
|
}
|
|
504
521
|
})
|
|
505
522
|
//this.#ListenAddressChange(mddrs)
|
|
506
523
|
this.address = mddrs
|
|
507
524
|
this.#ping()
|
|
525
|
+
this.#peerDiscoveryHybrid()
|
|
508
526
|
})
|
|
509
527
|
|
|
510
528
|
this.#libp2p.addEventListener('peer:identify', async (evt) => {
|
|
@@ -598,6 +616,10 @@ class webpeerjs{
|
|
|
598
616
|
this.#trackLastSeen()
|
|
599
617
|
},5e3)
|
|
600
618
|
|
|
619
|
+
setInterval(()=>{
|
|
620
|
+
this.#peerDiscoveryHybrid()
|
|
621
|
+
},10e3)
|
|
622
|
+
|
|
601
623
|
|
|
602
624
|
/*setTimeout(async()=>{
|
|
603
625
|
try{
|
|
@@ -662,7 +684,14 @@ class webpeerjs{
|
|
|
662
684
|
//join room version 1 user pupsub via pupsub peer discovery
|
|
663
685
|
if(config.CONFIG_JOIN_ROOM_VERSION == 1){
|
|
664
686
|
|
|
665
|
-
|
|
687
|
+
let topics = []
|
|
688
|
+
|
|
689
|
+
if(config.CONFIG_RUN_ON_TRANSIENT_CONNECTION == true){
|
|
690
|
+
topics = config.CONFIG_PUBSUB_PEER_DISCOVERY_HYBRID
|
|
691
|
+
}
|
|
692
|
+
else{
|
|
693
|
+
topics = config.CONFIG_PUPSUB_PEER_DATA
|
|
694
|
+
}
|
|
666
695
|
|
|
667
696
|
this.#rooms[room] = {
|
|
668
697
|
onMessage : () => {},
|
|
@@ -670,9 +699,14 @@ class webpeerjs{
|
|
|
670
699
|
sendMessage : async (message) => {
|
|
671
700
|
const msgId = (new Date()).getTime()
|
|
672
701
|
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,room,message,id:this.#libp2p.peerId.toString(),msgId})
|
|
702
|
+
const arr = uint8ArrayFromString(data)
|
|
703
|
+
const sizelimit = 102400 // 100KB
|
|
704
|
+
if(arr.byteLength > sizelimit){
|
|
705
|
+
throw mkErr('data too large')
|
|
706
|
+
}
|
|
673
707
|
const peer = {
|
|
674
708
|
publicKey: this.#libp2p.peerId.publicKey,
|
|
675
|
-
addrs: [
|
|
709
|
+
addrs: [arr],
|
|
676
710
|
}
|
|
677
711
|
const encodedPeer = PBPeer.encode(peer)
|
|
678
712
|
for(const topic of topics){
|
|
@@ -807,9 +841,9 @@ class webpeerjs{
|
|
|
807
841
|
}
|
|
808
842
|
|
|
809
843
|
await this.#libp2p.handle(config.CONFIG_PROTOCOL, handler, {
|
|
810
|
-
maxInboundStreams:
|
|
811
|
-
maxOutboundStreams:
|
|
812
|
-
runOnTransientConnection:
|
|
844
|
+
maxInboundStreams: 100,
|
|
845
|
+
maxOutboundStreams: 100,
|
|
846
|
+
runOnTransientConnection:config.CONFIG_RUN_ON_TRANSIENT_CONNECTION
|
|
813
847
|
})
|
|
814
848
|
|
|
815
849
|
await this.#libp2p.register(config.CONFIG_PROTOCOL, {
|
|
@@ -856,7 +890,7 @@ class webpeerjs{
|
|
|
856
890
|
|
|
857
891
|
try{
|
|
858
892
|
|
|
859
|
-
const stream = await this.#libp2p.dialProtocol(mddr, config.CONFIG_PROTOCOL,{runOnTransientConnection:
|
|
893
|
+
const stream = await this.#libp2p.dialProtocol(mddr, config.CONFIG_PROTOCOL,{runOnTransientConnection:config.CONFIG_RUN_ON_TRANSIENT_CONNECTION})
|
|
860
894
|
|
|
861
895
|
const output = await pipe(
|
|
862
896
|
message,
|
|
@@ -1075,9 +1109,29 @@ class webpeerjs{
|
|
|
1075
1109
|
}
|
|
1076
1110
|
|
|
1077
1111
|
|
|
1078
|
-
//announce and ping via pupsub peer discovery
|
|
1112
|
+
//announce and ping via pupsub peer discovery hybrid
|
|
1113
|
+
async #peerDiscoveryHybrid(){
|
|
1114
|
+
const topics = config.CONFIG_PUBSUB_PEER_DISCOVERY_HYBRID
|
|
1115
|
+
const data = JSON.stringify({prefix:'hybrid',id:this.#libp2p.peerId.toString(),address:this.address})
|
|
1116
|
+
const peer = {
|
|
1117
|
+
publicKey: this.#libp2p.peerId.publicKey,
|
|
1118
|
+
addrs: [uint8ArrayFromString(data)],
|
|
1119
|
+
}
|
|
1120
|
+
const encodedPeer = PBPeer.encode(peer)
|
|
1121
|
+
for(const topic of topics){
|
|
1122
|
+
await this.#libp2p.services.pubsub.publish(topic, encodedPeer)
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1079
1126
|
async #announce(){
|
|
1080
|
-
|
|
1127
|
+
let topics = []
|
|
1128
|
+
|
|
1129
|
+
if(config.CONFIG_RUN_ON_TRANSIENT_CONNECTION == true){
|
|
1130
|
+
topics = config.CONFIG_PUBSUB_PEER_DISCOVERY_HYBRID
|
|
1131
|
+
}
|
|
1132
|
+
else{
|
|
1133
|
+
topics = config.CONFIG_PUPSUB_PEER_DATA
|
|
1134
|
+
}
|
|
1081
1135
|
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'announce',id:this.#libp2p.peerId.toString(),address:this.address,rooms:Object.keys(this.#rooms)})
|
|
1082
1136
|
const peer = {
|
|
1083
1137
|
publicKey: this.#libp2p.peerId.publicKey,
|
|
@@ -1088,8 +1142,16 @@ class webpeerjs{
|
|
|
1088
1142
|
await this.#libp2p.services.pubsub.publish(topic, encodedPeer)
|
|
1089
1143
|
}
|
|
1090
1144
|
}
|
|
1145
|
+
|
|
1091
1146
|
async #ping(){
|
|
1092
|
-
|
|
1147
|
+
let topics = []
|
|
1148
|
+
|
|
1149
|
+
if(config.CONFIG_RUN_ON_TRANSIENT_CONNECTION == true){
|
|
1150
|
+
topics = config.CONFIG_PUBSUB_PEER_DISCOVERY_HYBRID
|
|
1151
|
+
}
|
|
1152
|
+
else{
|
|
1153
|
+
topics = config.CONFIG_PUPSUB_PEER_DATA
|
|
1154
|
+
}
|
|
1093
1155
|
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'ping',id:this.#libp2p.peerId.toString(),address:this.address,rooms:Object.keys(this.#rooms)})
|
|
1094
1156
|
const peer = {
|
|
1095
1157
|
publicKey: this.#libp2p.peerId.publicKey,
|
|
@@ -1633,17 +1695,29 @@ class webpeerjs{
|
|
|
1633
1695
|
let onMetricsFn = () => {}
|
|
1634
1696
|
const onMetrics = f => (onMetricsFn = f)
|
|
1635
1697
|
|
|
1698
|
+
let listenaddress = []
|
|
1699
|
+
|
|
1700
|
+
if(config.CONFIG_RUN_ON_TRANSIENT_CONNECTION == false){
|
|
1701
|
+
listenaddress.push('/webrtc')
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1636
1704
|
//create libp2p instance
|
|
1637
1705
|
const libp2p = await createLibp2p({
|
|
1638
1706
|
addresses: {
|
|
1639
|
-
listen:
|
|
1640
|
-
'/webrtc'
|
|
1641
|
-
],
|
|
1707
|
+
listen: listenaddress,
|
|
1642
1708
|
},
|
|
1643
1709
|
transports:[
|
|
1644
1710
|
webTransport(),
|
|
1645
1711
|
webSockets(),
|
|
1646
|
-
webRTC(
|
|
1712
|
+
webRTC({
|
|
1713
|
+
rtcConfiguration: {
|
|
1714
|
+
iceServers: [
|
|
1715
|
+
{
|
|
1716
|
+
urls: ['stun:stun.l.google.com:19302', 'stun:global.stun.twilio.com:3478'],
|
|
1717
|
+
},
|
|
1718
|
+
],
|
|
1719
|
+
},
|
|
1720
|
+
}),
|
|
1647
1721
|
circuitRelayTransport({
|
|
1648
1722
|
discoverRelays: config.CONFIG_DISCOVER_RELAYS,
|
|
1649
1723
|
reservationConcurrency: 1,
|
|
@@ -1669,8 +1743,8 @@ class webpeerjs{
|
|
|
1669
1743
|
connectionEncryption: [noise()],
|
|
1670
1744
|
streamMuxers: [
|
|
1671
1745
|
yamux({
|
|
1672
|
-
maxInboundStreams:
|
|
1673
|
-
maxOutboundStreams:
|
|
1746
|
+
maxInboundStreams: 100,
|
|
1747
|
+
maxOutboundStreams: 100,
|
|
1674
1748
|
})
|
|
1675
1749
|
],
|
|
1676
1750
|
connectionGater: {
|
|
@@ -1698,7 +1772,7 @@ class webpeerjs{
|
|
|
1698
1772
|
peerDiscovery: [
|
|
1699
1773
|
pubsubPeerDiscovery({
|
|
1700
1774
|
interval: 10_000,
|
|
1701
|
-
topics: config.
|
|
1775
|
+
topics: config.CONFIG_PUBSUB_PEER_DISCOVERY_WEBPEER,
|
|
1702
1776
|
listenOnly: false,
|
|
1703
1777
|
}),
|
|
1704
1778
|
|
|
@@ -1708,7 +1782,7 @@ class webpeerjs{
|
|
|
1708
1782
|
allowPublishToZeroTopicPeers: true,
|
|
1709
1783
|
msgIdFn: msgIdFnStrictNoSign,
|
|
1710
1784
|
ignoreDuplicatePublishError: true,
|
|
1711
|
-
runOnTransientConnection:
|
|
1785
|
+
runOnTransientConnection:config.CONFIG_RUN_ON_TRANSIENT_CONNECTION,
|
|
1712
1786
|
}),
|
|
1713
1787
|
identify: identify(),
|
|
1714
1788
|
identifyPush: identifyPush(),
|