Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(studio-server): the server loads where sharp's native binary is missing (#4654)
Miguel Ángel498643bea46b
diff --git a/packages/studio-server/src/routes/imageThumbnail.ts b/packages/studio-server/src/routes/imageThumbnail.tsindex 10de006c42..efafab5eca 100644--- a/packages/studio-server/src/routes/imageThumbnail.ts+++ b/packages/studio-server/src/routes/imageThumbnail.ts@@ -2,7 +2,6 @@ import type { Hono } from "hono";import { HTTPException } from "hono/http-exception";import { open, stat } from "node:fs/promises";import { createHash } from "node:crypto";-import sharp from "sharp";import type { StudioApiAdapter } from "../types.js";import { pinWithinProject } from "../helpers/safePath.js";import { requestSubPath } from "../helpers/requestSubPath.js";@@ -11,7 +10,20 @@ import { ThumbnailGenerationCoordinator } from "./thumbnailGenerationCoordinatorconst MAX_SOURCE_BYTES = 64 * 1024 * 1024;const MAX_CACHE_BYTES = 8 * 1024 * 1024;+let sharpLoad: Promise<typeof import("sharp").default> | undefined;+function loadSharp() {+ sharpLoad ??= import("sharp").then(+ (module) => module.default,+ (error: unknown) => {+ console.warn("[Studio] JPEG thumbnails are off: sharp could not load:", error);+ throw error;+ },+ );+ return sharpLoad;+}+async function generateThumbnail(path: string, signal: AbortSignal): Promise<Buffer> {+ const sharp = await loadSharp();const file = await open(path, "r");try {const info = await file.stat();diff --git a/packages/studio-server/src/routes/imageThumbnail.withoutSharp.test.ts b/packages/studio-server/src/routes/imageThumbnail.withoutSharp.test.tsnew file mode 100644index 0000000000..02bd226295--- /dev/null+++ b/packages/studio-server/src/routes/imageThumbnail.withoutSharp.test.ts@@ -0,0 +1,43 @@+import { afterEach, describe, expect, it, vi } from "vitest";+import { Hono } from "hono";+import { mkdtemp, rm, writeFile } from "node:fs/promises";+import { tmpdir } from "node:os";+import { join } from "node:path";+import type { StudioApiAdapter } from "../types";++vi.mock("sharp", () => {+ throw new Error("sharp's native binary is missing for this platform");+});++let dir: string | undefined;+afterEach(async () => {+ if (dir) await rm(dir, { recursive: true, force: true });+ vi.restoreAllMocks();+});++describe("studio-server where sharp cannot load", () => {+ it("still loads the package", async () => {+ await expect(import("../index.js")).resolves.toHaveProperty("createStudioApi");+ });++ it("answers a real JPEG with 422 and warns once", async () => {+ const { default: realSharp } = await vi.importActual<typeof import("sharp")>("sharp");+ const jpeg = await realSharp({+ create: { width: 64, height: 48, channels: 3, background: "#6495ed" },+ })+ .jpeg()+ .toBuffer();+ const { registerImageThumbnailRoutes } = await import("./imageThumbnail.js");+ dir = await mkdtemp(join(tmpdir(), "hf-image-no-sharp-"));+ await writeFile(join(dir, "a.jpg"), jpeg);+ await writeFile(join(dir, "b.jpg"), jpeg);+ const adapter = { resolveProject: (id: string) => (id === "p" ? { id, dir } : null) };+ const app = new Hono();+ registerImageThumbnailRoutes(app, adapter as unknown as StudioApiAdapter);+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});+ const url = (name: string) => `http://localhost/projects/p/image-thumbnail/${name}`;+ expect((await app.request(url("a.jpg"))).status).toBe(422);+ expect((await app.request(url("b.jpg"))).status).toBe(422);+ expect(warn).toHaveBeenCalledTimes(1);+ });+});