Skip to content

Commit

Permalink
fix(middleware-recursion-detection): address feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP committed Jun 3, 2022
1 parent 66f0f42 commit 980d924
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions packages/middleware-recursion-detection/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any, any> {
return <Output extends MetadataBearer>(next: BuildHandler<any, Output>): BuildHandler<any, Output> =>
async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => {
const { request } = args;
if (!HttpRequest.isInstance(request) || options.runtime !== "node") {
return next(args);
}
export const recursionDetectionMiddleware =
(options: PreviouslyResolved): BuildMiddleware<any, any> =>
<Output extends MetadataBearer>(next: BuildHandler<any, Output>): BuildHandler<any, Output> =>
async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => {
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 = {
Expand Down

0 comments on commit 980d924

Please sign in to comment.