diff --git a/src/core/chunked_stream.js b/src/core/chunked_stream.js index 94c01fc03..642323fae 100644 --- a/src/core/chunked_stream.js +++ b/src/core/chunked_stream.js @@ -14,7 +14,7 @@ */ import { arrayBuffersToBytes, MissingDataException } from "./core_utils.js"; -import { assert } from "../shared/util.js"; +import { assert, MathClamp } from "../shared/util.js"; import { Stream } from "./stream.js"; class ChunkedStream extends Stream { @@ -70,6 +70,12 @@ class ChunkedStream extends Stream { throw new Error(`Bad end offset: ${end}`); } + if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) { + assert( + chunk instanceof ArrayBuffer, + "onReceiveData - expected an ArrayBuffer." + ); + } this.bytes.set(new Uint8Array(chunk), begin); const beginChunk = Math.floor(begin / chunkSize); const endChunk = Math.floor((end - 1) / chunkSize) + 1; @@ -85,6 +91,12 @@ class ChunkedStream extends Stream { let position = this.progressiveDataLength; const beginChunk = Math.floor(position / this.chunkSize); + if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) { + assert( + data instanceof ArrayBuffer, + "onReceiveProgressiveData - expected an ArrayBuffer." + ); + } this.bytes.set(new Uint8Array(data), position); position += data.byteLength; this.progressiveDataLength = position; @@ -310,7 +322,7 @@ class ChunkedStreamManager { if (this.aborted) { return; // Ignoring any data after abort. } - this.onReceiveData({ chunk: data, begin }); + this.onReceiveData({ chunk: data.buffer, begin }); }); } @@ -511,7 +523,11 @@ class ChunkedStreamManager { } this.msgHandler.send("DocProgress", { - loaded: stream.numChunksLoaded * chunkSize, + loaded: MathClamp( + stream.numChunksLoaded * chunkSize, + stream.progressiveDataLength, + length + ), total: length, }); }