mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-10-30 17:27:09 +00:00
fix(studio-server): the server loads where sharp's native binary is missing (#4654)
* fix(studio-server): the server loads where sharp's native binary is missing * fix(studio-server): a missing sharp warns once and is checked before the image is read
This commit is contained in:
@@ -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 "./thumbnailGenerationCoordinator
|
||||
const 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();
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user