@leofcoin/peernet 0.9.2 → 0.9.3

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/README.md CHANGED
@@ -5,6 +5,30 @@
5
5
  import discovery from 'socket-discovery'
6
6
  ```
7
7
 
8
+ ## API
9
+ #### addRequestHandler
10
+
11
+ examples
12
+ ```js
13
+ peernet.addRequestHandler('lastBlock', () => {
14
+ let response;
15
+ const height = await chainStore.get('localIndex')
16
+ const hash = await chainStore.get('localBlock')
17
+ response = JSON.stringify({ height: height.toString(), hash: hash.toString() })
18
+ return new ResponseMessage({ response })
19
+ // or
20
+ return new peernet.protos['peernet-response']({ response })
21
+ })
22
+ ```
23
+
24
+ ```js
25
+ peernet.addRequestHandler('hello', () => {
26
+ return new ResponseMessage({ response: 'hi' })
27
+ // or
28
+ return new peernet.protos['peernet-response']({ response: 'hi' })
29
+ })
30
+ ```
31
+
8
32
  ## Development
9
33
  ### watch
10
34
  ```sh
@@ -19,7 +43,7 @@ npm run c
19
43
  npm run demo
20
44
  ```
21
45
 
22
- `note: you need to install jsproject`
46
+ `note: you need to install jsproject`
23
47
  ```sh
24
48
  npm i -g @vandeurenglenn/project
25
- ```
49
+ ```
@@ -261,7 +261,7 @@ class LeofcoinStorage$1 {
261
261
 
262
262
  }
263
263
 
264
- var version = "0.9.1";
264
+ var version = "0.9.2";
265
265
 
266
266
  var api$1 = {
267
267
  version: ({send}) => send({client: '@peernet/api/http', version}),
@@ -2062,6 +2062,7 @@ class Peernet {
2062
2062
  */
2063
2063
  this.peerMap = new Map();
2064
2064
  this.stores = [];
2065
+ this.requestProtos = {};
2065
2066
  this.storePrefix = options.storePrefix;
2066
2067
  this.root = options.root;
2067
2068
 
@@ -2186,6 +2187,10 @@ class Peernet {
2186
2187
  }
2187
2188
  }
2188
2189
 
2190
+ addRequestHandler(name, method) {
2191
+ this.requestProtos[name] = method;
2192
+ }
2193
+
2189
2194
  /**
2190
2195
  * @private
2191
2196
  *
@@ -2288,16 +2293,13 @@ class Peernet {
2288
2293
  } else if (proto.name === 'peernet-request') {
2289
2294
  // TODO: make dynamic
2290
2295
  // exposeddevapi[proto.decoded.request](proto.decoded.params)
2291
- let response;
2292
- if (proto.decoded.request === 'lastBlock') {
2293
- const height = await chainStore.get('localIndex');
2294
- const hash = await chainStore.get('localBlock');
2295
- response = JSON.stringify({ height: height.toString(), hash: hash.toString() });
2296
- const data = new ResponseMessage({ response });
2296
+ const method = this.requestProtos[proto.decoded.request];
2297
+ if (method) {
2298
+ const data = await method();
2297
2299
  const node = await this.prepareMessage(from, data.encoded);
2298
-
2299
2300
  peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2300
2301
  }
2302
+
2301
2303
  } else if (proto.name === 'peernet-ps' &&
2302
2304
  this._getPeerId(peer.id) !== this.id.toString()) {
2303
2305
  globalSub.publish(proto.decoded.topic.toString(), proto.decoded.data.toString());
@@ -264,7 +264,7 @@ class LeofcoinStorage$1 {
264
264
 
265
265
  }
266
266
 
267
- var version = "0.9.1";
267
+ var version = "0.9.2";
268
268
 
269
269
  var api$1 = {
270
270
  version: ({send}) => send({client: '@peernet/api/http', version}),
@@ -1497,6 +1497,7 @@ class Peernet {
1497
1497
  */
1498
1498
  this.peerMap = new Map();
1499
1499
  this.stores = [];
1500
+ this.requestProtos = {};
1500
1501
  this.storePrefix = options.storePrefix;
1501
1502
  this.root = options.root;
1502
1503
 
@@ -1621,6 +1622,10 @@ class Peernet {
1621
1622
  }
1622
1623
  }
1623
1624
 
1625
+ addRequestHandler(name, method) {
1626
+ this.requestProtos[name] = method;
1627
+ }
1628
+
1624
1629
  /**
1625
1630
  * @private
1626
1631
  *
@@ -1723,16 +1728,13 @@ class Peernet {
1723
1728
  } else if (proto.name === 'peernet-request') {
1724
1729
  // TODO: make dynamic
1725
1730
  // exposeddevapi[proto.decoded.request](proto.decoded.params)
1726
- let response$1;
1727
- if (proto.decoded.request === 'lastBlock') {
1728
- const height = await chainStore.get('localIndex');
1729
- const hash = await chainStore.get('localBlock');
1730
- response$1 = JSON.stringify({ height: height.toString(), hash: hash.toString() });
1731
- const data = new response({ response: response$1 });
1731
+ const method = this.requestProtos[proto.decoded.request];
1732
+ if (method) {
1733
+ const data = await method();
1732
1734
  const node = await this.prepareMessage(from, data.encoded);
1733
-
1734
1735
  peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
1735
1736
  }
1737
+
1736
1738
  } else if (proto.name === 'peernet-ps' &&
1737
1739
  this._getPeerId(peer.id) !== this.id.toString()) {
1738
1740
  globalSub.publish(proto.decoded.topic.toString(), proto.decoded.data.toString());
@@ -244,7 +244,7 @@ class LeofcoinStorage$1 {
244
244
 
245
245
  }
246
246
 
247
- var version = "0.9.1";
247
+ var version = "0.9.2";
248
248
 
249
249
  var api$1 = {
250
250
  version: ({send}) => send({client: '@peernet/api/http', version}),
@@ -2045,6 +2045,7 @@ class Peernet {
2045
2045
  */
2046
2046
  this.peerMap = new Map();
2047
2047
  this.stores = [];
2048
+ this.requestProtos = {};
2048
2049
  this.storePrefix = options.storePrefix;
2049
2050
  this.root = options.root;
2050
2051
 
@@ -2169,6 +2170,10 @@ class Peernet {
2169
2170
  }
2170
2171
  }
2171
2172
 
2173
+ addRequestHandler(name, method) {
2174
+ this.requestProtos[name] = method;
2175
+ }
2176
+
2172
2177
  /**
2173
2178
  * @private
2174
2179
  *
@@ -2271,14 +2276,10 @@ class Peernet {
2271
2276
  } else if (proto.name === 'peernet-request') {
2272
2277
  // TODO: make dynamic
2273
2278
  // exposeddevapi[proto.decoded.request](proto.decoded.params)
2274
- let response;
2275
- if (proto.decoded.request === 'lastBlock') {
2276
- const height = await chainStore.get('localIndex');
2277
- const hash = await chainStore.get('localBlock');
2278
- response = JSON.stringify({ height: height.toString(), hash: hash.toString() });
2279
- const data = new ResponseMessage({ response });
2279
+ const method = this.requestProtos[proto.decoded.request];
2280
+ if (method) {
2281
+ const data = await method();
2280
2282
  const node = await this.prepareMessage(from, data.encoded);
2281
-
2282
2283
  peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2283
2284
  }
2284
2285
  } else if (proto.name === 'peernet-ps' &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.9.2",
3
+ "version": "0.9.3",
4
4
  "description": "",
5
5
  "main": "dist/commonjs/peernet.js",
6
6
  "module": "dist/module/peernet.js",
@@ -0,0 +1,7 @@
1
+ peernet.addRequestHandler('lastBlock', () => {
2
+ let response;
3
+ const height = await chainStore.get('localIndex')
4
+ const hash = await chainStore.get('localBlock')
5
+ response = JSON.stringify({ height: height.toString(), hash: hash.toString() })
6
+ return new peernet.protos['peernet-response']({ response })
7
+ })