webpeerjs 0.1.6 → 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,88 +1,88 @@
1
- {
2
- "name": "webpeerjs",
3
- "version": "0.1.6",
4
- "description": "WebPEER.js is decentralized P2P JS library for communication between applications in browser.",
5
- "main": "./dist/umd/webpeerjs.js",
6
- "module": "./src/webpeerjs.js",
7
- "exports": {
8
- ".": {
9
- "import": "./src/webpeerjs.js",
10
- "require": "./dist/umd/webpeerjs.js"
11
- }
12
- },
13
- "files": [
14
- "dist/",
15
- "src/",
16
- "LICENSE"
17
- ],
18
- "scripts": {
19
- "start": "npm run dev",
20
- "dev": "vite serve test",
21
- "demo": "vite serve demo",
22
- "eslint": "eslint -c ./config/eslint.config.mjs ./src",
23
- "eslint:fix": "eslint -c ./config/eslint.config.mjs ./src --fix",
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
- "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
- "build-types": "tsc -p config/tsconfig-esm.json",
27
- "build": "npm run removedir dist temp && npm run build-all",
28
- "test": "cd test && cd project && npm start"
29
- },
30
- "repository": {
31
- "type": "git",
32
- "url": "git+https://github.com/nuzulul/webpeerjs.git"
33
- },
34
- "keywords": [
35
- "p2p",
36
- "ipfs",
37
- "libp2p",
38
- "peer",
39
- "peer-to-peer",
40
- "decentralized",
41
- "browser-to-browser",
42
- "dapp",
43
- "distributed",
44
- "decentralized-web",
45
- "distributed-web",
46
- "serverless"
47
- ],
48
- "author": {
49
- "name": "Nuzulul Zulkarnain",
50
- "url": "https://nuzulul.github.io"
51
- },
52
- "license": "MIT",
53
- "bugs": {
54
- "url": "https://github.com/nuzulul/webpeerjs/issues"
55
- },
56
- "homepage": "https://github.com/nuzulul/webpeerjs#readme",
57
- "dependencies": {
58
- "@chainsafe/libp2p-gossipsub": "^13.0.0",
59
- "@chainsafe/libp2p-noise": "^15.0.0",
60
- "@chainsafe/libp2p-yamux": "^6.0.2",
61
- "@helia/delegated-routing-v1-http-api-client": "^3.0.1",
62
- "@libp2p/circuit-relay-v2": "^1.0.24",
63
- "@libp2p/dcutr": "^1.1.0",
64
- "@libp2p/identify": "^2.0.2",
65
- "@libp2p/kad-dht": "^12.0.17",
66
- "@libp2p/peer-id": "^4.1.2",
67
- "@libp2p/pubsub-peer-discovery": "^10.0.2",
68
- "@libp2p/simple-metrics": "^1.0.2",
69
- "@libp2p/webrtc": "^4.1.0",
70
- "@libp2p/websockets": "^8.1.0",
71
- "@libp2p/webtransport": "^4.0.32",
72
- "datastore-idb": "^2.1.9",
73
- "libp2p": "^1.6.0"
74
- },
75
- "devDependencies": {
76
- "@eslint/js": "^9.4.0",
77
- "@rollup/plugin-commonjs": "^25.0.8",
78
- "@rollup/plugin-node-resolve": "^15.2.3",
79
- "@rollup/plugin-terser": "^0.4.4",
80
- "@rollup/plugin-typescript": "^11.1.6",
81
- "eslint": "^9.4.0",
82
- "globals": "^15.3.0",
83
- "rollup": "^4.18.0",
84
- "tslib": "^2.6.2",
85
- "typescript": "^5.4.5",
86
- "vite": "^5.2.11"
87
- }
88
- }
1
+ {
2
+ "name": "webpeerjs",
3
+ "version": "0.1.8",
4
+ "description": "WebPEER.js is decentralized P2P JS library for communication between applications in browser.",
5
+ "main": "./dist/umd/webpeerjs.js",
6
+ "module": "./src/webpeerjs.js",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./src/webpeerjs.js",
10
+ "require": "./dist/umd/webpeerjs.js"
11
+ }
12
+ },
13
+ "files": [
14
+ "dist/",
15
+ "src/",
16
+ "LICENSE"
17
+ ],
18
+ "scripts": {
19
+ "start": "npm run dev",
20
+ "dev": "vite serve test",
21
+ "demo": "vite serve demo",
22
+ "eslint": "eslint -c ./config/eslint.config.mjs ./src",
23
+ "eslint:fix": "eslint -c ./config/eslint.config.mjs ./src --fix",
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
+ "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
+ "build-types": "tsc -p config/tsconfig-esm.json",
27
+ "build": "npm run removedir dist temp && npm run build-all",
28
+ "test": "cd test && cd project && npm start"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+https://github.com/nuzulul/webpeerjs.git"
33
+ },
34
+ "keywords": [
35
+ "webpeer",
36
+ "p2p",
37
+ "p2p-network",
38
+ "webpeer-network",
39
+ "ipfs",
40
+ "libp2p",
41
+ "peer",
42
+ "peer-to-peer",
43
+ "decentralized",
44
+ "browser-to-browser",
45
+ "distributed",
46
+ "serverless"
47
+ ],
48
+ "author": {
49
+ "name": "Nuzulul Zulkarnain",
50
+ "url": "https://nuzulul.github.io"
51
+ },
52
+ "license": "MIT",
53
+ "bugs": {
54
+ "url": "https://github.com/nuzulul/webpeerjs/issues"
55
+ },
56
+ "homepage": "https://github.com/nuzulul/webpeerjs#readme",
57
+ "dependencies": {
58
+ "@chainsafe/libp2p-gossipsub": "^13.0.0",
59
+ "@chainsafe/libp2p-noise": "^15.0.0",
60
+ "@chainsafe/libp2p-yamux": "^6.0.2",
61
+ "@helia/delegated-routing-v1-http-api-client": "^3.0.1",
62
+ "@libp2p/circuit-relay-v2": "^1.0.24",
63
+ "@libp2p/dcutr": "^1.1.0",
64
+ "@libp2p/identify": "^2.0.2",
65
+ "@libp2p/kad-dht": "^12.0.17",
66
+ "@libp2p/peer-id": "^4.1.2",
67
+ "@libp2p/pubsub-peer-discovery": "^10.0.2",
68
+ "@libp2p/simple-metrics": "^1.0.2",
69
+ "@libp2p/webrtc": "^4.1.0",
70
+ "@libp2p/websockets": "^8.1.0",
71
+ "@libp2p/webtransport": "^4.0.32",
72
+ "datastore-idb": "^2.1.9",
73
+ "libp2p": "^1.6.0"
74
+ },
75
+ "devDependencies": {
76
+ "@eslint/js": "^9.4.0",
77
+ "@rollup/plugin-commonjs": "^25.0.8",
78
+ "@rollup/plugin-node-resolve": "^15.2.3",
79
+ "@rollup/plugin-terser": "^0.4.4",
80
+ "@rollup/plugin-typescript": "^11.1.6",
81
+ "eslint": "^9.4.0",
82
+ "globals": "^15.3.0",
83
+ "rollup": "^4.18.0",
84
+ "tslib": "^2.6.2",
85
+ "typescript": "^5.4.5",
86
+ "vite": "^5.2.11"
87
+ }
88
+ }
package/src/config.js CHANGED
@@ -24,13 +24,19 @@ export const CONFIG_TIMEOUT_DIAL_KNOWN_PEERS = 15000
24
24
  export const CONFIG_RUN_ON_TRANSIENT_CONNECTION = false
25
25
  export const CONFIG_WEBRTC_STUN_URLS = 'stun:stun.l.google.com:19302'
26
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
27
+ export const CONFIG_WEBRTC_TURN_HOST = 'dHVybjp0dXJuMDEuYnJpZS5maTo1MzQ5'
28
+ export const CONFIG_WEBRTC_TURN_USER = 'YnJpZQ=='
29
+ export const CONFIG_WEBRTC_TURN_PWD = 'Zmk='
30
+ export const CONFIG_WEBRTC_TURN_HOST_BACKUP = 'dHVybjpyZWxheTEuZXhwcmVzc3R1cm4uY29tOjM0Nzg='
31
+ export const CONFIG_WEBRTC_TURN_USER_BACKUP = 'ZWZJSllZNjdDNElRMzFZQUlP'
32
+ export const CONFIG_WEBRTC_TURN_PWD_BACKUP = 'Vk01SmdhODlkYjJaWU9aSA=='
33
+ export const CONFIG_WEBRTC_TURN_HOST_BACKUP1 = 'dHVybjpzdGFuZGFyZC5yZWxheS5tZXRlcmVkLmNhOjgw'
34
+ export const CONFIG_WEBRTC_TURN_USER_BACKUP1 = 'ZmZlNmIxOThjOGMxYjM5ODg1OWFiOGY4'
35
+ export const CONFIG_WEBRTC_TURN_PWD_BACKUP1 = 'aWpkQjVTcTIwREVsZzdDRg=='
36
+ export const CONFIG_MESSAGE_SIZE_LIMIT = 100*1024 // 100KB
37
+ export const CONFIG_DEBUG_ENABLED = false
38
+ export const CONFIG_AUTODIAL_MAX_ERROR_LIMIT = 45
39
+ export const CONFIG_DIAL_MAX_ERROR_LIMIT = 55
34
40
 
35
41
  // this list comes from https://github.com/ipfs/kubo/blob/196887cbe5fbcd41243c1dfb0db681a1cc2914ff/config/bootstrap_peers.go
36
42
  export const CONFIG_KNOWN_DEFAULT_BOOTSTRAP_ADDRESSES = [
package/src/utils.js CHANGED
@@ -28,7 +28,7 @@ const prefix = config.CONFIG_PREFIX
28
28
  export const mkErr = msg => new Error(`${prefix}: ${msg}`)
29
29
 
30
30
  export function mkDebug(msg){
31
- console.debug(msg)
31
+ if(config.CONFIG_DEBUG_ENABLED)console.debug(msg)
32
32
  return
33
33
  }
34
34
 
@@ -51,6 +51,28 @@ export async function first(farr){
51
51
  }
52
52
  }
53
53
 
54
+ //browser detector
55
+ //this code comes from https://stackoverflow.com/a/9851769
56
+
57
+ // Opera 8.0+
58
+ const isOpera = (!!window.opr && !!window.opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
59
+ // Firefox 1.0+
60
+ const isFirefox = typeof InstallTrigger !== 'undefined';
61
+ // Safari 3.0+ "[object HTMLElementConstructor]"
62
+ const isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && window['safari'].pushNotification));
63
+ // Internet Explorer 6-11
64
+ //const isIE = /*@cc_on!@*/false || !!document.documentMode;
65
+ // Edge 20+
66
+ //const isEdge = !isIE && !!window.StyleMedia;
67
+ // Chrome 1 - 79
68
+ const isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);
69
+ // Edge (based on chromium) detection
70
+ const isEdgeChromium = isChrome && (navigator.userAgent.indexOf("Edg") != -1);
71
+ // Blink engine detection
72
+ const isBlink = (isChrome || isOpera) && !!window.CSS;
73
+
74
+ export const browser = {isOpera,isFirefox,isSafari,isChrome,isEdgeChromium,isBlink}
75
+
54
76
  //Add id to pupsub message
55
77
  export async function msgIdFnStrictNoSign(msg){
56
78
  var enc = new TextEncoder()
@@ -98,11 +120,20 @@ let lastStats = {
98
120
  open: 0
99
121
  }
100
122
 
123
+ let fail = 0
124
+ let lastfail = 0
101
125
  let isDialEnabled = true
102
126
  let lastfailtreshold = 0
103
- let fail = 0
127
+ let isAutoDialEnabled = true
128
+ let lastfailtresholdauto = 0
129
+
104
130
 
105
131
  export function metrics(data){
132
+
133
+ if(isFirefox){
134
+ return {isDialEnabled,isAutoDialEnabled}
135
+ }
136
+
106
137
  try{
107
138
  const webTransportEvents = data.libp2p_webtransport_dialer_events_total
108
139
 
@@ -154,34 +185,64 @@ export function metrics(data){
154
185
  lastStats = webTransportEvents
155
186
 
156
187
  fail = errors+timeouts
157
- const treshold = errors+timeouts+stats.open+stats.pending
158
-
159
- if(treshold>30){
160
- //console.log(`Treeshold hit : ${treshold}`)
161
- }
188
+ //const treshold = errors+timeouts+stats.open+stats.pending
189
+ //console.log('fail',fail)
162
190
 
163
- if(fail>30){
164
- //console.log(`Open : ${stats.open} , Pending : ${stats.pending} , Succes : ${totals.success} , Fail : ${fail} `)
165
-
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)
166
201
  }
202
+ lastfail = fail
167
203
 
168
- if ((fail-lastfailtreshold)>30){
204
+ if ((fail-lastfailtreshold)>config.CONFIG_DIAL_MAX_ERROR_LIMIT){
169
205
  if(isDialEnabled){
170
206
  isDialEnabled = false
171
- //const str = JSON.stringify({isDialEnabled,fail,lastfailtreshold})
172
207
  console.warn('Peer dial is temporary disabled')
208
+ if(isAutoDialEnabled){
209
+ isAutoDialEnabled = false
210
+ console.warn('Peer autodial is temporary disabled')
211
+ }
173
212
  setTimeout(()=>{
174
213
  if(!isDialEnabled){
175
214
  isDialEnabled = true
176
215
  lastfailtreshold = fail
177
- //const str = JSON.stringify({isDialEnabled,fail,lastfailtreshold})
178
- console.warn('Peer dial is enabled')
216
+ console.warn('Peer dial is resumed')
217
+ if(!isAutoDialEnabled){
218
+ isAutoDialEnabled = true
219
+ lastfailtresholdauto = fail
220
+ console.warn('Peer autodial is resumed')
221
+ }
222
+ }
223
+ },6*60*1000)
224
+ }
225
+ }
226
+
227
+ if ((fail-lastfailtresholdauto)>config.CONFIG_AUTODIAL_MAX_ERROR_LIMIT){
228
+ if(isAutoDialEnabled){
229
+ isAutoDialEnabled = false
230
+ console.warn('Peer autodial is temporary disabled')
231
+ const last = fail
232
+ setTimeout(()=>{
233
+ if(!isAutoDialEnabled && isDialEnabled){
234
+ isAutoDialEnabled = true
235
+ lastfailtresholdauto = fail
236
+ console.warn('Peer autodial is resumed')
237
+ if(isDialEnabled){
238
+ lastfailtreshold = last
239
+ }
179
240
  }
180
241
  },6*60*1000)
181
242
  }
182
243
  }
183
244
 
184
- return isDialEnabled
245
+ return {isDialEnabled,isAutoDialEnabled}
185
246
 
186
247
  }
187
248
  catch{
@@ -200,4 +261,9 @@ export async function getDigest(){
200
261
  const buf = new TextEncoder().encode(Math.random().toString())
201
262
  const digest = await sha256.encode(buf)
202
263
  return digest
264
+ }
265
+
266
+ //detect webtransport
267
+ if(!WebTransport){
268
+ throw mkErr('It seems that your browser does not support the required WebTransport feature')
203
269
  }