-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmirror.js
executable file
·47 lines (40 loc) · 1.46 KB
/
mirror.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
#!/usr/bin/env node
const Corestore = require('corestore')
const Networker = require('@corestore/networker')
const { Header } = require('./lib/messages')
const keys = process.argv.slice(2).filter(k => /^[0-9a-fA-F]{64}$/.test(k)).map(k => Buffer.from(k, 'hex'))
const store = new Corestore('./hyperupdate-mirror')
const down = { releases: 0, chunks: 0 }
const up = { releases: 0, chunks: 0 }
store.ready(function () {
const network = new Networker(store)
const feeds = keys.map(k => store.get(k))
for (const feed of feeds) {
console.log('Mirroring release feed: ' + feed.key.toString('hex'))
network.configure(feed.discoveryKey, { announce: true, lookup: true })
feed.download({ start: 0, end: -1 })
feed.on('download', () => down.releases++)
feed.on('upload', () => up.releases++)
feed.get(0, { ifAvailable: false, wait: true }, function (_, data) {
if (!data) return
const c = parseChunkFeed(data)
if (!c) return
const chunkFeed = store.get(c)
console.log('Mirroring chunk feed: ' + c.toString('hex'))
chunkFeed.download({ start: 0, end: -1 })
chunkFeed.on('download', () => down.chunks++)
chunkFeed.on('upload', () => up.chunks++)
})
}
})
function parseChunkFeed (data) {
try {
const h = Header.decode(data)
if (h.protocol === 'hyperupdate') return h.chunkFeed
} catch (_) {
return null
}
}
setInterval(function () {
console.log('Current stats', { down, up })
}, 5000).unref()