mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
[api-minor] Re-factor how Node.js packages/polyfills are loaded (issue 17245)
*Please note:* This removes top level await from the GENERIC builds of the PDF.js library. Despite top level await being supported in all modern browsers/environments, note [the MDN compatibility data](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#browser_compatibility), it seems that many frameworks and build-tools unfortunately have trouble with it. Hence, in order to reduce the influx of support requests regarding top level await it thus seems that we'll have to try and fix this. Given that top level await is only needed for Node.js environments, to load packages/polyfills, we re-factor things to limit the asynchronicity to that environment. The "best" solution, with the least likelihood of causing future problems, would probably be to await the load of Node.js packages/polyfills e.g. at the top of the `getDocument`-function. Unfortunately that doesn't work though, since that's a *synchronous* function that we cannot change without breaking "the world". Hence we instead await the load of Node.js packages/polyfills together with the `PDFWorker` initialization, since that's the *first point* of asynchronicity during initialization/loading of a PDF document. The reason that this works is that the Node.js packages/polyfills are only needed during fetching of the PDF document respectively during rendering, neither of which can happen *until* the worker has been initialized. Hopefully this won't cause any future problems, since looking at the history of the PDF.js project I don't believe that we've (thus far) ever needed a Node.js dependency at an earlier point. This new pattern for accessing Node.js packages/polyfills will also require some care during development *and* importantly reviewing, to ensure that no new top level await is added in the main code-base.
This commit is contained in:
parent
b6765403a1
commit
2643570364
5 changed files with 103 additions and 58 deletions
|
@ -58,6 +58,7 @@ import {
|
|||
NodeCanvasFactory,
|
||||
NodeCMapReaderFactory,
|
||||
NodeFilterFactory,
|
||||
NodePackages,
|
||||
NodeStandardFontDataFactory,
|
||||
} from "display-node_utils";
|
||||
import { CanvasGraphics } from "./canvas.js";
|
||||
|
@ -2089,6 +2090,14 @@ 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -13,16 +13,12 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {
|
||||
AbortException,
|
||||
assert,
|
||||
isNodeJS,
|
||||
MissingPDFException,
|
||||
} from "../shared/util.js";
|
||||
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(
|
||||
|
@ -30,18 +26,10 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
|||
);
|
||||
}
|
||||
|
||||
let fs, http, https, url;
|
||||
if (isNodeJS) {
|
||||
// Native packages.
|
||||
fs = await __non_webpack_import__("fs");
|
||||
http = await __non_webpack_import__("http");
|
||||
https = await __non_webpack_import__("https");
|
||||
url = await __non_webpack_import__("url");
|
||||
}
|
||||
|
||||
const fileUriRegex = /^file:\/\/\/[a-zA-Z]:\//;
|
||||
|
||||
function parseUrl(sourceUrl) {
|
||||
const url = NodePackages.get("url");
|
||||
const parsedUrl = url.parse(sourceUrl);
|
||||
if (parsedUrl.protocol === "file:" || parsedUrl.host) {
|
||||
return parsedUrl;
|
||||
|
@ -347,11 +335,13 @@ class PDFNodeStreamFullReader extends BaseFullReader {
|
|||
|
||||
this._request = null;
|
||||
if (this._url.protocol === "http:") {
|
||||
const http = NodePackages.get("http");
|
||||
this._request = http.request(
|
||||
createRequestOptions(this._url, stream.httpHeaders),
|
||||
handleResponse
|
||||
);
|
||||
} else {
|
||||
const https = NodePackages.get("https");
|
||||
this._request = https.request(
|
||||
createRequestOptions(this._url, stream.httpHeaders),
|
||||
handleResponse
|
||||
|
@ -394,11 +384,13 @@ class PDFNodeStreamRangeReader extends BaseRangeReader {
|
|||
|
||||
this._request = null;
|
||||
if (this._url.protocol === "http:") {
|
||||
const http = NodePackages.get("http");
|
||||
this._request = http.request(
|
||||
createRequestOptions(this._url, this._httpHeaders),
|
||||
handleResponse
|
||||
);
|
||||
} else {
|
||||
const https = NodePackages.get("https");
|
||||
this._request = https.request(
|
||||
createRequestOptions(this._url, this._httpHeaders),
|
||||
handleResponse
|
||||
|
@ -423,6 +415,7 @@ class PDFNodeStreamFsFullReader extends BaseFullReader {
|
|||
path = path.replace(/^\//, "");
|
||||
}
|
||||
|
||||
const fs = NodePackages.get("fs");
|
||||
fs.promises.lstat(path).then(
|
||||
stat => {
|
||||
// Setting right content length.
|
||||
|
@ -453,6 +446,7 @@ class PDFNodeStreamFsRangeReader extends BaseRangeReader {
|
|||
path = path.replace(/^\//, "");
|
||||
}
|
||||
|
||||
const fs = NodePackages.get("fs");
|
||||
this._setReadableStream(fs.createReadStream(path, { start, end: end - 1 }));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,56 +27,90 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
|||
);
|
||||
}
|
||||
|
||||
let fs, canvas, path2d;
|
||||
if (isNodeJS) {
|
||||
// Native packages.
|
||||
fs = await __non_webpack_import__("fs");
|
||||
// Optional, third-party, packages.
|
||||
try {
|
||||
canvas = await __non_webpack_import__("canvas");
|
||||
} catch {}
|
||||
try {
|
||||
path2d = await __non_webpack_import__("path2d");
|
||||
} catch {}
|
||||
// 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")) {
|
||||
(function checkDOMMatrix() {
|
||||
if (globalThis.DOMMatrix || !isNodeJS) {
|
||||
return;
|
||||
}
|
||||
const DOMMatrix = canvas?.DOMMatrix;
|
||||
class NodePackages {
|
||||
static get promise() {
|
||||
return packageCapability.promise;
|
||||
}
|
||||
|
||||
if (DOMMatrix) {
|
||||
globalThis.DOMMatrix = DOMMatrix;
|
||||
} else {
|
||||
warn("Cannot polyfill `DOMMatrix`, rendering may be broken.");
|
||||
}
|
||||
})();
|
||||
|
||||
(function checkPath2D() {
|
||||
if (globalThis.Path2D || !isNodeJS) {
|
||||
return;
|
||||
}
|
||||
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.");
|
||||
}
|
||||
})();
|
||||
static get(name) {
|
||||
return packageMap?.get(name);
|
||||
}
|
||||
}
|
||||
|
||||
const fetchData = function (url) {
|
||||
const fs = NodePackages.get("fs");
|
||||
return fs.promises.readFile(url).then(data => new Uint8Array(data));
|
||||
};
|
||||
|
||||
|
@ -87,6 +121,7 @@ class NodeCanvasFactory extends BaseCanvasFactory {
|
|||
* @ignore
|
||||
*/
|
||||
_createCanvas(width, height) {
|
||||
const canvas = NodePackages.get("canvas");
|
||||
return canvas.createCanvas(width, height);
|
||||
}
|
||||
}
|
||||
|
@ -113,5 +148,6 @@ export {
|
|||
NodeCanvasFactory,
|
||||
NodeCMapReaderFactory,
|
||||
NodeFilterFactory,
|
||||
NodePackages,
|
||||
NodeStandardFontDataFactory,
|
||||
};
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
const NodeCanvasFactory = null;
|
||||
const NodeCMapReaderFactory = null;
|
||||
const NodeFilterFactory = null;
|
||||
const NodePackages = null;
|
||||
const NodeStandardFontDataFactory = null;
|
||||
const PDFFetchStream = null;
|
||||
const PDFNetworkStream = null;
|
||||
|
@ -25,6 +26,7 @@ export {
|
|||
NodeCanvasFactory,
|
||||
NodeCMapReaderFactory,
|
||||
NodeFilterFactory,
|
||||
NodePackages,
|
||||
NodeStandardFontDataFactory,
|
||||
PDFFetchStream,
|
||||
PDFNetworkStream,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue