1f7c4f6f90
- Refactored SSE stream parsing to use generic `readSseJson` utility instead of domain-specific handlers across multiple packages. - Migrated from Node.js Buffer API to Web standard APIs (Uint8Array, TextDecoder, DataView) for cross-platform compatibility. - Simplified stream transformation pipeline by consolidating multiple stream operations into unified `createTextLineSplitter` utility. - Refactored `ptree.ts` to remove complex stream pumping infrastructure and simplify stderr handling with direct async iteration. - Rewrote stream utilities to operate at binary level with byte-level parsing for improved efficiency and reduced string allocations. - Updated TypeScript configuration to include DOM.AsyncIterable type definitions for async iterable DOM API support.
28 lines
732 B
TypeScript
28 lines
732 B
TypeScript
import { ImageFormat, PhotonImage } from "@oh-my-pi/pi-natives";
|
|
|
|
/**
|
|
* 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 = await PhotonImage.parse(new Uint8Array(Buffer.from(base64Data, "base64")));
|
|
const pngBuffer = await image.encode(ImageFormat.PNG, 100);
|
|
return {
|
|
data: Buffer.from(pngBuffer).toBase64(),
|
|
mimeType: "image/png",
|
|
};
|
|
} catch {
|
|
// Conversion failed
|
|
return null;
|
|
}
|
|
}
|