@leofcoin/peernet 0.10.1 → 0.10.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.
package/package.json CHANGED
@@ -1,15 +1,13 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.10.1",
3
+ "version": "0.10.4",
4
4
  "description": "",
5
5
  "main": "dist/commonjs/peernet.js",
6
- "module": "dist/module/peernet.js",
7
- "browser": "dist/browser/peernet.js",
8
6
  "scripts": {
9
7
  "test": "node test/index.js",
10
8
  "server": "discovery-swarm-webrtc --port=4000",
11
9
  "demo": "jsproject --serve ./ --port 6868",
12
- "doc": "./node_modules/.bin/esdoc",
10
+ "doc": "esdoc",
13
11
  "lint": "./node_modules/.bin/eslint src/**/**.js --fix",
14
12
  "coverage": "nyc --reporter=lcov npm run test",
15
13
  "coveralls": "cat ./coverage/lcov.info | coveralls",
@@ -24,6 +22,7 @@
24
22
  "@leofcoin/generate-account": "^1.0.2",
25
23
  "@leofcoin/multi-wallet": "^2.0.6",
26
24
  "@leofcoin/storage": "^2.1.0",
25
+ "@vandeurenglenn/little-pubsub": "^1.3.1",
27
26
  "bs32": "^0.1.6",
28
27
  "bs58": "^4.0.1",
29
28
  "bs58check": "^2.1.2",
package/rollup.config.js CHANGED
@@ -27,10 +27,13 @@ export default [{
27
27
  modify({
28
28
  "import fetch from 'node-fetch'": ''
29
29
  }),
30
- lint({
31
- fix: true,
32
- exclude: ['package.json', "package-lock.json"]
33
- })
30
+ cjs(),
31
+ // resolve(),
32
+ // lint({
33
+ // fix: true,
34
+ // exclude: ['package.json', "package-lock.json"]
35
+ // })
36
+
34
37
 
35
38
  ]
36
39
  }, {
@@ -6,7 +6,7 @@ import Hash from './../hash/hash'
6
6
 
7
7
  export default class FormatInterface {
8
8
  /**
9
- * @param {Buffer|String|Object} buffer -
9
+ * @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
10
10
  * @param {Object} proto - {encode, decode}
11
11
  * @param {Object} options - {hashFormat, name}
12
12
  */
@@ -15,7 +15,9 @@ export default class FormatInterface {
15
15
  this.protoDecode = proto.decode
16
16
  if (options.name) this.name = options.name
17
17
  this.hashFormat = options.hashFormat || 'bs32'
18
- if (Buffer.isBuffer(buffer)) {
18
+ if (buffer.name === options.name) {
19
+ return buffer
20
+ } else if (Buffer.isBuffer(buffer)) {
19
21
  const codec = new Codec(buffer)
20
22
  if (codec.name) {
21
23
  this.fromEncoded(buffer)
@@ -133,7 +135,21 @@ export default class FormatInterface {
133
135
  * @param {Object} data
134
136
  */
135
137
  create(data) {
136
- this.decoded = data
137
- this.encode(data)
138
+ const decoded = {}
139
+ if (this.keys?.length > 0) {
140
+ for (const key of this.keys) {
141
+ Object.defineProperties(decoded, {
142
+ [key]: {
143
+ enumerable: true,
144
+ configurable: true,
145
+ set: (val) => value = data[key],
146
+ get: () => data[key]
147
+ }
148
+ })
149
+ }
150
+
151
+ this.decoded = decoded
152
+ this.encode()
153
+ }
138
154
  }
139
155
  }
@@ -2,13 +2,17 @@ import protons from 'protons'
2
2
  import proto from './../proto/data.proto.js'
3
3
  import CodecFormat from './../codec/codec-format-interface.js'
4
4
 
5
+ /**
6
+ * @extends {CodecFormat}
7
+ */
5
8
  export default class DataMessage extends CodecFormat {
6
9
  get keys() {
7
10
  return ['hash', 'store']
8
11
  }
9
-
12
+ /**
13
+ * @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
14
+ */
10
15
  constructor(data) {
11
- const name = 'peernet-data'
12
- super(data, protons(proto).PeernetDataMessage, {name})
16
+ super(data, protons(proto).PeernetDataMessage, {name: 'peernet-data'})
13
17
  }
14
18
  }
package/src/peernet.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import Pubsub from '@vandeurenglenn/little-pubsub'
2
2
  import Client from './client'
3
- import LeofcoinStorage from './../node_modules/@leofcoin/storage/src/level.js'
3
+ import LeofcoinStorage from '@leofcoin/storage'
4
4
  import http from './http/http.js'
5
5
  import httpClient from './http/client/client.js'
6
6
  import LeofcoinStorageClient from './http/client/storage.js'
@@ -62,6 +62,10 @@ export default class Peernet {
62
62
  else options.root = `.${this.network}/peernet`
63
63
  }
64
64
  globalThis.peernet = this
65
+ this.bw = {
66
+ up: 0,
67
+ down: 0,
68
+ }
65
69
  return this._init(options)
66
70
  }
67
71
 
@@ -274,6 +278,7 @@ export default class Peernet {
274
278
  */
275
279
  async _protoHandler(message, peer) {
276
280
  const {id, proto} = message
281
+ this.bw.down += proto.encoded.length
277
282
  if (proto.name === 'peernet-peer') {
278
283
  const from = proto.decoded.id
279
284
  if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id])
@@ -288,6 +293,7 @@ export default class Peernet {
288
293
  const node = await this.prepareMessage(from, data.encoded)
289
294
 
290
295
  peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
296
+ this.bw.up += node.encoded.length
291
297
  } else if (proto.name === 'peernet-peer-response') {
292
298
  const from = proto.decoded.id
293
299
  if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id])
@@ -303,9 +309,10 @@ export default class Peernet {
303
309
  if (!from) {
304
310
  const data = new PeerMessage({id: this.id})
305
311
  const node = await this.prepareMessage(peer.id, data.encoded)
306
-
312
+ this.bw.up += node.encoded.length
307
313
  let response = await peer.request(node.encoded)
308
314
  response = protoFor(response)
315
+
309
316
  response = new PeerMessageResponse(response.decoded.data)
310
317
 
311
318
  from = response.decoded.id
@@ -333,26 +340,28 @@ export default class Peernet {
333
340
  const node = await this.prepareMessage(from, data.encoded)
334
341
 
335
342
  peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
343
+ this.bw.up += node.encoded.length
336
344
  } else if (proto.name === 'peernet-data') {
337
345
  let { hash, store } = proto.decoded
338
346
  let data
339
347
 
340
348
  if (!store) {
341
- data = await this.get(hash)
342
- } else {
343
- store = globalThis[`${store}Store`]
344
- if (store.private) {
345
- // TODO: ban
346
- return
347
- } else data = await store.get(hash)
349
+ store = await this.whichStore([...this.stores], hash)
348
350
  }
351
+ if (store && !store.private) {
352
+ data = await store.get(hash)
349
353
 
350
- if (data) {
351
- data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
354
+ if (data) {
355
+ data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
352
356
 
353
- const node = await this.prepareMessage(from, data.encoded)
354
- peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
357
+ const node = await this.prepareMessage(from, data.encoded)
358
+ peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
359
+ this.bw.up += node.encoded.length
360
+ }
361
+ } else {
362
+ // ban (trying to access private store)
355
363
  }
364
+
356
365
  } else if (proto.name === 'peernet-peer') {
357
366
  const from = proto.decoded.id
358
367
  if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id])
@@ -365,6 +374,7 @@ export default class Peernet {
365
374
  const node = await this.prepareMessage(from, data.encoded)
366
375
 
367
376
  peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
377
+ this.bw.up += node.encoded.length
368
378
  } else if (proto.name === 'peernet-request') {
369
379
  // TODO: make dynamic
370
380
  // exposeddevapi[proto.decoded.request](proto.decoded.params)
@@ -373,6 +383,7 @@ export default class Peernet {
373
383
  const data = await method()
374
384
  const node = await this.prepareMessage(from, data.encoded)
375
385
  peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
386
+ this.bw.up += node.encoded.length
376
387
  }
377
388
  } else if (proto.name === 'peernet-ps' &&
378
389
  this._getPeerId(peer.id) !== this.id.toString()) {
@@ -453,7 +464,7 @@ export default class Peernet {
453
464
  get: async (hash) => {
454
465
  const data = await blockStore.has(hash)
455
466
  if (data) return await blockStore.get(hash)
456
- return this.requestData(hash)
467
+ return this.requestData(hash, 'block')
457
468
  },
458
469
  put: async (hash, data) => {
459
470
  if (await blockStore.has(hash)) return
@@ -590,7 +601,8 @@ export default class Peernet {
590
601
  /**
591
602
  * Get content for given hash
592
603
  *
593
- * @param {String} hash
604
+ * @param {String} hash - the hash of the wanted data
605
+ * @param {String} store - storeName to access
594
606
  */
595
607
  async get(hash, store) {
596
608
  debug(`get ${hash}`)
@@ -600,7 +612,7 @@ export default class Peernet {
600
612
  if (store && await store.has(hash)) data = await store.get(hash)
601
613
  if (data) return data
602
614
 
603
- return this.requestData(hash, 'data')
615
+ return this.requestData(hash, store)
604
616
  }
605
617
 
606
618
  /**
@@ -608,10 +620,11 @@ export default class Peernet {
608
620
  *
609
621
  * @param {String} hash
610
622
  * @param {Buffer} data
623
+ * @param {String} store - storeName to access
611
624
  */
612
625
  async put(hash, data, store = 'data') {
613
626
  store = globalThis[`${store}Store`]
614
- return await store.put(hash, data)
627
+ return store.put(hash, data)
615
628
  }
616
629
 
617
630
  /**