import * as net from 'net' ; import * as thrift from 'thrift' ; import { Calculator , Choice , CommonStruct , Operation , Work , } from './generated-apache/calculator-service' ; import { Code , MappedStruct , SharedStruct , SharedUnion , } from './generated-apache/shared' ; import { CommonUnion } from './generated-apache/common' ; export function createServer ( ) { const impl = { ping ( ) { return ; } , add ( a , b ) { return a + b ; } , addInt64 ( a , b ) { return new thrift . Int64 ( a . toNumber ( ) + b . toNumber ( ) ) ; } , addWithContext ( a , b ) { return a + b ; } , calculate ( logId , work ) { switch ( work . op ) { case Operation . ADD : return work . num1 + work . num2 ; case Operation . SUBTRACT : return work . num1 - work . num2 ; case Operation . DIVIDE : return work . num1 / work . num2 ; case Operation . MULTIPLY : return work . num1 * work . num2 ; } } , zip ( ) { return ; } , getStruct ( key ) { return new SharedStruct ( { code : new Code ( { status : 5 } ) , value : 'test' , } ) ; } , getUnion ( index ) { if ( index === 1 ) { return SharedUnion . fromOption1 ( 'foo' ) ; } else { return SharedUnion . fromOption2 ( 'bar' ) ; } } , getMappedStruct ( index ) { const map = new Map ( ) ; map . set ( 'one' , { code : { status : 5 , } , value : 'test' , } ) ; return new MappedStruct ( { data : map , } ) ; } , echoBinary ( word ) { return word . toString ( 'utf-8' ) ; } , echoString ( word ) { return word ; } , checkName ( choice ) { if ( choice . firstName !== undefined ) { return `FirstName: ${ choice . firstName . name }` ; } else if ( choice . lastName !== undefined ) { return `LastName: ${ choice . lastName . name }` ; } else { throw new Error ( `Unknown choice` ) ; } } , checkOptional ( type ? ) { if ( type === undefined ) { return 'undefined' ; } else { return type ; } } , mapOneList ( list ) { return list . map ( ( next ) => next + 1 ) ; } , mapValues ( map ) { return Array . from ( map . values ( ) ) ; } , listToMap ( list ) { return list . reduce ( ( acc , next ) => { acc . set ( next [ 0 ] , next [ 1 ] ) ; return acc ; } , new Map ( ) ) ; } , fetchThing ( ) { return new CommonStruct ( { code : new Code ( { status : 5 } ) , value : 'test' , } ) ; } , fetchUnion ( ) { return new CommonUnion ( { option1 : 'test' } ) ; } , broken ( ) { throw new Error ( `Yeah, this didn't work` ) ; } , } ; return thrift . createServer < Calculator . Processor , Calculator . IHandler > ( Calculator . Processor , impl , { transport : thrift . TFramedTransport , } ) ; }