1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

Remove most build-time require-calls from the src/display/-folder

By leveraging import maps we can get rid of *most* of the remaining `require`-calls in the `src/display/`-folder, since we should strive to use modern `import`-statements wherever possible.
The only remaining cases are Node.js-specific dependencies, since those seem very difficult to convert unless we start producing a bundle *specifically* for Node.js environments.
This commit is contained in:
Jonas Jenwald 2023-07-13 11:58:16 +02:00
parent e81c084a92
commit d022912719
11 changed files with 122 additions and 54 deletions

View file

@ -39,6 +39,7 @@ import {
} from "./display_utils.js";
import { AnnotationStorage } from "./annotation_storage.js";
import { ColorConverters } from "../shared/scripting_utils.js";
import { NullL10n } from "display-l10n_utils";
import { XfaLayer } from "./xfa_layer.js";
const DEFAULT_TAB_INDEX = 1000;
@ -2872,7 +2873,6 @@ class AnnotationLayer {
typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("GENERIC && !TESTING")
) {
const { NullL10n } = require("pdfjs-web/l10n_utils.js");
this.l10n ||= NullL10n;
}
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING")) {

View file

@ -58,12 +58,22 @@ import {
StatTimer,
} from "./display_utils.js";
import { FontFaceObject, FontLoader } from "./font_loader.js";
import {
NodeCanvasFactory,
NodeCMapReaderFactory,
NodeFilterFactory,
NodeStandardFontDataFactory,
} from "display-node_utils";
import { CanvasGraphics } from "./canvas.js";
import { GlobalWorkerOptions } from "./worker_options.js";
import { MessageHandler } from "../shared/message_handler.js";
import { Metadata } from "./metadata.js";
import { OptionalContentConfig } from "./optional_content_config.js";
import { PDFDataTransportStream } from "./transport_stream.js";
import { PDFFetchStream } from "display-fetch_stream";
import { PDFNetworkStream } from "display-network";
import { PDFNodeStream } from "display-node_stream";
import { SVGGraphics } from "display-svg";
import { XfaText } from "./xfa_text.js";
const DEFAULT_RANGE_CHUNK_SIZE = 65536; // 2^16 = 65536
@ -72,54 +82,21 @@ const DELAYED_CLEANUP_TIMEOUT = 5000; // ms
const DefaultCanvasFactory =
typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS
? require("./node_utils.js").NodeCanvasFactory
? NodeCanvasFactory
: DOMCanvasFactory;
const DefaultCMapReaderFactory =
typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS
? require("./node_utils.js").NodeCMapReaderFactory
? NodeCMapReaderFactory
: DOMCMapReaderFactory;
const DefaultFilterFactory =
typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS
? require("./node_utils.js").NodeFilterFactory
? NodeFilterFactory
: DOMFilterFactory;
const DefaultStandardFontDataFactory =
typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS
? require("./node_utils.js").NodeStandardFontDataFactory
? NodeStandardFontDataFactory
: DOMStandardFontDataFactory;
let createPDFNetworkStream;
if (typeof PDFJSDev === "undefined") {
const streamsPromise = Promise.all([
import("./network.js"),
import("./fetch_stream.js"),
]);
createPDFNetworkStream = async params => {
const [{ PDFNetworkStream }, { PDFFetchStream }] = await streamsPromise;
return isValidFetchUrl(params.url)
? new PDFFetchStream(params)
: new PDFNetworkStream(params);
};
} else if (PDFJSDev.test("GENERIC || CHROME")) {
if (PDFJSDev.test("GENERIC") && isNodeJS) {
const { PDFNodeStream } = require("./node_stream.js");
createPDFNetworkStream = params => {
return new PDFNodeStream(params);
};
} else {
const { PDFNetworkStream } = require("./network.js");
const { PDFFetchStream } = require("./fetch_stream.js");
createPDFNetworkStream = params => {
return isValidFetchUrl(params.url)
? new PDFFetchStream(params)
: new PDFNetworkStream(params);
};
}
}
/**
* @typedef { Int8Array | Uint8Array | Uint8ClampedArray |
* Int16Array | Uint16Array |
@ -448,6 +425,19 @@ function getDocument(src) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error("Not implemented: createPDFNetworkStream");
}
const createPDFNetworkStream = params => {
if (
typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("GENERIC") &&
isNodeJS
) {
return new PDFNodeStream(params);
}
return isValidFetchUrl(params.url)
? new PDFFetchStream(params)
: new PDFNetworkStream(params);
};
networkStream = createPDFNetworkStream({
url,
length,
@ -3454,5 +3444,6 @@ export {
PDFWorker,
PDFWorkerUtil,
RenderTask,
SVGGraphics,
version,
};

View file

@ -31,14 +31,10 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
);
}
const fs = __non_webpack_require__("fs");
const http = __non_webpack_require__("http");
const https = __non_webpack_require__("https");
const url = __non_webpack_require__("url");
const fileUriRegex = /^file:\/\/\/[a-zA-Z]:\//;
function parseUrl(sourceUrl) {
const url = __non_webpack_require__("url");
const parsedUrl = url.parse(sourceUrl);
if (parsedUrl.protocol === "file:" || parsedUrl.host) {
return parsedUrl;
@ -344,11 +340,13 @@ class PDFNodeStreamFullReader extends BaseFullReader {
this._request = null;
if (this._url.protocol === "http:") {
const http = __non_webpack_require__("http");
this._request = http.request(
createRequestOptions(this._url, stream.httpHeaders),
handleResponse
);
} else {
const https = __non_webpack_require__("https");
this._request = https.request(
createRequestOptions(this._url, stream.httpHeaders),
handleResponse
@ -391,11 +389,13 @@ class PDFNodeStreamRangeReader extends BaseRangeReader {
this._request = null;
if (this._url.protocol === "http:") {
const http = __non_webpack_require__("http");
this._request = http.request(
createRequestOptions(this._url, this._httpHeaders),
handleResponse
);
} else {
const https = __non_webpack_require__("https");
this._request = https.request(
createRequestOptions(this._url, this._httpHeaders),
handleResponse
@ -420,6 +420,7 @@ class PDFNodeStreamFsFullReader extends BaseFullReader {
path = path.replace(/^\//, "");
}
const fs = __non_webpack_require__("fs");
fs.lstat(path, (error, stat) => {
if (error) {
if (error.code === "ENOENT") {
@ -449,6 +450,7 @@ class PDFNodeStreamFsRangeReader extends BaseRangeReader {
path = path.replace(/^\//, "");
}
const fs = __non_webpack_require__("fs");
this._setReadableStream(fs.createReadStream(path, { start, end: end - 1 }));
}
}

36
src/display/stubs.js Normal file
View file

@ -0,0 +1,36 @@
/* Copyright 2023 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const NodeCanvasFactory = null;
const NodeCMapReaderFactory = null;
const NodeFilterFactory = null;
const NodeStandardFontDataFactory = null;
const NullL10n = null;
const PDFFetchStream = null;
const PDFNetworkStream = null;
const PDFNodeStream = null;
const SVGGraphics = null;
export {
NodeCanvasFactory,
NodeCMapReaderFactory,
NodeFilterFactory,
NodeStandardFontDataFactory,
NullL10n,
PDFFetchStream,
PDFNetworkStream,
PDFNodeStream,
SVGGraphics,
};

View file

@ -50,6 +50,7 @@ import {
getDocument,
PDFDataRangeTransport,
PDFWorker,
SVGGraphics,
version,
} from "./display/api.js";
import {
@ -78,11 +79,6 @@ const pdfjsVersion =
const pdfjsBuild =
typeof PDFJSDev !== "undefined" ? PDFJSDev.eval("BUNDLE_BUILD") : void 0;
const SVGGraphics =
typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC")
? require("./display/svg.js").SVGGraphics
: null;
export {
AbortException,
AnnotationEditorLayer,