@leofcoin/peernet 0.11.18 → 0.11.21

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
@@ -322,7 +325,7 @@ class Peer {
322
325
  * @params {Object} options
323
326
  * @params {string} options.channelName - this peerid : otherpeer id
324
327
  */
325
- constructor(options = {}) {
328
+ constructor(options = {}) {
326
329
  this._in = this._in.bind(this);
327
330
  this.offerOptions = options.offerOptions;
328
331
  this.initiator = options.initiator;
@@ -356,29 +359,51 @@ constructor(options = {}) {
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 += 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 @@ constructor(options = {}) {
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() {
@@ -403,6 +428,7 @@ constructor(options = {}) {
403
428
  }];
404
429
 
405
430
  this.#connection = new wrtc.RTCPeerConnection();
431
+
406
432
  this.#connection.onicecandidate = ({ candidate }) => {
407
433
  if (candidate) {
408
434
  this.address = candidate.address;
@@ -422,10 +448,7 @@ constructor(options = {}) {
422
448
  message.channel.onclose = () => this.close.bind(this);
423
449
 
424
450
  message.channel.onmessage = (message) => {
425
- pubsub.publish('peer:data', message);
426
- debug(`incoming message from ${this.id}`);
427
- debug(message);
428
- this.bw.down += message.length || message.byteLength;
451
+ this._handleMessage(this.id, message);
429
452
  };
430
453
  this.channel = message.channel;
431
454
  };
@@ -440,23 +463,41 @@ constructor(options = {}) {
440
463
  this.channel.onclose = () => this.close.bind(this);
441
464
 
442
465
  this.channel.onmessage = (message) => {
443
- pubsub.publish('peer:data', message);
444
- debug(`incoming message from ${this.peerId}`);
445
- this.bw.down += message.length || message.byteLength;
466
+ this._handleMessage(this.peerId, message);
446
467
  };
447
468
 
448
469
  const offer = await this.#connection.createOffer();
449
470
  await this.#connection.setLocalDescription(offer);
450
471
 
451
472
  this._sendMessage({'sdp': this.#connection.localDescription});
452
- }
453
- // }
454
-
473
+ }
455
474
  } catch (e) {
456
475
  console.log(e);
457
476
  }
458
477
  }
459
478
 
479
+ _handleMessage(peerId, message) {
480
+ debug(`incoming message from ${peerId}`);
481
+
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] = [];
486
+
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
+ }
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;
498
+
499
+ }
500
+
460
501
  _sendMessage(message) {
461
502
  this.socketClient.send({url: 'signal', params: {
462
503
  to: this.to,
@@ -618,7 +659,8 @@ class Client {
618
659
  debug(`star ${id} left`);
619
660
  }
620
661
 
621
- peerLeft(id) {
662
+ peerLeft(peer) {
663
+ const id = peer.peerId || peer;
622
664
  if (this.#connections[id]) {
623
665
  this.#connections[id].close();
624
666
  delete this.#connections[id];
@@ -626,7 +668,8 @@ class Client {
626
668
  debug(`peer ${id} left`);
627
669
  }
628
670
 
629
- peerJoined(id, signal) {
671
+ peerJoined(peer, signal) {
672
+ const id = peer.peerId || peer;
630
673
  if (this.#connections[id]) {
631
674
  if (this.#connections[id].connected) this.#connections[id].close();
632
675
  delete this.#connections[id];
@@ -2259,6 +2302,18 @@ class MessageHandler {
2259
2302
  }
2260
2303
  }
2261
2304
 
2305
+ const dataHandler = async message => {
2306
+ if (!message) return
2307
+
2308
+ const {data, id} = message;
2309
+
2310
+ message = protoFor(data);
2311
+ const proto = protoFor(message.decoded.data);
2312
+ const from = message.decoded.from;
2313
+
2314
+ peernet._protoHandler({id, proto}, peernet.client.connections[from], from);
2315
+ };
2316
+
2262
2317
  const encapsulatedError = () => {
2263
2318
  return new Error('Nodes/Data should be send encapsulated by peernet-message')
2264
2319
  };
@@ -2468,19 +2523,11 @@ class Peernet {
2468
2523
  // })
2469
2524
  });
2470
2525
 
2471
- pubsub.subscribe('peer:data', async message => {
2472
- if (!message.data) return
2473
- const {id, data} = JSON.parse(new TextDecoder().decode(message.data));
2474
- const uint8Array = new Uint8Array(Object.keys(data).length);
2475
- for (var i = 0; i < Object.keys(data).length; i++) {
2476
- uint8Array[i] = data[i];
2477
- }
2478
- message = new PeernetMessage(uint8Array);
2479
- const proto = protoFor(message.decoded.data);
2480
-
2481
- const from = message.decoded.from;
2482
- this._protoHandler({id, proto}, this.client.connections[from], from);
2483
- });
2526
+ /**
2527
+ * converts data -> message -> proto
2528
+ * @see DataHandler
2529
+ */
2530
+ pubsub.subscribe('peer:data', dataHandler);
2484
2531
 
2485
2532
  /**
2486
2533
  * @access public
@@ -2499,11 +2546,9 @@ class Peernet {
2499
2546
 
2500
2547
  sendMessage(peer, id, data) {
2501
2548
  if (peer.readyState === 'open') {
2502
- peer.send(new TextEncoder().encode(JSON.stringify({id, data})));
2549
+ peer.send(data, id);
2503
2550
  this.bw.up += data.length;
2504
- } else if (peer.readyState === 'closed') {
2505
- this.removePeer(peer);
2506
- }
2551
+ } else if (peer.readyState === 'closed') ;
2507
2552
 
2508
2553
  }
2509
2554
 
@@ -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.17";
13
+ var version = "0.11.19";
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
@@ -320,7 +323,7 @@ class Peer {
320
323
  * @params {Object} options
321
324
  * @params {string} options.channelName - this peerid : otherpeer id
322
325
  */
323
- constructor(options = {}) {
326
+ constructor(options = {}) {
324
327
  this._in = this._in.bind(this);
325
328
  this.offerOptions = options.offerOptions;
326
329
  this.initiator = options.initiator;
@@ -354,29 +357,51 @@ constructor(options = {}) {
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 += 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 @@ constructor(options = {}) {
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() {
@@ -401,6 +426,7 @@ constructor(options = {}) {
401
426
  }];
402
427
 
403
428
  this.#connection = new wrtc.RTCPeerConnection();
429
+
404
430
  this.#connection.onicecandidate = ({ candidate }) => {
405
431
  if (candidate) {
406
432
  this.address = candidate.address;
@@ -420,10 +446,7 @@ constructor(options = {}) {
420
446
  message.channel.onclose = () => this.close.bind(this);
421
447
 
422
448
  message.channel.onmessage = (message) => {
423
- pubsub.publish('peer:data', message);
424
- debug(`incoming message from ${this.id}`);
425
- debug(message);
426
- this.bw.down += message.length || message.byteLength;
449
+ this._handleMessage(this.id, message);
427
450
  };
428
451
  this.channel = message.channel;
429
452
  };
@@ -438,23 +461,41 @@ constructor(options = {}) {
438
461
  this.channel.onclose = () => this.close.bind(this);
439
462
 
440
463
  this.channel.onmessage = (message) => {
441
- pubsub.publish('peer:data', message);
442
- debug(`incoming message from ${this.peerId}`);
443
- this.bw.down += message.length || message.byteLength;
464
+ this._handleMessage(this.peerId, message);
444
465
  };
445
466
 
446
467
  const offer = await this.#connection.createOffer();
447
468
  await this.#connection.setLocalDescription(offer);
448
469
 
449
470
  this._sendMessage({'sdp': this.#connection.localDescription});
450
- }
451
- // }
452
-
471
+ }
453
472
  } catch (e) {
454
473
  console.log(e);
455
474
  }
456
475
  }
457
476
 
477
+ _handleMessage(peerId, message) {
478
+ debug(`incoming message from ${peerId}`);
479
+
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] = [];
484
+
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
+ }
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;
496
+
497
+ }
498
+
458
499
  _sendMessage(message) {
459
500
  this.socketClient.send({url: 'signal', params: {
460
501
  to: this.to,
@@ -616,7 +657,8 @@ class Client {
616
657
  debug(`star ${id} left`);
617
658
  }
618
659
 
619
- peerLeft(id) {
660
+ peerLeft(peer) {
661
+ const id = peer.peerId || peer;
620
662
  if (this.#connections[id]) {
621
663
  this.#connections[id].close();
622
664
  delete this.#connections[id];
@@ -624,7 +666,8 @@ class Client {
624
666
  debug(`peer ${id} left`);
625
667
  }
626
668
 
627
- peerJoined(id, signal) {
669
+ peerJoined(peer, signal) {
670
+ const id = peer.peerId || peer;
628
671
  if (this.#connections[id]) {
629
672
  if (this.#connections[id].connected) this.#connections[id].close();
630
673
  delete this.#connections[id];
@@ -1617,6 +1660,18 @@ class MessageHandler {
1617
1660
  }
1618
1661
  }
1619
1662
 
1663
+ const dataHandler = async message => {
1664
+ if (!message) return
1665
+
1666
+ const {data, id} = message;
1667
+
1668
+ message = protoFor(data);
1669
+ const proto = protoFor(message.decoded.data);
1670
+ const from = message.decoded.from;
1671
+
1672
+ peernet._protoHandler({id, proto}, peernet.client.connections[from], from);
1673
+ };
1674
+
1620
1675
  const encapsulatedError = () => {
1621
1676
  return new Error('Nodes/Data should be send encapsulated by peernet-message')
1622
1677
  };
@@ -1795,7 +1850,7 @@ class Peernet {
1795
1850
  protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
1796
1851
  });
1797
1852
  } else {
1798
- const http = await Promise.resolve().then(function () { return require('./http-6c848a1e.js'); });
1853
+ const http = await Promise.resolve().then(function () { return require('./http-62c5b155.js'); });
1799
1854
  if (environment !== 'browser') http.default(options);
1800
1855
  }
1801
1856
 
@@ -1834,19 +1889,11 @@ class Peernet {
1834
1889
  // })
1835
1890
  });
1836
1891
 
1837
- pubsub.subscribe('peer:data', async message => {
1838
- if (!message.data) return
1839
- const {id, data} = JSON.parse(new TextDecoder().decode(message.data));
1840
- const uint8Array = new Uint8Array(Object.keys(data).length);
1841
- for (var i = 0; i < Object.keys(data).length; i++) {
1842
- uint8Array[i] = data[i];
1843
- }
1844
- message = new peernetMessage(uint8Array);
1845
- const proto = protoFor(message.decoded.data);
1846
-
1847
- const from = message.decoded.from;
1848
- this._protoHandler({id, proto}, this.client.connections[from], from);
1849
- });
1892
+ /**
1893
+ * converts data -> message -> proto
1894
+ * @see DataHandler
1895
+ */
1896
+ pubsub.subscribe('peer:data', dataHandler);
1850
1897
 
1851
1898
  /**
1852
1899
  * @access public
@@ -1865,11 +1912,9 @@ class Peernet {
1865
1912
 
1866
1913
  sendMessage(peer, id, data) {
1867
1914
  if (peer.readyState === 'open') {
1868
- peer.send(new TextEncoder().encode(JSON.stringify({id, data})));
1915
+ peer.send(data, id);
1869
1916
  this.bw.up += data.length;
1870
- } else if (peer.readyState === 'closed') {
1871
- this.removePeer(peer);
1872
- }
1917
+ } else if (peer.readyState === 'closed') ;
1873
1918
 
1874
1919
  }
1875
1920
 
@@ -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
@@ -276,7 +279,7 @@ class Peer {
276
279
  * @params {Object} options
277
280
  * @params {string} options.channelName - this peerid : otherpeer id
278
281
  */
279
- constructor(options = {}) {
282
+ constructor(options = {}) {
280
283
  this._in = this._in.bind(this);
281
284
  this.offerOptions = options.offerOptions;
282
285
  this.initiator = options.initiator;
@@ -310,29 +313,51 @@ constructor(options = {}) {
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 += 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 @@ constructor(options = {}) {
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() {
@@ -357,6 +382,7 @@ constructor(options = {}) {
357
382
  }];
358
383
 
359
384
  this.#connection = new wrtc.RTCPeerConnection();
385
+
360
386
  this.#connection.onicecandidate = ({ candidate }) => {
361
387
  if (candidate) {
362
388
  this.address = candidate.address;
@@ -376,10 +402,7 @@ constructor(options = {}) {
376
402
  message.channel.onclose = () => this.close.bind(this);
377
403
 
378
404
  message.channel.onmessage = (message) => {
379
- pubsub.publish('peer:data', message);
380
- debug(`incoming message from ${this.id}`);
381
- debug(message);
382
- this.bw.down += message.length || message.byteLength;
405
+ this._handleMessage(this.id, message);
383
406
  };
384
407
  this.channel = message.channel;
385
408
  };
@@ -394,23 +417,41 @@ constructor(options = {}) {
394
417
  this.channel.onclose = () => this.close.bind(this);
395
418
 
396
419
  this.channel.onmessage = (message) => {
397
- pubsub.publish('peer:data', message);
398
- debug(`incoming message from ${this.peerId}`);
399
- this.bw.down += message.length || message.byteLength;
420
+ this._handleMessage(this.peerId, message);
400
421
  };
401
422
 
402
423
  const offer = await this.#connection.createOffer();
403
424
  await this.#connection.setLocalDescription(offer);
404
425
 
405
426
  this._sendMessage({'sdp': this.#connection.localDescription});
406
- }
407
- // }
408
-
427
+ }
409
428
  } catch (e) {
410
429
  console.log(e);
411
430
  }
412
431
  }
413
432
 
433
+ _handleMessage(peerId, message) {
434
+ debug(`incoming message from ${peerId}`);
435
+
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] = [];
440
+
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
+ }
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;
452
+
453
+ }
454
+
414
455
  _sendMessage(message) {
415
456
  this.socketClient.send({url: 'signal', params: {
416
457
  to: this.to,
@@ -572,7 +613,8 @@ class Client {
572
613
  debug(`star ${id} left`);
573
614
  }
574
615
 
575
- peerLeft(id) {
616
+ peerLeft(peer) {
617
+ const id = peer.peerId || peer;
576
618
  if (this.#connections[id]) {
577
619
  this.#connections[id].close();
578
620
  delete this.#connections[id];
@@ -580,7 +622,8 @@ class Client {
580
622
  debug(`peer ${id} left`);
581
623
  }
582
624
 
583
- peerJoined(id, signal) {
625
+ peerJoined(peer, signal) {
626
+ const id = peer.peerId || peer;
584
627
  if (this.#connections[id]) {
585
628
  if (this.#connections[id].connected) this.#connections[id].close();
586
629
  delete this.#connections[id];
@@ -2213,6 +2256,18 @@ class MessageHandler {
2213
2256
  }
2214
2257
  }
2215
2258
 
2259
+ const dataHandler = async message => {
2260
+ if (!message) return
2261
+
2262
+ const {data, id} = message;
2263
+
2264
+ message = protoFor(data);
2265
+ const proto = protoFor(message.decoded.data);
2266
+ const from = message.decoded.from;
2267
+
2268
+ peernet._protoHandler({id, proto}, peernet.client.connections[from], from);
2269
+ };
2270
+
2216
2271
  const encapsulatedError = () => {
2217
2272
  return new Error('Nodes/Data should be send encapsulated by peernet-message')
2218
2273
  };
@@ -2422,19 +2477,11 @@ class Peernet {
2422
2477
  // })
2423
2478
  });
2424
2479
 
2425
- pubsub.subscribe('peer:data', async message => {
2426
- if (!message.data) return
2427
- const {id, data} = JSON.parse(new TextDecoder().decode(message.data));
2428
- const uint8Array = new Uint8Array(Object.keys(data).length);
2429
- for (var i = 0; i < Object.keys(data).length; i++) {
2430
- uint8Array[i] = data[i];
2431
- }
2432
- message = new PeernetMessage(uint8Array);
2433
- const proto = protoFor(message.decoded.data);
2434
-
2435
- const from = message.decoded.from;
2436
- this._protoHandler({id, proto}, this.client.connections[from], from);
2437
- });
2480
+ /**
2481
+ * converts data -> message -> proto
2482
+ * @see DataHandler
2483
+ */
2484
+ pubsub.subscribe('peer:data', dataHandler);
2438
2485
 
2439
2486
  /**
2440
2487
  * @access public
@@ -2453,11 +2500,9 @@ class Peernet {
2453
2500
 
2454
2501
  sendMessage(peer, id, data) {
2455
2502
  if (peer.readyState === 'open') {
2456
- peer.send(new TextEncoder().encode(JSON.stringify({id, data})));
2503
+ peer.send(data, id);
2457
2504
  this.bw.up += data.length;
2458
- } else if (peer.readyState === 'closed') {
2459
- this.removePeer(peer);
2460
- }
2505
+ } else if (peer.readyState === 'closed') ;
2461
2506
 
2462
2507
  }
2463
2508
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.11.18",
3
+ "version": "0.11.21",
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.1.21",
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
  }