Merge pull request #18051 from Snuffleupagus/NodePackages
[api-minor] Re-factor how Node.js packages/polyfills are loaded (issue 17245)
This commit is contained in:
commit
298d72133e
10
gulpfile.mjs
10
gulpfile.mjs
@ -445,10 +445,8 @@ function checkChromePreferencesFile(chromePrefsPath, webPrefs) {
|
|||||||
|
|
||||||
function tweakWebpackOutput(jsName) {
|
function tweakWebpackOutput(jsName) {
|
||||||
const replacer = [
|
const replacer = [
|
||||||
" __webpack_exports__ = {};",
|
" __webpack_exports__ = {};", // Normal builds.
|
||||||
",__webpack_exports__={};",
|
",__webpack_exports__={};", // Minified builds.
|
||||||
" __webpack_exports__ = await __webpack_exports__;",
|
|
||||||
"\\(__webpack_exports__=await __webpack_exports__\\)",
|
|
||||||
];
|
];
|
||||||
const regex = new RegExp(`(${replacer.join("|")})`, "gm");
|
const regex = new RegExp(`(${replacer.join("|")})`, "gm");
|
||||||
|
|
||||||
@ -458,10 +456,6 @@ function tweakWebpackOutput(jsName) {
|
|||||||
return ` __webpack_exports__ = globalThis.${jsName} = {};`;
|
return ` __webpack_exports__ = globalThis.${jsName} = {};`;
|
||||||
case ",__webpack_exports__={};":
|
case ",__webpack_exports__={};":
|
||||||
return `,__webpack_exports__=globalThis.${jsName}={};`;
|
return `,__webpack_exports__=globalThis.${jsName}={};`;
|
||||||
case " __webpack_exports__ = await __webpack_exports__;":
|
|
||||||
return ` __webpack_exports__ = globalThis.${jsName} = await (globalThis.${jsName}Promise = __webpack_exports__);`;
|
|
||||||
case "(__webpack_exports__=await __webpack_exports__)":
|
|
||||||
return `(__webpack_exports__=globalThis.${jsName}=await (globalThis.${jsName}Promise=__webpack_exports__))`;
|
|
||||||
}
|
}
|
||||||
return match;
|
return match;
|
||||||
});
|
});
|
||||||
|
|||||||
@ -58,6 +58,7 @@ import {
|
|||||||
NodeCanvasFactory,
|
NodeCanvasFactory,
|
||||||
NodeCMapReaderFactory,
|
NodeCMapReaderFactory,
|
||||||
NodeFilterFactory,
|
NodeFilterFactory,
|
||||||
|
NodePackages,
|
||||||
NodeStandardFontDataFactory,
|
NodeStandardFontDataFactory,
|
||||||
} from "display-node_utils";
|
} from "display-node_utils";
|
||||||
import { CanvasGraphics } from "./canvas.js";
|
import { CanvasGraphics } from "./canvas.js";
|
||||||
@ -2094,6 +2095,14 @@ class PDFWorker {
|
|||||||
* @type {Promise<void>}
|
* @type {Promise<void>}
|
||||||
*/
|
*/
|
||||||
get 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;
|
return this._readyCapability.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -13,16 +13,12 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {
|
import { AbortException, assert, MissingPDFException } from "../shared/util.js";
|
||||||
AbortException,
|
|
||||||
assert,
|
|
||||||
isNodeJS,
|
|
||||||
MissingPDFException,
|
|
||||||
} from "../shared/util.js";
|
|
||||||
import {
|
import {
|
||||||
extractFilenameFromHeader,
|
extractFilenameFromHeader,
|
||||||
validateRangeRequestCapabilities,
|
validateRangeRequestCapabilities,
|
||||||
} from "./network_utils.js";
|
} from "./network_utils.js";
|
||||||
|
import { NodePackages } from "./node_utils.js";
|
||||||
|
|
||||||
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
||||||
throw new Error(
|
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]:\//;
|
const fileUriRegex = /^file:\/\/\/[a-zA-Z]:\//;
|
||||||
|
|
||||||
function parseUrl(sourceUrl) {
|
function parseUrl(sourceUrl) {
|
||||||
|
const url = NodePackages.get("url");
|
||||||
const parsedUrl = url.parse(sourceUrl);
|
const parsedUrl = url.parse(sourceUrl);
|
||||||
if (parsedUrl.protocol === "file:" || parsedUrl.host) {
|
if (parsedUrl.protocol === "file:" || parsedUrl.host) {
|
||||||
return parsedUrl;
|
return parsedUrl;
|
||||||
@ -347,11 +335,13 @@ class PDFNodeStreamFullReader extends BaseFullReader {
|
|||||||
|
|
||||||
this._request = null;
|
this._request = null;
|
||||||
if (this._url.protocol === "http:") {
|
if (this._url.protocol === "http:") {
|
||||||
|
const http = NodePackages.get("http");
|
||||||
this._request = http.request(
|
this._request = http.request(
|
||||||
createRequestOptions(this._url, stream.httpHeaders),
|
createRequestOptions(this._url, stream.httpHeaders),
|
||||||
handleResponse
|
handleResponse
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
const https = NodePackages.get("https");
|
||||||
this._request = https.request(
|
this._request = https.request(
|
||||||
createRequestOptions(this._url, stream.httpHeaders),
|
createRequestOptions(this._url, stream.httpHeaders),
|
||||||
handleResponse
|
handleResponse
|
||||||
@ -394,11 +384,13 @@ class PDFNodeStreamRangeReader extends BaseRangeReader {
|
|||||||
|
|
||||||
this._request = null;
|
this._request = null;
|
||||||
if (this._url.protocol === "http:") {
|
if (this._url.protocol === "http:") {
|
||||||
|
const http = NodePackages.get("http");
|
||||||
this._request = http.request(
|
this._request = http.request(
|
||||||
createRequestOptions(this._url, this._httpHeaders),
|
createRequestOptions(this._url, this._httpHeaders),
|
||||||
handleResponse
|
handleResponse
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
const https = NodePackages.get("https");
|
||||||
this._request = https.request(
|
this._request = https.request(
|
||||||
createRequestOptions(this._url, this._httpHeaders),
|
createRequestOptions(this._url, this._httpHeaders),
|
||||||
handleResponse
|
handleResponse
|
||||||
@ -423,6 +415,7 @@ class PDFNodeStreamFsFullReader extends BaseFullReader {
|
|||||||
path = path.replace(/^\//, "");
|
path = path.replace(/^\//, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fs = NodePackages.get("fs");
|
||||||
fs.promises.lstat(path).then(
|
fs.promises.lstat(path).then(
|
||||||
stat => {
|
stat => {
|
||||||
// Setting right content length.
|
// Setting right content length.
|
||||||
@ -453,6 +446,7 @@ class PDFNodeStreamFsRangeReader extends BaseRangeReader {
|
|||||||
path = path.replace(/^\//, "");
|
path = path.replace(/^\//, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fs = NodePackages.get("fs");
|
||||||
this._setReadableStream(fs.createReadStream(path, { start, end: end - 1 }));
|
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) {
|
if (isNodeJS) {
|
||||||
// Native packages.
|
// eslint-disable-next-line no-var
|
||||||
fs = await __non_webpack_import__("fs");
|
var packageCapability = Promise.withResolvers();
|
||||||
// Optional, third-party, packages.
|
// eslint-disable-next-line no-var
|
||||||
try {
|
var packageMap = null;
|
||||||
canvas = await __non_webpack_import__("canvas");
|
|
||||||
} catch {}
|
const loadPackages = async () => {
|
||||||
try {
|
// Native packages.
|
||||||
path2d = await __non_webpack_import__("path2d");
|
const fs = await __non_webpack_import__("fs"),
|
||||||
} catch {}
|
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")) {
|
class NodePackages {
|
||||||
(function checkDOMMatrix() {
|
static get promise() {
|
||||||
if (globalThis.DOMMatrix || !isNodeJS) {
|
return packageCapability.promise;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
const DOMMatrix = canvas?.DOMMatrix;
|
|
||||||
|
|
||||||
if (DOMMatrix) {
|
static get(name) {
|
||||||
globalThis.DOMMatrix = DOMMatrix;
|
return packageMap?.get(name);
|
||||||
} 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.");
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const fetchData = function (url) {
|
const fetchData = function (url) {
|
||||||
|
const fs = NodePackages.get("fs");
|
||||||
return fs.promises.readFile(url).then(data => new Uint8Array(data));
|
return fs.promises.readFile(url).then(data => new Uint8Array(data));
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -87,6 +121,7 @@ class NodeCanvasFactory extends BaseCanvasFactory {
|
|||||||
* @ignore
|
* @ignore
|
||||||
*/
|
*/
|
||||||
_createCanvas(width, height) {
|
_createCanvas(width, height) {
|
||||||
|
const canvas = NodePackages.get("canvas");
|
||||||
return canvas.createCanvas(width, height);
|
return canvas.createCanvas(width, height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -113,5 +148,6 @@ export {
|
|||||||
NodeCanvasFactory,
|
NodeCanvasFactory,
|
||||||
NodeCMapReaderFactory,
|
NodeCMapReaderFactory,
|
||||||
NodeFilterFactory,
|
NodeFilterFactory,
|
||||||
|
NodePackages,
|
||||||
NodeStandardFontDataFactory,
|
NodeStandardFontDataFactory,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
const NodeCanvasFactory = null;
|
const NodeCanvasFactory = null;
|
||||||
const NodeCMapReaderFactory = null;
|
const NodeCMapReaderFactory = null;
|
||||||
const NodeFilterFactory = null;
|
const NodeFilterFactory = null;
|
||||||
|
const NodePackages = null;
|
||||||
const NodeStandardFontDataFactory = null;
|
const NodeStandardFontDataFactory = null;
|
||||||
const PDFFetchStream = null;
|
const PDFFetchStream = null;
|
||||||
const PDFNetworkStream = null;
|
const PDFNetworkStream = null;
|
||||||
@ -25,6 +26,7 @@ export {
|
|||||||
NodeCanvasFactory,
|
NodeCanvasFactory,
|
||||||
NodeCMapReaderFactory,
|
NodeCMapReaderFactory,
|
||||||
NodeFilterFactory,
|
NodeFilterFactory,
|
||||||
|
NodePackages,
|
||||||
NodeStandardFontDataFactory,
|
NodeStandardFontDataFactory,
|
||||||
PDFFetchStream,
|
PDFFetchStream,
|
||||||
PDFNetworkStream,
|
PDFNetworkStream,
|
||||||
|
|||||||
@ -18,6 +18,7 @@ import {
|
|||||||
setVerbosityLevel,
|
setVerbosityLevel,
|
||||||
VerbosityLevel,
|
VerbosityLevel,
|
||||||
} from "../../src/shared/util.js";
|
} from "../../src/shared/util.js";
|
||||||
|
import { NodePackages } from "../../src/display/node_utils.js";
|
||||||
|
|
||||||
// Sets longer timeout, similar to `jasmine-boot.js`.
|
// Sets longer timeout, similar to `jasmine-boot.js`.
|
||||||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
|
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
|
||||||
@ -29,6 +30,9 @@ 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,
|
// Reduce the amount of console "spam", by ignoring `info`/`warn` calls,
|
||||||
// when running the unit-tests in Node.js/Travis.
|
// when running the unit-tests in Node.js/Travis.
|
||||||
setVerbosityLevel(VerbosityLevel.ERRORS);
|
setVerbosityLevel(VerbosityLevel.ERRORS);
|
||||||
|
|||||||
@ -13,15 +13,6 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Ensure that the viewer waits for the library to complete loading,
|
|
||||||
// to avoid breaking e.g. the standalone viewer components (see issue 17228).
|
|
||||||
if (
|
|
||||||
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) &&
|
|
||||||
!globalThis.pdfjsLib
|
|
||||||
) {
|
|
||||||
await globalThis.pdfjsLibPromise;
|
|
||||||
}
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
AbortException,
|
AbortException,
|
||||||
AnnotationEditorLayer,
|
AnnotationEditorLayer,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user