Resolving polling/server migration

This commit is contained in:
Marcelo
2026-01-15 18:43:21 +00:00
parent f231d87ae3
commit 9f1af71d15
15 changed files with 794 additions and 845 deletions

View File

@@ -6,11 +6,16 @@ let initialized = false;
let currentLocale: Locale = "en";
const listeners = new Set<() => void>();
const isBrowser = () => typeof document !== "undefined";
function readCookieLocale(): Locale | null {
if (!isBrowser()) return null;
const match = document.cookie
.split(";")
.map((part) => part.trim())
.find((part) => part.startsWith(`${LOCALE_COOKIE}=`));
if (!match) return null;
const value = match.split("=")[1];
if (value === "es-MX" || value === "en") return value;
@@ -18,10 +23,14 @@ function readCookieLocale(): Locale | null {
}
function readLocaleFromDocument(): Locale {
if (!isBrowser()) return "en";
const cookieLocale = readCookieLocale();
if (cookieLocale) return cookieLocale;
const docLang = document.documentElement.getAttribute("lang");
if (docLang === "es-MX" || docLang === "en") return docLang;
return "en";
}
@@ -32,20 +41,18 @@ function ensureInitialized() {
}
export function getLocaleSnapshot(): Locale {
if (typeof document === "undefined") return "en";
if (!isBrowser()) return "en";
ensureInitialized();
return currentLocale;
}
export function subscribeLocale(listener: () => void) {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
return () => listeners.delete(listener);
}
export function setLocale(next: Locale) {
if (typeof document !== "undefined") {
if (isBrowser()) {
document.documentElement.setAttribute("lang", next);
document.cookie = `${LOCALE_COOKIE}=${next}; Path=/; Max-Age=31536000; SameSite=Lax`;
}

38
lib/machineAuthCache.ts Normal file
View File

@@ -0,0 +1,38 @@
import { prisma } from "@/lib/prisma";
type MachineAuth = { id: string; orgId: string };
const TTL_MS = 60_000;
const MAX_SIZE = 1000;
const cache = new Map<string, { value: MachineAuth; expiresAt: number }>();
function makeKey(machineId: string, apiKey: string) {
return `${machineId}:${apiKey}`;
}
export async function getMachineAuth(machineId: string, apiKey: string) {
const key = makeKey(machineId, apiKey);
const now = Date.now();
const hit = cache.get(key);
if (hit && hit.expiresAt > now) {
return hit.value;
}
const machine = await prisma.machine.findFirst({
where: { id: machineId, apiKey },
select: { id: true, orgId: true },
});
if (!machine) {
cache.delete(key);
return null;
}
if (cache.size > MAX_SIZE) {
cache.clear();
}
cache.set(key, { value: machine, expiresAt: now + TTL_MS });
return machine;
}