Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : ORIX App Universe8 * File : src/lib/git/spbgit.ts9 * Module : lib10 * Repository : https://git.spboucher.ai/orix-spboucher-ai11 * Project Website : https://www.orixlab.dev12 * Created : 2026-11-2113 * Last Modified : 2026-11-2114 *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: support@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 { env } from "../env";4445/**46 * Client of the SPB Git forge (git.spboucher.ai, custom software — not GitHub/Gitea compatible).47 * Public JSON API, read-only here: GET /api/v1/repos/:name and GET /api/v1/repos/:name/releases.48 * Release assets are public files at /releases/<repo>/<tag>/<file>, each with a declared SHA-256.49 * No credential is ever sent: only public repositories are synchronized.50 */51export type ForgeAsset = { name: string; size: number; sha256: string; uploaded?: string; url: string };52export type ForgeRelease = { tag: string; date: string; subject: string; assets: ForgeAsset[] };53export type ForgeRepo = { name: string; description: string; head: string; lastPush: string | null; commitCount: number; tagCount: number; topLanguage?: string; license?: string };5455export type FetchLike = (url: string, init?: RequestInit) => Promise<Response>;56export class ForgeError extends Error { constructor(public status: number, message: string) { super(message); } }5758const REPO_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,99}$/;5960/** GET with timeout, bounded retries and exponential backoff. 4xx answers are final (no retry). */61export async function forgeGet<T>(path: string, opts: { fetchImpl?: FetchLike; retries?: number; baseDelayMs?: number; timeoutMs?: number } = {}): Promise<T> {62 const f = opts.fetchImpl ?? fetch, retries = opts.retries ?? 3, base = opts.baseDelayMs ?? 500;63 let last: unknown;64 for (let attempt = 0; attempt <= retries; attempt++) {65 if (attempt > 0) await new Promise((r) => setTimeout(r, base * 2 ** (attempt - 1)));66 try {67 const res = await f(env.spbgitUrl + path, { headers: { Accept: "application/json", "User-Agent": "orix-site-sync" }, signal: AbortSignal.timeout(opts.timeoutMs ?? 10_000) });68 if (res.status >= 400 && res.status < 500 && res.status !== 429) throw new ForgeError(res.status, `forge answered ${res.status} for ${path}`);69 if (!res.ok) { last = new ForgeError(res.status, `forge answered ${res.status} for ${path}`); continue; }70 return (await res.json()) as T;71 } catch (e) {72 if (e instanceof ForgeError && e.status >= 400 && e.status < 500 && e.status !== 429) throw e;73 last = e;74 }75 }76 throw last instanceof Error ? last : new Error(String(last));77}7879export async function getRepo(name: string, o?: Parameters<typeof forgeGet>[1]): Promise<ForgeRepo> {80 if (!REPO_NAME.test(name)) throw new ForgeError(400, "invalid repository name");81 const r = await forgeGet<ForgeRepo>(`/api/v1/repos/${name}`, o);82 return { name: r.name, description: r.description ?? "", head: r.head, lastPush: r.lastPush ?? null, commitCount: r.commitCount ?? 0, tagCount: r.tagCount ?? 0, topLanguage: r.topLanguage, license: r.license };83}8485export async function getReleases(name: string, o?: Parameters<typeof forgeGet>[1]): Promise<ForgeRelease[]> {86 if (!REPO_NAME.test(name)) throw new ForgeError(400, "invalid repository name");87 const r = await forgeGet<{ releases?: ForgeRelease[] }>(`/api/v1/repos/${name}/releases`, o);88 return Array.isArray(r.releases) ? r.releases : [];89}9091/** The only URLs the site will ever fetch a binary from: the forge's release area of the expected repository. */92export function isForgeAssetUrl(url: string, repo: string): boolean {93 try {94 const u = new URL(url), base = new URL(env.spbgitUrl);95 return u.protocol === base.protocol && u.host === base.host && u.pathname.startsWith(`/releases/${repo}/`) && !u.pathname.includes("..") && !u.search && !u.hash;96 } catch { return false; }97}98