Skip to content

Bug: HTTP Streaming Handlers Do Not Allow Duplex Streams To Be Returned #4626

@svozza

Description

@svozza

Expected Behavior

When a user creates a handler that returns a stream, we do a check to ensure that it is a readable stream using instanceOf:

However, a Duplex stream is also readable but it fails the the type check above.

Current Behavior

If you create a streaming handler that returns a proxy response with a Duplex stream as the body, it get passed to JSON.stringify rather than be read as a stream.

Code snippet

Example unit test:

import context from '@aws-lambda-powertools/testing-utils/context';
import {
  Router,
} from '../../../../src/rest/index.js';
import { createTestEvent } from '../helpers.js';

const app = new Router();

app.get('/stream', async () => {
    const stream = createDuplexStream();

    return {
        headers: {
            'content-type': 'application/json',
        },
        statusCode: 200,
        body: createDuplexStream
    }
});

const result = app.resolve(createTestEvent('/stream', 'GET'), context);

// returns
// {"_events":{},"_readableState":{"highWaterMark":65536,"buffer":[],"bufferIndex":0,"length":0, "pipes":[],"awaitDrainWriters":null},
//"_writableState" {"highWaterMark":65536,"length":0,"corked":0,"writelen":0,"bufferedIndex":0,"pendingcb":0},"allowHalfOpen":true}

Steps to Reproduce

  1. Create an HTTP event handler to return a stream as in the code example above.
  2. Invoke the handler.
  3. Message returned is a JSONified version of the stream rather than reading it.

Possible Solution

Change the type guard instanceof check to also check for Duplex instances:

(value instanceof Readable || value instanceof Duplex)

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

22.x

Packaging format used

npm

Execution logs

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingevent-handlerThis item relates to the Event Handler Utilitygood-first-issueSomething that is suitable for those who want to start contributingpending-releaseThis item has been merged and will be released soon

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions