Skip to content

Commit

Permalink
fix(server): do not terminate the server if uWS and node-http are not…
Browse files Browse the repository at this point in the history
… able to receive the response (#732)
  • Loading branch information
ardatan committed Aug 3, 2023
1 parent 03ad492 commit 0794ee5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changeset/angry-planes-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@whatwg-node/server': patch
---

If the environment is not able to send the response, do not terminate the server and handle internal
errors in a better way
42 changes: 28 additions & 14 deletions packages/server/src/createServerAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,18 @@ function createServerAdapter<
addWaitUntil(defaultServerContext, waitUntilPromises);
return handleNodeRequest(nodeRequest, defaultServerContext as any, ...ctx)
.then(response => {
if (response) {
return sendNodeResponse(response, serverResponse, nodeRequest);
}
return new Promise<void>(resolve => {
try {
if (serverResponse.closed || serverResponse.destroyed) {
return;
}
if (response) {
return sendNodeResponse(response, serverResponse, nodeRequest);
}
serverResponse.statusCode = 404;
serverResponse.once('end', resolve);
serverResponse.end();
});
} catch (e: any) {
console.error(`Unexpected error: ${e.message || e}`);
}
})
.finally(() => {
if (waitUntilPromises.length > 0) {
Expand All @@ -202,17 +206,27 @@ function createServerAdapter<
res,
fetchAPI,
});
let resAborted = false;
res.onAborted(() => {
resAborted = true;
});
return handleRequest(request, serverContext).then(response => {
if (!response) {
res.writeStatus('404 Not Found');
res.end();
if (resAborted) {
return;
}

return sendResponseToUwsOpts({
response,
res,
});
try {
if (!response) {
res.writeStatus('404 Not Found');
res.end();
return;
}
return sendResponseToUwsOpts({
response,
res,
});
} catch (e: any) {
console.error(`Unexpected error: ${e.message || e}`);
}
});
}

Expand Down
16 changes: 16 additions & 0 deletions packages/server/test/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Http2ServerResponse,
} from 'http2';
import { AddressInfo } from 'net';
import { HttpResponse } from 'uWebSockets.js';
import { fetch, ReadableStream, Response } from '@whatwg-node/fetch';
import { createServerAdapter } from '@whatwg-node/server';
import { runTestsForEachServerImpl } from './test-server.js';
Expand Down Expand Up @@ -99,6 +100,21 @@ describe('Node Specific Cases', () => {
await sleep(100);
expect(cancelFn).toHaveBeenCalledTimes(1);
});

it('should not kill the server if response is ended on low level', async () => {
const serverAdapter = createServerAdapter<{
res: HttpResponse | ServerResponse;
}>((_req, { res }) => {
res.end('This should reach the client.');
return new Response('This should never reach the client.', {
status: 200,
});
});
testServer.addOnceHandler(serverAdapter);
const response = await fetch(testServer.url);
const resText = await response.text();
expect(resText).toBe('This should reach the client.');
});
});
});

Expand Down

0 comments on commit 0794ee5

Please sign in to comment.