forked from beakerbrowser/dat-session-data-ext-msg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
226 lines (191 loc) · 5.28 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const EventEmitter = require('events')
const {BufferTooLargeError} = require('./errors')
// exported api
// =
class DatSessionDataExtMsg 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, 'session-data')
}
}
return false
}
// get all stored session datas
getSessionDatas (dat) {
var {watcher} = this.getWatcher(dat)
return (watcher) ? watcher.sessionDatas : {}
}
// get a peer's session data
getSessionData (dat, remoteId) {
remoteId = toRemoteId(remoteId)
var sessionDatas = this.getSessionDatas(dat)
return sessionDatas[toStr(remoteId)]
}
// get my session data
getLocalSessionData (dat) {
var {watcher} = this.getWatcher(dat)
if (watcher) {
return watcher.localSessionData
}
}
// set and broadcast my session data
setLocalSessionData (dat, sessionData) {
var {watcher} = this.getWatcher(dat)
if (watcher) {
watcher.setLocalSessionData(sessionData)
watcher.broadcastLocalSessionData()
}
}
// send my session data to the given peer
sendLocalSessionData (dat, remoteId) {
var {watcher} = this.getWatcher(dat)
if (watcher) {
watcher.sendLocalSessionData(remoteId)
}
}
}
exports.DatSessionDataExtMsg = DatSessionDataExtMsg
// internal
// =
// helper class to track individual dats
class DatWatcher {
constructor (dat, emitter) {
this.dat = dat
this.emitter = emitter
this.localSessionData = undefined
this.sessionDatas = {}
this.onPeerAdd = this.onPeerAdd.bind(this)
this.onPeerRemove = this.onPeerRemove.bind(this)
}
setLocalSessionData (sessionData) {
if (typeof sessionData === 'string') {
sessionData = Buffer.from(sessionData, 'utf8')
}
// validate
if (sessionData) {
if (Buffer.byteLength(sessionData) > 256) {
throw new BufferTooLargeError()
}
} else {
sessionData = Buffer.from([])
}
// store locally
this.localSessionData = sessionData
}
broadcastLocalSessionData () {
// send to peers
var peers = this.ddatabase.peers
for (let i = 0; i < peers.length; i++) {
if (remoteSupports(peers[i], 'session-data')) {
getPeerFeedStream(peers[i]).extension('session-data', this.localSessionData)
}
}
}
sendLocalSessionData (remoteId) {
var peer = this.getPeer(remoteId)
if (remoteSupports(peer, 'session-data')) {
getPeerFeedStream(peer).extension('session-data', this.localSessionData)
}
}
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
// otherwise assume dat is a ddatabase already
return this.dat.metadata ? this.dat.metadata : this.dat
}
getPeer (remoteId) {
remoteId = toRemoteId(remoteId)
return this.ddatabase.peers.find(p => isSameId(remoteId, toRemoteId(p)))
}
onPeerAdd (peer) {
getPeerFeedStream(peer).on('extension', (type, payload) => {
// handle session-data messages only
if (type !== 'session-data') return
// enforce 256-byte limit
if (payload) {
if (Buffer.byteLength(payload) > 256) {
// truncate
payload = payload.slice(0, 256)
}
}
// store
this.sessionDatas[toStr(toRemoteId(peer))] = payload
// emit
this.emitter.emit('session-data', this.dat, peer, payload)
})
}
onPeerRemove (peer) {
// unstore session data
delete this.sessionDatas[toStr(toRemoteId(peer))]
}
}
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)
}