From 1ef19b7319d049af362bad03746eefd78c5ae5de Mon Sep 17 00:00:00 2001 From: Ian Mark Muninio Date: Mon, 5 Feb 2024 12:37:26 +0800 Subject: [PATCH] Follow AWS Lambda handler resolving. --- nodejs/index.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/nodejs/index.js b/nodejs/index.js index 3a791e5..0c145ef 100644 --- a/nodejs/index.js +++ b/nodejs/index.js @@ -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}'`) @@ -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