Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor({
hostName,
port,
timeout = 5000,
transport = 'buffered',
protocol = 'binary',
logger = defaultLogger,
pool,
}: IConnectionOptions) {
super(getTransport(transport), getProtocol(protocol))
this.hostName = hostName
this.port = port
this.filters = []
this.logger = logger
this.pool = createPool(
{
port,
hostName,
timeout,
},
this.logger,
pool || {},
)
}
constructor({
hostName,
port,
path = '/thrift',
https = false,
transport = 'buffered',
protocol = 'binary',
requestOptions = {},
serviceName,
withEndpointPerMethod = false,
headerBlacklist = [],
}: IHttpConnectionOptions) {
super(Core.getTransport(transport), Core.getProtocol(protocol))
this.requestOptions = Object.freeze(
filterHeaders(requestOptions, headerBlacklist),
)
this.port = port
this.hostName = hostName
this.path = Core.normalizePath(path || DEFAULT_PATH)
this.protocol = https === true ? 'https' : 'http'
this.serviceName = serviceName
this.basePath = `${this.protocol}://${this.hostName}:${this.port}`
this.withEndpointPerMethod = withEndpointPerMethod
this.url = `${this.basePath}${this.path}`
this.filters = []
}
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])
},
)
}
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 !==
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
}
}
constructor(pluginOptions: ICoreThriftHandlerOptions) {
this.pluginOptions = pluginOptions;
this.transport = getTransport(pluginOptions.transport);
this.protocol = getProtocol(pluginOptions.protocol);
}
(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)
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()
}
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',
}