middyjs / middy

🛵 The stylish Node.js middleware engine for AWS Lambda 🛵

Home Page:https://middy.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The `inputOutputLogger` middleware fails if used with a streaming handler.

Panzki opened this issue · comments

Describe the bug
The inputOutputLogger middleware fails if used with a streaming handler. The problem is caused by the use of structuredClone for serialization in the middleware. It was introduced in this commit. The problem occurs if the following conditions are met:

  1. The handler uses streamifyResponse: true and returns a stream (Readable).
  2. The inputOutputLogger middleware is used and configured to omit properties.

When invoking structuredClone on the response containing a Readable body, a DataClone error is thrown. This is caused by the body containing properties of type function which can not be serializes by the structured clone algorithm, see here.

The replacer function that can be passed to the inputOutputLogger middleware gets ignored. It looks like this is a leftover from a previous implementation that was using a JSON parse approach for cloning the object.

To Reproduce
How to reproduce the behaviour:

  1. Create a handler with streamifyResponse: true and return a stream/Readable as body.
  2. Use the inputOutputLogger middleware and configure at least one property path to omit.
  3. Execute the handler.

Expected behaviour
No error is thrown and the response is properly logged.

Environment (please complete the following information):

  • Node.js: 18.x
  • Middy: 5.1.0
  • AWS SDK 3.405.0

Thanks for reporting. This was definitely missed when we added in stream support. I'll put a PR together for this.

PR created, Can you give it a review before I merge it in?