-
Notifications
You must be signed in to change notification settings - Fork 2
/
headless.js
executable file
·139 lines (123 loc) · 3.35 KB
/
headless.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
130
131
132
133
134
135
136
137
138
139
var Cabal = require('cabal-core')
var thunky = require('thunky')
var os = require('os')
var tools = require('./tools')
var homedir = os.homedir()
var rootdir = (homedir + `/.cabal/v${Cabal.databaseVersion}`)
var archivesdir = `${rootdir}/archives/`
var ram = require('random-access-memory')
console.log(archivesdir);
module.exports = Headless
function Headless (key, opts) {
if (!(this instanceof Headless)) return new Headless(key, opts)
if (!opts) opts = {}
this.peerlist = new Set()
this.key = tools.scrub(key)
this.db = opts.temp ? ram : archivesdir + this.key
this.cabal = Cabal(this.db, this.key)
this.starttime = opts.completeLog ? 0 : Date.now()
this.instance = thunky((cb) => this.cabal.ready(cb))
this.instance(() => {
this.cabal.on('peer-added', (peer) => this._addPeer(peer))
this.cabal.on('peer-removed', (peer) => this._removePeer(peer))
})
}
Headless.prototype.post = function (opts) {
if (!opts) return
if (typeof opts === 'string') { opts = { message: opts } }
if (!opts.messageType) { opts.messageType = 'chat/text' }
if (!opts.channel) { opts.channel = 'default' }
this.instance(() => {
this.cabal.publish({
type: opts.messageType,
content: {
channel: opts.channel,
text: opts.message
}
})
})
}
Headless.prototype._addPeer = function (peer, cb) {
if (!cb) { cb = tools.noop }
this.instance(() => {
this.cabal.getLocalKey((err, local) => {
if (err) throw err
if (peer === local) return
this.peerlist.add(peer)
cb(peer)
})
})
}
Headless.prototype._removePeer = function (peer, cb) {
if (!cb) { cb = tools.noop }
this.instance(() => {
this.cabal.getLocalKey((err, local) => {
if (err) throw err
if (peer === local) return
this.peerlist.delete(peer)
cb(peer)
})
})
}
Headless.prototype._msgRecv = function (data, cb) {
this.instance(() => {
this.cabal.getLocalKey((err, local) => {
if (err) throw err
if (data.key === local || parseInt(data.value.timestamp) < this.starttime) return
cb(data)
})
})
}
Headless.prototype.nick = function (nick) {
this.instance(() => this.cabal.publishNick(nick))
}
// join swarm
Headless.prototype.connect = function () {
//console.log("connecting!");
this.instance(() => {
if (!this.swarm) {
this.cabal.swarm((err, swarm) => {
if (err) throw err
this.swarm = swarm
})
}
})
}
// leave swarm
Headless.prototype.disconnect = function () {
this.instance(() => {
if (this.swarm) {
this.swarm.close()
this.swarm = null
}
})
}
Headless.prototype.onPeerConnected = function (cb) {
this.instance(() => {
this.cabal.on('peer-added', (peer) => this._addPeer(peer, cb))
})
}
Headless.prototype.onPeerDisconnected = function (cb) {
this.instance(() => {
this.cabal.on('peer-dropped', (peer) => this._removePeer(peer, cb))
})
}
Headless.prototype.onMessageReceived = function (cb) {
this.instance(() => {
this.cabal.messages.events.on('message', (data) => {
//console.log(data);
this._msgRecv(data, cb);
})
})
}
Headless.prototype.id = function (cb) {
this.instance(() => {
this.cabal.getLocalKey((err, key) => {
if (err) throw err
cb(key)
})
})
}
Headless.prototype.peers = function () {
return Array.from(this.peerlist)
}