fix(browser-tools): make sharp an optional lazy dependency

sharp requires platform-specific native binaries and is unavailable
when running via bunx or on platforms like Raspberry Pi (ARM) where
the prebuilt binary may not exist.

The previous top-level static import caused the browser-tools extension
to crash at load time before any tool was ever called.

Replace the static import with a lazy getSharp() helper that catches
import failures and caches the result. constrainScreenshot returns the
raw buffer unchanged when sharp is unavailable — screenshots remain
functional, just without resizing.

The core bunx extension-loading fix (routing bunx through virtualModules
in loader.ts) belongs upstream in pi-mono and will be submitted there
once the OSS weekend freeze lifts on 2026-04-13.

Related: #3504

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Nils Reeh 2026-04-05 23:40:24 +02:00
parent d1b7f6f85c
commit 701eccb588

View file

@ -6,7 +6,22 @@
*/
import type { Frame, Page } from "playwright";
import sharp from "sharp";
// sharp is an optional native dependency. Load it lazily so that the extension
// can still be loaded on platforms where sharp is unavailable (e.g. bunx on
// Raspberry Pi). constrainScreenshot falls back to returning the raw buffer
// when sharp is not installed, which means screenshots won't be resized but
// the tool remains functional.
let _sharp: typeof import("sharp") | null | undefined;
async function getSharp(): Promise<typeof import("sharp") | null> {
if (_sharp !== undefined) return _sharp;
try {
_sharp = (await import("sharp")).default;
} catch {
_sharp = null;
}
return _sharp;
}
import type { CompactPageState, CompactSelectorState } from "./state.js";
import { formatCompactStateSummary } from "./utils.js";
@ -168,6 +183,9 @@ export async function constrainScreenshot(
mimeType: string,
quality: number,
): Promise<Buffer> {
const sharp = await getSharp();
if (!sharp) return buffer;
const meta = await sharp(buffer).metadata();
const width = meta.width;
const height = meta.height;