Skip to content

Commit

Permalink
Update the Node.js compatibility-check in the worker-thread
Browse files Browse the repository at this point in the history
*Please note:* In Node.js environments a `legacy`-build must be used since only those versions include any polyfills.

Previously we'd only check if `ReadableStream` is natively supported, however since Node.js version 18 that's now been implemented; please see https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#browser_compatibility
Hence we'll also check for the availability of `Path2D`, since that's browser-specific functionality not expected to be available in Node.js environments; please see https://developer.mozilla.org/en-US/docs/Web/API/Path2D#browser_compatibility
  • Loading branch information
Snuffleupagus committed Mar 30, 2023
1 parent 2358757 commit 57a307d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/core/worker.js
Expand Up @@ -133,10 +133,14 @@ class WorkerMessageHandler {
// Ensure that (primarily) Node.js users won't accidentally attempt to use
// a non-translated/non-polyfilled build of the library, since that would
// quickly fail anyway because of missing functionality.
if (typeof ReadableStream === "undefined") {
if (
typeof Path2D === "undefined" ||
typeof ReadableStream === "undefined"
) {
const partialMsg =
"The browser/environment lacks native support for critical " +
"functionality used by the PDF.js library (e.g. `ReadableStream`); ";
"functionality used by the PDF.js library " +
"(e.g. `Path2D` and/or `ReadableStream`); ";

if (isNodeJS) {
throw new Error(partialMsg + "please use a `legacy`-build instead.");
Expand Down
2 changes: 1 addition & 1 deletion src/shared/compatibility.js
Expand Up @@ -36,7 +36,7 @@ import { isNodeJS } from "./is_node.js";
polyfillPath2D(globalThis);
})();

// Support: Node.js
// Support: Node.js<18.0.0
(function checkReadableStream() {
if (globalThis.ReadableStream || !isNodeJS) {
return;
Expand Down

0 comments on commit 57a307d

Please sign in to comment.