SPB Git

Repository Public

rollout.ts 4.5 KB 96 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : KA Maps Engine8 * File             : server/km-maps/lib/rollout.ts9 * Module           : server10 * Repository       : https://git.spboucher.ai/ka-maps-engine11 * Created          : 2026-12-0112 * Last Modified    : 2026-12-0113 *14 * Author           : Simon-Pierre Boucher15 * Position         : Professor of Finance16 * Institution      : Université du Québec en Outaouais (UQO)17 * Organization     : SP Boucher18 * Website          : https://spboucher.ai19 * Git              : https://git.spboucher.ai20 *21 * Preferred Contact: support@spboucher.ai22 * Development      : dev@spboucher.ai23 * Support          : support@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 */4142// Diffusion progressive et compatibilité : fonctions pures, partagées par le service de manifeste43// (qui décide) et le Studio (qui explique la décision à l'administrateur).44import { compareSemver } from './semver.ts';4546export const APPS = ['louka', 'immoka', 'vraiprix', 'valoplex'] as const;47export const PLATFORMS = ['web', 'ios', 'android'] as const;48export type App = (typeof APPS)[number];49export type Platform = (typeof PLATFORMS)[number];5051/** Décalage stable par thème (FNV-1a 32 bits, modulo 100) : ce ne sont pas toujours les mêmes52 *  installations qui reçoivent en premier les nouvelles versions de tous les thèmes. */53export function themeSalt(themeId: string): number {54  let h = 0x811c9dc5;55  for (let i = 0; i < themeId.length; i++) {56    h ^= themeId.charCodeAt(i);57    h = Math.imul(h, 0x01000193) >>> 0;58  }59  return h % 100;60}6162/** Vrai si l'installation `bucket` (0–99, tirée au hasard une fois par l'application, jamais liée63 *  à un compte ni à une adresse) fait partie des `percent` % servis. Monotone : une installation64 *  servie à 10 % l'est encore à 50 %. Sans `bucket`, seul 100 % sert la version candidate. */65export function inRollout(themeId: string, bucket: number | null, percent: number): boolean {66  if (percent >= 100) return true;67  if (percent <= 0 || bucket === null) return false;68  return (bucket + themeSalt(themeId)) % 100 < percent;69}7071export type VersionMeta = { version: number; minEngine: string };72export type ChannelState = { stable: number | null; candidate: number | null; percent: number };73export type Resolution =74  | { version: number; channel: 'stable' | 'candidate'; target: number; downgraded: boolean }75  | { version: null; reason: 'unpublished' | 'engine' };7677/** Version d'un thème à servir : la candidate si l'installation est dans la tranche, sinon la78 *  stable ; puis, si le moteur du client est trop ancien pour elle, la plus récente version79 *  publiée antérieure qu'il sait lire. */80export function resolveTheme(themeId: string, versions: VersionMeta[], ch: ChannelState, engine: string | null, bucket: number | null): Resolution {81  let target: number | null = ch.stable;82  let channel: 'stable' | 'candidate' = 'stable';83  if (ch.candidate !== null && inRollout(themeId, bucket, ch.percent)) {84    target = ch.candidate;85    channel = 'candidate';86  }87  if (target === null) return { version: null, reason: 'unpublished' };88  if (!versions.some((v) => v.version === target)) return { version: null, reason: 'unpublished' };89  if (engine === null) return { version: target, channel, target, downgraded: false };90  const readable = versions91    .filter((v) => v.version <= (target as number) && compareSemver(v.minEngine, engine) <= 0)92    .sort((a, b) => b.version - a.version);93  if (readable.length === 0) return { version: null, reason: 'engine' };94  return { version: readable[0].version, channel, target, downgraded: readable[0].version !== target };95}96
Files main
Menu

Theme