From 9b56c151f5213d00490bc2a09c6bf74f955b19aa Mon Sep 17 00:00:00 2001 From: Gildas Date: Mon, 11 Apr 2022 18:00:38 +0200 Subject: [PATCH] build Blob on the fly --- lib/core/io.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) 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();