Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: readable cosmetics #28975

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,7 @@ Object.defineProperty(Readable.prototype, 'destroyed', {
// userland will fail
enumerable: false,
get() {
if (this._readableState === undefined) {
return false;
}
return this._readableState.destroyed;
return this._readableState ? this._readableState.destroyed : false;
},
set(value) {
// We ignore the value if the stream
Expand Down Expand Up @@ -266,7 +263,7 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
state.reading = false;
onEofChunk(stream, state);
} else {
var er;
let er;
if (!skipChunkCheck)
er = chunkInvalid(state, chunk);
if (er) {
Expand Down Expand Up @@ -354,14 +351,16 @@ Readable.prototype.isPaused = function() {

// Backwards compatibility.
Readable.prototype.setEncoding = function(enc) {
const state = this._readableState;

if (!StringDecoder)
StringDecoder = require('string_decoder').StringDecoder;
const decoder = new StringDecoder(enc);
this._readableState.decoder = decoder;
state.decoder = decoder;
// If setEncoding(null), decoder.encoding equals utf8
this._readableState.encoding = this._readableState.decoder.encoding;
state.encoding = state.decoder.encoding;

const buffer = this._readableState.buffer;
const buffer = state.buffer;
// Iterate over current buffer to convert already stored Buffers:
let content = '';
for (const data of buffer) {
Expand All @@ -370,7 +369,7 @@ Readable.prototype.setEncoding = function(enc) {
buffer.clear();
if (content !== '')
buffer.push(content);
this._readableState.length = content.length;
state.length = content.length;
return this;
};

Expand Down Expand Up @@ -481,7 +480,7 @@ Readable.prototype.read = function(n) {
// 3. Actually pull the requested chunks out of the buffer and return.

// if we need a readable event, then we need to do some reading.
var doRead = state.needReadable;
let doRead = state.needReadable;
debug('need readable', doRead);

// If we currently have less than the highWaterMark, then also read some
Expand Down Expand Up @@ -511,7 +510,7 @@ Readable.prototype.read = function(n) {
n = howMuchToRead(nOrig, state);
}

var ret;
let ret;
if (n > 0)
ret = fromList(n, state);
else
Expand Down Expand Up @@ -550,7 +549,7 @@ function onEofChunk(stream, state) {
debug('onEofChunk');
if (state.ended) return;
if (state.decoder) {
var chunk = state.decoder.end();
const chunk = state.decoder.end();
if (chunk && chunk.length) {
state.buffer.push(chunk);
state.length += state.objectMode ? 1 : chunk.length;
Expand Down Expand Up @@ -711,7 +710,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) {

let ondrain;

var cleanedUp = false;
let cleanedUp = false;
function cleanup() {
debug('cleanup');
// Cleanup event handlers once the pipe is broken
Expand Down Expand Up @@ -847,11 +846,11 @@ Readable.prototype.unpipe = function(dest) {

if (!dest) {
// remove all.
var dests = state.pipes;
const dests = state.pipes;
state.pipes = [];
state.flowing = false;

for (var i = 0; i < dests.length; i++)
for (let i = 0; i < dests.length; i++)
dests[i].emit('unpipe', this, { hasUnpiped: false });
return this;
}
Expand Down Expand Up @@ -1012,12 +1011,12 @@ function flow(stream) {
// It is an ugly unfortunate mess of history.
Readable.prototype.wrap = function(stream) {
const state = this._readableState;
var paused = false;
let paused = false;

stream.on('end', () => {
debug('wrapped end');
if (state.decoder && !state.ended) {
var chunk = state.decoder.end();
const chunk = state.decoder.end();
if (chunk && chunk.length)
this.push(chunk);
}
Expand Down Expand Up @@ -1055,7 +1054,7 @@ Readable.prototype.wrap = function(stream) {
}

// Proxy certain important events.
for (var n = 0; n < kProxyEvents.length; n++) {
for (let n = 0; n < kProxyEvents.length; n++) {
stream.on(kProxyEvents[n], this.emit.bind(this, kProxyEvents[n]));
}

Expand Down Expand Up @@ -1151,7 +1150,7 @@ function fromList(n, state) {
if (state.length === 0)
return null;

var ret;
let ret;
if (state.objectMode)
ret = state.buffer.shift();
else if (!n || n >= state.length) {
Expand Down