webpeerjs 0.1.5 → 0.1.7

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.5",
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.7",
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
+ }
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
@@ -27,7 +27,8 @@ const prefix = config.CONFIG_PREFIX
27
27
 
28
28
  export const mkErr = msg => new Error(`${prefix}: ${msg}`)
29
29
 
30
- export function mkDebug(error){
30
+ export function mkDebug(msg){
31
+ if(config.CONFIG_DEBUG_ENABLED)console.debug(msg)
31
32
  return
32
33
  }
33
34
 
@@ -50,6 +51,28 @@ export async function first(farr){
50
51
  }
51
52
  }
52
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
+
53
76
  //Add id to pupsub message
54
77
  export async function msgIdFnStrictNoSign(msg){
55
78
  var enc = new TextEncoder()
@@ -97,11 +120,19 @@ let lastStats = {
97
120
  open: 0
98
121
  }
99
122
 
123
+ let fail = 0
100
124
  let isDialEnabled = true
101
125
  let lastfailtreshold = 0
102
- let fail = 0
126
+ let isAutoDialEnabled = true
127
+ let lastfailtresholdauto = 0
128
+
103
129
 
104
130
  export function metrics(data){
131
+
132
+ if(isFirefox){
133
+ return {isDialEnabled,isAutoDialEnabled}
134
+ }
135
+
105
136
  try{
106
137
  const webTransportEvents = data.libp2p_webtransport_dialer_events_total
107
138
 
@@ -153,34 +184,50 @@ export function metrics(data){
153
184
  lastStats = webTransportEvents
154
185
 
155
186
  fail = errors+timeouts
156
- const treshold = errors+timeouts+stats.open+stats.pending
157
-
158
- if(treshold>30){
159
- //console.log(`Treeshold hit : ${treshold}`)
160
- }
161
-
162
- if(fail>30){
163
- //console.log(`Open : ${stats.open} , Pending : ${stats.pending} , Succes : ${totals.success} , Fail : ${fail} `)
164
-
165
- }
187
+ //const treshold = errors+timeouts+stats.open+stats.pending
166
188
 
167
- if ((fail-lastfailtreshold)>30){
189
+ if ((fail-lastfailtreshold)>config.CONFIG_DIAL_MAX_ERROR_LIMIT){
168
190
  if(isDialEnabled){
169
191
  isDialEnabled = false
170
- const str = JSON.stringify({isDialEnabled,fail,lastfailtreshold})
171
- console.warn('dial disabled')
192
+ console.warn('Peer dial is temporary disabled')
193
+ if(isAutoDialEnabled){
194
+ isAutoDialEnabled = false
195
+ console.warn('Peer autodial is temporary disabled')
196
+ }
172
197
  setTimeout(()=>{
173
198
  if(!isDialEnabled){
174
199
  isDialEnabled = true
175
200
  lastfailtreshold = fail
176
- const str = JSON.stringify({isDialEnabled,fail,lastfailtreshold})
177
- console.warn('dial enabled')
201
+ console.warn('Peer dial is resumed')
202
+ if(!isAutoDialEnabled){
203
+ isAutoDialEnabled = true
204
+ lastfailtresholdauto = fail
205
+ console.warn('Peer autodial is resumed')
206
+ }
207
+ }
208
+ },6*60*1000)
209
+ }
210
+ }
211
+
212
+ if ((fail-lastfailtresholdauto)>config.CONFIG_AUTODIAL_MAX_ERROR_LIMIT){
213
+ if(isAutoDialEnabled){
214
+ isAutoDialEnabled = false
215
+ console.warn('Peer autodial is temporary disabled')
216
+ const last = fail
217
+ setTimeout(()=>{
218
+ if(!isAutoDialEnabled && isDialEnabled){
219
+ isAutoDialEnabled = true
220
+ lastfailtresholdauto = fail
221
+ console.warn('Peer autodial is resumed')
222
+ if(isDialEnabled){
223
+ lastfailtreshold = last
224
+ }
178
225
  }
179
226
  },6*60*1000)
180
227
  }
181
228
  }
182
229
 
183
- return isDialEnabled
230
+ return {isDialEnabled,isAutoDialEnabled}
184
231
 
185
232
  }
186
233
  catch{
@@ -199,4 +246,9 @@ export async function getDigest(){
199
246
  const buf = new TextEncoder().encode(Math.random().toString())
200
247
  const digest = await sha256.encode(buf)
201
248
  return digest
249
+ }
250
+
251
+ //detect webtransport
252
+ if(!WebTransport){
253
+ throw mkErr('It seems that your browser does not support the required WebTransport feature')
202
254
  }