trac-peer 0.1.21 → 0.1.23

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.1.21",
4
+ "version": "0.1.23",
5
5
  "type": "module",
6
6
  "dependencies": {
7
7
  "assert": "npm:bare-node-assert",
@@ -36,7 +36,6 @@
36
36
  "brittle": "3.0.0",
37
37
  "buffer": "npm:bare-node-buffer",
38
38
  "child_process": "npm:bare-node-child-process",
39
- "compact-encoding": "^2.16.0",
40
39
  "console": "npm:bare-node-console",
41
40
  "corestore": "7.1.0",
42
41
  "crypto": "npm:bare-node-crypto",
@@ -60,7 +59,6 @@
60
59
  "path": "npm:bare-node-path",
61
60
  "pear-interface": "1.0.0",
62
61
  "process": "npm:bare-node-process",
63
- "protomux": "^3.10.1",
64
62
  "protomux-wakeup": "^2.2.1",
65
63
  "readline": "npm:bare-node-readline",
66
64
  "ready-resource": "^1.0.0",
package/src/functions.js CHANGED
@@ -283,14 +283,18 @@ export async function addWriter(input, peer){
283
283
  msg: msg
284
284
  };
285
285
  const hash = peer.wallet.sign(JSON.stringify(msg) + nonce);
286
- peer.emit('announce', { op : 'append_writer', type: 'addIndexer', key: parsed.key, value: signature, hash: hash, nonce: nonce });
286
+ if(peer.base.writable){
287
+ await peer.base.append({ op : 'append_writer', type: 'addIndexer', key: parsed.key, value: signature, hash: hash, nonce: nonce });
288
+ }
287
289
  } else if(splitted[0] === '/add_writer') {
288
290
  const msg = { type: 'addWriter', key: ''+parsed.key }
289
291
  const signature = {
290
292
  msg: msg
291
293
  };
292
294
  const hash = peer.wallet.sign(JSON.stringify(msg) + nonce);
293
- peer.emit('announce', { op : 'append_writer', type: 'addWriter', key: ''+parsed.key, value: signature, hash: hash, nonce : nonce });
295
+ if(peer.base.writable){
296
+ await peer.base.append({ op : 'append_writer', type: 'addWriter', key: ''+parsed.key, value: signature, hash: hash, nonce : nonce });
297
+ }
294
298
  }
295
299
  }
296
300
 
package/src/index.js CHANGED
@@ -22,9 +22,6 @@ export {default as Contract} from "./contract.js";
22
22
  export {default as Feature} from "./feature.js";
23
23
  export {default as Wallet} from "./wallet.js";
24
24
 
25
- import Protomux from 'protomux';
26
- import * as c from 'compact-encoding';
27
-
28
25
  export class Peer extends ReadyResource {
29
26
 
30
27
  constructor(options = {}) {
@@ -59,6 +56,7 @@ export class Peer extends ReadyResource {
59
56
  this.check = new Check();
60
57
  this.dhtBootstrap = ['116.202.214.143:10001','116.202.214.149:10001', 'node1.hyperdht.org:49737', 'node2.hyperdht.org:49737', 'node3.hyperdht.org:49737'];
61
58
  this.dhtNode = new DHT({ bootstrap: this.dhtBootstrap });
59
+ this.seen_auto_add = {};
62
60
 
63
61
  this.tx_observer();
64
62
  this.nodeListener();
@@ -486,12 +484,14 @@ export class Peer extends ReadyResource {
486
484
  }
487
485
  });
488
486
  this.updater();
487
+ this.seenAutoAdd();
489
488
  const auto_add_writers = await this.base.view.get('auto_add_writers');
490
489
  if(!this.base.writable && null !== auto_add_writers && auto_add_writers.value === 'on'){
491
490
  this.emit('announce', {
492
491
  op : 'auto-add-writer',
493
492
  type : 'autoAddWriter',
494
- key : this.writerLocalKey
493
+ key : this.writerLocalKey,
494
+ id : Math.random() + new Date.now()
495
495
  });
496
496
  }
497
497
  }
@@ -586,41 +586,23 @@ export class Peer extends ReadyResource {
586
586
 
587
587
  async _replicate() {
588
588
  if (!this.swarm) {
589
- const keyPair = await this.store.createKeyPair('hyperswarm');
589
+
590
+ const keyPair = {
591
+ publicKey: b4a.from(this.wallet.publicKey, 'hex'),
592
+ secretKey: b4a.from(this.wallet.secretKey, 'hex')
593
+ };
594
+
590
595
  this.swarm = new Hyperswarm({ keyPair, bootstrap: this.dhtBootstrap });
591
596
 
592
597
  console.log(`Writer key: ${this.writerLocalKey}`)
593
598
 
594
599
  this.swarm.on('connection', async (connection, peerInfo) => {
595
-
596
600
  const peerName = b4a.toString(connection.remotePublicKey, 'hex');
597
601
  this.connectedPeers.add(peerName);
598
602
  wakeup.addStream(connection);
599
603
  this.store.replicate(connection);
600
604
  this.connectedNodes++;
601
605
 
602
- const mux = new Protomux(connection);
603
-
604
- const cool = mux.createChannel({
605
- protocol: 'cool-protocol',
606
- id: this.tx_channel,
607
- onopen () {
608
- console.log('the other side opened this protocol!')
609
- },
610
- onclose () {
611
- console.log('either side closed the protocol')
612
- }
613
- });
614
-
615
- const one = cool.addMessage({
616
- encoding: c.string,
617
- onmessage (m) {
618
- console.log('recv message (1)', m)
619
- }
620
- })
621
-
622
- cool.open()
623
-
624
606
  connection.on('close', () => {
625
607
  this.connectedNodes--;
626
608
  this.connectedPeers.delete(peerName);
@@ -631,18 +613,26 @@ export class Peer extends ReadyResource {
631
613
  connection.on('data', async (msg) => {
632
614
  try{
633
615
  msg = JSON.parse(msg);
634
- if(msg.op && msg.op === 'append_writer' && this.base.localWriter.isActive &&
635
- this.writerLocalKey !== msg.key) {
636
- await this.base.append(msg);
637
- } else if(msg.op && msg.op === 'auto-add-writer' && this.base.localWriter.isActive &&
638
- this.writerLocalKey !== msg.key) {
639
- await this.base.append(msg);
616
+ if(msg.op && msg.op === 'auto-add-writer' && this.base.localWriter.isActive &&
617
+ this.writerLocalKey !== msg.key &&
618
+ false === this.base.activeWriters.has(b4a.from(msg.key, 'hex')) &&
619
+ this.seen_auto_add[msg.id] === undefined) {
620
+ if(this.base.writable){
621
+ await this.base.append(msg);
622
+ } else {
623
+ this.seen_auto_add[msg.id] = new Date.now();
624
+ this.emit('announce', {
625
+ op : 'auto-add-writer',
626
+ type : 'autoAddWriter',
627
+ key : msg.key,
628
+ id : msg.id
629
+ });
630
+ }
640
631
  }
641
632
  } catch(e){ }
642
633
  });
643
634
 
644
635
  this.on('announce', async function(msg){
645
- one.send(JSON.stringify(msg))
646
636
  await connection.write(JSON.stringify(msg))
647
637
  });
648
638
 
@@ -658,6 +648,19 @@ export class Peer extends ReadyResource {
658
648
  }
659
649
  }
660
650
 
651
+ async seenAutoAdd(){
652
+ while(true){
653
+ const ts = Date.now();
654
+ for(let seen in this.seen_auto_add){
655
+ if(ts - this.seen_auto_add[seen] > 30_000){
656
+ console.log('Wiping last seen', seen);
657
+ delete this.seen_auto_add[seen];
658
+ }
659
+ }
660
+ await this.sleep(1000);
661
+ }
662
+ }
663
+
661
664
  nodeListener() {
662
665
  this.on('readyNode', async () => {
663
666
  if (!this.isStreaming) {