-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchive.js
391 lines (318 loc) · 9.1 KB
/
archive.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
const { unpack, keyRing } = require('ara-network/keys')
const { createChannel } = require('ara-network/discovery/channel')
const { createCFS } = require('cfsnet/create')
const { Handshake } = require('ara-network/handshake')
const isBuffer = require('is-buffer')
const crypto = require('ara-crypto')
const debug = require('debug')('ara:identity:archive')
const path = require('path')
const pump = require('pump')
const raf = require('random-access-file')
const net = require('net')
const messages = require('./protobuf/messages')
const { toHex } = require('./util')
const { createIdentityKeyPath } = require('./key-path')
const rc = require('./rc')()
const DEFAULT_ARCHIVER_CONNECTION_TIMEOUT = 10000
const DEFAULT_ARCHIVER_MAX_CONNECTIONS = 32
/**
* Archive an identity into the network
* @public
* @param {Object} identity Results from create(), { secretKey, publicKey, files }
* @param {Object} opts
* @param {String|Buffer} opts.secret
* @param {String} opts.keyring
* @param {String} opts.network
* @param {Boolean} opts.shallow
* @throws TypeError
* @return {Promise}
*/
async function archive(identity, opts = {}) {
if (null == identity || 'object' !== typeof identity) {
throw new TypeError('Expecting identity to be an object.')
}
let conf
try {
conf = {
secret:
rc.network.identity.archiver.secret || rc.network.identity.secret,
keyring:
rc.network.identity.archiver.keyring || rc.network.identity.keyring,
network:
rc.network.identity.archiver.network
}
} finally {
conf = conf || {}
}
opts.secret = opts.secret || conf.secret
opts.keyring = opts.keyring || conf.keyring
opts.network = opts.network || conf.network
if ('string' !== typeof opts.secret && !isBuffer(opts.secret)) {
throw new TypeError('Expecting shared secret to be a string or buffer.')
}
if (!opts.secret || 0 === opts.secret.length) {
throw new TypeError('Shared secret cannot be empty.')
}
if (!opts.keyring) {
throw new TypeError('Expecting network keys keyring.')
}
if (opts.name && 'string' === typeof opts.name && !opts.network) {
const msg = 'Please set \'opts.network\' property instead of \'opts.name\'.'
// eslint-disable-next-line no-console
console.warn('aid.archive():', msg)
// eslint-disable-next-line no-param-reassign
opts.network = opts.name
}
if (-1 === opts.keyring.indexOf('.pub')) {
debug(`Using keyring: ${opts.keyring}, which may not be a public keyring.`)
}
if (!opts.network || 'string' !== typeof opts.network) {
throw new TypeError('Expecting network name for the archiver.')
}
if (!opts.timeout) {
// eslint-disable-next-line no-param-reassign
opts.timeout = DEFAULT_ARCHIVER_CONNECTION_TIMEOUT
}
const { maxConnections = DEFAULT_ARCHIVER_MAX_CONNECTIONS } = opts
const { publicKey, secretKey, files } = identity
let keys = null
let secret = null
let channel = null
let keyring = null
let discoveryKey = null
let peerCount = 0
let didArchive = false
let totalConnections = 0
let activeConnections = 0
const cfs = await createCFS({
secretKey,
storeSecretKey: false,
shallow: true,
key: publicKey,
id: toHex(publicKey),
storage(filename, drive, dir) {
const root = createIdentityKeyPath({ publicKey })
if ('function' === typeof opts.storage) {
return opts.storage(filename, drive, root)
}
if ('home' === path.basename(dir)) {
return raf(path.resolve(root, 'home', filename))
}
return raf(path.resolve(root, filename))
}
})
const shallow = opts.shallow || false
let writes = 0
let blocks = 0
await Promise.all(files.map(async (file) => {
if (!shallow || 'ddo.json' === file.path) {
let doWrite = true
try {
await cfs.access(file.path)
const buf = await cfs.readFile(file.path)
if (0 === Buffer.compare(buf, file.buffer)) {
doWrite = false
}
} catch (err) {
debug(err)
}
if (doWrite) {
return cfs.writeFile(file.path, file.buffer)
}
}
return null
}))
if (true !== opts.local) {
secret = Buffer.from(opts.secret)
keyring = keyRing(opts.keyring, { secret })
channel = createChannel()
await keyring.ready()
const buffer = await keyring.get(opts.network)
keys = unpack({ buffer })
// eslint-disable-next-line prefer-destructuring
discoveryKey = keys.discoveryKey
channel.join(discoveryKey)
channel.on('peer', onpeer)
channel.on('error', onerror)
timeout()
await new Promise((resolve, reject) => {
channel.once('error', reject)
channel.once('close', resolve)
})
} else {
await cfs.close()
didArchive = true
}
return didArchive
function timeout(again) {
clearTimeout(timeout.timer)
if (false !== again && false !== timeout.timer) {
timeout.timer = setTimeout(ontimeout, opts.timeout)
} else {
timeout.timer = false
}
}
function onpeer(chan, peer) {
if (activeConnections >= maxConnections) {
return
}
timeout()
const socket = net.connect(peer.port, peer.host)
const peerIndex = peerCount++
const handshake = new Handshake({
publicKey,
secretKey,
secret,
remote: { publicKey: keys.publicKey },
domain: { publicKey: keys.domain.publicKey }
})
activeConnections++
totalConnections++
socket.on('error', onerror)
socket.on('close', onclose)
socket.on('close', () => {
activeConnections = Math.max(activeConnections - 1, 0)
})
handshake.on('error', onerror)
handshake.on('hello', onhello)
handshake.on('auth', onauth)
handshake.on('okay', onokay)
handshake.hello()
handshake.pipe(socket).pipe(handshake)
function onhello(hello) {
timeout()
if ('function' === typeof opts.onhello) {
opts.onhello(hello)
}
handshake.auth()
}
function onauth(auth) {
timeout()
if ('function' === typeof opts.onauth) {
opts.onauth(auth)
}
}
async function onokay(okay) {
timeout()
if ('function' === typeof opts.onokay) {
opts.onokay(okay)
}
timeout()
socket.pause()
socket.unpipe(handshake).unpipe(socket)
const key = publicKey
const signature = crypto.sign(
crypto.blake2b(messages.Archive.encode({ shallow, key })),
secretKey
)
const stream = cfs.replicate({
download: false,
upload: true,
live: true,
userData: messages.Archive.encode({
signature,
shallow,
key,
})
})
let pending = 0
timeoutLiveConnection()
function timeoutLiveConnection(again) {
clearTimeout(timeoutLiveConnection.timer)
if (false !== again) {
timeoutLiveConnection.timer = setTimeout(() => stream.end(), 500)
} else {
timeoutLiveConnection.timer = false
}
}
cfs.partitions.home.content.on('peer-add', () => {
timeoutLiveConnection(false)
pending++
})
cfs.partitions.home.content.on('peer-remove', () => {
timeoutLiveConnection(false)
if (0 === --pending) {
process.nextTick(() => stream.end())
}
})
if (cfs.partitions.home.content) {
oncontent()
} else {
cfs.partitions.home.once('content', oncontent)
}
function oncontent() {
cfs.partitions.home.content.on('upload', onupload)
}
cfs.partitions.home.metadata.on('upload', () => {
writes++
timeoutLiveConnection(false)
})
function onupload() {
timeoutLiveConnection()
blocks++
if ('function' === typeof opts.onupload) {
opts.onupload({
peerIndex,
blocks,
writes,
peer,
})
}
}
pump(socket, stream, socket, (err) => {
timeout(false)
if (err) { debug(err) }
if (err && !ignoredError(err)) {
onerror(err)
} else {
didArchive = true
}
})
stream.on('handshake', () => {
timeout(false)
didArchive = true
})
socket.resume()
}
function onclose() {
if (didArchive) {
if (channel) {
channel.destroy()
channel = null
}
}
if ('function' === typeof opts.onclose) {
opts.onclose({
activeConnections,
totalConnections,
discoveryKey,
peerIndex,
peer,
})
}
}
}
function onerror(err) {
totalConnections = Math.max(totalConnections - 1, 0)
debug(err)
if (err && !ignoredError(err)) {
if ('function' === typeof opts.onerror) {
opts.onerror(err)
}
}
}
function ontimeout() {
channel.emit('error', new Error('Archiver request timed out.'))
}
function ignoredError(err) {
return Boolean(err && [
'ECONNREFUSED',
'EPIPE'
].includes(err.code))
}
}
module.exports = {
DEFAULT_ARCHIVER_CONNECTION_TIMEOUT,
DEFAULT_ARCHIVER_MAX_CONNECTIONS,
archive
}