From 980d924989a18e84e8f7b9885fad13518727c11a Mon Sep 17 00:00:00 2001 From: AllanZhengYP Date: Fri, 3 Jun 2022 18:54:32 +0000 Subject: [PATCH] fix(middleware-recursion-detection): address feedbacks --- .../src/index.ts | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/packages/middleware-recursion-detection/src/index.ts b/packages/middleware-recursion-detection/src/index.ts index e346e6d763b4..e654334848b1 100644 --- a/packages/middleware-recursion-detection/src/index.ts +++ b/packages/middleware-recursion-detection/src/index.ts @@ -22,30 +22,30 @@ interface PreviouslyResolved { * Inject to trace ID to request header to detect recursion invocation in Lambda. * @internal */ -export function recursionDetectionMiddleware(options: PreviouslyResolved): BuildMiddleware { - return (next: BuildHandler): BuildHandler => - async (args: BuildHandlerArguments): Promise> => { - const { request } = args; - if (!HttpRequest.isInstance(request) || options.runtime !== "node") { - return next(args); - } +export const recursionDetectionMiddleware = + (options: PreviouslyResolved): BuildMiddleware => + (next: BuildHandler): BuildHandler => + async (args: BuildHandlerArguments): Promise> => { + const { request } = args; + if ( + !HttpRequest.isInstance(request) || + options.runtime !== "node" || + request.headers.hasOwnProperty(TRACE_ID_HEADER_NAME) + ) { + return next(args); + } - const functionName = process.env[ENV_LAMBDA_FUNCTION_NAME]; - const traceId = process.env[ENV_TRACE_ID]; - const nonEmptyString = (str: unknown): str is string => typeof str === "string" && str.length > 0; - if ( - !request.headers.hasOwnProperty(TRACE_ID_HEADER_NAME) && - nonEmptyString(functionName) && - nonEmptyString(traceId) - ) { - request.headers[TRACE_ID_HEADER_NAME] = traceId; - } - return next({ - ...args, - request, - }); - }; -} + const functionName = process.env[ENV_LAMBDA_FUNCTION_NAME]; + const traceId = process.env[ENV_TRACE_ID]; + const nonEmptyString = (str: unknown): str is string => typeof str === "string" && str.length > 0; + if (nonEmptyString(functionName) && nonEmptyString(traceId)) { + request.headers[TRACE_ID_HEADER_NAME] = traceId; + } + return next({ + ...args, + request, + }); + }; // @internal export const addRecursionDetectionMiddlewareOptions: BuildHandlerOptions & AbsoluteLocation = {