Add a helper function to resolve pending requests in src/display/transport_stream.js and src/display/network.js

Currently the same identical code is duplicated four times per file, which seems completely unnecessary.
Note that the function isn't placed in `src/display/network_utils.js`, since that file isn't included in MOZCENTRAL builds.
This commit is contained in:
Jonas Jenwald 2026-02-08 11:04:18 +01:00
parent 2d643efce5
commit 916b58a027
2 changed files with 29 additions and 39 deletions

View File

@ -27,6 +27,7 @@ import {
getResponseOrigin, getResponseOrigin,
validateRangeRequestCapabilities, validateRangeRequestCapabilities,
} from "./network_utils.js"; } from "./network_utils.js";
import { endRequests } from "./transport_stream.js";
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error( throw new Error(
@ -174,6 +175,8 @@ class PDFNetworkStream extends BasePDFStream {
} }
class PDFNetworkStreamReader extends BasePDFStreamReader { class PDFNetworkStreamReader extends BasePDFStreamReader {
#endRequests = endRequests.bind(this);
_cachedChunks = []; _cachedChunks = [];
_done = false; _done = false;
@ -254,13 +257,9 @@ class PDFNetworkStreamReader extends BasePDFStreamReader {
this._cachedChunks.push(chunk); this._cachedChunks.push(chunk);
} }
this._done = true; this._done = true;
if (this._cachedChunks.length > 0) { if (this._cachedChunks.length === 0) {
return; this.#endRequests();
} }
for (const capability of this._requests) {
capability.resolve({ value: undefined, done: true });
}
this._requests.length = 0;
} }
#onError(status) { #onError(status) {
@ -301,10 +300,7 @@ class PDFNetworkStreamReader extends BasePDFStreamReader {
cancel(reason) { cancel(reason) {
this._done = true; this._done = true;
this._headersCapability.reject(reason); this._headersCapability.reject(reason);
for (const capability of this._requests) { this.#endRequests();
capability.resolve({ value: undefined, done: true });
}
this._requests.length = 0;
this._stream._abortRequest(this._fullRequestXhr); this._stream._abortRequest(this._fullRequestXhr);
this._fullRequestXhr = null; this._fullRequestXhr = null;
@ -312,6 +308,8 @@ class PDFNetworkStreamReader extends BasePDFStreamReader {
} }
class PDFNetworkStreamRangeReader extends BasePDFStreamRangeReader { class PDFNetworkStreamRangeReader extends BasePDFStreamRangeReader {
#endRequests = endRequests.bind(this);
onClosed = null; onClosed = null;
_done = false; _done = false;
@ -353,10 +351,7 @@ class PDFNetworkStreamRangeReader extends BasePDFStreamRangeReader {
this._queuedChunk = chunk; this._queuedChunk = chunk;
} }
this._done = true; this._done = true;
for (const capability of this._requests) { this.#endRequests();
capability.resolve({ value: undefined, done: true });
}
this._requests.length = 0;
this.onClosed?.(); this.onClosed?.();
} }
@ -388,10 +383,7 @@ class PDFNetworkStreamRangeReader extends BasePDFStreamRangeReader {
cancel(reason) { cancel(reason) {
this._done = true; this._done = true;
for (const capability of this._requests) { this.#endRequests();
capability.resolve({ value: undefined, done: true });
}
this._requests.length = 0;
this._stream._abortRequest(this._requestXhr); this._stream._abortRequest(this._requestXhr);
this.onClosed?.(); this.onClosed?.();

View File

@ -29,6 +29,13 @@ function getArrayBuffer(val) {
: new Uint8Array(val).buffer; : new Uint8Array(val).buffer;
} }
function endRequests() {
for (const capability of this._requests) {
capability.resolve({ value: undefined, done: true });
}
this._requests.length = 0;
}
class PDFDataTransportStream extends BasePDFStream { class PDFDataTransportStream extends BasePDFStream {
_progressiveDone = false; _progressiveDone = false;
@ -112,6 +119,8 @@ class PDFDataTransportStream extends BasePDFStream {
} }
class PDFDataTransportStreamReader extends BasePDFStreamReader { class PDFDataTransportStreamReader extends BasePDFStreamReader {
#endRequests = endRequests.bind(this);
_done = false; _done = false;
_queuedChunks = null; _queuedChunks = null;
@ -179,26 +188,21 @@ class PDFDataTransportStreamReader extends BasePDFStreamReader {
cancel(reason) { cancel(reason) {
this._done = true; this._done = true;
for (const capability of this._requests) { this.#endRequests();
capability.resolve({ value: undefined, done: true });
}
this._requests.length = 0;
} }
progressiveDone() { progressiveDone() {
this._done ||= true; this._done ||= true;
if (this._queuedChunks.length > 0) { if (this._queuedChunks.length === 0) {
return; this.#endRequests();
} }
for (const capability of this._requests) {
capability.resolve({ value: undefined, done: true });
}
this._requests.length = 0;
} }
} }
class PDFDataTransportStreamRangeReader extends BasePDFStreamRangeReader { class PDFDataTransportStreamRangeReader extends BasePDFStreamRangeReader {
#endRequests = endRequests.bind(this);
onDone = null; onDone = null;
_begin = -1; _begin = -1;
@ -221,13 +225,10 @@ class PDFDataTransportStreamRangeReader extends BasePDFStreamRangeReader {
if (this._requests.length === 0) { if (this._requests.length === 0) {
this._queuedChunk = chunk; this._queuedChunk = chunk;
} else { } else {
const firstCapability = this._requests.shift(); const capability = this._requests.shift();
firstCapability.resolve({ value: chunk, done: false }); capability.resolve({ value: chunk, done: false });
for (const capability of this._requests) { this.#endRequests();
capability.resolve({ value: undefined, done: true });
}
this._requests.length = 0;
} }
this._done = true; this._done = true;
this.onDone?.(); this.onDone?.();
@ -249,12 +250,9 @@ class PDFDataTransportStreamRangeReader extends BasePDFStreamRangeReader {
cancel(reason) { cancel(reason) {
this._done = true; this._done = true;
for (const capability of this._requests) { this.#endRequests();
capability.resolve({ value: undefined, done: true });
}
this._requests.length = 0;
this.onDone?.(); this.onDone?.();
} }
} }
export { PDFDataTransportStream }; export { endRequests, PDFDataTransportStream };