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

Native stream #19

Open
jimmywarting opened this issue Aug 29, 2018 · 9 comments
Open

Native stream #19

jimmywarting opened this issue Aug 29, 2018 · 9 comments

Comments

@jimmywarting
Copy link
Contributor

jimmywarting commented Aug 29, 2018

Because the implementations that I found really didn't cut the mustard :/

Really? How about this hack? 😉

const readableStream = new  new Response(file).body
const blob = await new Response(readableStream).blob()

Anyhow, just wanted to share some light to alternative solutions, this is rather hacky but there is an issue for that at w3c/FileAPI#40

You can close this issue. suggest this in the readme or just discard this totally

@DamonOehlman
Copy link
Owner

@jimmywarting if there is a hack, then I reckon I'll definitely include that in the README :)

@jimmywarting
Copy link
Contributor Author

doe it is a different streaming api.

@jimmywarting
Copy link
Contributor Author

chrome (and soon FF) implemented the new reading methods readableStream = Blob.stream()

@feross
Copy link
Collaborator

feross commented Aug 23, 2019

@jimmywarting Good to know! It seems that even when this API is available this package should still offer a Node.js stream interface. Folks who need WHATWG streams can just use blob.stream() with no need for an npm package, right?

@jimmywarting
Copy link
Contributor Author

jimmywarting commented Aug 23, 2019

right, ppl who want to use whatwg stream don't need this package.
they can just do blob.stream() or new Response(blob).body for a more cross browser support

but if you really want to use something that is more cross platform supported (and don't want to care if it is a node or a whatwg stream) then you would treat both streams as a async iterable.
Node streams and whatwg both have a symbol.asyncIterator that means you can use for await on both of them

// stream can be either a node stream of a whatwg stream
for await (let chunk of stream) {
  console.log(chunk) // instance of Uint8array
}

the only difference is that whatwg streams will yield a Uint8Array and node will yield Buffer. But Buffer also inherit from Uint8Array, so if you treat both as if it where a Uint8array then you are golden.

https://github.com/cross-js/cross-js#dont-create-node-or-web-readable-stream-yourself

@feross
Copy link
Collaborator

feross commented Aug 23, 2019

@jimmywarting Cool, thanks for explaining. This is exactly what I thought. Is there a cross-platform way to handle WHATWG + Node.js writable streams?

@jimmywarting
Copy link
Contributor Author

jimmywarting commented Aug 23, 2019

Is there a cross-platform way to handle WHATWG + Node.js writable streams?

like i wrote earlier

// stream can be either a node stream of a whatwg stream
for await (let chunk of stream) {
  console.log(chunk) // instance of Uint8array
}

you could also do:

const iterator = stream[symbol.asyncIterator]() // either node or whatwg stream
const result = await iterator.next() // { done: Boolean, value: Uint8Array }

@feross
Copy link
Collaborator

feross commented Sep 5, 2019

@jimmywarting I was asking about writable streams, i.e. sinks not sources!

@jimmywarting
Copy link
Contributor Author

oh, sorry my mistake.
I'm not 100% sure of how i would handle a writable stream in a cross-platform way.
most often you are fine with just having a readable stream or a producer (generator)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants