import execa from "execa" ; import os from "os" ; export interface UserAgentOptions { name : string ; version : string ; docker ? : boolean ; } export async function userAgent ( options ) { const items = [ format ( options . name , options . version ) , format ( os . type ( ) , `${ os . release ( ) }-${ os . arch ( ) }` ) , format ( "Node" , process . version ) , ] ; const dev = await devInfo ( ) ; if ( dev ) items . push ( format ( "Dev" , dev ) ) ; const docker = options . docker && await dockerInfo ( ) ; if ( docker ) items . push ( format ( "Docker" , docker ) ) ; return items . join ( " " ) ; } function format ( name , info ? ) { name = name . replace ( /\//g , "-" ) ; if ( ! info ) return name ; info = info . replace ( / /g , "_" ) ; return `${ name }/${ info }` ; } async function dockerInfo ( ) { try { const result = await execa ( "docker" , [ "version" , "-f" , "{{json .}}" ] , { reject : false } ) ; const versions = JSON . parse ( result . stdout . split ( "\n" ) [ 0 ] ) ; const client = versions . Client . Version ; const server = versions . Server && versions . Server . Version ; if ( ! server ) return client ; return `${ client }+${ server }` ; } catch ( _ ) { return "" ; } } async function devInfo ( ) { try { await execa ( "git" , [ "rev-parse" , "--git-dir" ] , { cwd : __dirname } ) ; return "true" ; } catch ( _ ) { return "" ; } }