Skip to content

Commit

Permalink
build Blob on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
gildas-lormeau committed Apr 11, 2022
1 parent 7ab9f22 commit 9b56c15
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/core/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 9b56c15

Please sign in to comment.