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 Aug 14, 2024
1 parent 8446d3a commit 8602df4
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 114 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 @@ -58,7 +58,6 @@ import {
NodeCanvasFactory,
NodeCMapReaderFactory,
NodeFilterFactory,
NodePackages,
NodeStandardFontDataFactory,
} from "display-node_utils";
import { CanvasGraphics } from "./canvas.js";
Expand Down Expand Up @@ -2086,14 +2085,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
16 changes: 8 additions & 8 deletions src/display/node_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
* 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 {
extractFilenameFromHeader,
validateRangeRequestCapabilities,
} from "./network_utils.js";
import { NodePackages } from "./node_utils.js";

if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error(
Expand All @@ -29,7 +29,7 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
const fileUriRegex = /^file:\/\/\/[a-zA-Z]:\//;

function parseUrl(sourceUrl) {
const url = NodePackages.get("url");
const url = process.getBuiltinModule("url");
const parsedUrl = url.parse(sourceUrl);
if (parsedUrl.protocol === "file:" || parsedUrl.host) {
return parsedUrl;
Expand Down Expand Up @@ -335,13 +335,13 @@ class PDFNodeStreamFullReader extends BaseFullReader {

this._request = null;
if (this._url.protocol === "http:") {
const http = NodePackages.get("http");
const http = process.getBuiltinModule("http");
this._request = http.request(
createRequestOptions(this._url, stream.httpHeaders),
handleResponse
);
} else {
const https = NodePackages.get("https");
const https = process.getBuiltinModule("https");
this._request = https.request(
createRequestOptions(this._url, stream.httpHeaders),
handleResponse
Expand Down Expand Up @@ -384,13 +384,13 @@ class PDFNodeStreamRangeReader extends BaseRangeReader {

this._request = null;
if (this._url.protocol === "http:") {
const http = NodePackages.get("http");
const http = process.getBuiltinModule("http");
this._request = http.request(
createRequestOptions(this._url, this._httpHeaders),
handleResponse
);
} else {
const https = NodePackages.get("https");
const https = process.getBuiltinModule("https");
this._request = https.request(
createRequestOptions(this._url, this._httpHeaders),
handleResponse
Expand All @@ -415,7 +415,7 @@ class PDFNodeStreamFsFullReader extends BaseFullReader {
path = path.replace(/^\//, "");
}

const fs = NodePackages.get("fs");
const fs = process.getBuiltinModule("fs");
fs.promises.lstat(path).then(
stat => {
// Setting right content length.
Expand Down Expand Up @@ -446,7 +446,7 @@ class PDFNodeStreamFsRangeReader extends BaseRangeReader {
path = path.replace(/^\//, "");
}

const fs = NodePackages.get("fs");
const fs = process.getBuiltinModule("fs");
this._setReadableStream(fs.createReadStream(path, { 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);
5 changes: 2 additions & 3 deletions test/unit/node_stream_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ if (!isNodeJS) {
);
}

const path = await __non_webpack_import__("path");
const url = await __non_webpack_import__("url");

describe("node_stream", function () {
let tempServer = null;

const path = process.getBuiltinModule("path"),
url = process.getBuiltinModule("url");
const pdf = url.parse(
encodeURI(
"file://" + path.join(process.cwd(), "./test/pdfs/tracemonkey.pdf")
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 @@ -46,6 +39,7 @@ class DOMFileReaderFactory {
class NodeFileReaderFactory {
static async fetch(params) {
return new Promise((resolve, reject) => {
const fs = process.getBuiltinModule("fs");
fs.readFile(params.path, (error, data) => {
if (error || !data) {
reject(error || new Error(`Empty file for: ${params.path}`));
Expand Down Expand Up @@ -148,6 +142,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 8602df4

Please sign in to comment.