-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
58 lines (49 loc) · 1.57 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
bulk = require('bulk-write-stream')
fs = require('fs')
varint = require('varint')
lengthPrefixedStream = require('length-prefixed-stream')
through2 = require('through2')
module.exports = {
createWriteStream: function (filename, encoder) {
var fd = -1
var buffer = new Buffer(1024)
function open (list, callback) {
fs.open(filename, 'a', function(err, result) {
if (err) {
return callback(err)
}
fd = result
write(list, callback)
})
}
function write (list, callback) {
if (fd === -1) return open(list, callback)
var bytesNeeded = 0
for (var i=0; i<list.length; i++) {
var data = list[i]
bytesNeeded += encoder.encodingLength(data)
bytesNeeded += varint.encodingLength(encoder.encodingLength(data))
}
if (bytesNeeded > buffer.length) {
buffer = new Buffer(Math.max(2*buffer.length, bytesNeeded))
}
var offset = 0
for (var i=0; i<list.length; i++) {
var data = list[i]
varint.encode(encoder.encodingLength(data), buffer, offset)
offset += varint.encode.bytes
encoder.encode(data, buffer, offset)
offset += encoder.encode.bytes
}
fs.write(fd, buffer, 0, offset, callback)
}
return bulk.obj(write)
},
createReadStream: function (filename, encoder) {
var rs = fs.createReadStream(filename)
var decoder = through2.obj(function (data, enc, callback) {
callback(null, encoder.decode(data))
})
return rs.pipe(lengthPrefixedStream.decode()).pipe(decoder)
}
}