@leofcoin/peernet 0.11.22 → 0.11.25

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,14 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var protons = require('protons');
4
- var codecFormatInterface = require('./codec-format-interface.js');
5
- require('@vandeurenglenn/base32');
6
- require('@vandeurenglenn/base58');
7
- require('@vandeurenglenn/is-hex');
8
- require('./codec-45796010.js');
9
- require('varint');
10
- require('./hash.js');
11
- require('keccak');
4
+ var codecFormatInterface = require('@leofcoin/codec-format-interface');
12
5
 
13
6
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
14
7
 
@@ -22,7 +15,7 @@ message PeernetDHTMessageResponse {
22
15
  }
23
16
  `;
24
17
 
25
- class DHTMessageResponse extends codecFormatInterface {
18
+ class DHTMessageResponse extends codecFormatInterface.FormatInterface {
26
19
  get keys() {
27
20
  return ['hash', 'has']
28
21
  }
@@ -1,14 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var protons = require('protons');
4
- var codecFormatInterface = require('./codec-format-interface.js');
5
- require('@vandeurenglenn/base32');
6
- require('@vandeurenglenn/base58');
7
- require('@vandeurenglenn/is-hex');
8
- require('./codec-45796010.js');
9
- require('varint');
10
- require('./hash.js');
11
- require('keccak');
4
+ var codecFormatInterface = require('@leofcoin/codec-format-interface');
12
5
 
13
6
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
14
7
 
@@ -30,7 +23,7 @@ message PeernetDHTMessage {
30
23
  message = new DHTMessage('hashmvbs124xcfd...', 'block')
31
24
  `
32
25
  */
33
- class DHTMessage extends codecFormatInterface {
26
+ class DHTMessage extends codecFormatInterface.FormatInterface {
34
27
  /**
35
28
  *
36
29
  */
@@ -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.21";
13
+ var version = "0.11.24";
14
14
 
15
15
  var api$1 = {
16
16
  version: ({send}) => send({client: '@peernet/api/http', version}),
@@ -1,14 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var protons = require('protons');
4
- var codecFormatInterface = require('./codec-format-interface.js');
5
- require('@vandeurenglenn/base32');
6
- require('@vandeurenglenn/base58');
7
- require('@vandeurenglenn/is-hex');
8
- require('./codec-45796010.js');
9
- require('varint');
10
- require('./hash.js');
11
- require('keccak');
4
+ var codecFormatInterface = require('@leofcoin/codec-format-interface');
12
5
 
13
6
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
14
7
 
@@ -24,7 +17,7 @@ message PeernetMessage {
24
17
  optional string id = 5;
25
18
  }`;
26
19
 
27
- class PeernetMessage extends codecFormatInterface {
20
+ class PeernetMessage extends codecFormatInterface.FormatInterface {
28
21
  get keys() {
29
22
  return ['data', 'signature', 'from', 'to', 'id']
30
23
  }
@@ -1,17 +1,16 @@
1
1
  'use strict';
2
2
 
3
3
  require('@vandeurenglenn/debug');
4
+ var pako = require('pako');
4
5
  var LeofcoinStorage = require('@leofcoin/storage');
5
6
  var peernetMessage = require('./peernet-message.js');
6
7
  var dht = require('./dht.js');
7
8
  var dhtResponse = require('./dht-response.js');
8
9
  var protons = require('protons');
9
- var codecFormatInterface = require('./codec-format-interface.js');
10
+ var codecFormatInterface = require('@leofcoin/codec-format-interface');
10
11
  var request = require('./request.js');
11
12
  var response = require('./response.js');
12
13
  var fetch = require('node-fetch');
13
- var codec = require('./codec-45796010.js');
14
- var hash = require('./hash.js');
15
14
  var MultiWallet$1 = require('@leofcoin/multi-wallet');
16
15
  var bs58check = require('bs58check');
17
16
  var bip32 = require('bip32');
@@ -21,9 +20,6 @@ var Mnemonic = require('@leofcoin/mnemonic');
21
20
  var MultiSignature = require('multi-signature');
22
21
  var varint = require('varint');
23
22
  var randombytes = require('randombytes');
24
- require('@vandeurenglenn/base32');
25
- require('@vandeurenglenn/base58');
26
- require('@vandeurenglenn/is-hex');
27
23
 
28
24
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
29
25
 
@@ -45,6 +41,7 @@ function _interopNamespace(e) {
45
41
  return Object.freeze(n);
46
42
  }
47
43
 
44
+ var pako__default = /*#__PURE__*/_interopDefaultLegacy(pako);
48
45
  var LeofcoinStorage__default = /*#__PURE__*/_interopDefaultLegacy(LeofcoinStorage);
49
46
  var protons__default = /*#__PURE__*/_interopDefaultLegacy(protons);
50
47
  var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch);
@@ -285,11 +282,8 @@ const socketRequestClient = (url, protocols = 'echo-protocol', options = { retry
285
282
  });
286
283
  };
287
284
 
288
- const messageQue = {};
289
-
290
285
  class Peer {
291
286
  #connection
292
- #ready = false
293
287
  #connecting = false
294
288
  #connected = false
295
289
  #channelReady = false
@@ -302,10 +296,14 @@ class Peer {
302
296
  #remoteStreams = []
303
297
  #pendingCandidates = []
304
298
  #senderMap = new Map()
299
+ #messageQue = []
300
+ #chunksQue = {}
305
301
  #iceCompleteTimer
306
302
  #channel
307
303
  #peerId
308
- #chunkSize = 16384
304
+ #chunkSize = 16 * 1024 // 16384
305
+ #queRunning = false
306
+ #MAX_BUFFERED_AMOUNT = 16 * 1024 * 1024
309
307
 
310
308
  get connection() {
311
309
  return this.#connection
@@ -359,6 +357,7 @@ class Peer {
359
357
 
360
358
  splitMessage(message) {
361
359
  const chunks = [];
360
+ message = pako__default["default"].deflate(message);
362
361
  const size = message.byteLength || message.length;
363
362
  let offset = 0;
364
363
  return new Promise((resolve, reject) => {
@@ -374,27 +373,53 @@ class Peer {
374
373
  })
375
374
  }
376
375
 
377
- async send(message, id) {
378
- const { chunks, size } = await this.splitMessage(message);
376
+ async #runQue() {
377
+ this.#queRunning = true;
378
+ if (this.#messageQue.length > 0 && this.channel.bufferedAmount + this.#messageQue[0]?.length < this.#MAX_BUFFERED_AMOUNT) {
379
+ const message = this.#messageQue.shift();
380
+
381
+ switch (this.channel?.readyState) {
382
+ case 'open':
383
+ await this.channel.send(message);
384
+ if (this.#messageQue.length > 0) return this.#runQue()
385
+ else this.#queRunning = false;
386
+ break;
387
+ case 'closed':
388
+ case 'closing':
389
+ this.#messageQue = [];
390
+ this.#queRunning = false;
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
+ this.#messageQue = [];
395
+ this.#queRunning = false;
396
+ debug(`trying to send before a channel is created`);
397
+ break;
398
+ }
399
+
400
+
401
+ } else {
402
+ return setTimeout(() => this.#runQue(), 50)
403
+ }
404
+ }
405
+
406
+ #trySend({ size, id, chunks }) {
379
407
  let offset = 0;
408
+
380
409
  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
- }
410
+ const start = offset;
411
+ const end = offset + chunk.length;
412
+
413
+ const message = new TextEncoder().encode(JSON.stringify({ size, id, chunk, start, end }));
414
+ this.#messageQue.push(message);
415
+ }
416
+
417
+ if (!this.queRunning) return this.#runQue()
418
+ }
419
+
420
+ async send(message, id) {
421
+ const { chunks, size } = await this.splitMessage(message);
422
+ return this.#trySend({ size, id, chunks })
398
423
  }
399
424
 
400
425
  request(data) {
@@ -480,17 +505,21 @@ class Peer {
480
505
  message = JSON.parse(new TextDecoder().decode(message.data));
481
506
  // allow sharding (multiple peers share data)
482
507
  pubsub.publish('peernet:shard', message);
483
- if (!messageQue[message.id]) messageQue[message.id] = [];
508
+ const { id } = message;
509
+
510
+ if (!this.#chunksQue[id]) this.#chunksQue[id] = [];
484
511
 
485
- if (message.size > messageQue[message.id].length || message.size === messageQue[message.id].length) {
512
+ if (message.size > this.#chunksQue[id].length || message.size === this.#chunksQue[id].length) {
486
513
  for (const value of Object.values(message.chunk)) {
487
- messageQue[message.id].push(value);
514
+ this.#chunksQue[id].push(value);
488
515
  }
489
516
  }
490
517
 
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];
518
+ if (message.size === this.#chunksQue[id].length) {
519
+ let data = new Uint8Array(Object.values(this.#chunksQue[id]));
520
+ delete this.#chunksQue[id];
521
+ data = pako__default["default"].inflate(data);
522
+ pubsub.publish('peer:data', { id, data });
494
523
  }
495
524
  this.bw.down += message.byteLength || message.length;
496
525
  }
@@ -736,7 +765,7 @@ message PeernetDataMessage {
736
765
  /**
737
766
  * @extends {CodecFormat}
738
767
  */
739
- class DataMessage extends codecFormatInterface {
768
+ class DataMessage extends codecFormatInterface.FormatInterface {
740
769
  get keys() {
741
770
  return ['hash', 'store']
742
771
  }
@@ -755,7 +784,7 @@ message PsMessage {
755
784
  required bytes topic = 2;
756
785
  }`;
757
786
 
758
- class PsMessage extends codecFormatInterface {
787
+ class PsMessage extends codecFormatInterface.FormatInterface {
759
788
  get keys() {
760
789
  return ['data', 'topic']
761
790
  }
@@ -773,7 +802,7 @@ message PeernetPeerMessage {
773
802
  }
774
803
  `;
775
804
 
776
- class PeerMessage extends codecFormatInterface {
805
+ class PeerMessage extends codecFormatInterface.FormatInterface {
777
806
  get keys() {
778
807
  return ['id']
779
808
  }
@@ -791,7 +820,7 @@ message PeernetPeerMessageResponse {
791
820
  }
792
821
  `;
793
822
 
794
- class PeerMessageResponse extends codecFormatInterface {
823
+ class PeerMessageResponse extends codecFormatInterface.FormatInterface {
795
824
  get keys() {
796
825
  return ['id']
797
826
  }
@@ -810,7 +839,7 @@ message PeernetDataMessageResponse {
810
839
  }
811
840
  `;
812
841
 
813
- class DataMessageResponse extends codecFormatInterface {
842
+ class DataMessageResponse extends codecFormatInterface.FormatInterface {
814
843
  get keys() {
815
844
  return ['hash', 'data']
816
845
  }
@@ -829,7 +858,7 @@ message ChatMessage {
829
858
  repeated string files = 4;
830
859
  }`;
831
860
 
832
- class ChatMessage extends codecFormatInterface {
861
+ class ChatMessage extends codecFormatInterface.FormatInterface {
833
862
  get keys() {
834
863
  return ['author', 'value', 'timestamp', 'files']
835
864
  }
@@ -842,10 +871,10 @@ class ChatMessage extends codecFormatInterface {
842
871
 
843
872
  const protoFor = (data) => {
844
873
  if (!Buffer.isBuffer(data)) data = Buffer.from(data);
845
- const codec$1 = new codec.PeernetCodec(data);
846
- if (!codec$1.name) throw new Error('proto not found')
847
- const Proto = globalThis.peernet.protos[codec$1.name];
848
- if (!Proto) throw (new Error(`No proto defined for ${codec$1.name}`))
874
+ const codec = new codecFormatInterface.Codec(data);
875
+ if (!codec.name) throw new Error('proto not found')
876
+ const Proto = globalThis.peernet.protos[codec.name];
877
+ if (!Proto) throw (new Error(`No proto defined for ${codec.name}`))
849
878
  return new Proto(data)
850
879
  };
851
880
 
@@ -1627,7 +1656,7 @@ class MessageHandler {
1627
1656
  * @return signature
1628
1657
  */
1629
1658
  async hashAndSignMessage(message) {
1630
- const hasher = new hash(message, {name: 'peernet-message'});
1659
+ const hasher = new codecFormatInterface.CodecHash(message, {name: 'peernet-message'});
1631
1660
  let identity = await walletStore.get('identity');
1632
1661
  identity = JSON.parse(new TextDecoder().decode(identity));
1633
1662
  const wallet = new MultiWallet(this.network);
@@ -1836,7 +1865,7 @@ class Peernet {
1836
1865
  };
1837
1866
 
1838
1867
  this.protos = globalThis.peernet.protos;
1839
- this.codecs = codec.codecs;
1868
+ this.codecs = codecFormatInterface.codecs;
1840
1869
 
1841
1870
  this._messageHandler = new MessageHandler(this.network);
1842
1871
 
@@ -1849,7 +1878,7 @@ class Peernet {
1849
1878
  protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
1850
1879
  });
1851
1880
  } else {
1852
- const http = await Promise.resolve().then(function () { return require('./http-4e34d4a5.js'); });
1881
+ const http = await Promise.resolve().then(function () { return require('./http-78686629.js'); });
1853
1882
  if (environment !== 'browser') http.default(options);
1854
1883
  }
1855
1884
 
@@ -2245,7 +2274,7 @@ class Peernet {
2245
2274
  }
2246
2275
 
2247
2276
  createHash(data, name) {
2248
- return new hash(data, {name})
2277
+ return new codecFormatInterface.CodecHash(data, {name})
2249
2278
  }
2250
2279
 
2251
2280
  /**
@@ -1,14 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var protons = require('protons');
4
- var codecFormatInterface = require('./codec-format-interface.js');
5
- require('@vandeurenglenn/base32');
6
- require('@vandeurenglenn/base58');
7
- require('@vandeurenglenn/is-hex');
8
- require('./codec-45796010.js');
9
- require('varint');
10
- require('./hash.js');
11
- require('keccak');
4
+ var codecFormatInterface = require('@leofcoin/codec-format-interface');
12
5
 
13
6
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
14
7
 
@@ -21,7 +14,7 @@ message PeernetRequestMessage {
21
14
  }
22
15
  `;
23
16
 
24
- class RequestMessage extends codecFormatInterface {
17
+ class RequestMessage extends codecFormatInterface.FormatInterface {
25
18
  get keys() {
26
19
  return ['request']
27
20
  }
@@ -1,14 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var protons = require('protons');
4
- var codecFormatInterface = require('./codec-format-interface.js');
5
- require('@vandeurenglenn/base32');
6
- require('@vandeurenglenn/base58');
7
- require('@vandeurenglenn/is-hex');
8
- require('./codec-45796010.js');
9
- require('varint');
10
- require('./hash.js');
11
- require('keccak');
4
+ var codecFormatInterface = require('@leofcoin/codec-format-interface');
12
5
 
13
6
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
14
7
 
@@ -21,7 +14,7 @@ message PeernetResponseMessage {
21
14
  }
22
15
  `;
23
16
 
24
- class ResponseMessage extends codecFormatInterface {
17
+ class ResponseMessage extends codecFormatInterface.FormatInterface {
25
18
  get keys() {
26
19
  return ['response']
27
20
  }