@leofcoin/peernet 0.11.7 → 0.11.8

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.
@@ -10,7 +10,7 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
10
10
  var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
11
11
  var Koa__default = /*#__PURE__*/_interopDefaultLegacy(Koa);
12
12
 
13
- var version = "0.11.6";
13
+ var version = "0.11.7";
14
14
 
15
15
  var api$1 = {
16
16
  version: ({send}) => send({client: '@peernet/api/http', version}),
@@ -307,6 +307,7 @@ class Peer {
307
307
  #senderMap = new Map()
308
308
  #iceCompleteTimer
309
309
  #channel
310
+ #peerId
310
311
 
311
312
  get connection() {
312
313
  return this.#connection
@@ -333,12 +334,17 @@ constructor(options = {}) {
333
334
  down: 0
334
335
  };
335
336
 
336
- this.channelName = options.channelName || Buffer.from(Math.random().toString(36).slice(-12)).toString('hex');
337
- console.log(this.channelName);
337
+ this.channelName = options.channelName;
338
+
339
+ this.#peerId = options.peerId;
338
340
  this.options = options;
339
341
  this.#init();
340
342
  }
341
343
 
344
+ get peerId() {
345
+ return this.#peerId
346
+ }
347
+
342
348
  set socketClient(value) {
343
349
  // this.socketClient?.pubsub.unsubscribe('signal', this._in)
344
350
  this._socketClient = value;
@@ -357,6 +363,7 @@ constructor(options = {}) {
357
363
  request(data) {
358
364
  return new Promise((resolve, reject) => {
359
365
  const id = Math.random().toString(36).slice(-12);
366
+ // TODO: get rid of JSON
360
367
  data = new TextEncoder().encode(JSON.stringify({id, data}));
361
368
  const _onData = message => {
362
369
  message = JSON.parse(new TextDecoder().decode(message.data));
@@ -405,7 +412,7 @@ constructor(options = {}) {
405
412
  if (message.to) {
406
413
  if (message.to === this.id) pubsub.publish('peer:data', message);
407
414
  } else pubsub.publish('peer:data', message);
408
- this.bw.down += message.length;
415
+ this.bw.down += message.length || message.byteLength;
409
416
  };
410
417
  this.channel = message.channel;
411
418
  };
@@ -423,7 +430,7 @@ constructor(options = {}) {
423
430
  if (message.to) {
424
431
  if (message.to === this.id) pubsub.publish('peer:data', message);
425
432
  } else pubsub.publish('peer:data', message);
426
- this.bw.down += message.length;
433
+ this.bw.down += message.length || message.byteLength;
427
434
  };
428
435
 
429
436
  const offer = await this.#connection.createOffer();
@@ -526,7 +533,7 @@ class Client {
526
533
  }
527
534
  const peers = await this.socketClient.peernet.join({id: this.id});
528
535
  for (const id of peers) {
529
- if (id !== this.id && !this.#connections[id]) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id});
536
+ if (id !== this.id && !this.#connections[id]) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
530
537
  }
531
538
  this.setupListeners();
532
539
 
@@ -585,7 +592,7 @@ class Client {
585
592
  delete this.#connections[id];
586
593
  }
587
594
  // reconnect
588
- if (id !== this.id) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id});
595
+ if (id !== this.id) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
589
596
  }
590
597
 
591
598
  }
@@ -615,7 +622,7 @@ class Client {
615
622
  delete this.#connections[id];
616
623
  }
617
624
  // RTCPeerConnection
618
- this.#connections[id] = new Peer({initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id});
625
+ this.#connections[id] = new Peer({initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
619
626
  console.log(`peer ${id} joined`);
620
627
  }
621
628
 
@@ -1636,7 +1643,7 @@ class Peernet {
1636
1643
  protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
1637
1644
  });
1638
1645
  } else {
1639
- const http = await Promise.resolve().then(function () { return require('./http-906864ff.js'); });
1646
+ const http = await Promise.resolve().then(function () { return require('./http-12850e18.js'); });
1640
1647
  if (environment !== 'browser') http.default(options);
1641
1648
  }
1642
1649
 
@@ -1719,6 +1726,7 @@ class Peernet {
1719
1726
  * @param {PeernetPeer} peer - peernet peer
1720
1727
  */
1721
1728
  async _protoHandler(message, peer, from) {
1729
+
1722
1730
  const {id, proto} = message;
1723
1731
  this.bw.down += proto.encoded.length;
1724
1732
 
@@ -1769,8 +1777,7 @@ class Peernet {
1769
1777
 
1770
1778
  this.bw.up += node.encoded.length;
1771
1779
  }
1772
- } else if (proto.name === 'peernet-ps' &&
1773
- this._getPeerId(peer.id) !== this.id.toString()) {
1780
+ } else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
1774
1781
  globalSub.publish(proto.decoded.topic.toString(), proto.decoded.data.toString());
1775
1782
  }
1776
1783
  // }
@@ -1785,12 +1792,11 @@ class Peernet {
1785
1792
  if (!hash) throw new Error('hash expected, received undefined')
1786
1793
  const data = new dht({hash});
1787
1794
  this.client.id;
1788
- for (const peer of this.connections) {
1789
- const node = await this.prepareMessage(peer.id, data.encoded);
1790
-
1791
- const result = await peer.request(node.encoded);
1792
-
1793
- let proto = protoFor(result.data);
1795
+ const walk = async peer => {
1796
+ const node = await this.prepareMessage(peer.peerId, data.encoded);
1797
+ let result = await peer.request(node.encoded);
1798
+ result = new Uint8Array(Object.values(result));
1799
+ let proto = protoFor(result);
1794
1800
 
1795
1801
  if (proto.name !== 'peernet-message') throw encapsulatedError()
1796
1802
  const from = proto.decoded.from;
@@ -1813,8 +1819,14 @@ class Peernet {
1813
1819
  };
1814
1820
 
1815
1821
  if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash);
1822
+ };
1823
+ let walks = [];
1824
+ for (const peer of this.connections) {
1825
+ if (peer.peerId !== this.id) {
1826
+ walks.push(walk(peer));
1827
+ }
1816
1828
  }
1817
- return
1829
+ return Promise.all(walks)
1818
1830
  }
1819
1831
 
1820
1832
  /**
@@ -1885,7 +1897,7 @@ class Peernet {
1885
1897
  const id = closestPeer.id.toString();
1886
1898
  if (this.connections) {
1887
1899
  let closest = this.connections.filter((peer) => {
1888
- if (peer.id === id) return peer
1900
+ if (peer.peerId === id) return peer
1889
1901
  });
1890
1902
 
1891
1903
  let data = new DataMessage({hash, store: store.name ? store.name : store});
@@ -1894,7 +1906,7 @@ class Peernet {
1894
1906
  if (closest[0]) data = await closest[0].request(node.encoded);
1895
1907
  else {
1896
1908
  closest = this.connections.filter((peer) => {
1897
- if (peer.id === id) return peer
1909
+ if (peer.peerId === id) return peer
1898
1910
  });
1899
1911
  if (closest[0]) data = await closest[0].request(node.encoded);
1900
1912
  }
@@ -2037,8 +2049,8 @@ class Peernet {
2037
2049
  data = new PsMessage({data, topic});
2038
2050
  for (const peer of this.connections) {
2039
2051
  if (peer.connected) {
2040
- if (peer.id !== this.peerId) {
2041
- const node = await this.prepareMessage(peer.id, data.encoded);
2052
+ if (peer.peerId !== this.peerId) {
2053
+ const node = await this.prepareMessage(peer.peerId, data.encoded);
2042
2054
  peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
2043
2055
  }
2044
2056
  } else {
@@ -2063,7 +2075,7 @@ class Peernet {
2063
2075
  }
2064
2076
 
2065
2077
  async removePeer(peer) {
2066
- delete this.client.connections[peer.id];
2078
+ delete this.client.connections[peer.peerId];
2067
2079
  }
2068
2080
 
2069
2081
  get Buffer() {
@@ -264,6 +264,7 @@ class Peer {
264
264
  #senderMap = new Map()
265
265
  #iceCompleteTimer
266
266
  #channel
267
+ #peerId
267
268
 
268
269
  get connection() {
269
270
  return this.#connection
@@ -290,12 +291,17 @@ constructor(options = {}) {
290
291
  down: 0
291
292
  };
292
293
 
293
- this.channelName = options.channelName || Buffer.from(Math.random().toString(36).slice(-12)).toString('hex');
294
- console.log(this.channelName);
294
+ this.channelName = options.channelName;
295
+
296
+ this.#peerId = options.peerId;
295
297
  this.options = options;
296
298
  this.#init();
297
299
  }
298
300
 
301
+ get peerId() {
302
+ return this.#peerId
303
+ }
304
+
299
305
  set socketClient(value) {
300
306
  // this.socketClient?.pubsub.unsubscribe('signal', this._in)
301
307
  this._socketClient = value;
@@ -314,6 +320,7 @@ constructor(options = {}) {
314
320
  request(data) {
315
321
  return new Promise((resolve, reject) => {
316
322
  const id = Math.random().toString(36).slice(-12);
323
+ // TODO: get rid of JSON
317
324
  data = new TextEncoder().encode(JSON.stringify({id, data}));
318
325
  const _onData = message => {
319
326
  message = JSON.parse(new TextDecoder().decode(message.data));
@@ -362,7 +369,7 @@ constructor(options = {}) {
362
369
  if (message.to) {
363
370
  if (message.to === this.id) pubsub.publish('peer:data', message);
364
371
  } else pubsub.publish('peer:data', message);
365
- this.bw.down += message.length;
372
+ this.bw.down += message.length || message.byteLength;
366
373
  };
367
374
  this.channel = message.channel;
368
375
  };
@@ -380,7 +387,7 @@ constructor(options = {}) {
380
387
  if (message.to) {
381
388
  if (message.to === this.id) pubsub.publish('peer:data', message);
382
389
  } else pubsub.publish('peer:data', message);
383
- this.bw.down += message.length;
390
+ this.bw.down += message.length || message.byteLength;
384
391
  };
385
392
 
386
393
  const offer = await this.#connection.createOffer();
@@ -483,7 +490,7 @@ class Client {
483
490
  }
484
491
  const peers = await this.socketClient.peernet.join({id: this.id});
485
492
  for (const id of peers) {
486
- if (id !== this.id && !this.#connections[id]) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id});
493
+ if (id !== this.id && !this.#connections[id]) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
487
494
  }
488
495
  this.setupListeners();
489
496
 
@@ -542,7 +549,7 @@ class Client {
542
549
  delete this.#connections[id];
543
550
  }
544
551
  // reconnect
545
- if (id !== this.id) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id});
552
+ if (id !== this.id) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
546
553
  }
547
554
 
548
555
  }
@@ -572,7 +579,7 @@ class Client {
572
579
  delete this.#connections[id];
573
580
  }
574
581
  // RTCPeerConnection
575
- this.#connections[id] = new Peer({initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id});
582
+ this.#connections[id] = new Peer({initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
576
583
  console.log(`peer ${id} joined`);
577
584
  }
578
585
 
@@ -2308,6 +2315,7 @@ class Peernet {
2308
2315
  * @param {PeernetPeer} peer - peernet peer
2309
2316
  */
2310
2317
  async _protoHandler(message, peer, from) {
2318
+
2311
2319
  const {id, proto} = message;
2312
2320
  this.bw.down += proto.encoded.length;
2313
2321
 
@@ -2358,8 +2366,7 @@ class Peernet {
2358
2366
 
2359
2367
  this.bw.up += node.encoded.length;
2360
2368
  }
2361
- } else if (proto.name === 'peernet-ps' &&
2362
- this._getPeerId(peer.id) !== this.id.toString()) {
2369
+ } else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
2363
2370
  globalSub.publish(proto.decoded.topic.toString(), proto.decoded.data.toString());
2364
2371
  }
2365
2372
  // }
@@ -2374,12 +2381,11 @@ class Peernet {
2374
2381
  if (!hash) throw new Error('hash expected, received undefined')
2375
2382
  const data = new DHTMessage({hash});
2376
2383
  this.client.id;
2377
- for (const peer of this.connections) {
2378
- const node = await this.prepareMessage(peer.id, data.encoded);
2379
-
2380
- const result = await peer.request(node.encoded);
2381
-
2382
- let proto = protoFor(result.data);
2384
+ const walk = async peer => {
2385
+ const node = await this.prepareMessage(peer.peerId, data.encoded);
2386
+ let result = await peer.request(node.encoded);
2387
+ result = new Uint8Array(Object.values(result));
2388
+ let proto = protoFor(result);
2383
2389
 
2384
2390
  if (proto.name !== 'peernet-message') throw encapsulatedError()
2385
2391
  const from = proto.decoded.from;
@@ -2402,8 +2408,14 @@ class Peernet {
2402
2408
  };
2403
2409
 
2404
2410
  if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash);
2411
+ };
2412
+ let walks = [];
2413
+ for (const peer of this.connections) {
2414
+ if (peer.peerId !== this.id) {
2415
+ walks.push(walk(peer));
2416
+ }
2405
2417
  }
2406
- return
2418
+ return Promise.all(walks)
2407
2419
  }
2408
2420
 
2409
2421
  /**
@@ -2474,7 +2486,7 @@ class Peernet {
2474
2486
  const id = closestPeer.id.toString();
2475
2487
  if (this.connections) {
2476
2488
  let closest = this.connections.filter((peer) => {
2477
- if (peer.id === id) return peer
2489
+ if (peer.peerId === id) return peer
2478
2490
  });
2479
2491
 
2480
2492
  let data = new DataMessage({hash, store: store.name ? store.name : store});
@@ -2483,7 +2495,7 @@ class Peernet {
2483
2495
  if (closest[0]) data = await closest[0].request(node.encoded);
2484
2496
  else {
2485
2497
  closest = this.connections.filter((peer) => {
2486
- if (peer.id === id) return peer
2498
+ if (peer.peerId === id) return peer
2487
2499
  });
2488
2500
  if (closest[0]) data = await closest[0].request(node.encoded);
2489
2501
  }
@@ -2626,8 +2638,8 @@ class Peernet {
2626
2638
  data = new PsMessage({data, topic});
2627
2639
  for (const peer of this.connections) {
2628
2640
  if (peer.connected) {
2629
- if (peer.id !== this.peerId) {
2630
- const node = await this.prepareMessage(peer.id, data.encoded);
2641
+ if (peer.peerId !== this.peerId) {
2642
+ const node = await this.prepareMessage(peer.peerId, data.encoded);
2631
2643
  peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
2632
2644
  }
2633
2645
  } else {
@@ -2652,7 +2664,7 @@ class Peernet {
2652
2664
  }
2653
2665
 
2654
2666
  async removePeer(peer) {
2655
- delete this.client.connections[peer.id];
2667
+ delete this.client.connections[peer.peerId];
2656
2668
  }
2657
2669
 
2658
2670
  get Buffer() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.11.7",
3
+ "version": "0.11.8",
4
4
  "description": "",
5
5
  "main": "dist/commonjs/peernet.js",
6
6
  "module": "dist/module/peernet.js",
@@ -25,7 +25,7 @@
25
25
  "dependencies": {
26
26
  "@leofcoin/generate-account": "^1.0.2",
27
27
  "@leofcoin/multi-wallet": "^2.1.2",
28
- "@leofcoin/peernet-swarm": "^0.1.9",
28
+ "@leofcoin/peernet-swarm": "^0.1.12",
29
29
  "@leofcoin/storage": "^2.3.0",
30
30
  "@vandeurenglenn/base32": "^1.1.0",
31
31
  "@vandeurenglenn/base58": "^1.1.0",
package/src/peernet.js CHANGED
@@ -257,6 +257,7 @@ export default class Peernet {
257
257
  * @param {PeernetPeer} peer - peernet peer
258
258
  */
259
259
  async _protoHandler(message, peer, from) {
260
+
260
261
  const {id, proto} = message
261
262
  this.bw.down += proto.encoded.length
262
263
 
@@ -309,8 +310,7 @@ export default class Peernet {
309
310
 
310
311
  this.bw.up += node.encoded.length
311
312
  }
312
- } else if (proto.name === 'peernet-ps' &&
313
- this._getPeerId(peer.id) !== this.id.toString()) {
313
+ } else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
314
314
  globalSub.publish(proto.decoded.topic.toString(), proto.decoded.data.toString())
315
315
  }
316
316
  // }
@@ -325,12 +325,11 @@ export default class Peernet {
325
325
  if (!hash) throw new Error('hash expected, received undefined')
326
326
  const data = new DHTMessage({hash})
327
327
  const clientId = this.client.id
328
- for (const peer of this.connections) {
329
- const node = await this.prepareMessage(peer.id, data.encoded)
330
-
331
- const result = await peer.request(node.encoded)
332
-
333
- let proto = protoFor(result.data)
328
+ const walk = async peer => {
329
+ const node = await this.prepareMessage(peer.peerId, data.encoded)
330
+ let result = await peer.request(node.encoded)
331
+ result = new Uint8Array(Object.values(result))
332
+ let proto = protoFor(result)
334
333
 
335
334
  if (proto.name !== 'peernet-message') throw encapsulatedError()
336
335
  const from = proto.decoded.from
@@ -354,7 +353,13 @@ export default class Peernet {
354
353
 
355
354
  if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash)
356
355
  }
357
- return
356
+ let walks = []
357
+ for (const peer of this.connections) {
358
+ if (peer.peerId !== this.id) {
359
+ walks.push(walk(peer))
360
+ }
361
+ }
362
+ return Promise.all(walks)
358
363
  }
359
364
 
360
365
  /**
@@ -425,7 +430,7 @@ export default class Peernet {
425
430
  const id = closestPeer.id.toString()
426
431
  if (this.connections) {
427
432
  let closest = this.connections.filter((peer) => {
428
- if (peer.id === id) return peer
433
+ if (peer.peerId === id) return peer
429
434
  })
430
435
 
431
436
  let data = new DataMessage({hash, store: store.name ? store.name : store});
@@ -434,7 +439,7 @@ export default class Peernet {
434
439
  if (closest[0]) data = await closest[0].request(node.encoded)
435
440
  else {
436
441
  closest = this.connections.filter((peer) => {
437
- if (peer.id === id) return peer
442
+ if (peer.peerId === id) return peer
438
443
  })
439
444
  if (closest[0]) data = await closest[0].request(node.encoded)
440
445
  }
@@ -578,8 +583,8 @@ export default class Peernet {
578
583
  data = new PsMessage({data, topic})
579
584
  for (const peer of this.connections) {
580
585
  if (peer.connected) {
581
- if (peer.id !== this.peerId) {
582
- const node = await this.prepareMessage(peer.id, data.encoded)
586
+ if (peer.peerId !== this.peerId) {
587
+ const node = await this.prepareMessage(peer.peerId, data.encoded)
583
588
  peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})))
584
589
  }
585
590
  } else {
@@ -604,7 +609,7 @@ export default class Peernet {
604
609
  }
605
610
 
606
611
  async removePeer(peer) {
607
- delete this.client.connections[peer.id]
612
+ delete this.client.connections[peer.peerId]
608
613
  }
609
614
 
610
615
  get Buffer() {
package/test4.js ADDED
@@ -0,0 +1,7 @@
1
+ const Client = require('./dist/commonjs/peernet.js')
2
+
3
+ const client = new Client({root: '.peernet/test4'})
4
+
5
+ pubsub.subscribe('peer:connected', async peer => {
6
+ await peernet.get("ba5xcadcnub27naa7zrcvtmyruviahaiwvupatfgjkvdgcehuif7clissf")
7
+ })
package/webpack.config.js CHANGED
@@ -17,7 +17,7 @@ module.exports = [{
17
17
  })
18
18
  ],
19
19
  optimization: {
20
- minimize: true
20
+ minimize: false
21
21
  },
22
22
  resolve: {
23
23
  extensions: [ '.ts', '.js' ],
@@ -1,40 +0,0 @@
1
- /*!
2
- * Determine if an object is a Buffer
3
- *
4
- * @author Feross Aboukhadijeh <https://feross.org>
5
- * @license MIT
6
- */
7
-
8
- /*!
9
- * The buffer module from node.js, for the browser.
10
- *
11
- * @author Feross Aboukhadijeh <https://feross.org>
12
- * @license MIT
13
- */
14
-
15
- /*!
16
- * assert.js - assertions for javascript
17
- * Copyright (c) 2018, Christopher Jeffrey (MIT License).
18
- * https://github.com/chjj/bsert
19
- */
20
-
21
- /*!
22
- * base32.js - base32 for bcrypto
23
- * Copyright (c) 2014-2019, Christopher Jeffrey (MIT License).
24
- * https://github.com/bcoin-org/bcrypto
25
- *
26
- * Parts of this software are based on bitcoin/bitcoin:
27
- * Copyright (c) 2009-2019, The Bitcoin Core Developers (MIT License).
28
- * Copyright (c) 2009-2019, The Bitcoin Developers (MIT License).
29
- * https://github.com/bitcoin/bitcoin
30
- *
31
- * Resources:
32
- * https://tools.ietf.org/html/rfc4648
33
- * https://github.com/bitcoin/bitcoin/blob/11d486d/src/utilstrencodings.cpp#L230
34
- */
35
-
36
- /*! For license information please see browser.js.LICENSE.txt */
37
-
38
- /*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
39
-
40
- /*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */