Skip to content

Conversation

@jasnell
Copy link
Member

@mcollina@ronag ... here's an alternative approach to the Body mixin stuff. Rather than providing the Body mixin directly, these introduce utility functions that can be used by the ecosystem to provide those basic methods, at least in part.

For a very rudimentary example...

const{ arrayBuffer, blob, json, text,}=require('stream/consumers');constkStream=Symbol('kStream');constMyObjectWithBodyMixin{arrayBuffer(){returnarrayBuffer(this[kStream]);}blob(){returnblob(this[kStream]);}json(){returnjson(this[kStream]);}text(){returntext(this[kStream]);}}

These work with ReadableStream, stream.Readable, and async interables.

There's likely a bit more error handling that could be added but I wanted to at least open the PR to give a basic idea.

@nodejs-github-botnodejs-github-bot added the needs-ci PRs that need a full CI run. label Jul 30, 2021
@Mesteery
Copy link
Contributor

Mesteery commented Jul 30, 2021

Could a buffer(stream) function also be added?

Copy link
Member

@ronagronag left a comment

Choose a reason for hiding this comment

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

LGTM. I think error and type checking is done indirectly.

Copy link
Member

@mcollinamcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@targostargos added stream Issues and PRs related to the stream subsystem. semver-minor PRs that contain new features and should be released in the next minor version. labels Aug 1, 2021
@jasnelljasnellforce-pushed the web-streams-consumers branch 3 times, most recently from 344dba7 to d0efbc0CompareAugust 3, 2021 20:47
@nodejs-github-bot

This comment has been minimized.

@targos
Copy link
Member

What happens if the functions are called on object streams?

@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot
Copy link
Collaborator

nodejs-github-bot commented Aug 5, 2021

@jasnell
Copy link
MemberAuthor

@targos:

What happens if the functions are called on object streams?

For blob(), buffer() and arrayBuffer(), the object is coerced using toString() rules. For text() and json() the promises reject because the objects cannot be decoded as utf8 byte sequences. I've extended the tests accordingly.

@jasnelljasnellforce-pushed the web-streams-consumers branch from d0efbc0 to ac912f5CompareAugust 6, 2021 03:21
@jasnelljasnell requested a review from ronagAugust 6, 2021 14:48
@jasnelljasnell added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. and removed needs-ci PRs that need a full CI run. labels Aug 6, 2021
@jasnell
Copy link
MemberAuthor

Landed in c524107

@jasnelljasnell closed this Aug 6, 2021
jasnell added a commit that referenced this pull request Aug 6, 2021
Signed-off-by: James M Snell <[email protected]> PR-URL: #39594 Reviewed-By: Matteo Collina <[email protected]>
danielleadams pushed a commit that referenced this pull request Aug 16, 2021
Signed-off-by: James M Snell <[email protected]> PR-URL: #39594 Reviewed-By: Matteo Collina <[email protected]>
@jimmywarting
Copy link

jimmywarting commented Aug 20, 2021

I wished that buffer() wasn't added
If one would want to make a user-land package out of consumers then you are depending on Buffer that don't fit well into Deno or Browsers.
If someone would have really wanted a Buffer then they could just have done: arrayBuffer(stream).then(Buffer.from)

I'm a bit biased towards Buffer in general cuz it isn't cross env friendly. and it's bloated with stuff TextEncoder and DataView is suppose to solve for you when working with typed arrays

@jimmywarting
Copy link

Would it be optimizable if arrayBuffer() had a totalLength option too?

There would not be pkg like this otherwise that don't need to take up twice the size when it's time to concatinate:
https://github.com/feross/stream-with-known-length-to-buffer

@targos
Copy link
Member

Like #39134, this needs a volunteer to backport to v16.x-staging.

@Mesteery
Copy link
Contributor

I'm willing to take care of it.

Mesteery pushed a commit to Mesteery/node that referenced this pull request Oct 9, 2021
Signed-off-by: James M Snell <[email protected]> PR-URL: nodejs#39594 Reviewed-By: Matteo Collina <[email protected]>
Mesteery pushed a commit to Mesteery/node that referenced this pull request Oct 9, 2021
Signed-off-by: James M Snell <[email protected]> PR-URL: nodejs#39594 Reviewed-By: Matteo Collina <[email protected]>
@Mesteery
Copy link
Contributor

@targos
Copy link
Member

@Mesteery you're right, sorry. It looks like the backport-requested label was added by mistake

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs that have at least one approval, no pending requests for changes, and a CI started.semver-minorPRs that contain new features and should be released in the next minor version.streamIssues and PRs related to the stream subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

@jasnell@Mesteery@nodejs-github-bot@targos@jimmywarting@mcollina@ronag@VoltrexKeyva