diff --git a/lib/core/io.js b/lib/core/io.js index f1097fc6..1d69e72d 100644 --- a/lib/core/io.js +++ b/lib/core/io.js @@ -190,22 +190,40 @@ class BlobWriter extends Writer { constructor(contentType) { super(); this.contentType = contentType; - this.arrayBuffers = []; + this.arrayBuffersMaxlength = 8; + initArrayBuffers(this); } async writeUint8Array(array) { super.writeUint8Array(array); + if (this.arrayBuffers.length == this.arrayBuffersMaxlength) { + flushArrayBuffers(this); + } this.arrayBuffers.push(array.buffer); } getData() { if (!this.blob) { - this.blob = new Blob(this.arrayBuffers, { type: this.contentType }); + if (this.arrayBuffers.length) { + flushArrayBuffers(this); + } + this.blob = this.pendingBlob; + initArrayBuffers(this); } return this.blob; } } +function initArrayBuffers(blobWriter) { + blobWriter.pendingBlob = new Blob([], { type: blobWriter.contentType }); + blobWriter.arrayBuffers = []; +} + +function flushArrayBuffers(blobWriter) { + blobWriter.pendingBlob = new Blob([blobWriter.pendingBlob, ...blobWriter.arrayBuffers], { type: blobWriter.contentType }); + blobWriter.arrayBuffers = []; +} + class WritableStreamWriter extends Writer { constructor(writableStream) { super();