This repository has been archived on 2024-09-11. You can view files and clone it, but cannot push or open issues or pull requests.
svrjs-blog-newsletter/cronjob/node_modules/@aws-sdk/middleware-recursion-detection/dist-es/index.js

35 lines
1.3 KiB
JavaScript
Raw Normal View History

2024-05-26 22:54:55 +02:00
import { HttpRequest } from "@smithy/protocol-http";
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";
export const recursionDetectionMiddleware = (options) => (next) => async (args) => {
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) => typeof str === "string" && str.length > 0;
if (nonEmptyString(functionName) && nonEmptyString(traceId)) {
request.headers[TRACE_ID_HEADER_NAME] = traceId;
}
return next({
...args,
request,
});
};
export const addRecursionDetectionMiddlewareOptions = {
step: "build",
tags: ["RECURSION_DETECTION"],
name: "recursionDetectionMiddleware",
override: true,
priority: "low",
};
export const getRecursionDetectionPlugin = (options) => ({
applyToStack: (clientStack) => {
clientStack.add(recursionDetectionMiddleware(options), addRecursionDetectionMiddlewareOptions);
},
});