Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(request: Hapi.Request, reply: Hapi.ResponseToolkit) => {
const requestMethod: string =
isThrift === true
? Core.readThriftMethod(
request.payload as Buffer,
Core.getTransport(transport),
Core.getProtocol(protocol),
)
: request.method
const normalHeaders: Core.IRequestHeaders = normalizeHeaders(
request.headers,
)
return tracer.scoped(() => {
const traceId: TraceId = instrumentation.recordRequest(
requestMethod,
Core.formatUrl(url.format(request.url)),
(header: string): option.IOption => {
const val = normalHeaders[header.toLowerCase()]
if (val !== null && val !== undefined) {
return new option.Some(val)
} else {
return (
request: ThriftRequest,
response: express.Response,
next: express.NextFunction,
): void => {
const Transport: ITransportConstructor = getTransport(
pluginOptions.transport,
)
const Protocol: IProtocolConstructor = getProtocol(
pluginOptions.protocol,
)
const buffer: Buffer = request.body
const method: string = readThriftMethod(buffer, Transport, Protocol)
request.thrift = {
requestMethod: method,
processor: pluginOptions.handler,
transport: pluginOptions.transport || 'buffered',
protocol: pluginOptions.protocol || 'binary',
}
try {
process({
processor: pluginOptions.handler,
export function appendThriftObject(
value: LooseType,
data: Buffer,
ThriftCodec: IStructCodec,
transportType: TransportType = 'buffered',
protocolType: ProtocolType = 'binary',
): Promise {
const Transport: ITransportConstructor = getTransport(transportType)
const Protocol: IProtocolConstructor = getProtocol(protocolType)
return encode(value, ThriftCodec, Transport, Protocol).then(
(encoded: Buffer) => {
return Buffer.concat([encoded, data])
},
)
}
function upgradeRequest(
transportType: TransportType = 'buffered',
protocolType: ProtocolType = 'binary',
): Buffer {
const Transport: ITransportConstructor = getTransport(transportType)
const Protocol: IProtocolConstructor = getProtocol(protocolType)
const options: TTwitter.ConnectionOptions = new TTwitter.ConnectionOptions()
const writer: TTransport = new Transport()
const output: TProtocol = new Protocol(writer)
output.writeMessageBegin(CAN_TRACE_METHOD_NAME, MessageType.CALL, 0)
options.write(output)
output.writeMessageEnd()
return output.flush()
}
constructor(pluginOptions: ICoreThriftHandlerOptions) {
this.pluginOptions = pluginOptions;
this.transport = getTransport(pluginOptions.transport);
this.protocol = getProtocol(pluginOptions.protocol);
}
export function ThriftServerHapi<
TProcessor extends Core.IThriftProcessor
>(pluginOptions: IHapiPluginOptions): ThriftHapiPlugin {
const thriftOptions: IHapiServerOptions =
pluginOptions.thriftOptions
const logger: Core.LogFunction = thriftOptions.logger || defaultLogger
const thriftPath: string = Core.normalizePath(
pluginOptions.path || DEFAULT_PATH,
)
const serviceName: string = pluginOptions.thriftOptions.serviceName
const Transport: Core.ITransportConstructor = Core.getTransport(
thriftOptions.transport,
)
const Protocol: Core.IProtocolConstructor = Core.getProtocol(
thriftOptions.protocol,
)
const processor: Core.IThriftProcessor = thriftOptions.handler
const rawServiceName: string = processor._serviceName
return {
name: require('../../package.json').name,
version: require('../../package.json').version,
multiple: true,
async register(server: Hapi.Server, nothing: void): Promise {
if (
server.plugins.thrift !== undefined &&
(server.plugins.thrift.transport !==
(thriftOptions.transport || 'buffered') ||
server.plugins.thrift.protocol !==
(thriftOptions.protocol || 'binary'))
tracer.scoped(() => {
const requestMethod: string =
isThrift === true
? readThriftMethod(
request.body,
getTransport(transport),
getProtocol(protocol),
)
: request.method
const normalHeaders: IRequestHeaders = normalizeHeaders(
request.headers,
)
function readHeader(header: string): option.IOption {
const val = normalHeaders[header.toLocaleLowerCase()]
if (val !== null && val !== undefined) {
return new option.Some(val)
} else {
return option.None
}
}