@codification/cutwater-aws
Version:
A library providing general functionality for TypeScript based AWS projects.
131 lines • 6.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withOriginResponseHeaders = exports.withRequestHeaderConfig = exports.withCustomOriginRequestHeaders = exports.withOriginRequestResponse = void 0;
const http_1 = require("http");
const cutwater_core_1 = require("@codification/cutwater-core");
const cutwater_logging_1 = require("@codification/cutwater-logging");
const cutwater_node_core_1 = require("@codification/cutwater-node-core");
const LambdaEdgeUtils_1 = require("./LambdaEdgeUtils");
const LOG = cutwater_logging_1.LoggerFactory.getLogger();
const toRequestOptions = (req, origin) => {
const rval = {};
rval.protocol = origin.protocol + ':';
rval.hostname = origin.domainName;
rval.path = `${req.uri}${req.querystring ? '?' + req.querystring : ''}`;
rval.method = req.method;
const incomingHeaders = LambdaEdgeUtils_1.LambdaEdgeUtils.toIncomingHttpHeaders(req.headers);
rval.headers = incomingHeaders;
if (origin.customHeaders) {
rval.headers = cutwater_node_core_1.HttpUtils.mergeHeaders(incomingHeaders, LambdaEdgeUtils_1.LambdaEdgeUtils.toIncomingHttpHeaders(origin.customHeaders));
}
return rval;
};
/**
* @beta
*/
const withOriginRequestResponse = (config) => {
return {
before: (handler, next) => {
if (LambdaEdgeUtils_1.LambdaEdgeUtils.isCustomOriginRequestEvent(handler.event)) {
const request = handler.event.Records[0].cf.request;
if (config) {
config.filter(request);
}
const origin = LambdaEdgeUtils_1.LambdaEdgeUtils.toCloudFrontCustomOrigin(request);
const options = toRequestOptions(request, origin);
LOG.trace('Origin request options: %j', options);
const req = (0, http_1.request)(options, (response) => {
LambdaEdgeUtils_1.LambdaEdgeUtils.originResponseToCloudFrontResultResponse(response)
.then((result) => {
handler.event.originResponse = result;
next();
})
.catch((reason) => {
throw new Error(reason);
});
});
req.end();
}
else {
LOG.debug('Skipping middleware because event is not a custom Origin-Request.');
next();
}
},
};
};
exports.withOriginRequestResponse = withOriginRequestResponse;
const findPrefixedHeaders = (headers, prefix, stripPrefix = false) => {
const rval = {};
Object.keys(headers).forEach((header) => {
if (header.toLowerCase().indexOf(prefix.toLowerCase()) === 0) {
const headerName = stripPrefix ? header.substr(prefix.length) : header;
rval[headerName] = headers[header];
}
});
return rval;
};
/**
* @beta
*/
const withCustomOriginRequestHeaders = (customHeaderPrefix = 'x-custom-') => {
return {
before: (handler, next) => {
if (LambdaEdgeUtils_1.LambdaEdgeUtils.isCustomOriginRequestEvent(handler.event)) {
const request = handler.event.Records[0].cf.request;
const origin = LambdaEdgeUtils_1.LambdaEdgeUtils.toCloudFrontCustomOrigin(request);
const headers = LambdaEdgeUtils_1.LambdaEdgeUtils.toIncomingHttpHeaders(request.headers);
const customHeaders = findPrefixedHeaders(LambdaEdgeUtils_1.LambdaEdgeUtils.toIncomingHttpHeaders(origin.customHeaders), customHeaderPrefix, true);
request.headers = LambdaEdgeUtils_1.LambdaEdgeUtils.toCloudFrontHeaders(cutwater_node_core_1.HttpUtils.mergeHeaders(headers, customHeaders, true));
}
else {
LOG.debug('Skipping middleware because event is not a custom Origin-Request.');
}
next();
},
};
};
exports.withCustomOriginRequestHeaders = withCustomOriginRequestHeaders;
/**
* @beta
*/
const withRequestHeaderConfig = (customHeaderPrefix = 'x-config-') => {
return {
before: (handler, next) => {
if (LambdaEdgeUtils_1.LambdaEdgeUtils.isCustomOriginRequestEvent(handler.event)) {
const request = handler.event.Records[0].cf.request;
const origin = LambdaEdgeUtils_1.LambdaEdgeUtils.toCloudFrontCustomOrigin(request);
const customHeaders = findPrefixedHeaders(LambdaEdgeUtils_1.LambdaEdgeUtils.toIncomingHttpHeaders(origin.customHeaders), customHeaderPrefix, true);
Object.keys(customHeaders).forEach((header) => {
const headerValue = customHeaders[header] || '';
cutwater_core_1.Config.put(header, headerValue.toString());
LOG.debug('Adding [%s] config value: %s', header, headerValue.toString());
});
}
else {
LOG.debug('Skipping middleware because event is not a custom Origin-Request.');
}
next();
},
};
};
exports.withRequestHeaderConfig = withRequestHeaderConfig;
/**
* @beta
*/
const withOriginResponseHeaders = (config) => {
return {
before: (handler, next) => {
if (LambdaEdgeUtils_1.LambdaEdgeUtils.isCustomOriginResponseEvent(handler.event)) {
const response = handler.event.Records[0].cf.response;
const mergedHeaders = cutwater_node_core_1.HttpUtils.mergeHeaders(LambdaEdgeUtils_1.LambdaEdgeUtils.toIncomingHttpHeaders(response.headers), config);
response.headers = LambdaEdgeUtils_1.LambdaEdgeUtils.stripOriginResponseHeaders(LambdaEdgeUtils_1.LambdaEdgeUtils.toCloudFrontHeaders(mergedHeaders));
}
else {
LOG.debug('Skipping middleware because event is not a Origin-Response.');
}
next();
},
};
};
exports.withOriginResponseHeaders = withOriginResponseHeaders;
//# sourceMappingURL=MIddyMiddlewares.js.map