mirror of
https://github.com/earendil-works/pi.git
synced 2026-06-18 15:54:04 +08:00
5aa0689828
- Import photon-node from ESM entry point (photon_rs_bg.js) instead of CJS entry, allowing Bun to embed WASM in compiled binaries - Add photon.d.ts for TypeScript support of ESM entry - Add scripts/build-binaries.sh for local binary builds - Simplify GitHub workflow to use the build script - Add binaries/ to gitignore
34 lines
927 B
TypeScript
34 lines
927 B
TypeScript
// Use ESM entry point so Bun can embed the WASM in compiled binaries
|
|
// (the CJS entry uses fs.readFileSync which breaks in standalone binaries)
|
|
import { PhotonImage } from "@silvia-odwyer/photon-node/photon_rs_bg.js";
|
|
|
|
/**
|
|
* Convert image to PNG format for terminal display.
|
|
* Kitty graphics protocol requires PNG format (f=100).
|
|
*/
|
|
export async function convertToPng(
|
|
base64Data: string,
|
|
mimeType: string,
|
|
): Promise<{ data: string; mimeType: string } | null> {
|
|
// Already PNG, no conversion needed
|
|
if (mimeType === "image/png") {
|
|
return { data: base64Data, mimeType };
|
|
}
|
|
|
|
try {
|
|
const image = PhotonImage.new_from_byteslice(new Uint8Array(Buffer.from(base64Data, "base64")));
|
|
try {
|
|
const pngBuffer = image.get_bytes();
|
|
return {
|
|
data: Buffer.from(pngBuffer).toString("base64"),
|
|
mimeType: "image/png",
|
|
};
|
|
} finally {
|
|
image.free();
|
|
}
|
|
} catch {
|
|
// Conversion failed
|
|
return null;
|
|
}
|
|
}
|