webpeerjs 0.0.10 → 0.1.1
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 +19 -20
- package/dist/esm/webpeerjs.js +79 -1987
- package/dist/umd/webpeerjs.js +47 -48648
- package/package.json +6 -3
- package/src/config.js +2 -0
- package/src/webpeerjs.js +44 -27
- package/dist/esm/webpeerjs.d.ts +0 -17
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webpeerjs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Simple peer-to-peer with IPFS",
|
|
5
5
|
"main": "./dist/umd/webpeerjs.js",
|
|
6
6
|
"module": "./src/webpeerjs.js",
|
|
@@ -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
|
|
28
|
-
"test": "
|
|
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",
|
|
@@ -54,11 +54,13 @@
|
|
|
54
54
|
"@chainsafe/libp2p-yamux": "^6.0.2",
|
|
55
55
|
"@helia/delegated-routing-v1-http-api-client": "^3.0.1",
|
|
56
56
|
"@libp2p/circuit-relay-v2": "^1.0.24",
|
|
57
|
+
"@libp2p/dcutr": "^1.1.0",
|
|
57
58
|
"@libp2p/identify": "^2.0.2",
|
|
58
59
|
"@libp2p/kad-dht": "^12.0.17",
|
|
59
60
|
"@libp2p/peer-id": "^4.1.2",
|
|
60
61
|
"@libp2p/pubsub-peer-discovery": "^10.0.2",
|
|
61
62
|
"@libp2p/simple-metrics": "^1.0.2",
|
|
63
|
+
"@libp2p/webrtc": "^4.1.0",
|
|
62
64
|
"@libp2p/websockets": "^8.1.0",
|
|
63
65
|
"@libp2p/webtransport": "^4.0.32",
|
|
64
66
|
"datastore-idb": "^2.1.9",
|
|
@@ -68,6 +70,7 @@
|
|
|
68
70
|
"@eslint/js": "^9.4.0",
|
|
69
71
|
"@rollup/plugin-commonjs": "^25.0.8",
|
|
70
72
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
73
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
71
74
|
"@rollup/plugin-typescript": "^11.1.6",
|
|
72
75
|
"eslint": "^9.4.0",
|
|
73
76
|
"globals": "^15.3.0",
|
package/src/config.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//! WebpeerJS -- 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'
|
|
@@ -11,6 +12,7 @@ export const CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY = 'universal-connectiv
|
|
|
11
12
|
export const CONFIG_PEER_DISCOVERY_GLOBAL = '_peer-discovery._p2p._pubsub'
|
|
12
13
|
export const CONFIG_PEER_DISCOVERY_WEBPEERJS= prefix+'-peer-discovery'
|
|
13
14
|
export const CONFIG_PUBSUB_PEER_DISCOVERY = [CONFIG_PEER_DISCOVERY_GLOBAL, CONFIG_PEER_DISCOVERY_UNIVERSAL_CONNECTIVITY, CONFIG_PEER_DISCOVERY_WEBPEERJS]
|
|
15
|
+
export const CONFIG_PUPSUB_PEER_DATA = ['_'+prefix+'-peer-data_']
|
|
14
16
|
export const CONFIG_PUPSUB_TOPIC = prefix+'-room'
|
|
15
17
|
export const CONFIG_DELEGATED_API = 'https://delegated-ipfs.dev'
|
|
16
18
|
export const CONFIG_DNS_RESOLVER = 'https://dns.google/resolve'
|
package/src/webpeerjs.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//! WebpeerJS -- https://github.com/nuzulul/webpeerjs
|
|
1
2
|
import * as config from './config'
|
|
2
3
|
import {
|
|
3
4
|
mkErr,
|
|
@@ -21,6 +22,8 @@ import { createLibp2p } from 'libp2p'
|
|
|
21
22
|
import { IDBDatastore } from 'datastore-idb'
|
|
22
23
|
import { webTransport } from '@libp2p/webtransport'
|
|
23
24
|
import { webSockets } from '@libp2p/websockets'
|
|
25
|
+
import { webRTC } from '@libp2p/webrtc'
|
|
26
|
+
import { dcutr } from '@libp2p/dcutr'
|
|
24
27
|
import { noise } from '@chainsafe/libp2p-noise'
|
|
25
28
|
import { yamux } from '@chainsafe/libp2p-yamux'
|
|
26
29
|
import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery'
|
|
@@ -139,6 +142,10 @@ class webpeerjs{
|
|
|
139
142
|
|
|
140
143
|
this.id = this.#libp2p.peerId.toString()
|
|
141
144
|
|
|
145
|
+
for(const topic of config.CONFIG_PUPSUB_PEER_DATA){
|
|
146
|
+
this.#libp2p.services.pubsub.subscribe(topic)
|
|
147
|
+
}
|
|
148
|
+
|
|
142
149
|
|
|
143
150
|
//listen to peer connect event
|
|
144
151
|
this.#libp2p.addEventListener("peer:connect",async (evt) => {
|
|
@@ -207,7 +214,7 @@ class webpeerjs{
|
|
|
207
214
|
if(config.CONFIG_JOIN_ROOM_VERSION == 1){
|
|
208
215
|
const topic = event.detail.topic
|
|
209
216
|
const senderPeerId = event.detail.from.toString()
|
|
210
|
-
|
|
217
|
+
|
|
211
218
|
try{
|
|
212
219
|
|
|
213
220
|
//if it is webpeer
|
|
@@ -243,15 +250,17 @@ class webpeerjs{
|
|
|
243
250
|
const addrs = this.#discoveredPeers.get(senderPeerId)
|
|
244
251
|
let mddrs = []
|
|
245
252
|
for(const addr of addrs){
|
|
253
|
+
if(!addr.includes('webrtc'))continue
|
|
246
254
|
const mddr = multiaddr(addr)
|
|
247
255
|
mddrs.push(mddr)
|
|
248
256
|
}
|
|
249
257
|
this.#dialMultiaddress(mddrs)
|
|
250
258
|
}
|
|
251
|
-
else{
|
|
259
|
+
else if(this.#connectedPeers.has(senderPeerId)){
|
|
252
260
|
const addrs = this.#connectedPeers.get(senderPeerId).addrs
|
|
253
261
|
let mddrs = []
|
|
254
262
|
for(const addr of addrs){
|
|
263
|
+
if(!addr.includes('webrtc'))continue
|
|
255
264
|
const mddr = multiaddr(addr)
|
|
256
265
|
mddrs.push(mddr)
|
|
257
266
|
}
|
|
@@ -300,8 +309,10 @@ class webpeerjs{
|
|
|
300
309
|
|
|
301
310
|
//update room members
|
|
302
311
|
if(!this.#rooms[room].members.includes(id)){
|
|
303
|
-
this.#
|
|
304
|
-
|
|
312
|
+
if(this.#connectedPeers.has(id)){
|
|
313
|
+
this.#rooms[room].members.push(id)
|
|
314
|
+
this.#rooms[room].onMembers(this.#rooms[room].members)
|
|
315
|
+
}
|
|
305
316
|
}
|
|
306
317
|
|
|
307
318
|
//inbound message
|
|
@@ -317,11 +328,18 @@ class webpeerjs{
|
|
|
317
328
|
}
|
|
318
329
|
|
|
319
330
|
if(rooms){
|
|
320
|
-
for(const room of
|
|
321
|
-
|
|
322
|
-
if(
|
|
323
|
-
|
|
324
|
-
|
|
331
|
+
for(const room of rooms){
|
|
332
|
+
|
|
333
|
+
if(this.#rooms[room]){
|
|
334
|
+
|
|
335
|
+
//update room members
|
|
336
|
+
if(!this.#rooms[room].members.includes(id)){
|
|
337
|
+
if(this.#connectedPeers.has(id)){
|
|
338
|
+
this.#rooms[room].members.push(id)
|
|
339
|
+
this.#rooms[room].onMembers(this.#rooms[room].members)
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
325
343
|
}
|
|
326
344
|
}
|
|
327
345
|
}
|
|
@@ -330,7 +348,7 @@ class webpeerjs{
|
|
|
330
348
|
|
|
331
349
|
//repply announce with ping
|
|
332
350
|
if(signal == 'announce'){
|
|
333
|
-
setTimeout(()=>{this.#ping('
|
|
351
|
+
setTimeout(()=>{this.#ping('')},1000)
|
|
334
352
|
//console.log('rooms',rooms)
|
|
335
353
|
}
|
|
336
354
|
|
|
@@ -345,12 +363,7 @@ class webpeerjs{
|
|
|
345
363
|
//console.log('from '+event.detail.from.toString())
|
|
346
364
|
mkDebug(err)
|
|
347
365
|
}
|
|
348
|
-
|
|
349
|
-
const json = JSON.parse(topic)
|
|
350
|
-
const room = json.room
|
|
351
|
-
const message = new TextDecoder().decode(event.detail.data)
|
|
352
|
-
this.#rooms[room].onMessage(message)
|
|
353
|
-
}
|
|
366
|
+
|
|
354
367
|
}
|
|
355
368
|
|
|
356
369
|
})
|
|
@@ -391,6 +404,7 @@ class webpeerjs{
|
|
|
391
404
|
if(!this.#connections.has(id)){
|
|
392
405
|
let mddrs = []
|
|
393
406
|
for(const addr of addrs){
|
|
407
|
+
if(!addr.includes('webrtc'))continue
|
|
394
408
|
const mddr = multiaddr(addr)
|
|
395
409
|
mddrs.push(mddr)
|
|
396
410
|
}
|
|
@@ -484,7 +498,7 @@ class webpeerjs{
|
|
|
484
498
|
const mddrs = []
|
|
485
499
|
peer.addresses.forEach((addr)=>{
|
|
486
500
|
const maddr = addr.multiaddr.toString()+'/p2p/'+id
|
|
487
|
-
if(maddr.includes('webtransport') && maddr.includes('certhash')){
|
|
501
|
+
if(maddr.includes('webtransport') && maddr.includes('certhash') && maddr.includes('webrtc')){
|
|
488
502
|
mddrs.push(maddr)
|
|
489
503
|
}
|
|
490
504
|
})
|
|
@@ -648,7 +662,7 @@ class webpeerjs{
|
|
|
648
662
|
//join room version 1 user pupsub via pupsub peer discovery
|
|
649
663
|
if(config.CONFIG_JOIN_ROOM_VERSION == 1){
|
|
650
664
|
|
|
651
|
-
const topics = config.
|
|
665
|
+
const topics = config.CONFIG_PUPSUB_PEER_DATA
|
|
652
666
|
|
|
653
667
|
this.#rooms[room] = {
|
|
654
668
|
onMessage : () => {},
|
|
@@ -702,6 +716,7 @@ class webpeerjs{
|
|
|
702
716
|
else{
|
|
703
717
|
this.status = 'unconnected'
|
|
704
718
|
}
|
|
719
|
+
this.#ping()
|
|
705
720
|
}
|
|
706
721
|
|
|
707
722
|
async #registerProtocol(){
|
|
@@ -794,7 +809,7 @@ class webpeerjs{
|
|
|
794
809
|
await this.#libp2p.handle(config.CONFIG_PROTOCOL, handler, {
|
|
795
810
|
maxInboundStreams: 50,
|
|
796
811
|
maxOutboundStreams: 50,
|
|
797
|
-
runOnTransientConnection:
|
|
812
|
+
runOnTransientConnection:false
|
|
798
813
|
})
|
|
799
814
|
|
|
800
815
|
await this.#libp2p.register(config.CONFIG_PROTOCOL, {
|
|
@@ -841,7 +856,7 @@ class webpeerjs{
|
|
|
841
856
|
|
|
842
857
|
try{
|
|
843
858
|
|
|
844
|
-
const stream = await this.#libp2p.dialProtocol(mddr, config.CONFIG_PROTOCOL,{runOnTransientConnection:
|
|
859
|
+
const stream = await this.#libp2p.dialProtocol(mddr, config.CONFIG_PROTOCOL,{runOnTransientConnection:false})
|
|
845
860
|
|
|
846
861
|
const output = await pipe(
|
|
847
862
|
message,
|
|
@@ -858,7 +873,7 @@ class webpeerjs{
|
|
|
858
873
|
return string
|
|
859
874
|
}
|
|
860
875
|
)
|
|
861
|
-
|
|
876
|
+
//console.log(output)
|
|
862
877
|
const json = JSON.parse(output)
|
|
863
878
|
if(json.protocol == config.CONFIG_PROTOCOL){
|
|
864
879
|
const address = [addr]
|
|
@@ -1062,8 +1077,8 @@ class webpeerjs{
|
|
|
1062
1077
|
|
|
1063
1078
|
//announce and ping via pupsub peer discovery
|
|
1064
1079
|
async #announce(){
|
|
1065
|
-
const topics = config.
|
|
1066
|
-
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'announce',id:this.#libp2p.peerId.toString(),address:this.address,rooms:this.#rooms})
|
|
1080
|
+
const topics = config.CONFIG_PUPSUB_PEER_DATA
|
|
1081
|
+
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'announce',id:this.#libp2p.peerId.toString(),address:this.address,rooms:Object.keys(this.#rooms)})
|
|
1067
1082
|
const peer = {
|
|
1068
1083
|
publicKey: this.#libp2p.peerId.publicKey,
|
|
1069
1084
|
addrs: [uint8ArrayFromString(data)],
|
|
@@ -1074,8 +1089,8 @@ class webpeerjs{
|
|
|
1074
1089
|
}
|
|
1075
1090
|
}
|
|
1076
1091
|
async #ping(){
|
|
1077
|
-
const topics = config.
|
|
1078
|
-
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'ping',id:this.#libp2p.peerId.toString(),address:this.address,rooms:this.#rooms})
|
|
1092
|
+
const topics = config.CONFIG_PUPSUB_PEER_DATA
|
|
1093
|
+
const data = JSON.stringify({prefix:config.CONFIG_PREFIX,signal:'ping',id:this.#libp2p.peerId.toString(),address:this.address,rooms:Object.keys(this.#rooms)})
|
|
1079
1094
|
const peer = {
|
|
1080
1095
|
publicKey: this.#libp2p.peerId.publicKey,
|
|
1081
1096
|
addrs: [uint8ArrayFromString(data)],
|
|
@@ -1622,11 +1637,13 @@ class webpeerjs{
|
|
|
1622
1637
|
const libp2p = await createLibp2p({
|
|
1623
1638
|
addresses: {
|
|
1624
1639
|
listen: [
|
|
1640
|
+
'/webrtc'
|
|
1625
1641
|
],
|
|
1626
1642
|
},
|
|
1627
1643
|
transports:[
|
|
1628
1644
|
webTransport(),
|
|
1629
1645
|
webSockets(),
|
|
1646
|
+
webRTC(),
|
|
1630
1647
|
circuitRelayTransport({
|
|
1631
1648
|
discoverRelays: config.CONFIG_DISCOVER_RELAYS,
|
|
1632
1649
|
reservationConcurrency: 1,
|
|
@@ -1691,7 +1708,7 @@ class webpeerjs{
|
|
|
1691
1708
|
allowPublishToZeroTopicPeers: true,
|
|
1692
1709
|
msgIdFn: msgIdFnStrictNoSign,
|
|
1693
1710
|
ignoreDuplicatePublishError: true,
|
|
1694
|
-
runOnTransientConnection:
|
|
1711
|
+
runOnTransientConnection:false,
|
|
1695
1712
|
}),
|
|
1696
1713
|
identify: identify(),
|
|
1697
1714
|
identifyPush: identifyPush(),
|
|
@@ -1700,7 +1717,7 @@ class webpeerjs{
|
|
|
1700
1717
|
peerInfoMapper: removePrivateAddressesMapper,
|
|
1701
1718
|
clientMode: false
|
|
1702
1719
|
}),
|
|
1703
|
-
|
|
1720
|
+
dcutr: dcutr()
|
|
1704
1721
|
},
|
|
1705
1722
|
peerStore: {
|
|
1706
1723
|
persistence: true,
|
package/dist/esm/webpeerjs.d.ts
DELETED
|
@@ -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
|
-
}
|