Refactor and enhance type safety across various modules
- Updated function parameters and return types in `page-preview.ts`, `pdf-decrypt.ts`, and `pymupdf-loader.ts` for improved type safety. - Introduced type definitions for `CpdfInstance`, `PyMuPDFInstance`, and other related types to ensure better type checking. - Enhanced error handling in `sanitize.ts` by creating a utility function for error messages. - Removed unnecessary type assertions and improved type inference in `editor.ts`, `serialization.ts`, and `tools.test.ts`. - Added type definitions for markdown-it plugins to improve compatibility and type safety. - Enforced stricter TypeScript settings by enabling `noImplicitAny` in `tsconfig.json`. - Cleaned up test files by refining type assertions and ensuring consistency in type usage.
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
import { WasmProvider } from './wasm-provider.js';
|
||||
import type {
|
||||
GlobalScopeWithGhostscript,
|
||||
GhostscriptDynamicInstance,
|
||||
} from '@/types';
|
||||
|
||||
let cachedGS: any = null;
|
||||
let loadPromise: Promise<any> | null = null;
|
||||
let cachedGS: GhostscriptInterface | null = null;
|
||||
let loadPromise: Promise<GhostscriptInterface> | null = null;
|
||||
|
||||
export interface GhostscriptInterface {
|
||||
convertToPDFA(pdfBuffer: ArrayBuffer, profile: string): Promise<ArrayBuffer>;
|
||||
@@ -32,16 +36,20 @@ export async function loadGhostscript(): Promise<GhostscriptInterface> {
|
||||
|
||||
await loadScript(wrapperUrl);
|
||||
|
||||
const globalScope =
|
||||
typeof globalThis !== 'undefined' ? globalThis : window;
|
||||
const globalScope = (
|
||||
typeof globalThis !== 'undefined' ? globalThis : window
|
||||
) as typeof globalThis & GlobalScopeWithGhostscript;
|
||||
|
||||
if (typeof (globalScope as any).loadGS === 'function') {
|
||||
cachedGS = await (globalScope as any).loadGS({
|
||||
if (typeof globalScope.loadGS === 'function') {
|
||||
const instance = await globalScope.loadGS({
|
||||
baseUrl: normalizedUrl,
|
||||
});
|
||||
} else if (typeof (globalScope as any).GhostscriptWASM === 'function') {
|
||||
cachedGS = new (globalScope as any).GhostscriptWASM(normalizedUrl);
|
||||
await cachedGS.init?.();
|
||||
cachedGS = instance as unknown as GhostscriptInterface;
|
||||
} else if (typeof globalScope.GhostscriptWASM === 'function') {
|
||||
const instance: GhostscriptDynamicInstance =
|
||||
new globalScope.GhostscriptWASM(normalizedUrl);
|
||||
await instance.init?.();
|
||||
cachedGS = instance as unknown as GhostscriptInterface;
|
||||
} else {
|
||||
throw new Error(
|
||||
'Ghostscript wrapper did not expose expected interface. Expected loadGS() or GhostscriptWASM class.'
|
||||
@@ -49,10 +57,11 @@ export async function loadGhostscript(): Promise<GhostscriptInterface> {
|
||||
}
|
||||
|
||||
return cachedGS;
|
||||
} catch (error: any) {
|
||||
} catch (error: unknown) {
|
||||
loadPromise = null;
|
||||
const msg = error instanceof Error ? error.message : String(error);
|
||||
throw new Error(
|
||||
`Failed to load Ghostscript from ${normalizedUrl}: ${error.message}`,
|
||||
`Failed to load Ghostscript from ${normalizedUrl}: ${msg}`,
|
||||
{ cause: error }
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user