format import { ClientQuery , DeviceOS , ResponseMessage } from 'flipper-common' ; import { ParsedUrlQuery } from 'querystring' ; import { CertificateExchangeMedium } from '../utils/CertificateProvider' ; import { SecureClientQuery } from './ServerAdapter' ; param medium export function transformCertificateExchangeMediumToType ( medium ) { switch ( medium ) { case undefined : case 1 : return 'FS_ACCESS' ; case 2 : return 'WWW' ; case 3 : return 'NONE' ; default : throw new Error ( 'Unknown Certificate exchange medium: ' + medium ) ; } } param query export function appNameWithUpdateHint ( query ) { if ( query . os === 'Android' && ( ! query . sdk_version || query . sdk_version < 3 ) ) { return query . app + ' (Outdated SDK)' ; } return query . app ; } export function parseMessageToJson < T extends object = object > ( message ) { try { return JSON . parse ( message . toString ( ) ) ; } catch ( err ) { console . warn ( `Invalid JSON: ${ message }` , 'clientMessage' ) ; return ; } } export function isWsResponseMessage ( message ) { return typeof ( message as ResponseMessage ) . id === 'number' ; } const certExchangeSupportedOSes = new Set < DeviceOS > ( [ 'Android' , 'iOS' , 'MacOS' , 'Metro' , 'Windows' , ] ) ; param str export function verifyClientQueryComesFromCertExchangeSupportedOS ( query ) { if ( ! query || ! certExchangeSupportedOSes . has ( query . os ) ) { return ; } return query ; } param message export function parseClientQuery ( query ) { /** Any required arguments to construct a ClientQuery come
     * embedded in the query string.
     */ let device_id ; if ( typeof query . device_id === 'string' ) { device_id = query . device_id ; } else { return ; } let device ; if ( typeof query . device === 'string' ) { device = query . device ; } else { return ; } let app ; if ( typeof query . app === 'string' ) { app = query . app ; } else { return ; } let os ; if ( typeof query . os === 'string' ) { os = query . os as DeviceOS ; } else { return ; } const clientQuery = { device_id , device , app , os , } ; if ( typeof query . sdk_version === 'string' ) { const sdk_version = parseInt ( query . sdk_version , 10 ) ; if ( sdk_version ) { ( clientQuery as any ) . sdk_version = sdk_version ; } } return clientQuery ; } param message export function parseSecureClientQuery ( query ) { /** Any required arguments to construct a SecureClientQuery come
     * embedded in the query string.
     */ const clientQuery = verifyClientQueryComesFromCertExchangeSupportedOS ( parseClientQuery ( query ) ) ; if ( ! clientQuery ) { return ; } let csr ; if ( typeof query . csr === 'string' ) { const buffer = Buffer . from ( query . csr , 'base64' ) ; if ( buffer ) { csr = buffer . toString ( 'ascii' ) ; } } let csr_path ; if ( typeof query . csr_path === 'string' ) { csr_path = query . csr_path ; } let medium ; if ( typeof query . medium === 'string' ) { medium = parseInt ( query . medium , 10 ) ; } if ( medium !== undefined && ( medium < 1 || medium > 3 ) ) { throw new Error ( 'Unsupported exchange medium: ' + medium ) ; } return { ... clientQuery , csr , csr_path , medium : medium as any } ; } export function cloneClientQuerySafeForLogging ( clientQuery ) { return { ... clientQuery , csr : ! clientQuery . csr ? clientQuery . csr : '<hidden>' } ; } export function assertNotNull < T extends any > ( value , message = 'Unexpected null/undefined value found' ) { if ( value === null || value === undefined ) { throw new Error ( message ) ; } }