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

stream: utility consumers for web and node.js streams #39594

Closed
wants to merge 1 commit into from

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Jul 30, 2021

@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');

const kStream = Symbol('kStream');

const MyObjectWithBodyMixin {

  arrayBuffer() { return arrayBuffer(this[kStream]); }

  blob() { return blob(this[kStream]); }

  json() { return json(this[kStream]); }

  text() { return text(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-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Jul 30, 2021
@Mesteery
Copy link
Member

Mesteery commented Jul 30, 2021

Could a buffer(stream) function also be added?

doc/api/webstreams.md Outdated Show resolved Hide resolved
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.

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

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

@targos targos 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
@jasnell jasnell force-pushed the web-streams-consumers branch 3 times, most recently from 344dba7 to d0efbc0 Compare August 3, 2021 20:47
@nodejs-github-bot

This comment has been minimized.

@targos
Copy link
Member

targos commented Aug 4, 2021

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
Member Author

jasnell commented Aug 6, 2021

@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.

Signed-off-by: James M Snell <jasnell@gmail.com>
@jasnell jasnell requested a review from ronag August 6, 2021 14:48
@jasnell jasnell 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
Member Author

jasnell commented Aug 6, 2021

Landed in c524107

@jasnell jasnell closed this Aug 6, 2021
jasnell added a commit that referenced this pull request Aug 6, 2021
Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: #39594
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
danielleadams pushed a commit that referenced this pull request Aug 16, 2021
Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: #39594
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
@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

targos commented Oct 9, 2021

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

@Mesteery
Copy link
Member

Mesteery commented Oct 9, 2021

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 <jasnell@gmail.com>

PR-URL: nodejs#39594
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Mesteery pushed a commit to Mesteery/node that referenced this pull request Oct 9, 2021
Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: nodejs#39594
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
@Mesteery
Copy link
Member

Mesteery commented Oct 9, 2021

@targos
Copy link
Member

targos commented Oct 9, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. semver-minor PRs that contain new features and should be released in the next minor version. stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants