char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
521a64cd0
char/aliceai-t5-moe
1import { redactValue } from './redact' ; 2import { CORS_PROXY , HEADERS } from '$lib/constants' ; 3import { MimeTypeApplication } from '$lib/enums' ; 4import { settingsStore } from '$lib/stores/settings/index.svelte' ; 5 6/** 7* Get authorization headers for API requests 8* Includes Bearer token if API key is configured 9*/ 10export function getAuthHeaders () :Record < string , string > { 11const currentConfig = settingsStore . config ; 12const apiKey = currentConfig . apiKey ?. toString (). trim (); 13 14return apiKey ?{ [ HEADERS . AUTHORIZATION ] :` ${ HEADERS . BEARER } ${ apiKey } ` } :{}; 15} 16 17/** 18* Get standard JSON headers with optional authorization 19*/ 20export function getJsonHeaders () :Record < string , string > { 21return { 22[ HEADERS . CONTENT_TYPE ] :MimeTypeApplication . JSON , 23 ...getAuthHeaders () 24}; 25} 26 27/** 28* Sanitize HTTP headers by redacting sensitive values. 29* Known sensitive headers (from HEADERS.REDACTED) and any extra headers 30* specified by the caller are fully redacted. Headers listed in 31* `partialRedactHeaders` are partially redacted, showing only the 32* specified number of trailing characters. 33* 34* @param headers - Headers to sanitize 35* @param extraRedactedHeaders - Additional header names to fully redact 36* @param partialRedactHeaders - Map of header name -> number of trailing chars to keep visible 37* @returns Object with header names as keys and (possibly redacted) values 38*/ 39export function sanitizeHeaders ( 40headers ?:HeadersInit , 41extraRedactedHeaders ?:Iterable < string >, 42partialRedactHeaders ?:Map < string , number > 43) :Record < string , string > { 44if ( ! headers ) { 45return {}; 46} 47 48const normalized = new Headers ( headers ); 49const sanitized :Record < string , string > = {}; 50const redactedHeaders = new Set ( 51Array . from ( extraRedactedHeaders ?? [], ( header ) => header . toLowerCase ()) 52); 53 54for ( const [ key , value ] of normalized . entries ()) { 55const normalizedKey = key . toLowerCase (); 56const unproxiedKey = normalizedKey . startsWith ( CORS_PROXY . HEADER_PREFIX ) 57 ?normalizedKey . slice ( CORS_PROXY . HEADER_PREFIX . length ) 58 :normalizedKey ; 59const partialChars = 60partialRedactHeaders ?. get ( normalizedKey ) ?? partialRedactHeaders ?. get ( unproxiedKey ); 61 62if ( partialChars !== undefined ) { 63sanitized [ key ] = redactValue ( value , partialChars ); 64} else if ( 65HEADERS . REDACTED . has ( normalizedKey ) || 66HEADERS . REDACTED . has ( unproxiedKey ) || 67redactedHeaders . has ( normalizedKey ) || 68redactedHeaders . has ( unproxiedKey ) 69) { 70sanitized [ key ] = redactValue ( value ); 71} else { 72sanitized [ key ] = value ; 73} 74} 75 76return sanitized ; 77}