Skip to content

Commit

Permalink
Adds support for prefixes and suffixes and a bugfix for having no pre…
Browse files Browse the repository at this point in the history
…fix/suffix option at all
  • Loading branch information
jcohenho committed Mar 5, 2022
1 parent 3c52f01 commit 1620ff8
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,25 @@ module.exports = {
options.lambdas.forEach(l => {
Object.keys(l.events).forEach(e => {
let eventParts = e.split(':');
let filterMatch;
// TODO: prefix and suffix support
if (l.events[e].length > 0) {
let eventFilterPairs = l.events[e][0];
let eventFilterType = eventFilterPairs[0];
let eventFilterKey = eventFilterPairs[1];
if (eventFilterType === 'prefix') {
filterMatch = record.s3.object.key.split('/')[0] === eventFilterKey;
}
else if (eventFilterType === 'suffix') {
filterMatch = record.s3.object.key.split('.').pop() === eventFilterKey;
}
}
else {
filterMatch = true;
}
let evt = eventParts[1]; // i.e. ObjectCreated or ObjectRemoved
let api = eventParts[2]; // i.e. *, Put, Post, Copy
if (evt === triggerEvt && (api === '*' || triggerApi === api)) {
if (evt === triggerEvt && (api === '*' || triggerApi === api) && filterMatch) {
if (!lambdasToTrigger.includes(l)) lambdasToTrigger.push(l);
}
});
Expand Down Expand Up @@ -348,15 +363,24 @@ function opts (pragma) {
events: {}
};
let props = opt[key];
let lambdaKeys = Object.keys(props);
lambdaKeys.forEach(eventName => {
let filterPairs = props[eventName];
lambda.events[eventName] = [];
for (let i = 0; i < filterPairs.length - 1; i++) {
if (i % 2 === 1) continue;
lambda.events[eventName].push([ filterPairs[i], filterPairs[i + 1] ]);
}
});
if (Array.isArray(props)) {
let lambdaKeys = props;
lambdaKeys.forEach(eventName => {
lambda.events[eventName] = [];
obj.lambdas.push(lambda);
});
} else {
let lambdaKeys = Object.keys(props);
lambdaKeys.forEach(eventName => {
let filterPairs = props[eventName];
lambda.events[eventName] = [];
for (let i = 0; i < filterPairs.length - 1; i++) {
if (i % 2 === 1) continue;
lambda.events[eventName].push([ filterPairs[i], filterPairs[i + 1] ]);
}
});
obj.lambdas.push(lambda);
}
obj.lambdas.push(lambda);
} else {
obj[key] = opt[key];
Expand Down

0 comments on commit 1620ff8

Please sign in to comment.