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
4 changes: 2 additions & 2 deletions packages/event-handler/src/rest/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Readable, Writable } from 'node:stream';
import { Duplex, Readable, Writable } from 'node:stream';
import {
isRecord,
isRegExp,
Expand Down Expand Up @@ -112,7 +112,7 @@ export const isNodeReadableStream = (value: unknown): value is Readable => {
return (
value != null &&
typeof value === 'object' &&
value instanceof Readable &&
(value instanceof Readable || value instanceof Duplex) &&
'readable' in value &&
'read' in value &&
typeof value.read === 'function'
Expand Down
27 changes: 26 additions & 1 deletion packages/event-handler/tests/unit/rest/Router/streaming.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Readable } from 'node:stream';
import { Duplex, PassThrough, Readable } from 'node:stream';
import context from '@aws-lambda-powertools/testing-utils/context';
import { describe, expect, it, vi } from 'vitest';
import { UnauthorizedError } from '../../../../src/rest/errors.js';
Expand Down Expand Up @@ -306,4 +306,29 @@ describe('Class: Router - Streaming', () => {
app.resolveStream(invalidEvent, context, { responseStream })
).rejects.toThrow();
});

it('handles duplex stream body', async () => {
// Prepare
const app = new Router();
const passThrough = new PassThrough();
passThrough.write(Buffer.from('{"message":"duplex stream body"}'));
passThrough.end();

app.get('/test', () => ({
statusCode: 200,
body: Duplex.from(passThrough),
}));

const responseStream = new MockResponseStream();

// Act
await app.resolveStream(createTestEvent('/test', 'GET'), context, {
responseStream,
});

// Assess
const { prelude, body } = parseStreamOutput(responseStream.chunks);
expect(prelude.statusCode).toBe(200);
expect(JSON.parse(body)).toEqual({ message: 'duplex stream body' });
});
});