import os from 'os'; import path from 'path'; import crypto from 'crypto'; // Konfiguration des Git-Backends ausschließlich aus Umgebungsvariablen (ADR 0003, "Konfiguration"). const bool = (v, def = false)=>{ if(v === undefined || v === null || v === '') return def; return ['1', 'true', 'yes', 'on'].includes(String(v).trim().toLowerCase()); }; const int = (v, def, { min = 1, max = Number.MAX_SAFE_INTEGER } = {})=>{ const n = parseInt(v, 10); return Number.isFinite(n) ? Math.min(Math.max(n, min), max) : def; }; export const parseKey = (value, name = 'HB_GIT_TOKEN_KEY')=>{ const key = Buffer.from(String(value).trim(), 'base64'); if(key.length !== 32) throw new Error(`${name} muss 32 Byte (base64) lang sein, z. B. openssl rand -base64 32`); return key; }; export const keyId = (key)=>crypto.createHash('sha256').update(key).digest('hex').slice(0, 8); export const readGitConfig = (env = process.env)=>{ const key = env.HB_GIT_TOKEN_KEY ? parseKey(env.HB_GIT_TOKEN_KEY) : undefined; const oldKeys = (env.HB_GIT_TOKEN_KEYS_OLD || '').split(',').map((s)=>s.trim()).filter(Boolean).map((k)=>parseKey(k, 'HB_GIT_TOKEN_KEYS_OLD')); const timeoutMs = int(env.HB_GIT_TIMEOUT_MS, 20000, { min: 1000 }); const publicHost = (()=>{ try { return new URL(env.HB_PUBLIC_URL).hostname; } catch { return 'localhost'; } })(); return { enabled : !!key, key, oldKeys, allowedHosts : (env.HB_GIT_ALLOWED_HOSTS || '').split(',').map((s)=>s.trim().toLowerCase()).filter(Boolean), allowPrivate : bool(env.HB_GIT_ALLOW_PRIVATE), allowHttp : bool(env.HB_GIT_ALLOW_HTTP), workdir : env.HB_GIT_WORKDIR || path.join(os.tmpdir(), 'hb-git'), timeoutMs, openTimeoutMs : int(env.HB_GIT_OPEN_TIMEOUT_MS, 4000, { min: 1000, max: timeoutMs }), maxFileBytes : int(env.HB_GIT_MAX_FILE_BYTES, 5 * 1024 * 1024), maxConcurrency : int(env.HB_GIT_MAX_CONCURRENCY, 2), maxQueue : 20, committerName : env.HB_GIT_COMMITTER_NAME || 'Homebrewery', committerEmail : env.HB_GIT_COMMITTER_EMAIL || `homebrewery@${publicHost}`, noreplyHost : publicHost }; };