Files
probo/packages/cookie-banner/src/cookie-utils.ts
Émile Ré f1fc2dc0e0 Show persistent for local-storage trackers
Local storage, IndexedDB, and cache storage have no expiry yet
persist until explicitly cleared, so a missing max-age should read
as "persistent", not "session" (the latter only fits cookies and
session storage, which end with the session or tab).

Thread the tracker type through humanizeSeconds (helpers) and
humanizeDuration (cookie-banner, with a localized persistent label)
and pass it at every console and banner call site. The consent
record query now selects trackerType so its duration column can
make the same distinction. This mirrors the Go HumanizedDuration
helper that already renders these types as persistent.

Signed-off-by: Émile Ré <emile@probo.com>
2026-06-19 10:46:44 +02:00

241 lines
7.9 KiB
TypeScript

// Copyright (c) 2026 Probo Inc <hello@probo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
import { interpolate } from "./i18n";
import type { TrackerType } from "./types";
// Tracker types whose data persists until explicitly cleared. When such a
// tracker has no max-age, its lifetime is "persistent" rather than "session"
// (cookies and session storage are cleared when the session/tab ends).
const PERSISTENT_TRACKER_TYPES: ReadonlySet<TrackerType> = new Set([
"LOCAL_STORAGE",
"INDEXED_DB",
"CACHE_STORAGE",
]);
interface DurationTexts {
[key: string]: string;
}
const durationTextsByLanguage: Record<string, DurationTexts> = {
en: {
duration_year_one: "{{count}} year",
duration_year_other: "{{count}} years",
duration_month_one: "{{count}} month",
duration_month_other: "{{count}} months",
duration_week_one: "{{count}} week",
duration_week_other: "{{count}} weeks",
duration_day_one: "{{count}} day",
duration_day_other: "{{count}} days",
duration_hour_one: "{{count}} hour",
duration_hour_other: "{{count}} hours",
duration_minute_one: "{{count}} minute",
duration_minute_other: "{{count}} minutes",
duration_second_one: "{{count}} second",
duration_second_other: "{{count}} seconds",
duration_session: "session",
duration_persistent: "persistent",
},
fr: {
duration_year_one: "{{count}} an",
duration_year_other: "{{count}} ans",
duration_month_one: "{{count}} mois",
duration_month_other: "{{count}} mois",
duration_week_one: "{{count}} semaine",
duration_week_other: "{{count}} semaines",
duration_day_one: "{{count}} jour",
duration_day_other: "{{count}} jours",
duration_hour_one: "{{count}} heure",
duration_hour_other: "{{count}} heures",
duration_minute_one: "{{count}} minute",
duration_minute_other: "{{count}} minutes",
duration_second_one: "{{count}} seconde",
duration_second_other: "{{count}} secondes",
duration_session: "session",
duration_persistent: "persistant",
},
de: {
duration_year_one: "{{count}} Jahr",
duration_year_other: "{{count}} Jahre",
duration_month_one: "{{count}} Monat",
duration_month_other: "{{count}} Monate",
duration_week_one: "{{count}} Woche",
duration_week_other: "{{count}} Wochen",
duration_day_one: "{{count}} Tag",
duration_day_other: "{{count}} Tage",
duration_hour_one: "{{count}} Stunde",
duration_hour_other: "{{count}} Stunden",
duration_minute_one: "{{count}} Minute",
duration_minute_other: "{{count}} Minuten",
duration_second_one: "{{count}} Sekunde",
duration_second_other: "{{count}} Sekunden",
duration_session: "Sitzung",
duration_persistent: "dauerhaft",
},
es: {
duration_year_one: "{{count}} año",
duration_year_other: "{{count}} años",
duration_month_one: "{{count}} mes",
duration_month_other: "{{count}} meses",
duration_week_one: "{{count}} semana",
duration_week_other: "{{count}} semanas",
duration_day_one: "{{count}} día",
duration_day_other: "{{count}} días",
duration_hour_one: "{{count}} hora",
duration_hour_other: "{{count}} horas",
duration_minute_one: "{{count}} minuto",
duration_minute_other: "{{count}} minutos",
duration_second_one: "{{count}} segundo",
duration_second_other: "{{count}} segundos",
duration_session: "sesión",
duration_persistent: "persistente",
},
};
function getDurationTexts(lang?: string): DurationTexts {
if (lang && durationTextsByLanguage[lang]) return durationTextsByLanguage[lang];
return durationTextsByLanguage.en;
}
// [unitSeconds, textKey, snapBuffer]
// snapBuffer: if the remainder is within this many seconds of the next
// whole unit, round up instead of carrying into smaller units.
const DURATION_UNITS: [number, string, number][] = [
[365 * 24 * 3600, "duration_year", 21 * 24 * 3600],
[30 * 24 * 3600, "duration_month", 2 * 24 * 3600],
[7 * 24 * 3600, "duration_week", 12 * 3600],
[24 * 3600, "duration_day", 2 * 3600],
[3600, "duration_hour", 5 * 60],
[60, "duration_minute", 5],
[1, "duration_second", 0],
];
export function humanizeDuration(
seconds: number,
lang?: string,
trackerType?: TrackerType,
): string {
const texts = getDurationTexts(lang);
if (seconds <= 0) {
return trackerType && PERSISTENT_TRACKER_TYPES.has(trackerType)
? texts.duration_persistent
: texts.duration_session;
}
let remaining = seconds;
const parts: string[] = [];
for (const [unit, key, snap] of DURATION_UNITS) {
if (remaining >= unit - snap) {
let count = Math.floor(remaining / unit);
const leftover = remaining - count * unit;
if (leftover >= unit - snap) {
count++;
remaining = 0;
} else if (leftover <= snap) {
remaining = 0;
} else {
remaining = leftover;
}
const tplKey = count === 1 ? `${key}_one` : `${key}_other`;
parts.push(interpolate(texts[tplKey], { count: String(count) }));
}
}
return parts.length > 0 ? parts.join(", ") : texts.duration_session;
}
export function parseCookieName(raw: string): string {
const eqIdx = raw.indexOf("=");
if (eqIdx === -1) return raw.trim();
return raw.substring(0, eqIdx).trim();
}
export function parseMaxAgeSeconds(raw: string): number | null {
const parts = raw.split(";").map((s) => s.trim());
for (const part of parts) {
const lower = part.toLowerCase();
if (lower.startsWith("max-age=")) {
const val = parseInt(part.substring(8), 10);
if (isNaN(val) || val <= 0) return null;
return val;
}
}
for (const part of parts) {
const lower = part.toLowerCase();
if (lower.startsWith("expires=")) {
const dateStr = part.substring(8);
const expires = new Date(dateStr);
if (isNaN(expires.getTime())) return null;
const deltaSeconds = Math.round(
(expires.getTime() - Date.now()) / 1000,
);
if (deltaSeconds <= 0) return null;
return deltaSeconds;
}
}
return null;
}
export function isDeletion(raw: string): boolean {
const parts = raw.split(";").map((s) => s.trim().toLowerCase());
for (const part of parts) {
if (part.startsWith("max-age=")) {
const val = parseInt(part.substring(8), 10);
if (val <= 0) return true;
}
if (part.startsWith("expires=")) {
const dateStr = part.substring(8);
const expires = new Date(dateStr);
if (!isNaN(expires.getTime()) && expires.getTime() <= Date.now()) {
return true;
}
}
}
return false;
}
function getCandidateDomains(hostname: string): string[] {
const parts = hostname.split(".");
if (parts.length <= 1) return [];
const candidates: string[] = [];
// Try progressively broader parent domains. The browser silently
// ignores attempts to clear cookies on public suffixes, so
// over-trying is safe and avoids maintaining a TLD list.
for (let i = 0; i < parts.length - 1; i++) {
candidates.push("." + parts.slice(i).join("."));
}
return candidates;
}
export function removeCookies(names: string[]): void {
const domains = getCandidateDomains(location.hostname);
for (const name of names) {
document.cookie = `${name}=; path=/; max-age=0`;
for (const domain of domains) {
document.cookie = `${name}=; path=/; domain=${domain}; max-age=0`;
}
}
}