Skip to content

Commit 6b04023

Browse files
author
Daniel Bot
committed
feat: added support for stopping infinite loop
1 parent 84deb0b commit 6b04023

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/correlation.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,17 @@ const increaseChainLength = (correlationIds: CorrelationIds): CorrelationIds =>
8484
'call-chain-length': (Number.parseInt(chainLength) + 1).toString(),
8585
};
8686
};
87+
88+
export const stopInfiniteLoop = (threshold: number = 10) => {
89+
return {
90+
before: async (handler: middy.Request) => {
91+
const len = Number.parseInt(useCorrelationIds()['call-chain-length'] || '1');
92+
if (len >= threshold) {
93+
const awsRequestId = handler.context.awsRequestId;
94+
const invocationEvent = JSON.stringify(handler.event);
95+
console.error('Possible infinite recursion detected, invocation is stopped.', { awsRequestId, invocationEvent, ...useCorrelationIds() });
96+
throw new Error(`'call-chain-length' reached threshold of ${threshold}, possible infinite recursion`);
97+
}
98+
},
99+
};
100+
};

0 commit comments

Comments
 (0)