← Back to Pull requests

stream,http,http2: use public stream state accessors #67677

Open
unknown opened this pull request with 475 lines diff across 5 files
View on GitHub
unknown unknown commented on Nov 21, 2026 13:16 UTC
Author

Motivation

lib/internal/http/, lib/internal/fs/, lib/zlib.js, lib/internal/console/
and lib/internal/webstreams/
reach into _readableState / _writableState
directly. That couples non-streams modules to the internal state objects'
shape, so refactoring lib/internal/streams/ (e.g. replacing boolean flags
with bitmask fields) silently ripples outward.

This PR exposes the needed state through the same public accessor surface
already used for readableLength, writableLength, etc., and migrates every
call site that has a public equivalent. Depending on the internal state object
outside lib/internal/streams/ is reduced from 43 references to 17, and
the remaining ones are genuinely internal writes / state hand-offs with no
sensible public accessor.

New public accessors

Readable / Writable (and Duplex where applicable):

  • readableErrored / writableErrored — exposes the per-side errored

  • state. On a Duplex, stream.errored reflects only the readable side, so
    the writable-side getter is wired into Duplex too. (Note: the
    Object.keys(Writable.prototype) copy loop that builds Duplex.prototype
    skips non-enumerable getters, so these are added explicitly.)
  • readableHighWaterMark / writableHighWaterMark — now settable, validated

  • through a shared validateHighWaterMark helper that throws
    ERR_INVALID_ARG_VALUE on non-integer / negative values.
  • writableDefaultEncoding — getter.

  • readableErrorEmitted / writableErrorEmitted — getters.

  • readableResumeScheduled — getter. isPaused() is not equivalent:

  • calling resume() while a 'readable' listener is attached leaves
    isPaused() true while resumeScheduled is set, so the flag is exposed
    verbatim to preserve behaviour.
  • autoDestroy — getter/setter on Readable and Writable. The Duplex

  • accessor keeps _readableState.autoDestroy and _writableState.autoDestroy
    in sync when set dynamically, matching how destroyed is already handled.

    Call sites migrated

  • lib/internal/http2/core.js

  • lib/internal/http/outgoing.js, incoming.js, agent.js, server.js

  • lib/internal/fs/streams.js

  • lib/zlib.js

  • lib/internal/console/constructor.js

  • lib/internal/http2/compat.js

  • lib/internal/webstreams/adapters.js (duck-typing checks switched to

  • instanceof Readable / instanceof Duplex)

    http2 receivedEnd

    lib/internal/http2/core.js onStreamClose() now reads
    Http2Stream[kState].receivedEnd instead of _readableState.ended. The flag
    is set by an override of Http2Stream.prototype.push on any null push of the
    readable side, so it mirrors _readableState.ended exactly and covers
    server-initiated push streams whose readable side ends on a clean close (not a
    peer END_STREAM).

    This reconciles cleanly with the existing STREAM_FLAGS_CLEAN_REQUEST_RESET
    handling: receivedEnd drives the "peer did not END_STREAM → abort the
    request" decision, while the clean-reset flag governs keeping a client
    response readable. Http2Stream._destroy() reads the same receivedEnd flag
    instead of _readableState.ended when recording an incomplete request.

    Fix a regression

    Making the high-water-mark setter validating caused
    new IncomingMessage(<truthy non-socket>) to throw ERR_INVALID_ARG_VALUE
    with an undefined highWaterMark. lib/internal/http/incoming.js now only
    syncs the socket high-water mark when it is defined.

    Tests

    New tests:

  • test/parallel/test-stream-errored-accessors.js

  • test/parallel/test-stream-settable-highwatermark.js

  • test/parallel/test-stream-state-accessors.js
  • And an extended test-stream-duplex-/test-stream- coverage for the
    autoDestroy setter. Ran the full test/parallel stream, http2 (300, incl.
    compat), http, fs, zlib and console suites.

    Diff: 475 lines (+356 / -119) across 5 files Reviewer: Ethan-Arrowood Review duration: 10108m
    All Vouch review confidence checks passed
    Residual risk 0.24 = Change Risk (0.46) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.46 · 475 lines diff across 5 files
    0.46
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.369
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #67677"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.7% (MEDIUM) · Higher = larger blast radius
    45.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 24.4% · Safe to merge
    24.4%
    Repository