-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (109 loc) · 2.78 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'use strict'
const hyperdb = require('hyperdb')
const createUI = require('really-basic-chat-ui')
const findPeers = require('./lib/find-peers')
const validKey = /^[0-9a-f]{64}$/i
const onceAuthorized = (db, key, cb) => {
const check = () => {
db.authorized(key, (err, isAuthorized) => {
if (err) cb(err)
else if (isAuthorized === true) cb(null)
else db.once('append', check) // todo: correct event?
})
}
setImmediate(check)
}
const onReady = (db, name) => {
findPeers(db.discoveryKey, (peer) => {
const s = db.replicate({live: true})
s.pipe(peer).pipe(s)
})
db.put('names/' + db.local.key.toString('hex'), name, (err) => {
// todo: handle error
})
// state
const msgs = [{
from: name,
when: Date.now(),
content: `The key for this chat is ${db.key.toString('hex')}.`
}]
let unauthorizedError = `Tell someone to authorize ${db.local.key.toString('hex')}.`
let err = null
const send = (content) => {
if (content.slice(0, 11) === ':authorize ') {
let key = content.slice(11).trim()
if (!validKey.test(key)) {
err = 'Invalid key.'
rerender()
return
}
key = Buffer.from(key, 'hex')
db.authorize(key, (err) => {
if (err) return null // todo: handle error
msgs.push({
from: name,
when: Date.now(),
content: `I authorized ${key.toString('hex')}.`
})
rerender()
})
} else {
const from = db.local.key.toString('hex')
const t = Date.now()
const val = {from, when: t, content}
db.put('msgs/' + t + '-' + from, val, (err) => {
if (err) return null // todo
// todo: handle error
})
}
}
const render = createUI(send)
const rerender = () => {
render(db.opened, msgs, unauthorizedError || err || null)
}
setImmediate(rerender)
onceAuthorized(db, db.local.key, (err) => {
if (err) return null // todo
unauthorizedError = null
rerender()
})
const seen = Object.create(null)
const onRow = (row) => {
if (seen[row.key]) return null
seen[row.key] = true
const key = row.key.split('/')
if (key[0] !== 'msgs') return
const peer = (key[1] || '').split('-')[1]
db.get('names/' + peer, (err, name) => {
if (err) return null // todo: handle error
msgs.push({
from: name && name.value || 'oops',
when: row.value.when,
content: row.value.content
})
rerender()
})
}
const onAppend = (feed) => {
const h = db.createHistoryStream({reverse: true})
h.once('data', (row) => {
h.destroy()
onRow(row)
})
}
db.on('remote-update', onAppend)
db.on('append', onAppend)
// todo: mafintosh/hyperdb#92
db.createHistoryStream().on('data', onRow)
}
const openChat = (dir, key, name) => {
const db = hyperdb(dir, key, {
firstNode: true,
valueEncoding: 'json'
})
db.ready((err) => {
if (err) return null // todo
onReady(db, name)
})
}
module.exports = openChat