interface InfisicalOptions { siteUrl: string; clientId: string; clientSecret: string; projectId: string; environment: string; } interface InfisicalSecret { secretKey: string; secretValue: string; } const REQUEST_TIMEOUT_MS = 10_000; const MAX_RETRIES = 4; /** The two Infisical operations used during SecretService initialization. */ export async function loadInfisicalSecrets( options: InfisicalOptions, ): Promise<{ secrets: InfisicalSecret[] }> { const baseUrl = options.siteUrl.endsWith('/') ? options.siteUrl : `${options.siteUrl}/`; const login = await requestJson( new URL('api/v1/auth/universal-auth/login', baseUrl), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ clientId: options.clientId, clientSecret: options.clientSecret, }), }, ); if ( !isRecord(login) || typeof login.accessToken !== 'string' || !login.accessToken ) { throw new Error('Infisical login did not return an access token'); } // Preserve the SDK's v3 endpoint and defaults, including self-hosted URL prefixes. const url = new URL('api/v3/secrets/raw', baseUrl); url.search = new URLSearchParams({ workspaceId: options.projectId, environment: options.environment, expandSecretReferences: 'true', include_imports: 'false', recursive: 'false', viewSecretValue: 'true', }).toString(); const result = await requestJson(url, { method: 'GET', headers: { Authorization: `Bearer ${login.accessToken}` }, }); if ( !isRecord(result) || !Array.isArray(result.secrets) || !result.secrets.every(isSecret) ) { throw new Error('Infisical returned an invalid secrets response'); } return { secrets: result.secrets }; } async function requestJson(url: URL, init: RequestInit): Promise { for (let attempt = 0; ; attempt += 1) { const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS); let response: Response; try { response = await fetch(url.toString(), { ...init, signal: controller.signal, }); } catch (error) { if (attempt >= MAX_RETRIES) throw error; clearTimeout(timeout); await retryDelay(attempt); continue; } finally { clearTimeout(timeout); } // Match the SDK: retry network errors and 429, but not other HTTP errors. if (response.status === 429 && attempt < MAX_RETRIES) { await response.body?.cancel(); await retryDelay(attempt); continue; } if (!response.ok) { await response.body?.cancel(); throw new Error( `Infisical ${init.method} ${url.pathname} failed with ${response.status}`, ); } try { return await response.json(); } catch { // JSON parse errors can contain secrets from the response body. throw new Error('Infisical returned an invalid JSON response'); } } } function retryDelay(attempt: number): Promise { const base = 1_000 * 2 ** attempt; const delay = base + base * 0.2 * (Math.random() * 2 - 1); return new Promise((resolve) => setTimeout(resolve, delay)); } function isRecord(value: unknown): value is Record { return typeof value === 'object' && value !== null; } function isSecret(value: unknown): value is InfisicalSecret { return ( isRecord(value) && typeof value.secretKey === 'string' && typeof value.secretValue === 'string' ); }