Compare commits

..

No commits in common. "bfd17b25868485c6e5024600cb43c766abda7fd3" and "f4326e17c40760d0251768ca2279141f6ead1b0a" have entirely different histories.

17 changed files with 89 additions and 119 deletions

View File

@ -639,6 +639,8 @@ class PDFDataRangeTransport {
#progressiveReadListeners = [];
#progressListeners = [];
#rangeListeners = [];
/**
@ -657,18 +659,6 @@ class PDFDataRangeTransport {
this.initialData = initialData;
this.progressiveDone = progressiveDone;
this.contentDispositionFilename = contentDispositionFilename;
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
Object.defineProperty(this, "onDataProgress", {
value: () => {
deprecated(
"`PDFDataRangeTransport.prototype.onDataProgress` - method was " +
"removed, since loading progress is now reported automatically " +
"through the `PDFDataTransportStream` class (and related code)."
);
},
});
}
}
/**
@ -678,6 +668,13 @@ class PDFDataRangeTransport {
this.#rangeListeners.push(listener);
}
/**
* @param {function} listener
*/
addProgressListener(listener) {
this.#progressListeners.push(listener);
}
/**
* @param {function} listener
*/
@ -702,6 +699,18 @@ class PDFDataRangeTransport {
}
}
/**
* @param {number} loaded
* @param {number|undefined} total
*/
onDataProgress(loaded, total) {
this.#capability.promise.then(() => {
for (const listener of this.#progressListeners) {
listener(loaded, total);
}
});
}
/**
* @param {Uint8Array|null} chunk
*/

View File

@ -25,7 +25,7 @@ function getUrlProp(val) {
return null; // The 'url' is unused with `PDFDataRangeTransport`.
}
if (val instanceof URL) {
return val;
return val.href;
}
if (typeof val === "string") {
if (
@ -33,18 +33,13 @@ function getUrlProp(val) {
PDFJSDev.test("GENERIC") &&
isNodeJS
) {
if (/^[a-z][a-z0-9\-+.]+:/i.test(val)) {
return new URL(val);
}
// eslint-disable-next-line no-undef
const url = process.getBuiltinModule("url");
return new URL(url.pathToFileURL(val));
return val; // Use the url as-is in Node.js environments.
}
// The full path is required in the 'url' field.
const url = URL.parse(val, window.location);
if (url) {
return url;
return url.href;
}
}
throw new Error(

View File

@ -464,7 +464,7 @@ function isValidFetchUrl(url, baseUrl) {
}
const res = baseUrl ? URL.parse(url, baseUrl) : URL.parse(url);
// The Fetch API only supports the http/https protocols, and not file/ftp.
return /https?:/.test(res?.protocol ?? "");
return res?.protocol === "http:" || res?.protocol === "https:";
}
/**
@ -799,7 +799,7 @@ class CSSConstants {
}
function applyOpacity(r, g, b, opacity) {
opacity = MathClamp(opacity ?? 1, 0, 1);
opacity = Math.min(Math.max(opacity ?? 1, 0), 1);
const white = 255 * (1 - opacity);
r = Math.round(r * opacity + white);
g = Math.round(g * opacity + white);

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
import { AbortException, assert, warn } from "../shared/util.js";
import { AbortException, warn } from "../shared/util.js";
import {
BasePDFStream,
BasePDFStreamRangeReader,
@ -67,13 +67,8 @@ class PDFFetchStream extends BasePDFStream {
constructor(source) {
super(source, PDFFetchStreamReader, PDFFetchStreamRangeReader);
const { httpHeaders, url } = source;
assert(
/https?:/.test(url.protocol),
"PDFFetchStream only supports http(s):// URLs."
);
this.headers = createHeaders(/* isHttp = */ true, httpHeaders);
this.isHttp = /^https?:/i.test(source.url);
this.headers = createHeaders(this.isHttp, source.httpHeaders);
}
}
@ -111,7 +106,7 @@ class PDFFetchStreamReader extends BasePDFStreamReader {
const { allowRangeRequests, suggestedLength } =
validateRangeRequestCapabilities({
responseHeaders,
isHttp: true,
isHttp: stream.isHttp,
rangeChunkSize,
disableRange,
});
@ -140,7 +135,10 @@ class PDFFetchStreamReader extends BasePDFStreamReader {
return { value, done };
}
this._loaded += value.byteLength;
this._callOnProgress();
this.onProgress?.({
loaded: this._loaded,
total: this._contentLength,
});
return { value: getArrayBuffer(value), done: false };
}

View File

@ -48,11 +48,9 @@ class PDFNetworkStream extends BasePDFStream {
constructor(source) {
super(source, PDFNetworkStreamReader, PDFNetworkStreamRangeReader);
const { httpHeaders, url } = source;
this.url = url;
this.isHttp = /https?:/.test(url.protocol);
this.headers = createHeaders(this.isHttp, httpHeaders);
this.url = source.url;
this.isHttp = /^https?:/i.test(this.url);
this.headers = createHeaders(this.isHttp, source.httpHeaders);
}
/**

View File

@ -101,9 +101,9 @@ function extractFilenameFromHeader(responseHeaders) {
function createResponseError(status, url) {
return new ResponseException(
`Unexpected server response (${status}) while retrieving PDF "${url.href}".`,
`Unexpected server response (${status}) while retrieving PDF "${url}".`,
status,
/* missing = */ status === 404 || (status === 0 && url.protocol === "file:")
/* missing = */ status === 404 || (status === 0 && url.startsWith("file:"))
);
}

View File

@ -28,6 +28,16 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
);
}
const urlRegex = /^[a-z][a-z0-9\-+.]+:/i;
function parseUrlOrPath(sourceUrl) {
if (urlRegex.test(sourceUrl)) {
return new URL(sourceUrl);
}
const url = process.getBuiltinModule("url");
return new URL(url.pathToFileURL(sourceUrl));
}
function getReadableStream(readStream) {
const { Readable } = process.getBuiltinModule("stream");
@ -58,10 +68,9 @@ function getArrayBuffer(val) {
class PDFNodeStream extends BasePDFStream {
constructor(source) {
super(source, PDFNodeStreamReader, PDFNodeStreamRangeReader);
const { url } = source;
this.url = parseUrlOrPath(source.url);
assert(
url.protocol === "file:",
this.url.protocol === "file:",
"PDFNodeStream only supports file:// URLs."
);
}
@ -72,13 +81,14 @@ class PDFNodeStreamReader extends BasePDFStreamReader {
constructor(stream) {
super(stream);
const { disableRange, disableStream, length, rangeChunkSize, url } =
const { disableRange, disableStream, length, rangeChunkSize } =
stream._source;
this._contentLength = length;
this._isStreamingSupported = !disableStream;
this._isRangeSupported = !disableRange;
const url = stream.url;
const fs = process.getBuiltinModule("fs");
fs.promises
.lstat(url)
@ -107,7 +117,7 @@ class PDFNodeStreamReader extends BasePDFStreamReader {
})
.catch(error => {
if (error.code === "ENOENT") {
error = createResponseError(/* status = */ 0, url);
error = createResponseError(/* status = */ 0, url.href);
}
this._headersCapability.reject(error);
});
@ -119,8 +129,11 @@ class PDFNodeStreamReader extends BasePDFStreamReader {
if (done) {
return { value, done };
}
this._loaded += value.byteLength;
this._callOnProgress();
this._loaded += value.length;
this.onProgress?.({
loaded: this._loaded,
total: this._contentLength,
});
return { value: getArrayBuffer(value), done: false };
}
@ -137,8 +150,8 @@ class PDFNodeStreamRangeReader extends BasePDFStreamRangeReader {
constructor(stream, begin, end) {
super(stream, begin, end);
const { url } = stream._source;
const url = stream.url;
const fs = process.getBuiltinModule("fs");
try {
const readStream = fs.createReadStream(url, {

View File

@ -53,6 +53,12 @@ class PDFDataTransportStream extends BasePDFStream {
this.#onReceiveData(begin, chunk);
});
pdfDataRangeTransport.addProgressListener((loaded, total) => {
if (total !== undefined) {
this._fullReader?.onProgress?.({ loaded, total });
}
});
pdfDataRangeTransport.addProgressiveReadListener(chunk => {
this.#onReceiveData(/* begin = */ undefined, chunk);
});
@ -138,16 +144,6 @@ class PDFDataTransportStreamReader extends BasePDFStreamReader {
this._filename = contentDispositionFilename;
}
this._headersCapability.resolve();
// Report loading progress when there is `initialData`, and `_enqueue` has
// not been invoked, but with a small delay to give an `onProgress` callback
// a chance to be registered first.
const loaded = this._loaded;
Promise.resolve().then(() => {
if (loaded > 0 && this._loaded === loaded) {
this._callOnProgress();
}
});
}
_enqueue(chunk) {
@ -161,7 +157,6 @@ class PDFDataTransportStreamReader extends BasePDFStreamReader {
this._queuedChunks.push(chunk);
}
this._loaded += chunk.byteLength;
this._callOnProgress();
}
async read() {

View File

@ -128,10 +128,6 @@ class BasePDFStreamReader {
this._stream = stream;
}
_callOnProgress() {
this.onProgress?.({ loaded: this._loaded, total: this._contentLength });
}
/**
* Gets a promise that is resolved when the headers and other metadata of
* the PDF data stream are available.

View File

@ -1235,12 +1235,9 @@ function MathClamp(v, min, max) {
return Math.min(Math.max(v, min), max);
}
// TODO: Remove this once `Math.sumPrecise` is generally available.
if (
(typeof PDFJSDev === "undefined" ||
PDFJSDev.test("SKIP_BABEL && !MOZCENTRAL")) &&
typeof Math.sumPrecise !== "function"
) {
// TODO: Remove this once the `javascript.options.experimental.math_sumprecise`
// preference is removed from Firefox.
if (typeof Math.sumPrecise !== "function") {
// Note that this isn't a "proper" polyfill, but since we're only using it to
// replace `Array.prototype.reduce()` invocations it should be fine.
Math.sumPrecise = function (numbers) {

View File

@ -5180,7 +5180,6 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
it("should fetch document info and page using ranges", async function () {
const initialDataLength = 4000;
const subArrays = [];
let initialProgress = null;
let fetches = 0;
const data = await dataPromise;
@ -5194,16 +5193,12 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
const chunk = new Uint8Array(data.subarray(begin, end));
subArrays.push(chunk);
transport.onDataProgress(initialDataLength);
transport.onDataRange(begin, chunk);
});
};
const loadingTask = getDocument({ range: transport });
loadingTask.onProgress = evt => {
initialProgress = evt;
loadingTask.onProgress = null;
};
const pdfDocument = await loadingTask.promise;
expect(pdfDocument.numPages).toEqual(14);
@ -5211,12 +5206,6 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
expect(pdfPage.rotate).toEqual(0);
expect(fetches).toBeGreaterThan(2);
expect(initialProgress).toEqual({
loaded: initialDataLength,
total: data.length,
percent: 0,
});
// Check that the TypedArrays were transferred.
for (const array of subArrays) {
expect(array.length).toEqual(0);
@ -5228,7 +5217,6 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
it("should fetch document info and page using range and streaming", async function () {
const initialDataLength = 4000;
const subArrays = [];
let initialProgress = null;
let fetches = 0;
const data = await dataPromise;
@ -5254,11 +5242,6 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
};
const loadingTask = getDocument({ range: transport });
loadingTask.onProgress = evt => {
initialProgress = evt;
loadingTask.onProgress = null;
};
const pdfDocument = await loadingTask.promise;
expect(pdfDocument.numPages).toEqual(14);
@ -5266,12 +5249,6 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
expect(pdfPage.rotate).toEqual(0);
expect(fetches).toEqual(1);
expect(initialProgress).toEqual({
loaded: initialDataLength,
total: data.length,
percent: 0,
});
await new Promise(resolve => {
waitSome(resolve);
});
@ -5289,7 +5266,6 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
"using complete initialData",
async function () {
const subArrays = [];
let initialProgress = null;
let fetches = 0;
const data = await dataPromise;
@ -5309,11 +5285,6 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
disableRange: true,
range: transport,
});
loadingTask.onProgress = evt => {
initialProgress = evt;
loadingTask.onProgress = null;
};
const pdfDocument = await loadingTask.promise;
expect(pdfDocument.numPages).toEqual(14);
@ -5321,12 +5292,6 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
expect(pdfPage.rotate).toEqual(0);
expect(fetches).toEqual(0);
expect(initialProgress).toEqual({
loaded: data.length,
total: data.length,
percent: 100,
});
// Check that the TypedArrays were transferred.
for (const array of subArrays) {
expect(array.length).toEqual(0);

View File

@ -24,7 +24,7 @@ async function testCrossOriginRedirects({
redirectIfRange,
testRangeReader,
}) {
const basicApiUrl = TestPdfsServer.resolveURL("basicapi.pdf");
const basicApiUrl = TestPdfsServer.resolveURL("basicapi.pdf").href;
const basicApiFileLength = 105779;
const rangeSize = 32768;
@ -83,7 +83,7 @@ function getCrossOriginUrlWithRedirects(testserverUrl, redirectIfRange) {
if (redirectIfRange) {
url.searchParams.set("redirectIfRange", "1");
}
return url;
return url.href;
}
export { testCrossOriginRedirects };

View File

@ -20,7 +20,7 @@ import { TestPdfsServer } from "./test_utils.js";
describe("fetch_stream", function () {
function getPdfUrl() {
return TestPdfsServer.resolveURL("tracemonkey.pdf");
return TestPdfsServer.resolveURL("tracemonkey.pdf").href;
}
const pdfLength = 1016315;

View File

@ -19,7 +19,7 @@ import { testCrossOriginRedirects } from "./common_pdfstream_tests.js";
import { TestPdfsServer } from "./test_utils.js";
describe("network", function () {
const pdf1 = new URL("../pdfs/tracemonkey.pdf", window.location);
const pdf1 = new URL("../pdfs/tracemonkey.pdf", window.location).href;
const pdf1Length = 1016315;
it("read without stream and range", async function () {
@ -124,12 +124,9 @@ describe("network", function () {
}
async function readRanges(mode) {
const pdfUrl = new URL(pdf1);
pdfUrl.searchParams.set("test-network-break-ranges", mode);
const rangeSize = 32768;
const stream = new PDFNetworkStream({
url: pdfUrl,
url: `${pdf1}?test-network-break-ranges=${mode}`,
length: pdf1Length,
rangeChunkSize: rangeSize,
disableStream: true,

View File

@ -372,22 +372,22 @@ describe("network_utils", function () {
expect(error instanceof ResponseException).toEqual(true);
expect(error.message).toEqual(
`Unexpected server response (${status}) while retrieving PDF "${url.href}".`
`Unexpected server response (${status}) while retrieving PDF "${url}".`
);
expect(error.status).toEqual(status);
expect(error.missing).toEqual(missing);
}
it("handles missing PDF file responses", function () {
testCreateResponseError(new URL("https://foo.com/bar.pdf"), 404, true);
testCreateResponseError("https://foo.com/bar.pdf", 404, true);
testCreateResponseError(new URL("file://foo.pdf"), 0, true);
testCreateResponseError("file://foo.pdf", 0, true);
});
it("handles unexpected responses", function () {
testCreateResponseError(new URL("https://foo.com/bar.pdf"), 302, false);
testCreateResponseError("https://foo.com/bar.pdf", 302, false);
testCreateResponseError(new URL("https://foo.com/bar.pdf"), 0, false);
testCreateResponseError("https://foo.com/bar.pdf", 0, false);
});
});
});

View File

@ -26,7 +26,7 @@ if (!isNodeJS) {
describe("node_stream", function () {
const url = process.getBuiltinModule("url");
const cwdURL = url.pathToFileURL(process.cwd()) + "/";
const pdf = new URL("./test/pdfs/tracemonkey.pdf", cwdURL);
const pdf = new URL("./test/pdfs/tracemonkey.pdf", cwdURL).href;
const pdfLength = 1016315;
it("read filesystem pdf files", async function () {
@ -119,7 +119,7 @@ describe("node_stream", function () {
});
it("read filesystem pdf files (smaller than two range requests)", async function () {
const smallPdf = new URL("./test/pdfs/empty.pdf", cwdURL);
const smallPdf = new URL("./test/pdfs/empty.pdf", cwdURL).href;
const smallLength = 4920;
const stream = new PDFNodeStream({

View File

@ -570,8 +570,15 @@ class ExternalServices extends BaseExternalServices {
case "range":
pdfDataRangeTransport.onDataRange(args.begin, args.chunk);
break;
case "rangeProgress":
pdfDataRangeTransport.onDataProgress(args.loaded);
break;
case "progressiveRead":
pdfDataRangeTransport.onDataProgressiveRead(args.chunk);
// Don't forget to report loading progress as well, since otherwise
// the loadingBar won't update when `disableRange=true` is set.
pdfDataRangeTransport.onDataProgress(args.loaded, args.total);
break;
case "progressiveDone":
pdfDataRangeTransport?.onDataProgressiveDone();