@@ -27,56 +27,90 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
2727 ) ;
2828}
2929
30- let fs , canvas , path2d ;
3130if ( isNodeJS ) {
32- // Native packages.
33- fs = await __non_webpack_import__ ( "fs" ) ;
34- // Optional, third-party, packages.
35- try {
36- canvas = await __non_webpack_import__ ( "canvas" ) ;
37- } catch { }
38- try {
39- path2d = await __non_webpack_import__ ( "path2d" ) ;
40- } catch { }
41- }
31+ // eslint-disable-next-line no-var
32+ var packageCapability = Promise . withResolvers ( ) ;
33+ // eslint-disable-next-line no-var
34+ var packageMap = null ;
4235
43- if ( typeof PDFJSDev !== "undefined" && ! PDFJSDev . test ( "SKIP_BABEL" ) ) {
44- ( function checkDOMMatrix ( ) {
45- if ( globalThis . DOMMatrix || ! isNodeJS ) {
46- return ;
47- }
48- const DOMMatrix = canvas ?. DOMMatrix ;
36+ const loadPackages = async ( ) => {
37+ // Native packages.
38+ const fs = await __non_webpack_import__ ( "fs" ) ,
39+ http = await __non_webpack_import__ ( "http" ) ,
40+ https = await __non_webpack_import__ ( "https" ) ,
41+ url = await __non_webpack_import__ ( "url" ) ;
4942
50- if ( DOMMatrix ) {
51- globalThis . DOMMatrix = DOMMatrix ;
52- } else {
53- warn ( "Cannot polyfill `DOMMatrix`, rendering may be broken." ) ;
43+ // Optional, third-party, packages.
44+ let canvas , path2d ;
45+ if ( typeof PDFJSDev !== "undefined" && ! PDFJSDev . test ( "SKIP_BABEL" ) ) {
46+ try {
47+ canvas = await __non_webpack_import__ ( "canvas" ) ;
48+ } catch { }
49+ try {
50+ path2d = await __non_webpack_import__ ( "path2d" ) ;
51+ } catch { }
5452 }
55- } ) ( ) ;
5653
57- ( function checkPath2D ( ) {
58- if ( globalThis . Path2D || ! isNodeJS ) {
59- return ;
60- }
61- const CanvasRenderingContext2D = canvas ?. CanvasRenderingContext2D ;
62- const applyPath2DToCanvasRenderingContext =
63- path2d ?. applyPath2DToCanvasRenderingContext ;
64- const Path2D = path2d ?. Path2D ;
65-
66- if (
67- CanvasRenderingContext2D &&
68- applyPath2DToCanvasRenderingContext &&
69- Path2D
70- ) {
71- applyPath2DToCanvasRenderingContext ( CanvasRenderingContext2D ) ;
72- globalThis . Path2D = Path2D ;
73- } else {
74- warn ( "Cannot polyfill `Path2D`, rendering may be broken." ) ;
54+ return new Map ( Object . entries ( { fs, http, https, url, canvas, path2d } ) ) ;
55+ } ;
56+
57+ loadPackages ( ) . then (
58+ map => {
59+ packageMap = map ;
60+ packageCapability . resolve ( ) ;
61+
62+ if ( typeof PDFJSDev === "undefined" || PDFJSDev . test ( "SKIP_BABEL" ) ) {
63+ return ;
64+ }
65+ if ( ! globalThis . DOMMatrix ) {
66+ const DOMMatrix = map . get ( "canvas" ) ?. DOMMatrix ;
67+
68+ if ( DOMMatrix ) {
69+ globalThis . DOMMatrix = DOMMatrix ;
70+ } else {
71+ warn ( "Cannot polyfill `DOMMatrix`, rendering may be broken." ) ;
72+ }
73+ }
74+ if ( ! globalThis . Path2D ) {
75+ const CanvasRenderingContext2D =
76+ map . get ( "canvas" ) ?. CanvasRenderingContext2D ;
77+ const applyPath2DToCanvasRenderingContext =
78+ map . get ( "path2d" ) ?. applyPath2DToCanvasRenderingContext ;
79+ const Path2D = map . get ( "path2d" ) ?. Path2D ;
80+
81+ if (
82+ CanvasRenderingContext2D &&
83+ applyPath2DToCanvasRenderingContext &&
84+ Path2D
85+ ) {
86+ applyPath2DToCanvasRenderingContext ( CanvasRenderingContext2D ) ;
87+ globalThis . Path2D = Path2D ;
88+ } else {
89+ warn ( "Cannot polyfill `Path2D`, rendering may be broken." ) ;
90+ }
91+ }
92+ } ,
93+ reason => {
94+ warn ( `loadPackages: ${ reason } ` ) ;
95+
96+ packageMap = new Map ( ) ;
97+ packageCapability . resolve ( ) ;
7598 }
76- } ) ( ) ;
99+ ) ;
100+ }
101+
102+ class NodePackages {
103+ static get promise ( ) {
104+ return packageCapability . promise ;
105+ }
106+
107+ static get ( name ) {
108+ return packageMap ?. get ( name ) ;
109+ }
77110}
78111
79112const fetchData = function ( url ) {
113+ const fs = NodePackages . get ( "fs" ) ;
80114 return fs . promises . readFile ( url ) . then ( data => new Uint8Array ( data ) ) ;
81115} ;
82116
@@ -87,6 +121,7 @@ class NodeCanvasFactory extends BaseCanvasFactory {
87121 * @ignore
88122 */
89123 _createCanvas ( width , height ) {
124+ const canvas = NodePackages . get ( "canvas" ) ;
90125 return canvas . createCanvas ( width , height ) ;
91126 }
92127}
@@ -113,5 +148,6 @@ export {
113148 NodeCanvasFactory ,
114149 NodeCMapReaderFactory ,
115150 NodeFilterFactory ,
151+ NodePackages ,
116152 NodeStandardFontDataFactory ,
117153} ;
0 commit comments