-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
250 lines (219 loc) · 7.95 KB
/
index.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
/*! fs-chunk-store. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
import fs from 'fs'
import os from 'os'
import parallel from 'run-parallel'
import path from 'path'
import queueMicrotask from 'queue-microtask'
import RAF from 'random-access-file'
import randombytes from 'randombytes'
import thunky from 'thunky'
import getFileRegex from 'filename-reserved-regex'
const RESERVED_FILENAME_REGEX = getFileRegex()
let TMP
try {
TMP = fs.statSync('/tmp') && '/tmp'
} catch (err) {
TMP = os.tmpdir()
}
export default class Storage {
constructor (chunkLength, opts = {}) {
this.chunkLength = Number(chunkLength)
if (!this.chunkLength) throw new Error('First argument must be a chunk length')
this.name = opts.name || path.join('fs-chunk-store', randombytes(20).toString('hex'))
this.addUID = opts.addUID
if (opts.files) {
this.path = opts.path
if (!Array.isArray(opts.files)) {
throw new Error('`files` option must be an array')
}
this.files = opts.files.map((file, i, files) => {
if (file.path == null) throw new Error('File is missing `path` property')
if (file.length == null) throw new Error('File is missing `length` property')
if (file.offset == null) {
if (i === 0) {
file.offset = 0
} else {
const prevFile = files[i - 1]
file.offset = prevFile.offset + prevFile.length
}
}
let newPath = path.dirname(file.path)
const filename = path.basename(file.path)
if (this.path) {
newPath = this.addUID ? path.resolve(path.join(this.path, this.name, newPath)) : path.resolve(path.join(this.path, newPath))
}
newPath = path.join(newPath, filename.replace(RESERVED_FILENAME_REGEX, ''))
return { path: newPath, length: file.length, offset: file.offset }
})
this.length = this.files.reduce((sum, file) => { return sum + file.length }, 0)
if (opts.length != null && opts.length !== this.length) {
throw new Error('total `files` length is not equal to explicit `length` option')
}
} else {
const len = Number(opts.length) || Infinity
this.files = [{
offset: 0,
path: path.resolve(opts.path || path.join(TMP, this.name)),
length: len
}]
this.length = len
}
this.chunkMap = []
this.closed = false
this.files.forEach(file => {
file.open = thunky(cb => {
if (this.closed) return cb(new Error('Storage is closed'))
fs.mkdir(path.dirname(file.path), { recursive: true }, err => {
if (err) return cb(err)
if (this.closed) return cb(new Error('Storage is closed'))
cb(null, new RAF(file.path))
})
})
})
// If the length is Infinity (i.e. a length was not specified) then the store will
// automatically grow.
if (this.length !== Infinity) {
this.lastChunkLength = (this.length % this.chunkLength) || this.chunkLength
this.lastChunkIndex = Math.ceil(this.length / this.chunkLength) - 1
this.files.forEach(file => {
const fileStart = file.offset
const fileEnd = file.offset + file.length
const firstChunk = Math.floor(fileStart / this.chunkLength)
const lastChunk = Math.floor((fileEnd - 1) / this.chunkLength)
for (let p = firstChunk; p <= lastChunk; ++p) {
const chunkStart = p * this.chunkLength
const chunkEnd = chunkStart + this.chunkLength
const from = (fileStart < chunkStart) ? 0 : fileStart - chunkStart
const to = (fileEnd > chunkEnd) ? this.chunkLength : fileEnd - chunkStart
const offset = (fileStart > chunkStart) ? 0 : chunkStart - fileStart
if (!this.chunkMap[p]) this.chunkMap[p] = []
this.chunkMap[p].push({
from,
to,
offset,
file
})
}
})
}
}
put (index, buf, cb) {
if (typeof cb !== 'function') cb = noop
if (this.closed) return nextTick(cb, new Error('Storage is closed'))
const isLastChunk = (index === this.lastChunkIndex)
if (isLastChunk && buf.length !== this.lastChunkLength) {
return nextTick(cb, new Error('Last chunk length must be ' + this.lastChunkLength))
}
if (!isLastChunk && buf.length !== this.chunkLength) {
return nextTick(cb, new Error('Chunk length must be ' + this.chunkLength))
}
if (this.length === Infinity) {
this.files[0].open((err, file) => {
if (err) return cb(err)
file.write(index * this.chunkLength, buf, cb)
})
} else {
const targets = this.chunkMap[index]
if (!targets) return nextTick(cb, new Error('no files matching the request range'))
const tasks = targets.map((target) => {
return (cb) => {
target.file.open((err, file) => {
if (err) return cb(err)
file.write(target.offset, buf.slice(target.from, target.to), cb)
})
}
})
parallel(tasks, cb)
}
}
get (index, opts, cb) {
if (typeof opts === 'function') return this.get(index, null, opts)
if (this.closed) return nextTick(cb, new Error('Storage is closed'))
const chunkLength = (index === this.lastChunkIndex)
? this.lastChunkLength
: this.chunkLength
const rangeFrom = (opts && opts.offset) || 0
const rangeTo = (opts && opts.length) ? rangeFrom + opts.length : chunkLength
if (rangeFrom < 0 || rangeFrom < 0 || rangeTo > chunkLength) {
return nextTick(cb, new Error('Invalid offset and/or length'))
}
if (this.length === Infinity) {
if (rangeFrom === rangeTo) return nextTick(cb, null, Buffer.from(0))
this.files[0].open((err, file) => {
if (err) return cb(err)
const offset = (index * this.chunkLength) + rangeFrom
file.read(offset, rangeTo - rangeFrom, cb)
})
} else {
let targets = this.chunkMap[index]
if (!targets) return nextTick(cb, new Error('no files matching the request range'))
if (opts) {
targets = targets.filter((target) => {
return target.to > rangeFrom && target.from < rangeTo
})
if (targets.length === 0) {
return nextTick(cb, new Error('no files matching the requested range'))
}
}
if (rangeFrom === rangeTo) return nextTick(cb, null, Buffer.from(0))
const tasks = targets.map((target) => {
return (cb) => {
let from = target.from
let to = target.to
let offset = target.offset
if (opts) {
if (to > rangeTo) to = rangeTo
if (from < rangeFrom) {
offset += (rangeFrom - from)
from = rangeFrom
}
}
target.file.open((err, file) => {
if (err) return cb(err)
file.read(offset, to - from, cb)
})
}
})
parallel(tasks, (err, buffers) => {
if (err) return cb(err)
cb(null, Buffer.concat(buffers))
})
}
}
close (cb) {
if (this.closed) return nextTick(cb, new Error('Storage is closed'))
this.closed = true
const tasks = this.files.map((file) => {
return (cb) => {
file.open((err, file) => {
// an open error is okay because that means the file is not open
if (err) return cb(null)
file.close(cb)
})
}
})
parallel(tasks, cb)
}
destroy (cb) {
this.close(() => {
if (this.addUID && this.path) {
fs.rm(path.resolve(path.join(this.path, this.name)), { recursive: true, maxBusyTries: 10 }, cb)
} else {
const tasks = this.files.map((file) => {
return (cb) => {
fs.rm(file.path, { recursive: true, maxRetries: 10 }, err => {
err && err.code === 'ENOENT' ? cb() : cb(err)
})
}
})
parallel(tasks, cb)
}
})
}
}
function nextTick (cb, err, val) {
queueMicrotask(() => {
if (cb) cb(err, val)
})
}
function noop () {}