-
Notifications
You must be signed in to change notification settings - Fork 7
/
files.js
271 lines (240 loc) · 6.97 KB
/
files.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
// SPDX-FileCopyrightText: 2021 Anders Rune Jensen
//
// SPDX-License-Identifier: LGPL-3.0-only
const jsesc = require('jsesc')
const sanitize = require('sanitize-filename')
const TypedFastBitSet = require('typedfastbitset')
const { readFile, writeFile } = require('atomic-file-rw')
const toBuffer = require('typedarray-to-buffer')
const crcCalculate = require('crc/lib/crc32')
const FIELD_SIZE = 4 // bytes
/*
* ## File format for tarr files
*
* Each header field is 4 bytes in size.
*
* | offset (bytes) | name | type |
* | 0 | version | UInt32LE |
* |----------------|---------|----------|
* | 4 | offset | UInt32LE |
* | 8 | count | UInt32LE |
* | 12 | crc | UInt32LE |
* | 16 | body | Buffer |
*/
function calculateCRCAndWriteFile(buf, filename, cb) {
try {
const crc = crcCalculate(buf)
buf.writeUInt32LE(crc, 3 * FIELD_SIZE)
} catch (err) {
return cb(err)
}
writeFile(filename, buf, cb)
}
function readFileAndCheckCRC(filename, cb) {
readFile(filename, (err, buf) => {
if (err) return cb(err)
if (buf.length < 16) return cb(Error('file too short'))
let crcFile
let crc
try {
crcFile = buf.readUInt32LE(3 * FIELD_SIZE)
buf.writeUInt32LE(0, 3 * FIELD_SIZE)
crc = crcCalculate(buf)
} catch (err) {
return cb(err)
}
if (crcFile !== 0 && crc !== crcFile) return cb(Error('crc check failed'))
cb(null, buf)
})
}
function saveTypedArrayFile(filename, version, offset, count, tarr, cb) {
if (!cb)
cb = (err) => {
if (err) console.error(err)
}
if (typeof version !== 'number') {
return cb(Error('cannot save file ' + filename + ' without version'))
}
let buf
try {
const dataBuffer = toBuffer(tarr)
// we try to save an extra 10% so we don't have to immediately grow
// after loading and adding again
const saveSize = Math.min(count * 1.1, tarr.length)
buf = Buffer.alloc(4 * FIELD_SIZE + saveSize * tarr.BYTES_PER_ELEMENT)
buf.writeUInt32LE(version, 0)
buf.writeUInt32LE(offset, FIELD_SIZE)
buf.writeUInt32LE(count, 2 * FIELD_SIZE)
dataBuffer.copy(buf, 4 * FIELD_SIZE)
} catch (err) {
return cb(err)
}
calculateCRCAndWriteFile(buf, filename, cb)
}
function loadTypedArrayFile(filename, Type, cb) {
readFileAndCheckCRC(filename, (err, buf) => {
if (err) return cb(err)
let version, offset, count, tarr
try {
version = buf.readUInt32LE(0)
offset = buf.readUInt32LE(FIELD_SIZE)
count = buf.readUInt32LE(2 * FIELD_SIZE)
const body = buf.slice(4 * FIELD_SIZE)
tarr = new Type(
body.buffer,
body.offset,
body.byteLength / (Type === Float64Array ? 8 : 4)
)
} catch (err) {
return cb(err)
}
cb(null, { version, offset, count, tarr })
})
}
function savePrefixMapFile(filename, version, offset, count, map, cb) {
if (!cb)
cb = (err) => {
if (err) console.error(err)
}
if (typeof version !== 'number') {
return cb(Error('cannot save file ' + filename + ' without version'))
}
let buf
try {
const jsonMap = JSON.stringify(map)
buf = Buffer.alloc(4 * FIELD_SIZE + jsonMap.length)
buf.writeUInt32LE(version, 0)
buf.writeUInt32LE(offset, FIELD_SIZE)
buf.writeUInt32LE(count, 2 * FIELD_SIZE)
Buffer.from(jsonMap).copy(buf, 4 * FIELD_SIZE)
} catch (err) {
return cb(err)
}
calculateCRCAndWriteFile(buf, filename, cb)
}
function loadPrefixMapFile(filename, cb) {
readFileAndCheckCRC(filename, (err, buf) => {
if (err) return cb(err)
let version, offset, count, map
try {
version = buf.readUInt32LE(0)
offset = buf.readUInt32LE(FIELD_SIZE)
count = buf.readUInt32LE(2 * FIELD_SIZE)
const body = buf.slice(4 * FIELD_SIZE)
map = JSON.parse(body)
} catch (err) {
return cb(err)
}
cb(null, { version, offset, count, map })
})
}
function saveBitsetFile(filename, version, offset, bitset, cb) {
let count
try {
bitset.trim()
count = bitset.words.length
} catch (err) {
return cb(err)
}
saveTypedArrayFile(filename, version, offset, count, bitset.words, cb)
}
function loadBitsetFile(filename, cb) {
loadTypedArrayFile(filename, Uint32Array, (err, data) => {
if (err) return cb(err)
const { version, offset, count, tarr } = data
const bitset = new TypedFastBitSet()
bitset.words = tarr
cb(null, { version, offset, bitset })
})
}
function listFiles(dir, cb) {
if (typeof window !== 'undefined') {
// browser
const IdbKvStore = require('idb-kv-store')
const store = new IdbKvStore(dir, { disableBroadcast: true })
store.keys(cb)
} else {
// node.js
const fs = require('fs')
const mkdirp = require('mkdirp')
mkdirp(dir).then(() => {
fs.readdir(dir, cb)
}, cb)
}
}
function safeFilename(filename) {
// in general we want to escape wierd characters
let result = jsesc(filename)
// sanitize will remove special characters, which means that two
// indexes might end up with the same name so lets replace those
// with jsesc escapeEverything values
result = result.replace(/\./g, 'x2E')
result = result.replace(/\//g, 'x2F')
result = result.replace(/\?/g, 'x3F')
result = result.replace(/\</g, 'x3C')
result = result.replace(/\>/g, 'x3E')
result = result.replace(/\:/g, 'x3A')
result = result.replace(/\*/g, 'x2A')
result = result.replace(/\|/g, 'x7C')
// finally sanitize
return sanitize(result)
}
const EmptyFile = {
create(filename) {
if (typeof window !== 'undefined') {
// browser
const IdbKvStore = require('idb-kv-store')
const store = new IdbKvStore(filename, { disableBroadcast: true })
store.set('x', 'y', () => {})
} else {
// node.js
const fs = require('fs')
fs.closeSync(fs.openSync(filename, 'w'))
}
},
exists(filename, cb) {
if (typeof window !== 'undefined') {
// browser
const IdbKvStore = require('idb-kv-store')
const store = new IdbKvStore(filename, { disableBroadcast: true })
store.get('x', (err, y) => {
if (err) return cb(null, false)
cb(null, y === 'y')
})
} else {
// node.js
const fs = require('fs')
cb(null, fs.existsSync(filename))
}
},
delete(filename, cb) {
EmptyFile.exists(filename, (err, exists) => {
if (err) return cb(err)
if (!exists) cb(null)
else EmptyFile._actuallyDelete(filename, cb)
})
},
_actuallyDelete(filename, cb) {
if (typeof window !== 'undefined') {
// browser
const IdbKvStore = require('idb-kv-store')
const store = new IdbKvStore(filename, { disableBroadcast: true })
store.remove('x', cb)
} else {
// node.js
const rimraf = require('rimraf')
cb(null, rimraf.sync(filename))
}
},
}
module.exports = {
saveTypedArrayFile,
loadTypedArrayFile,
savePrefixMapFile,
loadPrefixMapFile,
saveBitsetFile,
loadBitsetFile,
listFiles,
EmptyFile,
safeFilename,
}