Merge pull request #20660 from Snuffleupagus/ChunkedStream-async-sendRequest

Convert `ChunkedStreamManager.prototype.sendRequest` to an asynchronous method
This commit is contained in:
Tim van der Meij 2026-02-20 21:39:26 +01:00 committed by GitHub
commit 82de22428a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -293,46 +293,37 @@ class ChunkedStreamManager {
this.msgHandler = args.msgHandler; this.msgHandler = args.msgHandler;
} }
sendRequest(begin, end) { async sendRequest(begin, end) {
const rangeReader = this.pdfStream.getRangeReader(begin, end); const rangeReader = this.pdfStream.getRangeReader(begin, end);
let chunks = []; let chunks = [];
return new Promise((resolve, reject) => {
const readChunk = ({ value, done }) => { while (true) {
try { const { value, done } = await rangeReader.read();
if (done) {
resolve( if (this.aborted) {
chunks.length > 0 || !this.disableAutoFetch
? arrayBuffersToBytes(chunks)
: null
);
chunks = null; chunks = null;
return; return; // Ignoring any data after abort.
}
if (done) {
break;
} }
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
assert( assert(
value instanceof ArrayBuffer, value instanceof ArrayBuffer,
"readChunk (sendRequest) - expected an ArrayBuffer." "sendRequest - expected an ArrayBuffer."
); );
} }
chunks.push(value); chunks.push(value);
rangeReader.read().then(readChunk, reject);
} catch (e) {
reject(e);
} }
};
rangeReader.read().then(readChunk, reject); if (chunks.length === 0 && this.disableAutoFetch) {
}).then(data => {
if (this.aborted) {
return; // Ignoring any data after abort.
}
if (!data) {
// The range request wasn't dispatched, see the "GetRangeReader" handler // The range request wasn't dispatched, see the "GetRangeReader" handler
// in the `src/display/api.js` file. // in the `src/display/api.js` file.
return; return;
} }
const data = arrayBuffersToBytes(chunks);
chunks = null;
this.onReceiveData({ chunk: data.buffer, begin }); this.onReceiveData({ chunk: data.buffer, begin });
});
} }
/** /**