webpeerjs 0.0.10 → 0.1.0
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/README.md +12 -18
- package/dist/esm/webpeerjs.js +35 -23
- package/dist/umd/webpeerjs.js +2868 -723
- package/package.json +3 -1
- package/src/config.js +1 -0
- package/src/webpeerjs.js +33 -22
package/README.md
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
# WebpeerJS
|
|
2
|
-
> Simple peer-to-peer in
|
|
2
|
+
> Simple peer-to-peer communication between applications in browser.
|
|
3
3
|
|
|
4
|
-
Build completely P2P web applications, no trackers or relay servers required. [WebpeerJS](https://github.com/nuzulul/webpeerjs) enables browser to browser connectivity without a central server. Connecting people safely, privately, openly and reliably.
|
|
4
|
+
Build completely P2P web applications, no trackers or relay servers required. [WebpeerJS](https://github.com/nuzulul/webpeerjs) enables browser to browser connectivity without a central server. Connecting people safely, privately, openly and reliably. Let's create a next generation internet that is fair, free and open.
|
|
5
5
|
|
|
6
|
-
[
|
|
6
|
+
[Basic Connection Demo](https://nuzulul.github.io/webpeerjs/demo/)
|
|
7
7
|
|
|
8
8
|
## Features
|
|
9
9
|
|
|
10
|
-
* ✅ Decentralized
|
|
10
|
+
* ✅ Decentralized P2P
|
|
11
11
|
* ✅ Unlimited Peers
|
|
12
|
-
* ✅ Works in
|
|
13
|
-
* ✅ Broadcast Message
|
|
12
|
+
* ✅ Works in Browsers
|
|
13
|
+
* ✅ Broadcast Message
|
|
14
14
|
|
|
15
15
|
## Install
|
|
16
16
|
|
|
@@ -25,7 +25,7 @@ Browser `<script>` tag :
|
|
|
25
25
|
Uses built-in JS files from [latest release](https://github.com/nuzulul/webpeerjs/releases/latest) or [CDN](https://www.jsdelivr.com/package/npm/webpeerjs) will make it's exports available as `webpeerjs` in the global namespace.
|
|
26
26
|
|
|
27
27
|
```
|
|
28
|
-
<script src="https://cdn.jsdelivr.net/npm/webpeerjs@0.
|
|
28
|
+
<script src="https://cdn.jsdelivr.net/npm/webpeerjs@0.1/dist/umd/webpeerjs.min.js"></script>
|
|
29
29
|
```
|
|
30
30
|
|
|
31
31
|
## Usage
|
|
@@ -55,22 +55,16 @@ void async function main() {
|
|
|
55
55
|
|
|
56
56
|
## API
|
|
57
57
|
|
|
58
|
-
- `createWebpeer()` Create a new node.
|
|
59
|
-
- `id` The unique ID of the node as an identity in the global network.
|
|
60
|
-
- `status` Get the node status, returns `connected` or `unconnected`.
|
|
61
|
-
- `peers` Get all connected peers.
|
|
62
|
-
- `joinRoom(namespace)` Join to the room, returns an array of three functions (Broadcaster, onListenBroadcast, onMembersUpdate).
|
|
58
|
+
- `createWebpeer()` - Create a new node.
|
|
59
|
+
- `id` - The unique ID of the node as an identity in the global network.
|
|
60
|
+
- `status` - Get the node status, returns `connected` or `unconnected`.
|
|
61
|
+
- `peers` - Get all connected peers.
|
|
62
|
+
- `joinRoom(namespace)` - Join to the room, returns an array of three functions (Broadcaster, onListenBroadcast, onMembersUpdate).
|
|
63
63
|
|
|
64
64
|
## API Docs
|
|
65
65
|
|
|
66
66
|
[https://nuzulul.github.io/webpeerjs](https://nuzulul.github.io/webpeerjs)
|
|
67
67
|
|
|
68
|
-
## Related
|
|
69
|
-
|
|
70
|
-
- [simple-peer](https://github.com/feross/simple-peer) - Simple WebRTC video, voice, and data channels.
|
|
71
|
-
- [peerjs](https://github.com/peers/peerjs) - Simple peer-to-peer with WebRTC.
|
|
72
|
-
- [trystero](https://github.com/dmotz/trystero) - Build instant multiplayer webapps, no server required.
|
|
73
|
-
|
|
74
68
|
## License
|
|
75
69
|
|
|
76
70
|
MIT
|
package/dist/esm/webpeerjs.js
CHANGED
|
@@ -12,6 +12,8 @@ import { createLibp2p } from 'libp2p';
|
|
|
12
12
|
import { IDBDatastore } from 'datastore-idb';
|
|
13
13
|
import { webTransport } from '@libp2p/webtransport';
|
|
14
14
|
import { webSockets } from '@libp2p/websockets';
|
|
15
|
+
import { webRTC } from '@libp2p/webrtc';
|
|
16
|
+
import { dcutr } from '@libp2p/dcutr';
|
|
15
17
|
import { noise } from '@chainsafe/libp2p-noise';
|
|
16
18
|
import { yamux } from '@chainsafe/libp2p-yamux';
|
|
17
19
|
import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery';
|
|
@@ -33,6 +35,7 @@ const CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY = 'universal-connectivity-bro
|
|
|
33
35
|
const CONFIG_PEER_DISCOVERY_GLOBAL = '_peer-discovery._p2p._pubsub';
|
|
34
36
|
const CONFIG_PEER_DISCOVERY_WEBPEERJS= prefix$1+'-peer-discovery';
|
|
35
37
|
const CONFIG_PUBSUB_PEER_DISCOVERY = [CONFIG_PEER_DISCOVERY_GLOBAL, CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY, CONFIG_PEER_DISCOVERY_WEBPEERJS];
|
|
38
|
+
const CONFIG_PUPSUB_PEER_DATA = ['_'+prefix$1+'-peer-data_'];
|
|
36
39
|
const CONFIG_DELEGATED_API = 'https://delegated-ipfs.dev';
|
|
37
40
|
const CONFIG_DNS_RESOLVER = 'https://dns.google/resolve';
|
|
38
41
|
const CONFIG_KNOWN_BOOTSTRAP_DNS = '_dnsaddr.bootstrap.libp2p.io';
|
|
@@ -416,6 +419,10 @@ class webpeerjs{
|
|
|
416
419
|
|
|
417
420
|
this.id = this.#libp2p.peerId.toString();
|
|
418
421
|
|
|
422
|
+
for(const topic of CONFIG_PUPSUB_PEER_DATA){
|
|
423
|
+
this.#libp2p.services.pubsub.subscribe(topic);
|
|
424
|
+
}
|
|
425
|
+
|
|
419
426
|
|
|
420
427
|
//listen to peer connect event
|
|
421
428
|
this.#libp2p.addEventListener("peer:connect",async (evt) => {
|
|
@@ -482,9 +489,9 @@ class webpeerjs{
|
|
|
482
489
|
return
|
|
483
490
|
}
|
|
484
491
|
{
|
|
485
|
-
|
|
492
|
+
event.detail.topic;
|
|
486
493
|
const senderPeerId = event.detail.from.toString();
|
|
487
|
-
|
|
494
|
+
|
|
488
495
|
try{
|
|
489
496
|
|
|
490
497
|
//if it is webpeer
|
|
@@ -520,15 +527,17 @@ class webpeerjs{
|
|
|
520
527
|
const addrs = this.#discoveredPeers.get(senderPeerId);
|
|
521
528
|
let mddrs = [];
|
|
522
529
|
for(const addr of addrs){
|
|
530
|
+
if(!addr.includes('webrtc'))continue
|
|
523
531
|
const mddr = multiaddr(addr);
|
|
524
532
|
mddrs.push(mddr);
|
|
525
533
|
}
|
|
526
534
|
this.#dialMultiaddress(mddrs);
|
|
527
535
|
}
|
|
528
|
-
else {
|
|
536
|
+
else if(this.#connectedPeers.has(senderPeerId)){
|
|
529
537
|
const addrs = this.#connectedPeers.get(senderPeerId).addrs;
|
|
530
538
|
let mddrs = [];
|
|
531
539
|
for(const addr of addrs){
|
|
540
|
+
if(!addr.includes('webrtc'))continue
|
|
532
541
|
const mddr = multiaddr(addr);
|
|
533
542
|
mddrs.push(mddr);
|
|
534
543
|
}
|
|
@@ -577,8 +586,10 @@ class webpeerjs{
|
|
|
577
586
|
|
|
578
587
|
//update room members
|
|
579
588
|
if(!this.#rooms[room].members.includes(id)){
|
|
580
|
-
this.#
|
|
581
|
-
|
|
589
|
+
if(this.#connectedPeers.has(id)){
|
|
590
|
+
this.#rooms[room].members.push(id);
|
|
591
|
+
this.#rooms[room].onMembers(this.#rooms[room].members);
|
|
592
|
+
}
|
|
582
593
|
}
|
|
583
594
|
|
|
584
595
|
//inbound message
|
|
@@ -597,8 +608,10 @@ class webpeerjs{
|
|
|
597
608
|
for(const room of Object.keys(this.#rooms)){
|
|
598
609
|
//update room members
|
|
599
610
|
if(!this.#rooms[room].members.includes(id)){
|
|
600
|
-
this.#
|
|
601
|
-
|
|
611
|
+
if(this.#connectedPeers.has(id)){
|
|
612
|
+
this.#rooms[room].members.push(id);
|
|
613
|
+
this.#rooms[room].onMembers(this.#rooms[room].members);
|
|
614
|
+
}
|
|
602
615
|
}
|
|
603
616
|
}
|
|
604
617
|
}
|
|
@@ -607,7 +620,7 @@ class webpeerjs{
|
|
|
607
620
|
|
|
608
621
|
//repply announce with ping
|
|
609
622
|
if(signal == 'announce'){
|
|
610
|
-
setTimeout(()=>{this.#ping('
|
|
623
|
+
setTimeout(()=>{this.#ping('');},1000);
|
|
611
624
|
//console.log('rooms',rooms)
|
|
612
625
|
}
|
|
613
626
|
|
|
@@ -620,12 +633,7 @@ class webpeerjs{
|
|
|
620
633
|
|
|
621
634
|
}catch(err){
|
|
622
635
|
}
|
|
623
|
-
|
|
624
|
-
const json = JSON.parse(topic);
|
|
625
|
-
const room = json.room;
|
|
626
|
-
const message = new TextDecoder().decode(event.detail.data);
|
|
627
|
-
this.#rooms[room].onMessage(message);
|
|
628
|
-
}
|
|
636
|
+
|
|
629
637
|
}
|
|
630
638
|
|
|
631
639
|
});
|
|
@@ -666,6 +674,7 @@ class webpeerjs{
|
|
|
666
674
|
if(!this.#connections.has(id)){
|
|
667
675
|
let mddrs = [];
|
|
668
676
|
for(const addr of addrs){
|
|
677
|
+
if(!addr.includes('webrtc'))continue
|
|
669
678
|
const mddr = multiaddr(addr);
|
|
670
679
|
mddrs.push(mddr);
|
|
671
680
|
}
|
|
@@ -753,7 +762,7 @@ class webpeerjs{
|
|
|
753
762
|
const mddrs = [];
|
|
754
763
|
peer.addresses.forEach((addr)=>{
|
|
755
764
|
const maddr = addr.multiaddr.toString()+'/p2p/'+id;
|
|
756
|
-
if(maddr.includes('webtransport') && maddr.includes('certhash')){
|
|
765
|
+
if(maddr.includes('webtransport') && maddr.includes('certhash') && maddr.includes('webrtc')){
|
|
757
766
|
mddrs.push(maddr);
|
|
758
767
|
}
|
|
759
768
|
});
|
|
@@ -915,7 +924,7 @@ class webpeerjs{
|
|
|
915
924
|
//join room version 1 user pupsub via pupsub peer discovery
|
|
916
925
|
{
|
|
917
926
|
|
|
918
|
-
const topics =
|
|
927
|
+
const topics = CONFIG_PUPSUB_PEER_DATA;
|
|
919
928
|
|
|
920
929
|
this.#rooms[room] = {
|
|
921
930
|
onMessage : () => {},
|
|
@@ -969,6 +978,7 @@ class webpeerjs{
|
|
|
969
978
|
else {
|
|
970
979
|
this.status = 'unconnected';
|
|
971
980
|
}
|
|
981
|
+
this.#ping();
|
|
972
982
|
}
|
|
973
983
|
|
|
974
984
|
async #registerProtocol(){
|
|
@@ -1061,7 +1071,7 @@ class webpeerjs{
|
|
|
1061
1071
|
await this.#libp2p.handle(CONFIG_PROTOCOL, handler, {
|
|
1062
1072
|
maxInboundStreams: 50,
|
|
1063
1073
|
maxOutboundStreams: 50,
|
|
1064
|
-
runOnTransientConnection:
|
|
1074
|
+
runOnTransientConnection:false
|
|
1065
1075
|
});
|
|
1066
1076
|
|
|
1067
1077
|
await this.#libp2p.register(CONFIG_PROTOCOL, {
|
|
@@ -1108,7 +1118,7 @@ class webpeerjs{
|
|
|
1108
1118
|
|
|
1109
1119
|
try{
|
|
1110
1120
|
|
|
1111
|
-
const stream = await this.#libp2p.dialProtocol(mddr, CONFIG_PROTOCOL,{runOnTransientConnection:
|
|
1121
|
+
const stream = await this.#libp2p.dialProtocol(mddr, CONFIG_PROTOCOL,{runOnTransientConnection:false});
|
|
1112
1122
|
|
|
1113
1123
|
const output = await pipe(
|
|
1114
1124
|
message,
|
|
@@ -1125,7 +1135,7 @@ class webpeerjs{
|
|
|
1125
1135
|
return string
|
|
1126
1136
|
}
|
|
1127
1137
|
);
|
|
1128
|
-
|
|
1138
|
+
//console.log(output)
|
|
1129
1139
|
const json = JSON.parse(output);
|
|
1130
1140
|
if(json.protocol == CONFIG_PROTOCOL){
|
|
1131
1141
|
const address = [addr];
|
|
@@ -1329,7 +1339,7 @@ class webpeerjs{
|
|
|
1329
1339
|
|
|
1330
1340
|
//announce and ping via pupsub peer discovery
|
|
1331
1341
|
async #announce(){
|
|
1332
|
-
const topics =
|
|
1342
|
+
const topics = CONFIG_PUPSUB_PEER_DATA;
|
|
1333
1343
|
const data = JSON.stringify({prefix:CONFIG_PREFIX,signal:'announce',id:this.#libp2p.peerId.toString(),address:this.address,rooms:this.#rooms});
|
|
1334
1344
|
const peer = {
|
|
1335
1345
|
publicKey: this.#libp2p.peerId.publicKey,
|
|
@@ -1341,7 +1351,7 @@ class webpeerjs{
|
|
|
1341
1351
|
}
|
|
1342
1352
|
}
|
|
1343
1353
|
async #ping(){
|
|
1344
|
-
const topics =
|
|
1354
|
+
const topics = CONFIG_PUPSUB_PEER_DATA;
|
|
1345
1355
|
const data = JSON.stringify({prefix:CONFIG_PREFIX,signal:'ping',id:this.#libp2p.peerId.toString(),address:this.address,rooms:this.#rooms});
|
|
1346
1356
|
const peer = {
|
|
1347
1357
|
publicKey: this.#libp2p.peerId.publicKey,
|
|
@@ -1881,11 +1891,13 @@ class webpeerjs{
|
|
|
1881
1891
|
const libp2p = await createLibp2p({
|
|
1882
1892
|
addresses: {
|
|
1883
1893
|
listen: [
|
|
1894
|
+
'/webrtc'
|
|
1884
1895
|
],
|
|
1885
1896
|
},
|
|
1886
1897
|
transports:[
|
|
1887
1898
|
webTransport(),
|
|
1888
1899
|
webSockets(),
|
|
1900
|
+
webRTC(),
|
|
1889
1901
|
circuitRelayTransport({
|
|
1890
1902
|
discoverRelays: CONFIG_DISCOVER_RELAYS,
|
|
1891
1903
|
reservationConcurrency: 1,
|
|
@@ -1950,7 +1962,7 @@ class webpeerjs{
|
|
|
1950
1962
|
allowPublishToZeroTopicPeers: true,
|
|
1951
1963
|
msgIdFn: msgIdFnStrictNoSign,
|
|
1952
1964
|
ignoreDuplicatePublishError: true,
|
|
1953
|
-
runOnTransientConnection:
|
|
1965
|
+
runOnTransientConnection:false,
|
|
1954
1966
|
}),
|
|
1955
1967
|
identify: identify(),
|
|
1956
1968
|
identifyPush: identifyPush(),
|
|
@@ -1959,7 +1971,7 @@ class webpeerjs{
|
|
|
1959
1971
|
peerInfoMapper: removePrivateAddressesMapper,
|
|
1960
1972
|
clientMode: false
|
|
1961
1973
|
}),
|
|
1962
|
-
|
|
1974
|
+
dcutr: dcutr()
|
|
1963
1975
|
},
|
|
1964
1976
|
peerStore: {
|
|
1965
1977
|
persistence: true,
|