From 096aa9898fbeee3732201835b522525c71e50d9a Mon Sep 17 00:00:00 2001 From: Sebastian Fastner Date: Mon, 11 Apr 2022 19:18:43 +0200 Subject: [PATCH] Support UInt8Array data in response (#232) --- lib/response.js | 2 +- test/spec.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/response.js b/lib/response.js index 20d3ec7..6215d83 100644 --- a/lib/response.js +++ b/lib/response.js @@ -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}`); diff --git a/test/spec.js b/test/spec.js index 34b448c..5828339 100644 --- a/test/spec.js +++ b/test/spec.js @@ -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); + }); + });