Skip to content

Commit

Permalink
Fix Post login trigger according to recommendations in https://github…
Browse files Browse the repository at this point in the history
  • Loading branch information
fbdo committed Aug 5, 2021
1 parent 2ad362b commit 6240705
Showing 1 changed file with 39 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
/*
this file will loop through all js modules which are uploaded to the lambda resource,
provided that the file names (without extension) are included in the "MODULES" env variable.
"MODULES" is a comma-delimmited string.
*/
const moduleNames = process.env.MODULES.split(',');
const modules = moduleNames.map(name => require(`./${name}`));
/**
* @fileoverview
*
* This CloudFormation Trigger creates a handler which awaits the other handlers
* specified in the `MODULES` env var, located at `./${MODULE}`.
*/

exports.handler = (event, context, callback) => {
for (let i = 0; i < modules.length; i += 1) {
const { handler } = modules[i];
handler(event, context, callback);
}
};
/**
* The names of modules to load are stored as a comma-delimited string in the
* `MODULES` env var.
*/
const moduleNames = process.env.MODULES.split(',');
/**
* The array of imported modules.
*/
const modules = moduleNames.map(name => require(`./${name}`));

/**
* This async handler iterates over the given modules and awaits them.
*
* @see https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html#nodejs-handler-async
*
* @param {object} event
*
* The event that triggered this Lambda.
*
* @returns
*
* The handler response.
*/
exports.handler = async event => {
/**
* Instead of naively iterating over all handlers, run them concurrently with
* `await Promise.all(...)`. This would otherwise just be determined by the
* order of names in the `MODULES` var.
*/
await Promise.all(modules.map(module => module.handler(event)));
return event;
};

0 comments on commit 6240705

Please sign in to comment.