diff --git a/packages/astro/src/core/app/node.ts b/packages/astro/src/core/app/node.ts index ccb2068306dd..439d056da27e 100644 --- a/packages/astro/src/core/app/node.ts +++ b/packages/astro/src/core/app/node.ts @@ -99,30 +99,30 @@ export class NodeApp extends App { const { status, headers, body } = source; destination.writeHead(status, createOutgoingHttpHeaders(headers)); if (!body) return destination.end(); - try { - const reader = body.getReader(); - destination.on('close', () => { - // Cancelling the reader may reject not just because of - // an error in the ReadableStream's cancel callback, but - // also because of an error anywhere in the stream. - reader.cancel().catch((err) => { - // eslint-disable-next-line no-console - console.error( - `There was an uncaught error in the middle of the stream while rendering ${destination.req.url}.`, - err - ); - }); + try { + const reader = body.getReader(); + destination.on('close', () => { + // Cancelling the reader may reject not just because of + // an error in the ReadableStream's cancel callback, but + // also because of an error anywhere in the stream. + reader.cancel().catch((err) => { + // eslint-disable-next-line no-console + console.error( + `There was an uncaught error in the middle of the stream while rendering ${destination.req.url}.`, + err + ); }); - let result = await reader.read(); - while (!result.done) { - destination.write(result.value); - result = await reader.read(); - } - destination.end(); - // the error will be logged by the "on end" callback above - } catch { - destination.end('Internal server error'); + }); + let result = await reader.read(); + while (!result.done) { + destination.write(result.value); + result = await reader.read(); } + destination.end(); + // the error will be logged by the "on end" callback above + } catch { + destination.end('Internal server error'); + } } } diff --git a/packages/integrations/node/test/errors.test.js b/packages/integrations/node/test/errors.test.js index 8d54bcd4edf7..09fc9e7347c0 100644 --- a/packages/integrations/node/test/errors.test.js +++ b/packages/integrations/node/test/errors.test.js @@ -43,13 +43,11 @@ describe('Errors', () => { const chunk3 = await reader.read(); assert.equal(chunk1.done, false); if (chunk2.done) { - assert.equal(decoder.decode(chunk1.value), result.join("")); - } - else if (chunk3.done) { + assert.equal(decoder.decode(chunk1.value), result.join('')); + } else if (chunk3.done) { assert.equal(decoder.decode(chunk1.value), result[0]); assert.equal(decoder.decode(chunk2.value), result[1]); - } - else { + } else { throw new Error('The response should take at most 2 chunks.'); } });