Skip to content

Instantly share code, notes, and snippets.

@dumbmatter
Created October 19, 2021 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dumbmatter/3b1d90bf7cfb6c5951bfffeefecf40aa to your computer and use it in GitHub Desktop.
Save dumbmatter/3b1d90bf7cfb6c5951bfffeefecf40aa to your computer and use it in GitHub Desktop.
Polyfill for Blob.stream
if (!Blob.prototype.stream) {
Blob.prototype.stream = function () {
let offset = 0;
const chunkSize = 64 * 1024;
const blob = this;
return new ReadableStream({
pull(controller) {
return new Promise((resolve) => {
if (offset < blob.size) {
const blobChunk = blob.slice(offset, offset + chunkSize);
const reader = new FileReader();
reader.onload = (event) => {
controller.enqueue(event.currentTarget.result);
offset += chunkSize;
if (offset >= blob.size) {
controller.close();
}
resolve();
};
reader.readAsArrayBuffer(blobChunk);
}
});
},
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment