Skip to content

Instantly share code, notes, and snippets.

@dannycoates
Created May 15, 2018 19:50
Show Gist options
  • Save dannycoates/99be55f18b0268241e01819f82f22a98 to your computer and use it in GitHub Desktop.
Save dannycoates/99be55f18b0268241e01819f82f22a98 to your computer and use it in GitHub Desktop.
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