Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 11, 2019
1 parent 4ddb7f0 commit 8fbff0d
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions buffer-stream.js
Expand Up @@ -6,46 +6,47 @@ module.exports = options => {

const {array} = options;
let {encoding} = options;
const buffer = encoding === 'buffer';
const isBuffer = encoding === 'buffer';
let objectMode = false;

if (array) {
objectMode = !(encoding || buffer);
objectMode = !(encoding || isBuffer);
} else {
encoding = encoding || 'utf8';
}

if (buffer) {
if (isBuffer) {
encoding = null;
}

let len = 0;
const ret = [];
const stream = new PassThroughStream({objectMode});

if (encoding) {
stream.setEncoding(encoding);
}

let length = 0;
const chunks = [];

stream.on('data', chunk => {
ret.push(chunk);
chunks.push(chunk);

if (objectMode) {
len = ret.length;
length = chunks.length;
} else {
len += chunk.length;
length += chunk.length;
}
});

stream.getBufferedValue = () => {
if (array) {
return ret;
return chunks;
}

return buffer ? Buffer.concat(ret, len) : ret.join('');
return isBuffer ? Buffer.concat(chunks, length) : chunks.join('');
};

stream.getBufferedLength = () => len;
stream.getBufferedLength = () => length;

return stream;
};

0 comments on commit 8fbff0d

Please sign in to comment.