SPB Git

Repository Public

phone.ts 3.0 KB 66 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr SMS Gateway8 * File             : backend/src/security/phone.ts9 * Module           : security10 * Repository       : https://git.spboucher.ai/maclustr-sms11 * Project Website  : https://sms.spboucher.ai12 * Created          : 2026-11-3013 * Last Modified    : 2026-11-3014 *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: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142import { parsePhoneNumberFromString, type CountryCode } from "libphonenumber-js/max"43import { config } from "../config.ts"44import { unprocessable } from "../util.ts"4546/** Valide et normalise un numéro de destinataire en E.164. Les codes courts et expéditeurs alphanumériques sont refusés à l'envoi. */47export function normalizeRecipient(input: string): string {48  const raw = String(input ?? "").trim()49  const p = parsePhoneNumberFromString(raw, config.defaultRegion as CountryCode)50  if (!p || !p.isValid()) throw unprocessable("invalid_phone_number", "numéro de destinataire invalide (format E.164 attendu, ex. +14185550123)")51  return p.number52}53/** Normalise un expéditeur reçu : E.164 si c'est un numéro valide, sinon la valeur brute (code court, alphanumérique). */54export function normalizeSender(input: string, countryIso?: string | null): string {55  const raw = String(input ?? "").trim()56  const p = parsePhoneNumberFromString(raw, ((countryIso || config.defaultRegion).toUpperCase()) as CountryCode)57  return p && p.isValid() ? p.number : raw.slice(0, 32)58}59export function tryE164(input: string | null | undefined, countryIso?: string | null): string | null {60  if (!input) return null61  const p = parsePhoneNumberFromString(String(input).trim(), ((countryIso || config.defaultRegion).toUpperCase()) as CountryCode)62  return p && p.isValid() ? p.number : null63}6465export { MAX_SEGMENTS, segmentInfo, type SegmentInfo } from "./segments.ts"66
Files main
Menu

Theme