Skip to content

Commit

Permalink
[api-minor] Load Node.js packages/polyfills with `process.getBuiltinM…
Browse files Browse the repository at this point in the history
…odule`
  • Loading branch information
Snuffleupagus committed Sep 29, 2024
1 parent a7e1bf6 commit 4de9a43
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 111 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
check-latest: true

- name: Install dependencies
run: npm ci
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/font_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
check-latest: true

- name: Install dependencies
run: npm ci
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
check-latest: true

- name: Install dependencies
run: npm ci
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/types_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
check-latest: true

- name: Install dependencies
run: npm ci
Expand Down
9 changes: 0 additions & 9 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import {
NodeCanvasFactory,
NodeCMapReaderFactory,
NodeFilterFactory,
NodePackages,
NodeStandardFontDataFactory,
} from "display-node_utils";
import { CanvasGraphics } from "./canvas.js";
Expand Down Expand Up @@ -2107,14 +2106,6 @@ class PDFWorker {
* @type {Promise<void>}
*/
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;
}

Expand Down
12 changes: 6 additions & 6 deletions src/display/node_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
* 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 {
createHeaders,
extractFilenameFromHeader,
validateRangeRequestCapabilities,
} from "./network_utils.js";
import { NodePackages } from "./node_utils.js";

if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error(
Expand All @@ -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);
}

Expand Down Expand Up @@ -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.
Expand All @@ -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 })
);
Expand Down
124 changes: 44 additions & 80 deletions src/display/node_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals process */

import {
BaseCanvasFactory,
Expand All @@ -27,90 +28,51 @@ 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;

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 {}
}

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
) {
applyPath2DToCanvasRenderingContext(CanvasRenderingContext2D);
globalThis.Path2D = Path2D;
} else {
warn("Cannot polyfill `Path2D`, rendering may be broken.");
}
}
},
reason => {
warn(`loadPackages: ${reason}`);

packageMap = new Map();
packageCapability.resolve();
if (
typeof PDFJSDev !== "undefined" &&
!PDFJSDev.test("SKIP_BABEL") &&
isNodeJS
) {
let canvas, path2d;
try {
const require = process
.getBuiltinModule("module")
.createRequire(import.meta.url);

canvas = require("canvas");
path2d = require("path2d");
} catch {}

if (!globalThis.DOMMatrix) {
const DOMMatrix = canvas?.DOMMatrix;

if (DOMMatrix) {
globalThis.DOMMatrix = DOMMatrix;
} else {
warn("Cannot polyfill `DOMMatrix`, rendering may be broken.");
}
);
}

class NodePackages {
static get promise() {
return packageCapability.promise;
}

static get(name) {
return packageMap?.get(name);
if (!globalThis.Path2D) {
const CanvasRenderingContext2D = canvas?.CanvasRenderingContext2D;
const applyPath2DToCanvasRenderingContext =
path2d?.applyPath2DToCanvasRenderingContext;
const Path2D = path2d?.Path2D;

if (
CanvasRenderingContext2D &&
applyPath2DToCanvasRenderingContext &&
Path2D
) {
applyPath2DToCanvasRenderingContext(CanvasRenderingContext2D);
globalThis.Path2D = Path2D;
} else {
warn("Cannot polyfill `Path2D`, rendering may be broken.");
}
}
}

const fetchData = function (url) {
const fs = NodePackages.get("fs");
const fs = process.getBuiltinModule("fs");
return fs.promises.readFile(url).then(data => new Uint8Array(data));
};

Expand All @@ -121,7 +83,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);
}
}
Expand All @@ -148,6 +113,5 @@ export {
NodeCanvasFactory,
NodeCMapReaderFactory,
NodeFilterFactory,
NodePackages,
NodeStandardFontDataFactory,
};
2 changes: 0 additions & 2 deletions src/display/stubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
const NodeCanvasFactory = null;
const NodeCMapReaderFactory = null;
const NodeFilterFactory = null;
const NodePackages = null;
const NodeStandardFontDataFactory = null;
const PDFFetchStream = null;
const PDFNetworkStream = null;
Expand All @@ -26,7 +25,6 @@ export {
NodeCanvasFactory,
NodeCMapReaderFactory,
NodeFilterFactory,
NodePackages,
NodeStandardFontDataFactory,
PDFFetchStream,
PDFNetworkStream,
Expand Down
2 changes: 1 addition & 1 deletion test/types/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"module": "ESNext",
"baseUrl": "./",
"strict": true,
"types": [],
"types": ["node"],
"lib": ["ESNext", "DOM"],
"paths": {
"pdfjs-dist": ["../../build/typestest"],
Expand Down
4 changes: 0 additions & 4 deletions test/unit/clitests_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
3 changes: 1 addition & 2 deletions test/unit/node_stream_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 3 additions & 7 deletions test/unit/test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ import { NullStream, StringStream } from "../../src/core/stream.js";
import { Page, PDFDocument } from "../../src/core/document.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/";
Expand All @@ -45,6 +38,7 @@ class DOMFileReaderFactory {

class NodeFileReaderFactory {
static async fetch(params) {
const fs = process.getBuiltinModule("fs");
const data = await fs.promises.readFile(params.path);
return new Uint8Array(data);
}
Expand Down Expand Up @@ -141,6 +135,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) => {
Expand Down

0 comments on commit 4de9a43

Please sign in to comment.