import { SecurityDataResults , TestResult } from '../../utils/interfaces' ; import { env } from '../../utils/environment' ; import { default_timeout } from '../../utils/api' ; export const default_results = [ { result : false , infoString : 'Uses HTTPS' , category : 'required' , } , { result : false , infoString : 'Has a valid SSL certificate' , category : 'required' , } , { result : false , infoString : 'No mixed content on page' , category : 'required' , } , ] ; export async function testSecurity ( url ) { const twentySecondTimeout = new Promise < void > ( resolve => setTimeout ( ( ) => resolve ( ) , default_timeout ) ) ; const encodedUrl = encodeURIComponent ( url ) ; const securityUrl = `${ env . api }/Security?site=${ encodedUrl }` ; const fetchResult = fetch ( securityUrl ) ; const fetchResultOrTimeout = await Promise . race ( [ twentySecondTimeout , fetchResult , ] ) ; if ( ! fetchResultOrTimeout ) { console . warn ( 'Security check timed out after 20 seconds.' ) ; return default_results ; } if ( ! fetchResultOrTimeout . ok ) { console . warn ( 'Security check failed with non-successful status code' , fetchResultOrTimeout . status , fetchResultOrTimeout . statusText ) ; return default_results ; } const results = await fetchResultOrTimeout . json ( ) ; console . info ( 'Security detection completed successfully' , results ) ; const organizedResults = [ { result : results . data . isHTTPS , infoString : 'Uses HTTPS' , category : 'required' , } , { result : results . data . valid , infoString : 'Has a valid SSL certificate' , category : 'required' , } , { result : results . data . validProtocol , infoString : 'No mixed content on page' , category : 'required' , } , ] ; return organizedResults ; }