-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
133 lines (120 loc) · 3.79 KB
/
Copy pathindex.js
File metadata and controls
133 lines (120 loc) · 3.79 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
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 DWebX = require('dwebx-node')
var ram = require('random-access-memory')
module.exports = function (opts) {
var usage = 'Usage: dwebx-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()
}
DWebX(dir, state.opts, function (err, dwebx) {
if (err && err.name === 'MissingError') {
bus.clear()
console.error('No dwebx found in', dir)
console.error('')
console.error(usage)
process.exit(1)
} else if (err) {
console.error(err)
process.exit(1)
}
state.dwebx = dwebx
state.log = []
state.puts = 0
state.dels = 0
bus.on('update', function () {
state.verLen = state.dwebx.archive.version.toString().length
state.zeros = new Array(state.verLen).join('0')
})
bus.emit('render')
dwebx.trackStats()
if (dwebx.writable) {
bus.emit('update')
return run()
}
// var waitTimeout TODO
state.offline = true
dwebx.joinNetwork(function () {
if (!state.opts.live && state.offline && !dwebx.network.connecting) exit()
}).once('connection', function () {
state.offline = false
// clearTimeout(waitTimeout) // TODO: close if not live
})
dwebx.archive.ready(function () {
bus.emit('update')
})
dwebx.archive.on('content', function () {
bus.emit('update')
dwebx.archive.content.update()
})
if (!state.opts.live) {
// wait for connections
setTimeout(exit, 3000)
}
if (!state.opts.key) run()
else dwebx.archive.metadata.update(run)
function run () {
state.running = true
var rs = dwebx.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 dwebx 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.dwebx.archive.version} changes (puts: +${state.puts}, dels: -${state.dels})
Current Size: ${prettyBytes(state.dwebx.stats.get().byteLength)}
Total Size:
- Metadata ${prettyBytes(state.dwebx.archive.metadata.byteLength)}
- Content ${prettyBytes(state.dwebx.archive.content.byteLength)}
Blocks:
- Metadata ${state.dwebx.archive.metadata.length}
- Content ${state.dwebx.archive.content.length}
`)
}
}