'use strict' ; const LEGAL_ENCODINGS = [ 'json' , 'utf8' , 'base64' ] ; const DEFAULT_ENCODING = process . env . DefaultEncoding || 'json' ; export default class Encoder { public static determineEncoding ( encoding ) { return encoding !== undefined && LEGAL_ENCODINGS . includes ( encoding ) ? encoding : DEFAULT_ENCODING ; } public static encode ( data , encoding = DEFAULT_ENCODING ) { const encoded = encoding === 'json' ? data : new Buffer ( JSON . stringify ( data ) ) . toString ( encoding ) ; return encoded ; } public static decode ( data , encoding = DEFAULT_ENCODING ) { const decoded = encoding === 'json' ? data : JSON . parse ( new Buffer ( data . toString ( ) , encoding ) . toString ( 'utf8' ) ) ; return decoded ; } }