Re: [w3c/FileAPI] Consider Blob.fromStream (returns a Promise<Blob>) (#140)

In `node-fetch` / `fetch-blob` package I have been figuring about such method myself.
I found that NodeJS new stream consumer `(node:stream/consumers).blob(iterable)` to be somewhat useful but one thing i did not like about it was that I had to do `new Blob([part], { type: contentType })` afterwards, b/c it currently lacks options for it...

One thing i found useful about this proposal is that we could then have a way of creating quite large blobs backed up by the file system if this is needed. (could be useful for eg FileSaver.js)

---

if we could hint about a known size then there wouldn't even be a reason for a person to have to wait for the concatination to finish either, you could return a blob directly.
```js
const blob = Blob.fromStream(stream, { size: 1024 })
```

I think that the ability to generate your own blob backed with whatever method you may prefer would be a neat feature.

Another pretty useful thing i built in `fetch-blob` was the ability to create a File/Blob-like item backed up by the filesystem that could be handled by the Blob class itself, (now undici have adapter theirs FormData impl to be able to accept any 3th party BlobLike items also - same as node-fetch's FormData implementation)

so instead of having `blob = await blobFromPath('./readme.md')` i could maybe have something like:

```js
var blob = Blob.from({
  size: 1024,

  type: 'text/plain',

  slice(start, end) {
    // return a new blob with a different size/offset
    return Blob.from(...)
  },

  /**
   * return your own readable stream method, that could
   * technically be called multiple times over and over.
   * 
   * @return {ReadableStream<Uint8Array>}
   */
  stream() {
    // create your own readable stream method that returns the data
    const ts = new TransformStream({...})
    fetch(url).then(res => res.body.pipeTo(ts.writable))
    return ts.readable
  }
})
```

this is esentially what i have built in [fetch-blob](https://github.com/node-fetch/fetch-blob/blob/main/from.js#L59)
I have also made a [Blob/File like class out of a http request](https://github.com/transcend-io/conflux/issues/31) that supports byte ranges in order to parse zip files and download only the parts of the zip and unpack individual files from the entire thing.

if they could somehow be representable by a native Blob class then you could do so much more with them. like creating a Blob URL \w `URL.createObjectURL` and play it in a video element. but this can't work cuz they are only blob like items and you can't create other native blobs with this made up blob parts `new Blob( [ 'str', blobLikeItem ], { type: 'text/html' })` which is a bit sad 

-- 
Reply to this email directly or view it on GitHub:
https://github.com/w3c/FileAPI/issues/140#issuecomment-1029273966
You are receiving this because you are subscribed to this thread.

Message ID: <w3c/FileAPI/issues/140/1029273966@github.com>

Received on Thursday, 3 February 2022 18:24:06 UTC