-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdat-mirror-share.js
More file actions
93 lines (83 loc) · 2.56 KB
/
dat-mirror-share.js
File metadata and controls
93 lines (83 loc) · 2.56 KB
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
#!/usr/bin/env node
var program = require('commander')
var discovery = require('discovery-swarm')
var hypercore = require('hypercore')
var multifeed = require('multifeed')
var pump = require('pump')
var getConfig = require('./client/config')
const chalk = require('chalk')
const constants = require('./constants')
const dataFile = require('./data-file-path')
const ProgressBar = require('ascii-progress')
program
.version('1.0.0')
.usage('<dat>')
.option('--subdomain <subdomain>', 'Subdomain for http mirroring')
.option('--wait', 'Wait for server to finish syncing before exiting')
.action(function(dat) {
program.dat = dat
})
.parse(process.argv)
async function run() {
const config = await getConfig()
const datString = program.dat
const regex = /[0-9a-fA-F]{64}/g
const matches = datString.match(regex)
if (!matches || matches.length == 0) {
console.error(chalk.red("Need a 64 char dat address"))
process.exit(1)
}
const dat = matches[0]
var multi = multifeed(hypercore, dataFile('dat-mirror-client.db'), {
valueEncoding: 'json'
})
var swarm = discovery()
console.log(`Sharing: ${chalk.green(dat)}`)
console.log(`Server : ${config.mirrorKey}`)
let progressBar = null
multi.ready(feeds => {
const listenToFeed = feed => {
feed.createReadStream({live: true})
.on('data', data => {
if (data.type == constants.SYNC_PROGRESS &&
data.datKey == dat) {
if (!progressBar) {
progressBar = new ProgressBar()
}
progressBar.update(data.percent)
if (data.percent >= 1) {
console.log("Dat successfully synced!")
process.exit()
}
}
if (data.type == constants.CONFIRM_MIRROR &&
data.datKey == dat) {
console.log("Share command synced to server!")
if (!program.wait) {
process.exit()
}
}
})
}
multi.feeds().forEach(listenToFeed)
multi.on('feed', listenToFeed)
multi.writer('local', (err, feed) => {
const msg = {
type: constants.ADD_MIRROR,
datKey: dat,
wait: program.wait
}
if (program.subdomain) {
msg.subdomain = program.subdomain
}
feed.append(msg)
console.log(`Added to local hyperlog, waiting for response from server`)
swarm.join(config.mirrorKey)
swarm.on('connection', function (connection) {
console.log('Connected to peer, syncing...')
pump(connection, multi.replicate({ live: true }), connection)
})
})
})
}
run()