Skip to content

Commit

Permalink
Support UInt8Array data in response (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
fastner authored Apr 11, 2022
1 parent 7cc0ad7 commit 096aa98
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function getString(data) {
}

function addData(stream, data) {
if (Buffer.isBuffer(data) || typeof data === 'string') {
if (Buffer.isBuffer(data) || typeof data === 'string' || data instanceof Uint8Array) {
stream[BODY].push(Buffer.from(data));
} else {
throw new Error(`response.write() of unexpected type: ${typeof data}`);
Expand Down
18 changes: 18 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,22 @@ describe('spec', () => {
expect(response.headers).to.have.property('location', '/foo');
});

it('should support UInt8Array data', async () => {
const expected = "hello";

const uint8Array = Uint8Array.from(
Array.from(expected).map(
ch => ch.charCodeAt(0)
)
);

const handler = serverless((req, res) => {
res.end(uint8Array);
});

const response = await handler({});

expect(response.body).to.equal(expected);
});

});

0 comments on commit 096aa98

Please sign in to comment.