@@ -14,6 +14,13 @@ const newrelic = require('newrelic')
1414const fs = require ( 'node:fs' )
1515const path = require ( 'node:path' )
1616
17+ function getNestedHandler ( object , nestedProperty ) {
18+ return nestedProperty . split ( '.' ) . reduce ( ( nested , key ) => {
19+ return nested && nested [ key ]
20+ } , object )
21+ }
22+
23+
1724function getHandlerPath ( ) {
1825 let handler
1926 const { NEW_RELIC_LAMBDA_HANDLER } = process . env
@@ -32,9 +39,12 @@ function getHandlerPath() {
3239 )
3340 }
3441
35- const handlerToWrap = parts [ parts . length - 1 ]
36- const moduleToImport = handler . slice ( 0 , handler . lastIndexOf ( '.' ) )
37- return { moduleToImport, handlerToWrap }
42+ const lastSlashIndex = handler . lastIndexOf ( '/' ) + 1
43+ const firstDotAfterSlash = handler . indexOf ( '.' , lastSlashIndex )
44+ const moduleToImport = handler . slice ( 0 , firstDotAfterSlash )
45+ const handlerToWrap = handler . slice ( firstDotAfterSlash + 1 )
46+
47+ return { moduleToImport, handlerToWrap}
3848}
3949
4050function handleRequireImportError ( e , moduleToImport ) {
@@ -117,14 +127,17 @@ if (process.env.NEW_RELIC_USE_ESM === 'true') {
117127}
118128
119129async function getHandler ( ) {
120- const userHandler = ( await getModuleWithImport ( LAMBDA_TASK_ROOT , moduleToImport ) ) [ handlerToWrap ]
130+ const userModule = await getModuleWithImport ( LAMBDA_TASK_ROOT , moduleToImport )
131+ const userHandler = getNestedHandler ( userModule , handlerToWrap )
132+
121133 validateHandlerDefinition ( userHandler , handlerToWrap , moduleToImport )
122134
123135 return userHandler
124136}
125137
126138function getHandlerSync ( ) {
127- const userHandler = getModuleWithRequire ( LAMBDA_TASK_ROOT , moduleToImport ) [ handlerToWrap ]
139+ const userModule = getModuleWithRequire ( LAMBDA_TASK_ROOT , moduleToImport )
140+ const userHandler = getNestedHandler ( userModule , handlerToWrap )
128141 validateHandlerDefinition ( userHandler , handlerToWrap , moduleToImport )
129142
130143 return userHandler
0 commit comments