Cleanup `_writableState` and `_readableState` access across codebase

This is a meta-issue to keep to track of all usages of _writableState and _readableState outside of streams source. These properties are considered private and should not be used unless absolutely necessary. Usage of them can indicate a few things:

  • the code can be rewritten using existing documented API to achieve the same result;
  • streams lack some consumer functionality and new public API should be introduced;
  • streams lack some implementor functionality and new protected API should be introduced;
  • documentation needs to be added for some parts of private state for implementors;
  • it is an optimization that is and always be possible only in core.

The list of all _writableState and _readableState usages:

src/node.js

lib/_debug_agent.js

lib/_http_server.js

  • L348 socket._readableState.flowing = null (TODO(isaacs): Need a way to reset a stream to fresh state IE, not flowing, and not explicitly paused.). Added in 967b5db by @isaacs;
  • L408 var needPause = socket._writableState.needDrain. Added in 085dd30 by @isaacs ;
  • L445 req._readableState.resumeScheduled. Not sure where this originated, but in 2efe4ab @indutny added oldMode check here;

lib/_tls_legacy.js

  • L421 this._writableState.finished;
  • L508 self._readableState.length > 0;

lib/_tls_wrap.js

lib/child_process.js

lib/crypto.js

  • whole LazyTransform thing. Is it really necessary? Maybe it should go to stream? Maybe it should be public? Maybe transforms should be lazy by default?;
  • L56 this._writableState.decodeStrings = false;
  • L57 this._writableState.defaultEncoding = 'binary';
  • L90 var encoding = this._readableState.encoding || 'buffer' (crypto: remove use of this._readableState #610);

lib/fs.js

  • L1624 allocNewPool(this._readableState.highWaterMark);

lib/net.js

  • L162 this._writableState.decodeStrings = false (Cleanup stream state in net #465);
  • L174 this._readableState.flowing = false (Cleanup stream state in net #465);
  • L196 this._readableState.ended (Cleanup stream state in net #465);
  • L226 self._readableState.ended;
  • L242 this._readableState.ended = true (comment: ended should already be true, since this is called after the EOF errno and onread has eof'ed) (Cleanup stream state in net #465);
  • L243 this._readableState.endEmitted;
  • L362 this._writableState.length;
  • L392 this._readableState.endEmitted;
  • L405 socket._writableState.length;
  • L415 if (this._writableState.finished);
  • L429 self._writableState.errorEmitted (Cleanup stream state in net #465);
  • L433 self._writableState.errorEmitted = true (Cleanup stream state in net #465);
  • L535 self._readableState.length === 0;
  • L715 state.getBuffer();
  • L842 this._readableState.reading = false;
  • L843 this._readableState.ended = false;
  • L844 this._readableState.endEmitted = false;
  • L845 this._writableState.ended = false;
  • L846 this._writableState.ending = false;
  • L847 this._writableState.finished = false;
  • L848 this._writableState.errorEmitted = false (Cleanup stream state in net #465);

lib/zlib.js

List of used properties:

_readableState

  • reading;
  • objectMode;
  • flowing (boolean?) is used to determine which mode readable stream is in; can be true, false or null; `null is the initial state which means that is implicitly paused;
  • resumeScheduled;
  • length;
  • encoding;
  • highWaterMark;
  • ended;
  • endEmitted;

_writableState

  • needDrain;
  • ended;
  • ending;
  • finished;
  • errorEmitted;
  • decodeStrings;
  • defaultEncoding;
  • length;
  • getBuffer();

/cc @chrisdickinson