From 352990b62270923adbedef1a903bfde81249db11 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Tue, 26 Jul 2022 09:44:16 -0400 Subject: [PATCH 1/2] Fixes binary data request bodies in the Node adapter --- .changeset/slow-terms-repeat.md | 6 ++++++ packages/astro/src/core/app/node.ts | 11 ++--------- .../integrations/node/test/api-route.test.js | 16 ++++++++++++++++ .../test/fixtures/api-route/src/pages/binary.ts | 11 +++++++++++ 4 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 .changeset/slow-terms-repeat.md create mode 100644 packages/integrations/node/test/fixtures/api-route/src/pages/binary.ts diff --git a/.changeset/slow-terms-repeat.md b/.changeset/slow-terms-repeat.md new file mode 100644 index 000000000000..808556f5470d --- /dev/null +++ b/.changeset/slow-terms-repeat.md @@ -0,0 +1,6 @@ +--- +'astro': patch +'@astrojs/node': patch +--- + +Handle binary data request bodies in the Node adapter diff --git a/packages/astro/src/core/app/node.ts b/packages/astro/src/core/app/node.ts index cb5356f630b6..861e4b0e711d 100644 --- a/packages/astro/src/core/app/node.ts +++ b/packages/astro/src/core/app/node.ts @@ -28,17 +28,10 @@ export class NodeApp extends App { } render(req: IncomingMessage | Request) { if ('on' in req) { - let body: string | undefined = undefined; + let body = Buffer.from([]); let reqBodyComplete = new Promise((resolve, reject) => { req.on('data', (d) => { - if (body === undefined) { - body = ''; - } - if (d instanceof Buffer) { - body += d.toString('utf-8'); - } else if (typeof d === 'string') { - body += d; - } + body = Buffer.concat([body, d]); }); req.on('end', () => { resolve(body); diff --git a/packages/integrations/node/test/api-route.test.js b/packages/integrations/node/test/api-route.test.js index 034b53c07f53..cd074ef2737e 100644 --- a/packages/integrations/node/test/api-route.test.js +++ b/packages/integrations/node/test/api-route.test.js @@ -31,4 +31,20 @@ describe('API routes', () => { expect(json.length).to.equal(1); expect(json[0].name).to.equal('Broccoli Soup'); }); + + it('Can get binary data', async () => { + const { handler } = await import('./fixtures/api-route/dist/server/entry.mjs'); + + let { req, res, done } = createRequestAndResponse({ + method: 'POST', + url: '/binary', + }); + + handler(req, res); + req.send(Buffer.from(new Uint8Array([1, 2, 3, 4, 5]))); + + let [out] = await done; + let arr = Array.from(new Uint8Array(out.buffer)); + expect(arr).to.deep.equal([5, 4, 3, 2, 1]); + }); }); diff --git a/packages/integrations/node/test/fixtures/api-route/src/pages/binary.ts b/packages/integrations/node/test/fixtures/api-route/src/pages/binary.ts new file mode 100644 index 000000000000..6b50bc341e51 --- /dev/null +++ b/packages/integrations/node/test/fixtures/api-route/src/pages/binary.ts @@ -0,0 +1,11 @@ + +export async function post({ request }: { request: Request }) { + let body = await request.arrayBuffer(); + let data = new Uint8Array(body); + let r = data.reverse(); + return new Response(r, { + headers: { + 'Content-Type': 'application/octet-stream' + } + }); +} From 5e23e5a27cc921e838d801c6cae9072469954d9b Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Tue, 26 Jul 2022 09:50:28 -0400 Subject: [PATCH 2/2] Fix type --- packages/astro/src/core/app/node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/core/app/node.ts b/packages/astro/src/core/app/node.ts index 861e4b0e711d..c7e6f1ecad08 100644 --- a/packages/astro/src/core/app/node.ts +++ b/packages/astro/src/core/app/node.ts @@ -7,7 +7,7 @@ import { App } from './index.js'; const clientAddressSymbol = Symbol.for('astro.clientAddress'); -function createRequestFromNodeRequest(req: IncomingMessage, body?: string): Request { +function createRequestFromNodeRequest(req: IncomingMessage, body?: Uint8Array): Request { let url = `http://${req.headers.host}${req.url}`; let rawHeaders = req.headers as Record; const entries = Object.entries(rawHeaders);