w3c / w3c/FileAPI

Consider Blob.fromStream (returns a Promise<Blob>)

Open
#140 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request TPAC2024
Dominant language
HTML
Stars
118
Forks
52
Avg merge
9d 16h
Merged PRs (30d)
1

Description

I'm filing this as a tracking issue, although I don't think it's particularly urgent. But maybe web developers have run into this more, in which case hearing from them would be great.

Anyway, right now, to convert a ReadableStream to a Blob, you have to do

const blob = await new Response(stream).blob();

which is pretty silly. Just as blob.stream() was added to avoid the silly new Request(blob).body pattern, perhaps we should consider adding a promise-returning Blob.fromStream() so you could do

const blob = await Blob.fromStream(stream);

There is a bigger savings if we add an options parameter:

const blob1 = new Blob([await new Response(stream).blob()], options);
const blob2 = Blob.fromStream(stream, options);

Points to consider:

  • We could instead make this stream.toBlob(), but I feel the layering of keeping streams ignorant of blobs is a bit cleaner.
  • We may not want to introduce this because it's better if people avoid using blobs? (Or is it just blob URLs that we dislike?)
  • If someone wanted a File, we could either add File.fromStream(stream, fileName, options) or we could have people do new File([Blob.fromStream(stream)], fileName, options). Probably File.fromStream() is nicer.
  • This does not allow creating a blob that represents an "in progress" stream, which I've heard people have wanted in order to have self-referential blobs. That would require new concepts and infrastructure. This proposal is basically just exposing existing infrastructure in a more straightforward way.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no repository files or tests. Start by comparing the current Blob, ReadableStream, and Response(...).blob() APIs described here; done would require a settled API design for Blob.fromStream, its options, and the related File question, followed by the applicable specification and conformance tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
html
Domain
api, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.