Open jimmywarting opened 6 years ago
@jimmywarting if there is a hack, then I reckon I'll definitely include that in the README :)
doe it is a different streaming api.
chrome (and soon FF) implemented the new reading methods readableStream = Blob.stream()
@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?
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
@jimmywarting Cool, thanks for explaining. This is exactly what I thought. Is there a cross-platform way to handle WHATWG + Node.js writable streams?
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 }
@jimmywarting I was asking about writable streams, i.e. sinks not sources!
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)
Really? How about this hack? 😉
Anyhow, just wanted to share some light to alternative solutions, this is rather hacky but there is an issue for that at https://github.com/w3c/FileAPI/issues/40
You can close this issue. suggest this in the readme or just discard this totally