Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : UQO Éval — Le Grand Défi Immobilier8 * File : apps/api/src/util.ts9 * Module : api10 * Repository : https://git.spboucher.ai/uqo-eval-jeux11 * Project Website : https://jeux.uqo-eval.app12 * Created : 2026-11-2513 * Last Modified : 2026-11-2514 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development : dev@spboucher.ai24 * Support : support@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import crypto from "node:crypto";44import { config } from "./config.ts";4546export class HttpError extends Error {47 status: number;48 code: string;49 constructor(status: number, code: string, message: string) {50 super(message);51 this.status = status;52 this.code = code;53 }54}5556export function hmac(value: string): string {57 return crypto.createHmac("sha256", config.sessionSecret).update(value).digest("hex");58}5960export function randomToken(bytes = 32): string {61 return crypto.randomBytes(bytes).toString("base64url");62}6364const CODE_ALPHABET = "ABCDEFGHJKMNPQRSTUVWXYZ23456789"; // sans 0/O, 1/I/L6566/** Code de reprise lisible : 3 groupes de 4 caractères (≈ 59 bits). */67export function randomResumeCode(): string {68 const bytes = crypto.randomBytes(12);69 let out = "";70 for (let i = 0; i < 12; i++) {71 out += CODE_ALPHABET[bytes[i]! % CODE_ALPHABET.length];72 if (i === 3 || i === 7) out += "-";73 }74 return out;75}7677export function normalizeResumeCode(input: string): string {78 const raw = input.toUpperCase().replace(/[^A-Z0-9]/g, "");79 return raw.length === 12 ? `${raw.slice(0, 4)}-${raw.slice(4, 8)}-${raw.slice(8)}` : raw;80}8182export function timingSafeEqualText(a: string, b: string): boolean {83 const x = Buffer.from(hmac(a));84 const y = Buffer.from(hmac(b));85 return crypto.timingSafeEqual(x, y);86}8788export function slugify(text: string): string {89 return text90 .normalize("NFD")91 .replace(/[̀-ͯ]/g, "")92 .toLowerCase()93 .replace(/[^a-z0-9]+/g, "-")94 .replace(/^-+|-+$/g, "")95 .slice(0, 60);96}9798/** Nom affiché nettoyé : espaces normalisés, caractères de contrôle retirés. */99export function cleanDisplayName(input: string): string {100 return input.replace(/[\u0000-\u001f\u007f<>]/g, "").replace(/\s+/g, " ").trim().slice(0, 40);101}102103/** Clé d'unicité d'un nom dans une compétition. */104export function nameKey(displayName: string): string {105 return displayName.normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase().replace(/[^\p{L}\p{N}]+/gu, " ").trim();106}107