Re: [whatwg/streams] ReadableStream.from(X) (#1018)

> btw, An idea that poped up in [whatwg/fetch#809](https://github.com/whatwg/fetch/issues/809) is if we maybe could have something like `Symbol.toStream`... (just throwing out alternative ideas)

In the future, we could consider calling `x[Symbol.toStream]()` (if it exists) from `ReadableStream.from(x)` to allow for custom conversions. But for now, I think `Symbol.asyncIterator` will suffice for most use cases.

> PS. have BYOB reader in mind also when designing this?

You wouldn't benefit much from having a BYOB reader. We'll be reading chunks with `AsyncIterator.next()`, which has no way for passing in a buffer to use for the returned chunk. So even if we were to support a BYOB reader for the constructed stream, we would still be copying bytes from `it.next()`'s result over to the buffer provided to `reader.readInto(view)`.

That said, it would still be nice if there were an easy way to convert any stream with byte chunks into a proper byte stream, even if it incurred extra copies. Something like this could work:
```js
ReadableStream.from(x, { type: "bytes" })
```

This would be roughly equivalent to a "special" identity transform:
```js
ReadableStream.from(x).pipeThrough(new TransformStream({ readableType: "bytes" }))
```
Note: this does not work yet. This assumes that `TransformStream` will at some point support setting `readableType` and/or `writableType` to `"bytes"`. These properties are already reserved for this purpose (I think?) in [the TransformStream constructor](https://streams.spec.whatwg.org/#ts-constructor), but they can only be `undefined` at the moment.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/1018#issuecomment-573450716

Received on Sunday, 12 January 2020 19:53:19 UTC