1 |
3a515b92
|
cagy
|
'use strict';
|
2 |
|
|
const {PassThrough} = require('stream');
|
3 |
|
|
|
4 |
|
|
module.exports = options => {
|
5 |
|
|
options = Object.assign({}, options);
|
6 |
|
|
|
7 |
|
|
const {array} = options;
|
8 |
|
|
let {encoding} = options;
|
9 |
|
|
const buffer = encoding === 'buffer';
|
10 |
|
|
let objectMode = false;
|
11 |
|
|
|
12 |
|
|
if (array) {
|
13 |
|
|
objectMode = !(encoding || buffer);
|
14 |
|
|
} else {
|
15 |
|
|
encoding = encoding || 'utf8';
|
16 |
|
|
}
|
17 |
|
|
|
18 |
|
|
if (buffer) {
|
19 |
|
|
encoding = null;
|
20 |
|
|
}
|
21 |
|
|
|
22 |
|
|
let len = 0;
|
23 |
|
|
const ret = [];
|
24 |
|
|
const stream = new PassThrough({objectMode});
|
25 |
|
|
|
26 |
|
|
if (encoding) {
|
27 |
|
|
stream.setEncoding(encoding);
|
28 |
|
|
}
|
29 |
|
|
|
30 |
|
|
stream.on('data', chunk => {
|
31 |
|
|
ret.push(chunk);
|
32 |
|
|
|
33 |
|
|
if (objectMode) {
|
34 |
|
|
len = ret.length;
|
35 |
|
|
} else {
|
36 |
|
|
len += chunk.length;
|
37 |
|
|
}
|
38 |
|
|
});
|
39 |
|
|
|
40 |
|
|
stream.getBufferedValue = () => {
|
41 |
|
|
if (array) {
|
42 |
|
|
return ret;
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
return buffer ? Buffer.concat(ret, len) : ret.join('');
|
46 |
|
|
};
|
47 |
|
|
|
48 |
|
|
stream.getBufferedLength = () => len;
|
49 |
|
|
|
50 |
|
|
return stream;
|
51 |
|
|
};
|