From 01deb085f8acbc4f173f63e2e7c36514cdc81df4 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 5 Feb 2026 16:04:45 +0100 Subject: [PATCH] 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. --- src/core/chunked_stream.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/core/chunked_stream.js b/src/core/chunked_stream.js index 94c01fc03..e6ebf065c 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 { @@ -511,7 +511,11 @@ class ChunkedStreamManager { } this.msgHandler.send("DocProgress", { - loaded: stream.numChunksLoaded * chunkSize, + loaded: MathClamp( + stream.numChunksLoaded * chunkSize, + stream.progressiveDataLength, + length + ), total: length, }); }