co-mono/packages/coding-agent/src/utils/photon.ts
2026-01-17 21:32:23 +01:00

45 lines
1.4 KiB
TypeScript

/**
* Photon image processing wrapper.
*
* This module provides a unified interface to @silvia-odwyer/photon-node that works in:
* 1. Node.js (development, npm run build)
* 2. Bun compiled binaries (standalone distribution)
*
* The challenge: photon-node's CJS entry uses fs.readFileSync(__dirname + '/photon_rs_bg.wasm')
* which bakes the build machine's absolute path into Bun compiled binaries.
*
* Solution: Lazy-load photon via dynamic import and gracefully handle failures.
* Image processing functions have fallbacks that return original images when photon isn't available.
*/
// Re-export types from the main package
export type { PhotonImage as PhotonImageType } from "@silvia-odwyer/photon-node";
// Lazy-loaded photon module
let photonModule: typeof import("@silvia-odwyer/photon-node") | null = null;
let loadPromise: Promise<typeof import("@silvia-odwyer/photon-node") | null> | null = null;
/**
* Load the photon module asynchronously.
* Returns cached module on subsequent calls.
*/
export async function loadPhoton(): Promise<typeof import("@silvia-odwyer/photon-node") | null> {
if (photonModule) {
return photonModule;
}
if (loadPromise) {
return loadPromise;
}
loadPromise = (async () => {
try {
photonModule = await import("@silvia-odwyer/photon-node");
} catch {
photonModule = null;
}
return photonModule;
})();
return loadPromise;
}