@leofcoin/peernet 0.14.3 → 0.14.4

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.
@@ -357,14 +357,12 @@ function _interopNamespace(e) {
357
357
 
358
358
  var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
359
359
 
360
- const protoFor = (data) => {
361
- console.log(data);
362
- if (!Buffer.isBuffer(data)) data = Buffer.from(data);
363
- const codec = new codecFormatInterface.Codec(data);
360
+ const protoFor = (message) => {
361
+ const codec = new codecFormatInterface.Codec(message);
364
362
  if (!codec.name) throw new Error('proto not found')
365
363
  const Proto = globalThis.peernet.protos[codec.name];
366
364
  if (!Proto) throw (new Error(`No proto defined for ${codec.name}`))
367
- return new Proto(data)
365
+ return new Proto(message)
368
366
  };
369
367
 
370
368
  /**
@@ -742,21 +740,12 @@ class MessageHandler {
742
740
  const dataHandler = async message => {
743
741
  if (!message) return
744
742
 
745
- const {data, id} = message;
746
- console.log({data});
747
- message = await protoFor(data);
748
- console.log(message.decoded.data);
749
- console.log({message});
750
- const proto = await protoFor(message.decoded.data);
751
- const from = message.decoded.from;
743
+ const {data, id, from} = message;
744
+ const proto = await protoFor(data);
752
745
 
753
746
  peernet._protoHandler({id, proto}, peernet.client.connections[from], from);
754
747
  };
755
748
 
756
- const encapsulatedError = () => {
757
- return new Error('Nodes/Data should be send encapsulated by peernet-message')
758
- };
759
-
760
749
  const dhtError = (proto) => {
761
750
  const text = `Received proto ${proto.name} expected peernet-dht-response`;
762
751
  return new Error(`Routing error: ${text}`)
@@ -1095,11 +1084,9 @@ class Peernet {
1095
1084
  const walk = async peer => {
1096
1085
  const node = await this.prepareMessage(data);
1097
1086
  let result = await peer.request(node.encoded);
1087
+ console.log({result});
1098
1088
  result = new Uint8Array(Object.values(result));
1099
- let proto = await protoFor(result);
1100
- if (proto.name !== 'peernet-message') throw encapsulatedError()
1101
- const from = proto.decoded.from;
1102
- proto = await protoFor(proto.decoded.data);
1089
+ const proto = await protoFor(result);
1103
1090
  if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
1104
1091
 
1105
1092
  // TODO: give ip and port (just used for location)
@@ -1113,7 +1100,7 @@ class Peernet {
1113
1100
  family: peer.connection.remoteFamily || peer.connection.localFamily,
1114
1101
  address: peer.connection.remoteAddress || peer.connection.localAddress,
1115
1102
  port: peer.connection.remotePort || peer.connection.localPort,
1116
- id: from,
1103
+ id: peerId,
1117
1104
  };
1118
1105
 
1119
1106
  if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash);
@@ -1208,9 +1195,9 @@ class Peernet {
1208
1195
  });
1209
1196
  if (closest[0]) data = await closest[0].request(node.encoded);
1210
1197
  }
1198
+ console.log({data});
1211
1199
  data = new Uint8Array(Object.values(data));
1212
- let proto = await protoFor(data);
1213
- proto = await protoFor(proto.decoded.data);
1200
+ proto = await protoFor(data);
1214
1201
  // TODO: store data automaticly or not
1215
1202
  return proto.decoded.data
1216
1203
 
@@ -28,14 +28,12 @@ function _interopNamespace(e) {
28
28
  var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
29
29
  var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch);
30
30
 
31
- const protoFor = (data) => {
32
- console.log(data);
33
- if (!Buffer.isBuffer(data)) data = Buffer.from(data);
34
- const codec = new codecFormatInterface.Codec(data);
31
+ const protoFor = (message) => {
32
+ const codec = new codecFormatInterface.Codec(message);
35
33
  if (!codec.name) throw new Error('proto not found')
36
34
  const Proto = globalThis.peernet.protos[codec.name];
37
35
  if (!Proto) throw (new Error(`No proto defined for ${codec.name}`))
38
- return new Proto(data)
36
+ return new Proto(message)
39
37
  };
40
38
 
41
39
  /**
@@ -413,21 +411,12 @@ class MessageHandler {
413
411
  const dataHandler = async message => {
414
412
  if (!message) return
415
413
 
416
- const {data, id} = message;
417
- console.log({data});
418
- message = await protoFor(data);
419
- console.log(message.decoded.data);
420
- console.log({message});
421
- const proto = await protoFor(message.decoded.data);
422
- const from = message.decoded.from;
414
+ const {data, id, from} = message;
415
+ const proto = await protoFor(data);
423
416
 
424
417
  peernet._protoHandler({id, proto}, peernet.client.connections[from], from);
425
418
  };
426
419
 
427
- const encapsulatedError = () => {
428
- return new Error('Nodes/Data should be send encapsulated by peernet-message')
429
- };
430
-
431
420
  const dhtError = (proto) => {
432
421
  const text = `Received proto ${proto.name} expected peernet-dht-response`;
433
422
  return new Error(`Routing error: ${text}`)
@@ -766,11 +755,9 @@ class Peernet {
766
755
  const walk = async peer => {
767
756
  const node = await this.prepareMessage(data);
768
757
  let result = await peer.request(node.encoded);
758
+ console.log({result});
769
759
  result = new Uint8Array(Object.values(result));
770
- let proto = await protoFor(result);
771
- if (proto.name !== 'peernet-message') throw encapsulatedError()
772
- const from = proto.decoded.from;
773
- proto = await protoFor(proto.decoded.data);
760
+ const proto = await protoFor(result);
774
761
  if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
775
762
 
776
763
  // TODO: give ip and port (just used for location)
@@ -784,7 +771,7 @@ class Peernet {
784
771
  family: peer.connection.remoteFamily || peer.connection.localFamily,
785
772
  address: peer.connection.remoteAddress || peer.connection.localAddress,
786
773
  port: peer.connection.remotePort || peer.connection.localPort,
787
- id: from,
774
+ id: peerId,
788
775
  };
789
776
 
790
777
  if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash);
@@ -879,9 +866,9 @@ class Peernet {
879
866
  });
880
867
  if (closest[0]) data = await closest[0].request(node.encoded);
881
868
  }
869
+ console.log({data});
882
870
  data = new Uint8Array(Object.values(data));
883
- let proto = await protoFor(data);
884
- proto = await protoFor(proto.decoded.data);
871
+ proto = await protoFor(data);
885
872
  // TODO: store data automaticly or not
886
873
  return proto.decoded.data
887
874
 
@@ -26,14 +26,12 @@ function _interopNamespace(e) {
26
26
 
27
27
  var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
28
28
 
29
- const protoFor = (data) => {
30
- console.log(data);
31
- if (!Buffer.isBuffer(data)) data = Buffer.from(data);
32
- const codec = new codecFormatInterface.Codec(data);
29
+ const protoFor = (message) => {
30
+ const codec = new codecFormatInterface.Codec(message);
33
31
  if (!codec.name) throw new Error('proto not found')
34
32
  const Proto = globalThis.peernet.protos[codec.name];
35
33
  if (!Proto) throw (new Error(`No proto defined for ${codec.name}`))
36
- return new Proto(data)
34
+ return new Proto(message)
37
35
  };
38
36
 
39
37
  /**
@@ -411,21 +409,12 @@ class MessageHandler {
411
409
  const dataHandler = async message => {
412
410
  if (!message) return
413
411
 
414
- const {data, id} = message;
415
- console.log({data});
416
- message = await protoFor(data);
417
- console.log(message.decoded.data);
418
- console.log({message});
419
- const proto = await protoFor(message.decoded.data);
420
- const from = message.decoded.from;
412
+ const {data, id, from} = message;
413
+ const proto = await protoFor(data);
421
414
 
422
415
  peernet._protoHandler({id, proto}, peernet.client.connections[from], from);
423
416
  };
424
417
 
425
- const encapsulatedError = () => {
426
- return new Error('Nodes/Data should be send encapsulated by peernet-message')
427
- };
428
-
429
418
  const dhtError = (proto) => {
430
419
  const text = `Received proto ${proto.name} expected peernet-dht-response`;
431
420
  return new Error(`Routing error: ${text}`)
@@ -764,11 +753,9 @@ class Peernet {
764
753
  const walk = async peer => {
765
754
  const node = await this.prepareMessage(data);
766
755
  let result = await peer.request(node.encoded);
756
+ console.log({result});
767
757
  result = new Uint8Array(Object.values(result));
768
- let proto = await protoFor(result);
769
- if (proto.name !== 'peernet-message') throw encapsulatedError()
770
- const from = proto.decoded.from;
771
- proto = await protoFor(proto.decoded.data);
758
+ const proto = await protoFor(result);
772
759
  if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
773
760
 
774
761
  // TODO: give ip and port (just used for location)
@@ -782,7 +769,7 @@ class Peernet {
782
769
  family: peer.connection.remoteFamily || peer.connection.localFamily,
783
770
  address: peer.connection.remoteAddress || peer.connection.localAddress,
784
771
  port: peer.connection.remotePort || peer.connection.localPort,
785
- id: from,
772
+ id: peerId,
786
773
  };
787
774
 
788
775
  if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash);
@@ -877,9 +864,9 @@ class Peernet {
877
864
  });
878
865
  if (closest[0]) data = await closest[0].request(node.encoded);
879
866
  }
867
+ console.log({data});
880
868
  data = new Uint8Array(Object.values(data));
881
- let proto = await protoFor(data);
882
- proto = await protoFor(proto.decoded.data);
869
+ proto = await protoFor(data);
883
870
  // TODO: store data automaticly or not
884
871
  return proto.decoded.data
885
872
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.14.3",
3
+ "version": "0.14.4",
4
4
  "description": "",
5
5
  "source": "src/peernet.js",
6
6
  "main": "dist/commonjs/peernet.js",
@@ -3,13 +3,8 @@ import { protoFor } from './../utils/utils.js'
3
3
  const dataHandler = async message => {
4
4
  if (!message) return
5
5
 
6
- const {data, id} = message
7
- console.log({data});
8
- message = await protoFor(data)
9
- console.log(message.decoded.data);
10
- console.log({message});
11
- const proto = await protoFor(message.decoded.data)
12
- const from = message.decoded.from
6
+ const {data, id, from} = message
7
+ const proto = await protoFor(data)
13
8
 
14
9
  peernet._protoHandler({id, proto}, peernet.client.connections[from], from)
15
10
  }
package/src/peernet.js CHANGED
@@ -342,11 +342,9 @@ export default class Peernet {
342
342
  const walk = async peer => {
343
343
  const node = await this.prepareMessage(data)
344
344
  let result = await peer.request(node.encoded)
345
+ console.log({result});
345
346
  result = new Uint8Array(Object.values(result))
346
- let proto = await protoFor(result)
347
- if (proto.name !== 'peernet-message') throw encapsulatedError()
348
- const from = proto.decoded.from
349
- proto = await protoFor(proto.decoded.data)
347
+ const proto = await protoFor(result)
350
348
  if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
351
349
 
352
350
  // TODO: give ip and port (just used for location)
@@ -360,7 +358,7 @@ export default class Peernet {
360
358
  family: peer.connection.remoteFamily || peer.connection.localFamily,
361
359
  address: peer.connection.remoteAddress || peer.connection.localAddress,
362
360
  port: peer.connection.remotePort || peer.connection.localPort,
363
- id: from,
361
+ id: peerId,
364
362
  }
365
363
 
366
364
  if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash)
@@ -455,9 +453,9 @@ export default class Peernet {
455
453
  })
456
454
  if (closest[0]) data = await closest[0].request(node.encoded)
457
455
  }
456
+ console.log({data});
458
457
  data = new Uint8Array(Object.values(data))
459
- let proto = await protoFor(data)
460
- proto = await protoFor(proto.decoded.data)
458
+ proto = await protoFor(data)
461
459
  // TODO: store data automaticly or not
462
460
  return proto.decoded.data
463
461
 
@@ -17,14 +17,12 @@ export const expected = (expected, actual) => {
17
17
  ${entries.join('\n\t')}`;
18
18
  }
19
19
 
20
- export const protoFor = (data) => {
21
- console.log(data);
22
- if (!Buffer.isBuffer(data)) data = Buffer.from(data)
23
- const codec = new Codec(data)
20
+ export const protoFor = (message) => {
21
+ const codec = new Codec(message)
24
22
  if (!codec.name) throw new Error('proto not found')
25
23
  const Proto = globalThis.peernet.protos[codec.name]
26
24
  if (!Proto) throw (new Error(`No proto defined for ${codec.name}`))
27
- return new Proto(data)
25
+ return new Proto(message)
28
26
  }
29
27
 
30
28
  /**