Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support UInt8Array data in response #232

Merged
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
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);
});

});