-
Notifications
You must be signed in to change notification settings - Fork 8
/
cmd.js
132 lines (106 loc) Β· 2.92 KB
/
cmd.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
const identity = require('./identity')
const raf = require('random-access-file')
const path = require('path')
const swarm = require('hyperdiscovery')
const importFiles = require('hyperdrive-import-files')
const hyperdrive = require('hyperdrive')
const level = require('level')
const ln = require('hyperdrive-ln')
const async = require('async')
const DEFAULT_CLONE_PATH = '~/.hyperidentity-linked'
function up (drive, archive, opts, cb) {
if (cb === undefined) return up(drive, archive, {}, opts)
var clonePath = opts.clone_path || DEFAULT_CLONE_PATH
var self = swarm(archive)
// also replicate all linked archives
archive.list((err, entries) => {
if (err) return cb(err)
var links = entries.filter(e => e.name.startsWith('.links'))
async.map(
links,
(entry, cb) => {
ln.readlink(archive, entry.name, cb)
},
(err, links) => {
if (err) return cb(err)
var conns = links.reduce((result, link) => { return result.concat([clone(link.link)]) }, [])
// push self
conns.push({archive, sw: self})
cb(null, conns)
}
)
})
function clone (key) {
var archive = drive.createArchive(key, { file: name => raf(path.join(clonePath, name)) })
var sw = swarm(archive)
return {archive, sw}
}
function startLocalServer (archive, cb) {
var app = localServer(archive)
var server = app.listen(9064, err => {
cb(err, server)
})
}
}
// close all swarm connection in a `up` result.
// only needed for testing.
function down (conns, cb) {
async.each(conns, close, err => {
if (err) return cb(err)
cb()
})
function close (r, cb) {
r.sw.close(cb)
}
}
function init (dir, meta, cb) {
var drive = hyperdrive(level(path.join(dir, '.hyperidentity')))
var archive = drive.createArchive({
file: name => raf(path.join(dir, name))
})
var id = identity(archive)
id.setMeta(meta, done)
function done (err) {
if (err) cb(err)
var importStatus = importFiles(
archive,
dir,
{
ignore: [
'/**/.hyperidentity/**',
'/**/.proof/**',
'/**/.links/**'
],
index: true
}, err => {
cb(err, id, archive, importStatus, drive)
})
}
}
function info (archive, cb) {
var result = {key: archive.key, links: []}
archive.list((err, entries) => {
if (err) return cb(err)
var links = entries.filter(e => e.name.startsWith('.links'))
async.map(
links,
(entry, cb) => {
ln.readlink(archive, entry.name, cb)
},
(err, links) => {
if (err) return cb(err)
result.links = links
cb(null, result)
}
)
})
}
function login (archive, encodedToken, cb) {
var id = identity(archive)
var decoded = new Buffer(encodedToken, 'base64')
id.acceptLinkToken(decoded, err => {
if (err) return cb(err)
cb(null, archive)
})
}
module.exports = {init, up, down, info, login}