Skip to content

Commit 4debe0c

Browse files
author
Sebastian Sauerer
committed
fix: safely parse resp payload
1 parent c32758c commit 4debe0c

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/lambda-invoker.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ export const invokeLambda = async (params: { payload: string, functionName: stri
2727
throw new Error(`Failed to invoke lambda ${params.functionName} synchronously`)
2828
}
2929

30-
if (hasTimeout(res)) {
31-
throw new Error(res.Payload?.toString())
30+
const safePayload = Buffer.from(res.Payload).toString('utf-8')
31+
32+
if (hasTimeout(res, safePayload)) {
33+
throw new Error(safePayload)
3234
}
3335

34-
const response = safeParseJson(res.Payload?.transformToString())
36+
const response = safeParseJson(safePayload)
3537

3638
// response is not in JSON format
3739
if (!isApiGwStructuredResp(response)) {
@@ -55,4 +57,4 @@ const isApiGwStructuredResp = (resp: unknown): resp is APIGatewayProxyStructured
5557
return true
5658
}
5759

58-
const hasTimeout = (res: InvocationResponse) => 'FunctionError' in res && res.Payload?.toString().includes('Task timed out')
60+
const hasTimeout = (res: InvocationResponse, safePayload: string) => 'FunctionError' in res && safePayload?.includes('Task timed out')

0 commit comments

Comments
 (0)