@leofcoin/peernet 0.11.8 → 0.11.9

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.7";
13
+ var version = "0.11.8";
14
14
 
15
15
  var api$1 = {
16
16
  version: ({send}) => send({client: '@peernet/api/http', version}),
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ require('@vandeurenglenn/debug');
3
4
  var LeofcoinStorage = require('@leofcoin/storage');
4
5
  var peernetMessage = require('./peernet-message.js');
5
6
  var dht = require('./dht.js');
@@ -356,7 +357,7 @@ constructor(options = {}) {
356
357
  }
357
358
 
358
359
  send(message) {
359
- this.bw.up += message.length;
360
+ this.bw.up += message.length || message.byteLength;
360
361
  this.channel.send(message);
361
362
  }
362
363
 
@@ -369,16 +370,16 @@ constructor(options = {}) {
369
370
  message = JSON.parse(new TextDecoder().decode(message.data));
370
371
  if (message.id === id) {
371
372
  resolve(message.data);
372
- pubsub.unsubscribe('peer:data', _onData);
373
+ pubsub.unsubscribe(`peer:data`, _onData);
373
374
  }
374
375
  };
375
376
 
376
- pubsub.subscribe('peer:data', _onData);
377
+ pubsub.subscribe(`peer:data`, _onData);
377
378
 
378
379
  // cleanup subscriptions
379
- setTimeout(() => {
380
- pubsub.unsubscribe('peer:data', _onData);
381
- }, 5000);
380
+ // setTimeout(() => {
381
+ // pubsub.unsubscribe(`peer:data-request-${id}`, _onData)
382
+ // }, 5000);
382
383
 
383
384
  this.send(data);
384
385
  });
@@ -407,11 +408,12 @@ constructor(options = {}) {
407
408
  this.#connected = true;
408
409
  pubsub.publish('peer:connected', this);
409
410
  };
410
- message.channel.onclose = () => console.log('close');
411
+ message.channel.onclose = () => this.close.bind(this);
412
+
411
413
  message.channel.onmessage = (message) => {
412
- if (message.to) {
413
- if (message.to === this.id) pubsub.publish('peer:data', message);
414
- } else pubsub.publish('peer:data', message);
414
+ pubsub.publish('peer:data', message);
415
+ debug(`incoming message from ${this.id}`);
416
+ debug(message);
415
417
  this.bw.down += message.length || message.byteLength;
416
418
  };
417
419
  this.channel = message.channel;
@@ -427,9 +429,9 @@ constructor(options = {}) {
427
429
  this.channel.onclose = () => this.close.bind(this);
428
430
 
429
431
  this.channel.onmessage = (message) => {
430
- if (message.to) {
431
- if (message.to === this.id) pubsub.publish('peer:data', message);
432
- } else pubsub.publish('peer:data', message);
432
+ pubsub.publish('peer:data', message);
433
+ debug(`incoming message from ${this.peerId}`);
434
+ debug(message);
433
435
  this.bw.down += message.length || message.byteLength;
434
436
  };
435
437
 
@@ -460,6 +462,8 @@ constructor(options = {}) {
460
462
  // if (data.videocall) return this._startStream(true, false); // start video and audio stream
461
463
  // if (data.call) return this._startStream(true, true); // start audio stream
462
464
  if (message.candidate) {
465
+ debug(`incoming candidate ${this.channelName}`);
466
+ debug(message.candidate.candidate);
463
467
  this.remoteAddress = message.candidate.address;
464
468
  this.remotePort = message.candidate.port;
465
469
  this.remoteProtocol = message.candidate.protocol;
@@ -469,12 +473,14 @@ constructor(options = {}) {
469
473
  try {
470
474
  if (message.sdp) {
471
475
  if (message.sdp.type === 'offer') {
476
+ debug(`incoming offer ${this.channelName}`);
472
477
  await this.#connection.setRemoteDescription(new wrtc.RTCSessionDescription(message.sdp));
473
478
  const answer = await this.#connection.createAnswer();
474
479
  await this.#connection.setLocalDescription(answer);
475
480
  this._sendMessage({'sdp': this.#connection.localDescription});
476
481
  }
477
482
  if (message.sdp.type === 'answer') {
483
+ debug(`incoming answer ${this.channelName}`);
478
484
  await this.#connection.setRemoteDescription(new wrtc.RTCSessionDescription(message.sdp));
479
485
  }
480
486
  }
@@ -484,6 +490,7 @@ constructor(options = {}) {
484
490
  }
485
491
 
486
492
  close() {
493
+ debug(`closing ${this.peerId}`);
487
494
  this.channel?.close();
488
495
  this.#connection?.close();
489
496
 
@@ -514,7 +521,6 @@ class Client {
514
521
  async _init(identifiers, stars = []) {
515
522
  if (stars.length === 0) {
516
523
  stars.push('wss://star.leofcoin.org');
517
- stars.push('ws://localhost:44444');
518
524
  }
519
525
  this.identifiers = identifiers;
520
526
  this.starsConfig = stars;
@@ -536,14 +542,6 @@ class Client {
536
542
  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});
537
543
  }
538
544
  this.setupListeners();
539
-
540
- pubsub.subscribe('peer:connected', (peer) => {
541
- // peer.send(JSON.stringify({data: 'hello', from: this.id, to: peer.to}))
542
- console.log({peer: peer.to});
543
- console.log({id: peer.id});
544
- console.log({id: this.id});
545
- });
546
- // pubsub.subscribe('peer:data', (data) => console.log({data}))
547
545
  }
548
546
 
549
547
  setupListeners() {
@@ -602,10 +600,7 @@ class Client {
602
600
  }
603
601
  }
604
602
  }
605
-
606
- console.log(`star ${id} left`);
607
-
608
-
603
+ debug(`star ${id} left`);
609
604
  }
610
605
 
611
606
  peerLeft(id) {
@@ -613,7 +608,7 @@ class Client {
613
608
  this.#connections[id].close();
614
609
  delete this.#connections[id];
615
610
  }
616
- console.log(`peer ${id} left`);
611
+ debug(`peer ${id} left`);
617
612
  }
618
613
 
619
614
  peerJoined(id, signal) {
@@ -623,7 +618,7 @@ class Client {
623
618
  }
624
619
  // RTCPeerConnection
625
620
  this.#connections[id] = new Peer({initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
626
- console.log(`peer ${id} joined`);
621
+ debug(`peer ${id} joined`);
627
622
  }
628
623
 
629
624
 
@@ -781,7 +776,7 @@ class ChatMessage extends codecFormatInterface {
781
776
  }
782
777
  }
783
778
 
784
- const debug = (log) => {
779
+ const debug$1 = (log) => {
785
780
  if (globalThis.DEBUG || globalThis.debug) console.log(`%c ${log}`, 'color: #0080ff;');
786
781
  };
787
782
 
@@ -1643,7 +1638,7 @@ class Peernet {
1643
1638
  protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
1644
1639
  });
1645
1640
  } else {
1646
- const http = await Promise.resolve().then(function () { return require('./http-12850e18.js'); });
1641
+ const http = await Promise.resolve().then(function () { return require('./http-63196be9.js'); });
1647
1642
  if (environment !== 'browser') http.default(options);
1648
1643
  }
1649
1644
 
@@ -1729,7 +1724,6 @@ class Peernet {
1729
1724
 
1730
1725
  const {id, proto} = message;
1731
1726
  this.bw.down += proto.encoded.length;
1732
-
1733
1727
  if (proto.name === 'peernet-dht') {
1734
1728
  let { hash, store } = proto.decoded;
1735
1729
  let has;
@@ -1797,11 +1791,9 @@ class Peernet {
1797
1791
  let result = await peer.request(node.encoded);
1798
1792
  result = new Uint8Array(Object.values(result));
1799
1793
  let proto = protoFor(result);
1800
-
1801
1794
  if (proto.name !== 'peernet-message') throw encapsulatedError()
1802
1795
  const from = proto.decoded.from;
1803
1796
  proto = protoFor(proto.decoded.data);
1804
-
1805
1797
  if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
1806
1798
 
1807
1799
  // TODO: give ip and port (just used for location)
@@ -1888,19 +1880,19 @@ class Peernet {
1888
1880
  async requestData(hash, store) {
1889
1881
  const providers = await this.providersFor(hash);
1890
1882
  if (!providers || providers.size === 0) throw nothingFoundError(hash)
1891
- debug(`found ${providers.size} provider(s) for ${hash}`);
1883
+ debug$1(`found ${providers.size} provider(s) for ${hash}`);
1892
1884
  // get closest peer on earth
1893
1885
  const closestPeer = await this.dht.closestPeer(providers);
1894
1886
  // get peer instance by id
1895
- if (!closestPeer || !closestPeer.id) return this.requestData(hash, store.name ? store.name : store)
1887
+ if (!closestPeer || !closestPeer.id) return this.requestData(hash, store?.name ? store?.name : store)
1896
1888
 
1897
- const id = closestPeer.id.toString();
1889
+ const id = closestPeer.id;
1898
1890
  if (this.connections) {
1899
1891
  let closest = this.connections.filter((peer) => {
1900
1892
  if (peer.peerId === id) return peer
1901
1893
  });
1902
1894
 
1903
- let data = new DataMessage({hash, store: store.name ? store.name : store});
1895
+ let data = new DataMessage({hash, store: store?.name ? store?.name : store});
1904
1896
 
1905
1897
  const node = await this.prepareMessage(id, data.encoded);
1906
1898
  if (closest[0]) data = await closest[0].request(node.encoded);
@@ -1910,12 +1902,11 @@ class Peernet {
1910
1902
  });
1911
1903
  if (closest[0]) data = await closest[0].request(node.encoded);
1912
1904
  }
1913
- if (data.data) {
1914
- console.log(data.data);
1915
- let proto = protoFor(data.data);
1916
- proto = protoFor(proto.decoded.data);
1917
- return proto.decoded.data
1918
- }
1905
+ data = new Uint8Array(Object.values(data));
1906
+ let proto = protoFor(data);
1907
+ proto = protoFor(proto.decoded.data);
1908
+ // TODO: store data automaticly or not
1909
+ return proto.decoded.data
1919
1910
 
1920
1911
  // this.put(hash, proto.decoded.data)
1921
1912
  }
@@ -1931,7 +1922,7 @@ class Peernet {
1931
1922
  * @param {String} hash
1932
1923
  */
1933
1924
  get: async (hash) => {
1934
- debug(`get message ${hash}`);
1925
+ debug$1(`get message ${hash}`);
1935
1926
  const message = await messageStore.has(hash);
1936
1927
  if (message) return await messageStore.get(hash)
1937
1928
  return this.requestData(hash, 'message')
@@ -1959,7 +1950,7 @@ class Peernet {
1959
1950
  * @param {String} hash
1960
1951
  */
1961
1952
  get: async (hash) => {
1962
- debug(`get data ${hash}`);
1953
+ debug$1(`get data ${hash}`);
1963
1954
  const data = await dataStore.has(hash);
1964
1955
  if (data) return await dataStore.get(hash)
1965
1956
  return this.requestData(hash, 'data')
@@ -2001,14 +1992,14 @@ class Peernet {
2001
1992
  * @param {String} store - storeName to access
2002
1993
  */
2003
1994
  async get(hash, store) {
2004
- debug(`get ${hash}`);
1995
+ debug$1(`get ${hash}`);
2005
1996
  let data;
2006
1997
  if (store) store = globalThis[`${store}Store`];
2007
1998
  if (!store) store = await this.whichStore([...this.stores], hash);
2008
1999
  if (store && await store.has(hash)) data = await store.get(hash);
2009
2000
  if (data) return data
2010
2001
 
2011
- return this.requestData(hash, store.name ? store.name : store)
2002
+ return this.requestData(hash, store?.name ? store.name : store)
2012
2003
  }
2013
2004
 
2014
2005
  /**
@@ -1,3 +1,4 @@
1
+ import '@vandeurenglenn/debug';
1
2
  import LeofcoinStorage from '@leofcoin/storage';
2
3
  import protons from 'protons';
3
4
  import bs32 from '@vandeurenglenn/base32';
@@ -313,7 +314,7 @@ constructor(options = {}) {
313
314
  }
314
315
 
315
316
  send(message) {
316
- this.bw.up += message.length;
317
+ this.bw.up += message.length || message.byteLength;
317
318
  this.channel.send(message);
318
319
  }
319
320
 
@@ -326,16 +327,16 @@ constructor(options = {}) {
326
327
  message = JSON.parse(new TextDecoder().decode(message.data));
327
328
  if (message.id === id) {
328
329
  resolve(message.data);
329
- pubsub.unsubscribe('peer:data', _onData);
330
+ pubsub.unsubscribe(`peer:data`, _onData);
330
331
  }
331
332
  };
332
333
 
333
- pubsub.subscribe('peer:data', _onData);
334
+ pubsub.subscribe(`peer:data`, _onData);
334
335
 
335
336
  // cleanup subscriptions
336
- setTimeout(() => {
337
- pubsub.unsubscribe('peer:data', _onData);
338
- }, 5000);
337
+ // setTimeout(() => {
338
+ // pubsub.unsubscribe(`peer:data-request-${id}`, _onData)
339
+ // }, 5000);
339
340
 
340
341
  this.send(data);
341
342
  });
@@ -364,11 +365,12 @@ constructor(options = {}) {
364
365
  this.#connected = true;
365
366
  pubsub.publish('peer:connected', this);
366
367
  };
367
- message.channel.onclose = () => console.log('close');
368
+ message.channel.onclose = () => this.close.bind(this);
369
+
368
370
  message.channel.onmessage = (message) => {
369
- if (message.to) {
370
- if (message.to === this.id) pubsub.publish('peer:data', message);
371
- } else pubsub.publish('peer:data', message);
371
+ pubsub.publish('peer:data', message);
372
+ debug(`incoming message from ${this.id}`);
373
+ debug(message);
372
374
  this.bw.down += message.length || message.byteLength;
373
375
  };
374
376
  this.channel = message.channel;
@@ -384,9 +386,9 @@ constructor(options = {}) {
384
386
  this.channel.onclose = () => this.close.bind(this);
385
387
 
386
388
  this.channel.onmessage = (message) => {
387
- if (message.to) {
388
- if (message.to === this.id) pubsub.publish('peer:data', message);
389
- } else pubsub.publish('peer:data', message);
389
+ pubsub.publish('peer:data', message);
390
+ debug(`incoming message from ${this.peerId}`);
391
+ debug(message);
390
392
  this.bw.down += message.length || message.byteLength;
391
393
  };
392
394
 
@@ -417,6 +419,8 @@ constructor(options = {}) {
417
419
  // if (data.videocall) return this._startStream(true, false); // start video and audio stream
418
420
  // if (data.call) return this._startStream(true, true); // start audio stream
419
421
  if (message.candidate) {
422
+ debug(`incoming candidate ${this.channelName}`);
423
+ debug(message.candidate.candidate);
420
424
  this.remoteAddress = message.candidate.address;
421
425
  this.remotePort = message.candidate.port;
422
426
  this.remoteProtocol = message.candidate.protocol;
@@ -426,12 +430,14 @@ constructor(options = {}) {
426
430
  try {
427
431
  if (message.sdp) {
428
432
  if (message.sdp.type === 'offer') {
433
+ debug(`incoming offer ${this.channelName}`);
429
434
  await this.#connection.setRemoteDescription(new wrtc.RTCSessionDescription(message.sdp));
430
435
  const answer = await this.#connection.createAnswer();
431
436
  await this.#connection.setLocalDescription(answer);
432
437
  this._sendMessage({'sdp': this.#connection.localDescription});
433
438
  }
434
439
  if (message.sdp.type === 'answer') {
440
+ debug(`incoming answer ${this.channelName}`);
435
441
  await this.#connection.setRemoteDescription(new wrtc.RTCSessionDescription(message.sdp));
436
442
  }
437
443
  }
@@ -441,6 +447,7 @@ constructor(options = {}) {
441
447
  }
442
448
 
443
449
  close() {
450
+ debug(`closing ${this.peerId}`);
444
451
  this.channel?.close();
445
452
  this.#connection?.close();
446
453
 
@@ -471,7 +478,6 @@ class Client {
471
478
  async _init(identifiers, stars = []) {
472
479
  if (stars.length === 0) {
473
480
  stars.push('wss://star.leofcoin.org');
474
- stars.push('ws://localhost:44444');
475
481
  }
476
482
  this.identifiers = identifiers;
477
483
  this.starsConfig = stars;
@@ -493,14 +499,6 @@ class Client {
493
499
  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});
494
500
  }
495
501
  this.setupListeners();
496
-
497
- pubsub.subscribe('peer:connected', (peer) => {
498
- // peer.send(JSON.stringify({data: 'hello', from: this.id, to: peer.to}))
499
- console.log({peer: peer.to});
500
- console.log({id: peer.id});
501
- console.log({id: this.id});
502
- });
503
- // pubsub.subscribe('peer:data', (data) => console.log({data}))
504
502
  }
505
503
 
506
504
  setupListeners() {
@@ -559,10 +557,7 @@ class Client {
559
557
  }
560
558
  }
561
559
  }
562
-
563
- console.log(`star ${id} left`);
564
-
565
-
560
+ debug(`star ${id} left`);
566
561
  }
567
562
 
568
563
  peerLeft(id) {
@@ -570,7 +565,7 @@ class Client {
570
565
  this.#connections[id].close();
571
566
  delete this.#connections[id];
572
567
  }
573
- console.log(`peer ${id} left`);
568
+ debug(`peer ${id} left`);
574
569
  }
575
570
 
576
571
  peerJoined(id, signal) {
@@ -580,7 +575,7 @@ class Client {
580
575
  }
581
576
  // RTCPeerConnection
582
577
  this.#connections[id] = new Peer({initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
583
- console.log(`peer ${id} joined`);
578
+ debug(`peer ${id} joined`);
584
579
  }
585
580
 
586
581
 
@@ -1378,7 +1373,7 @@ class ChatMessage extends FormatInterface {
1378
1373
  }
1379
1374
  }
1380
1375
 
1381
- const debug = (log) => {
1376
+ const debug$1 = (log) => {
1382
1377
  if (globalThis.DEBUG || globalThis.debug) console.log(`%c ${log}`, 'color: #0080ff;');
1383
1378
  };
1384
1379
 
@@ -2318,7 +2313,6 @@ class Peernet {
2318
2313
 
2319
2314
  const {id, proto} = message;
2320
2315
  this.bw.down += proto.encoded.length;
2321
-
2322
2316
  if (proto.name === 'peernet-dht') {
2323
2317
  let { hash, store } = proto.decoded;
2324
2318
  let has;
@@ -2386,11 +2380,9 @@ class Peernet {
2386
2380
  let result = await peer.request(node.encoded);
2387
2381
  result = new Uint8Array(Object.values(result));
2388
2382
  let proto = protoFor(result);
2389
-
2390
2383
  if (proto.name !== 'peernet-message') throw encapsulatedError()
2391
2384
  const from = proto.decoded.from;
2392
2385
  proto = protoFor(proto.decoded.data);
2393
-
2394
2386
  if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
2395
2387
 
2396
2388
  // TODO: give ip and port (just used for location)
@@ -2477,19 +2469,19 @@ class Peernet {
2477
2469
  async requestData(hash, store) {
2478
2470
  const providers = await this.providersFor(hash);
2479
2471
  if (!providers || providers.size === 0) throw nothingFoundError(hash)
2480
- debug(`found ${providers.size} provider(s) for ${hash}`);
2472
+ debug$1(`found ${providers.size} provider(s) for ${hash}`);
2481
2473
  // get closest peer on earth
2482
2474
  const closestPeer = await this.dht.closestPeer(providers);
2483
2475
  // get peer instance by id
2484
- if (!closestPeer || !closestPeer.id) return this.requestData(hash, store.name ? store.name : store)
2476
+ if (!closestPeer || !closestPeer.id) return this.requestData(hash, store?.name ? store?.name : store)
2485
2477
 
2486
- const id = closestPeer.id.toString();
2478
+ const id = closestPeer.id;
2487
2479
  if (this.connections) {
2488
2480
  let closest = this.connections.filter((peer) => {
2489
2481
  if (peer.peerId === id) return peer
2490
2482
  });
2491
2483
 
2492
- let data = new DataMessage({hash, store: store.name ? store.name : store});
2484
+ let data = new DataMessage({hash, store: store?.name ? store?.name : store});
2493
2485
 
2494
2486
  const node = await this.prepareMessage(id, data.encoded);
2495
2487
  if (closest[0]) data = await closest[0].request(node.encoded);
@@ -2499,12 +2491,11 @@ class Peernet {
2499
2491
  });
2500
2492
  if (closest[0]) data = await closest[0].request(node.encoded);
2501
2493
  }
2502
- if (data.data) {
2503
- console.log(data.data);
2504
- let proto = protoFor(data.data);
2505
- proto = protoFor(proto.decoded.data);
2506
- return proto.decoded.data
2507
- }
2494
+ data = new Uint8Array(Object.values(data));
2495
+ let proto = protoFor(data);
2496
+ proto = protoFor(proto.decoded.data);
2497
+ // TODO: store data automaticly or not
2498
+ return proto.decoded.data
2508
2499
 
2509
2500
  // this.put(hash, proto.decoded.data)
2510
2501
  }
@@ -2520,7 +2511,7 @@ class Peernet {
2520
2511
  * @param {String} hash
2521
2512
  */
2522
2513
  get: async (hash) => {
2523
- debug(`get message ${hash}`);
2514
+ debug$1(`get message ${hash}`);
2524
2515
  const message = await messageStore.has(hash);
2525
2516
  if (message) return await messageStore.get(hash)
2526
2517
  return this.requestData(hash, 'message')
@@ -2548,7 +2539,7 @@ class Peernet {
2548
2539
  * @param {String} hash
2549
2540
  */
2550
2541
  get: async (hash) => {
2551
- debug(`get data ${hash}`);
2542
+ debug$1(`get data ${hash}`);
2552
2543
  const data = await dataStore.has(hash);
2553
2544
  if (data) return await dataStore.get(hash)
2554
2545
  return this.requestData(hash, 'data')
@@ -2590,14 +2581,14 @@ class Peernet {
2590
2581
  * @param {String} store - storeName to access
2591
2582
  */
2592
2583
  async get(hash, store) {
2593
- debug(`get ${hash}`);
2584
+ debug$1(`get ${hash}`);
2594
2585
  let data;
2595
2586
  if (store) store = globalThis[`${store}Store`];
2596
2587
  if (!store) store = await this.whichStore([...this.stores], hash);
2597
2588
  if (store && await store.has(hash)) data = await store.get(hash);
2598
2589
  if (data) return data
2599
2590
 
2600
- return this.requestData(hash, store.name ? store.name : store)
2591
+ return this.requestData(hash, store?.name ? store.name : store)
2601
2592
  }
2602
2593
 
2603
2594
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.11.8",
3
+ "version": "0.11.9",
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.12",
28
+ "@leofcoin/peernet-swarm": "^0.1.16",
29
29
  "@leofcoin/storage": "^2.3.0",
30
30
  "@vandeurenglenn/base32": "^1.1.0",
31
31
  "@vandeurenglenn/base58": "^1.1.0",
@@ -46,6 +46,7 @@
46
46
  "@rollup/plugin-eslint": "^8.0.1",
47
47
  "@rollup/plugin-json": "^4.1.0",
48
48
  "@rollup/plugin-node-resolve": "^13.0.4",
49
+ "@vandeurenglenn/debug": "^1.0.0",
49
50
  "coveralls": "^3.1.1",
50
51
  "esdoc": "^1.1.0",
51
52
  "esdoc-ecmascript-proposal-plugin": "^1.0.0",
package/src/peernet.js CHANGED
@@ -260,7 +260,6 @@ export default class Peernet {
260
260
 
261
261
  const {id, proto} = message
262
262
  this.bw.down += proto.encoded.length
263
-
264
263
  if (proto.name === 'peernet-dht') {
265
264
  let { hash, store } = proto.decoded
266
265
  let has;
@@ -330,11 +329,9 @@ export default class Peernet {
330
329
  let result = await peer.request(node.encoded)
331
330
  result = new Uint8Array(Object.values(result))
332
331
  let proto = protoFor(result)
333
-
334
332
  if (proto.name !== 'peernet-message') throw encapsulatedError()
335
333
  const from = proto.decoded.from
336
334
  proto = protoFor(proto.decoded.data)
337
-
338
335
  if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
339
336
 
340
337
  // TODO: give ip and port (just used for location)
@@ -425,15 +422,15 @@ export default class Peernet {
425
422
  // get closest peer on earth
426
423
  const closestPeer = await this.dht.closestPeer(providers)
427
424
  // get peer instance by id
428
- if (!closestPeer || !closestPeer.id) return this.requestData(hash, store.name ? store.name : store)
425
+ if (!closestPeer || !closestPeer.id) return this.requestData(hash, store?.name ? store?.name : store)
429
426
 
430
- const id = closestPeer.id.toString()
427
+ const id = closestPeer.id
431
428
  if (this.connections) {
432
429
  let closest = this.connections.filter((peer) => {
433
430
  if (peer.peerId === id) return peer
434
431
  })
435
432
 
436
- let data = new DataMessage({hash, store: store.name ? store.name : store});
433
+ let data = new DataMessage({hash, store: store?.name ? store?.name : store});
437
434
 
438
435
  const node = await this.prepareMessage(id, data.encoded)
439
436
  if (closest[0]) data = await closest[0].request(node.encoded)
@@ -443,12 +440,11 @@ export default class Peernet {
443
440
  })
444
441
  if (closest[0]) data = await closest[0].request(node.encoded)
445
442
  }
446
- if (data.data) {
447
- console.log(data.data);
448
- let proto = protoFor(data.data)
449
- proto = protoFor(proto.decoded.data)
450
- return proto.decoded.data
451
- }
443
+ data = new Uint8Array(Object.values(data))
444
+ let proto = protoFor(data)
445
+ proto = protoFor(proto.decoded.data)
446
+ // TODO: store data automaticly or not
447
+ return proto.decoded.data
452
448
 
453
449
  // this.put(hash, proto.decoded.data)
454
450
  }
@@ -542,7 +538,7 @@ export default class Peernet {
542
538
  if (store && await store.has(hash)) data = await store.get(hash)
543
539
  if (data) return data
544
540
 
545
- return this.requestData(hash, store.name ? store.name : store)
541
+ return this.requestData(hash, store?.name ? store.name : store)
546
542
  }
547
543
 
548
544
  /**
package/test.js CHANGED
@@ -8,6 +8,7 @@ pubsub.subscribe('peer:connected', async peer => {
8
8
  request:'lastBlock'
9
9
  })
10
10
  const to = peer.id
11
+ await peernet.data.put('hello', 'hi')
11
12
  console.log(request);
12
13
  const node = await peernet.prepareMessage(to, request.encoded)
13
14
  console.log({node});
@@ -25,4 +26,10 @@ pubsub.subscribe('peer:connected', async peer => {
25
26
 
26
27
  const block = new TextDecoder().decode(response.decoded.response)
27
28
  console.log(block);
29
+ const task = () => setTimeout(() => {
30
+ console.log(peernet.connections[0]?.connected);
31
+ console.log(pubsub.subscribers);
32
+ task()
33
+ }, 5000);
34
+ task()
28
35
  })