Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/middleware-recursion-detection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/types": "*",
"@aws/lambda-invoke-store": "^0.1.1",
"@aws/lambda-invoke-store": "^0.2.0",
"@smithy/protocol-http": "^5.3.5",
"@smithy/types": "^4.9.0",
"tslib": "^2.6.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
// @ts-ignore
import { InvokeStore as InvokeStoreImpl } from "@aws/lambda-invoke-store";
// eslint-disable-next-line no-restricted-imports
import type { InvokeStore as InvokeStoreType } from "@aws/lambda-invoke-store/dist-types/invoke-store.d";
import { InvokeStore } from "@aws/lambda-invoke-store";
import { HttpRequest } from "@smithy/protocol-http";
import { afterAll, beforeEach, describe, expect, test as it, vi } from "vitest";

import { recursionDetectionMiddleware } from "./recursionDetectionMiddleware";

const InvokeStore = InvokeStoreImpl as typeof InvokeStoreType;

describe(recursionDetectionMiddleware.name, () => {
const mockNextHandler = vi.fn();
const originEnv = process.env;
Expand All @@ -17,7 +13,7 @@ describe(recursionDetectionMiddleware.name, () => {

beforeEach(() => {
vi.clearAllMocks();
vi.spyOn(InvokeStore, "getXRayTraceId").mockImplementation(() => undefined);
vi.spyOn(InvokeStore, "getInstanceAsync").mockResolvedValue(undefined as any);
process.env = {};
});

Expand Down Expand Up @@ -46,7 +42,9 @@ describe(recursionDetectionMiddleware.name, () => {
});

it("trace id value is set in InvokeStore", async () => {
vi.spyOn(InvokeStore, "getXRayTraceId").mockImplementation(() => mockTraceIdInvokeStore);
vi.spyOn(InvokeStore, "getInstanceAsync").mockResolvedValue({
getXRayTraceId: () => mockTraceIdInvokeStore,
} as any);
process.env = {
AWS_LAMBDA_FUNCTION_NAME: "some-function",
};
Expand All @@ -62,7 +60,9 @@ describe(recursionDetectionMiddleware.name, () => {
});

it("favors trace id value from InvokeStore over that from env variable", async () => {
vi.spyOn(InvokeStore, "getXRayTraceId").mockImplementation(() => mockTraceIdInvokeStore);
vi.spyOn(InvokeStore, "getInstanceAsync").mockResolvedValue({
getXRayTraceId: () => mockTraceIdInvokeStore,
} as any);
process.env = {
AWS_LAMBDA_FUNCTION_NAME: "some-function",
_X_AMZN_TRACE_ID: mockTraceIdEnv,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// @ts-ignore
import { InvokeStore } from "@aws/lambda-invoke-store";
// eslint-disable-next-line no-restricted-imports
import type { InvokeStore as InvokeStoreType } from "@aws/lambda-invoke-store/dist-types/invoke-store.d";
import { HttpRequest } from "@smithy/protocol-http";
import {
BuildHandler,
Expand Down Expand Up @@ -37,7 +35,8 @@ export const recursionDetectionMiddleware =
const functionName = process.env[ENV_LAMBDA_FUNCTION_NAME];

const traceIdFromEnv = process.env[ENV_TRACE_ID];
const traceIdFromInvokeStore = (InvokeStore as typeof InvokeStoreType).getXRayTraceId();
const invokeStore = await InvokeStore.getInstanceAsync();
const traceIdFromInvokeStore = invokeStore?.getXRayTraceId();
const traceId = traceIdFromInvokeStore ?? traceIdFromEnv;

const nonEmptyString = (str: unknown): str is string => typeof str === "string" && str.length > 0;
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23448,7 +23448,7 @@ __metadata:
resolution: "@aws-sdk/middleware-recursion-detection@workspace:packages/middleware-recursion-detection"
dependencies:
"@aws-sdk/types": "npm:*"
"@aws/lambda-invoke-store": "npm:^0.1.1"
"@aws/lambda-invoke-store": "npm:^0.2.0"
"@smithy/protocol-http": "npm:^5.3.5"
"@smithy/types": "npm:^4.9.0"
"@tsconfig/recommended": "npm:1.0.1"
Expand Down Expand Up @@ -24371,10 +24371,10 @@ __metadata:
languageName: node
linkType: hard

"@aws/lambda-invoke-store@npm:^0.1.1":
version: 0.1.1
resolution: "@aws/lambda-invoke-store@npm:0.1.1"
checksum: 10c0/27c90d9af7cca7ff4870e87dc303516e6d09ebe18f5fa13813397cd6a37fd26cf3ff1715469e3c5323fea0404a55c110f35e21bcc3ea595a4f6ba6406ea1f103
"@aws/lambda-invoke-store@npm:^0.2.0":
version: 0.2.0
resolution: "@aws/lambda-invoke-store@npm:0.2.0"
checksum: 10c0/ff37fe5d7c3b59afffa4591f9be89c5b53b808fe9dd85dd02d324bbd2b8f11366de481569284b9a5b65221cf17ba254730085aa00d6a3d1a57d9ab08a3a39879
languageName: node
linkType: hard

Expand Down
Loading