[api-minor] Load Node.js packages/polyfills with process.getBuiltinModule
This allows *synchronous* loading of Node.js modules and (indirectly) packages, thus simplifying the code a fair bit.
This commit is contained in:
parent
4f01cdef18
commit
c7407230c1
@ -56,7 +56,6 @@ 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";
|
||||||
@ -2137,14 +2136,6 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -12,6 +12,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
/* globals process */
|
||||||
|
|
||||||
import { AbortException, assert, MissingPDFException } from "../shared/util.js";
|
import { AbortException, assert, MissingPDFException } from "../shared/util.js";
|
||||||
import {
|
import {
|
||||||
@ -19,7 +20,6 @@ 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(
|
||||||
@ -33,16 +33,16 @@ function parseUrlOrPath(sourceUrl) {
|
|||||||
if (urlRegex.test(sourceUrl)) {
|
if (urlRegex.test(sourceUrl)) {
|
||||||
return new URL(sourceUrl);
|
return new URL(sourceUrl);
|
||||||
}
|
}
|
||||||
const url = NodePackages.get("url");
|
const url = process.getBuiltinModule("url");
|
||||||
return new URL(url.pathToFileURL(sourceUrl));
|
return new URL(url.pathToFileURL(sourceUrl));
|
||||||
}
|
}
|
||||||
|
|
||||||
function createRequest(url, headers, callback) {
|
function createRequest(url, headers, callback) {
|
||||||
if (url.protocol === "http:") {
|
if (url.protocol === "http:") {
|
||||||
const http = NodePackages.get("http");
|
const http = process.getBuiltinModule("http");
|
||||||
return http.request(url, { headers }, callback);
|
return http.request(url, { headers }, callback);
|
||||||
}
|
}
|
||||||
const https = NodePackages.get("https");
|
const https = process.getBuiltinModule("https");
|
||||||
return https.request(url, { headers }, callback);
|
return https.request(url, { headers }, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -365,7 +365,7 @@ class PDFNodeStreamFsFullReader extends BaseFullReader {
|
|||||||
constructor(stream) {
|
constructor(stream) {
|
||||||
super(stream);
|
super(stream);
|
||||||
|
|
||||||
const fs = NodePackages.get("fs");
|
const fs = process.getBuiltinModule("fs");
|
||||||
fs.promises.lstat(this._url).then(
|
fs.promises.lstat(this._url).then(
|
||||||
stat => {
|
stat => {
|
||||||
// Setting right content length.
|
// Setting right content length.
|
||||||
@ -389,7 +389,7 @@ class PDFNodeStreamFsRangeReader extends BaseRangeReader {
|
|||||||
constructor(stream, start, end) {
|
constructor(stream, start, end) {
|
||||||
super(stream);
|
super(stream);
|
||||||
|
|
||||||
const fs = NodePackages.get("fs");
|
const fs = process.getBuiltinModule("fs");
|
||||||
this._setReadableStream(
|
this._setReadableStream(
|
||||||
fs.createReadStream(this._url, { start, end: end - 1 })
|
fs.createReadStream(this._url, { start, end: end - 1 })
|
||||||
);
|
);
|
||||||
|
|||||||
@ -12,6 +12,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
/* globals process */
|
||||||
|
|
||||||
import { isNodeJS, warn } from "../shared/util.js";
|
import { isNodeJS, warn } from "../shared/util.js";
|
||||||
import { BaseCanvasFactory } from "./canvas_factory.js";
|
import { BaseCanvasFactory } from "./canvas_factory.js";
|
||||||
@ -25,94 +26,63 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isNodeJS) {
|
if (
|
||||||
// eslint-disable-next-line no-var
|
typeof PDFJSDev !== "undefined" &&
|
||||||
var packageCapability = Promise.withResolvers();
|
!PDFJSDev.test("SKIP_BABEL") &&
|
||||||
// eslint-disable-next-line no-var
|
isNodeJS
|
||||||
var packageMap = null;
|
) {
|
||||||
|
let canvas, path2d;
|
||||||
|
try {
|
||||||
|
const require = process
|
||||||
|
.getBuiltinModule("module")
|
||||||
|
.createRequire(import.meta.url);
|
||||||
|
|
||||||
const loadPackages = async () => {
|
try {
|
||||||
// Native packages.
|
canvas = require("canvas");
|
||||||
const fs = await __non_webpack_import__("fs"),
|
} catch (ex) {
|
||||||
http = await __non_webpack_import__("http"),
|
warn(`Cannot load "canvas" package: "${ex}".`);
|
||||||
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 {}
|
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
return new Map(Object.entries({ fs, http, https, url, canvas, path2d }));
|
path2d = require("path2d");
|
||||||
};
|
} catch (ex) {
|
||||||
|
warn(`Cannot load "path2d" package: "${ex}".`);
|
||||||
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
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
applyPath2DToCanvasRenderingContext(CanvasRenderingContext2D);
|
|
||||||
} catch (ex) {
|
|
||||||
warn(`applyPath2DToCanvasRenderingContext: "${ex}".`);
|
|
||||||
}
|
|
||||||
globalThis.Path2D = Path2D;
|
|
||||||
} else {
|
|
||||||
warn("Cannot polyfill `Path2D`, rendering may be broken.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
reason => {
|
|
||||||
warn(`loadPackages: ${reason}`);
|
|
||||||
|
|
||||||
packageMap = new Map();
|
|
||||||
packageCapability.resolve();
|
|
||||||
}
|
}
|
||||||
);
|
} catch {}
|
||||||
}
|
|
||||||
|
|
||||||
class NodePackages {
|
if (!globalThis.DOMMatrix) {
|
||||||
static get promise() {
|
const DOMMatrix = canvas?.DOMMatrix;
|
||||||
return packageCapability.promise;
|
|
||||||
|
if (DOMMatrix) {
|
||||||
|
globalThis.DOMMatrix = DOMMatrix;
|
||||||
|
} else {
|
||||||
|
warn("Cannot polyfill `DOMMatrix`, rendering may be broken.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (!globalThis.Path2D) {
|
||||||
|
const CanvasRenderingContext2D = canvas?.CanvasRenderingContext2D;
|
||||||
|
const applyPath2DToCanvasRenderingContext =
|
||||||
|
path2d?.applyPath2DToCanvasRenderingContext;
|
||||||
|
const Path2D = path2d?.Path2D;
|
||||||
|
|
||||||
static get(name) {
|
if (
|
||||||
return packageMap?.get(name);
|
CanvasRenderingContext2D &&
|
||||||
|
applyPath2DToCanvasRenderingContext &&
|
||||||
|
Path2D
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
applyPath2DToCanvasRenderingContext(CanvasRenderingContext2D);
|
||||||
|
} catch (ex) {
|
||||||
|
warn(`applyPath2DToCanvasRenderingContext: "${ex}".`);
|
||||||
|
}
|
||||||
|
globalThis.Path2D = Path2D;
|
||||||
|
} else {
|
||||||
|
warn("Cannot polyfill `Path2D`, rendering may be broken.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchData(url) {
|
async function fetchData(url) {
|
||||||
const fs = NodePackages.get("fs");
|
const fs = process.getBuiltinModule("fs");
|
||||||
const data = await fs.promises.readFile(url);
|
const data = await fs.promises.readFile(url);
|
||||||
return new Uint8Array(data);
|
return new Uint8Array(data);
|
||||||
}
|
}
|
||||||
@ -124,7 +94,10 @@ class NodeCanvasFactory extends BaseCanvasFactory {
|
|||||||
* @ignore
|
* @ignore
|
||||||
*/
|
*/
|
||||||
_createCanvas(width, height) {
|
_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);
|
return canvas.createCanvas(width, height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -152,6 +125,5 @@ export {
|
|||||||
NodeCanvasFactory,
|
NodeCanvasFactory,
|
||||||
NodeCMapReaderFactory,
|
NodeCMapReaderFactory,
|
||||||
NodeFilterFactory,
|
NodeFilterFactory,
|
||||||
NodePackages,
|
|
||||||
NodeStandardFontDataFactory,
|
NodeStandardFontDataFactory,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -18,7 +18,6 @@ const DOMStandardFontDataFactory = null;
|
|||||||
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;
|
||||||
@ -30,7 +29,6 @@ export {
|
|||||||
NodeCanvasFactory,
|
NodeCanvasFactory,
|
||||||
NodeCMapReaderFactory,
|
NodeCMapReaderFactory,
|
||||||
NodeFilterFactory,
|
NodeFilterFactory,
|
||||||
NodePackages,
|
|
||||||
NodeStandardFontDataFactory,
|
NodeStandardFontDataFactory,
|
||||||
PDFFetchStream,
|
PDFFetchStream,
|
||||||
PDFNetworkStream,
|
PDFNetworkStream,
|
||||||
|
|||||||
@ -18,7 +18,6 @@ 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;
|
||||||
@ -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,
|
// 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);
|
||||||
|
|||||||
@ -24,11 +24,10 @@ if (!isNodeJS) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const url = await __non_webpack_import__("url");
|
|
||||||
|
|
||||||
describe("node_stream", function () {
|
describe("node_stream", function () {
|
||||||
let tempServer = null;
|
let tempServer = null;
|
||||||
|
|
||||||
|
const url = process.getBuiltinModule("url");
|
||||||
const cwdURL = url.pathToFileURL(process.cwd()) + "/";
|
const cwdURL = url.pathToFileURL(process.cwd()) + "/";
|
||||||
const pdf = new URL("./test/pdfs/tracemonkey.pdf", cwdURL).href;
|
const pdf = new URL("./test/pdfs/tracemonkey.pdf", cwdURL).href;
|
||||||
const pdfLength = 1016315;
|
const pdfLength = 1016315;
|
||||||
|
|||||||
@ -20,13 +20,6 @@ import { fetchData as fetchDataDOM } from "../../src/display/display_utils.js";
|
|||||||
import { fetchData as fetchDataNode } from "../../src/display/node_utils.js";
|
import { fetchData as fetchDataNode } from "../../src/display/node_utils.js";
|
||||||
import { Ref } from "../../src/core/primitives.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 TEST_PDFS_PATH = isNodeJS ? "./test/pdfs/" : "../pdfs/";
|
||||||
|
|
||||||
const CMAP_URL = isNodeJS ? "./external/bcmaps/" : "../../external/bcmaps/";
|
const CMAP_URL = isNodeJS ? "./external/bcmaps/" : "../../external/bcmaps/";
|
||||||
@ -132,6 +125,8 @@ function createIdFactory(pageIndex) {
|
|||||||
function createTemporaryNodeServer() {
|
function createTemporaryNodeServer() {
|
||||||
assert(isNodeJS, "Should only be used in Node.js environments.");
|
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.
|
// Create http server to serve pdf data for tests.
|
||||||
const server = http
|
const server = http
|
||||||
.createServer((request, response) => {
|
.createServer((request, response) => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user