Skip to content

Commit

Permalink
Fixes binary data request bodies in the Node adapter (#4055)
Browse files Browse the repository at this point in the history
* Fixes binary data request bodies in the Node adapter

* Fix type
  • Loading branch information
matthewp authored Jul 26, 2022
1 parent 80e2e59 commit 8c3de73
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/integrations/node/test/api-route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
});
Original file line number Diff line number Diff line change
@@ -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'
}
});
}

0 comments on commit 8c3de73

Please sign in to comment.