/** * @author xiangshouding * @file protocol/thrift.ts */ import {Map} from '../types/lang'; import {Address, Protocol} from './Protocol'; import * as thrift from 'thrift'; export class Thrift implements Protocol { service: any; addr: Address; timeout: number; transport: string; public constructor(addr: Address, options?: Map) { this.service = options['service']; this.timeout = options['timeout']; this.transport = options['transport'] this.addr = addr; } public createConnection() { let transport, protocol; if (this.transport && this.transport === 'FRAMED') { transport = thrift.TFramedTransport; } else { transport = thrift.TBufferedTransport; } protocol = thrift.TBinaryProtocol; return thrift.createConnection(this.addr.getHost(), this.addr.getPort(), { transport: transport, protocol: protocol, 'connect_timeout': this.timeout }); } public request(cb) { if (!this.service) { return cb(new Error('invalid service or not given, please use .loadService() given it.')); } let con = this.createConnection(); con.on('error', cb); return cb(null, thrift.createClient(this.service, con), con); } }