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

@@ -30,6 +30,7 @@
"@lib/": "./lib/",
"@/": "./",
"@libsql/client": "npm:@libsql/client@^0.17.0",
"@libsql/linux-x64-gnu": "npm:@libsql/linux-x64-gnu@^0.5.22",
"@openai/openai": "jsr:@openai/openai@^6.16.0",
"@preact-icons/tb": "jsr:@preact-icons/tb@^1.0.14",
"@std/http": "jsr:@std/http@^1.0.23",
@@ -88,13 +89,11 @@
},
"allowScripts": {
"allow": [
"npm:sharp@0.34.5"
],
"deny": [
"npm:sharp@0.34.5",
"npm:esbuild@0.27.2",
"npm:esbuild@0.18.20",
"npm:esbuild@0.25.12",
"npm:esbuild@0.25.7",
"npm:esbuild@0.27.2"
"npm:esbuild@0.25.7"
]
}
}

2
deno.lock generated
View File

@@ -44,6 +44,7 @@
"npm:@babel/core@^7.28.0": "7.28.5",
"npm:@babel/preset-react@^7.27.1": "7.28.5_@babel+core@7.28.5",
"npm:@libsql/client@0.17": "0.17.0",
"npm:@libsql/linux-x64-gnu@~0.5.22": "0.5.22",
"npm:@mjackson/node-fetch-server@0.7": "0.7.0",
"npm:@opentelemetry/api@^1.9.0": "1.9.0",
"npm:@preact/signals@^2.2.1": "2.5.1_preact@10.28.2",
@@ -2887,6 +2888,7 @@
"jsr:@std/media-types@^1.1.0",
"jsr:@zaubrik/djwt@^3.0.2",
"npm:@libsql/client@0.17",
"npm:@libsql/linux-x64-gnu@~0.5.22",
"npm:@preact/signals@^2.5.0",
"npm:@tailwindcss/vite@^4.1.12",
"npm:defuddle@~0.6.6",

View File

@@ -1,20 +1,22 @@
CREATE TABLE `performance` (
`path` text NOT NULL,
`search` text,
`time` integer NOT NULL,
`created_at` integer DEFAULT (current_timestamp)
`path` text NOT NULL,
`search` text,
`time` integer NOT NULL,
`created_at` integer DEFAULT (CURRENT_TIMESTAMP)
);
--> statement-breakpoint
CREATE TABLE `session` (
`id` text PRIMARY KEY NOT NULL,
`created_at` integer DEFAULT (current_timestamp),
`expires_at` integer NOT NULL,
`user_id` text NOT NULL
`id` text PRIMARY KEY NOT NULL,
`created_at` integer DEFAULT (CURRENT_TIMESTAMP),
`expires_at` integer NOT NULL,
`user_id` text NOT NULL
);
--> statement-breakpoint
CREATE TABLE `user` (
`id` text PRIMARY KEY NOT NULL,
`created_at` integer DEFAULT (current_timestamp) NOT NULL,
`email` text NOT NULL,
`name` text NOT NULL
`id` text PRIMARY KEY NOT NULL,
`created_at` integer DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
`email` text NOT NULL,
`name` text NOT NULL
);

View File

@@ -1 +1,4 @@
ALTER TABLE `performance` ALTER COLUMN "created_at" TO "created_at" integer DEFAULT (STRFTIME('%s', 'now') * 1000);
ALTER TABLE
`performance`
ALTER COLUMN
"created_at" TO "created_at" integer DEFAULT (STRFTIME('%s', 'now') * 1000);

View File

@@ -1,7 +1,7 @@
CREATE TABLE `image` (
`created_at` integer DEFAULT (current_timestamp),
`url` text NOT NULL,
`average` text NOT NULL,
`blurhash` text NOT NULL,
`mime` text NOT NULL
`created_at` integer DEFAULT (CURRENT_TIMESTAMP),
`url` text NOT NULL,
`average` text NOT NULL,
`blurhash` text NOT NULL,
`mime` text NOT NULL
);

View File

@@ -1,7 +1,7 @@
CREATE TABLE `document` (
`name` text NOT NULL,
`last_modified` integer NOT NULL,
`contentType` text NOT NULL,
`size` integer NOT NULL,
`perm` text NOT NULL
`name` text NOT NULL,
`last_modified` integer NOT NULL,
`contentType` text NOT NULL,
`size` integer NOT NULL,
`perm` text NOT NULL
);

View File

@@ -1,13 +1,38 @@
PRAGMA foreign_keys=OFF;--> statement-breakpoint
CREATE TABLE `__new_document` (
`name` text PRIMARY KEY NOT NULL,
`last_modified` integer NOT NULL,
`contentType` text NOT NULL,
`size` integer NOT NULL,
`perm` text NOT NULL
);
PRAGMA foreign_keys = OFF;
--> statement-breakpoint
INSERT INTO `__new_document`("name", "last_modified", "contentType", "size", "perm") SELECT "name", "last_modified", "contentType", "size", "perm" FROM `document`;--> statement-breakpoint
DROP TABLE `document`;--> statement-breakpoint
ALTER TABLE `__new_document` RENAME TO `document`;--> statement-breakpoint
PRAGMA foreign_keys=ON;
CREATE TABLE `__new_document` (
`name` text PRIMARY KEY NOT NULL,
`last_modified` integer NOT NULL,
`contentType` text NOT NULL,
`size` integer NOT NULL,
`perm` text NOT NULL
);
--> statement-breakpoint
INSERT INTO
`__new_document`(
"name",
"last_modified",
"contentType",
"size",
"perm"
)
SELECT
"name",
"last_modified",
"contentType",
"size",
"perm"
FROM
`document`;
--> statement-breakpoint
DROP TABLE `document`;
--> statement-breakpoint
ALTER TABLE
`__new_document` RENAME TO `document`;
--> statement-breakpoint
PRAGMA foreign_keys = ON;

View File

@@ -1 +1,2 @@
ALTER TABLE `document` RENAME COLUMN "contentType" TO "content_type";
ALTER TABLE
`document` RENAME COLUMN "contentType" TO "content_type";

View File

@@ -1 +1,4 @@
ALTER TABLE `document` ADD `content` text;
ALTER TABLE
`document`
ADD
`content` text;

View File

@@ -1 +1,4 @@
ALTER TABLE `document` ALTER COLUMN "content" TO "content" text NOT NULL;
ALTER TABLE
`document`
ALTER COLUMN
"content" TO "content" text NOT NULL;

View File

@@ -1 +1,4 @@
ALTER TABLE `document` ALTER COLUMN "content" TO "content" text;
ALTER TABLE
`document`
ALTER COLUMN
"content" TO "content" text;

View File

@@ -1,12 +1,17 @@
CREATE TABLE `cache` (
`scope` text NOT NULL,
`key` text PRIMARY KEY NOT NULL,
`json` text,
`binary` blob,
`created_at` integer DEFAULT (current_timestamp),
`expires_at` integer
`scope` text NOT NULL,
`key` text PRIMARY KEY NOT NULL,
`json` text,
`binary` blob,
`created_at` integer DEFAULT (CURRENT_TIMESTAMP),
`expires_at` integer
);
--> statement-breakpoint
CREATE INDEX `key_idx` ON `cache` (`key`);--> statement-breakpoint
CREATE INDEX `scope_idx` ON `cache` (`scope`);--> statement-breakpoint
CREATE INDEX `name_idx` ON `document` (`name`);
CREATE INDEX `key_idx` ON `cache` (`key`);
--> statement-breakpoint
CREATE INDEX `scope_idx` ON `cache` (`scope`);
--> statement-breakpoint
CREATE INDEX `name_idx` ON `document` (`name`);

View File

@@ -1 +1,2 @@
ALTER TABLE `image` RENAME COLUMN "blurhash" TO "thumbhash";
ALTER TABLE
`image` RENAME COLUMN "blurhash" TO "thumbhash";

View File

@@ -1,7 +1,22 @@
DROP INDEX "key_idx";--> statement-breakpoint
DROP INDEX "scope_idx";--> statement-breakpoint
DROP INDEX "name_idx";--> statement-breakpoint
ALTER TABLE `image` ALTER COLUMN "created_at" TO "created_at" integer DEFAULT (unixepoch());--> statement-breakpoint
CREATE INDEX `key_idx` ON `cache` (`key`);--> statement-breakpoint
CREATE INDEX `scope_idx` ON `cache` (`scope`);--> statement-breakpoint
DROP INDEX "key_idx";
--> statement-breakpoint
DROP INDEX "scope_idx";
--> statement-breakpoint
DROP INDEX "name_idx";
--> statement-breakpoint
ALTER TABLE
`image`
ALTER COLUMN
"created_at" TO "created_at" integer DEFAULT (unixepoch());
--> statement-breakpoint
CREATE INDEX `key_idx` ON `cache` (`key`);
--> statement-breakpoint
CREATE INDEX `scope_idx` ON `cache` (`scope`);
--> statement-breakpoint
CREATE INDEX `name_idx` ON `document` (`name`);

View File

@@ -306,4 +306,4 @@
"internal": {
"indexes": {}
}
}
}

View File

@@ -87,4 +87,4 @@
"breakpoints": true
}
]
}
}

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);