@file-viewer/core 2.0.7 → 2.0.8

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.
@@ -8,6 +8,8 @@ export declare const DEFAULT_FILE_VIEWER_CAD_WORKER_PATH = "wasm/cad/dwg-worker.
8
8
  export declare const DEFAULT_FILE_VIEWER_CAD_DWF_WASM_PATH = "wasm/cad/dwfv-render.wasm";
9
9
  export declare const DEFAULT_FILE_VIEWER_TYPST_COMPILER_WASM_URL = "wasm/typst/typst_ts_web_compiler_bg.wasm";
10
10
  export declare const DEFAULT_FILE_VIEWER_TYPST_RENDERER_WASM_URL = "wasm/typst/typst_ts_renderer_bg.wasm";
11
+ export declare const FALLBACK_FILE_VIEWER_TYPST_COMPILER_WASM_URL = "https://cdn.jsdelivr.net/npm/@myriaddreamin/typst-ts-web-compiler@0.7.0/pkg/typst_ts_web_compiler_bg.wasm";
12
+ export declare const FALLBACK_FILE_VIEWER_TYPST_RENDERER_WASM_URL = "https://cdn.jsdelivr.net/npm/@myriaddreamin/typst-ts-renderer@0.7.0/pkg/typst_ts_renderer_bg.wasm";
11
13
  export declare const DEFAULT_FILE_VIEWER_TYPST_COMPILER_WASM_PACKAGE_PATH = "@myriaddreamin/typst-ts-web-compiler/pkg/typst_ts_web_compiler_bg.wasm";
12
14
  export declare const DEFAULT_FILE_VIEWER_TYPST_RENDERER_WASM_PACKAGE_PATH = "@myriaddreamin/typst-ts-renderer/pkg/typst_ts_renderer_bg.wasm";
13
15
  export declare const DEFAULT_FILE_VIEWER_DATA_SQL_WASM_URL = "wasm/data/sql-wasm.wasm";
@@ -7,6 +7,8 @@ export const DEFAULT_FILE_VIEWER_CAD_WORKER_PATH = 'wasm/cad/dwg-worker.js';
7
7
  export const DEFAULT_FILE_VIEWER_CAD_DWF_WASM_PATH = 'wasm/cad/dwfv-render.wasm';
8
8
  export const DEFAULT_FILE_VIEWER_TYPST_COMPILER_WASM_URL = 'wasm/typst/typst_ts_web_compiler_bg.wasm';
9
9
  export const DEFAULT_FILE_VIEWER_TYPST_RENDERER_WASM_URL = 'wasm/typst/typst_ts_renderer_bg.wasm';
10
+ export const FALLBACK_FILE_VIEWER_TYPST_COMPILER_WASM_URL = 'https://cdn.jsdelivr.net/npm/@myriaddreamin/typst-ts-web-compiler@0.7.0/pkg/typst_ts_web_compiler_bg.wasm';
11
+ export const FALLBACK_FILE_VIEWER_TYPST_RENDERER_WASM_URL = 'https://cdn.jsdelivr.net/npm/@myriaddreamin/typst-ts-renderer@0.7.0/pkg/typst_ts_renderer_bg.wasm';
10
12
  export const DEFAULT_FILE_VIEWER_TYPST_COMPILER_WASM_PACKAGE_PATH = '@myriaddreamin/typst-ts-web-compiler/pkg/typst_ts_web_compiler_bg.wasm';
11
13
  export const DEFAULT_FILE_VIEWER_TYPST_RENDERER_WASM_PACKAGE_PATH = '@myriaddreamin/typst-ts-renderer/pkg/typst_ts_renderer_bg.wasm';
12
14
  export const DEFAULT_FILE_VIEWER_DATA_SQL_WASM_URL = 'wasm/data/sql-wasm.wasm';
@@ -1,5 +1,5 @@
1
1
  import { $typst } from '@myriaddreamin/typst.ts';
2
- import { resolveFileViewerTypstCompilerWasmUrl, resolveFileViewerTypstRendererWasmUrl, } from '../platform/assets.js';
2
+ import { FALLBACK_FILE_VIEWER_TYPST_COMPILER_WASM_URL, FALLBACK_FILE_VIEWER_TYPST_RENDERER_WASM_URL, resolveFileViewerTypstCompilerWasmUrl, resolveFileViewerTypstRendererWasmUrl, } from '../platform/assets.js';
3
3
  import { registerFileViewerZoomProvider, unregisterFileViewerZoomProvider, } from '../features/document/dom/index.js';
4
4
  import { createFileViewerZoomChangeEmitter } from '../features/document/zoom.js';
5
5
  import { formatCssPixels } from '../output/printLayout.js';
@@ -70,15 +70,7 @@ const getWindowOverride = (key) => {
70
70
  }
71
71
  return window[key];
72
72
  };
73
- const configureTypstEngine = (context, documentBaseUrl) => {
74
- var _a;
75
- const typstOptions = (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.typst;
76
- const compilerWasmUrl = resolveFileViewerTypstCompilerWasmUrl(typstOptions, [
77
- getWindowOverride('__FLYFISH_TYPST_COMPILER_WASM_URL__'),
78
- ], documentBaseUrl);
79
- const rendererWasmUrl = resolveFileViewerTypstRendererWasmUrl(typstOptions, [
80
- getWindowOverride('__FLYFISH_TYPST_RENDERER_WASM_URL__'),
81
- ], documentBaseUrl);
73
+ const configureTypstEngine = (compilerWasmUrl, rendererWasmUrl) => {
82
74
  const configKey = `${compilerWasmUrl}\n${rendererWasmUrl}`;
83
75
  if (typstEngineConfigKey === configKey) {
84
76
  return;
@@ -91,6 +83,69 @@ const configureTypstEngine = (context, documentBaseUrl) => {
91
83
  });
92
84
  typstEngineConfigKey = configKey;
93
85
  };
86
+ const pushUniqueTypstCandidate = (candidates, candidate) => {
87
+ if (candidates.some(item => item.compilerWasmUrl === candidate.compilerWasmUrl &&
88
+ item.rendererWasmUrl === candidate.rendererWasmUrl)) {
89
+ return;
90
+ }
91
+ candidates.push(candidate);
92
+ };
93
+ const resolveTypstEngineCandidates = (context, documentBaseUrl) => {
94
+ var _a;
95
+ const typstOptions = (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.typst;
96
+ const compilerOverride = getWindowOverride('__FLYFISH_TYPST_COMPILER_WASM_URL__');
97
+ const rendererOverride = getWindowOverride('__FLYFISH_TYPST_RENDERER_WASM_URL__');
98
+ const compilerWasmUrl = resolveFileViewerTypstCompilerWasmUrl(typstOptions, [
99
+ compilerOverride,
100
+ ], documentBaseUrl);
101
+ const rendererWasmUrl = resolveFileViewerTypstRendererWasmUrl(typstOptions, [
102
+ rendererOverride,
103
+ ], documentBaseUrl);
104
+ const hasConfiguredAsset = Boolean((typstOptions === null || typstOptions === void 0 ? void 0 : typstOptions.compilerWasmUrl) ||
105
+ (typstOptions === null || typstOptions === void 0 ? void 0 : typstOptions.rendererWasmUrl) ||
106
+ compilerOverride ||
107
+ rendererOverride);
108
+ const candidates = [];
109
+ pushUniqueTypstCandidate(candidates, {
110
+ compilerWasmUrl,
111
+ rendererWasmUrl,
112
+ source: hasConfiguredAsset ? 'configured' : 'local',
113
+ preflight: !hasConfiguredAsset,
114
+ });
115
+ // Static hosts such as Cloudflare Pages reject files larger than 25 MiB.
116
+ // Keep the package self-contained, but let hosted demos fall back gracefully
117
+ // when the local compiler WASM was intentionally omitted from deployment.
118
+ pushUniqueTypstCandidate(candidates, {
119
+ compilerWasmUrl: FALLBACK_FILE_VIEWER_TYPST_COMPILER_WASM_URL,
120
+ rendererWasmUrl: FALLBACK_FILE_VIEWER_TYPST_RENDERER_WASM_URL,
121
+ source: 'cdn',
122
+ preflight: false,
123
+ });
124
+ return candidates;
125
+ };
126
+ const isHttpUrl = (url) => /^https?:\/\//i.test(url);
127
+ const isKnownMissingWasmUrl = async (url) => {
128
+ if (typeof fetch !== 'function' || !isHttpUrl(url)) {
129
+ return false;
130
+ }
131
+ try {
132
+ const response = await fetch(url, {
133
+ cache: 'force-cache',
134
+ method: 'HEAD',
135
+ });
136
+ return response.status === 404 || response.status === 410;
137
+ }
138
+ catch {
139
+ return false;
140
+ }
141
+ };
142
+ const isTypstAssetLoadError = (error) => {
143
+ if (Array.isArray(error)) {
144
+ return false;
145
+ }
146
+ const message = error instanceof Error ? error.message : String(error);
147
+ return /wasm|webassembly|fetch|module|instantiate|compile|network|404|410/i.test(message);
148
+ };
94
149
  const escapeAttribute = (value) => {
95
150
  return value.replace(/[&<>"']/g, char => {
96
151
  const entities = {
@@ -426,8 +481,39 @@ export default async function renderTypst(buffer, target, _type, context) {
426
481
  renderPages();
427
482
  }
428
483
  };
484
+ const renderTypstSvg = async () => {
485
+ var _a, _b;
486
+ const candidates = resolveTypstEngineCandidates(context, documentRef.baseURI);
487
+ const timeoutMs = normalizeRenderTimeoutMs((_b = (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.typst) === null || _b === void 0 ? void 0 : _b.renderTimeoutMs);
488
+ let lastError;
489
+ for (const candidate of candidates) {
490
+ if (candidate.preflight && await isKnownMissingWasmUrl(candidate.compilerWasmUrl)) {
491
+ lastError = new Error(`Typst compiler WASM missing: ${candidate.compilerWasmUrl}`);
492
+ continue;
493
+ }
494
+ try {
495
+ configureTypstEngine(candidate.compilerWasmUrl, candidate.rendererWasmUrl);
496
+ return await withRenderTimeout($typst.svg({
497
+ mainContent: source,
498
+ data_selection: {
499
+ body: true,
500
+ defs: true,
501
+ css: true,
502
+ js: false,
503
+ },
504
+ }), timeoutMs);
505
+ }
506
+ catch (error) {
507
+ lastError = error;
508
+ if (error instanceof TypstRenderTimeoutError || !isTypstAssetLoadError(error)) {
509
+ throw error;
510
+ }
511
+ }
512
+ }
513
+ throw lastError instanceof Error ? lastError : new Error('Typst WASM 加载失败');
514
+ };
429
515
  const render = async () => {
430
- var _a, _b, _c, _d, _e;
516
+ var _a, _b, _c;
431
517
  const token = ++renderToken;
432
518
  state = 'loading';
433
519
  errorMessage = '';
@@ -436,16 +522,7 @@ export default async function renderTypst(buffer, target, _type, context) {
436
522
  (_a = context === null || context === void 0 ? void 0 : context.registerExportAdapter) === null || _a === void 0 ? void 0 : _a.call(context, null);
437
523
  syncUi();
438
524
  try {
439
- configureTypstEngine(context, documentRef.baseURI);
440
- const svg = await withRenderTimeout($typst.svg({
441
- mainContent: source,
442
- data_selection: {
443
- body: true,
444
- defs: true,
445
- css: true,
446
- js: false,
447
- },
448
- }), normalizeRenderTimeoutMs((_c = (_b = context === null || context === void 0 ? void 0 : context.options) === null || _b === void 0 ? void 0 : _b.typst) === null || _c === void 0 ? void 0 : _c.renderTimeoutMs));
525
+ const svg = await renderTypstSvg();
449
526
  if (disposed || token !== renderToken) {
450
527
  return;
451
528
  }
@@ -453,7 +530,7 @@ export default async function renderTypst(buffer, target, _type, context) {
453
530
  state = 'ready';
454
531
  syncUi();
455
532
  registerExportAdapter();
456
- (_d = context === null || context === void 0 ? void 0 : context.onProgressiveRender) === null || _d === void 0 ? void 0 : _d.call(context);
533
+ (_b = context === null || context === void 0 ? void 0 : context.onProgressiveRender) === null || _b === void 0 ? void 0 : _b.call(context);
457
534
  }
458
535
  catch (error) {
459
536
  if (disposed || token !== renderToken) {
@@ -463,7 +540,7 @@ export default async function renderTypst(buffer, target, _type, context) {
463
540
  sourceFallbackMessage = error.message;
464
541
  state = 'source';
465
542
  syncUi();
466
- (_e = context === null || context === void 0 ? void 0 : context.onProgressiveRender) === null || _e === void 0 ? void 0 : _e.call(context);
543
+ (_c = context === null || context === void 0 ? void 0 : context.onProgressiveRender) === null || _c === void 0 ? void 0 : _c.call(context);
467
544
  return;
468
545
  }
469
546
  errorMessage = formatTypstError(error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@file-viewer/core",
3
- "version": "2.0.7",
3
+ "version": "2.0.8",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Framework-neutral TypeScript core for Flyfish File Viewer",
@@ -58,7 +58,7 @@
58
58
  "LICENSE"
59
59
  ],
60
60
  "dependencies": {
61
- "@file-viewer/pptx": "^2.0.7",
61
+ "@file-viewer/pptx": "^2.0.8",
62
62
  "@flyfish-dev/cad-viewer": "^0.6.4",
63
63
  "@excalidraw/excalidraw": "^0.18.1",
64
64
  "@kenjiuno/msgreader": "^1.28.0",