-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·133 lines (120 loc) · 3.77 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var fs = require('fs')
var prettyBytes = require('prettier-bytes')
var neatLog = require('neat-log')
var output = require('neat-log/output')
var DWeb = require('dwebs-core')
var ram = require('random-access-memory')
module.exports = function (opts) {
var usage = 'Usage: dweb-log [key|dir] [--live,-l]'
if (opts.help) {
console.error(usage)
process.exit()
}
var neat = neatLog(view)
neat.use(function (state, bus) {
state.opts = opts
state.opts.sparse = true
state.opts.createIfMissing = false
var dir = null
if (opts._[0]) {
try {
if (fs.statSync(opts._[0]).isDirectory()) dir = opts._[0]
} catch (e) {
state.opts.key = opts._[0]
dir = ram
}
} else {
dir = process.cwd()
}
DWeb(dir, state.opts, function (err, dweb) {
if (err && err.name === 'MissingError') {
bus.clear()
console.error('No dweb found in', dir)
console.error('')
console.error(usage)
process.exit(1)
} else if (err) {
console.error(err)
process.exit(1)
}
state.dweb = dweb
state.log = []
state.puts = 0
state.dels = 0
bus.on('update', function () {
state.verLen = state.dweb.archive.version.toString().length
state.zeros = new Array(state.verLen).join('0')
})
bus.emit('render')
dweb.trackStats()
if (dweb.writable) {
bus.emit('update')
return run()
}
// var waitTimeout TODO
state.offline = true
dweb.joinNetwork(function () {
if (!state.opts.live && state.offline && !dweb.network.connecting) exit()
}).once('connection', function () {
state.offline = false
// clearTimeout(waitTimeout) // TODO: close if not live
})
dweb.archive.ready(function () {
bus.emit('update')
})
dweb.archive.on('content', function () {
bus.emit('update')
dweb.archive.content.update()
})
if (!state.opts.live) {
// wait for connections
setTimeout(exit, 3000)
}
if (!state.opts.key) run()
else dweb.archive.metadata.update(run)
function run () {
state.running = true
var rs = dweb.archive.history({ live: state.opts.live || state.offline })
rs.on('data', function (data) {
var version = `${state.zeros.slice(0, state.verLen - data.version.toString().length)}${data.version}`
var msg = `${version} [${data.type}] ${data.name}`
if (data.type === 'put') {
msg += ` ${prettyBytes(data.value.size)} (${data.value.blocks} ${data.value.blocks === 1 ? 'block' : 'blocks'})`
state.puts++
} else {
state.dels++
}
state.log.push(msg)
bus.emit('render')
})
}
function exit () {
state.exiting = true
bus.render()
process.exit(0)
}
})
})
function view (state) {
if (!state.running) {
if (state.opts.key) return 'Connecting to network...'
return 'Reading dweb history...'
}
return output(`
${state.log.join('\n')}
${state.offline
? state.exiting
? '\nNo sources found in network.\nLog may be outdated.'
: '...\n\nConnecting to network to update & verify log...'
: '\nLog synced with network'}
Archive has ${state.dweb.archive.version} changes (puts: +${state.puts}, dels: -${state.dels})
Current Size: ${prettyBytes(state.dweb.stats.get().byteLength)}
Total Size:
- Metadata ${prettyBytes(state.dweb.archive.metadata.byteLength)}
- Content ${prettyBytes(state.dweb.archive.content.byteLength)}
Blocks:
- Metadata ${state.dweb.archive.metadata.length}
- Content ${state.dweb.archive.content.length}
`)
}
}