@@ -817,18 +817,34 @@ function isLambdaExecutionEnvironment() {
817
817
// .mjs file extension (which indicates an ECMAScript/import module, which the
818
818
// agent does not support.
819
819
//
820
- // @param string taskRoot
821
- // @param string handlerModule
822
- // @return string
823
- function getFilePath ( taskRoot , handlerModule ) {
824
- let filePath = path . resolve ( taskRoot , `${ handlerModule } .js` ) ;
825
- if ( ! fs . existsSync ( filePath ) ) {
826
- filePath = path . resolve ( taskRoot , `${ handlerModule } .cjs` ) ;
820
+ // TODO: support "extensionless"? per https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/blob/v3.2.1/src/UserFunction.js#L149 Is this for a dir/index.js?
821
+ // TODO: support ESM and .mjs
822
+ //
823
+ // @param {string } taskRoot
824
+ // @param {string } moduleRoot - The subdir under `taskRoot` holding the module.
825
+ // @param {string } module - The module name.
826
+ // @return {string | null }
827
+ function getFilePath ( taskRoot , moduleRoot , module ) {
828
+ const lambdaStylePath = path . resolve ( taskRoot , moduleRoot , module ) ;
829
+ if ( fs . existsSync ( lambdaStylePath + '.js' ) ) {
830
+ return lambdaStylePath + '.js' ;
831
+ } else if ( fs . existsSync ( lambdaStylePath + '.cjs' ) ) {
832
+ return lambdaStylePath + '.cjs' ;
833
+ } else {
834
+ return null ;
827
835
}
828
- return filePath ;
829
836
}
830
837
831
- function getLambdaHandlerInfo ( env ) {
838
+ /**
839
+ * Gather module and export info for the Lambda "handler" string.
840
+ *
841
+ * Compare to the Node.js Lambda runtime's equivalent processing here:
842
+ * https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/blob/v3.2.1/src/UserFunction.js#L288
843
+ *
844
+ * @param {object } env - The process environment.
845
+ * @param {any } [logger] - Optional logger for trace/warn log output.
846
+ */
847
+ function getLambdaHandlerInfo ( env , logger ) {
832
848
if (
833
849
! isLambdaExecutionEnvironment ( ) ||
834
850
! env . _HANDLER ||
@@ -837,22 +853,48 @@ function getLambdaHandlerInfo(env) {
837
853
return null ;
838
854
}
839
855
840
- // extract module name and "path" from handler using the same regex as the runtime
841
- // from https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/blob/c31c41ffe5f2f03ae9e8589b96f3b005e2bb8a4a/src/utils/UserFunction.ts#L21
842
- const functionExpression = / ^ ( [ ^ . ] * ) \. ( .* ) $ / ;
843
- const match = env . _HANDLER . match ( functionExpression ) ;
856
+ // Dev Note: This intentionally uses some of the same var names at
857
+ // https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/blob/v3.2.1/src/UserFunction.js#L288
858
+ const fullHandlerString = env . _HANDLER ;
859
+ const moduleAndHandler = path . basename ( fullHandlerString ) ;
860
+ const moduleRoot = fullHandlerString . substring (
861
+ 0 ,
862
+ fullHandlerString . indexOf ( moduleAndHandler ) ,
863
+ ) ;
864
+ const FUNCTION_EXPR = / ^ ( [ ^ . ] * ) \. ( .* ) $ / ;
865
+ const match = moduleAndHandler . match ( FUNCTION_EXPR ) ;
844
866
if ( ! match || match . length !== 3 ) {
867
+ if ( logger ) {
868
+ logger . warn (
869
+ { fullHandlerString, moduleAndHandler } ,
870
+ 'Lambda handler string did not match FUNCTION_EXPR' ,
871
+ ) ;
872
+ }
873
+ return null ;
874
+ }
875
+ const module = match [ 1 ] ;
876
+ const handlerPath = match [ 2 ] ;
877
+
878
+ const moduleAbsPath = getFilePath ( env . LAMBDA_TASK_ROOT , moduleRoot , module ) ;
879
+ if ( ! moduleAbsPath ) {
880
+ if ( logger ) {
881
+ logger . warn (
882
+ { fullHandlerString, moduleRoot, module } ,
883
+ 'could not find Lambda handler module file (ESM not yet supported)' ,
884
+ ) ;
885
+ }
845
886
return null ;
846
887
}
847
- const handlerModule = match [ 1 ] . split ( '/' ) . pop ( ) ;
848
- const handlerFunctionPath = match [ 2 ] ;
849
- const handlerFilePath = getFilePath ( env . LAMBDA_TASK_ROOT , match [ 1 ] ) ;
850
888
851
- return {
852
- filePath : handlerFilePath ,
853
- modName : handlerModule ,
854
- propPath : handlerFunctionPath ,
889
+ const lambdaHandlerInfo = {
890
+ filePath : moduleAbsPath ,
891
+ modName : module ,
892
+ propPath : handlerPath ,
855
893
} ;
894
+ if ( logger ) {
895
+ logger . trace ( { fullHandlerString, lambdaHandlerInfo } , 'lambdaHandlerInfo' ) ;
896
+ }
897
+ return lambdaHandlerInfo ;
856
898
}
857
899
858
900
function lowerCaseObjectKeys ( obj ) {
0 commit comments