reboot alterflixhub
This commit is contained in:
commit
1e84023a44
188 files changed
+31629
No files matched your search
@@ -0,0 +1,80 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import { defineEventHandler, getHeader, getRequestIP, readBody } from "h3";
|
||||
|
||||
import { appendEvent } from "@/lib/analytics.server";
|
||||
import type { AnalyticsEvent, AnalyticsEventType } from "@/lib/analytics-types";
|
||||
|
||||
const ALLOWED_TYPES: AnalyticsEventType[] = [
|
||||
"search",
|
||||
"cinema_select",
|
||||
"cinema_view",
|
||||
"near_me",
|
||||
];
|
||||
|
||||
const MAX_EVENTS_PER_REQUEST = 20;
|
||||
const MAX_TEXT = 120;
|
||||
|
||||
function text(value: unknown): string | undefined {
|
||||
if (typeof value !== "string") return undefined;
|
||||
const trimmed = value.trim().slice(0, MAX_TEXT);
|
||||
return trimmed === "" ? undefined : trimmed;
|
||||
}
|
||||
|
||||
function count(value: unknown): number | undefined {
|
||||
const parsed = Number(value);
|
||||
return Number.isFinite(parsed) && parsed >= 0 ? Math.floor(parsed) : undefined;
|
||||
}
|
||||
|
||||
/** เกลือหมุนรายวัน — hash ของผู้เข้าชมจึงย้อนระบุตัวตนไม่ได้ */
|
||||
function visitorHash(ip: string, userAgent: string): string {
|
||||
const salt = `${new Date().toISOString().slice(0, 10)}${
|
||||
process.env.ANALYTICS_SALT ?? "alterflixhub-analytics"
|
||||
}`;
|
||||
return createHash("sha256").update(`${salt}|${ip}|${userAgent}`).digest("hex").slice(0, 16);
|
||||
}
|
||||
|
||||
/**
|
||||
* ตัวรับ event ฝั่งเว็บ (เทียบเท่า /api/send ของ Umami)
|
||||
* ไม่เก็บคุกกี้ ไม่เก็บ IP ดิบ — เก็บเฉพาะ hash ที่หมุนเกลือทุกวัน
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = (await readBody(event)) as
|
||||
| { events?: unknown }
|
||||
| Record<string, unknown>;
|
||||
const rawEvents = Array.isArray(body)
|
||||
? body
|
||||
: Array.isArray((body as { events?: unknown })?.events)
|
||||
? ((body as { events: unknown[] }).events)
|
||||
: [body];
|
||||
|
||||
const ip = getRequestIP(event, { xForwardedFor: true }) ?? "0.0.0.0";
|
||||
const userAgent = getHeader(event, "user-agent") ?? "";
|
||||
const visitor = visitorHash(ip, userAgent);
|
||||
const at = new Date().toISOString();
|
||||
|
||||
const accepted: AnalyticsEvent[] = [];
|
||||
for (const raw of rawEvents.slice(0, MAX_EVENTS_PER_REQUEST)) {
|
||||
if (!raw || typeof raw !== "object") continue;
|
||||
const entry = raw as Record<string, unknown>;
|
||||
const type = entry.type;
|
||||
if (typeof type !== "string" || !ALLOWED_TYPES.includes(type as AnalyticsEventType)) {
|
||||
continue;
|
||||
}
|
||||
accepted.push({
|
||||
type: type as AnalyticsEventType,
|
||||
at,
|
||||
visitor,
|
||||
locale: text(entry.locale) ?? "th",
|
||||
query: text(entry.query),
|
||||
category: text(entry.category),
|
||||
result_count: count(entry.result_count),
|
||||
slug: text(entry.slug),
|
||||
source: text(entry.source),
|
||||
position: count(entry.position),
|
||||
});
|
||||
}
|
||||
|
||||
for (const item of accepted) await appendEvent(item);
|
||||
return { ok: true, stored: accepted.length };
|
||||
});
|
||||
Reference in new issue
Block a user