trac-peer 0.0.56 → 0.0.58

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,7 +1,7 @@
1
1
  {
2
2
  "name": "trac-peer",
3
3
  "main": "src/index.js",
4
- "version": "0.0.56",
4
+ "version": "0.0.58",
5
5
  "type": "module",
6
6
  "dependencies": {
7
7
  "autobase": "7.1.1",
@@ -11,6 +11,7 @@
11
11
  "brittle": "3.0.0",
12
12
  "corestore": "7.0.23",
13
13
  "debounceify": "1.1.0",
14
+ "fastest-validator": "^1.19.0",
14
15
  "hyperbee": "2.23.0",
15
16
  "hypercore": "11.0.48",
16
17
  "hypercore-crypto": "3.4.0",
package/src/check.js ADDED
@@ -0,0 +1,49 @@
1
+ import Validator from 'fastest-validator';
2
+
3
+ class Check {
4
+
5
+ constructor() {
6
+ this._node = null;
7
+ this._tx = null;
8
+ this.validator = new Validator();
9
+ }
10
+
11
+ async compileNode() {
12
+ const schema = {
13
+ value: {
14
+ $$type: "object",
15
+ type: "string"
16
+ }
17
+ };
18
+ return this.validator.compile(schema);
19
+ }
20
+
21
+ async node(node){
22
+ if(this._node === null) {
23
+ this._node = this.compileNode();
24
+ }
25
+ return this._node(node);
26
+ }
27
+
28
+ async compileTx() {
29
+ // if(op.key === undefined || op.value === undefined || op.value.dispatch === undefined) continue;
30
+ const schema = {
31
+ key: "hex",
32
+ value : {
33
+ $$type : "object",
34
+ dispatch : "object",
35
+ msbsl : "integer"
36
+ }
37
+ };
38
+ return this.validator.compile(schema);
39
+ }
40
+
41
+ async tx(op){
42
+ if(this._tx === null) {
43
+ this._tx = this.compileTx();
44
+ }
45
+ return this._tx(op);
46
+ }
47
+ }
48
+
49
+ export default Check;
package/src/index.js CHANGED
@@ -12,6 +12,7 @@ const wakeup = new w();
12
12
  import {addWriter, addAdmin, setAutoAddWriters, setChatStatus, setMod, deleteMessage,
13
13
  enableWhitelist, postMessage, jsonStringify, visibleLength, setNick,
14
14
  muteStatus, setWhitelistStatus} from "./functions.js";
15
+ import Check from "./check.js";
15
16
  export {default as Protocol} from "./protocol.js";
16
17
  export {default as Contract} from "./contract.js";
17
18
  export {default as Feature} from "./feature.js";
@@ -48,6 +49,7 @@ export class Peer extends ReadyResource {
48
49
  this.isStreaming = false;
49
50
  this.connectedPeers = new Set();
50
51
  this.options = options;
52
+ this.check = new Check();
51
53
 
52
54
  this.tx_observer();
53
55
  this.nodeListener();
@@ -74,11 +76,10 @@ export class Peer extends ReadyResource {
74
76
  const batch = view.batch();
75
77
 
76
78
  for (const node of nodes) {
77
- if(node.value === undefined || node.value.type === undefined) continue;
79
+ if(false === await this.check.node(node)) continue;
78
80
  const op = node.value;
79
81
  if (op.type === 'tx') {
80
- if(op.key === undefined || op.value === undefined || op.value.dispatch === undefined) continue;
81
-
82
+ if(false === await this.check.tx(op)) continue;
82
83
  const str_dispatch = jsonStringify(op.value.dispatch);
83
84
  const msb_view_session = _this.msb.base.view.checkout(op.value.msbsl);
84
85
  const post_tx = await msb_view_session.get(op.key);
@@ -86,8 +87,9 @@ export class Peer extends ReadyResource {
86
87
  if (null !== str_dispatch &&
87
88
  null !== post_tx &&
88
89
  null === await batch.get('tx/'+op.key) &&
89
- post_tx.value &&
90
- post_tx.value.tx &&
90
+ post_tx.value !== undefined &&
91
+ post_tx.value.tx !== undefined &&
92
+ post_tx.value.ch !== undefined &&
91
93
  op.key === post_tx.value.tx &&
92
94
  post_tx.value.ch === createHash('sha256').update(str_dispatch).digest('hex')) {
93
95
  await batch.put('tx/'+op.key, op.value);
@@ -468,7 +470,7 @@ export class Peer extends ReadyResource {
468
470
  connection.on('error', (error) => { });
469
471
 
470
472
  _this.on('tx', async (msg) => {
471
- if(Object.keys(_this.tx_pool).length < _this.tx_pool_max_size && !_this.tx_pool[msg.tx]){
473
+ if(Object.keys(_this.tx_pool).length <= _this.tx_pool_max_size && !_this.tx_pool[msg.tx]){
472
474
  await connection.write(JSON.stringify(msg))
473
475
  msg['ts'] = Math.floor(Date.now() / 1000);
474
476
  _this.tx_pool[msg.tx] = msg;
@@ -624,7 +626,7 @@ export class Peer extends ReadyResource {
624
626
  console.log('- /set_nick | Change your nickname like this \'/set_nick --nick "Peter"\'. Chat must be enabled. Can be edited by admin and mods using the optional --user <address> flag.');
625
627
  console.log('- /mute_status | Only admin and mods. Mute or unmute a user by their address: \'/mute_status --user "<address>" --muted 1\'.');
626
628
  console.log('- /set_mod | Only admin. Set a user as mod: \'/set_mod --user "<address>" --mod 1\'.');
627
- console.log('- /delete_message | Delete a messages: \'/delete_message --id 1\'. Chat must be enabled.');
629
+ console.log('- /delete_message | Delete a message: \'/delete_message --id 1\'. Chat must be enabled.');
628
630
  console.log('- /enable_whitelist | Only admin. Enable/disable chat whitelists: \'/enable_whitelist --enabled 1\'.');
629
631
  console.log('- /set_whitelist_status | Only admin. Add/remove users to/from the chat whitelist: \'/set_whitelist_status --user "<address>" --status 1\'.');
630
632
  console.log(' ');