Last change
on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
841 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | '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 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.