refactor: centralize CDN config and fix service worker fallback
- Centralize CDN versions/URLs in cdn-version.ts - Switch LibreOffice to @bentopdf/libreoffice-wasm package - Fix service worker fallback to support all WASM packages - Remove dead code and silence production logs
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
import loadWASM from '@bentopdf/gs-wasm';
|
||||
import { getWasmBaseUrl, fetchWasmFile } from '../config/wasm-cdn-config.js';
|
||||
|
||||
interface GhostscriptModule {
|
||||
FS: {
|
||||
@@ -32,35 +33,35 @@ export function getCachedGsModule(): GhostscriptModule | null {
|
||||
* Encode binary data to Adobe ASCII85 (Base85) format.
|
||||
* This matches Python's base64.a85encode(data, adobe=True)
|
||||
*/
|
||||
function encodeBase85(data: Uint8Array): string {
|
||||
const POW85 = [85 * 85 * 85 * 85, 85 * 85 * 85, 85 * 85, 85, 1];
|
||||
let result = '';
|
||||
// function encodeBase85(data: Uint8Array): string {
|
||||
// const POW85 = [85 * 85 * 85 * 85, 85 * 85 * 85, 85 * 85, 85, 1];
|
||||
// let result = '';
|
||||
|
||||
// Process 4 bytes at a time
|
||||
for (let i = 0; i < data.length; i += 4) {
|
||||
// Get 4 bytes (pad with zeros if needed)
|
||||
let value = 0;
|
||||
const remaining = Math.min(4, data.length - i);
|
||||
for (let j = 0; j < 4; j++) {
|
||||
value = value * 256 + (j < remaining ? data[i + j] : 0);
|
||||
}
|
||||
// // Process 4 bytes at a time
|
||||
// for (let i = 0; i < data.length; i += 4) {
|
||||
// // Get 4 bytes (pad with zeros if needed)
|
||||
// let value = 0;
|
||||
// const remaining = Math.min(4, data.length - i);
|
||||
// for (let j = 0; j < 4; j++) {
|
||||
// value = value * 256 + (j < remaining ? data[i + j] : 0);
|
||||
// }
|
||||
|
||||
// Special case: all zeros become 'z'
|
||||
if (value === 0 && remaining === 4) {
|
||||
result += 'z';
|
||||
} else {
|
||||
// Encode to 5 ASCII85 characters
|
||||
const encoded: string[] = [];
|
||||
for (let j = 0; j < 5; j++) {
|
||||
encoded.push(String.fromCharCode((value / POW85[j]) % 85 + 33));
|
||||
}
|
||||
// For partial blocks, only output needed characters
|
||||
result += encoded.slice(0, remaining + 1).join('');
|
||||
}
|
||||
}
|
||||
// // Special case: all zeros become 'z'
|
||||
// if (value === 0 && remaining === 4) {
|
||||
// result += 'z';
|
||||
// } else {
|
||||
// // Encode to 5 ASCII85 characters
|
||||
// const encoded: string[] = [];
|
||||
// for (let j = 0; j < 5; j++) {
|
||||
// encoded.push(String.fromCharCode((value / POW85[j]) % 85 + 33));
|
||||
// }
|
||||
// // For partial blocks, only output needed characters
|
||||
// result += encoded.slice(0, remaining + 1).join('');
|
||||
// }
|
||||
// }
|
||||
|
||||
return result;
|
||||
}
|
||||
// return result;
|
||||
// }
|
||||
|
||||
export async function convertToPdfA(
|
||||
pdfData: Uint8Array,
|
||||
@@ -74,10 +75,11 @@ export async function convertToPdfA(
|
||||
if (cachedGsModule) {
|
||||
gs = cachedGsModule;
|
||||
} else {
|
||||
const gsBaseUrl = getWasmBaseUrl('ghostscript');
|
||||
gs = await loadWASM({
|
||||
locateFile: (path: string) => {
|
||||
if (path.endsWith('.wasm')) {
|
||||
return import.meta.env.BASE_URL + 'ghostscript-wasm/gs.wasm';
|
||||
return gsBaseUrl + 'gs.wasm';
|
||||
}
|
||||
return path;
|
||||
},
|
||||
@@ -104,9 +106,7 @@ export async function convertToPdfA(
|
||||
const pdfaDefPath = '/tmp/pdfa.ps';
|
||||
|
||||
try {
|
||||
const response = await fetch(import.meta.env.BASE_URL + 'ghostscript-wasm/sRGB_v4_ICC_preference.icc');
|
||||
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
|
||||
const response = await fetchWasmFile('ghostscript', 'sRGB_v4_ICC_preference.icc');
|
||||
const iccData = new Uint8Array(await response.arrayBuffer());
|
||||
console.log('[Ghostscript] sRGB v4 ICC profile loaded:', iccData.length, 'bytes');
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
*/
|
||||
|
||||
import { WorkerBrowserConverter } from '@matbee/libreoffice-converter/browser';
|
||||
import { getWasmBaseUrl } from '../config/wasm-cdn-config.js';
|
||||
|
||||
export interface LoadProgress {
|
||||
phase: 'loading' | 'initializing' | 'converting' | 'complete' | 'ready';
|
||||
@@ -24,8 +25,9 @@ export class LibreOfficeConverter {
|
||||
private initializing = false;
|
||||
private basePath: string;
|
||||
|
||||
constructor(basePath: string = import.meta.env.BASE_URL + 'libreoffice-wasm/') {
|
||||
this.basePath = basePath;
|
||||
constructor(basePath?: string) {
|
||||
// Use CDN if available, otherwise use provided basePath or default local path
|
||||
this.basePath = basePath || getWasmBaseUrl('libreoffice');
|
||||
}
|
||||
|
||||
async initialize(onProgress?: ProgressCallback): Promise<void> {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { getLibreOfficeConverter } from './libreoffice-loader.js';
|
||||
import { PyMuPDF } from '@bentopdf/pymupdf-wasm';
|
||||
import loadGsWASM from '@bentopdf/gs-wasm';
|
||||
import { setCachedGsModule } from './ghostscript-loader.js';
|
||||
import { getWasmBaseUrl } from '../config/wasm-cdn-config.js';
|
||||
|
||||
export enum PreloadStatus {
|
||||
IDLE = 'idle',
|
||||
@@ -56,7 +57,8 @@ async function preloadPyMuPDF(): Promise<void> {
|
||||
console.log('[Preloader] Starting PyMuPDF preload...');
|
||||
|
||||
try {
|
||||
pymupdfInstance = new PyMuPDF(import.meta.env.BASE_URL + 'pymupdf-wasm/');
|
||||
const pymupdfBaseUrl = getWasmBaseUrl('pymupdf');
|
||||
pymupdfInstance = new PyMuPDF(pymupdfBaseUrl);
|
||||
await pymupdfInstance.load();
|
||||
preloadState.pymupdf = PreloadStatus.READY;
|
||||
console.log('[Preloader] PyMuPDF ready');
|
||||
@@ -73,10 +75,11 @@ async function preloadGhostscript(): Promise<void> {
|
||||
console.log('[Preloader] Starting Ghostscript WASM preload...');
|
||||
|
||||
try {
|
||||
const gsBaseUrl = getWasmBaseUrl('ghostscript');
|
||||
const gsModule = await loadGsWASM({
|
||||
locateFile: (path: string) => {
|
||||
if (path.endsWith('.wasm')) {
|
||||
return import.meta.env.BASE_URL + 'ghostscript-wasm/gs.wasm';
|
||||
return gsBaseUrl + 'gs.wasm';
|
||||
}
|
||||
return path;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user