-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirror.js
66 lines (49 loc) · 1.51 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const process = require('process')
const path = require('path')
const Corestore = require('corestore')
const Hyperswarm = require('hyperswarm')
const Hyperdrive = require('hyperdrive')
const Localdrive = require('localdrive')
const id = require('hypercore-id-encoding')
const [
cwd,
prefix,
checkout,
source,
destination
] = process.argv.slice(2)
const symbols = {
add: '+',
remove: '-',
change: '~'
}
mirror(source, destination)
async function mirror (source, destination) {
const store = new Corestore(path.resolve(cwd, 'corestore'))
const swarm = new Hyperswarm().on('connection', (socket) => store.replicate(socket))
source = await open(source, { store, swarm, cwd })
destination = await open(destination, { store, swarm, cwd })
if (checkout) source = source.checkout(+checkout)
for await (const entry of source.mirror(destination, { prefix, prune: false })) {
console.log(`${symbols[entry.op]} ${entry.key}`)
}
await swarm.destroy()
}
async function open (key, opts = {}) {
const { store, swarm, cwd } = opts
if (!isKey(key)) return new Localdrive(path.resolve(cwd, key))
const drive = new Hyperdrive(store, id.decode(key))
await drive.ready()
if (swarm) {
swarm.join(drive.discoveryKey)
const done = store.findingPeers()
swarm
.flush()
.then(done)
await drive.core.update()
}
return drive
}
function isKey (key) {
return (key.length === 52 || key.length === 64) && key.indexOf('/') === -1 && key.indexOf('\\') === -1 && key.indexOf('.') === -1
}