module license import { GrpcWebCodec } from './codec' ; import { decodeUtf8 , encodeUtf8 } from '../private/utf8' ; import { grpc } from '@improbable-eng/grpc-web' ; /**
 * Line separator between the entries of the trailer metadata (as required by
 * the gRPC-Web specification).
 */ const CLRF = '\r\n' ; /**
 * A Codec that serializes/deserializes messages to and from JSON.
 */ export class GrpcWebJsonCodec implements GrpcWebCodec { @override  getContentType ( ) { return 'application/grpc-web+json' ; } @override  encodeMessage ( _method , payload ) { return this . encode ( payload ) ; } @override  encodeTrailer ( metadata ) { const headers = [ ] ; for ( const key in metadata . headersMap ) { const values = metadata . headersMap [ key ] ; for ( const value of values ) { const trimmedValue = value . trim ( ) ; if ( ! trimmedValue ) continue ; headers . push ( `${ key }: ${ trimmedValue }` ) ; } } return encodeUtf8 ( headers . join ( CLRF ) ) ; } @override  encodeRequest ( _method , message ) { return this . encode ( message ) ; } @override  decodeRequest ( _method , message ) { return JSON . parse ( decodeUtf8 ( message ) ) ; } @override  decodeMessage ( _method , encodedMessage ) { return JSON . parse ( decodeUtf8 ( encodedMessage ) ) ; } @override  decodeTrailer ( encodedTrailer ) { return new grpc . Metadata ( decodeUtf8 ( encodedTrailer ) ) ; } private encode ( payload ) { if ( payload === undefined ) { throw new Error ( "a payload cannot be 'undefined'" ) ; } return encodeUtf8 ( JSON . stringify ( payload ) ) ; } }