serverless-offline-edge-lambda
Version:
A plugin for the Serverless Framework that simulates the behavior of AWS CloudFront Edge Lambdas while developing offline.
98 lines • 4.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOriginFromCfDistribution = void 0;
const types_1 = require("../types");
const convert_headers_1 = require("./convert-headers");
/**
* Extracts the lambda@edge event origin based on the current path pattern and CF details
* @param targetPathPattern
* @param resource
* @returns
*/
function getOriginFromCfDistribution(targetPathPattern, resource) {
var _a, _b, _c, _d;
const origins = (_b = (_a = resource === null || resource === void 0 ? void 0 : resource.Properties) === null || _a === void 0 ? void 0 : _a.DistributionConfig) === null || _b === void 0 ? void 0 : _b.Origins;
const cacheBehaviors = (_d = (_c = resource === null || resource === void 0 ? void 0 : resource.Properties) === null || _c === void 0 ? void 0 : _c.DistributionConfig) === null || _d === void 0 ? void 0 : _d.CacheBehaviors;
if (!origins || !cacheBehaviors) {
return null;
}
const origin = getCFOriginForPathPattern(targetPathPattern, origins, cacheBehaviors);
if (!origin) {
return null;
}
if (origin === null || origin === void 0 ? void 0 : origin.CustomOriginConfig) {
return {
custom: getCustomOriginDetails(origin)
};
}
if (origin === null || origin === void 0 ? void 0 : origin.S3OriginConfig) {
return {
s3: getS3OriginDetails(origin)
};
}
return null;
}
exports.getOriginFromCfDistribution = getOriginFromCfDistribution;
/**
* Extracts the CF origin matching a given path pattern using the cache behaviors as is done in the CF
* @param targetPathPattern
* @param origins
* @param cacheBehaviors
*/
function getCFOriginForPathPattern(targetPathPattern, origins, cacheBehaviors) {
const cacheBehaviour = cacheBehaviors.find((item) => item.PathPattern === targetPathPattern);
if (!cacheBehaviour) {
return null;
}
return origins.find((origin) => origin.Id === cacheBehaviour.TargetOriginId) || null;
}
/**
* Handler for fetching generic information shared between s3 and custom endpoints
* @param origin
*/
function getGenericOriginDetails(origin) {
return {
// Default connection timeout is 10 seconds
keepaliveTimeout: (origin === null || origin === void 0 ? void 0 : origin.ConnectionTimeout) || 10,
customHeaders: getCustomHeaders((origin === null || origin === void 0 ? void 0 : origin.OriginCustomHeaders) || []),
domainName: origin.DomainName,
};
}
/**
* Gets details on a given s3 origin
* @param origin
* @returns
*/
function getS3OriginDetails(origin) {
return Object.assign(Object.assign({}, getGenericOriginDetails(origin)), { domainName: origin.DomainName,
// No clue on how to get this so hard coded for now
authMethod: 'none', region: 'us-east-2', path: '' });
}
/**
* Get details on a custom origin
* @param origin
* @returns
*/
function getCustomOriginDetails(origin) {
const customOriginConfig = origin.CustomOriginConfig;
return Object.assign(Object.assign({}, getGenericOriginDetails(origin)), { port: ((customOriginConfig === null || customOriginConfig === void 0 ? void 0 : customOriginConfig.OriginProtocolPolicy) === types_1.CFOriginProtocolPolicy.HTTPS_ONLY ?
customOriginConfig === null || customOriginConfig === void 0 ? void 0 : customOriginConfig.HTTPSPort :
customOriginConfig === null || customOriginConfig === void 0 ? void 0 : customOriginConfig.HTTPPort),
// No clue on how to get this so hard coded for now
path: '', protocol: (customOriginConfig === null || customOriginConfig === void 0 ? void 0 : customOriginConfig.OriginProtocolPolicy) === types_1.CFOriginProtocolPolicy.HTTPS_ONLY ?
'https' :
'http', readTimeout: customOriginConfig === null || customOriginConfig === void 0 ? void 0 : customOriginConfig.OriginReadTimeout, sslProtocols: customOriginConfig === null || customOriginConfig === void 0 ? void 0 : customOriginConfig.OriginSSLProtocols });
}
/**
* Converts CF custom headers to event custom headers
* @param customHeaders
* @returns
*/
function getCustomHeaders(customHeaders) {
const headers = {};
for (const header of customHeaders) {
headers[header.HeaderName] = header.HeaderValue;
}
return (0, convert_headers_1.toCloudFrontHeaders)(headers);
}
//# sourceMappingURL=get-origin-from-cf.js.map