diff --git a/src/display/api.js b/src/display/api.js index b9e08f6fd..cf11866ad 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -56,7 +56,6 @@ import { NodeCanvasFactory, NodeCMapReaderFactory, NodeFilterFactory, - NodePackages, NodeStandardFontDataFactory, } from "display-node_utils"; import { CanvasGraphics } from "./canvas.js"; @@ -2137,14 +2136,6 @@ class PDFWorker { * @type {Promise} */ get promise() { - if ( - typeof PDFJSDev !== "undefined" && - PDFJSDev.test("GENERIC") && - isNodeJS - ) { - // Ensure that all Node.js packages/polyfills have loaded. - return Promise.all([NodePackages.promise, this._readyCapability.promise]); - } return this._readyCapability.promise; } diff --git a/src/display/node_stream.js b/src/display/node_stream.js index 6808488d6..069f025da 100644 --- a/src/display/node_stream.js +++ b/src/display/node_stream.js @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +/* globals process */ import { AbortException, assert, MissingPDFException } from "../shared/util.js"; import { @@ -19,7 +20,6 @@ import { extractFilenameFromHeader, validateRangeRequestCapabilities, } from "./network_utils.js"; -import { NodePackages } from "./node_utils.js"; if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { throw new Error( @@ -33,16 +33,16 @@ function parseUrlOrPath(sourceUrl) { if (urlRegex.test(sourceUrl)) { return new URL(sourceUrl); } - const url = NodePackages.get("url"); + const url = process.getBuiltinModule("url"); return new URL(url.pathToFileURL(sourceUrl)); } function createRequest(url, headers, callback) { if (url.protocol === "http:") { - const http = NodePackages.get("http"); + const http = process.getBuiltinModule("http"); return http.request(url, { headers }, callback); } - const https = NodePackages.get("https"); + const https = process.getBuiltinModule("https"); return https.request(url, { headers }, callback); } @@ -365,7 +365,7 @@ class PDFNodeStreamFsFullReader extends BaseFullReader { constructor(stream) { super(stream); - const fs = NodePackages.get("fs"); + const fs = process.getBuiltinModule("fs"); fs.promises.lstat(this._url).then( stat => { // Setting right content length. @@ -389,7 +389,7 @@ class PDFNodeStreamFsRangeReader extends BaseRangeReader { constructor(stream, start, end) { super(stream); - const fs = NodePackages.get("fs"); + const fs = process.getBuiltinModule("fs"); this._setReadableStream( fs.createReadStream(this._url, { start, end: end - 1 }) ); diff --git a/src/display/node_utils.js b/src/display/node_utils.js index 037bea241..81ed9375b 100644 --- a/src/display/node_utils.js +++ b/src/display/node_utils.js @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +/* globals process */ import { isNodeJS, warn } from "../shared/util.js"; import { BaseCanvasFactory } from "./canvas_factory.js"; @@ -25,94 +26,63 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { ); } -if (isNodeJS) { - // eslint-disable-next-line no-var - var packageCapability = Promise.withResolvers(); - // eslint-disable-next-line no-var - var packageMap = null; +if ( + typeof PDFJSDev !== "undefined" && + !PDFJSDev.test("SKIP_BABEL") && + isNodeJS +) { + let canvas, path2d; + try { + const require = process + .getBuiltinModule("module") + .createRequire(import.meta.url); - const loadPackages = async () => { - // Native packages. - const fs = await __non_webpack_import__("fs"), - http = await __non_webpack_import__("http"), - https = await __non_webpack_import__("https"), - url = await __non_webpack_import__("url"); - - // Optional, third-party, packages. - let canvas, path2d; - if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("SKIP_BABEL")) { - try { - canvas = await __non_webpack_import__("canvas"); - } catch {} - try { - path2d = await __non_webpack_import__("path2d"); - } catch {} + try { + canvas = require("canvas"); + } catch (ex) { + warn(`Cannot load "canvas" package: "${ex}".`); } - - return new Map(Object.entries({ fs, http, https, url, canvas, path2d })); - }; - - loadPackages().then( - map => { - packageMap = map; - packageCapability.resolve(); - - if (typeof PDFJSDev === "undefined" || PDFJSDev.test("SKIP_BABEL")) { - return; - } - if (!globalThis.DOMMatrix) { - const DOMMatrix = map.get("canvas")?.DOMMatrix; - - if (DOMMatrix) { - globalThis.DOMMatrix = DOMMatrix; - } else { - warn("Cannot polyfill `DOMMatrix`, rendering may be broken."); - } - } - if (!globalThis.Path2D) { - const CanvasRenderingContext2D = - map.get("canvas")?.CanvasRenderingContext2D; - const applyPath2DToCanvasRenderingContext = - map.get("path2d")?.applyPath2DToCanvasRenderingContext; - const Path2D = map.get("path2d")?.Path2D; - - if ( - CanvasRenderingContext2D && - applyPath2DToCanvasRenderingContext && - Path2D - ) { - try { - applyPath2DToCanvasRenderingContext(CanvasRenderingContext2D); - } catch (ex) { - warn(`applyPath2DToCanvasRenderingContext: "${ex}".`); - } - globalThis.Path2D = Path2D; - } else { - warn("Cannot polyfill `Path2D`, rendering may be broken."); - } - } - }, - reason => { - warn(`loadPackages: ${reason}`); - - packageMap = new Map(); - packageCapability.resolve(); + try { + path2d = require("path2d"); + } catch (ex) { + warn(`Cannot load "path2d" package: "${ex}".`); } - ); -} + } catch {} -class NodePackages { - static get promise() { - return packageCapability.promise; + if (!globalThis.DOMMatrix) { + const DOMMatrix = canvas?.DOMMatrix; + + if (DOMMatrix) { + globalThis.DOMMatrix = DOMMatrix; + } else { + warn("Cannot polyfill `DOMMatrix`, rendering may be broken."); + } } + if (!globalThis.Path2D) { + const CanvasRenderingContext2D = canvas?.CanvasRenderingContext2D; + const applyPath2DToCanvasRenderingContext = + path2d?.applyPath2DToCanvasRenderingContext; + const Path2D = path2d?.Path2D; - static get(name) { - return packageMap?.get(name); + if ( + CanvasRenderingContext2D && + applyPath2DToCanvasRenderingContext && + Path2D + ) { + try { + applyPath2DToCanvasRenderingContext(CanvasRenderingContext2D); + } catch (ex) { + warn(`applyPath2DToCanvasRenderingContext: "${ex}".`); + } + globalThis.Path2D = Path2D; + } else { + warn("Cannot polyfill `Path2D`, rendering may be broken."); + } } } async function fetchData(url) { - const fs = NodePackages.get("fs"); + const fs = process.getBuiltinModule("fs"); const data = await fs.promises.readFile(url); return new Uint8Array(data); } @@ -124,7 +94,10 @@ class NodeCanvasFactory extends BaseCanvasFactory { * @ignore */ _createCanvas(width, height) { - const canvas = NodePackages.get("canvas"); + const require = process + .getBuiltinModule("module") + .createRequire(import.meta.url); + const canvas = require("canvas"); return canvas.createCanvas(width, height); } } @@ -152,6 +125,5 @@ export { NodeCanvasFactory, NodeCMapReaderFactory, NodeFilterFactory, - NodePackages, NodeStandardFontDataFactory, }; diff --git a/src/display/stubs.js b/src/display/stubs.js index bea46b67e..7bd6eedd2 100644 --- a/src/display/stubs.js +++ b/src/display/stubs.js @@ -18,7 +18,6 @@ const DOMStandardFontDataFactory = null; const NodeCanvasFactory = null; const NodeCMapReaderFactory = null; const NodeFilterFactory = null; -const NodePackages = null; const NodeStandardFontDataFactory = null; const PDFFetchStream = null; const PDFNetworkStream = null; @@ -30,7 +29,6 @@ export { NodeCanvasFactory, NodeCMapReaderFactory, NodeFilterFactory, - NodePackages, NodeStandardFontDataFactory, PDFFetchStream, PDFNetworkStream, diff --git a/test/unit/clitests_helper.js b/test/unit/clitests_helper.js index a5a1c788e..d86eeacbb 100644 --- a/test/unit/clitests_helper.js +++ b/test/unit/clitests_helper.js @@ -18,7 +18,6 @@ import { setVerbosityLevel, VerbosityLevel, } from "../../src/shared/util.js"; -import { NodePackages } from "../../src/display/node_utils.js"; // Sets longer timeout, similar to `jasmine-boot.js`. jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000; @@ -30,9 +29,6 @@ if (!isNodeJS) { ); } -// Ensure that all Node.js packages/polyfills have loaded. -await NodePackages.promise; - // Reduce the amount of console "spam", by ignoring `info`/`warn` calls, // when running the unit-tests in Node.js/Travis. setVerbosityLevel(VerbosityLevel.ERRORS); diff --git a/test/unit/node_stream_spec.js b/test/unit/node_stream_spec.js index 775131caa..085164bb7 100644 --- a/test/unit/node_stream_spec.js +++ b/test/unit/node_stream_spec.js @@ -24,11 +24,10 @@ if (!isNodeJS) { ); } -const url = await __non_webpack_import__("url"); - describe("node_stream", function () { let tempServer = null; + const url = process.getBuiltinModule("url"); const cwdURL = url.pathToFileURL(process.cwd()) + "/"; const pdf = new URL("./test/pdfs/tracemonkey.pdf", cwdURL).href; const pdfLength = 1016315; diff --git a/test/unit/test_utils.js b/test/unit/test_utils.js index 473b292e8..5ca113989 100644 --- a/test/unit/test_utils.js +++ b/test/unit/test_utils.js @@ -20,13 +20,6 @@ import { fetchData as fetchDataDOM } from "../../src/display/display_utils.js"; import { fetchData as fetchDataNode } from "../../src/display/node_utils.js"; import { Ref } from "../../src/core/primitives.js"; -let fs, http; -if (isNodeJS) { - // Native packages. - fs = await __non_webpack_import__("fs"); - http = await __non_webpack_import__("http"); -} - const TEST_PDFS_PATH = isNodeJS ? "./test/pdfs/" : "../pdfs/"; const CMAP_URL = isNodeJS ? "./external/bcmaps/" : "../../external/bcmaps/"; @@ -132,6 +125,8 @@ function createIdFactory(pageIndex) { function createTemporaryNodeServer() { assert(isNodeJS, "Should only be used in Node.js environments."); + const fs = process.getBuiltinModule("fs"), + http = process.getBuiltinModule("http"); // Create http server to serve pdf data for tests. const server = http .createServer((request, response) => {