-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunpack.js
114 lines (96 loc) · 2.86 KB
/
unpack.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
const messages = require('./messages')
const TinyBox = require('tinybox')
const mkdirp = require('mkdirp')
const Batch = require('batch')
const path = require('path')
const raf = require('random-access-file')
const fs = require('fs')
// quick util
const noop = () => void 0
/**
* Mask applied to `entry.mode` to get permissions value.
* @private
*/
const FS_STAT_MODE_MASK = 0x1ff // 0777
/**
* Unpacks `target` archive to disk or optionally, a given
* `random-access-storage` instance where `target` is path to
* an archive on the file system or a `random-access-storage`
* instance containing the archive data.
* @param {String|Object} target
* @param {?(Object)} opts
* @param {?(String)} opts.output
* @param {?(Function)} opts.storage
* @param {?(Number)} opts.concurrency
* @param {Function} callback
*/
function unpack(target, opts, callback) {
if ('function' === typeof opts) {
callback = opts
}
if (!opts || 'object' !== typeof opts) {
opts = {}
}
// istanbul ignore next
if ('function' !== typeof callback) {
callback = noop
}
const archive = new TinyBox(target)
archive.get('index', onindex)
function onindex(err, result) {
// istanbul ignore next
if (err) {
return callback(err)
} else if (!result || !result.value) {
return callback(null, [])
}
const index = messages.Archive.Index.decode(result.value)
const batch = new Batch().concurrency(opts.concurrency || Infinity)
for (const entry of index.entries) {
onentry(entry)
}
return batch.end(onend)
function onend(err) {
// istanbul ignore next
if (err) { return callback(err) }
callback(null, index.entries)
}
function onentry(entry) {
batch.push((next) => {
archive.get(entry.filename, (err, result) => {
const output = opts.output || process.cwd()
const filename = path.resolve(output, entry.filename)
// istanbul ignore next
if (err) { return next(err) }
const { buffer, mode } = messages.Archive.Entry.decode(result.value)
const hasCustomStorage = 'function' === typeof opts.storage
if (!hasCustomStorage) {
const dirname = path.dirname(filename)
return mkdirp(dirname, (err) => {
// istanbul ignore next
if (err) { return next(err) }
write(raf(filename), buffer, (err) => {
// istanbul ignore next
if (err) { return next(err) }
fs.chmod(filename, mode & FS_STAT_MODE_MASK, next)
})
})
}
write(opts.storage(entry.filename), buffer, next)
})
})
}
function write(storage, value, done) {
storage.write(0, value, (err) => {
storage.close()
done(err)
})
}
}
}
/**
* Module exports.
*/
module.exports = {
unpack
}