@leofcoin/peernet 0.11.31 → 0.12.0

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,3 @@
1
- import protons from 'protons'
2
1
  import proto from './../proto/request.proto.js'
3
2
  import { FormatInterface } from '@leofcoin/codec-format-interface'
4
3
 
@@ -7,8 +6,12 @@ export default class RequestMessage extends FormatInterface {
7
6
  return ['request']
8
7
  }
9
8
 
9
+ get messageName() {
10
+ return 'PeernetRequestMessage'
11
+ }
12
+
10
13
  constructor(data) {
11
14
  const name = 'peernet-request'
12
- super(data, protons(proto).PeernetRequestMessage, {name})
15
+ super(data, proto, {name})
13
16
  }
14
17
  }
@@ -1,4 +1,3 @@
1
- import protons from 'protons'
2
1
  import proto from './../proto/response.proto.js'
3
2
  import { FormatInterface } from '@leofcoin/codec-format-interface'
4
3
 
@@ -7,8 +6,12 @@ export default class ResponseMessage extends FormatInterface {
7
6
  return ['response']
8
7
  }
9
8
 
9
+ get messageName() {
10
+ return 'PeernetResponseMessage'
11
+ }
12
+
10
13
  constructor(data) {
11
14
  const name = 'peernet-response'
12
- super(data, protons(proto).PeernetResponseMessage, {name})
15
+ super(data, proto, {name})
13
16
  }
14
17
  }
package/src/peernet.js CHANGED
@@ -69,6 +69,14 @@ export default class Peernet {
69
69
  return ['account', 'wallet', 'block', 'transaction', 'chain', 'data', 'message']
70
70
  }
71
71
 
72
+ get protos() {
73
+ return globalThis.peernet.protos
74
+ }
75
+
76
+ get codecs() {
77
+ return codecs
78
+ }
79
+
72
80
  addProto(name, proto) {
73
81
  if (!this.protos[name]) this.protos[name] = proto
74
82
  }
@@ -173,16 +181,11 @@ export default class Peernet {
173
181
  'chat-message': ChatMessage,
174
182
  }
175
183
 
176
- this.protos = globalThis.peernet.protos
177
- this.codecs = codecs
178
-
179
184
  this._messageHandler = new MessageHandler(this.network)
180
185
 
181
186
  const {daemon, environment} = await target()
182
187
  this.hasDaemon = daemon
183
188
 
184
- HTTP_IMPORT
185
-
186
189
  for (const store of this.defaultStores) {
187
190
  await this.addStore(store, options.storePrefix, options.root)
188
191
  }
@@ -281,7 +284,7 @@ export default class Peernet {
281
284
  if (store.private) has = false
282
285
  else has = await store.has(hash)
283
286
  }
284
- const data = new DHTMessageResponse({hash, has})
287
+ const data = await new DHTMessageResponse({hash, has})
285
288
  const node = await this.prepareMessage(from, data.encoded)
286
289
 
287
290
  this.sendMessage(peer, id, node.encoded)
@@ -297,7 +300,7 @@ export default class Peernet {
297
300
  data = await store.get(hash)
298
301
 
299
302
  if (data) {
300
- data = new DataMessageResponse({hash, data});
303
+ data = await new DataMessageResponse({hash, data});
301
304
 
302
305
  const node = await this.prepareMessage(from, data.encoded)
303
306
  this.sendMessage(peer, id, node.encoded)
@@ -326,16 +329,16 @@ export default class Peernet {
326
329
  */
327
330
  async walk(hash) {
328
331
  if (!hash) throw new Error('hash expected, received undefined')
329
- const data = new DHTMessage({hash})
332
+ const data = await new DHTMessage({hash})
330
333
  const clientId = this.client.id
331
334
  const walk = async peer => {
332
335
  const node = await this.prepareMessage(peer.peerId, data.encoded)
333
336
  let result = await peer.request(node.encoded)
334
337
  result = new Uint8Array(Object.values(result))
335
- let proto = protoFor(result)
338
+ let proto = await protoFor(result)
336
339
  if (proto.name !== 'peernet-message') throw encapsulatedError()
337
340
  const from = proto.decoded.from
338
- proto = protoFor(proto.decoded.data)
341
+ proto = await protoFor(proto.decoded.data)
339
342
  if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
340
343
 
341
344
  // TODO: give ip and port (just used for location)
@@ -434,7 +437,7 @@ export default class Peernet {
434
437
  if (peer.peerId === id) return peer
435
438
  })
436
439
 
437
- let data = new DataMessage({hash, store: store?.name ? store?.name : store});
440
+ let data = await new DataMessage({hash, store: store?.name ? store?.name : store});
438
441
 
439
442
  const node = await this.prepareMessage(id, data.encoded)
440
443
  if (closest[0]) data = await closest[0].request(node.encoded)
@@ -445,8 +448,8 @@ export default class Peernet {
445
448
  if (closest[0]) data = await closest[0].request(node.encoded)
446
449
  }
447
450
  data = new Uint8Array(Object.values(data))
448
- let proto = protoFor(data)
449
- proto = protoFor(proto.decoded.data)
451
+ let proto = await protoFor(data)
452
+ proto = await protoFor(proto.decoded.data)
450
453
  // TODO: store data automaticly or not
451
454
  return proto.decoded.data
452
455
 
@@ -580,7 +583,7 @@ export default class Peernet {
580
583
  if (topic instanceof Uint8Array === false) topic = new TextEncoder().encode(topic)
581
584
  if (data instanceof Uint8Array === false) data = new TextEncoder().encode(JSON.stringify(data))
582
585
  const id = Math.random().toString(36).slice(-12)
583
- data = new PsMessage({data, topic})
586
+ data = await new PsMessage({data, topic})
584
587
  for (const peer of this.connections) {
585
588
  if (peer.peerId !== this.peerId) {
586
589
  const node = await this.prepareMessage(peer.peerId, data.encoded)