From 6708da54b1805756908dea84bf65fe73ab03426a Mon Sep 17 00:00:00 2001 From: amankumarsingh77 Date: Sat, 2 May 2026 16:20:09 +0530 Subject: feat(domains): migrate screenshot, add typed sharp shim, restore click crosshair overlay --- src/util/sharp-shim.ts | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/util/sharp-shim.ts (limited to 'src/util') diff --git a/src/util/sharp-shim.ts b/src/util/sharp-shim.ts new file mode 100644 index 0000000..7ea409d --- /dev/null +++ b/src/util/sharp-shim.ts @@ -0,0 +1,49 @@ +// Typed minimal shim for the optional `sharp` dependency. Sharp is a heavy +// native module not installed in every workspace; we import dynamically and +// distinguish "missing" (no install) from "error" (installed but threw). +// +// We declare only the surface we use. The dynamic import returns `unknown` +// at the boundary; the cast inside loadSharp() is the documented escape +// hatch — every other consumer sees the typed SharpFactory. + +export type SharpInstance = { + metadata(): Promise<{ width?: number; height?: number }>; + composite(layers: ReadonlyArray<{ input: Buffer; top: number; left: number }>): SharpInstance; + resize(width: number, height: number, opts?: { fit?: "inside" }): SharpInstance; + toFile(path: string): Promise; +}; + +export type SharpFactory = (input: string | Buffer) => SharpInstance; + +export type SharpLoad = + | { readonly kind: "ok"; readonly sharp: SharpFactory } + | { readonly kind: "missing" } + | { readonly kind: "error"; readonly message: string }; + +export const loadSharp = async (): Promise => { + let mod: unknown; + try { + // sharp is an optional peer dependency; it may not be installed. + // @ts-expect-error sharp is an optional peer dependency not required at compile time + mod = await import("sharp"); + } catch (e) { + const msg = e instanceof Error ? e.message : String(e); + if ( + msg.includes("Cannot find module") || + msg.includes("MODULE_NOT_FOUND") || + msg.includes("ERR_MODULE_NOT_FOUND") + ) { + return { kind: "missing" }; + } + return { kind: "error", message: msg }; + } + if (mod === null || mod === undefined) return { kind: "missing" }; + // The CommonJS sharp module's default export IS the factory. ESM-wrapped + // sharp puts it on .default. Probe both. + const m = mod as { default?: unknown }; + const candidate: unknown = typeof m.default === "function" ? m.default : mod; + if (typeof candidate !== "function") { + return { kind: "error", message: "sharp module did not expose a callable factory" }; + } + return { kind: "ok", sharp: candidate as SharpFactory }; +}; -- cgit v1.2.3