Skip to content

Commit

Permalink
Take care of ESM based (.mjs) handlers for the AWS Lambda instrumen…
Browse files Browse the repository at this point in the history
…tation
  • Loading branch information
serkan-ozal committed Oct 28, 2024
1 parent 7633cae commit fea7f36
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,28 @@ export class AwsLambdaInstrumentation extends InstrumentationBase<AwsLambdaInstr
// Lambda loads user function using an absolute path.
let filename = path.resolve(taskRoot, moduleRoot, module);
if (!filename.endsWith('.js')) {
// its impossible to know in advance if the user has a cjs or js file.
// check that the .js file exists otherwise fallback to next known possibility
// Its impossible to know in advance if the user has a js, cjs or mjs file.
// Check that the .js file exists otherwise fallback to the next known possibilities (.cjs, .mjs).
try {
fs.statSync(`${filename}.js`);
filename += '.js';
} catch (e) {
// fallback to .cjs
filename += '.cjs';
try {
fs.statSync(`${filename}.cjs`);
// fallback to .cjs (CommonJS)
filename += '.cjs';
} catch (e2) {
try {
fs.statSync(`${filename}.mjs`);
// fallback to .mjs (ESM)
filename += '.mjs';
} catch (e3) {
diag.error(
'No handler file was able to resolved with one of the known extensions for the file',
filename
);
}
}
}
}

Expand Down

0 comments on commit fea7f36

Please sign in to comment.