-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
85 lines (66 loc) · 2.09 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
module.exports = function (feed, res) {
res.setHeader('Content-Type', 'text/event-stream; charset=utf-8')
var archive = feed.metadata ? feed : null
if (archive) {
feed = archive.metadata
}
var key = feed.key.toString('hex')
send(res, {type: 'key', key: key})
feed.ready(function () {
if (archive) track(feed, 'metadata')
else track(feed, null)
})
send(res, {type: 'peer-update', peers: feed.peers.length})
feed.on('peer-add', onpeeradd)
feed.on('peer-remove', onpeerremove)
if (archive) {
if (archive.content) {
track(archive.content, 'content')
} else {
archive.on('content', function () {
track(archive.content, 'content')
})
}
}
res.on('close', function () {
feed.removeListener('peer-add', onpeeradd)
feed.removeListener('peer-remove', onpeerremove)
})
function track (feed, name) {
send(res, {type: 'feed', name: name, key: key, blocks: bitfield(feed), bytes: feed.byteLength})
feed.on('update', onupdate)
feed.on('append', onupdate)
feed.on('download', ondownload)
feed.on('upload', onupload)
res.on('close', function () {
feed.removeListener('update', onupdate)
feed.removeListener('download', ondownload)
feed.removeListener('upload', onupload)
})
function onupdate () {
send(res, {type: 'update', name: name, key: key, blocks: bitfield(feed), bytes: feed.byteLength})
}
function ondownload (index, data) {
send(res, {type: 'download', name: name, index: index, bytes: data.length})
}
function onupload (index, data) {
send(res, {type: 'upload', name: name, index: index, bytes: data.length})
}
}
function onpeeradd () {
send(res, {type: 'peer-update', peers: feed.peers.length})
}
function onpeerremove () {
send(res, {type: 'peer-update', peers: feed.peers.length})
}
function bitfield (feed) {
var list = []
for (var i = 0; i < feed.length; i++) {
list.push(feed.has(i))
}
return list
}
function send (res, message) {
res.write('data: ' + JSON.stringify(message) + '\n\n')
}
}