@bitblit/epsilon
Version:
Tiny adapter to simplify building API gateway Lambda APIS
109 lines • 5.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContextUtil = void 0;
const number_ratchet_1 = require("@bitblit/ratchet/common/number-ratchet");
const error_ratchet_1 = require("@bitblit/ratchet/common/error-ratchet");
const built_in_trace_id_generators_1 = require("../built-in/built-in-trace-id-generators");
const string_ratchet_1 = require("@bitblit/ratchet/common/string-ratchet");
// This class serves as a static holder for the AWS Lambda context, and also adds some
// simple helper functions
class ContextUtil {
// Prevent instantiation
// eslint-disable-next-line @typescript-eslint/no-empty-function
constructor() { }
static initContext(epsilon, evt, ctx, processLabel) {
ContextUtil.CURRENT_EPSILON_REFERENCE = epsilon;
ContextUtil.CURRENT_CONTEXT = ctx;
ContextUtil.CURRENT_EVENT = evt;
ContextUtil.CURRENT_LOG_VARS = {};
ContextUtil.CURRENT_PROCESS_LABEL = processLabel;
}
static clearContext() {
ContextUtil.CURRENT_EPSILON_REFERENCE = null;
ContextUtil.CURRENT_CONTEXT = null;
ContextUtil.CURRENT_EVENT = null;
ContextUtil.CURRENT_LOG_VARS = {};
ContextUtil.CURRENT_PROCESS_LABEL = null;
ContextUtil.CURRENT_OVERRIDE_TRACE_ID = null;
ContextUtil.CURRENT_OVERRIDE_TRACE_DEPTH = null;
}
static setOverrideTrace(traceId, traceDepth) {
ContextUtil.CURRENT_OVERRIDE_TRACE_ID = traceId || ContextUtil.CURRENT_OVERRIDE_TRACE_ID;
ContextUtil.CURRENT_OVERRIDE_TRACE_DEPTH = traceDepth || ContextUtil.CURRENT_OVERRIDE_TRACE_DEPTH;
}
static setOverrideTraceFromInternalBackgroundEntry(entry) {
ContextUtil.setOverrideTrace(entry.traceId, entry.traceDepth);
}
static setOverrideTraceFromInterApiEntry(interApiEntry) {
ContextUtil.setOverrideTrace(interApiEntry.traceId, interApiEntry.traceDepth);
}
static addHeadersToRecord(input, depthOffset = 0) {
if (input) {
input[ContextUtil.traceHeaderName()] = ContextUtil.currentTraceId();
input[ContextUtil.traceDepthHeaderName()] = string_ratchet_1.StringRatchet.safeString(ContextUtil.currentTraceDepth() + depthOffset);
}
else {
error_ratchet_1.ErrorRatchet.throwFormattedErr('Cannot add headers to null/undefined input');
}
}
static addTraceToProxyResult(pr) {
pr.headers = pr.headers || {};
ContextUtil.addHeadersToRecord(pr.headers);
}
static addTraceToHttpRequestInit(ri) {
ri.headers = ri.headers || {};
ContextUtil.addHeadersToRecord(ri.headers, 1);
}
static setProcessLabel(processLabel) {
ContextUtil.CURRENT_PROCESS_LABEL = processLabel;
}
static currentRequestId() {
const ctx = ContextUtil.CURRENT_CONTEXT;
return ctx ? ctx.awsRequestId : null;
}
static remainingTimeMS() {
const ctx = ContextUtil.CURRENT_CONTEXT;
return ctx ? ctx.getRemainingTimeInMillis() : null;
}
static currentProcessLabel() {
return ContextUtil.CURRENT_PROCESS_LABEL || 'unset';
}
static traceHeaderName() {
var _a, _b, _c;
const headerName = ((_c = (_b = (_a = ContextUtil === null || ContextUtil === void 0 ? void 0 : ContextUtil.CURRENT_EPSILON_REFERENCE) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.loggerConfig) === null || _c === void 0 ? void 0 : _c.traceHeaderName) || 'X-TRACE-ID';
return headerName;
}
static traceDepthHeaderName() {
var _a, _b, _c;
const headerName = ((_c = (_b = (_a = ContextUtil === null || ContextUtil === void 0 ? void 0 : ContextUtil.CURRENT_EPSILON_REFERENCE) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.loggerConfig) === null || _c === void 0 ? void 0 : _c.traceDepthHeaderName) || 'X-TRACE-DEPTH';
return headerName;
}
static currentTraceId() {
var _a, _b, _c, _d, _e;
const traceFn = ((_c = (_b = (_a = ContextUtil === null || ContextUtil === void 0 ? void 0 : ContextUtil.CURRENT_EPSILON_REFERENCE) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.loggerConfig) === null || _c === void 0 ? void 0 : _c.traceIdGenerator) || built_in_trace_id_generators_1.BuiltInTraceIdGenerators.fullAwsRequestId;
const traceId = ContextUtil.CURRENT_OVERRIDE_TRACE_ID ||
((_e = (_d = ContextUtil.CURRENT_EVENT) === null || _d === void 0 ? void 0 : _d.headers) === null || _e === void 0 ? void 0 : _e[ContextUtil.traceHeaderName()]) ||
traceFn(ContextUtil.CURRENT_EVENT, ContextUtil.CURRENT_CONTEXT);
return traceId;
}
static currentTraceDepth() {
var _a, _b;
const caller = ContextUtil.CURRENT_OVERRIDE_TRACE_DEPTH ||
number_ratchet_1.NumberRatchet.safeNumber((_b = (_a = ContextUtil.CURRENT_EVENT) === null || _a === void 0 ? void 0 : _a.headers) === null || _b === void 0 ? void 0 : _b[ContextUtil.traceDepthHeaderName()]) ||
1;
return caller;
}
static addLogVariable(name, val) {
ContextUtil.CURRENT_LOG_VARS[name] = val;
}
static fetchLogVariable(name) {
var _a;
return (_a = ContextUtil.CURRENT_LOG_VARS) === null || _a === void 0 ? void 0 : _a[name];
}
static fetchLogVariables() {
return Object.assign({}, ContextUtil.CURRENT_LOG_VARS || {});
}
}
exports.ContextUtil = ContextUtil;
ContextUtil.CURRENT_LOG_VARS = {};
//# sourceMappingURL=context-util.js.map