mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-23 08:38:06 +02:00
[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
7 changed files with 61 additions and 110 deletions
|
@ -56,7 +56,6 @@ import {
|
|||
NodeCanvasFactory,
|
||||
NodeCMapReaderFactory,
|
||||
NodeFilterFactory,
|
||||
NodePackages,
|
||||
NodeStandardFontDataFactory,
|
||||
} from "display-node_utils";
|
||||
import { CanvasGraphics } from "./canvas.js";
|
||||
|
@ -2137,14 +2136,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;
|
||||
}
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
* 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 {
|
||||
|
@ -19,7 +20,6 @@ import {
|
|||
extractFilenameFromHeader,
|
||||
validateRangeRequestCapabilities,
|
||||
} from "./network_utils.js";
|
||||
import { NodePackages } from "./node_utils.js";
|
||||
|
||||
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
||||
throw new Error(
|
||||
|
@ -33,16 +33,16 @@ function parseUrlOrPath(sourceUrl) {
|
|||
if (urlRegex.test(sourceUrl)) {
|
||||
return new URL(sourceUrl);
|
||||
}
|
||||
const url = NodePackages.get("url");
|
||||
const url = process.getBuiltinModule("url");
|
||||
return new URL(url.pathToFileURL(sourceUrl));
|
||||
}
|
||||
|
||||
function createRequest(url, headers, callback) {
|
||||
if (url.protocol === "http:") {
|
||||
const http = NodePackages.get("http");
|
||||
const http = process.getBuiltinModule("http");
|
||||
return http.request(url, { headers }, callback);
|
||||
}
|
||||
const https = NodePackages.get("https");
|
||||
const https = process.getBuiltinModule("https");
|
||||
return https.request(url, { headers }, callback);
|
||||
}
|
||||
|
||||
|
@ -365,7 +365,7 @@ class PDFNodeStreamFsFullReader extends BaseFullReader {
|
|||
constructor(stream) {
|
||||
super(stream);
|
||||
|
||||
const fs = NodePackages.get("fs");
|
||||
const fs = process.getBuiltinModule("fs");
|
||||
fs.promises.lstat(this._url).then(
|
||||
stat => {
|
||||
// Setting right content length.
|
||||
|
@ -389,7 +389,7 @@ class PDFNodeStreamFsRangeReader extends BaseRangeReader {
|
|||
constructor(stream, start, end) {
|
||||
super(stream);
|
||||
|
||||
const fs = NodePackages.get("fs");
|
||||
const fs = process.getBuiltinModule("fs");
|
||||
this._setReadableStream(
|
||||
fs.createReadStream(this._url, { start, end: end - 1 })
|
||||
);
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/* globals process */
|
||||
|
||||
import { isNodeJS, warn } from "../shared/util.js";
|
||||
import { BaseCanvasFactory } from "./canvas_factory.js";
|
||||
|
@ -25,94 +26,63 @@ 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;
|
||||
if (
|
||||
typeof PDFJSDev !== "undefined" &&
|
||||
!PDFJSDev.test("SKIP_BABEL") &&
|
||||
isNodeJS
|
||||
) {
|
||||
let canvas, path2d;
|
||||
try {
|
||||
const require = process
|
||||
.getBuiltinModule("module")
|
||||
.createRequire(import.meta.url);
|
||||
|
||||
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 {}
|
||||
try {
|
||||
canvas = require("canvas");
|
||||
} catch (ex) {
|
||||
warn(`Cannot load "canvas" package: "${ex}".`);
|
||||
}
|
||||
|
||||
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
|
||||
) {
|
||||
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();
|
||||
try {
|
||||
path2d = require("path2d");
|
||||
} catch (ex) {
|
||||
warn(`Cannot load "path2d" package: "${ex}".`);
|
||||
}
|
||||
);
|
||||
}
|
||||
} catch {}
|
||||
|
||||
class NodePackages {
|
||||
static get promise() {
|
||||
return packageCapability.promise;
|
||||
if (!globalThis.DOMMatrix) {
|
||||
const DOMMatrix = canvas?.DOMMatrix;
|
||||
|
||||
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) {
|
||||
return packageMap?.get(name);
|
||||
if (
|
||||
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) {
|
||||
const fs = NodePackages.get("fs");
|
||||
const fs = process.getBuiltinModule("fs");
|
||||
const data = await fs.promises.readFile(url);
|
||||
return new Uint8Array(data);
|
||||
}
|
||||
|
@ -124,7 +94,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);
|
||||
}
|
||||
}
|
||||
|
@ -152,6 +125,5 @@ export {
|
|||
NodeCanvasFactory,
|
||||
NodeCMapReaderFactory,
|
||||
NodeFilterFactory,
|
||||
NodePackages,
|
||||
NodeStandardFontDataFactory,
|
||||
};
|
||||
|
|
|
@ -18,7 +18,6 @@ const DOMStandardFontDataFactory = null;
|
|||
const NodeCanvasFactory = null;
|
||||
const NodeCMapReaderFactory = null;
|
||||
const NodeFilterFactory = null;
|
||||
const NodePackages = null;
|
||||
const NodeStandardFontDataFactory = null;
|
||||
const PDFFetchStream = null;
|
||||
const PDFNetworkStream = null;
|
||||
|
@ -30,7 +29,6 @@ export {
|
|||
NodeCanvasFactory,
|
||||
NodeCMapReaderFactory,
|
||||
NodeFilterFactory,
|
||||
NodePackages,
|
||||
NodeStandardFontDataFactory,
|
||||
PDFFetchStream,
|
||||
PDFNetworkStream,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue