fix: resolve analytics data gaps and resize validation failures

- Fix resize 20% failure rate: add Zod refine requiring at least one
  dimension, enforce integer/max constraints, clamp percentage scaling
  to minimum 1px, and guard against missing metadata in withoutEnlargement
- Fix PostHog init race condition: move consent check before async import
  so frontend events (search, pageview) are no longer silently dropped
- Fix identify() passing nested $set/$set_once wrappers instead of flat
  properties, so version person property now appears on PostHog profiles
- Add error_code and error_message to failed tool_used analytics events
  for debugging tool failures from PostHog
This commit is contained in:
SnapOtter
2026-05-06 23:21:45 +08:00
parent c9b24ada78
commit 979a833978
9 changed files with 50 additions and 41 deletions
+10 -8
View File
@@ -8,10 +8,11 @@ export async function resize(image: Sharp, options: ResizeOptions): Promise<Shar
throw new Error("Resize percentage must be greater than 0");
}
const metadata = await image.metadata();
const currentWidth = metadata.width ?? 0;
const currentHeight = metadata.height ?? 0;
width = Math.round(currentWidth * (percentage / 100));
height = Math.round(currentHeight * (percentage / 100));
if (!metadata.width || !metadata.height) {
throw new Error("Cannot determine image dimensions for percentage resize");
}
width = Math.max(1, Math.round(metadata.width * (percentage / 100)));
height = Math.max(1, Math.round(metadata.height * (percentage / 100)));
}
if (width !== undefined && width <= 0) {
@@ -26,10 +27,11 @@ export async function resize(image: Sharp, options: ResizeOptions): Promise<Shar
if (withoutEnlargement) {
const meta = await image.metadata();
const curW = meta.width ?? 0;
const curH = meta.height ?? 0;
if (width !== undefined && width > curW) width = curW;
if (height !== undefined && height > curH) height = curH;
if (!meta.width || !meta.height) {
throw new Error("Cannot determine image dimensions for resize clamping");
}
if (width !== undefined && width > meta.width) width = meta.width;
if (height !== undefined && height > meta.height) height = meta.height;
}
return image.resize({