Skip to content

Commit

Permalink
Failing test for uWebSockets read stream (#1143)
Browse files Browse the repository at this point in the history
Co-authored-by: Arda TANRIKULU <[email protected]>
  • Loading branch information
aarne and ardatan authored Feb 27, 2024
1 parent c861d12 commit 9958bb1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-rivers-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@whatwg-node/server": patch
---

Fix async handling of uWS
10 changes: 2 additions & 8 deletions packages/server/src/uwebsockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,17 @@ export function getRequestFromUWSRequest({ req, res, fetchAPI }: GetRequestFromU
res.onAborted(() => {
readable.push(null);
});
let multipleChunks = false;
res.onData(function (ab, isLast) {
const chunk = Buffer.from(ab, 0, ab.byteLength);
if (!multipleChunks && isLast) {
readable.push(chunk);
} else {
readable.push(Buffer.from(chunk));
}
readable.push(Buffer.from(chunk));
if (isLast) {
readable.push(null);
}
multipleChunks = true;
});
}
const headers = new fetchAPI.Headers();
req.forEach((key, value) => {
headers.set(key, value);
headers.append(key, value);
});
let url = `http://localhost${req.getUrl()}`;
const query = req.getQuery();
Expand Down
15 changes: 15 additions & 0 deletions packages/server/test/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ describe('Node Specific Cases', () => {
expect(await response.text()).toContain('This is an error.');
});

it('should handle async body read streams', async () => {
const serverAdapter = createServerAdapter(async request => {
await new Promise(resolve => setTimeout(resolve, 10));
const reqText = await request.text();
return new Response(reqText, { status: 200 });
});
testServer.addOnceHandler(serverAdapter);
const response = await fetch(testServer.url, {
method: 'POST',
body: 'Hello World',
});
expect(response.status).toBe(200);
expect(await response.text()).toContain('Hello World');
});

it('should respect the status code', async () => {
const serverAdapter = createServerAdapter(() => {
const error = new Error('This is an error.');
Expand Down

0 comments on commit 9958bb1

Please sign in to comment.