Skip to content

Commit 7ff1e23

Browse files
committed
stream: readable cosmetics
1 parent bd02775 commit 7ff1e23

1 file changed

Lines changed: 18 additions & 19 deletions

File tree

lib/_stream_readable.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,7 @@ Object.defineProperty(Readable.prototype, 'destroyed', {
192192
// userland will fail
193193
enumerable: false,
194194
get() {
195-
if (this._readableState === undefined) {
196-
return false;
197-
}
198-
return this._readableState.destroyed;
195+
return this._readableState ? this._readableState.destroyed : false;
199196
},
200197
set(value) {
201198
// We ignore the value if the stream
@@ -266,7 +263,7 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
266263
state.reading = false;
267264
onEofChunk(stream, state);
268265
} else {
269-
var er;
266+
let er;
270267
if (!skipChunkCheck)
271268
er = chunkInvalid(state, chunk);
272269
if (er) {
@@ -354,14 +351,16 @@ Readable.prototype.isPaused = function() {
354351

355352
// Backwards compatibility.
356353
Readable.prototype.setEncoding = function(enc) {
354+
const state = this._readableState;
355+
357356
if (!StringDecoder)
358357
StringDecoder = require('string_decoder').StringDecoder;
359358
const decoder = new StringDecoder(enc);
360-
this._readableState.decoder = decoder;
359+
state.decoder = decoder;
361360
// If setEncoding(null), decoder.encoding equals utf8
362-
this._readableState.encoding = this._readableState.decoder.encoding;
361+
state.encoding = state.decoder.encoding;
363362

364-
const buffer = this._readableState.buffer;
363+
const buffer = state.buffer;
365364
// Iterate over current buffer to convert already stored Buffers:
366365
let content = '';
367366
for (const data of buffer) {
@@ -370,7 +369,7 @@ Readable.prototype.setEncoding = function(enc) {
370369
buffer.clear();
371370
if (content !== '')
372371
buffer.push(content);
373-
this._readableState.length = content.length;
372+
state.length = content.length;
374373
return this;
375374
};
376375

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

483482
// if we need a readable event, then we need to do some reading.
484-
var doRead = state.needReadable;
483+
let doRead = state.needReadable;
485484
debug('need readable', doRead);
486485

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

514-
var ret;
513+
let ret;
515514
if (n > 0)
516515
ret = fromList(n, state);
517516
else
@@ -550,7 +549,7 @@ function onEofChunk(stream, state) {
550549
debug('onEofChunk');
551550
if (state.ended) return;
552551
if (state.decoder) {
553-
var chunk = state.decoder.end();
552+
const chunk = state.decoder.end();
554553
if (chunk && chunk.length) {
555554
state.buffer.push(chunk);
556555
state.length += state.objectMode ? 1 : chunk.length;
@@ -711,7 +710,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
711710

712711
let ondrain;
713712

714-
var cleanedUp = false;
713+
let cleanedUp = false;
715714
function cleanup() {
716715
debug('cleanup');
717716
// Cleanup event handlers once the pipe is broken
@@ -847,11 +846,11 @@ Readable.prototype.unpipe = function(dest) {
847846

848847
if (!dest) {
849848
// remove all.
850-
var dests = state.pipes;
849+
const dests = state.pipes;
851850
state.pipes = [];
852851
state.flowing = false;
853852

854-
for (var i = 0; i < dests.length; i++)
853+
for (let i = 0; i < dests.length; i++)
855854
dests[i].emit('unpipe', this, { hasUnpiped: false });
856855
return this;
857856
}
@@ -1012,12 +1011,12 @@ function flow(stream) {
10121011
// It is an ugly unfortunate mess of history.
10131012
Readable.prototype.wrap = function(stream) {
10141013
const state = this._readableState;
1015-
var paused = false;
1014+
let paused = false;
10161015

10171016
stream.on('end', () => {
10181017
debug('wrapped end');
10191018
if (state.decoder && !state.ended) {
1020-
var chunk = state.decoder.end();
1019+
const chunk = state.decoder.end();
10211020
if (chunk && chunk.length)
10221021
this.push(chunk);
10231022
}
@@ -1055,7 +1054,7 @@ Readable.prototype.wrap = function(stream) {
10551054
}
10561055

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

@@ -1151,7 +1150,7 @@ function fromList(n, state) {
11511150
if (state.length === 0)
11521151
return null;
11531152

1154-
var ret;
1153+
let ret;
11551154
if (state.objectMode)
11561155
ret = state.buffer.shift();
11571156
else if (!n || n >= state.length) {

0 commit comments

Comments
 (0)