-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider Blob.fromStream (returns a Promise<Blob>) #140
Comments
We only dislike |
Yep, blob URLs are bad, both blobs in general are fine. I'd be supportive of adding a Blob.fromStream method. |
AFAICT
throws a
The concept appears to be similar what is possible with Native File System, e.g.,
|
Can you explain why? What is the available substitute? |
I would also like to know why. I presume it's b/c developers forget to revoke them? but they are currently so "badly" needed b/c there is no substitute for creating video/audio urls, download links, web workers, iframes, css & font link's from dynamic generated content. if we want to get rid of them then you should be able to create them from a ReadableStream, Request or Response or a Blob directly. In One thing i found useful about this Blob.fromStream 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. 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. A pretty useful thing i built in my arbitrary So instead of having var blob = Blob.from({
size: 1024,
type: 'text/plain',
// `start` and `end` would be normalized by eg: `blob.slice(-3)`
// to never be larger than blob size, or below 0
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 rs = fs.createReadStream(path, { start, end })
const wrs = stream.Readable.toWeb(rs)
// const [rs1, rs2] = wrs.tee() // using `tee()` could provide a way to cache the body if you need to reuse it.
return wrs
}
}) this is essentially what i have built in fetch-blob (but in another format) and then wrapped blob-like items around a new blob with if they could somehow be representable by a native Blob class then you could do so much more with them. like:
but all of this can't work cuz they are only blob look-a-like items and you can't create other native blobs with this made up blob parts. So this currently dose not work: new globalThis.Blob([ 'prefix', blobLikeItem ], { type })` which is a bit sad |
This is something i badly want to have right now. I feel like Even being able to create File that are not in progress at all would be useful too. (just reading the stream when it calls for it) would be useful as a way to create cloud provided file handles as a way to list the items in a directory without having to download everything |
TPAC 2024: General consensus with Chromium & FF that this seems nice. But not urgent, we are all happy to get patches. No strong opinion from WebKit. Likely would impl in C++. Easy polyfill. |
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 aBlob
, you have to dowhich is pretty silly. Just as
blob.stream()
was added to avoid the sillynew Request(blob).body
pattern, perhaps we should consider adding a promise-returningBlob.fromStream()
so you could doThere is a bigger savings if we add an
options
parameter:Points to consider:
stream.toBlob()
, but I feel the layering of keeping streams ignorant of blobs is a bit cleaner.File
, we could either addFile.fromStream(stream, fileName, options)
or we could have people donew File([Blob.fromStream(stream)], fileName, options)
. ProbablyFile.fromStream()
is nicer.The text was updated successfully, but these errors were encountered: