webpeerjs 0.1.0 → 0.1.2

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,7 +1,7 @@
1
1
  {
2
2
  "name": "webpeerjs",
3
- "version": "0.1.0",
4
- "description": "Simple peer-to-peer with IPFS",
3
+ "version": "0.1.2",
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": {
@@ -24,8 +24,8 @@
24
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
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
26
  "build-types": "tsc -p config/tsconfig-esm.json",
27
- "build": "npm run removedir dist temp && npm run build-all && npm run build-types",
28
- "test": "echo \"Error: no test specified\" && exit 1"
27
+ "build": "npm run removedir dist temp && npm run build-all",
28
+ "test": "cd test && cd project && npm start"
29
29
  },
30
30
  "repository": {
31
31
  "type": "git",
@@ -34,10 +34,16 @@
34
34
  "keywords": [
35
35
  "p2p",
36
36
  "ipfs",
37
+ "libp2p",
37
38
  "peer",
38
- "peer to peer",
39
+ "peer-to-peer",
39
40
  "decentralized",
40
- "browser to 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",
@@ -70,6 +76,7 @@
70
76
  "@eslint/js": "^9.4.0",
71
77
  "@rollup/plugin-commonjs": "^25.0.8",
72
78
  "@rollup/plugin-node-resolve": "^15.2.3",
79
+ "@rollup/plugin-terser": "^0.4.4",
73
80
  "@rollup/plugin-typescript": "^11.1.6",
74
81
  "eslint": "^9.4.0",
75
82
  "globals": "^15.3.0",
package/src/config.js CHANGED
@@ -1,3 +1,4 @@
1
+ //! WebPEER.js -- https://github.com/nuzulul/webpeerjs
1
2
  const prefix = 'webpeerjs'
2
3
  export const CONFIG_PREFIX = prefix
3
4
  export const CONFIG_PROTOCOL = '/'+prefix+'/1.0.0'
@@ -10,7 +11,8 @@ export const CONFIG_DISCOVER_RELAYS = 1
10
11
  export const CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY = 'universal-connectivity-browser-peer-discovery'
11
12
  export const CONFIG_PEER_DISCOVERY_GLOBAL = '_peer-discovery._p2p._pubsub'
12
13
  export const CONFIG_PEER_DISCOVERY_WEBPEERJS= prefix+'-peer-discovery'
13
- export const CONFIG_PUBSUB_PEER_DISCOVERY = [CONFIG_PEER_DISCOVERY_GLOBAL, CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY, CONFIG_PEER_DISCOVERY_WEBPEERJS]
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]
14
16
  export const CONFIG_PUPSUB_PEER_DATA = ['_'+prefix+'-peer-data_']
15
17
  export const CONFIG_PUPSUB_TOPIC = prefix+'-room'
16
18
  export const CONFIG_DELEGATED_API = 'https://delegated-ipfs.dev'
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
@@ -1,3 +1,4 @@
1
+ //! WebPEER.js -- https://github.com/nuzulul/webpeerjs
1
2
  import {webpeerjs} from './webpeerjs';
2
3
 
3
4
  export default webpeerjs;
package/src/utils.js CHANGED
@@ -1,3 +1,4 @@
1
+ //! WebPEER.js -- https://github.com/nuzulul/webpeerjs
1
2
  import * as config from './config'
2
3
  import { Peer as PBPeer } from './peer'
3
4
  import { Key } from 'interface-datastore'
package/src/webpeerjs.js CHANGED
@@ -1,3 +1,4 @@
1
+ //! WebPEER.js -- https://github.com/nuzulul/webpeerjs
1
2
  import * as config from './config'
2
3
  import {
3
4
  mkErr,
@@ -144,7 +145,10 @@ class webpeerjs{
144
145
  for(const topic of config.CONFIG_PUPSUB_PEER_DATA){
145
146
  this.#libp2p.services.pubsub.subscribe(topic)
146
147
  }
147
-
148
+
149
+ for(const topic of config.CONFIG_PUBSUB_PEER_DISCOVERY_HYBRID){
150
+ this.#libp2p.services.pubsub.subscribe(topic)
151
+ }
148
152
 
149
153
  //listen to peer connect event
150
154
  this.#libp2p.addEventListener("peer:connect",async (evt) => {
@@ -169,8 +173,10 @@ class webpeerjs{
169
173
  //required by joinRoom version 1 to announce via universal connectivity
170
174
  if(config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
171
175
  setTimeout(()=>{
176
+ this.#peerDiscoveryHybrid()
172
177
  this.#announce()
173
178
  setTimeout(()=>{
179
+ this.#peerDiscoveryHybrid()
174
180
  this.#announce()
175
181
  },5000)
176
182
  },1000)
@@ -327,13 +333,18 @@ class webpeerjs{
327
333
  }
328
334
 
329
335
  if(rooms){
330
- for(const room of Object.keys(this.#rooms)){
331
- //update room members
332
- if(!this.#rooms[room].members.includes(id)){
333
- if(this.#connectedPeers.has(id)){
334
- this.#rooms[room].members.push(id)
335
- this.#rooms[room].onMembers(this.#rooms[room].members)
336
+ for(const room of rooms){
337
+
338
+ if(this.#rooms[room]){
339
+
340
+ //update room members
341
+ if(!this.#rooms[room].members.includes(id)){
342
+ if(this.#connectedPeers.has(id)){
343
+ this.#rooms[room].members.push(id)
344
+ this.#rooms[room].onMembers(this.#rooms[room].members)
345
+ }
336
346
  }
347
+
337
348
  }
338
349
  }
339
350
  }
@@ -352,6 +363,18 @@ class webpeerjs{
352
363
 
353
364
  }
354
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
+ }
355
378
 
356
379
  }catch(err){
357
380
  //console.log('from '+event.detail.from.toString())
@@ -499,6 +522,7 @@ class webpeerjs{
499
522
  //this.#ListenAddressChange(mddrs)
500
523
  this.address = mddrs
501
524
  this.#ping()
525
+ this.#peerDiscoveryHybrid()
502
526
  })
503
527
 
504
528
  this.#libp2p.addEventListener('peer:identify', async (evt) => {
@@ -592,6 +616,10 @@ class webpeerjs{
592
616
  this.#trackLastSeen()
593
617
  },5e3)
594
618
 
619
+ setInterval(()=>{
620
+ this.#peerDiscoveryHybrid()
621
+ },10e3)
622
+
595
623
 
596
624
  /*setTimeout(async()=>{
597
625
  try{
@@ -664,9 +692,14 @@ class webpeerjs{
664
692
  sendMessage : async (message) => {
665
693
  const msgId = (new Date()).getTime()
666
694
  const data = JSON.stringify({prefix:config.CONFIG_PREFIX,room,message,id:this.#libp2p.peerId.toString(),msgId})
695
+ const arr = uint8ArrayFromString(data)
696
+ const sizelimit = 102400 // 100KB
697
+ if(arr.byteLength > sizelimit){
698
+ throw mkErr('data too large')
699
+ }
667
700
  const peer = {
668
701
  publicKey: this.#libp2p.peerId.publicKey,
669
- addrs: [uint8ArrayFromString(data)],
702
+ addrs: [arr],
670
703
  }
671
704
  const encodedPeer = PBPeer.encode(peer)
672
705
  for(const topic of topics){
@@ -1069,10 +1102,23 @@ class webpeerjs{
1069
1102
  }
1070
1103
 
1071
1104
 
1072
- //announce and ping via pupsub peer discovery
1105
+ //announce and ping via pupsub peer discovery hybrid
1106
+ async #peerDiscoveryHybrid(){
1107
+ const topics = config.CONFIG_PUBSUB_PEER_DISCOVERY_HYBRID
1108
+ const data = JSON.stringify({prefix:'hybrid',id:this.#libp2p.peerId.toString(),address:this.address})
1109
+ const peer = {
1110
+ publicKey: this.#libp2p.peerId.publicKey,
1111
+ addrs: [uint8ArrayFromString(data)],
1112
+ }
1113
+ const encodedPeer = PBPeer.encode(peer)
1114
+ for(const topic of topics){
1115
+ await this.#libp2p.services.pubsub.publish(topic, encodedPeer)
1116
+ }
1117
+ }
1118
+
1073
1119
  async #announce(){
1074
1120
  const topics = config.CONFIG_PUPSUB_PEER_DATA
1075
- const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'announce',id:this.#libp2p.peerId.toString(),address:this.address,rooms:this.#rooms})
1121
+ const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'announce',id:this.#libp2p.peerId.toString(),address:this.address,rooms:Object.keys(this.#rooms)})
1076
1122
  const peer = {
1077
1123
  publicKey: this.#libp2p.peerId.publicKey,
1078
1124
  addrs: [uint8ArrayFromString(data)],
@@ -1082,9 +1128,10 @@ class webpeerjs{
1082
1128
  await this.#libp2p.services.pubsub.publish(topic, encodedPeer)
1083
1129
  }
1084
1130
  }
1131
+
1085
1132
  async #ping(){
1086
1133
  const topics = config.CONFIG_PUPSUB_PEER_DATA
1087
- const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'ping',id:this.#libp2p.peerId.toString(),address:this.address,rooms:this.#rooms})
1134
+ const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'ping',id:this.#libp2p.peerId.toString(),address:this.address,rooms:Object.keys(this.#rooms)})
1088
1135
  const peer = {
1089
1136
  publicKey: this.#libp2p.peerId.publicKey,
1090
1137
  addrs: [uint8ArrayFromString(data)],
@@ -1637,7 +1684,15 @@ class webpeerjs{
1637
1684
  transports:[
1638
1685
  webTransport(),
1639
1686
  webSockets(),
1640
- webRTC(),
1687
+ webRTC({
1688
+ rtcConfiguration: {
1689
+ iceServers: [
1690
+ {
1691
+ urls: ['stun:stun.l.google.com:19302', 'stun:global.stun.twilio.com:3478'],
1692
+ },
1693
+ ],
1694
+ },
1695
+ }),
1641
1696
  circuitRelayTransport({
1642
1697
  discoverRelays: config.CONFIG_DISCOVER_RELAYS,
1643
1698
  reservationConcurrency: 1,
@@ -1692,7 +1747,7 @@ class webpeerjs{
1692
1747
  peerDiscovery: [
1693
1748
  pubsubPeerDiscovery({
1694
1749
  interval: 10_000,
1695
- topics: config.CONFIG_PUBSUB_PEER_DISCOVERY,
1750
+ topics: config.CONFIG_PUBSUB_PEER_DISCOVERY_WEBPEER,
1696
1751
  listenOnly: false,
1697
1752
  }),
1698
1753
 
@@ -1,17 +0,0 @@
1
- export class webpeerjs {
2
- static createWebpeer(): Promise<webpeerjs>;
3
- constructor(libp2p: any, dbstore: any, onMetrics: any);
4
- id: any;
5
- status: string;
6
- IPFS: {
7
- libp2p: any;
8
- discoveredPeers: Map<any, any>;
9
- };
10
- address: any[];
11
- peers: any[];
12
- onConnect: (f: any) => any;
13
- onDisconnect: (f: any) => any;
14
- joinRoom: (room: any) => any[];
15
- dial(addr: any): void;
16
- #private;
17
- }