Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow AWS Lambda handler resolution for nodejs. #204

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ function getHandlerPath() {
)
}

const handlerToWrap = parts[parts.length - 1]
const moduleToImport = handler.slice(0, handler.lastIndexOf('.'))
const handlerToWrap = parts.slice(1).join('.')
const moduleToImport = handler.slice(0, handler.indexOf('.'))
return { moduleToImport, handlerToWrap }
}

function resolveHandler(object, nestedProperty) {
return nestedProperty.split('.').reduce((nested, key) => {
return nested && nested[key]
}, object)
}

function handleRequireImportError(e, moduleToImport) {
if (e.code === 'MODULE_NOT_FOUND') {
return new Error(`Unable to import module '${moduleToImport}'`)
Expand Down Expand Up @@ -117,14 +123,16 @@ if (process.env.NEW_RELIC_USE_ESM === 'true') {
}

async function getHandler() {
const userHandler = (await getModuleWithImport(LAMBDA_TASK_ROOT, moduleToImport))[handlerToWrap]
const userApp = await getModuleWithImport(LAMBDA_TASK_ROOT, moduleToImport)
const userHandler = resolveHandler(userApp, handlerToWrap)
validateHandlerDefinition(userHandler, handlerToWrap, moduleToImport)

return userHandler
}

function getHandlerSync() {
const userHandler = getModuleWithRequire(LAMBDA_TASK_ROOT, moduleToImport)[handlerToWrap]
const userApp = getModuleWithRequire(LAMBDA_TASK_ROOT, moduleToImport)
const userHandler = resolveHandler(userApp, handlerToWrap)
validateHandlerDefinition(userHandler, handlerToWrap, moduleToImport)

return userHandler
Expand Down