Improve progress reporting in the ChunkedStreamManager

Currently there's two small bugs, which have existed around a decade, in the `loaded` property that's sent via the "DocProgress" message from the `ChunkedStreamManager.prototype.onReceiveData` method.

 - When the entire PDF has loaded the `loaded` property can become larger than the `total` property, which obviously doesn't make sense.
   This happens whenever the size of the PDF is *not* a multiple of the `rangeChunkSize` API-option, which is a very common situation.

 - When streaming is being used, the `loaded` property can become smaller than the actually loaded amount of data.
   This happens whenever the size of a streamed chunk is *not* a multiple of the `rangeChunkSize` API-option, which is a common situation.
This commit is contained in:
Jonas Jenwald 2026-02-05 16:04:45 +01:00
parent bfd17b2586
commit 01deb085f8

View File

@ -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 {
@ -511,7 +511,11 @@ class ChunkedStreamManager {
}
this.msgHandler.send("DocProgress", {
loaded: stream.numChunksLoaded * chunkSize,
loaded: MathClamp(
stream.numChunksLoaded * chunkSize,
stream.progressiveDataLength,
length
),
total: length,
});
}