-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadStream.js
44 lines (34 loc) · 1.07 KB
/
readStream.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
var ReadStream = require("read-stream")
, makeStreamData = require("level-encoding/makeStreamData")
, getOptions = require("./getOptions")
module.exports = LevelReadStream
function LevelReadStream(db, cache, defaults) {
return readStream
function readStream(options) {
if (db) {
return db.readStream(options)
}
options = getOptions(defaults, options)
var queue = ReadStream()
, start = options.start
, end = options.end
, keys = Object.keys(cache.dump())
keys.forEach(function (key) {
if (within(key)) {
var value = cache.get(key)
if (value !== undefined) {
queue.push(makeStreamData({
id: key
, value: value
}, options))
}
}
})
queue.end()
return queue.stream
function within(key) {
return (!start || start < key ) &&
(!end || key < end)
}
}
}