import { Converter } from './Converter' ; import { EncodedData } from './EncodedData' ; export class Encoder { public static toBase64 ( data ) { const decoded = Converter . toArrayBufferView ( data ) ; const asString = Encoder . fromByteArray ( decoded ) ; const asBytes = Converter . stringToArrayBufferViewUTF8 ( asString ) ; return new EncodedData ( asBytes , asString ) ; } private static fromByteArray ( decoded ) { if ( typeof window === 'object' ) { const decodedString = Converter . arrayBufferViewToBaselineString ( decoded ) ; return window . btoa ( decodedString ) ; } return Buffer . from ( decoded . buffer ) . toString ( 'base64' ) ; } }