trac-peer 0.1.21 → 0.1.22

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.22",
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 = {}) {
@@ -586,41 +583,23 @@ export class Peer extends ReadyResource {
586
583
 
587
584
  async _replicate() {
588
585
  if (!this.swarm) {
589
- const keyPair = await this.store.createKeyPair('hyperswarm');
586
+
587
+ const keyPair = {
588
+ publicKey: b4a.from(this.wallet.publicKey, 'hex'),
589
+ secretKey: b4a.from(this.wallet.secretKey, 'hex')
590
+ };
591
+
590
592
  this.swarm = new Hyperswarm({ keyPair, bootstrap: this.dhtBootstrap });
591
593
 
592
594
  console.log(`Writer key: ${this.writerLocalKey}`)
593
595
 
594
596
  this.swarm.on('connection', async (connection, peerInfo) => {
595
-
596
597
  const peerName = b4a.toString(connection.remotePublicKey, 'hex');
597
598
  this.connectedPeers.add(peerName);
598
599
  wakeup.addStream(connection);
599
600
  this.store.replicate(connection);
600
601
  this.connectedNodes++;
601
602
 
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
603
  connection.on('close', () => {
625
604
  this.connectedNodes--;
626
605
  this.connectedPeers.delete(peerName);
@@ -631,18 +610,23 @@ export class Peer extends ReadyResource {
631
610
  connection.on('data', async (msg) => {
632
611
  try{
633
612
  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);
613
+ if(msg.op && msg.op === 'auto-add-writer' && this.base.localWriter.isActive &&
614
+ this.writerLocalKey !== msg.key &&
615
+ false === this.base.activeWriters.has(b4a.from(msg.key, 'hex'))) {
616
+ if(this.base.writable){
617
+ await this.base.append(msg);
618
+ } else {
619
+ this.emit('announce', {
620
+ op : 'auto-add-writer',
621
+ type : 'autoAddWriter',
622
+ key : msg.key
623
+ });
624
+ }
640
625
  }
641
626
  } catch(e){ }
642
627
  });
643
628
 
644
629
  this.on('announce', async function(msg){
645
- one.send(JSON.stringify(msg))
646
630
  await connection.write(JSON.stringify(msg))
647
631
  });
648
632