extracted blobSlicer, fixed download cancel tests

This commit is contained in:
Danny Coates 2018-07-25 12:29:19 -07:00
parent 5483dc2506
commit 5e9e63944b
No known key found for this signature in database
GPG key ID: 4C442633C62E00CB
6 changed files with 58 additions and 58 deletions

View file

@ -1,4 +1,5 @@
import 'buffer';
import BlobSlicer from './blobSlicer';
import { transformStream } from './streams';
const NONCE_LENGTH = 12;
@ -225,40 +226,6 @@ class ECETransformer {
}
}
export class BlobSlicer {
constructor(blob, rs, mode) {
this.blob = blob;
this.index = 0;
this.mode = mode;
this.chunkSize = mode === MODE_ENCRYPT ? rs - 17 : rs;
}
pull(controller) {
return new Promise((resolve, reject) => {
const bytesLeft = this.blob.size - this.index;
if (bytesLeft <= 0) {
controller.close();
return resolve();
}
let size = 1;
if (this.mode === MODE_DECRYPT && this.index === 0) {
size = Math.min(21, bytesLeft);
} else {
size = Math.min(this.chunkSize, bytesLeft);
}
const blob = this.blob.slice(this.index, this.index + size);
const reader = new FileReader();
reader.onload = () => {
controller.enqueue(new Uint8Array(reader.result));
resolve();
};
reader.onerror = reject;
reader.readAsArrayBuffer(blob);
this.index += size;
});
}
}
class StreamSlicer {
constructor(rs, mode) {
this.mode = mode;
@ -350,7 +317,7 @@ export default class ECE {
if (this.input instanceof Blob) {
inputStream = new ReadableStream(
new BlobSlicer(this.input, this.rs, this.mode)
new BlobSlicer(this.input, this.rs - 17)
);
} else {
inputStream = transformStream(