import * as commonmark from 'commonmark' ; import * as request from 'request-promise-native' ; const CACHE_AGE = 1000 * 60 * 60 * 24 ; const cache = { } ; const localeMapping = { 'fy-NL' : 'fy' , 'ga-IE' : 'ga' , hsb : 'de' , 'pa-IN' : 'pa' , 'rm-sursilv' : 'rm' , 'rm-vallader' : 'rm' , 'sv-SE' : 'se' , } ; export default async function fetchLegalDocument ( name , locale ) { if ( ! cache [ name ] ) cache [ name ] = { } ; let { fetchedAt , textHTML } = cache [ name ] [ locale ] || ( { } as any ) ; if ( textHTML && fetchedAt > Date . now ( ) - CACHE_AGE ) { return textHTML ; } const legalLocale = localeMapping [ locale ] ?? locale ; const [ status , text ] = await request ( { uri : `https://raw.githubusercontent.com/mozilla/legal-docs/master/${ legalLocale }/common_voice_${ name }.md` , resolveWithFullResponse : true , } ) . then ( ( response ) => [ response . statusCode , response . body ] ) . catch ( response => [ response . statusCode , null ] ) ; if ( status >= 400 && status < 500 ) { return ( await Promise . all ( [ 'en' , 'es' , 'fr' , 'pt' , 'zh-TW' ] . map ( locale => fetchLegalDocument ( name , locale ) ) ) ) . join ( '<br>' ) ; } else if ( status < 300 ) { textHTML = new commonmark . HtmlRenderer ( ) . render ( new commonmark . Parser ( ) . parse ( ( text as string ) . replace ( /{:\sdatetime=".*" }/ , '' ) ) ) ; } cache [ name ] [ locale ] = { fetchedAt : Date . now ( ) , textHTML } ; return textHTML ; }