DamonOehlman / filestream

W3C File Reader API streaming interfaces
30 stars 11 forks source link

Native stream #19

Open jimmywarting opened 6 years ago

jimmywarting commented 6 years ago

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 https://github.com/w3c/FileAPI/issues/40

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

DamonOehlman commented 6 years ago

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

jimmywarting commented 6 years ago

doe it is a different streaming api.

jimmywarting commented 5 years ago

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

feross commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

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

jimmywarting commented 5 years ago

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)