-
Notifications
You must be signed in to change notification settings - Fork 1
/
archiver.js
219 lines (188 loc) · 5.61 KB
/
archiver.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
const Resource = require('nanoresource')
const messages = require('./messages')
const TinyBox = require('tinybox')
const rimraf = require('rimraf')
const ready = require('nanoresource-ready')
const Batch = require('batch')
const raf = require('random-access-file')
const fs = require('fs')
/**
* Default file permissions for inputs.
* @private
*/
const DEFAULT_INPUT_PERMISSIONS = 438 // 0666
/**
* Mask applied to `DEFAULT_INPUT_PERMISSIONS` for default mode.
* @private
*/
const FS_STAT_MODE_MASK = 0x1ff // 0777
/**
* The `Archiver` class represents an abstraction for storing
* compiled objects into a `TinyBox`
* @class
* @extends Resource
*/
class Archiver extends Resource {
/**
* `Archiver` class constructor.
* @param {?(Object)} opts
* @param {?(Function)} opts.storage
*/
constructor(opts) {
if (!opts || 'object' !== typeof opts) {
opts = {}
}
super()
if ('function' === typeof opts.storage) {
this.storage = opts.storage
} else {
this.storage = raf
}
}
/**
* Waits for archiver to be ready and calling `callback()`
* when it is.
* @param {Function} ready
*/
ready(callback) {
ready(this, callback)
}
/**
* Creates a named archive specified at `filename`
* with given inputs calling `callback(err)` upon success
* or error.
* @param {String} filename
* @param {Array|Map} inputs
* @param {?(Object)} opts
* @param {?(Object)} opts.storage
* @param {?(Boolean)} opts.truncate
* @param {?(Number)} opts.concurrency
* @param {Function} callback
*/
archive(filename, inputs, opts, callback) {
if ('function' === typeof opts) {
callback = opts
opts = {}
}
// istanbul ignore next
if (!opts || 'object' !== typeof opts) {
opts = {}
}
const steps = new Batch().concurrency(1)
if (!opts.storage && false !== opts.truncate) {
steps.push((done) => {
fs.access(filename, (err) => {
// istanbul ignore next
if (err) { return done(null) }
rimraf(filename, done)
})
})
}
steps.push((done) => {
if (Array.isArray(inputs)) {
if (Array.isArray(inputs[0])) {
inputs = new Map(inputs)
} else {
inputs = new Map(inputs.map((input) => [input, null]))
}
}
const filenames = [ ...inputs.keys() ]
const entries = filenames.sort().map((filename, id) => ({ id, filename }))
const storage = opts.storage || this.storage(filename)
const batch = new Batch().concurrency(opts.concurrency || Infinity)
const box = new TinyBox(storage)
const versions = messages.Versions.encode(process.versions)
batch.push((next) => box.put('versions', versions, next))
batch.push((next) => {
box.get('index', (err, result) => {
const prev = result && result.value
? messages.Archive.Index.decode(result.value)
: null
let size = entries.length
if (prev && prev.entries) {
const existing = new Set(filenames)
for (const entry of prev.entries) {
if (!existing.has(entry.filename)) {
entries.unshift(entry)
size++
}
}
}
let i = 0
for (const entry of entries) {
entry.id = i++
}
const index = messages.Archive.Index.encode({ size, entries })
box.put('index', index, next)
})
})
for (const [ inputPath, inputBuffer ] of inputs) {
batch.push((next) => {
if (Buffer.isBuffer(inputBuffer)) {
if (opts.storage) {
const entry = messages.Archive.Entry.encode({
mode: (DEFAULT_INPUT_PERMISSIONS|FS_STAT_MODE_MASK),
size: inputBuffer.length,
buffer: inputBuffer
})
box.put(inputPath, entry, next)
} else {
fs.stat(inputPath, (err, stats) => {
const entry = messages.Archive.Entry.encode({
mode: stats.mode,
size: stats.size,
buffer: inputBuffer
})
box.put(inputPath, entry, next)
})
}
} else {
// `inputBuffer` could be a `random-access-storage` instance
const file = inputBuffer && inputBuffer.stat && inputBuffer.read
? inputBuffer
: raf(inputPath)
file.stat((err, stats) => {
// istanbul ignore next
if (err) { return next(err) }
// istanbul ignore next
const {
size = 0,
mode = (DEFAULT_INPUT_PERMISSIONS|FS_STAT_MODE_MASK)
} = stats
file.read(0, size, (err, buffer) => {
// istanbul ignore next
if (err) { return next(err) }
const entry = messages.Archive.Entry.encode({
mode,
size,
buffer
})
box.put(inputPath, entry, (err) => {
// istanbul ignore next
if (err) { return next(err) }
file.close(next)
})
})
})
}
})
}
batch.end((err) => {
// istanbul ignore next
if (err) { return done(err) }
if (!opts.storage) {
storage.close(done)
} else {
process.nextTick(done, null)
}
})
})
steps.end(callback)
}
}
/**
* Module exports.
*/
module.exports = {
Archiver
}