sd-streams icon indicating copy to clipboard operation
sd-streams copied to clipboard

Missing Blob/File stream methods

Open rotu opened this issue 4 years ago • 3 comments

This polyfill works great for streaming fetches, but it doesn't patch the ReadableStream implementation of Blob (and by extension, File).

The following returns a function in Chrome but not in Firefox when using the polyfill: x=new Blob().stream().pipeThrough

rotu avatar Jun 18 '21 09:06 rotu

Thanks for the good work!

Looks like this already has been implemented - any chance to get it released? I'm using the unpkg hosted version at https://unpkg.com/@stardazed/streams-polyfill/dist/sd-streams-polyfill.min.js and there Blob is unpatched on FF

jo avatar Apr 28 '22 22:04 jo

I made a test implementation indeed, but there's a fundamental problem where this will not just work as a simple method polyfill. Without resorting to terrible page scanning and other hackery there is for example no way to get a patched File back from an <input type="file"> onchange event.

There were some other issues as well but it's been a while but for me not being able to just use a File without calling some wrapper function was a dealbreaker, polyfill-wise.

Also I can't remember how well I tested the Blob modification. I will have to look back at it, but schedule for that is currently undetermined.

zenmumbler avatar Apr 29 '22 13:04 zenmumbler

Thanks for your response, @zenmumbler! I look forward to it, if something comes once :)

I would like to compress blobs in this way:

blob.stream().pipeThrough(new CompressionStream('gzip'))

which is not yet polyfillable. I use this stupid patch to work around the problem for the time being:

export class PatchableReadableStream extends ReadableStream {
  constructor (reader) {
    super({
      async start(controller) {
        while (true) {
          const { done, value } = await reader.read()
          if (done) break
          controller.enqueue(value)
        }
        controller.close()
        reader.releaseLock()
      }
    })
  }
}

const readableStream = new PatchableReadableStream(blob.stream().getReader())
readableStream.pipeThrough(new CompressionStream('gzip'))

Not pretty, but works.

jo avatar May 01 '22 21:05 jo