From a4033fd5a9470de80317fd87da72f752ec6a6b13 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Sat, 21 Nov 2026 08:06:42 +0000 Subject: [PATCH] stream,http,http2: use public stream state accessors Replace direct _readableState/_writableState access outside lib/internal/streams/ with the public accessors so internal modules no longer depend on the state objects' shape. Added accessors: - readableErrored / writableErrored (per-side; on a Duplex stream.errored only reflects the readable side, and writable-side getters must be wired into Duplex because the non-enumerable property copy loop skips them) - settable readableHighWaterMark / writableHighWaterMark (validated via a shared validateHighWaterMark helper, ERR_INVALID_ARG_VALUE) - writableDefaultEncoding, readableErrorEmitted / writableErrorEmitted - readableResumeScheduled (isPaused() is not equivalent: resume() with a 'readable' listener leaves isPaused() true while resumeScheduled is set) - autoDestroy get/set on Readable and Writable; a Duplex accessor keeps _readableState/_writableState.autoDestroy in sync when set dynamically Call sites migrated to the accessors: - lib/internal/http2/core.js, lib/internal/http/outgoing.js, lib/internal/http/incoming.js, lib/internal/http/agent.js, lib/internal/http/server.js, lib/internal/http2/compat.js, lib/zlib.js, lib/internal/console/constructor.js, lib/internal/fs/streams.js - lib/internal/webstreams/adapters.js drops duck-typing checks for instanceof Readable / Duplex - lib/internal/http2/core.js onStreamClose() now reads Http2Stream[kState].receivedEnd (set on any null push of the readable side) instead of _readableState.ended, so a server-initiated push stream that ends cleanly is no longer misclassified as an abort. Also fix a regression from the settable high-water-mark setter: its validation made new IncomingMessage() throw ERR_INVALID_ARG_VALUE, so http/incoming.js now only syncs the socket HWM when it is defined. Non-streams-core _readableState/_writableState references: 43 -> 18. --- doc/api/stream.md | 98 +++++++++++- lib/internal/console/constructor.js | 2 +- lib/internal/fs/streams.js | 8 +- lib/internal/http/agent.js | 2 +- lib/internal/http/incoming.js | 4 +- lib/internal/http/outgoing.js | 2 +- lib/internal/http/server.js | 5 +- lib/internal/http2/compat.js | 2 +- lib/internal/http2/core.js | 34 ++-- lib/internal/stream_base_commons.js | 2 +- lib/internal/streams/duplex.js | 26 +++ lib/internal/streams/readable.js | 44 ++++++ lib/internal/streams/state.js | 15 +- lib/internal/streams/writable.js | 47 ++++++ lib/internal/webstreams/adapters.js | 5 +- lib/zlib.js | 2 +- .../parallel/test-stream-errored-accessors.js | 74 +++++++++ .../test-stream-settable-highwatermark.js | 91 +++++++++++ test/parallel/test-stream-state-accessors.js | 149 ++++++++++++++++++ 19 files changed, 579 insertions(+), 33 deletions(-) create mode 100644 test/parallel/test-stream-errored-accessors.js create mode 100644 test/parallel/test-stream-settable-highwatermark.js create mode 100644 test/parallel/test-stream-state-accessors.js diff --git a/doc/api/stream.md b/doc/api/stream.md index a676b98923df..5ec09eb289da 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -762,6 +762,19 @@ myStream.destroy(); console.log(myStream.destroyed); // true ``` +##### `writable.autoDestroy` + + + +* Type: {boolean} + +Returns whether the stream should automatically call [`writable.destroy()`][writable-destroy] +after `'finish'`. Setting this property updates the `autoDestroy` option used +when the stream was created. On a [`Duplex`][] stream, setting this updates +both the readable and the writable side. + ##### `writable.end([chunk[, encoding]][, callback])` + +* Type: {string} + +The default encoding used when a string is written without an explicit `encoding` +argument. + ##### `writable.errored` + +* Type: {Error} + +Returns error if the writable side of the stream has been destroyed with an +error. This is the writable-side counterpart of `writable.errored`. On a +[`Duplex`][] stream, `errored` reflects the readable side, so use +`writableErrored` to inspect the writable side. + +##### `writable.writableErrorEmitted` + + + +* Type: {boolean} + +Returns whether the `'error'` event has been emitted on the writable side. + ##### `writable.writableFinished` + +* Type: {boolean} + +Returns whether the stream should automatically call [`readable.destroy()`][readable-destroy] +after `'end'`. Setting this property updates the `autoDestroy` option used when +the stream was created. On a [`Duplex`][] stream, setting this updates both the +readable and the writable side. + ##### `readable.isPaused()` + +* Type: {boolean} + +Returns whether [`readable.resume()`][stream-resume] has been called but the +scheduled resume has not yet been processed. + ##### `readable.readableEncoding` + +* Type: {Error} + +Returns error if the readable side of the stream has been destroyed with an +error. This is the readable-side counterpart of `readable.errored`. + +##### `readable.readableErrorEmitted` + + + +* Type: {boolean} + +Returns whether the `'error'` event has been emitted on the readable side. + ##### `readable.readableFlowing`