stuartforrest-infinity 13444caa28
feat(AWS EventBridge): Support for using native CloudFormation (#8437)
Co-authored-by: Piotr Grzesik <pj.grzesik@gmail.com>
2021-02-24 15:00:48 +01:00

41 lines
1.0 KiB
JavaScript

'use strict';
const crypto = require('crypto');
const makeAndHashRuleName = ({ functionName, index }) => {
const name = makeRuleName({ functionName, index });
if (name.length > 64) {
// Rule names cannot be longer than 64.
// Temporary solution until we have https://github.com/serverless/serverless/issues/6598
return hashName(name, makeRuleNameSuffix(index));
}
return name;
};
const makeRuleName = ({ functionName, index }) => `${functionName}-${makeRuleNameSuffix(index)}`;
const makeRuleNameSuffix = (index) => `rule-${index}`;
const makeEventBusTargetId = (ruleName) => {
const suffix = 'target';
let targetId = `${ruleName}-${suffix}`;
if (targetId.length > 64) {
// Target ids cannot be longer than 64.
targetId = hashName(targetId, suffix);
}
return targetId;
};
const hashName = (name, suffix) =>
`${name.slice(0, 31 - suffix.length)}${crypto
.createHash('md5')
.update(name)
.digest('hex')}-${suffix}`;
module.exports = {
makeAndHashRuleName,
makeRuleName,
hashName,
makeEventBusTargetId,
};