import { getProtocol , getTransport , IProtocolConstructor , IThriftProcessor , ITransportConstructor , process , readThriftMethod , } from '@creditkarma/thrift-server-core' ; import * as express from 'express' ; import { IExpressServerOptions } from './types' ; type ThriftRequest = express . Request & { thrift ? : { requestMethod : string ; processor : IThriftProcessor < express . Request > ; transport : string ; protocol : string ; } ; } ; export function ThriftServerExpress < TProcessor extends IThriftProcessor < express . Request > > ( pluginOptions ) { return ( request , response , next ) : void => { const Transport = getTransport ( pluginOptions . transport ) ; const Protocol = getProtocol ( pluginOptions . protocol ) ; const buffer = request . body ; const method = readThriftMethod ( buffer , Transport , Protocol ) ; request . thrift = { requestMethod : method , processor : pluginOptions . handler , transport : pluginOptions . transport || 'buffered' , protocol : pluginOptions . protocol || 'binary' , } ; try { process ( { processor : pluginOptions . handler , buffer , Transport , Protocol , context : request , } ) . then ( ( result ) => { response . status ( 200 ) . end ( result ) ; } , ( err ) => { next ( err ) ; } ) ; } catch ( err ) { next ( err ) ; } } ; }