-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
43 lines (36 loc) · 1.13 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
'use strict'
const inherits = require('inherits')
const { Readable } = require('readable-stream')
module.exports = ReadStream
inherits(ReadStream, Readable)
function ReadStream (iterator, options) {
if (!(this instanceof ReadStream)) return new ReadStream(iterator, options)
options = options || {}
Readable.call(this, Object.assign({}, options, {
objectMode: true
}))
this._iterator = iterator
this._options = options
this.on('end', this.destroy.bind(this, null, null))
}
ReadStream.prototype._read = function () {
if (this.destroyed) return
this._iterator.next((err, key, value) => {
if (this.destroyed) return
if (err) return this.destroy(err)
if (key === undefined && value === undefined) {
this.push(null)
} else if (this._options.keys !== false && this._options.values === false) {
this.push(key)
} else if (this._options.keys === false && this._options.values !== false) {
this.push(value)
} else {
this.push({ key, value })
}
})
}
ReadStream.prototype._destroy = function (err, callback) {
this._iterator.end(function (err2) {
callback(err || err2)
})
}