webpeerjs 0.1.1 → 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.1",
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": {
@@ -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",
package/src/config.js CHANGED
@@ -1,4 +1,4 @@
1
- //! WebpeerJS -- https://github.com/nuzulul/webpeerjs
1
+ //! WebPEER.js -- https://github.com/nuzulul/webpeerjs
2
2
  const prefix = 'webpeerjs'
3
3
  export const CONFIG_PREFIX = prefix
4
4
  export const CONFIG_PROTOCOL = '/'+prefix+'/1.0.0'
@@ -11,7 +11,8 @@ export const CONFIG_DISCOVER_RELAYS = 1
11
11
  export const CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY = 'universal-connectivity-browser-peer-discovery'
12
12
  export const CONFIG_PEER_DISCOVERY_GLOBAL = '_peer-discovery._p2p._pubsub'
13
13
  export const CONFIG_PEER_DISCOVERY_WEBPEERJS= prefix+'-peer-discovery'
14
- 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]
15
16
  export const CONFIG_PUPSUB_PEER_DATA = ['_'+prefix+'-peer-data_']
16
17
  export const CONFIG_PUPSUB_TOPIC = prefix+'-room'
17
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,4 +1,4 @@
1
- //! WebpeerJS -- https://github.com/nuzulul/webpeerjs
1
+ //! WebPEER.js -- https://github.com/nuzulul/webpeerjs
2
2
  import * as config from './config'
3
3
  import {
4
4
  mkErr,
@@ -145,7 +145,10 @@ class webpeerjs{
145
145
  for(const topic of config.CONFIG_PUPSUB_PEER_DATA){
146
146
  this.#libp2p.services.pubsub.subscribe(topic)
147
147
  }
148
-
148
+
149
+ for(const topic of config.CONFIG_PUBSUB_PEER_DISCOVERY_HYBRID){
150
+ this.#libp2p.services.pubsub.subscribe(topic)
151
+ }
149
152
 
150
153
  //listen to peer connect event
151
154
  this.#libp2p.addEventListener("peer:connect",async (evt) => {
@@ -170,8 +173,10 @@ class webpeerjs{
170
173
  //required by joinRoom version 1 to announce via universal connectivity
171
174
  if(config.CONFIG_KNOWN_BOOTSTRAP_HYBRID_IDS.includes(id)){
172
175
  setTimeout(()=>{
176
+ this.#peerDiscoveryHybrid()
173
177
  this.#announce()
174
178
  setTimeout(()=>{
179
+ this.#peerDiscoveryHybrid()
175
180
  this.#announce()
176
181
  },5000)
177
182
  },1000)
@@ -358,6 +363,18 @@ class webpeerjs{
358
363
 
359
364
  }
360
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
+ }
361
378
 
362
379
  }catch(err){
363
380
  //console.log('from '+event.detail.from.toString())
@@ -505,6 +522,7 @@ class webpeerjs{
505
522
  //this.#ListenAddressChange(mddrs)
506
523
  this.address = mddrs
507
524
  this.#ping()
525
+ this.#peerDiscoveryHybrid()
508
526
  })
509
527
 
510
528
  this.#libp2p.addEventListener('peer:identify', async (evt) => {
@@ -598,6 +616,10 @@ class webpeerjs{
598
616
  this.#trackLastSeen()
599
617
  },5e3)
600
618
 
619
+ setInterval(()=>{
620
+ this.#peerDiscoveryHybrid()
621
+ },10e3)
622
+
601
623
 
602
624
  /*setTimeout(async()=>{
603
625
  try{
@@ -670,9 +692,14 @@ class webpeerjs{
670
692
  sendMessage : async (message) => {
671
693
  const msgId = (new Date()).getTime()
672
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
+ }
673
700
  const peer = {
674
701
  publicKey: this.#libp2p.peerId.publicKey,
675
- addrs: [uint8ArrayFromString(data)],
702
+ addrs: [arr],
676
703
  }
677
704
  const encodedPeer = PBPeer.encode(peer)
678
705
  for(const topic of topics){
@@ -1075,7 +1102,20 @@ class webpeerjs{
1075
1102
  }
1076
1103
 
1077
1104
 
1078
- //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
+
1079
1119
  async #announce(){
1080
1120
  const topics = config.CONFIG_PUPSUB_PEER_DATA
1081
1121
  const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'announce',id:this.#libp2p.peerId.toString(),address:this.address,rooms:Object.keys(this.#rooms)})
@@ -1088,6 +1128,7 @@ class webpeerjs{
1088
1128
  await this.#libp2p.services.pubsub.publish(topic, encodedPeer)
1089
1129
  }
1090
1130
  }
1131
+
1091
1132
  async #ping(){
1092
1133
  const topics = config.CONFIG_PUPSUB_PEER_DATA
1093
1134
  const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'ping',id:this.#libp2p.peerId.toString(),address:this.address,rooms:Object.keys(this.#rooms)})
@@ -1643,7 +1684,15 @@ class webpeerjs{
1643
1684
  transports:[
1644
1685
  webTransport(),
1645
1686
  webSockets(),
1646
- 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
+ }),
1647
1696
  circuitRelayTransport({
1648
1697
  discoverRelays: config.CONFIG_DISCOVER_RELAYS,
1649
1698
  reservationConcurrency: 1,
@@ -1698,7 +1747,7 @@ class webpeerjs{
1698
1747
  peerDiscovery: [
1699
1748
  pubsubPeerDiscovery({
1700
1749
  interval: 10_000,
1701
- topics: config.CONFIG_PUBSUB_PEER_DISCOVERY,
1750
+ topics: config.CONFIG_PUBSUB_PEER_DISCOVERY_WEBPEER,
1702
1751
  listenOnly: false,
1703
1752
  }),
1704
1753