-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhyperchat.js
119 lines (107 loc) · 3.01 KB
/
hyperchat.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
const hypercore = require('hypercore')
const hyperdiscovery = require('hyperdiscovery')
const Listener = require('./listener')
const fs = require('fs')
const path = require('path')
const events = require('events')
const homedir = require('os').homedir()
const chatsDirectory = path.resolve(homedir, './hyperchats')
class Hyperchat extends events.EventEmitter {
constructor (name) {
super()
this.name = name
try {
fs.statSync(chatsDirectory)
} catch (e) {
fs.mkdirSync(chatsDirectory)
}
this.feed = hypercore(path.join(chatsDirectory, name), { valueEncoding: 'json' })
this.listeningTo = []
this.ready = false
this.feed.on('ready', () => this.connect())
this.feed.on('error', e => console.error('feed error:', e))
this.swarm = undefined
}
get key () {
return this.feed.key.toString('hex')
}
get discoveryKey () {
return this.feed.discoveryKey.toString('hex')
}
connect () {
this.feed.append({ time: Date.now(), name: this.name, status: 'START' }, (err) => {
if (err) throw err
this.emit('ready')
this.ready = true
const archive = this.feed
this.swarm = hyperdiscovery(this.feed, {
stream: function (peer) {
const stream = archive.replicate({
live: true,
upload: true,
download: true,
userData: archive.key
})
stream.on('handshake', () => {
console.log('HANDSHAKE RECIEVER', stream.remoteUserData.toString('hex'))
})
return stream
}
})
this.swarm.once('connection', () => { this.emit('connection') })
})
}
disconnect (cb) {
const kill = () => {
if (count === 0 && this.swarm) {
this.feed.append({ time: Date.now(), name: this.name, status: 'END' }, () => {
setTimeout(() => this._destroy(cb), 40)
})
};
}
var count = this.listeningTo.length
if (count) {
this.listeningTo.forEach((remote) => {
this.emit('disconnecting', remote.key)
remote.disconnect(() => {
this.emit('disconnected', remote.key)
count--
kill()
})
})
} else {
kill()
}
}
chat (msg) {
if (this.ready) {
this.feed.append({ time: Date.now(), msg })
} else {
console.warn('Feed is not ready yet')
}
}
heard (discoveryKey, index) {
this.feed.append({ time: Date.now(), heard: discoveryKey, index, status: 'HEARD' })
}
add (key) {
const remote = new Listener(key, this)
// attach listener events
this.listeningTo.push(remote)
}
remove (key) {
const id = this.listeningTo.findIndex(e => e.key === key)
if (id >= 0) {
this.listeningTo[id].disconnect()
this.listeningTo.splice(id, 1)
} else {
console.warn('You dont seem to be connected to:', key)
}
}
_destroy (cb) {
this.swarm.leave(this.feed.discoveryKey)
this.swarm.destroy(cb)
this.swarm = undefined
this.emit('destroyed')
}
}
module.exports = Hyperchat