-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·68 lines (58 loc) · 1.35 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
#!/usr/bin/env node
const flume = require('flumedb')
const log = require('flumelog-offset')
const os = require('os')
const path = require('path')
const pull = require('pull-stream')
const mv = require('mv')
const crypto = require('crypto')
const yargs = require('yargs')
const config = yargs
.usage('Usage: $0 [options]')
.options('path', {
describe: 'database path',
default: path.join(os.homedir(), '.ssb', 'flume', 'log.offset'),
type: 'string'
})
.argv
const createDb = (file) =>
flume(log(file, {
codec: {
encode: JSON.stringify,
decode: (data) => {
if (data.length >= 0x3fffffe7) {
// way too long
return null
}
try {
return JSON.parse(data)
} catch (e) {
console.log(`Ignored error: ${e}`)
return null
}
}
}
}))
const rand = crypto.randomBytes(32).toString('hex')
const paths = {
db: config.path,
tmp: `/tmp/fix-flume-db-${rand}`
}
const a = createDb(paths.db)
const b = createDb(paths.tmp)
const onEachMessage = msg => {
b.append(msg, function (err) {
if (err) throw err
})
}
const onDone = () => {
// overwrite the real db with the temporary db
mv(paths.tmp, paths.db, function (err) {
if (err) throw err
})
}
pull(
a.stream({ seqs: false }),
pull.filter(),
pull.drain(onEachMessage, onDone)
)