The source headers, LICENSE files, and license metadata had drifted apart. Align the entire project to MIT: - Convert every source-file header to the MIT text across all comment styles (Go, TS, TSX, JS, MJS, SQL, CSS, GraphQL, shell), including SPDX-License-Identifier tags - Set the root and cookie-banner LICENSE files to the MIT text with a "MIT License" title line - Switch the package.json license fields, Docker image label, and cookie-banner README to MIT - Update docs and the genmodels header generator accordingly - Normalize copyright lines to a single format (Copyright (c) <year(s)> Probo Inc <hello@probo.com>.): unify the hello@getprobo.com and hello@probo.inc emails to hello@probo.com and the comma-separated years to a hyphenated range Genuine third-party references are intentionally left untouched: the Lucide icon attributions (Lucide is ISC) and the trivy dependency license allowlist. Signed-off-by: Sacha Al Himdani <sacha@probo.com>
247 lines
8.3 KiB
TypeScript
247 lines
8.3 KiB
TypeScript
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// 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`;
|
|
}
|
|
}
|
|
}
|