import { injectable } from "inversify" ; import { fromByteArray , toByteArray } from "base64-js" ; /**
 * Service handling text/byte array en-/decoding.
 */ @ injectable ( ) export class EncodingService { readonly #textEncoder : TextEncoder = new TextEncoder ( ) ; readonly #textDecoder : TextDecoder = new TextDecoder ( ) ; see TextEncoder encodeText ( text ) { return this . #textEncoder . encode ( text ) ; } see TextDecoder decodeText ( input ) { return this . #textDecoder . decode ( input ) ; } param input param useUriParamSafeAlphabet encodeBase64String ( input , useUriParamSafeAlphabet ) { let encoded = fromByteArray ( input ) ; if ( useUriParamSafeAlphabet ) { encoded = encoded . replace ( /=/g , "~" ) . replace ( /\+/g , "_" ) . replace ( /\//g , "-" ) ; } return encoded ; } param base64 param useUriParamSafeAlphabet decodeBase64String ( base64 , useUriParamSafeAlphabet ) { let encoded = base64 ; if ( useUriParamSafeAlphabet ) { encoded = encoded . replace ( /~/g , "=" ) . replace ( /_/g , "+" ) . replace ( /-/g , "/" ) ; } return toByteArray ( encoded ) ; } }