@leofcoin/peernet 0.11.19 → 0.11.22

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.
@@ -287,6 +287,8 @@ const socketRequestClient = (url, protocols = 'echo-protocol', options = { retry
287
287
  });
288
288
  };
289
289
 
290
+ const messageQue = {};
291
+
290
292
  class Peer {
291
293
  #connection
292
294
  #ready = false
@@ -305,6 +307,7 @@ class Peer {
305
307
  #iceCompleteTimer
306
308
  #channel
307
309
  #peerId
310
+ #chunkSize = 16384
308
311
 
309
312
  get connection() {
310
313
  return this.#connection
@@ -356,29 +359,51 @@ class Peer {
356
359
  return this._socketClient
357
360
  }
358
361
 
359
- send(message) {
360
- switch (this.channel?.readyState) {
361
- case 'open':
362
- this.bw.up += message.length || message.byteLength;
363
- this.channel.send(message);
364
- break;
365
- case 'closed':
366
- case 'closing':
367
- debug('channel already closed, this usually means a bad implementation, try checking the readyState or check if the peer is connected before sending');
368
- break;
369
- case undefined:
370
- debug(`trying to send before a channel is created`);
371
- break;
372
- }
362
+ splitMessage(message) {
363
+ const chunks = [];
364
+ const size = message.byteLength || message.length;
365
+ let offset = 0;
366
+ return new Promise((resolve, reject) => {
367
+ const splitMessage = () => {
368
+ const chunk = message.slice(offset, offset + this.#chunkSize > size ? size : offset + this.#chunkSize);
369
+ offset += this.#chunkSize;
370
+ chunks.push(chunk);
371
+ if (offset < size) return splitMessage()
372
+ else resolve({chunks, size});
373
+ };
374
+
375
+ splitMessage();
376
+ })
377
+ }
378
+
379
+ async send(message, id) {
380
+ const { chunks, size } = await this.splitMessage(message);
381
+ let offset = 0;
382
+ for (const chunk of chunks) {
383
+ const start = offset;
384
+ const end = offset + chunk.length;
385
+ const message = new TextEncoder().encode(JSON.stringify({ size, id, chunk, start, end }));
386
+ switch (this.channel?.readyState) {
387
+ case 'open':
388
+ this.bw.up += message.length || message.byteLength;
389
+ this.channel.send(message);
390
+ break;
391
+ case 'closed':
392
+ case 'closing':
393
+ debug('channel already closed, this usually means a bad implementation, try checking the readyState or check if the peer is connected before sending');
394
+ break;
395
+ case undefined:
396
+ debug(`trying to send before a channel is created`);
397
+ break;
398
+ }
399
+ }
373
400
  }
374
401
 
375
402
  request(data) {
376
403
  return new Promise((resolve, reject) => {
377
404
  const id = Math.random().toString(36).slice(-12);
378
- // TODO: get rid of JSON
379
- data = new TextEncoder().encode(JSON.stringify({id, data}));
405
+
380
406
  const _onData = message => {
381
- message = JSON.parse(new TextDecoder().decode(message.data));
382
407
  if (message.id === id) {
383
408
  resolve(message.data);
384
409
  pubsub.unsubscribe(`peer:data`, _onData);
@@ -392,8 +417,8 @@ class Peer {
392
417
  // pubsub.unsubscribe(`peer:data-request-${id}`, _onData)
393
418
  // }, 5000);
394
419
 
395
- this.send(data);
396
- });
420
+ this.send(data, id);
421
+ })
397
422
  }
398
423
 
399
424
  async #init() {
@@ -423,10 +448,7 @@ class Peer {
423
448
  message.channel.onclose = () => this.close.bind(this);
424
449
 
425
450
  message.channel.onmessage = (message) => {
426
- pubsub.publish('peer:data', message);
427
- debug(`incoming message from ${this.id}`);
428
- debug(message);
429
- this.bw.down += message.length || message.byteLength;
451
+ this._handleMessage(this.id, message);
430
452
  };
431
453
  this.channel = message.channel;
432
454
  };
@@ -441,28 +463,38 @@ class Peer {
441
463
  this.channel.onclose = () => this.close.bind(this);
442
464
 
443
465
  this.channel.onmessage = (message) => {
444
- pubsub.publish('peer:data', message);
445
- debug(`incoming message from ${this.peerId}`);
446
- this.bw.down += message.length || message.byteLength;
466
+ this._handleMessage(this.peerId, message);
447
467
  };
448
468
 
449
469
  const offer = await this.#connection.createOffer();
450
470
  await this.#connection.setLocalDescription(offer);
451
471
 
452
472
  this._sendMessage({'sdp': this.#connection.localDescription});
473
+ }
474
+ } catch (e) {
475
+ console.log(e);
476
+ }
477
+ }
453
478
 
454
- this.#connection.onnegotiationneeded = async () => {
455
- const offer = await this.#connection.createOffer();
456
- await this.#connection.setLocalDescription(offer);
479
+ _handleMessage(peerId, message) {
480
+ debug(`incoming message from ${peerId}`);
457
481
 
458
- this._sendMessage({'sdp': this.#connection.localDescription});
459
- };
460
- }
461
- // }
482
+ message = JSON.parse(new TextDecoder().decode(message.data));
483
+ // allow sharding (multiple peers share data)
484
+ pubsub.publish('peernet:shard', message);
485
+ if (!messageQue[message.id]) messageQue[message.id] = [];
462
486
 
463
- } catch (e) {
464
- console.log(e);
487
+ if (message.size > messageQue[message.id].length || message.size === messageQue[message.id].length) {
488
+ for (const value of Object.values(message.chunk)) {
489
+ messageQue[message.id].push(value);
490
+ }
465
491
  }
492
+
493
+ if (message.size === messageQue[message.id].length) {
494
+ pubsub.publish('peer:data', {id: message.id, data: new Uint8Array(Object.values(messageQue[message.id]))});
495
+ delete messageQue[message.id];
496
+ }
497
+ this.bw.down += message.byteLength || message.length;
466
498
  }
467
499
 
468
500
  _sendMessage(message) {
@@ -626,7 +658,8 @@ class Client {
626
658
  debug(`star ${id} left`);
627
659
  }
628
660
 
629
- peerLeft(id) {
661
+ peerLeft(peer) {
662
+ const id = peer.peerId || peer;
630
663
  if (this.#connections[id]) {
631
664
  this.#connections[id].close();
632
665
  delete this.#connections[id];
@@ -634,7 +667,8 @@ class Client {
634
667
  debug(`peer ${id} left`);
635
668
  }
636
669
 
637
- peerJoined(id, signal) {
670
+ peerJoined(peer, signal) {
671
+ const id = peer.peerId || peer;
638
672
  if (this.#connections[id]) {
639
673
  if (this.#connections[id].connected) this.#connections[id].close();
640
674
  delete this.#connections[id];
@@ -2267,6 +2301,18 @@ class MessageHandler {
2267
2301
  }
2268
2302
  }
2269
2303
 
2304
+ const dataHandler = async message => {
2305
+ if (!message) return
2306
+
2307
+ const {data, id} = message;
2308
+
2309
+ message = protoFor(data);
2310
+ const proto = protoFor(message.decoded.data);
2311
+ const from = message.decoded.from;
2312
+
2313
+ peernet._protoHandler({id, proto}, peernet.client.connections[from], from);
2314
+ };
2315
+
2270
2316
  const encapsulatedError = () => {
2271
2317
  return new Error('Nodes/Data should be send encapsulated by peernet-message')
2272
2318
  };
@@ -2476,19 +2522,11 @@ class Peernet {
2476
2522
  // })
2477
2523
  });
2478
2524
 
2479
- pubsub.subscribe('peer:data', async message => {
2480
- if (!message.data) return
2481
- const {id, data} = JSON.parse(new TextDecoder().decode(message.data));
2482
- const uint8Array = new Uint8Array(Object.keys(data).length);
2483
- for (var i = 0; i < Object.keys(data).length; i++) {
2484
- uint8Array[i] = data[i];
2485
- }
2486
- message = new PeernetMessage(uint8Array);
2487
- const proto = protoFor(message.decoded.data);
2488
-
2489
- const from = message.decoded.from;
2490
- this._protoHandler({id, proto}, this.client.connections[from], from);
2491
- });
2525
+ /**
2526
+ * converts data -> message -> proto
2527
+ * @see DataHandler
2528
+ */
2529
+ pubsub.subscribe('peer:data', dataHandler);
2492
2530
 
2493
2531
  /**
2494
2532
  * @access public
@@ -2507,11 +2545,9 @@ class Peernet {
2507
2545
 
2508
2546
  sendMessage(peer, id, data) {
2509
2547
  if (peer.readyState === 'open') {
2510
- peer.send(new TextEncoder().encode(JSON.stringify({id, data})));
2548
+ peer.send(data, id);
2511
2549
  this.bw.up += data.length;
2512
- } else if (peer.readyState === 'closed') {
2513
- this.removePeer(peer);
2514
- }
2550
+ } else if (peer.readyState === 'closed') ;
2515
2551
 
2516
2552
  }
2517
2553
 
@@ -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.18";
13
+ var version = "0.11.21";
14
14
 
15
15
  var api$1 = {
16
16
  version: ({send}) => send({client: '@peernet/api/http', version}),
@@ -285,6 +285,8 @@ const socketRequestClient = (url, protocols = 'echo-protocol', options = { retry
285
285
  });
286
286
  };
287
287
 
288
+ const messageQue = {};
289
+
288
290
  class Peer {
289
291
  #connection
290
292
  #ready = false
@@ -303,6 +305,7 @@ class Peer {
303
305
  #iceCompleteTimer
304
306
  #channel
305
307
  #peerId
308
+ #chunkSize = 16384
306
309
 
307
310
  get connection() {
308
311
  return this.#connection
@@ -354,29 +357,51 @@ class Peer {
354
357
  return this._socketClient
355
358
  }
356
359
 
357
- send(message) {
358
- switch (this.channel?.readyState) {
359
- case 'open':
360
- this.bw.up += message.length || message.byteLength;
361
- this.channel.send(message);
362
- break;
363
- case 'closed':
364
- case 'closing':
365
- debug('channel already closed, this usually means a bad implementation, try checking the readyState or check if the peer is connected before sending');
366
- break;
367
- case undefined:
368
- debug(`trying to send before a channel is created`);
369
- break;
370
- }
360
+ splitMessage(message) {
361
+ const chunks = [];
362
+ const size = message.byteLength || message.length;
363
+ let offset = 0;
364
+ return new Promise((resolve, reject) => {
365
+ const splitMessage = () => {
366
+ const chunk = message.slice(offset, offset + this.#chunkSize > size ? size : offset + this.#chunkSize);
367
+ offset += this.#chunkSize;
368
+ chunks.push(chunk);
369
+ if (offset < size) return splitMessage()
370
+ else resolve({chunks, size});
371
+ };
372
+
373
+ splitMessage();
374
+ })
375
+ }
376
+
377
+ async send(message, id) {
378
+ const { chunks, size } = await this.splitMessage(message);
379
+ let offset = 0;
380
+ for (const chunk of chunks) {
381
+ const start = offset;
382
+ const end = offset + chunk.length;
383
+ const message = new TextEncoder().encode(JSON.stringify({ size, id, chunk, start, end }));
384
+ switch (this.channel?.readyState) {
385
+ case 'open':
386
+ this.bw.up += message.length || message.byteLength;
387
+ this.channel.send(message);
388
+ break;
389
+ case 'closed':
390
+ case 'closing':
391
+ debug('channel already closed, this usually means a bad implementation, try checking the readyState or check if the peer is connected before sending');
392
+ break;
393
+ case undefined:
394
+ debug(`trying to send before a channel is created`);
395
+ break;
396
+ }
397
+ }
371
398
  }
372
399
 
373
400
  request(data) {
374
401
  return new Promise((resolve, reject) => {
375
402
  const id = Math.random().toString(36).slice(-12);
376
- // TODO: get rid of JSON
377
- data = new TextEncoder().encode(JSON.stringify({id, data}));
403
+
378
404
  const _onData = message => {
379
- message = JSON.parse(new TextDecoder().decode(message.data));
380
405
  if (message.id === id) {
381
406
  resolve(message.data);
382
407
  pubsub.unsubscribe(`peer:data`, _onData);
@@ -390,8 +415,8 @@ class Peer {
390
415
  // pubsub.unsubscribe(`peer:data-request-${id}`, _onData)
391
416
  // }, 5000);
392
417
 
393
- this.send(data);
394
- });
418
+ this.send(data, id);
419
+ })
395
420
  }
396
421
 
397
422
  async #init() {
@@ -421,10 +446,7 @@ class Peer {
421
446
  message.channel.onclose = () => this.close.bind(this);
422
447
 
423
448
  message.channel.onmessage = (message) => {
424
- pubsub.publish('peer:data', message);
425
- debug(`incoming message from ${this.id}`);
426
- debug(message);
427
- this.bw.down += message.length || message.byteLength;
449
+ this._handleMessage(this.id, message);
428
450
  };
429
451
  this.channel = message.channel;
430
452
  };
@@ -439,28 +461,38 @@ class Peer {
439
461
  this.channel.onclose = () => this.close.bind(this);
440
462
 
441
463
  this.channel.onmessage = (message) => {
442
- pubsub.publish('peer:data', message);
443
- debug(`incoming message from ${this.peerId}`);
444
- this.bw.down += message.length || message.byteLength;
464
+ this._handleMessage(this.peerId, message);
445
465
  };
446
466
 
447
467
  const offer = await this.#connection.createOffer();
448
468
  await this.#connection.setLocalDescription(offer);
449
469
 
450
470
  this._sendMessage({'sdp': this.#connection.localDescription});
471
+ }
472
+ } catch (e) {
473
+ console.log(e);
474
+ }
475
+ }
451
476
 
452
- this.#connection.onnegotiationneeded = async () => {
453
- const offer = await this.#connection.createOffer();
454
- await this.#connection.setLocalDescription(offer);
477
+ _handleMessage(peerId, message) {
478
+ debug(`incoming message from ${peerId}`);
455
479
 
456
- this._sendMessage({'sdp': this.#connection.localDescription});
457
- };
458
- }
459
- // }
480
+ message = JSON.parse(new TextDecoder().decode(message.data));
481
+ // allow sharding (multiple peers share data)
482
+ pubsub.publish('peernet:shard', message);
483
+ if (!messageQue[message.id]) messageQue[message.id] = [];
460
484
 
461
- } catch (e) {
462
- console.log(e);
485
+ if (message.size > messageQue[message.id].length || message.size === messageQue[message.id].length) {
486
+ for (const value of Object.values(message.chunk)) {
487
+ messageQue[message.id].push(value);
488
+ }
463
489
  }
490
+
491
+ if (message.size === messageQue[message.id].length) {
492
+ pubsub.publish('peer:data', {id: message.id, data: new Uint8Array(Object.values(messageQue[message.id]))});
493
+ delete messageQue[message.id];
494
+ }
495
+ this.bw.down += message.byteLength || message.length;
464
496
  }
465
497
 
466
498
  _sendMessage(message) {
@@ -624,7 +656,8 @@ class Client {
624
656
  debug(`star ${id} left`);
625
657
  }
626
658
 
627
- peerLeft(id) {
659
+ peerLeft(peer) {
660
+ const id = peer.peerId || peer;
628
661
  if (this.#connections[id]) {
629
662
  this.#connections[id].close();
630
663
  delete this.#connections[id];
@@ -632,7 +665,8 @@ class Client {
632
665
  debug(`peer ${id} left`);
633
666
  }
634
667
 
635
- peerJoined(id, signal) {
668
+ peerJoined(peer, signal) {
669
+ const id = peer.peerId || peer;
636
670
  if (this.#connections[id]) {
637
671
  if (this.#connections[id].connected) this.#connections[id].close();
638
672
  delete this.#connections[id];
@@ -1625,6 +1659,18 @@ class MessageHandler {
1625
1659
  }
1626
1660
  }
1627
1661
 
1662
+ const dataHandler = async message => {
1663
+ if (!message) return
1664
+
1665
+ const {data, id} = message;
1666
+
1667
+ message = protoFor(data);
1668
+ const proto = protoFor(message.decoded.data);
1669
+ const from = message.decoded.from;
1670
+
1671
+ peernet._protoHandler({id, proto}, peernet.client.connections[from], from);
1672
+ };
1673
+
1628
1674
  const encapsulatedError = () => {
1629
1675
  return new Error('Nodes/Data should be send encapsulated by peernet-message')
1630
1676
  };
@@ -1803,7 +1849,7 @@ class Peernet {
1803
1849
  protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
1804
1850
  });
1805
1851
  } else {
1806
- const http = await Promise.resolve().then(function () { return require('./http-7f0fcdaf.js'); });
1852
+ const http = await Promise.resolve().then(function () { return require('./http-4e34d4a5.js'); });
1807
1853
  if (environment !== 'browser') http.default(options);
1808
1854
  }
1809
1855
 
@@ -1842,19 +1888,11 @@ class Peernet {
1842
1888
  // })
1843
1889
  });
1844
1890
 
1845
- pubsub.subscribe('peer:data', async message => {
1846
- if (!message.data) return
1847
- const {id, data} = JSON.parse(new TextDecoder().decode(message.data));
1848
- const uint8Array = new Uint8Array(Object.keys(data).length);
1849
- for (var i = 0; i < Object.keys(data).length; i++) {
1850
- uint8Array[i] = data[i];
1851
- }
1852
- message = new peernetMessage(uint8Array);
1853
- const proto = protoFor(message.decoded.data);
1854
-
1855
- const from = message.decoded.from;
1856
- this._protoHandler({id, proto}, this.client.connections[from], from);
1857
- });
1891
+ /**
1892
+ * converts data -> message -> proto
1893
+ * @see DataHandler
1894
+ */
1895
+ pubsub.subscribe('peer:data', dataHandler);
1858
1896
 
1859
1897
  /**
1860
1898
  * @access public
@@ -1873,11 +1911,9 @@ class Peernet {
1873
1911
 
1874
1912
  sendMessage(peer, id, data) {
1875
1913
  if (peer.readyState === 'open') {
1876
- peer.send(new TextEncoder().encode(JSON.stringify({id, data})));
1914
+ peer.send(data, id);
1877
1915
  this.bw.up += data.length;
1878
- } else if (peer.readyState === 'closed') {
1879
- this.removePeer(peer);
1880
- }
1916
+ } else if (peer.readyState === 'closed') ;
1881
1917
 
1882
1918
  }
1883
1919
 
@@ -241,6 +241,8 @@ const socketRequestClient = (url, protocols = 'echo-protocol', options = { retry
241
241
  });
242
242
  };
243
243
 
244
+ const messageQue = {};
245
+
244
246
  class Peer {
245
247
  #connection
246
248
  #ready = false
@@ -259,6 +261,7 @@ class Peer {
259
261
  #iceCompleteTimer
260
262
  #channel
261
263
  #peerId
264
+ #chunkSize = 16384
262
265
 
263
266
  get connection() {
264
267
  return this.#connection
@@ -310,29 +313,51 @@ class Peer {
310
313
  return this._socketClient
311
314
  }
312
315
 
313
- send(message) {
314
- switch (this.channel?.readyState) {
315
- case 'open':
316
- this.bw.up += message.length || message.byteLength;
317
- this.channel.send(message);
318
- break;
319
- case 'closed':
320
- case 'closing':
321
- debug('channel already closed, this usually means a bad implementation, try checking the readyState or check if the peer is connected before sending');
322
- break;
323
- case undefined:
324
- debug(`trying to send before a channel is created`);
325
- break;
326
- }
316
+ splitMessage(message) {
317
+ const chunks = [];
318
+ const size = message.byteLength || message.length;
319
+ let offset = 0;
320
+ return new Promise((resolve, reject) => {
321
+ const splitMessage = () => {
322
+ const chunk = message.slice(offset, offset + this.#chunkSize > size ? size : offset + this.#chunkSize);
323
+ offset += this.#chunkSize;
324
+ chunks.push(chunk);
325
+ if (offset < size) return splitMessage()
326
+ else resolve({chunks, size});
327
+ };
328
+
329
+ splitMessage();
330
+ })
331
+ }
332
+
333
+ async send(message, id) {
334
+ const { chunks, size } = await this.splitMessage(message);
335
+ let offset = 0;
336
+ for (const chunk of chunks) {
337
+ const start = offset;
338
+ const end = offset + chunk.length;
339
+ const message = new TextEncoder().encode(JSON.stringify({ size, id, chunk, start, end }));
340
+ switch (this.channel?.readyState) {
341
+ case 'open':
342
+ this.bw.up += message.length || message.byteLength;
343
+ this.channel.send(message);
344
+ break;
345
+ case 'closed':
346
+ case 'closing':
347
+ debug('channel already closed, this usually means a bad implementation, try checking the readyState or check if the peer is connected before sending');
348
+ break;
349
+ case undefined:
350
+ debug(`trying to send before a channel is created`);
351
+ break;
352
+ }
353
+ }
327
354
  }
328
355
 
329
356
  request(data) {
330
357
  return new Promise((resolve, reject) => {
331
358
  const id = Math.random().toString(36).slice(-12);
332
- // TODO: get rid of JSON
333
- data = new TextEncoder().encode(JSON.stringify({id, data}));
359
+
334
360
  const _onData = message => {
335
- message = JSON.parse(new TextDecoder().decode(message.data));
336
361
  if (message.id === id) {
337
362
  resolve(message.data);
338
363
  pubsub.unsubscribe(`peer:data`, _onData);
@@ -346,8 +371,8 @@ class Peer {
346
371
  // pubsub.unsubscribe(`peer:data-request-${id}`, _onData)
347
372
  // }, 5000);
348
373
 
349
- this.send(data);
350
- });
374
+ this.send(data, id);
375
+ })
351
376
  }
352
377
 
353
378
  async #init() {
@@ -377,10 +402,7 @@ class Peer {
377
402
  message.channel.onclose = () => this.close.bind(this);
378
403
 
379
404
  message.channel.onmessage = (message) => {
380
- pubsub.publish('peer:data', message);
381
- debug(`incoming message from ${this.id}`);
382
- debug(message);
383
- this.bw.down += message.length || message.byteLength;
405
+ this._handleMessage(this.id, message);
384
406
  };
385
407
  this.channel = message.channel;
386
408
  };
@@ -395,28 +417,38 @@ class Peer {
395
417
  this.channel.onclose = () => this.close.bind(this);
396
418
 
397
419
  this.channel.onmessage = (message) => {
398
- pubsub.publish('peer:data', message);
399
- debug(`incoming message from ${this.peerId}`);
400
- this.bw.down += message.length || message.byteLength;
420
+ this._handleMessage(this.peerId, message);
401
421
  };
402
422
 
403
423
  const offer = await this.#connection.createOffer();
404
424
  await this.#connection.setLocalDescription(offer);
405
425
 
406
426
  this._sendMessage({'sdp': this.#connection.localDescription});
427
+ }
428
+ } catch (e) {
429
+ console.log(e);
430
+ }
431
+ }
407
432
 
408
- this.#connection.onnegotiationneeded = async () => {
409
- const offer = await this.#connection.createOffer();
410
- await this.#connection.setLocalDescription(offer);
433
+ _handleMessage(peerId, message) {
434
+ debug(`incoming message from ${peerId}`);
411
435
 
412
- this._sendMessage({'sdp': this.#connection.localDescription});
413
- };
414
- }
415
- // }
436
+ message = JSON.parse(new TextDecoder().decode(message.data));
437
+ // allow sharding (multiple peers share data)
438
+ pubsub.publish('peernet:shard', message);
439
+ if (!messageQue[message.id]) messageQue[message.id] = [];
416
440
 
417
- } catch (e) {
418
- console.log(e);
441
+ if (message.size > messageQue[message.id].length || message.size === messageQue[message.id].length) {
442
+ for (const value of Object.values(message.chunk)) {
443
+ messageQue[message.id].push(value);
444
+ }
419
445
  }
446
+
447
+ if (message.size === messageQue[message.id].length) {
448
+ pubsub.publish('peer:data', {id: message.id, data: new Uint8Array(Object.values(messageQue[message.id]))});
449
+ delete messageQue[message.id];
450
+ }
451
+ this.bw.down += message.byteLength || message.length;
420
452
  }
421
453
 
422
454
  _sendMessage(message) {
@@ -580,7 +612,8 @@ class Client {
580
612
  debug(`star ${id} left`);
581
613
  }
582
614
 
583
- peerLeft(id) {
615
+ peerLeft(peer) {
616
+ const id = peer.peerId || peer;
584
617
  if (this.#connections[id]) {
585
618
  this.#connections[id].close();
586
619
  delete this.#connections[id];
@@ -588,7 +621,8 @@ class Client {
588
621
  debug(`peer ${id} left`);
589
622
  }
590
623
 
591
- peerJoined(id, signal) {
624
+ peerJoined(peer, signal) {
625
+ const id = peer.peerId || peer;
592
626
  if (this.#connections[id]) {
593
627
  if (this.#connections[id].connected) this.#connections[id].close();
594
628
  delete this.#connections[id];
@@ -2221,6 +2255,18 @@ class MessageHandler {
2221
2255
  }
2222
2256
  }
2223
2257
 
2258
+ const dataHandler = async message => {
2259
+ if (!message) return
2260
+
2261
+ const {data, id} = message;
2262
+
2263
+ message = protoFor(data);
2264
+ const proto = protoFor(message.decoded.data);
2265
+ const from = message.decoded.from;
2266
+
2267
+ peernet._protoHandler({id, proto}, peernet.client.connections[from], from);
2268
+ };
2269
+
2224
2270
  const encapsulatedError = () => {
2225
2271
  return new Error('Nodes/Data should be send encapsulated by peernet-message')
2226
2272
  };
@@ -2430,19 +2476,11 @@ class Peernet {
2430
2476
  // })
2431
2477
  });
2432
2478
 
2433
- pubsub.subscribe('peer:data', async message => {
2434
- if (!message.data) return
2435
- const {id, data} = JSON.parse(new TextDecoder().decode(message.data));
2436
- const uint8Array = new Uint8Array(Object.keys(data).length);
2437
- for (var i = 0; i < Object.keys(data).length; i++) {
2438
- uint8Array[i] = data[i];
2439
- }
2440
- message = new PeernetMessage(uint8Array);
2441
- const proto = protoFor(message.decoded.data);
2442
-
2443
- const from = message.decoded.from;
2444
- this._protoHandler({id, proto}, this.client.connections[from], from);
2445
- });
2479
+ /**
2480
+ * converts data -> message -> proto
2481
+ * @see DataHandler
2482
+ */
2483
+ pubsub.subscribe('peer:data', dataHandler);
2446
2484
 
2447
2485
  /**
2448
2486
  * @access public
@@ -2461,11 +2499,9 @@ class Peernet {
2461
2499
 
2462
2500
  sendMessage(peer, id, data) {
2463
2501
  if (peer.readyState === 'open') {
2464
- peer.send(new TextEncoder().encode(JSON.stringify({id, data})));
2502
+ peer.send(data, id);
2465
2503
  this.bw.up += data.length;
2466
- } else if (peer.readyState === 'closed') {
2467
- this.removePeer(peer);
2468
- }
2504
+ } else if (peer.readyState === 'closed') ;
2469
2505
 
2470
2506
  }
2471
2507
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.11.19",
3
+ "version": "0.11.22",
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.17",
28
+ "@leofcoin/peernet-swarm": "^0.2.0",
29
29
  "@leofcoin/storage": "^2.3.0",
30
30
  "@vandeurenglenn/base32": "^1.1.0",
31
31
  "@vandeurenglenn/base58": "^1.1.0",
@@ -0,0 +1,15 @@
1
+ import { protoFor } from './../utils/utils.js'
2
+
3
+ const dataHandler = async message => {
4
+ if (!message) return
5
+
6
+ const {data, id} = message
7
+
8
+ message = protoFor(data)
9
+ const proto = protoFor(message.decoded.data)
10
+ const from = message.decoded.from
11
+
12
+ peernet._protoHandler({id, proto}, peernet.client.connections[from], from)
13
+ }
14
+
15
+ export default dataHandler
package/src/peernet.js CHANGED
@@ -20,6 +20,7 @@ import codecs from './codec/codecs'
20
20
  import { protoFor, target } from './utils/utils.js'
21
21
  import generateAccount from './../node_modules/@leofcoin/generate-account/dist/module/generate-account'
22
22
  import MessageHandler from './handlers/message.js'
23
+ import dataHandler from './handlers/data.js'
23
24
  import { encapsulatedError, dhtError,
24
25
  nothingFoundError } from './errors/errors.js'
25
26
 
@@ -220,19 +221,11 @@ export default class Peernet {
220
221
  // })
221
222
  })
222
223
 
223
- pubsub.subscribe('peer:data', async message => {
224
- if (!message.data) return
225
- const {id, data} = JSON.parse(new TextDecoder().decode(message.data))
226
- const uint8Array = new Uint8Array(Object.keys(data).length)
227
- for (var i = 0; i < Object.keys(data).length; i++) {
228
- uint8Array[i] = data[i]
229
- }
230
- message = new PeernetMessage(uint8Array)
231
- const proto = protoFor(message.decoded.data)
232
-
233
- const from = message.decoded.from
234
- this._protoHandler({id, proto}, this.client.connections[from], from)
235
- })
224
+ /**
225
+ * converts data -> message -> proto
226
+ * @see DataHandler
227
+ */
228
+ pubsub.subscribe('peer:data', dataHandler)
236
229
 
237
230
  /**
238
231
  * @access public
@@ -251,10 +244,10 @@ export default class Peernet {
251
244
 
252
245
  sendMessage(peer, id, data) {
253
246
  if (peer.readyState === 'open') {
254
- peer.send(new TextEncoder().encode(JSON.stringify({id, data})))
247
+ peer.send(data, id)
255
248
  this.bw.up += data.length
256
249
  } else if (peer.readyState === 'closed') {
257
- this.removePeer(peer)
250
+ // this.removePeer(peer)
258
251
  }
259
252
 
260
253
  }