forked from beakerbrowser/dat-ephemeral-ext-msg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
211 lines (178 loc) · 4.72 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const EventEmitter = require('events')
const encodings = require('./encodings')
// exported api
// =
class DatEphemeralExtMsg extends EventEmitter {
constructor () {
super()
this.datWatchers = {}
}
getWatcher (dat) {
var key = toStr(dat.key)
return {key, watcher: this.datWatchers[key]}
}
watchDat (dat) {
var {key, watcher} = this.getWatcher(dat)
if (!watcher) {
watcher = this.datWatchers[key] = new DatWatcher(dat, this)
watcher.listen()
}
}
unwatchDat (dat) {
var {key, watcher} = this.getWatcher(dat)
if (watcher) {
watcher.unlisten()
delete this.datWatchers[key]
}
}
// does the given peer have protocol support?
hasSupport (dat, remoteId) {
var {watcher} = this.getWatcher(dat)
if (watcher) {
var peer = watcher.getPeer(remoteId)
if (peer) {
return remoteSupports(peer, 'ephemeral')
}
}
return false
}
// send a message to a peer
send (dat, remoteId, message) {
var {watcher} = this.getWatcher(dat)
if (watcher) {
return watcher.send(remoteId, message)
}
}
// send a message to all peers
broadcast (dat, message) {
var {watcher} = this.getWatcher(dat)
if (watcher) {
return watcher.broadcast(message)
}
}
}
exports.DatEphemeralExtMsg = DatEphemeralExtMsg
// internal
// =
// helper class to track individual dats
class DatWatcher {
constructor (dat, emitter) {
this.dat = dat
this.emitter = emitter
this.onPeerAdd = this.onPeerAdd.bind(this)
this.onPeerRemove = this.onPeerRemove.bind(this)
}
send (remoteId, message = {}) {
// get peer and assure support exists
var peer = this.getPeer(remoteId)
if (!remoteSupports(peer, 'ephemeral')) {
return
}
// send
message = serialize(message)
getPeerFeedStream(peer).extension('ephemeral', message)
}
broadcast (message) {
// serialize message
message = serialize(message)
// send to all peers
var peers = this.ddatabase.peers
for (let i = 0; i < peers.length; i++) {
this.send(peers[i], message)
}
}
listen () {
this.ddatabase.on('peer-add', this.onPeerAdd)
this.ddatabase.on('peer-remove', this.onPeerRemove)
}
unlisten () {
this.ddatabase.removeListener('peer-add', this.onPeerAdd)
this.ddatabase.removeListener('peer-remove', this.onPeerRemove)
}
get ddatabase () {
// if dat is a dwebfs, use the metadata ddatabase,
// if it’s hyperdb, use the source ddatabase,
// otherwise assume dat is a ddatabase already
if (this.dat.metadata) {
return this.dat.metadata
} else if (this.dat.source) {
return this.dat.source
} else {
return this.dat
}
}
getPeer (remoteId) {
remoteId = toRemoteId(remoteId)
return this.ddatabase.peers.find(p => isSameId(remoteId, toRemoteId(p)))
}
onPeerAdd (peer) {
getPeerFeedStream(peer).on('extension', (type, message) => {
// handle ephemeral messages only
if (type !== 'ephemeral') return
try {
// decode
message = encodings.EphemeralMessage.decode(message)
// emit
this.emitter.emit('message', this.dat, peer, message)
} catch (e) {
this.emitter.emit('received-bad-message', e, this.dat, peer, message)
}
})
}
onPeerRemove (peer) {
// TODO needed?
}
}
function serialize (message) {
if (Buffer.isBuffer(message)) {
return message // already encoded
}
// massage values
if (!message.payload) {
message.payload = Buffer.from([])
} else if (typeof message.payload === 'string') {
message.payload = Buffer.from(message.payload, 'utf8')
}
if (typeof message.contentType !== 'string') {
message.contentType = undefined
}
// serialize
return encodings.EphemeralMessage.encode(message)
}
function getPeerFeedStream (peer) {
if (!peer) return null
return peer.stream
}
function getPeerProtocolStream (peer) {
var feedStream = getPeerFeedStream(peer)
if (!feedStream) return null
return feedStream.stream
}
function getPeerRemoteId (peer) {
var protocolStream = getPeerProtocolStream(peer)
if (!protocolStream) return null
return protocolStream.remoteId
}
function remoteSupports (peer, ext) {
var protocolStream = getPeerProtocolStream(peer)
if (!protocolStream) return false
return protocolStream.remoteSupports(ext)
}
function toRemoteId (peer) {
if (peer && typeof peer === 'object') {
return getPeerRemoteId(peer)
}
return peer
}
function toStr (buf) {
if (!buf) return buf
if (Buffer.isBuffer(buf)) return buf.toString('hex')
return buf
}
function isSameId (a, b) {
if (!a || !b) return false
if (Buffer.isBuffer(a) && Buffer.isBuffer(b)) {
return a.equals(b)
}
return toStr(a) === toStr(b)
}