This repository has been archived by the owner on Aug 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
router now supports flexible s3 event handling (#18)
* router now supports flexible s3 event handling
- Loading branch information
1 parent
dd1cc5c
commit 5c4396b
Showing
7 changed files
with
560 additions
and
368 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
"use strict"; | ||
|
||
function validateArguments(s3Config, event) { | ||
|
||
if (!Array.isArray(event.Records) || event.Records.length < 1 || event.Records[0].eventSource !== 'aws:s3') { | ||
console.log('Event does not look like S3'); | ||
return false; | ||
} | ||
|
||
//validate config | ||
if (!Array.isArray(s3Config.routes) || s3Config.routes.length < 1) { | ||
throw new Error('s3Config.routes must not be empty'); | ||
} | ||
|
||
//validate config | ||
for (const route of s3Config.routes) { | ||
if (!route.action) { | ||
throw new Error('s3Config.routes.action must not be empty'); | ||
} | ||
} | ||
|
||
return true; | ||
|
||
} | ||
|
||
function matchConfigToEventValue(config, eventValue) { | ||
|
||
if (!config) { | ||
return true; | ||
} | ||
|
||
if (config instanceof RegExp) { | ||
if (config.test(eventValue)) { | ||
return true; | ||
} | ||
} else { | ||
if (config === eventValue) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function matchObjectKeyWithPrefix(config, eventValue) { | ||
if (!config || eventValue.startsWith(config)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function process(s3Config, event, context) { | ||
|
||
if (s3Config.debug) { | ||
console.log('s3:Event', JSON.stringify(event)); | ||
console.log('s3:context', context); | ||
} | ||
|
||
if (!validateArguments(s3Config, event)) { | ||
return null; | ||
} | ||
|
||
const results = []; | ||
for (const record of event.Records) { | ||
for (const routeConfig of s3Config.routes) { | ||
const bucketNameMatched = matchConfigToEventValue(routeConfig.bucketName, record.s3.bucket.name); | ||
const eventNameMatched = matchConfigToEventValue(routeConfig.eventName, record.eventName); | ||
const objectKeyPrefixMatched = matchObjectKeyWithPrefix(routeConfig.objectKeyPrefix, record.s3.object.key); | ||
|
||
if (s3Config.debug) { | ||
console.log(`match record '${record.eventName}'/'${record.s3.bucket.name}'/'${record.s3.object.key}': bucketMatch ' | ||
${bucketNameMatched}', eventMatch '${eventNameMatched}', key '${objectKeyPrefixMatched}' for route '${JSON.stringify(routeConfig)}'`); | ||
} | ||
|
||
if (bucketNameMatched && eventNameMatched && objectKeyPrefixMatched) { | ||
const result = routeConfig.action(record, context); | ||
if (result) { | ||
results.push(result); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return results; | ||
} | ||
|
||
module.exports = process; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.