-
Notifications
You must be signed in to change notification settings - Fork 634
chore(middleware-recursion-detection): add no-op middleware outside of Node.js #7326
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
Merged
+116
−97
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e79c761
chore(middleware-recursion-detection): split into modular components
trivikr f894e15
chore: add no-op middleware outside of Node.js
trivikr 4e27682
chore: explcitly use '.browser' and '.native'
trivikr 9a59e2c
fix: add options in getRecursionDetectionPlugin
trivikr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/middleware-recursion-detection/src/configuration.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { AbsoluteLocation, BuildHandlerOptions } from "@smithy/types"; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const recursionDetectionMiddlewareOptions: BuildHandlerOptions & AbsoluteLocation = { | ||
step: "build", | ||
tags: ["RECURSION_DETECTION"], | ||
name: "recursionDetectionMiddleware", | ||
override: true, | ||
priority: "low", | ||
}; |
15 changes: 15 additions & 0 deletions
15
packages/middleware-recursion-detection/src/getRecursionDetectionPlugin.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Pluggable } from "@smithy/types"; | ||
|
||
import { recursionDetectionMiddlewareOptions } from "./configuration"; | ||
import { recursionDetectionMiddleware } from "./recursionDetectionMiddleware"; | ||
|
||
// @internal | ||
/** | ||
* @internal | ||
*/ | ||
|
||
export const getRecursionDetectionPlugin = (): Pluggable<any, any> => ({ | ||
applyToStack: (clientStack) => { | ||
clientStack.add(recursionDetectionMiddleware(), recursionDetectionMiddlewareOptions); | ||
}, | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,2 @@ | ||
import { HttpRequest } from "@smithy/protocol-http"; | ||
import { | ||
AbsoluteLocation, | ||
BuildHandler, | ||
BuildHandlerArguments, | ||
BuildHandlerOptions, | ||
BuildHandlerOutput, | ||
BuildMiddleware, | ||
MetadataBearer, | ||
Pluggable, | ||
} from "@smithy/types"; | ||
|
||
const TRACE_ID_HEADER_NAME = "X-Amzn-Trace-Id"; | ||
const ENV_LAMBDA_FUNCTION_NAME = "AWS_LAMBDA_FUNCTION_NAME"; | ||
const ENV_TRACE_ID = "_X_AMZN_TRACE_ID"; | ||
|
||
interface PreviouslyResolved { | ||
runtime: string; | ||
} | ||
|
||
/** | ||
* Inject to trace ID to request header to detect recursion invocation in Lambda. | ||
* @internal | ||
*/ | ||
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") { | ||
return next(args); | ||
} | ||
const traceIdHeader = | ||
Object.keys(request.headers ?? {}).find((h) => h.toLowerCase() === TRACE_ID_HEADER_NAME.toLowerCase()) ?? | ||
TRACE_ID_HEADER_NAME; | ||
|
||
if (request.headers.hasOwnProperty(traceIdHeader)) { | ||
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 (nonEmptyString(functionName) && nonEmptyString(traceId)) { | ||
request.headers[TRACE_ID_HEADER_NAME] = traceId; | ||
} | ||
return next({ | ||
...args, | ||
request, | ||
}); | ||
}; | ||
|
||
// @internal | ||
/** | ||
* @internal | ||
*/ | ||
export const addRecursionDetectionMiddlewareOptions: BuildHandlerOptions & AbsoluteLocation = { | ||
step: "build", | ||
tags: ["RECURSION_DETECTION"], | ||
name: "recursionDetectionMiddleware", | ||
override: true, | ||
priority: "low", | ||
}; | ||
|
||
// @internal | ||
/** | ||
* @internal | ||
*/ | ||
export const getRecursionDetectionPlugin = (options: PreviouslyResolved): Pluggable<any, any> => ({ | ||
applyToStack: (clientStack) => { | ||
clientStack.add(recursionDetectionMiddleware(options), addRecursionDetectionMiddlewareOptions); | ||
}, | ||
}); | ||
export * from "./getRecursionDetectionPlugin"; | ||
export * from "./recursionDetectionMiddleware"; |
17 changes: 17 additions & 0 deletions
17
packages/middleware-recursion-detection/src/recursionDetectionMiddleware.no-op.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { | ||
BuildHandler, | ||
BuildHandlerArguments, | ||
BuildHandlerOutput, | ||
BuildMiddleware, | ||
MetadataBearer, | ||
} from "@smithy/types"; | ||
|
||
/** | ||
* No-op middleware for runtimes outside of Node.js | ||
* @internal | ||
*/ | ||
export const recursionDetectionMiddleware = | ||
(): BuildMiddleware<any, any> => | ||
<Output extends MetadataBearer>(next: BuildHandler<any, Output>): BuildHandler<any, Output> => | ||
async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => | ||
next(args); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/middleware-recursion-detection/src/recursionDetectionMiddleware.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { HttpRequest } from "@smithy/protocol-http"; | ||
import { | ||
BuildHandler, | ||
BuildHandlerArguments, | ||
BuildHandlerOutput, | ||
BuildMiddleware, | ||
MetadataBearer, | ||
} from "@smithy/types"; | ||
|
||
const TRACE_ID_HEADER_NAME = "X-Amzn-Trace-Id"; | ||
const ENV_LAMBDA_FUNCTION_NAME = "AWS_LAMBDA_FUNCTION_NAME"; | ||
const ENV_TRACE_ID = "_X_AMZN_TRACE_ID"; | ||
|
||
/** | ||
* Inject to trace ID to request header to detect recursion invocation in Lambda. | ||
* @internal | ||
*/ | ||
export const recursionDetectionMiddleware = | ||
(): 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)) { | ||
return next(args); | ||
} | ||
const traceIdHeader = | ||
Object.keys(request.headers ?? {}).find((h) => h.toLowerCase() === TRACE_ID_HEADER_NAME.toLowerCase()) ?? | ||
TRACE_ID_HEADER_NAME; | ||
|
||
if (request.headers.hasOwnProperty(traceIdHeader)) { | ||
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 (nonEmptyString(functionName) && nonEmptyString(traceId)) { | ||
request.headers[TRACE_ID_HEADER_NAME] = traceId; | ||
} | ||
return next({ | ||
...args, | ||
request, | ||
}); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.