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

View File

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