@leofcoin/peernet 1.1.8 → 1.1.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.
@@ -1,4 +1,4 @@
1
- import { g as getDefaultExportFromCjs, c as commonjsGlobal } from './peernet-73c61737.js';
1
+ import { g as getDefaultExportFromCjs, c as commonjsGlobal } from './peernet-3281374b.js';
2
2
  import './value-157ab062.js';
3
3
 
4
4
  function commonjsRequire(path) {
@@ -10529,7 +10529,7 @@ var p2pt_umd = {
10529
10529
  this.responseWaiting[peer.id][data.id]([peer, msg]);
10530
10530
  delete this.responseWaiting[peer.id][data.id];
10531
10531
  } else {
10532
- this.emit('msg', peer, msg);
10532
+ this.emit('msg', peer, msg, data.from);
10533
10533
  }
10534
10534
  this._destroyChunks(data.id);
10535
10535
  }
@@ -10634,7 +10634,8 @@ var p2pt_umd = {
10634
10634
  return new Promise((resolve, reject) => {
10635
10635
  const data = {
10636
10636
  id: msgID !== '' ? msgID : Math.floor(Math.random() * 100000 + 100000),
10637
- msg
10637
+ msg,
10638
+ from: this.peerId
10638
10639
  };
10639
10640
 
10640
10641
  if (typeof msg === 'object') {
@@ -40890,16 +40891,9 @@ var p2pt_umd = {
40890
40891
  var P2PT = /*@__PURE__*/getDefaultExportFromCjs(p2pt_umdExports);
40891
40892
 
40892
40893
  class P2PTPeer {
40893
- #connected = false
40894
- #messageQue = []
40895
- #chunksQue = {}
40896
- #channel
40897
40894
  id;
40898
40895
  #peerId;
40899
40896
  #channelName
40900
- #chunkSize = 16 * 1024 // 16384
40901
- #queRunning = false
40902
- #MAX_BUFFERED_AMOUNT = 16 * 1024 * 1024
40903
40897
  initiator = false
40904
40898
  state;
40905
40899
  #connection
@@ -40908,6 +40902,10 @@ class P2PTPeer {
40908
40902
  return this.#channelName
40909
40903
  }
40910
40904
 
40905
+ get connected() {
40906
+ return this.#connection.connected
40907
+ }
40908
+
40911
40909
  constructor(peer, p2pt, options = {}) {
40912
40910
  this.#connection = peer;
40913
40911
  this.p2pt = p2pt;
@@ -40920,83 +40918,31 @@ class P2PTPeer {
40920
40918
 
40921
40919
  this.#channelName = peer.channelName;
40922
40920
 
40923
- this.#peerId = options.peerId;
40921
+ this.#peerId = this.p2pt.peerId;
40924
40922
  this.options = options;
40925
- return this.#init(options)
40926
- }
40927
-
40928
- async #init(options) {
40929
- if (!globalThis.pako) {
40930
- const importee = await import('./pako.esm-aa674ebf.js');
40931
- globalThis.pako = importee.default;
40932
- }
40933
- return this
40934
40923
  }
40935
40924
 
40936
40925
  _handleMessage(message) {
40937
-
40938
40926
  message = JSON.parse(new TextDecoder().decode(message.slice(1)));
40939
40927
  // allow sharding (multiple peers share data)
40940
40928
  pubsub.publish('peernet:shard', message);
40941
- const { id } = message;
40929
+ const { id, from } = message;
40942
40930
 
40943
40931
  // console.log(new TextDecoder().decode(new Uint8Array(Object.values(JSON.parse(message.msg)))));
40944
40932
  const data = new Uint8Array(Object.values(JSON.parse(message.msg)));
40945
- pubsub.publish('peer:data', { id, data, from: this.peerId });
40933
+ pubsub.publish('peer:data', { id, data, from, peer: this });
40946
40934
 
40947
40935
  this.bw.down += data.byteLength || data.length;
40948
40936
  }
40949
40937
 
40950
40938
  async send(data, id) {
40951
40939
  this.bw.up += data.byteLength || data.length;
40952
- return this.p2pt.send(this.#connection, data)
40953
- }
40954
-
40955
- request(data) {
40956
- return this.send(data)
40957
- }
40958
-
40959
- #trySend({ size, id, chunks }) {
40960
- let offset = 0;
40961
-
40962
- for (const chunk of chunks) {
40963
- const start = offset;
40964
- const end = offset + chunk.length;
40965
-
40966
- const message = new TextEncoder().encode(JSON.stringify({ size, id, chunk, start, end }));
40967
- this.#messageQue.push(message);
40968
- }
40969
-
40970
- if (!this.queRunning) return this.#runQue()
40971
- }
40972
-
40973
- async #runQue() {
40974
- this.#queRunning = true;
40975
- if (this.#messageQue.length > 0 && this.#channel?.bufferedAmount + this.#messageQue[0]?.length < this.#MAX_BUFFERED_AMOUNT) {
40976
- const message = this.#messageQue.shift();
40977
- await this.#connection.send(message);
40978
- if (this.#messageQue.length > 0) return this.#runQue()
40979
- } else {
40980
- return setTimeout(() => this.#runQue(), 50)
40981
- }
40940
+ return this.p2pt.send(this.#connection, data, id)
40982
40941
  }
40983
40942
 
40984
- splitMessage(message) {
40985
- const chunks = [];
40986
- message = pako.deflate(message);
40987
- const size = message.byteLength || message.length;
40988
- let offset = 0;
40989
- return new Promise((resolve, reject) => {
40990
- const splitMessage = () => {
40991
- const chunk = message.slice(offset, offset + this.#chunkSize > size ? size : offset + this.#chunkSize);
40992
- offset += this.#chunkSize;
40993
- chunks.push(chunk);
40994
- if (offset < size) return splitMessage()
40995
- else resolve({chunks, size});
40996
- };
40997
-
40998
- splitMessage();
40999
- })
40943
+ async request(data) {
40944
+ const [peer, msg] = await this.send(data);
40945
+ return msg
41000
40946
  }
41001
40947
 
41002
40948
  get peerId() {
@@ -41056,8 +41002,9 @@ class LittlePubSub {
41056
41002
  }
41057
41003
 
41058
41004
  if (!globalThis.pubsub) globalThis.pubsub = new LittlePubSub();
41005
+
41059
41006
  class P2PTClient extends P2PT {
41060
- #connections = {
41007
+ #discovered = {
41061
41008
 
41062
41009
  }
41063
41010
 
@@ -41066,81 +41013,45 @@ class P2PTClient extends P2PT {
41066
41013
  down: 0
41067
41014
  }
41068
41015
 
41069
- // static trackersAnnounceURLs = [
41070
- // // "wss://tracker.openw/ebtorrent.com",
41071
- // 'wss://peach.leofcoin.org'
41072
- // // "wss://tracker.btorrent.xyz"
41073
- // ]
41074
- static JSON_MESSAGE_IDENTIFIER = '^'
41016
+ get discovered() {
41017
+ return this.#discovered || {}
41018
+ }
41075
41019
 
41076
41020
  constructor(peerId, networkVersion = 'leofcoin:peach', stars = ['wss://peach.leofcoin.org']) {
41077
- super(stars, networkVersion);
41021
+ super(stars, networkVersion, peerId);
41022
+
41078
41023
  this.stars = stars;
41079
41024
  this.networkVersion = networkVersion;
41080
41025
  this.peerId = peerId;
41081
41026
 
41082
- // If a tracker connection was successful
41083
- this.on('trackerconnect', async (tracker, stats) => {
41084
- // console.log({tracker});
41085
- const peers = tracker.peers;
41086
- // console.log(peers);
41087
- for (const [id, peer] of Object.entries(peers)) {
41088
- this.#connections[id] = await new P2PTPeer(peer, this);
41089
- // promises.push(() => peer.send('request'))
41090
- // this.peers.includes(peerId)
41091
- }
41092
- });
41093
-
41094
- // If a new peer, send message
41095
- this.on('peerconnect', async (peer) => {
41096
- console.log(peer.id);
41097
- console.log('id');
41098
- this.#connections[peer.id] = await new P2PTPeer(peer, this);
41099
- // console.log(peer);
41100
- console.log(peer.id);
41101
- // console.log(peer.send(new TextEncoder().encode(JSON.stringify({data: {type: 'requestId', from: this.peerId}}))));
41102
- // await this.#connections[peer.id].send(new TextEncoder().encode(JSON.stringify({type: 'requestId', from: this.peerId})))
41103
- const id = await this.#connections[peer.id].request(new TextEncoder().encode(JSON.stringify({type: 'requestId', from: this.peerId})));
41104
- console.log({id});
41105
- // this.send(peer, 'Hi').then(([peer, msg]) => {
41106
- // console.log('Got response : ' + msg)
41107
-
41108
- // this.send(peer, 'request')
41109
- // return peer.respond('Bye')
41110
- // }).then(([peer, msg]) => {
41111
- // console.log('Got response2 : ' + msg)
41112
- // })
41113
- });
41114
-
41115
- // If message received from peer
41116
- // this.on('msg', async (peer, msg) => {
41117
- // console.log({msg});
41118
- // const hasPeer = this.#connections[peer.id]
41119
- // if (!hasPeer) this.#connections[peer.id] = await new P2PTPeer(peer, this)
41120
- // // data.message = new Uint8Array()
41121
- // this.#connections[peer.id]?._handleMessage(new Uint8Array(Object.values(msg)))
41122
- // if (msg.type === 'requestId') peer.respond(this.peerId)
41123
- // console.log(`Got message from ${peer.id} : ${msg}`)
41124
- // if (msg === 'Hi') {
41125
- // peer.respond('Hello !').then(([peer, msg]) => {
41126
- // peer.respond('Bye !')
41127
- // })
41128
- // }
41129
- // })
41130
-
41131
- this.on('data', async (peer, data) => {
41132
- // console.log(peer.id);
41133
- // data = data.slice(1)
41134
- // console.log(data);
41135
- const hasPeer = this.#connections[peer.id];
41136
- if (!hasPeer) this.#connections[peer.id] = await new P2PTPeer(peer, this);
41137
- // data.message = new Uint8Array()
41138
- this.#connections[peer.id]?._handleMessage(new Uint8Array(Object.values(data)));
41139
- console.log(this.#connections[peer.id]?.bw);
41140
- this.bw.down += data.length || data.byteLength;
41141
- });
41142
- console.log('P2PT started. My peer id : ' + this._peerId);
41143
- this.start();
41027
+ this.on('trackerconnect', async (tracker, stats) => {
41028
+ const peers = tracker.peers;
41029
+ let promises = Object.entries(peers).map(async ([id, peer]) => {
41030
+ this.#discovered[id] = await new P2PTPeer(peer, this);
41031
+ });
41032
+
41033
+ promises = await Promise.allSettled(promises);
41034
+ pubsub.publish('star:connected', tracker);
41035
+ });
41036
+
41037
+ this.on('peerconnect', async (peer) => {
41038
+ this.#discovered[peer.id] = await new P2PTPeer(peer, this);
41039
+ pubsub.publish('peer:discovered', this.#discovered[peer.id]);
41040
+ });
41041
+
41042
+ this.on('peerclose', async (peer) => {
41043
+ pubsub.publish('peer:left', this.#discovered[peer.id]);
41044
+ });
41045
+
41046
+ this.on('data', async (peer, data) => {
41047
+ const hasPeer = this.#discovered[peer.id];
41048
+ if (!hasPeer) this.#discovered[peer.id] = await new P2PTPeer(peer, this);
41049
+
41050
+ this.#discovered[peer.id]?._handleMessage(new Uint8Array(Object.values(data)));
41051
+ this.bw.down += data.length || data.byteLength;
41052
+ });
41053
+ console.log('Peer started as: ' + this._peerId);
41054
+ this.start();
41144
41055
  }
41145
41056
 
41146
41057
  }
@@ -1,4 +1,4 @@
1
- import { M as MultiWallet, e as encrypt, b as base58$1 } from './peernet-73c61737.js';
1
+ import { M as MultiWallet, e as encrypt, b as base58$1 } from './peernet-3281374b.js';
2
2
  import './value-157ab062.js';
3
3
 
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { F as FormatInterface } from './peernet-73c61737.js';
1
+ import { F as FormatInterface } from './peernet-3281374b.js';
2
2
  import './value-157ab062.js';
3
3
 
4
4
  var proto$b = {
@@ -2190,9 +2190,9 @@ class MessageHandler {
2190
2190
  const dataHandler = async (message) => {
2191
2191
  if (!message)
2192
2192
  return;
2193
- const { data, id, from } = message;
2193
+ const { data, id, from, peer } = message;
2194
2194
  const proto = await protoFor(data);
2195
- peernet._protoHandler({ id, proto }, peernet.client.connections[from], from);
2195
+ peernet._protoHandler({ id, proto }, peernet.connections[from] || peer, from);
2196
2196
  };
2197
2197
 
2198
2198
  const dhtError = (proto) => {
@@ -16086,7 +16086,7 @@ class Identity {
16086
16086
  globalThis.peernet.selectedAccount = new TextDecoder().decode(selected);
16087
16087
  }
16088
16088
  else {
16089
- const importee = await import(/* webpackChunkName: "generate-account" */ './index-60443f35.js');
16089
+ const importee = await import(/* webpackChunkName: "generate-account" */ './index-59ec4d46.js');
16090
16090
  const { identity, accounts } = await importee.default(password, this.network);
16091
16091
  await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
16092
16092
  await globalThis.walletStore.put('version', String(1));
@@ -16150,6 +16150,7 @@ class Peernet {
16150
16150
  autoStart = true;
16151
16151
  #starting = false;
16152
16152
  #started = false;
16153
+ #connections = {};
16153
16154
  /**
16154
16155
  * @access public
16155
16156
  * @param {Object} options
@@ -16228,13 +16229,13 @@ class Peernet {
16228
16229
  * @return {Array} peerId
16229
16230
  */
16230
16231
  get peers() {
16231
- return Object.keys(this.client.connections);
16232
+ return Object.values(this.#connections);
16232
16233
  }
16233
16234
  get connections() {
16234
- return Object.values(this.client.connections);
16235
+ return Object.values(this.#connections);
16235
16236
  }
16236
16237
  get peerEntries() {
16237
- return Object.entries(this.client.connections);
16238
+ return Object.values(this.#connections);
16238
16239
  }
16239
16240
  /**
16240
16241
  * @return {String} id - peerId
@@ -16256,7 +16257,7 @@ class Peernet {
16256
16257
  this.root = options.root;
16257
16258
  const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
16258
16259
  // FolderMessageResponse
16259
- } = await import(/* webpackChunkName: "messages" */ './messages-54d97ac1.js');
16260
+ } = await import(/* webpackChunkName: "messages" */ './messages-c720a8ba.js');
16260
16261
  /**
16261
16262
  * proto Object containing protos
16262
16263
  * @type {Object}
@@ -16290,15 +16291,19 @@ class Peernet {
16290
16291
  await this.identity.load(password);
16291
16292
  this._peerHandler = new PeerDiscovery(this.id);
16292
16293
  this.peerId = this.id;
16293
- pubsub.subscribe('peer:connected', async (peer) => {
16294
+ this.addRequestHandler('handshake', () => {
16295
+ return new peernet.protos['peernet-response']({ response: { peerId: this.id } });
16296
+ });
16297
+ pubsub.subscribe('peer:discovered', async (peer) => {
16294
16298
  // console.log(peer);
16295
- // console.log({connected: peer.id, as: this._getPeerId(peer.id) });
16296
- // peer.on('peernet.data', async (message) => {
16297
- // const id = message.id
16298
- // message = new PeernetMessage(Buffer.from(message.data.data))
16299
- // const proto = protoFor(message.decoded.data)
16300
- // this._protoHandler({id, proto}, peer)
16301
- // })
16299
+ let data = await new globalThis.peernet.protos['peernet-request']({ request: 'handshake' });
16300
+ let node = await globalThis.peernet.prepareMessage(data);
16301
+ let response = await peer.request(node.encoded);
16302
+ response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
16303
+ // todo: response.decoded should be the response and not response.peerId
16304
+ this.#connections[response.decoded.response.peerId] = peer;
16305
+ pubsub.publish('peer:connected', peer);
16306
+ // todo: cleanup discovered
16302
16307
  });
16303
16308
  /**
16304
16309
  * converts data -> message -> proto
@@ -16323,7 +16328,7 @@ class Peernet {
16323
16328
  if (this.#starting || this.#started)
16324
16329
  return;
16325
16330
  this.#starting = true;
16326
- const importee = await import('./client-56d3de03.js');
16331
+ const importee = await import('./client-6f8b633a.js');
16327
16332
  /**
16328
16333
  * @access public
16329
16334
  * @type {PeernetClient}
@@ -16335,12 +16340,13 @@ class Peernet {
16335
16340
  addRequestHandler(name, method) {
16336
16341
  this.requestProtos[name] = method;
16337
16342
  }
16338
- sendMessage(peer, id, data) {
16339
- if (peer.readyState === 'open') {
16340
- peer.send(data, id);
16341
- this.bw.up += data.length;
16342
- }
16343
- else if (peer.readyState === 'closed') ;
16343
+ async sendMessage(peer, id, data) {
16344
+ // if (peer.readyState === 'open') {
16345
+ await peer.send(data, id);
16346
+ this.bw.up += data.length;
16347
+ // } else if (peer.readyState === 'closed') {
16348
+ // this.removePeer(peer)
16349
+ // }
16344
16350
  }
16345
16351
  async handleDHT(peer, id, proto) {
16346
16352
  let { hash, store } = proto.decoded;
@@ -16397,6 +16403,7 @@ class Peernet {
16397
16403
  }
16398
16404
  case 'peernet-request': {
16399
16405
  this.handleRequest(peer, id, proto);
16406
+ break;
16400
16407
  }
16401
16408
  case 'peernet-ps': {
16402
16409
  if (peer.peerId !== this.id)
@@ -1,2 +1,2 @@
1
- export { P as default } from './peernet-73c61737.js';
1
+ export { P as default } from './peernet-3281374b.js';
2
2
  import './value-157ab062.js';
@@ -290,9 +290,9 @@ class MessageHandler {
290
290
  const dataHandler = async (message) => {
291
291
  if (!message)
292
292
  return;
293
- const { data, id, from } = message;
293
+ const { data, id, from, peer } = message;
294
294
  const proto = await protoFor(data);
295
- peernet._protoHandler({ id, proto }, peernet.client.connections[from], from);
295
+ peernet._protoHandler({ id, proto }, peernet.connections[from] || peer, from);
296
296
  };
297
297
 
298
298
  const dhtError = (proto) => {
@@ -404,6 +404,7 @@ class Peernet {
404
404
  autoStart = true;
405
405
  #starting = false;
406
406
  #started = false;
407
+ #connections = {};
407
408
  /**
408
409
  * @access public
409
410
  * @param {Object} options
@@ -482,13 +483,13 @@ class Peernet {
482
483
  * @return {Array} peerId
483
484
  */
484
485
  get peers() {
485
- return Object.keys(this.client.connections);
486
+ return Object.values(this.#connections);
486
487
  }
487
488
  get connections() {
488
- return Object.values(this.client.connections);
489
+ return Object.values(this.#connections);
489
490
  }
490
491
  get peerEntries() {
491
- return Object.entries(this.client.connections);
492
+ return Object.values(this.#connections);
492
493
  }
493
494
  /**
494
495
  * @return {String} id - peerId
@@ -544,15 +545,19 @@ class Peernet {
544
545
  await this.identity.load(password);
545
546
  this._peerHandler = new PeerDiscovery(this.id);
546
547
  this.peerId = this.id;
547
- pubsub.subscribe('peer:connected', async (peer) => {
548
+ this.addRequestHandler('handshake', () => {
549
+ return new peernet.protos['peernet-response']({ response: { peerId: this.id } });
550
+ });
551
+ pubsub.subscribe('peer:discovered', async (peer) => {
548
552
  // console.log(peer);
549
- // console.log({connected: peer.id, as: this._getPeerId(peer.id) });
550
- // peer.on('peernet.data', async (message) => {
551
- // const id = message.id
552
- // message = new PeernetMessage(Buffer.from(message.data.data))
553
- // const proto = protoFor(message.decoded.data)
554
- // this._protoHandler({id, proto}, peer)
555
- // })
553
+ let data = await new globalThis.peernet.protos['peernet-request']({ request: 'handshake' });
554
+ let node = await globalThis.peernet.prepareMessage(data);
555
+ let response = await peer.request(node.encoded);
556
+ response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
557
+ // todo: response.decoded should be the response and not response.peerId
558
+ this.#connections[response.decoded.response.peerId] = peer;
559
+ pubsub.publish('peer:connected', peer);
560
+ // todo: cleanup discovered
556
561
  });
557
562
  /**
558
563
  * converts data -> message -> proto
@@ -577,7 +582,7 @@ class Peernet {
577
582
  if (this.#starting || this.#started)
578
583
  return;
579
584
  this.#starting = true;
580
- const importee = await import('@netpeer/p2pt-swarm/src/client/client.js');
585
+ const importee = await import('@netpeer/p2pt-swarm/client.js');
581
586
  /**
582
587
  * @access public
583
588
  * @type {PeernetClient}
@@ -589,12 +594,13 @@ class Peernet {
589
594
  addRequestHandler(name, method) {
590
595
  this.requestProtos[name] = method;
591
596
  }
592
- sendMessage(peer, id, data) {
593
- if (peer.readyState === 'open') {
594
- peer.send(data, id);
595
- this.bw.up += data.length;
596
- }
597
- else if (peer.readyState === 'closed') ;
597
+ async sendMessage(peer, id, data) {
598
+ // if (peer.readyState === 'open') {
599
+ await peer.send(data, id);
600
+ this.bw.up += data.length;
601
+ // } else if (peer.readyState === 'closed') {
602
+ // this.removePeer(peer)
603
+ // }
598
604
  }
599
605
  async handleDHT(peer, id, proto) {
600
606
  let { hash, store } = proto.decoded;
@@ -651,6 +657,7 @@ class Peernet {
651
657
  }
652
658
  case 'peernet-request': {
653
659
  this.handleRequest(peer, id, proto);
660
+ break;
654
661
  }
655
662
  case 'peernet-ps': {
656
663
  if (peer.peerId !== this.id)
package/index.html CHANGED
@@ -9,7 +9,7 @@
9
9
  </script> -->
10
10
 
11
11
  <script type="module">
12
- import Peernet from './dist/peernet.js';
12
+ import Peernet from './exports/browser/peernet.js';
13
13
  (async () => {
14
14
  const peernet = await new Peernet({network: 'leofcoin:peach', networkVersion: 'peach'})
15
15
  peernet.addRequestHandler('lastBlock', () => new peernet.protos['peernet-response']({response: new TextEncoder().encode(100)}))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "1.1.8",
3
+ "version": "1.1.9",
4
4
  "description": "",
5
5
  "main": "src/peernet.js",
6
6
  "exports": {
@@ -31,7 +31,7 @@
31
31
  "@leofcoin/multi-wallet": "^3.0.1",
32
32
  "@leofcoin/peernet-swarm": "^1.1.0",
33
33
  "@leofcoin/storage": "^3.0.0",
34
- "@netpeer/p2pt-swarm": "^1.0.0",
34
+ "@netpeer/p2pt-swarm": "^1.1.5",
35
35
  "@types/node": "^18.11.18",
36
36
  "@vandeurenglenn/base32": "^1.1.0",
37
37
  "@vandeurenglenn/base58": "^1.1.0",
@@ -3,10 +3,9 @@ import { protoFor } from './../utils/utils.js'
3
3
  const dataHandler = async message => {
4
4
  if (!message) return
5
5
 
6
- const {data, id, from} = message
6
+ const {data, id, from, peer} = message
7
7
  const proto = await protoFor(data)
8
-
9
- peernet._protoHandler({id, proto}, peernet.client.connections[from], from)
8
+ peernet._protoHandler({id, proto}, peernet.connections[from] || peer, from)
10
9
  }
11
10
 
12
11
  export default dataHandler
package/src/peernet.ts CHANGED
@@ -46,6 +46,7 @@ export default class Peernet {
46
46
  autoStart: boolean = true
47
47
  #starting: boolean = false
48
48
  #started: boolean = false
49
+ #connections = {}
49
50
 
50
51
  /**
51
52
  * @access public
@@ -135,15 +136,15 @@ export default class Peernet {
135
136
  * @return {Array} peerId
136
137
  */
137
138
  get peers() {
138
- return Object.keys(this.client.connections)
139
+ return Object.values(this.#connections)
139
140
  }
140
141
 
141
142
  get connections() {
142
- return Object.values(this.client.connections)
143
+ return Object.values(this.#connections)
143
144
  }
144
145
 
145
146
  get peerEntries() {
146
- return Object.entries(this.client.connections)
147
+ return Object.values(this.#connections)
147
148
  }
148
149
 
149
150
  /**
@@ -223,15 +224,22 @@ export default class Peernet {
223
224
  this._peerHandler = new PeerDiscovery(this.id)
224
225
  this.peerId = this.id
225
226
 
226
- pubsub.subscribe('peer:connected', async (peer) => {
227
+ this.addRequestHandler('handshake', ( ) => {
228
+ return new peernet.protos['peernet-response']({response: {peerId: this.id}})
229
+ })
230
+
231
+ pubsub.subscribe('peer:discovered', async (peer) => {
227
232
  // console.log(peer);
228
- // console.log({connected: peer.id, as: this._getPeerId(peer.id) });
229
- // peer.on('peernet.data', async (message) => {
230
- // const id = message.id
231
- // message = new PeernetMessage(Buffer.from(message.data.data))
232
- // const proto = protoFor(message.decoded.data)
233
- // this._protoHandler({id, proto}, peer)
234
- // })
233
+
234
+ let data = await new globalThis.peernet.protos['peernet-request']({request: 'handshake'});
235
+ let node = await globalThis.peernet.prepareMessage(data);
236
+ let response = await peer.request(node.encoded)
237
+
238
+ response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)))
239
+ // todo: response.decoded should be the response and not response.peerId
240
+ this.#connections[response.decoded.response.peerId] = peer
241
+ pubsub.publish('peer:connected', peer)
242
+ // todo: cleanup discovered
235
243
  })
236
244
 
237
245
  /**
@@ -257,7 +265,7 @@ export default class Peernet {
257
265
  if (this.#starting || this.#started) return
258
266
 
259
267
  this.#starting = true
260
- const importee = await import('@netpeer/p2pt-swarm/src/client/client.js')
268
+ const importee = await import('@netpeer/p2pt-swarm/client.js')
261
269
  /**
262
270
  * @access public
263
271
  * @type {PeernetClient}
@@ -271,13 +279,14 @@ export default class Peernet {
271
279
  this.requestProtos[name] = method
272
280
  }
273
281
 
274
- sendMessage(peer, id, data) {
275
- if (peer.readyState === 'open') {
276
- peer.send(data, id)
282
+ async sendMessage(peer, id, data) {
283
+
284
+ // if (peer.readyState === 'open') {
285
+ await peer.send(data, id)
277
286
  this.bw.up += data.length
278
- } else if (peer.readyState === 'closed') {
287
+ // } else if (peer.readyState === 'closed') {
279
288
  // this.removePeer(peer)
280
- }
289
+ // }
281
290
 
282
291
  }
283
292
 
@@ -334,6 +343,7 @@ export default class Peernet {
334
343
  */
335
344
  async _protoHandler(message, peer, from) {
336
345
  const {id, proto} = message
346
+
337
347
  this.bw.down += proto.encoded.length
338
348
  switch(proto.name) {
339
349
  case 'peernet-dht': {
@@ -346,6 +356,7 @@ export default class Peernet {
346
356
  }
347
357
  case 'peernet-request': {
348
358
  this.handleRequest(peer, id, proto)
359
+ break
349
360
  }
350
361
 
351
362
  case 'peernet-ps': {