char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
92d1bb0c9
char/aliceai-t5-moe
1/** 2* Text file processing utilities 3* Handles text file detection, reading, and validation 4*/ 5 6import { DEFAULT_BINARY_DETECTION_OPTIONS } from '$lib/constants' ; 7import { FileExtensionText } from '$lib/enums' ; 8import type { BinaryDetectionOptions } from '$lib/types' ; 9 10/** 11* Check if a filename indicates a text file based on its extension 12* @param filename - The filename to check 13* @returns True if the filename has a recognized text file extension 14*/ 15export function isTextFileByName ( filename :string ) :boolean { 16const textExtensions = Object . values ( FileExtensionText ); 17 18return textExtensions . some (( ext :FileExtensionText ) => filename . toLowerCase (). endsWith ( ext )); 19} 20 21/** 22* Read a file's content as text 23* @param file - The file to read 24* @returns Promise resolving to the file's text content 25*/ 26export async function readFileAsText ( file :File ) :Promise < string > { 27return new Promise (( resolve , reject ) => { 28const reader = new FileReader (); 29 30reader . onload = ( event ) => { 31if ( event . target ?. result !== null && event . target ?. result !== undefined ) { 32resolve ( event . target . result as string ); 33} else { 34reject ( new Error ( 'Failed to read file' )); 35} 36}; 37 38reader . onerror = () => reject ( new Error ( 'File reading error' )); 39 40reader . readAsText ( file ); 41}); 42} 43 44/** 45* Heuristic check to determine if content is likely from a text file 46* Detects binary files by counting suspicious characters and null bytes 47* @param content - The file content to analyze 48* @param options - Optional configuration for detection parameters 49* @returns True if the content appears to be text-based 50*/ 51export function isLikelyTextFile ( 52content :string , 53options :Partial < BinaryDetectionOptions > = {} 54) :boolean { 55if ( ! content ) return true ; 56 57const config = { ...DEFAULT_BINARY_DETECTION_OPTIONS , ...options }; 58const sample = content . substring ( 0 , config . prefixLength ); 59 60let nullCount = 0 ; 61let suspiciousControlCount = 0 ; 62 63for ( let i = 0 ; i < sample . length ; i ++ ) { 64const charCode = sample . charCodeAt ( i ); 65 66// Count null bytes - these are strong indicators of binary files 67if ( charCode === 0 ) { 68nullCount ++ ; 69 70continue ; 71} 72 73// Count suspicious control characters 74// Allow common whitespace characters: tab (9), newline (10), carriage return (13) 75if ( charCode < 32 && charCode !== 9 && charCode !== 10 && charCode !== 13 ) { 76// Count most suspicious control characters 77if ( charCode < 8 || ( charCode > 13 && charCode < 27 )) { 78suspiciousControlCount ++ ; 79} 80} 81 82// Count replacement characters (indicates encoding issues) 83if ( charCode === 0xfffd ) { 84suspiciousControlCount ++ ; 85} 86} 87 88// Reject if too many null bytes 89if ( nullCount > config . maxAbsoluteNullBytes ) return false ; 90 91// Reject if too many suspicious characters 92if ( suspiciousControlCount / sample . length > config . suspiciousCharThresholdRatio ) return false ; 93 94return true ; 95}