From 0210f8e8197f18da607daa8b166cb3aabac6d752 Mon Sep 17 00:00:00 2001 From: signadou <39036502+signadou@users.noreply.github.com> Date: Tue, 10 Mar 2026 18:45:39 -0600 Subject: [PATCH] Handle WASMagic import errors --- src/installationDetection.ts | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/installationDetection.ts b/src/installationDetection.ts index 1e1da824..ef21a43e 100644 --- a/src/installationDetection.ts +++ b/src/installationDetection.ts @@ -1,7 +1,6 @@ import path from 'path'; import fs from 'node:fs/promises'; import which from 'which'; -import { WASMagic } from 'wasmagic'; import { debug, @@ -49,22 +48,37 @@ export class InstallationDetectionError extends Error { // WASMagic singleton (with graceful fallback for SIMD-unsupported systems) // ============================================================================ -let magicInstancePromise: Promise | null = null; +// WASMagic detection interface — matches the subset of the WASMagic API we use. +interface MagicDetector { + detect(buf: Buffer): string | null; +} + +let magicInstancePromise: Promise | null = null; /** * Gets the WASMagic instance, or null if it fails to initialize. * This can happen on older CPUs that don't support WebAssembly SIMD (requires SSE 4.1+). + * + * Uses dynamic import() so that the WASM module is never loaded at the top level. + * The wasmagic abort() function throws synchronously during WASM compilation, + * which would crash the process if loaded via a static import. Dynamic import + * ensures the crash is contained within the promise chain. */ -async function getMagicInstance(): Promise { +async function getMagicInstance(): Promise { if (!magicInstancePromise) { - magicInstancePromise = WASMagic.create().catch(error => { - debug( - 'WASMagic initialization failed (likely SIMD unsupported on this CPU):', - error - ); - debug('Using fallback file type detection'); - return null; - }); + magicInstancePromise = (async () => { + try { + const { WASMagic } = await import('wasmagic'); + return await WASMagic.create(); + } catch (error) { + debug( + 'WASMagic initialization failed (likely SIMD unsupported on this CPU):', + error + ); + debug('Using fallback file type detection'); + return null; + } + })(); } return magicInstancePromise; }