Merge pull request #20627 from Snuffleupagus/ChunkedStream-onReceiveData-rm-copy

Improve progress reporting in `ChunkedStreamManager`, and prevent unnecessary data copy in `ChunkedStream.prototype.onReceiveData`
This commit is contained in:
Tim van der Meij 2026-02-05 21:27:42 +01:00 committed by GitHub
commit f302323c7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

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 {
@ -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,
});
}