UNPKG

@creditkarma/thrift-server-hapi

Version:

A Hapi server plugin for the Apache Thrift protocol

128 lines 5.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ThriftServerHapi = exports.DEFAULT_PATH = void 0; const Core = require("@creditkarma/thrift-server-core"); const logger_1 = require("./logger"); exports.DEFAULT_PATH = '/thrift'; function ThriftServerHapi(pluginOptions) { const thriftOptions = pluginOptions.thriftOptions; const logger = thriftOptions.logger || logger_1.defaultLogger; const thriftPath = Core.normalizePath(pluginOptions.path || exports.DEFAULT_PATH); const serviceName = pluginOptions.thriftOptions.serviceName; const Transport = Core.getTransport(thriftOptions.transport); const Protocol = Core.getProtocol(thriftOptions.protocol); const processor = thriftOptions.handler; const rawServiceName = processor._serviceName; return { name: require('../../package.json').name, version: require('../../package.json').version, multiple: true, async register(server, nothing) { if (server.plugins.thrift !== undefined && (server.plugins.thrift.transport !== (thriftOptions.transport || 'buffered') || server.plugins.thrift.protocol !== (thriftOptions.protocol || 'binary'))) { logger(['error', 'ThriftServerHapi'], `You are registering services with different transport/protocol combinations on the same Hapi.Server instance. You may experience unexpected behavior.`); } server.plugins.thrift = { transport: thriftOptions.transport || 'buffered', protocol: thriftOptions.protocol || 'binary', services: { [serviceName]: { processor, }, }, }; server.ext('onPreResponse', (request, reply) => { const response = request.response; if (response !== null) { if (response.isBoom && request.plugins.thrift !== undefined) { try { const transportWithData = Transport.receiver(request.payload); const input = new Protocol(transportWithData); const metadata = input.readMessageBegin(); const fieldName = metadata.fieldName; const requestId = metadata.requestId; const output = new Protocol(new Transport()); const exception = new Core.TApplicationException(Core.TApplicationExceptionType.INTERNAL_ERROR, response.message); output.writeMessageBegin(fieldName, Core.MessageType.EXCEPTION, requestId); Core.TApplicationExceptionCodec.encode(exception, output); output.writeMessageEnd(); return reply.response(output.flush()).code(500); } catch (err) { logger(['error', 'ThriftServerHapi'], `Unable to build TApplicationException for response error: ${err instanceof Error ? err.message : 'Unexpected error thrown'}`); return reply.continue; } } else { return reply.continue; } } else { return reply.continue; } }); const handler = (request, reply) => { const buffer = Core.stripStruct(request.payload); const method = Core.readThriftMethod(buffer, Transport, Protocol); request.plugins.thrift = { requestMethod: method, }; return Core.process({ processor, buffer, Transport, Protocol, context: request, }); }; if (thriftOptions.withEndpointPerMethod === true) { thriftOptions.handler._methodNames.forEach((methodName) => { const methodPerEndpointPath = `${thriftPath}/${rawServiceName}/${methodName}`; server.route({ method: 'POST', path: methodPerEndpointPath, handler, options: { payload: { parse: false, }, auth: pluginOptions.auth, }, }); }); server.route({ method: 'POST', path: `${thriftPath}/{p*}`, handler, options: { payload: { parse: false, }, auth: pluginOptions.auth, }, }); } else { server.route({ method: 'POST', path: thriftPath === '' ? '/' : `${thriftPath}`, handler, options: { payload: { parse: false, }, auth: pluginOptions.auth, }, }); } }, }; } exports.ThriftServerHapi = ThriftServerHapi; //# sourceMappingURL=ThriftServerHapi.js.map