fix: lazily import sharp to fix commonjs error

This commit is contained in:
Max Richter
2026-01-10 20:13:15 +01:00
parent c74a19b527
commit 47d32d68c7
18 changed files with 145 additions and 68 deletions

View File

@@ -12,7 +12,9 @@ export const userTable = sqliteTable("user", {
id: text()
.primaryKey(),
createdAt: integer("created_at", { mode: "timestamp" })
.default(sql`(current_timestamp)`)
.default(sql`
(CURRENT_TIMESTAMP)
`)
.notNull(),
email: text()
.notNull(),
@@ -24,7 +26,9 @@ export const sessionTable = sqliteTable("session", {
id: text("id")
.primaryKey(),
createdAt: integer("created_at", { mode: "timestamp_ms" }).default(
sql`(current_timestamp)`,
sql`
(CURRENT_TIMESTAMP)
`,
),
expiresAt: integer("expires_at", { mode: "timestamp" })
.notNull(),
@@ -38,12 +42,16 @@ export const performanceTable = sqliteTable("performance", {
time: int().notNull(),
createdAt: integer("created_at", {
mode: "timestamp_ms",
}).default(sql`(STRFTIME('%s', 'now') * 1000)`),
}).default(sql`
(STRFTIME('%s', 'now') * 1000)
`),
});
export const imageTable = sqliteTable("image", {
createdAt: integer("created_at", { mode: "timestamp" }).default(
sql`(unixepoch())`,
sql`
(unixepoch())
`,
),
url: text().notNull(),
average: text().notNull(),
@@ -70,7 +78,9 @@ export const cacheTable = sqliteTable("cache", {
json: text({ mode: "json" }),
binary: blob(),
createdAt: integer("created_at", { mode: "timestamp" }).default(
sql`(current_timestamp)`,
sql`
(CURRENT_TIMESTAMP)
`,
),
expiresAt: integer("expires_at", { mode: "timestamp" }),
}, (table) => {

View File

@@ -8,7 +8,6 @@ import { DATA_DIR } from "@lib/env.ts";
import { db } from "@lib/db/sqlite.ts";
import { imageTable } from "@lib/db/schema.ts";
import { eq } from "drizzle-orm";
import sharp from "sharp";
const log = createLogger("cache/image");
@@ -158,6 +157,8 @@ async function resizeImage(
mediaType: string;
},
) {
const sharp = (await import("sharp")).default;
try {
log.debug("Resizing image", { params });
@@ -211,6 +212,8 @@ async function resizeImage(
async function createThumbhash(
image: Uint8Array,
): Promise<{ hash: string; average: string }> {
const sharp = (await import("sharp")).default;
try {
const resizedImage = await sharp(image)
.resize(100, 100, { fit: "cover" }) // Keep aspect ratio within bounds
@@ -238,6 +241,8 @@ async function createThumbhash(
* Verifies that an image buffer contains valid image data
*/
async function verifyImage(imageBuffer: Uint8Array): Promise<boolean> {
const sharp = (await import("sharp")).default;
try {
const metadata = await sharp(imageBuffer).metadata();
return !!(metadata.width && metadata.height && metadata.format);