Created
October 19, 2021 17:07
-
-
Save dumbmatter/3b1d90bf7cfb6c5951bfffeefecf40aa to your computer and use it in GitHub Desktop.
Polyfill for Blob.stream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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