import { IncomingMessage } from 'http' ; import fs from 'fs' ; import { http , https , FollowResponse } from 'follow-redirects' ; import { Track } from '../base' ; import path from 'path' ; export async function createRemoteStream ( urlIn ) { const newUrl = new URL ( urlIn ) ; const fetcher = newUrl . protocol === 'https:' ? https : http ; return new Promise < IncomingMessage & FollowResponse > ( ( resolve , reject ) => fetcher . get ( urlIn , resolve ) . on ( 'error' , reject ) ) ; } export function isURL ( FileName ) { let url ; try { url = new URL ( FileName ) ; } catch ( _ ) { return false ; } return url . protocol === 'http:' || url . protocol === 'https:' ; } export function guessAdapterFromFileName ( filePath ) { const uri = isURL ( filePath ) ? filePath : path . resolve ( filePath ) ; const name = path . basename ( uri ) ; if ( /\.vcf\.b?gz$/i . test ( filePath ) ) { return { trackId : name , name : name , assemblyNames : [ ] , adapter : { type : 'VcfTabixAdapter' , vcfGzLocation : { uri , locationType : 'UriLocation' } , } , } ; } else if ( /\.gff3?\.b?gz$/i . test ( filePath ) ) { return { trackId : name , name , assemblyNames : [ ] , adapter : { type : 'Gff3TabixAdapter' , gffGzLocation : { uri , locationType : 'UriLocation' } , } , } ; } else if ( /\.gtf?$/i . test ( filePath ) ) { return { trackId : name , name , assemblyNames : [ ] , adapter : { type : 'GtfAdapter' , gtfLocation : { uri , locationType : 'UriLocation' } , } , } ; } else { throw new Error ( `Unsupported file type ${ filePath }` ) ; } } export function supported ( type ) { return [ 'Gff3TabixAdapter' , 'VcfTabixAdapter' ] . includes ( type ) ; } param name param attributes param include param exclude param configs export async function generateMeta ( { configs , attributes , outDir , name , exclude , assemblyNames , } ) { const tracks = configs . map ( config => { const { trackId , textSearching , adapter } = config ; const includeExclude = textSearching ?. indexingFeatureTypesToExclude || exclude ; const metaAttrs = textSearching ?. indexingAttributes || attributes ; return { trackId : trackId , attributesIndexed : metaAttrs , excludedTypes : includeExclude , adapterConf : adapter , } ; } ) ; fs . writeFileSync ( path . join ( outDir , 'trix' , `${ name }_meta.json` ) , JSON . stringify ( { dateCreated : new Date ( ) . toISOString ( ) , tracks , assemblyNames , } , null , 2 ) ) ; }