@file-viewer/renderer-pdf 2.1.9 → 2.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/pdf.js +74 -1
  2. package/package.json +2 -2
package/dist/pdf.js CHANGED
@@ -11,12 +11,14 @@ const FIT_HORIZONTAL_PADDING = 28;
11
11
  const PAGE_BORDER_WIDTH = 18;
12
12
  const PDF_EXPORT_MAX_PAGE_PIXELS = 8000000;
13
13
  const PDF_WORKER_PROBE_TIMEOUT_MS = 1200;
14
+ const PDF_JS_DESTROY_CONSOLE_SUPPRESSION_MS = 1500;
14
15
  let bundledPdfWorkerModulePromise = null;
15
16
  // PDF.js viewer CSS references image assets that are not shipped with the
16
17
  // on-demand renderer chunk, so keep the preview self-contained and 404-free.
17
18
  const normalizedPdfViewerStyle = pdfViewerStyle
18
19
  .replace(/--page-border-image:\s*url\(images\/shadow\.png\)\s*9 9 repeat;/g, '--page-border-image:none;')
19
20
  .replace(/background:\s*url\("\.\/images\/loading-icon\.gif"\)\s*center no-repeat;/g, 'background:none;');
21
+ const pdfJsConsoleErrorSuppressions = new WeakMap();
20
22
  const createStyle = (documentRef) => {
21
23
  const style = documentRef.createElement('style');
22
24
  style.textContent = `${normalizedPdfViewerStyle}
@@ -88,6 +90,71 @@ const waitForPaint = (view) => new Promise(resolve => {
88
90
  }
89
91
  globalThis.setTimeout(resolve, 0);
90
92
  });
93
+ const readErrorLikeMessage = (value) => {
94
+ if (value instanceof Error) {
95
+ return value.message;
96
+ }
97
+ if (value && typeof value === 'object' && 'message' in value) {
98
+ return String(value.message || '');
99
+ }
100
+ return String(value || '');
101
+ };
102
+ const isPdfJsDestroyedTransportPageInitError = (args) => {
103
+ const [message, reason] = args;
104
+ return typeof message === 'string' &&
105
+ /^Unable to get page \d+ to initialize viewer$/.test(message) &&
106
+ readErrorLikeMessage(reason).includes('Transport destroyed');
107
+ };
108
+ const suppressPdfJsDestroyedTransportPageInitErrors = (view) => {
109
+ const consoleRef = (view.console ||
110
+ globalThis.console);
111
+ if (!consoleRef || typeof consoleRef.error !== 'function') {
112
+ return () => { };
113
+ }
114
+ let suppression = pdfJsConsoleErrorSuppressions.get(consoleRef);
115
+ if (!suppression) {
116
+ const originalError = consoleRef.error;
117
+ suppression = {
118
+ originalError,
119
+ patchedError: (...args) => {
120
+ if (isPdfJsDestroyedTransportPageInitError(args)) {
121
+ return;
122
+ }
123
+ return originalError.apply(consoleRef, args);
124
+ },
125
+ depth: 0,
126
+ restoreTimer: undefined,
127
+ };
128
+ pdfJsConsoleErrorSuppressions.set(consoleRef, suppression);
129
+ consoleRef.error = suppression.patchedError;
130
+ }
131
+ else if (suppression.restoreTimer !== undefined) {
132
+ view.clearTimeout(suppression.restoreTimer);
133
+ suppression.restoreTimer = undefined;
134
+ }
135
+ suppression.depth += 1;
136
+ let restored = false;
137
+ return () => {
138
+ if (restored || !suppression) {
139
+ return;
140
+ }
141
+ restored = true;
142
+ suppression.depth = Math.max(0, suppression.depth - 1);
143
+ if (suppression.depth > 0) {
144
+ return;
145
+ }
146
+ suppression.restoreTimer = view.setTimeout(() => {
147
+ if (!suppression || suppression.depth > 0) {
148
+ return;
149
+ }
150
+ if (consoleRef.error === suppression.patchedError) {
151
+ consoleRef.error = suppression.originalError;
152
+ }
153
+ pdfJsConsoleErrorSuppressions.delete(consoleRef);
154
+ suppression.restoreTimer = undefined;
155
+ }, PDF_JS_DESTROY_CONSOLE_SUPPRESSION_MS);
156
+ };
157
+ };
91
158
  const isConfiguredUrl = (value) => {
92
159
  return value !== undefined && value !== null && String(value).trim().length > 0;
93
160
  };
@@ -810,6 +877,7 @@ export default async function renderPdf(buffer, target, context) {
810
877
  if (!resource) {
811
878
  return;
812
879
  }
880
+ const restorePdfJsConsoleErrors = suppressPdfJsDestroyedTransportPageInitErrors(targetWindow);
813
881
  try {
814
882
  await resource.loadingTask.destroy();
815
883
  }
@@ -817,7 +885,12 @@ export default async function renderPdf(buffer, target, context) {
817
885
  console.warn('PDF 加载任务销毁失败', error);
818
886
  }
819
887
  finally {
820
- (_a = resource.worker) === null || _a === void 0 ? void 0 : _a.destroy();
888
+ try {
889
+ (_a = resource.worker) === null || _a === void 0 ? void 0 : _a.destroy();
890
+ }
891
+ finally {
892
+ restorePdfJsConsoleErrors();
893
+ }
821
894
  }
822
895
  };
823
896
  const loadOutline = async (pdfDocument) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@file-viewer/renderer-pdf",
3
- "version": "2.1.9",
3
+ "version": "2.1.11",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Standalone PDF renderer plugin for Flyfish File Viewer powered by PDF.js.",
@@ -54,7 +54,7 @@
54
54
  "LICENSE"
55
55
  ],
56
56
  "dependencies": {
57
- "@file-viewer/core": "^2.1.9",
57
+ "@file-viewer/core": "^2.1.11",
58
58
  "pdfjs-dist": "^6.0.227"
59
59
  },
60
60
  "devDependencies": {