@eagleeye-solutions/integration-events-common
Version:
Eagle Eye CDP connector common functionality
79 lines • 3.69 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_INCLUDE_TRACE_ID_IN_HTTP_RESPONSE_HEADERS = exports.DEFAULT_TRACE_ID_NAME = void 0;
exports.connector = connector;
const express_1 = __importDefault(require("express"));
const handlers_1 = require("./handlers");
const platform_1 = require("./platform");
const logger_1 = require("./logger");
const uuid_1 = require("uuid");
const newrelic_middleware_1 = require("./common/newrelic-middleware");
exports.DEFAULT_TRACE_ID_NAME = 'x-ees-connector-trace-id';
exports.DEFAULT_INCLUDE_TRACE_ID_IN_HTTP_RESPONSE_HEADERS = true;
function defaultCdpRequestIdMiddleware(req, res, next) {
req.id = (0, uuid_1.v4)();
next();
}
async function connector(appConfig) {
const app = (0, express_1.default)();
app.set('appConfig', appConfig);
app.use(express_1.default.json());
// External requests coming from EE AIR - we take the eesEventId from the
// request body headers.
app.use(`/out/${appConfig.configPlatform.toLowerCase()}/:connectorId`, (req, res, next) => {
req.id = req?.body?.headers?.eesEventId;
next();
});
// Requests incoming from GCP Pub/Sub - we take the trace id from the
// message attributes.
app.use(appConfig.routes.internal.path, (req, res, next) => {
req.id = req?.body?.message?.attributes?.[appConfig.traceIdName];
next();
});
// External requests coming from the CDP - we either generate a random
// trace id, or use a custom function to extract one from the incoming
// request (if supplied).
if (appConfig.routes) {
if (appConfig.pathSpecificCustomCdpRequestIdMiddlewares) {
for (const x of appConfig.pathSpecificCustomCdpRequestIdMiddlewares) {
app.use(x.path, x.customCdpRequestIdMiddleware);
}
}
else {
app.use(`/in/${appConfig.configPlatform.toLowerCase()}/:connectorId`, appConfig.customCdpRequestIdMiddleware ?? defaultCdpRequestIdMiddleware);
}
}
if (appConfig.includeTraceIdInHttpResponseHeaders) {
app.use((req, res, next) => {
res.set(appConfig.traceIdName, `${req.id}`);
next();
});
}
app.use((0, logger_1.httpLogger)(appConfig));
// Middeware to set custom attributes to New Relic transactions
app.use((0, newrelic_middleware_1.createNewRelicMiddleware)());
app.get('/status', handlers_1.handleStatus);
app.get(`/out/${appConfig.configPlatform.toLowerCase()}/status`, handlers_1.handleStatus);
app.use(`/out/${appConfig.configPlatform.toLowerCase()}/:connectorId`, handlers_1.handleEeAirOutboundRequest);
if (appConfig.routes) {
if (appConfig.routes.in) {
app.use(`/in/${appConfig.configPlatform.toLowerCase()}`, appConfig.routes.in);
}
else {
app.get(`/in/${appConfig.configPlatform.toLowerCase()}/status`, handlers_1.handleStatus);
app.use(`/in/${appConfig.configPlatform.toLowerCase()}/:connectorId`, handlers_1.handleCdpOutboundRequest);
}
if (appConfig.platformConfig.platform === 'GCP' && (0, platform_1.isGoogleCloudRun)()) {
app.use(appConfig.routes.internal.path, async (req, res, next) => {
await (0, platform_1.requireGoogleJwt)(appConfig, req, res, next);
});
}
app.use(appConfig.routes.internal.path, handlers_1.handleInternalMessage);
}
return app;
}
exports.default = connector;
//# sourceMappingURL=connector.js.map