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

fix: Ensure Duplex streams are returned unwrapped in stream operators #55394

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

KunalKumar-1
Copy link

@KunalKumar-1 KunalKumar-1 commented Oct 15, 2024

Updated the implementation of the readable.compose() method to ensure that it correctly returns a Duplex stream when applicable, avoiding unintended conversion to Readable streams.
fixes: #55203

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/streams

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. stream Issues and PRs related to the stream subsystem. labels Oct 15, 2024
@RedYetiDev
Copy link
Member

Please add a test

@KunalKumar-1
Copy link
Author

@RedYetiDev test added

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 15, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 15, 2024
@nodejs-github-bot
Copy link
Collaborator

Copy link

codecov bot commented Oct 15, 2024

Codecov Report

Attention: Patch coverage is 66.66667% with 3 lines in your changes missing coverage. Please review.

Project coverage is 88.35%. Comparing base (51d8146) to head (90ac00d).
Report is 9 commits behind head on main.

Files with missing lines Patch % Lines
lib/stream.js 66.66% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #55394      +/-   ##
==========================================
- Coverage   88.41%   88.35%   -0.07%     
==========================================
  Files         652      652              
  Lines      186878   186897      +19     
  Branches    36061    36024      -37     
==========================================
- Hits       165226   165126     -100     
- Misses      14902    15017     +115     
- Partials     6750     6754       +4     
Files with missing lines Coverage Δ
lib/stream.js 94.37% <66.66%> (-5.63%) ⬇️

... and 32 files with indirect coverage changes

@KunalKumar-1
Copy link
Author

KunalKumar-1 commented Oct 16, 2024

@mcollina why the test cases are failing ?

Copy link
Member

@ronag ronag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm on the fence here. The returned stream should not be written to so semantically it makes more sense to return an explicit readable. Though there is some performance costs associated with this.

@mcollina @lpinca wdyt?

@KunalKumar-1
Copy link
Author

KunalKumar-1 commented Oct 16, 2024

@ronag I see your point regarding the stream semantics. If performance impact is minimal, then returning an explicit Readable does make sense to clarify the intended use.
Could you clarify the exact changes you’d like to see here?

@ronag
Copy link
Member

ronag commented Oct 16, 2024

Could you clarify the exact changes you’d like to see here?

Well if consider my concern. Then we should just leave this as is? We might ned update docs/typings to clarify that if the first param (this) to compose is not writable then we return a readable instead of duplex, ditto with if the last stream is not readable, we return a writable.

@ronag
Copy link
Member

ronag commented Oct 16, 2024

Alternatively returning a new Duplex with writable: false

@KunalKumar-1
Copy link
Author

Alternatively returning a new Duplex with writable: false

After considering both options, would it be preferable to return a new Duplex with writable: false, or should we lean towards your suggestion of simply returning a Readable or Writable stream based on the first and last streams in the composition?

I’m inclined to go with whichever approach aligns best with the project’s consistency and performance considerations !

@ronag
Copy link
Member

ronag commented Oct 16, 2024

I'm happy with the Duplex approach. Give it a day or two for others to also voice their opinions before you spend more time on this. Otherwise, you might risk doing more work.

@KunalKumar-1
Copy link
Author

KunalKumar-1 commented Oct 16, 2024

I'm happy with the Duplex approach. Give it a day or two for others to also voice their opinions before you spend more time on this. Otherwise, you might risk doing more work.

Yeah , sure It sounds great.
I Will start working on it after a day or two ....
After hearing some options.

@Renegade334
Copy link
Contributor

Renegade334 commented Oct 17, 2024

I'm on the fence here. The returned stream should not be written to so semantically it makes more sense to return an explicit readable. Though there is some performance costs associated with this.

readable.compose(x) should have the same behaviour as stream.compose(readable, x) imo (ie. return a read-only Duplex), given that the former is essentially just a wrapper for the latter. This also keeps things consistent if called as duplex.compose(x), which should also return a Duplex (read-only or read-write depending on the source stream).

After considering both options, would it be preferable to return a new Duplex with writable: false

It's worth mentioning that this is already what's returned by the internal compose() operator, so wouldn't need any further handling.

Indeed, if the desired behaviour of readable.compose() is that it just passes through the Duplex returned by the compose operator, then it wouldn't need to be included in streamReturningOperators at all; the operator could just be added straight onto Readable.prototype.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci PRs that need a full CI run. stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

stream: readable.compose fails to return a Duplex
6 participants