webpeerjs 0.1.2 → 0.1.4
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 +2 -2
- package/dist/esm/webpeerjs.js +18 -18
- package/dist/umd/webpeerjs.js +19 -19
- package/package.json +1 -1
- package/src/config.js +12 -1
- package/src/utils.js +1 -0
- package/src/webpeerjs.js +156 -27
package/package.json
CHANGED
package/src/config.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
//! WebPEER.js -- https://github.com/nuzulul/webpeerjs
|
|
2
|
+
|
|
2
3
|
const prefix = 'webpeerjs'
|
|
3
4
|
export const CONFIG_PREFIX = prefix
|
|
4
5
|
export const CONFIG_PROTOCOL = '/'+prefix+'/1.0.0'
|
|
5
6
|
export const CONFIG_BLOCKSTORE_PATH = prefix+'-blockstore'
|
|
6
7
|
export const CONFIG_DATASTORE_PATH = prefix+'-datastore'
|
|
7
8
|
export const CONFIG_DBSTORE_PATH = prefix+'-dbstore'
|
|
8
|
-
export const CONFIG_MAX_CONNECTIONS =
|
|
9
|
+
export const CONFIG_MAX_CONNECTIONS = 50
|
|
9
10
|
export const CONFIG_MIN_CONNECTIONS = 0
|
|
10
11
|
export const CONFIG_DISCOVER_RELAYS = 1
|
|
11
12
|
export const CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY = 'universal-connectivity-browser-peer-discovery'
|
|
@@ -20,6 +21,16 @@ export const CONFIG_DNS_RESOLVER = 'https://dns.google/resolve'
|
|
|
20
21
|
export const CONFIG_KNOWN_BOOTSTRAP_DNS = '_dnsaddr.bootstrap.libp2p.io'
|
|
21
22
|
export const CONFIG_JOIN_ROOM_VERSION = 1
|
|
22
23
|
export const CONFIG_TIMEOUT_DIAL_KNOWN_PEERS = 15000
|
|
24
|
+
export const CONFIG_RUN_ON_TRANSIENT_CONNECTION = false
|
|
25
|
+
export const CONFIG_WEBRTC_STUN_URLS = 'stun:stun.l.google.com:19302'
|
|
26
|
+
export const CONFIG_WEBRTC_STUN_URLS_BACKUP = 'stun:global.stun.twilio.com:3478'
|
|
27
|
+
export const CONFIG_WEBRTC_TURN_HOST = 'dHVybjpyZWxheTEuZXhwcmVzc3R1cm4uY29tOjM0Nzg='
|
|
28
|
+
export const CONFIG_WEBRTC_TURN_USER = 'ZWZJSllZNjdDNElRMzFZQUlP'
|
|
29
|
+
export const CONFIG_WEBRTC_TURN_PWD = 'Vk01SmdhODlkYjJaWU9aSA=='
|
|
30
|
+
export const CONFIG_WEBRTC_TURN_HOST_BACKUP = 'dHVybjpzdGFuZGFyZC5yZWxheS5tZXRlcmVkLmNhOjgw'
|
|
31
|
+
export const CONFIG_WEBRTC_TURN_USER_BACKUP = 'ZmZlNmIxOThjOGMxYjM5ODg1OWFiOGY4'
|
|
32
|
+
export const CONFIG_WEBRTC_TURN_PWD_BACKUP = 'aWpkQjVTcTIwREVsZzdDRg=='
|
|
33
|
+
export const CONFIG_MESSAGE_SIZE_LIMIT = 10240 // 10KB
|
|
23
34
|
|
|
24
35
|
// this list comes from https://github.com/ipfs/kubo/blob/196887cbe5fbcd41243c1dfb0db681a1cc2914ff/config/bootstrap_peers.go
|
|
25
36
|
export const CONFIG_KNOWN_DEFAULT_BOOTSTRAP_ADDRESSES = [
|
package/src/utils.js
CHANGED
package/src/webpeerjs.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
//! WebPEER.js -- https://github.com/nuzulul/webpeerjs
|
|
2
|
+
|
|
2
3
|
import * as config from './config'
|
|
3
4
|
import {
|
|
4
5
|
mkErr,
|
|
@@ -515,7 +516,7 @@ class webpeerjs{
|
|
|
515
516
|
const mddrs = []
|
|
516
517
|
peer.addresses.forEach((addr)=>{
|
|
517
518
|
const maddr = addr.multiaddr.toString()+'/p2p/'+id
|
|
518
|
-
if(maddr.includes('webtransport') && maddr.includes('certhash')
|
|
519
|
+
if(maddr.includes('webtransport') && maddr.includes('certhash')){
|
|
519
520
|
mddrs.push(maddr)
|
|
520
521
|
}
|
|
521
522
|
})
|
|
@@ -684,7 +685,14 @@ class webpeerjs{
|
|
|
684
685
|
//join room version 1 user pupsub via pupsub peer discovery
|
|
685
686
|
if(config.CONFIG_JOIN_ROOM_VERSION == 1){
|
|
686
687
|
|
|
687
|
-
|
|
688
|
+
let topics = []
|
|
689
|
+
|
|
690
|
+
if(config.CONFIG_RUN_ON_TRANSIENT_CONNECTION == true){
|
|
691
|
+
topics = config.CONFIG_PUBSUB_PEER_DISCOVERY_HYBRID
|
|
692
|
+
}
|
|
693
|
+
else{
|
|
694
|
+
topics = config.CONFIG_PUPSUB_PEER_DATA
|
|
695
|
+
}
|
|
688
696
|
|
|
689
697
|
this.#rooms[room] = {
|
|
690
698
|
onMessage : () => {},
|
|
@@ -693,9 +701,9 @@ class webpeerjs{
|
|
|
693
701
|
const msgId = (new Date()).getTime()
|
|
694
702
|
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,room,message,id:this.#libp2p.peerId.toString(),msgId})
|
|
695
703
|
const arr = uint8ArrayFromString(data)
|
|
696
|
-
const sizelimit =
|
|
704
|
+
const sizelimit = config.CONFIG_MESSAGE_SIZE_LIMIT
|
|
697
705
|
if(arr.byteLength > sizelimit){
|
|
698
|
-
throw mkErr('
|
|
706
|
+
throw mkErr('message too large')
|
|
699
707
|
}
|
|
700
708
|
const peer = {
|
|
701
709
|
publicKey: this.#libp2p.peerId.publicKey,
|
|
@@ -834,9 +842,9 @@ class webpeerjs{
|
|
|
834
842
|
}
|
|
835
843
|
|
|
836
844
|
await this.#libp2p.handle(config.CONFIG_PROTOCOL, handler, {
|
|
837
|
-
maxInboundStreams:
|
|
838
|
-
maxOutboundStreams:
|
|
839
|
-
runOnTransientConnection:
|
|
845
|
+
maxInboundStreams: 100,
|
|
846
|
+
maxOutboundStreams: 100,
|
|
847
|
+
runOnTransientConnection:config.CONFIG_RUN_ON_TRANSIENT_CONNECTION
|
|
840
848
|
})
|
|
841
849
|
|
|
842
850
|
await this.#libp2p.register(config.CONFIG_PROTOCOL, {
|
|
@@ -883,7 +891,7 @@ class webpeerjs{
|
|
|
883
891
|
|
|
884
892
|
try{
|
|
885
893
|
|
|
886
|
-
const stream = await this.#libp2p.dialProtocol(mddr, config.CONFIG_PROTOCOL,{runOnTransientConnection:
|
|
894
|
+
const stream = await this.#libp2p.dialProtocol(mddr, config.CONFIG_PROTOCOL,{runOnTransientConnection:config.CONFIG_RUN_ON_TRANSIENT_CONNECTION})
|
|
887
895
|
|
|
888
896
|
const output = await pipe(
|
|
889
897
|
message,
|
|
@@ -1025,7 +1033,7 @@ class webpeerjs{
|
|
|
1025
1033
|
|
|
1026
1034
|
|
|
1027
1035
|
//add multiaddr address to queue list
|
|
1028
|
-
#dialMultiaddress(mddrs){
|
|
1036
|
+
async #dialMultiaddress(mddrs){
|
|
1029
1037
|
if(mddrs.length>0){
|
|
1030
1038
|
|
|
1031
1039
|
const id = mddrs[0].toString().split('/').pop()
|
|
@@ -1043,10 +1051,21 @@ class webpeerjs{
|
|
|
1043
1051
|
const limitCount = config.CONFIG_MAX_CONNECTIONS / 2
|
|
1044
1052
|
|
|
1045
1053
|
if(this.#webPeersId.includes(id)){
|
|
1046
|
-
if(webPeerCount>limitCount)
|
|
1054
|
+
if(webPeerCount>limitCount){
|
|
1055
|
+
return
|
|
1056
|
+
}
|
|
1047
1057
|
}
|
|
1048
1058
|
else{
|
|
1049
|
-
if(nodePeerCount>limitCount)
|
|
1059
|
+
if(nodePeerCount>limitCount){
|
|
1060
|
+
//close random peers
|
|
1061
|
+
let peers = []
|
|
1062
|
+
for(const peer of this.#libp2p.getPeers()){
|
|
1063
|
+
peers.push(peer.toString())
|
|
1064
|
+
}
|
|
1065
|
+
const randomKey = Math.floor(Math.random() * peers.length)
|
|
1066
|
+
const randompeerid = peers[randomKey]
|
|
1067
|
+
await this.#libp2p.hangUp(peerIdFromString(randompeerid))
|
|
1068
|
+
}
|
|
1050
1069
|
}
|
|
1051
1070
|
|
|
1052
1071
|
if(this.#webPeersId.includes(id) || config.CONFIG_KNOWN_BOOTSTRAP_PEERS_IDS.includes(id) || config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
|
|
@@ -1117,7 +1136,14 @@ class webpeerjs{
|
|
|
1117
1136
|
}
|
|
1118
1137
|
|
|
1119
1138
|
async #announce(){
|
|
1120
|
-
|
|
1139
|
+
let topics = []
|
|
1140
|
+
|
|
1141
|
+
if(config.CONFIG_RUN_ON_TRANSIENT_CONNECTION == true){
|
|
1142
|
+
topics = config.CONFIG_PUBSUB_PEER_DISCOVERY_HYBRID
|
|
1143
|
+
}
|
|
1144
|
+
else{
|
|
1145
|
+
topics = config.CONFIG_PUPSUB_PEER_DATA
|
|
1146
|
+
}
|
|
1121
1147
|
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'announce',id:this.#libp2p.peerId.toString(),address:this.address,rooms:Object.keys(this.#rooms)})
|
|
1122
1148
|
const peer = {
|
|
1123
1149
|
publicKey: this.#libp2p.peerId.publicKey,
|
|
@@ -1130,7 +1156,14 @@ class webpeerjs{
|
|
|
1130
1156
|
}
|
|
1131
1157
|
|
|
1132
1158
|
async #ping(){
|
|
1133
|
-
|
|
1159
|
+
let topics = []
|
|
1160
|
+
|
|
1161
|
+
if(config.CONFIG_RUN_ON_TRANSIENT_CONNECTION == true){
|
|
1162
|
+
topics = config.CONFIG_PUBSUB_PEER_DISCOVERY_HYBRID
|
|
1163
|
+
}
|
|
1164
|
+
else{
|
|
1165
|
+
topics = config.CONFIG_PUPSUB_PEER_DATA
|
|
1166
|
+
}
|
|
1134
1167
|
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'ping',id:this.#libp2p.peerId.toString(),address:this.address,rooms:Object.keys(this.#rooms)})
|
|
1135
1168
|
const peer = {
|
|
1136
1169
|
publicKey: this.#libp2p.peerId.publicKey,
|
|
@@ -1340,13 +1373,13 @@ class webpeerjs{
|
|
|
1340
1373
|
#dialKnownPeers(){
|
|
1341
1374
|
setTimeout(()=>{
|
|
1342
1375
|
this.#dialSavedKnownID()
|
|
1343
|
-
setTimeout(()=>{this.#dialUpdateSavedKnownID()},
|
|
1344
|
-
setTimeout(()=>{this.#findHybridPeer()},
|
|
1376
|
+
setTimeout(()=>{this.#dialUpdateSavedKnownID()},20000)
|
|
1377
|
+
setTimeout(()=>{this.#findHybridPeer()},30000)
|
|
1345
1378
|
setTimeout(()=>{
|
|
1346
1379
|
const peers = this.#libp2p.getPeers().length
|
|
1347
1380
|
if(peers == 0){
|
|
1348
1381
|
this.#dialKnownID()
|
|
1349
|
-
setTimeout(()=>{this.#findHybridPeer()},
|
|
1382
|
+
setTimeout(()=>{this.#findHybridPeer()},30000)
|
|
1350
1383
|
setTimeout(()=>{
|
|
1351
1384
|
const peers = this.#libp2p.getPeers().length
|
|
1352
1385
|
if(peers == 0){
|
|
@@ -1674,23 +1707,119 @@ class webpeerjs{
|
|
|
1674
1707
|
let onMetricsFn = () => {}
|
|
1675
1708
|
const onMetrics = f => (onMetricsFn = f)
|
|
1676
1709
|
|
|
1710
|
+
let listenaddress = []
|
|
1711
|
+
|
|
1712
|
+
if(config.CONFIG_RUN_ON_TRANSIENT_CONNECTION == false){
|
|
1713
|
+
listenaddress.push('/webrtc')
|
|
1714
|
+
}
|
|
1715
|
+
|
|
1716
|
+
const turnurls = atob(config.CONFIG_WEBRTC_TURN_HOST)
|
|
1717
|
+
const turnusername = atob(config.CONFIG_WEBRTC_TURN_USER)
|
|
1718
|
+
const turncredential = atob(config.CONFIG_WEBRTC_TURN_PWD)
|
|
1719
|
+
|
|
1720
|
+
const turnurlsbackup = atob(config.CONFIG_WEBRTC_TURN_HOST_BACKUP)
|
|
1721
|
+
const turnusernamebackup = atob(config.CONFIG_WEBRTC_TURN_USER_BACKUP)
|
|
1722
|
+
const turncredentialbackup = atob(config.CONFIG_WEBRTC_TURN_PWD_BACKUP)
|
|
1723
|
+
|
|
1724
|
+
const ice = await new Promise((resolve)=>{
|
|
1725
|
+
|
|
1726
|
+
let stun = false
|
|
1727
|
+
let turn = false
|
|
1728
|
+
|
|
1729
|
+
const timeout = setTimeout(()=>{
|
|
1730
|
+
let ice = []
|
|
1731
|
+
if(stun){
|
|
1732
|
+
ice.push(stun)
|
|
1733
|
+
}else{
|
|
1734
|
+
const backup = {
|
|
1735
|
+
urls: config.CONFIG_WEBRTC_STUN_URLS_BACKUP
|
|
1736
|
+
}
|
|
1737
|
+
ice.push(backup)
|
|
1738
|
+
}
|
|
1739
|
+
if(turn){
|
|
1740
|
+
ice.push(turn)
|
|
1741
|
+
}else{
|
|
1742
|
+
const backup = {
|
|
1743
|
+
urls: turnurlsbackup,
|
|
1744
|
+
username: turnusernamebackup,
|
|
1745
|
+
credential: turncredentialbackup
|
|
1746
|
+
}
|
|
1747
|
+
ice.push(backup)
|
|
1748
|
+
}
|
|
1749
|
+
resolve(ice)
|
|
1750
|
+
},5000)
|
|
1751
|
+
|
|
1752
|
+
function check(){
|
|
1753
|
+
if(stun && turn){
|
|
1754
|
+
let ice = []
|
|
1755
|
+
ice.push(stun)
|
|
1756
|
+
ice.push(turn)
|
|
1757
|
+
clearTimeout(timeout)
|
|
1758
|
+
resolve(ice)
|
|
1759
|
+
}
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
//test ice servers
|
|
1763
|
+
|
|
1764
|
+
const iceServers = [
|
|
1765
|
+
{
|
|
1766
|
+
urls: config.CONFIG_WEBRTC_STUN_URLS
|
|
1767
|
+
},
|
|
1768
|
+
{
|
|
1769
|
+
urls: turnurls,
|
|
1770
|
+
username: turnusername,
|
|
1771
|
+
credential: turncredential
|
|
1772
|
+
}
|
|
1773
|
+
];
|
|
1774
|
+
|
|
1775
|
+
const pc = new RTCPeerConnection({
|
|
1776
|
+
iceServers
|
|
1777
|
+
});
|
|
1778
|
+
|
|
1779
|
+
pc.onicecandidate = (e) => {
|
|
1780
|
+
if (!e.candidate) return;
|
|
1781
|
+
|
|
1782
|
+
//console.log(e.candidate.candidate);
|
|
1783
|
+
|
|
1784
|
+
// stun works
|
|
1785
|
+
if(e.candidate.type == "srflx"){
|
|
1786
|
+
//console.log('publicip',e.candidate.address);
|
|
1787
|
+
stun = {
|
|
1788
|
+
urls: config.CONFIG_WEBRTC_STUN_URLS
|
|
1789
|
+
}
|
|
1790
|
+
check()
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
// turn works
|
|
1794
|
+
if(e.candidate.type == "relay"){
|
|
1795
|
+
turn = {
|
|
1796
|
+
urls: turnurls,
|
|
1797
|
+
username: turnusername,
|
|
1798
|
+
credential: turncredential
|
|
1799
|
+
}
|
|
1800
|
+
check()
|
|
1801
|
+
}
|
|
1802
|
+
};
|
|
1803
|
+
|
|
1804
|
+
pc.onicecandidateerror = (e) => {
|
|
1805
|
+
//console.error(e);
|
|
1806
|
+
};
|
|
1807
|
+
|
|
1808
|
+
pc.createDataChannel('webpeerjs');
|
|
1809
|
+
pc.createOffer().then(offer => pc.setLocalDescription(offer));
|
|
1810
|
+
})
|
|
1811
|
+
|
|
1677
1812
|
//create libp2p instance
|
|
1678
1813
|
const libp2p = await createLibp2p({
|
|
1679
1814
|
addresses: {
|
|
1680
|
-
listen:
|
|
1681
|
-
'/webrtc'
|
|
1682
|
-
],
|
|
1815
|
+
listen: listenaddress,
|
|
1683
1816
|
},
|
|
1684
1817
|
transports:[
|
|
1685
1818
|
webTransport(),
|
|
1686
1819
|
webSockets(),
|
|
1687
1820
|
webRTC({
|
|
1688
1821
|
rtcConfiguration: {
|
|
1689
|
-
|
|
1690
|
-
{
|
|
1691
|
-
urls: ['stun:stun.l.google.com:19302', 'stun:global.stun.twilio.com:3478'],
|
|
1692
|
-
},
|
|
1693
|
-
],
|
|
1822
|
+
iceServers: ice,
|
|
1694
1823
|
},
|
|
1695
1824
|
}),
|
|
1696
1825
|
circuitRelayTransport({
|
|
@@ -1718,8 +1847,8 @@ class webpeerjs{
|
|
|
1718
1847
|
connectionEncryption: [noise()],
|
|
1719
1848
|
streamMuxers: [
|
|
1720
1849
|
yamux({
|
|
1721
|
-
maxInboundStreams:
|
|
1722
|
-
maxOutboundStreams:
|
|
1850
|
+
maxInboundStreams: 100,
|
|
1851
|
+
maxOutboundStreams: 100,
|
|
1723
1852
|
})
|
|
1724
1853
|
],
|
|
1725
1854
|
connectionGater: {
|
|
@@ -1757,7 +1886,7 @@ class webpeerjs{
|
|
|
1757
1886
|
allowPublishToZeroTopicPeers: true,
|
|
1758
1887
|
msgIdFn: msgIdFnStrictNoSign,
|
|
1759
1888
|
ignoreDuplicatePublishError: true,
|
|
1760
|
-
runOnTransientConnection:
|
|
1889
|
+
runOnTransientConnection:config.CONFIG_RUN_ON_TRANSIENT_CONNECTION,
|
|
1761
1890
|
}),
|
|
1762
1891
|
identify: identify(),
|
|
1763
1892
|
identifyPush: identifyPush(),
|