blind-peer 0.0.1 → 0.0.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/bin.js ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { command, flag } = require('paparam')
4
+ const goodbye = require('graceful-goodbye')
5
+ const idEnc = require('hypercore-id-encoding')
6
+ const BlindPeer = require('.')
7
+
8
+ const cmd = command('blind-peer',
9
+ flag('--storage|-s [path]', 'storage path, defaults to ./blind-peer'),
10
+ async function ({ flags }) {
11
+ console.info('Starting blind peer')
12
+
13
+ const storage = flags.storage || 'blind-peer'
14
+ const blindPeer = new BlindPeer(storage)
15
+
16
+ console.info(`Using storage '${storage}'`)
17
+
18
+ goodbye(async () => {
19
+ console.info('Shutting down blind peer')
20
+ await blindPeer.close()
21
+ })
22
+
23
+ await blindPeer.listen()
24
+
25
+ console.info(`Listening at ${idEnc.normalize(blindPeer.publicKey)}`)
26
+ }
27
+ )
28
+
29
+ cmd.parse()
package/build.js CHANGED
@@ -52,7 +52,7 @@ blind.register({
52
52
  },
53
53
  {
54
54
  name: 'message',
55
- type: 'string'
55
+ type: 'buffer'
56
56
  }
57
57
  ]
58
58
  })
@@ -90,7 +90,7 @@ blind.register({
90
90
 
91
91
  Hyperschema.toDisk(schema)
92
92
 
93
- const db = HyperDB.from(SCHEMA_DIR, DB_DIR)
93
+ const db = HyperDB.from(SCHEMA_DIR, DB_DIR, { offset: 64 })
94
94
  const blindDB = db.namespace('blind-peer')
95
95
 
96
96
  blindDB.collections.register({
@@ -4,17 +4,18 @@ import c from 'compact-encoding'
4
4
  import Corestore from 'corestore'
5
5
  import Hyperswarm from 'hyperswarm'
6
6
  import debounce from 'debounceify'
7
+ import IdEnc from 'hypercore-id-encoding'
7
8
 
8
9
  const base = new Autobase(new Corestore('/tmp/my-corestore'), {
9
10
  encryptionKey: Buffer.alloc(30).fill('secret'),
10
- valueEncoding: c.json,
11
11
  open (store) {
12
12
  return store.get('view', { valueEncoding: c.json })
13
13
  },
14
14
  async apply (nodes, view, base) {
15
15
  for (const node of nodes) {
16
- if (node.value.add) await base.addWriter(Buffer.from(node.value.key, 'hex'), { indexer: false })
17
- view.append(node.value)
16
+ const jsonValue = JSON.parse(node.value.toString())
17
+ if (jsonValue.add) await base.addWriter(Buffer.from(jsonValue.key, 'hex'), { indexer: false })
18
+ view.append(jsonValue)
18
19
  }
19
20
  }
20
21
  })
@@ -31,7 +32,7 @@ base.view.on('append', debounce(async function () {
31
32
  }))
32
33
 
33
34
  // TODO: record in autobase
34
- const publicKey = Buffer.from(process.argv[2], 'hex')
35
+ const publicKey = IdEnc.decode(process.argv[2])
35
36
 
36
37
  const s = new Hyperswarm({ keyPair: await base.store.createKeyPair('tmp') })
37
38
 
@@ -45,7 +46,10 @@ s.on('connection', async c => {
45
46
  const info = await peer.addMailbox({ autobase: base.key })
46
47
 
47
48
  if (info.open === false) {
48
- await base.append({ add: true, key: info.writer.toString('hex') })
49
+ const message = Buffer.from(
50
+ JSON.stringify({ add: true, key: info.writer.toString('hex') })
51
+ )
52
+ await base.append(message)
49
53
  await base.update()
50
54
 
51
55
  const core = base.store.get({ key: info.writer, active: false })
package/example/post.mjs CHANGED
@@ -1,9 +1,13 @@
1
1
  import BlindPeerClient from '../client.js'
2
2
  import Hyperswarm from 'hyperswarm'
3
+ import IdEnc from 'hypercore-id-encoding'
3
4
 
4
- const publicKey = Buffer.from(process.argv[2], 'hex')
5
- const autobase = Buffer.from(process.argv[3], 'hex')
6
- const message = process.argv[4]
5
+ const publicKey = IdEnc.decode(process.argv[2])
6
+ const autobase = IdEnc.decode(process.argv[3])
7
+ const rawMessage = process.argv[4]
8
+ const message = Buffer.from(
9
+ JSON.stringify({ mailbox: true, message: rawMessage })
10
+ )
7
11
 
8
12
  const s = new Hyperswarm()
9
13
 
package/index.js CHANGED
@@ -56,8 +56,7 @@ module.exports = class BlindPeer {
56
56
 
57
57
  const w = new AutobaseLightWriter(this.store.namespace(entry.autobase), entry.autobase, {
58
58
  active: false,
59
- blockEncryptionKey: entry.blockEncryptionKey,
60
- valueEncoding: c.json
59
+ blockEncryptionKey: entry.blockEncryptionKey
61
60
  })
62
61
 
63
62
  for (const peer of core.peers) {
@@ -120,10 +119,9 @@ module.exports = class BlindPeer {
120
119
 
121
120
  const w = new AutobaseLightWriter(this.store.namespace(autobase), autobase, {
122
121
  active: false,
123
- blockEncryptionKey: entry.blockEncryptionKey,
124
- valueEncoding: c.json
122
+ blockEncryptionKey: entry.blockEncryptionKey
125
123
  })
126
- await w.append({ mailbox: true, text: message })
124
+ await w.append(message)
127
125
  const length = w.local.length
128
126
  await w.close()
129
127
 
package/package.json CHANGED
@@ -1,14 +1,20 @@
1
1
  {
2
2
  "name": "blind-peer",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "WIP - nothing to see here",
5
5
  "main": "index.js",
6
+ "bin": {
7
+ "blind-peer": "bin.js"
8
+ },
6
9
  "dependencies": {
7
10
  "autobase-light-writer": "^1.1.0",
8
11
  "corestore": "^6.18.4",
12
+ "graceful-goodbye": "^1.3.2",
13
+ "hypercore-id-encoding": "^1.3.0",
9
14
  "hyperdb": "^4.1.2",
10
15
  "hyperschema": "^1.0.3",
11
16
  "hyperswarm": "^4.8.4",
17
+ "paparam": "^1.6.1",
12
18
  "protomux-rpc": "^1.6.0"
13
19
  },
14
20
  "devDependencies": {
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "version": 0,
3
- "offset": 0,
3
+ "offset": 64,
4
4
  "schema": [
5
5
  {
6
6
  "name": "mailbox",
7
7
  "namespace": "blind-peer",
8
- "id": 0,
8
+ "id": 64,
9
9
  "type": 1,
10
10
  "indexes": [],
11
11
  "schema": "@blind-peer/mailbox",
@@ -6,18 +6,18 @@ const { IndexEncoder, c } = require('hyperdb/runtime')
6
6
  const { version, resolveStruct } = require('./messages.js')
7
7
 
8
8
  // '@blind-peer/mailbox' collection key
9
- const collection0_key = new IndexEncoder([
9
+ const collection64_key = new IndexEncoder([
10
10
  IndexEncoder.BUFFER
11
- ], { prefix: 0 })
11
+ ], { prefix: 64 })
12
12
 
13
- function collection0_indexify (record) {
13
+ function collection64_indexify (record) {
14
14
  const a = record.autobase
15
15
  return a === undefined ? [] : [a]
16
16
  }
17
17
 
18
18
  // '@blind-peer/mailbox' reconstruction function
19
- function collection0_reconstruct (version, keyBuf, valueBuf) {
20
- const key = collection0_key.decode(keyBuf)
19
+ function collection64_reconstruct (version, keyBuf, valueBuf) {
20
+ const key = collection64_key.decode(keyBuf)
21
21
  const value = c.decode(resolveStruct('@blind-peer/mailbox/value', version), valueBuf)
22
22
  // TODO: This should be fully code generated
23
23
  return {
@@ -26,42 +26,42 @@ function collection0_reconstruct (version, keyBuf, valueBuf) {
26
26
  }
27
27
  }
28
28
  // '@blind-peer/mailbox' key reconstruction function
29
- function collection0_reconstruct_key (keyBuf) {
30
- const key = collection0_key.decode(keyBuf)
29
+ function collection64_reconstruct_key (keyBuf) {
30
+ const key = collection64_key.decode(keyBuf)
31
31
  return {
32
32
  autobase: key[0]
33
33
  }
34
34
  }
35
35
 
36
36
  // '@blind-peer/mailbox'
37
- const collection0 = {
37
+ const collection64 = {
38
38
  name: '@blind-peer/mailbox',
39
- id: 0,
39
+ id: 64,
40
40
  encodeKey (record) {
41
41
  const key = [record.autobase]
42
- return collection0_key.encode(key)
42
+ return collection64_key.encode(key)
43
43
  },
44
44
  encodeKeyRange ({ gt, lt, gte, lte } = {}) {
45
- return collection0_key.encodeRange({
46
- gt: gt ? collection0_indexify(gt) : null,
47
- lt: lt ? collection0_indexify(lt) : null,
48
- gte: gte ? collection0_indexify(gte) : null,
49
- lte: lte ? collection0_indexify(lte) : null
45
+ return collection64_key.encodeRange({
46
+ gt: gt ? collection64_indexify(gt) : null,
47
+ lt: lt ? collection64_indexify(lt) : null,
48
+ gte: gte ? collection64_indexify(gte) : null,
49
+ lte: lte ? collection64_indexify(lte) : null
50
50
  })
51
51
  },
52
52
  encodeValue (version, record) {
53
53
  return c.encode(resolveStruct('@blind-peer/mailbox/value', version), record)
54
54
  },
55
55
  trigger: null,
56
- reconstruct: collection0_reconstruct,
57
- reconstructKey: collection0_reconstruct_key,
56
+ reconstruct: collection64_reconstruct,
57
+ reconstructKey: collection64_reconstruct_key,
58
58
  indexes: []
59
59
  }
60
60
 
61
61
  module.exports = {
62
62
  version,
63
63
  collections: [
64
- collection0
64
+ collection64
65
65
  ],
66
66
  indexes: [
67
67
  ],
@@ -71,7 +71,7 @@ module.exports = {
71
71
 
72
72
  function resolveCollection (name) {
73
73
  switch (name) {
74
- case '@blind-peer/mailbox': return collection0
74
+ case '@blind-peer/mailbox': return collection64
75
75
  default: return null
76
76
  }
77
77
  }
@@ -86,7 +86,7 @@ const encoding2 = {
86
86
  c.fixed32.preencode(state, m.autobase)
87
87
  c.uint.preencode(state, flags)
88
88
 
89
- if (m.message) c.string.preencode(state, m.message)
89
+ if (m.message) c.buffer.preencode(state, m.message)
90
90
  },
91
91
  encode (state, m) {
92
92
  let flags = 0
@@ -95,7 +95,7 @@ const encoding2 = {
95
95
  c.fixed32.encode(state, m.autobase)
96
96
  c.uint.encode(state, flags)
97
97
 
98
- if (m.message) c.string.encode(state, m.message)
98
+ if (m.message) c.buffer.encode(state, m.message)
99
99
  },
100
100
  decode (state) {
101
101
  const res = {}
@@ -105,7 +105,7 @@ const encoding2 = {
105
105
  res.autobase = c.fixed32.decode(state)
106
106
 
107
107
  const flags = state.start < state.end ? c.uint.decode(state) : 0
108
- if ((flags & 1) !== 0) res.message = c.string.decode(state)
108
+ if ((flags & 1) !== 0) res.message = c.buffer.decode(state)
109
109
 
110
110
  return res
111
111
  }
@@ -86,7 +86,7 @@ const encoding2 = {
86
86
  c.fixed32.preencode(state, m.autobase)
87
87
  c.uint.preencode(state, flags)
88
88
 
89
- if (m.message) c.string.preencode(state, m.message)
89
+ if (m.message) c.buffer.preencode(state, m.message)
90
90
  },
91
91
  encode (state, m) {
92
92
  let flags = 0
@@ -95,7 +95,7 @@ const encoding2 = {
95
95
  c.fixed32.encode(state, m.autobase)
96
96
  c.uint.encode(state, flags)
97
97
 
98
- if (m.message) c.string.encode(state, m.message)
98
+ if (m.message) c.buffer.encode(state, m.message)
99
99
  },
100
100
  decode (state) {
101
101
  const res = {}
@@ -105,7 +105,7 @@ const encoding2 = {
105
105
  res.autobase = c.fixed32.decode(state)
106
106
 
107
107
  const flags = state.start < state.end ? c.uint.decode(state) : 0
108
- if ((flags & 1) !== 0) res.message = c.string.decode(state)
108
+ if ((flags & 1) !== 0) res.message = c.buffer.decode(state)
109
109
 
110
110
  return res
111
111
  }
@@ -59,7 +59,7 @@
59
59
  },
60
60
  {
61
61
  "name": "message",
62
- "type": "string",
62
+ "type": "buffer",
63
63
  "version": 1
64
64
  }
65
65
  ]