webpeerjs 0.1.7 → 0.1.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webpeerjs",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
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",
@@ -32,17 +32,17 @@
32
32
  "url": "git+https://github.com/nuzulul/webpeerjs.git"
33
33
  },
34
34
  "keywords": [
35
- "p2p",
35
+ "webpeer",
36
+ "p2p",
37
+ "p2p-network",
38
+ "webpeer-network",
36
39
  "ipfs",
37
40
  "libp2p",
38
41
  "peer",
39
42
  "peer-to-peer",
40
43
  "decentralized",
41
44
  "browser-to-browser",
42
- "dapp",
43
45
  "distributed",
44
- "decentralized-web",
45
- "distributed-web",
46
46
  "serverless"
47
47
  ],
48
48
  "author": {
package/src/utils.js CHANGED
@@ -121,6 +121,7 @@ let lastStats = {
121
121
  }
122
122
 
123
123
  let fail = 0
124
+ let lastfail = 0
124
125
  let isDialEnabled = true
125
126
  let lastfailtreshold = 0
126
127
  let isAutoDialEnabled = true
@@ -185,6 +186,20 @@ export function metrics(data){
185
186
 
186
187
  fail = errors+timeouts
187
188
  //const treshold = errors+timeouts+stats.open+stats.pending
189
+ //console.log('fail',fail)
190
+
191
+ //detect fail timeout, in chrome fail webtransport get reset after 5 minutes
192
+ if(lastfail>fail){
193
+ setTimeout(()=>{
194
+ if(isAutoDialEnabled){
195
+ lastfailtresholdauto = lastfailtresholdauto+(lastfail-fail)
196
+ }
197
+ if(isDialEnabled){
198
+ lastfailtreshold = lastfailtreshold+(lastfail-fail)
199
+ }
200
+ },5*60*1000)
201
+ }
202
+ lastfail = fail
188
203
 
189
204
  if ((fail-lastfailtreshold)>config.CONFIG_DIAL_MAX_ERROR_LIMIT){
190
205
  if(isDialEnabled){
package/src/webpeerjs.js CHANGED
@@ -92,6 +92,9 @@ class webpeerjs{
92
92
  //message tracker avoid double
93
93
  #msgIdtracker
94
94
 
95
+ //inbound message time tracker
96
+ #msgTimeTracker
97
+
95
98
  //map of peer exchange data
96
99
  #peerexchangedata
97
100
 
@@ -105,6 +108,9 @@ class webpeerjs{
105
108
  //callback to websocket dialable
106
109
  #onWebsocketFn
107
110
 
111
+ //time tracker for sending message
112
+ #sendMessageTimeTracker
113
+
108
114
  id
109
115
  status
110
116
  IPFS
@@ -133,11 +139,13 @@ class webpeerjs{
133
139
  this.#trackDisconnect = new Map()
134
140
  this.#dialQueue = []
135
141
  this.#isDialEnabled = true
136
- this.#msgIdtracker = []
142
+ this.#msgIdtracker = new Map()
137
143
  this.#peerexchangedata = new Map()
138
144
  this.#lastTimeConnectToNetwork = new Date().getTime()
139
145
  this.#lastTimeReceiveData = new Date().getTime()
140
146
  this.#onConnectQueue = []
147
+ this.#msgTimeTracker = new Map()
148
+ this.#sendMessageTimeTracker = 0
141
149
 
142
150
  this.peers = (function(f) {
143
151
  return f
@@ -386,10 +394,17 @@ class webpeerjs{
386
394
  }
387
395
 
388
396
  //inbound message
397
+ //use #msgIdtracker to prevent double message
398
+ //use #msgTimeTracker to limit inbound message 1msg/s
389
399
  if(message){
390
400
  const msgID = msgId+id
391
- if(!this.#msgIdtracker.includes(msgID)){
392
- this.#msgIdtracker.push(msgID)
401
+ let oldmsgtime = 0
402
+ let newmsgtime = new Date().getTime()
403
+ if(this.#msgTimeTracker.has(id))oldmsgtime = this.#msgTimeTracker.get(id)
404
+ const msgtimelimit = 1000
405
+ if(!this.#msgIdtracker.has(msgID) && newmsgtime-oldmsgtime>msgtimelimit){
406
+ this.#msgIdtracker.set(msgID,newmsgtime)
407
+ this.#msgTimeTracker.set(id,newmsgtime)
393
408
  this.#rooms[room].onMessage(message,id)
394
409
  }
395
410
  }
@@ -695,6 +710,7 @@ class webpeerjs{
695
710
  this.#peerDiscoveryHybrid()
696
711
  this.#trackHybridPeersConnection()
697
712
  this.#trackWebpeerConnection()
713
+ this.#garbageCollectionMsgIdTracker()
698
714
  },10e3)
699
715
 
700
716
 
@@ -774,6 +790,11 @@ class webpeerjs{
774
790
  onMessage : () => {},
775
791
  listenMessage : f => (this.#rooms[room] = {...this.#rooms[room], onMessage: f}),
776
792
  sendMessage : async (message) => {
793
+ const now = (new Date()).getTime()
794
+ if(now-this.#sendMessageTimeTracker < 1000){
795
+ throw mkErr('can not send more than 1 message/s')
796
+ }
797
+ this.#sendMessageTimeTracker = now
777
798
  const msgId = (new Date()).getTime()
778
799
  const data = JSON.stringify({prefix:config.CONFIG_PREFIX,room,message,id:this.#libp2p.peerId.toString(),msgId})
779
800
  const arr = uint8ArrayFromString(data)
@@ -809,12 +830,25 @@ class webpeerjs{
809
830
  mddrs.push(mddr)
810
831
  this.#dialMultiaddress(mddrs)
811
832
  }
833
+
834
+ plugin(callback){
835
+ callback(this)
836
+ }
812
837
 
813
838
 
814
839
  /*
815
840
  PRIVATE FUNCTION
816
841
  */
817
842
 
843
+ #garbageCollectionMsgIdTracker(){
844
+ const limit = 60*1000
845
+ for(const msg of this.#msgIdtracker){
846
+ if(msg[1] > limit){
847
+ this.#msgIdtracker.delete(msg[0])
848
+ }
849
+ }
850
+ }
851
+
818
852
  //track hybrid peer connection and try to dial if not connected at least 1
819
853
  #trackHybridPeersConnection(){
820
854
  let isConnectedToHybridPeers = false
@@ -1863,6 +1897,9 @@ class webpeerjs{
1863
1897
  listenaddress.push('/webrtc')
1864
1898
  }
1865
1899
 
1900
+ const stunurls = config.CONFIG_WEBRTC_STUN_URLS
1901
+ const stunurlsbackup = config.CONFIG_WEBRTC_STUN_URLS_BACKUP
1902
+
1866
1903
  const turnurls = atob(config.CONFIG_WEBRTC_TURN_HOST)
1867
1904
  const turnusername = atob(config.CONFIG_WEBRTC_TURN_USER)
1868
1905
  const turncredential = atob(config.CONFIG_WEBRTC_TURN_PWD)
@@ -1871,95 +1908,114 @@ class webpeerjs{
1871
1908
  const turnusernamebackup = atob(config.CONFIG_WEBRTC_TURN_USER_BACKUP)
1872
1909
  const turncredentialbackup = atob(config.CONFIG_WEBRTC_TURN_PWD_BACKUP)
1873
1910
 
1874
- const ice = await new Promise((resolve)=>{
1875
-
1876
- let stun = false
1877
- let turn = false
1878
-
1879
- const timeout = setTimeout(()=>{
1880
- let ice = []
1881
- if(stun){
1882
- ice.push(stun)
1883
- }else{
1884
- const backup = {
1885
- urls: config.CONFIG_WEBRTC_STUN_URLS_BACKUP
1886
- }
1887
- ice.push(backup)
1888
- }
1889
- if(turn){
1890
- ice.push(turn)
1891
- }else{
1892
- const backup = {
1893
- urls: turnurlsbackup,
1894
- username: turnusernamebackup,
1895
- credential: turncredentialbackup
1896
- }
1897
- ice.push(backup)
1898
- }
1899
- resolve(ice)
1900
- },5000)
1901
-
1902
- function check(){
1903
- if(stun && turn){
1911
+ async function checkice(stunurls,turnurls,turnusername,turncredential,time){
1912
+ return new Promise((resolve)=>{
1913
+
1914
+ let stun = false
1915
+ let turn = false
1916
+
1917
+ const timeout = setTimeout(()=>{
1904
1918
  let ice = []
1905
1919
  ice.push(stun)
1906
1920
  ice.push(turn)
1907
- clearTimeout(timeout)
1908
1921
  resolve(ice)
1922
+ },time)
1923
+
1924
+ function check(){
1925
+ if(stun && turn){
1926
+ let ice = []
1927
+ ice.push(stun)
1928
+ ice.push(turn)
1929
+ clearTimeout(timeout)
1930
+ resolve(ice)
1931
+ }
1909
1932
  }
1910
- }
1911
-
1912
- //test ice servers
1913
-
1914
- const iceServers = [
1915
- {
1916
- urls: config.CONFIG_WEBRTC_STUN_URLS
1917
- },
1918
- {
1919
- urls: turnurls,
1920
- username: turnusername,
1921
- credential: turncredential
1922
- }
1923
- ];
1933
+
1934
+ //test ice servers
1935
+
1936
+ const iceServers = [
1937
+ {
1938
+ urls: stunurls
1939
+ },
1940
+ {
1941
+ urls: turnurls,
1942
+ username: turnusername,
1943
+ credential: turncredential
1944
+ }
1945
+ ];
1924
1946
 
1925
- const pc = new RTCPeerConnection({
1926
- iceServers
1927
- });
1947
+ const pc = new RTCPeerConnection({
1948
+ iceServers
1949
+ });
1928
1950
 
1929
- pc.onicecandidate = (e) => {
1930
- if (!e.candidate) return;
1951
+ pc.onicecandidate = (e) => {
1952
+ if (!e.candidate) return;
1931
1953
 
1932
- //console.log(e.candidate.candidate);
1954
+ //console.log(e.candidate.candidate);
1933
1955
 
1934
- // stun works
1935
- if(e.candidate.type == "srflx"){
1936
- //console.log('publicip',e.candidate.address);
1937
- stun = {
1938
- urls: config.CONFIG_WEBRTC_STUN_URLS
1939
- }
1940
- check()
1941
- }
1956
+ // stun works
1957
+ if(e.candidate.type == "srflx"){
1958
+ //console.log('publicip',e.candidate.address);
1959
+ stun = {
1960
+ urls: stunurls
1961
+ }
1962
+ check()
1963
+ }
1942
1964
 
1943
- // turn works
1944
- if(e.candidate.type == "relay"){
1945
- turn = {
1946
- urls: turnurls,
1947
- username: turnusername,
1948
- credential: turncredential
1949
- }
1950
- check()
1951
- }
1952
- };
1965
+ // turn works
1966
+ if(e.candidate.type == "relay"){
1967
+ turn = {
1968
+ urls: turnurls,
1969
+ username: turnusername,
1970
+ credential: turncredential
1971
+ }
1972
+ check()
1973
+ }
1974
+ };
1953
1975
 
1954
- pc.onicecandidateerror = (e) => {
1955
- console.debug(e);
1956
- };
1976
+ pc.onicecandidateerror = (e) => {
1977
+ console.debug(e);
1978
+ };
1957
1979
 
1958
- pc.createDataChannel('webpeerjs');
1959
- pc.createOffer().then(offer => pc.setLocalDescription(offer));
1960
- })
1980
+ pc.createDataChannel('webpeerjs');
1981
+ pc.createOffer().then(offer => pc.setLocalDescription(offer));
1982
+ })
1983
+ }
1984
+
1985
+ let ice = await checkice(stunurls,turnurls,turnusername,turncredential,5000)
1986
+
1987
+ //console.log(ice)
1988
+
1989
+ //recheck ice
1990
+ if(!ice[0] && !ice[1]){
1991
+ ice = await checkice(stunurlsbackup,turnurlsbackup,turnusernamebackup,turncredentialbackup,5000)
1992
+ }else if (ice[0] && !ice[1]){
1993
+ ice = await checkice(stunurls,turnurlsbackup,turnusernamebackup,turncredentialbackup,5000)
1994
+ }else if (!ice[0] && ice[1]){
1995
+ ice = await checkice(stunurlsbackup,turnurls,turnusername,turncredential,5000)
1996
+ }
1997
+
1961
1998
  //console.log(ice)
1962
1999
 
2000
+ //final ice remove false value
2001
+ ice.forEach(function(value, index) {
2002
+ if(!value){
2003
+ this.splice(index, 1)
2004
+ }
2005
+ }, ice);
2006
+
2007
+ //console.log(ice)
2008
+
2009
+ let configuration = {}
2010
+
2011
+ if(arguments.length > 0){
2012
+ configuration = arguments[0]
2013
+ }else{
2014
+ configuration.rtcConfiguration = {
2015
+ iceServers: ice,
2016
+ }
2017
+ }
2018
+
1963
2019
  //create libp2p instance
1964
2020
  const libp2p = await createLibp2p({
1965
2021
  addresses: {
@@ -1969,9 +2025,7 @@ class webpeerjs{
1969
2025
  webTransport(),
1970
2026
  webSockets(),
1971
2027
  webRTC({
1972
- rtcConfiguration: {
1973
- iceServers: ice,
1974
- },
2028
+ rtcConfiguration: configuration.rtcConfiguration,
1975
2029
  }),
1976
2030
  circuitRelayTransport({
1977
2031
  discoverRelays: config.CONFIG_DISCOVER_RELAYS,