mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-18 03:00:38 +00:00
fix(coding-agent): use lazy-loaded photon wrapper for Node.js compatibility
Previous commit broke Node.js/tsx by using the ESM entry point which doesn't work with Node. This creates a wrapper module that: - Uses require() for lazy loading (works in both Node and Bun) - Gracefully handles load failures (returns original image) - Works in Node.js, tsx, and Bun compiled binaries
This commit is contained in:
parent
5aa0689828
commit
75628e0cfd
5 changed files with 86 additions and 16 deletions
59
packages/coding-agent/src/utils/photon.ts
Normal file
59
packages/coding-agent/src/utils/photon.ts
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
/**
|
||||
* 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 and gracefully handle failures. Image processing functions
|
||||
* already 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 loadAttempted = false;
|
||||
let loadError: Error | null = null;
|
||||
|
||||
/**
|
||||
* Get the photon module, loading it lazily on first access.
|
||||
* Returns null if loading fails (e.g., in broken Bun binary).
|
||||
*/
|
||||
export function getPhoton(): typeof import("@silvia-odwyer/photon-node") | null {
|
||||
if (loadAttempted) {
|
||||
return photonModule;
|
||||
}
|
||||
|
||||
loadAttempted = true;
|
||||
|
||||
try {
|
||||
// Dynamic require to defer loading until actually needed
|
||||
// This also allows the error to be caught gracefully
|
||||
photonModule = require("@silvia-odwyer/photon-node");
|
||||
} catch (e) {
|
||||
loadError = e as Error;
|
||||
photonModule = null;
|
||||
}
|
||||
|
||||
return photonModule;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if photon is available and working.
|
||||
*/
|
||||
export function isPhotonAvailable(): boolean {
|
||||
return getPhoton() !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the error that occurred during photon loading, if any.
|
||||
*/
|
||||
export function getPhotonLoadError(): Error | null {
|
||||
getPhoton(); // Ensure load was attempted
|
||||
return loadError;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue