blind-peer 0.0.2 → 0.0.4
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 +53 -0
- package/build.js +1 -1
- package/example/autobase.mjs +2 -1
- package/example/post.mjs +3 -2
- package/index.js +11 -2
- package/package.json +7 -1
- package/spec/hyperdb/db.json +2 -2
- package/spec/hyperdb/index.js +19 -19
package/bin.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
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
|
+
blindPeer.on('add-request', req => {
|
|
17
|
+
try {
|
|
18
|
+
console.log(`Add-mailbox request received for autobase ${idEnc.normalize(req.autobase)}`)
|
|
19
|
+
} catch {
|
|
20
|
+
console.log('Invalid add-mailbox request received')
|
|
21
|
+
console.log(req)
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
blindPeer.on('post-request', req => {
|
|
26
|
+
try {
|
|
27
|
+
console.log(`Post request received to autobase ${idEnc.normalize(req.autobase)}`)
|
|
28
|
+
} catch {
|
|
29
|
+
console.log('Invalid post request received')
|
|
30
|
+
console.log(req)
|
|
31
|
+
}
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
console.info(`Using storage '${storage}'`)
|
|
35
|
+
|
|
36
|
+
goodbye(async () => {
|
|
37
|
+
console.info('Shutting down blind peer')
|
|
38
|
+
await blindPeer.close()
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
await blindPeer.listen()
|
|
42
|
+
|
|
43
|
+
blindPeer.swarm.on('connection', (conn, peerInfo) => {
|
|
44
|
+
const key = idEnc.normalize(peerInfo.publicKey)
|
|
45
|
+
console.log(`Opened connection to ${key}`)
|
|
46
|
+
conn.on('close', () => console.log(`Closed connection to ${key}`))
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
console.info(`Listening at ${idEnc.normalize(blindPeer.publicKey)}`)
|
|
50
|
+
}
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
cmd.parse()
|
package/build.js
CHANGED
package/example/autobase.mjs
CHANGED
|
@@ -4,6 +4,7 @@ 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'),
|
|
@@ -31,7 +32,7 @@ base.view.on('append', debounce(async function () {
|
|
|
31
32
|
}))
|
|
32
33
|
|
|
33
34
|
// TODO: record in autobase
|
|
34
|
-
const publicKey =
|
|
35
|
+
const publicKey = IdEnc.decode(process.argv[2])
|
|
35
36
|
|
|
36
37
|
const s = new Hyperswarm({ keyPair: await base.store.createKeyPair('tmp') })
|
|
37
38
|
|
package/example/post.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
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 =
|
|
5
|
-
const autobase =
|
|
5
|
+
const publicKey = IdEnc.decode(process.argv[2])
|
|
6
|
+
const autobase = IdEnc.decode(process.argv[3])
|
|
6
7
|
const rawMessage = process.argv[4]
|
|
7
8
|
const message = Buffer.from(
|
|
8
9
|
JSON.stringify({ mailbox: true, message: rawMessage })
|
package/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const { EventEmitter } = require('events')
|
|
1
2
|
const AutobaseLightWriter = require('autobase-light-writer')
|
|
2
3
|
const HyperDB = require('hyperdb')
|
|
3
4
|
const Corestore = require('corestore')
|
|
@@ -8,8 +9,10 @@ const Hyperswarm = require('hyperswarm')
|
|
|
8
9
|
const ProtomuxRPC = require('protomux-rpc')
|
|
9
10
|
const c = require('compact-encoding')
|
|
10
11
|
|
|
11
|
-
module.exports = class BlindPeer {
|
|
12
|
+
module.exports = class BlindPeer extends EventEmitter {
|
|
12
13
|
constructor (storage) {
|
|
14
|
+
super()
|
|
15
|
+
|
|
13
16
|
this.db = HyperDB.rocks(path.join(storage, 'hyperdb'), definition)
|
|
14
17
|
this.store = new Corestore(path.join(storage, 'corestore'))
|
|
15
18
|
this.store.on('core-open', this._oncoreopen.bind(this))
|
|
@@ -36,7 +39,9 @@ module.exports = class BlindPeer {
|
|
|
36
39
|
}
|
|
37
40
|
|
|
38
41
|
async _onrpcadd (req) {
|
|
42
|
+
this.emit('add-request', req)
|
|
39
43
|
const res = await this.add(req)
|
|
44
|
+
this.emit('add-response', req, res)
|
|
40
45
|
|
|
41
46
|
return {
|
|
42
47
|
autobase: res.autobase,
|
|
@@ -46,7 +51,11 @@ module.exports = class BlindPeer {
|
|
|
46
51
|
}
|
|
47
52
|
|
|
48
53
|
async _onrpcpost (req) {
|
|
49
|
-
|
|
54
|
+
this.emit('post-request', req)
|
|
55
|
+
const res = await this.post(req)
|
|
56
|
+
this.emit('post-response', req, res)
|
|
57
|
+
|
|
58
|
+
return res
|
|
50
59
|
}
|
|
51
60
|
|
|
52
61
|
async _oncoreopen (core) {
|
package/package.json
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "blind-peer",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
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": {
|
package/spec/hyperdb/db.json
CHANGED
package/spec/hyperdb/index.js
CHANGED
|
@@ -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
|
|
9
|
+
const collection64_key = new IndexEncoder([
|
|
10
10
|
IndexEncoder.BUFFER
|
|
11
|
-
], { prefix:
|
|
11
|
+
], { prefix: 64 })
|
|
12
12
|
|
|
13
|
-
function
|
|
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
|
|
20
|
-
const key =
|
|
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
|
|
30
|
-
const key =
|
|
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
|
|
37
|
+
const collection64 = {
|
|
38
38
|
name: '@blind-peer/mailbox',
|
|
39
|
-
id:
|
|
39
|
+
id: 64,
|
|
40
40
|
encodeKey (record) {
|
|
41
41
|
const key = [record.autobase]
|
|
42
|
-
return
|
|
42
|
+
return collection64_key.encode(key)
|
|
43
43
|
},
|
|
44
44
|
encodeKeyRange ({ gt, lt, gte, lte } = {}) {
|
|
45
|
-
return
|
|
46
|
-
gt: gt ?
|
|
47
|
-
lt: lt ?
|
|
48
|
-
gte: gte ?
|
|
49
|
-
lte: lte ?
|
|
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:
|
|
57
|
-
reconstructKey:
|
|
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
|
-
|
|
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
|
|
74
|
+
case '@blind-peer/mailbox': return collection64
|
|
75
75
|
default: return null
|
|
76
76
|
}
|
|
77
77
|
}
|