-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdat-mirror-serve.js
63 lines (56 loc) · 1.68 KB
/
dat-mirror-serve.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
#!/usr/bin/env node
const fs = require('fs')
const neatLog = require('neat-log')
const path = require('path')
const program = require('commander')
const mirrorViaHttp = require('./server/dat-http-mirror')
const express = require('express')
const seed = require('./server/seed-dat')
const { view } = require('./server/components')
const connect = require('./server/connect')
const config = require('./server/config')()
const dataFile = require('./data-file-path')
const { throttle } = require('throttle-debounce')
program
.option('-p, --port [port]', 'Port to start on', 3002)
.option('-d, --cachedir [cachedir]', "Directory of dat cache", dataFile('dat-mirror-seeds'))
.parse(process.argv)
const cache = program.cachedir
if (!fs.existsSync(cache)){
fs.mkdirSync(cache);
}
const app = express()
const ui = neatLog(view, {
fullscreen: true
})
ui.use(serve)
async function serve(state, bus) {
state.currentlyHosted = []
state.mirrorKey = config.mirrorKey
state.httpPort = program.port
const change = () => bus.emit('render')
const connection = connect(config.mirrorKey)
connection.on("mirror", async (opts) => {
const dir = path.join(cache, opts.datKey)
const datInfo = await seed(dir)
const httpInfo = await mirrorViaHttp(app, dir, opts)
const emitSyncStateThrottled = throttle(500, () => {
connection.emit('syncState', {
datKey: datInfo.address,
percent: datInfo.syncPercent
})
})
datInfo.on('change', function() {
change()
emitSyncStateThrottled()
})
state.currentlyHosted.push({
datKey: datInfo.address,
dat: datInfo,
http: httpInfo
})
change()
})
change()
}
app.listen(program.port)