-
Notifications
You must be signed in to change notification settings - Fork 0
/
unarchive.js
71 lines (59 loc) · 1.64 KB
/
unarchive.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
const { create } = require('./create')
const mirror = require('mirror-folder')
const debug = require('debug')('ara-filesystem:unarchive')
const {
resolve,
isAbsolute
} = require('path')
/**
* Unarchives the AFS with the given Ara identity
* @param {Object} opts
* @param {String} opts.did
* @param {String} opts.path
* @param {Object} [opts.keyringOpts]
* @return {Object}
*/
async function unarchive(opts) {
if (!opts || 'object' !== typeof opts) {
throw new TypeError('Expecting opts object.')
} else if (!opts.did || 'string' !== typeof opts.did) {
throw new TypeError('DID URI must be of type string.')
} else if (opts.path && 'string' !== typeof opts.path) {
throw new TypeError('Path must be of type string.')
}
const { did, keyringOpts } = opts
const { afs } = await create({ did, keyringOpts })
try {
const result = await afs.readdir(afs.HOME)
if (0 === result.length) {
throw new Error('Can only unarchive a non-empty AFS.')
}
} catch (err) {
throw err
}
let path = opts.path || __dirname
if (!isAbsolute(path)) {
path = resolve(path)
}
const progress = mirror({
name: afs.HOME,
fs: afs
}, { name: path }, { keepExisting: true })
progress.on('put', (src) => {
debug('onput', src.name)
})
progress.on('skip', (src) => {
debug('onskip', src.name)
})
// Await end or error
const error = await new Promise((accept, reject) => progress.once('end', accept).once('error', reject))
afs.close()
if (error) {
debug(`unarchive error: ${path}: ${error}`)
} else {
debug(`unarchive complete: ${path}`)
}
}
module.exports = {
unarchive
}