-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (37 loc) · 909 Bytes
/
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
const
Readable = require('stream').Readable,
kLineObjectStream = Symbol('line object stream');
module.exports = function(stream) {
stream[Symbol.asyncIterator] = function() {
if ( ! this[kLineObjectStream]) {
const readable = new Readable({
objectMode: true,
read: () => {
this.resume();
},
destroy: (err, cb) => {
this.off('line', lineListener);
this.off('close', closeListener);
if (this.close) {
this.close();
}
stream.destroy()
cb(err);
}
});
const lineListener = (row) => {
if ( ! readable.push(row)) {
this.pause();
}
};
const closeListener = () => {
readable.push(null);
};
this.on('data', lineListener);
this.on('close', closeListener);
this[kLineObjectStream] = readable;
}
return this[kLineObjectStream][Symbol.asyncIterator]();
};
return stream;
};