-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·86 lines (73 loc) · 1.78 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
#!/usr/bin/env node
var createStream = require('hypercore-stream-swarm')
var level = require('level-party')
var through = require('through2')
var opts = require('minimist')(process.argv.splice(2), { alias: {
r: 'read',
t: 'tail',
e: 'exit',
f: 'format',
s: 'static',
l: 'log',
v: 'version',
h: 'help'
}})
var len = opts._.length
process.title = 'dat-pipe'
if (opts.help) {
console.log(require('./usage')('root.txt'))
process.exit(0)
}
if (opts.version) {
var pkg = require('./package.json')
console.log(pkg.version)
process.exit(0)
}
var path, key
if (len === 1) {
if (opts._[0].length === 64 || opts._[0].length === 128) {
key = opts._[0]
} else {
path = opts._[0]
}
} else {
path = opts._[0]
key = opts._[1]
}
opts.db = (typeof path === 'string') ? level(path) : undefined
delete opts._
var stream = createStream(key, opts)
if (stream.write) {
process.stdin.pipe(stream)
opts.tail = true
}
if ((stream.live && !stream.secretKey) || opts.read) {
stream.pipe(through(function (chunk, enc, cb) {
if (opts.format) chunk += '\r\n'
this.push(chunk)
cb()
})).pipe(process.stdout)
}
if (opts.log) {
if (stream.live) {
log(stream.key, stream.secretKey)
} else {
stream.feed.on('close', function () {
log(stream.key, stream.secretKey)
})
}
}
function log (key, secretKey) {
key = (key) ? key.toString('hex') : undefined
secretKey = (secretKey) ? secretKey.toString('hex') : undefined
if (opts.log === true) {
console.log('public key', key)
if (opts.secret && secretKey) console.log('secret key', secretKey)
} else {
var file = require('fs').createWriteStream(opts.log, { flags: 'a' })
file.write(key)
if (opts.secret && secretKey) file.write(' ' + secretKey)
file.write('\n')
file.end()
}
}