Created
May 15, 2018 19:50
-
-
Save dannycoates/99be55f18b0268241e01819f82f22a98 to your computer and use it in GitHub Desktop.
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
class BlobSlicer { | |
constructor(blob, size) { | |
this.blob = blob | |
this.size = size | |
this.index = 0 | |
} | |
start(controller) {} | |
pull(controller) { | |
return new Promise((resolve, reject) => { | |
const bytesLeft = this.blob.size - this.index | |
if (bytesLeft <= 0) { | |
controller.close() | |
return resolve() | |
} | |
const size = Math.min(this.size, bytesLeft) | |
const blob = this.blob.slice(this.index, this.index + size) | |
const reader = new FileReader() | |
reader.onload = function () { | |
controller.enqueue(new Uint8Array(this.result)) | |
resolve() | |
} | |
reader.onerror = reject | |
reader.readAsArrayBuffer(blob) | |
this.index += size | |
}) | |
} | |
cancel(reason) {} | |
} | |
export default class BlobSliceStream extends ReadableStream { | |
constructor(blob, size) { | |
super(new BlobSlicer(blob, size)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment