Skip to content

Commit

Permalink
fix: use event.handled guard before sending direct reponses (#1326)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Jun 20, 2023
1 parent 46e6f10 commit 34d9d1d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
8 changes: 6 additions & 2 deletions src/dev/error.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { setResponseStatus } from "h3";
import { H3Event, setResponseStatus } from "h3";
import { NitroErrorHandler } from "../types";

function errorHandler(error, event) {
function errorHandler(error: any, event: H3Event) {
event.node.res.setHeader("Content-Type", "text/html; charset=UTF-8");
setResponseStatus(event, 503, "Server Unavailable");

Expand All @@ -16,6 +16,10 @@ function errorHandler(error, event) {
"<progress></progress><script>document.querySelector('progress').indeterminate=true</script>";
}

if (event.handled) {
return;
}

event.node.res.end(`<!DOCTYPE html>
<html lang="en">
<head>
Expand Down
14 changes: 8 additions & 6 deletions src/runtime/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ export default <NitroErrorHandler>function (error, event) {

setResponseStatus(event, statusCode, statusMessage);

if (isJsonRequest(event)) {
event.node.res.setHeader("Content-Type", "application/json");
event.node.res.end(JSON.stringify(errorObject));
} else {
event.node.res.setHeader("Content-Type", "text/html");
event.node.res.end(renderHTMLError(errorObject));
if (!event.handled) {
if (isJsonRequest(event)) {
event.node.res.setHeader("Content-Type", "application/json");
event.node.res.end(JSON.stringify(errorObject));
} else {
event.node.res.setHeader("Content-Type", "text/html");
event.node.res.end(renderHTMLError(errorObject));
}
}
};

Expand Down
12 changes: 7 additions & 5 deletions src/runtime/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ export function defineRenderHandler(handler: RenderHandler) {
return eventHandler(async (event) => {
// TODO: Use serve-placeholder
if (event.node.req.url.endsWith("/favicon.ico")) {
event.node.res.setHeader("Content-Type", "image/x-icon");
event.node.res.end(
"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
);
if (!event.handled) {
event.node.res.setHeader("Content-Type", "image/x-icon");
event.node.res.end(
"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
);
}
return;
}

const response = await handler(event);
if (!response) {
if (!event.node.res.writableEnded) {
if (!event.handled) {
event.node.res.statusCode =
event.node.res.statusCode === 200 ? 500 : event.node.res.statusCode;
event.node.res.end(
Expand Down
12 changes: 8 additions & 4 deletions src/runtime/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ export default eventHandler((event) => {

const ifNotMatch = event.node.req.headers["if-none-match"] === asset.etag;
if (ifNotMatch) {
event.node.res.statusCode = 304;
event.node.res.end();
if (!event.handled) {
event.node.res.statusCode = 304;
event.node.res.end();
}
return;
}

Expand All @@ -73,8 +75,10 @@ export default eventHandler((event) => {
asset.mtime &&
new Date(ifModifiedSinceH) >= mtimeDate
) {
event.node.res.statusCode = 304;
event.node.res.end();
if (!event.handled) {
event.node.res.statusCode = 304;
event.node.res.end();
}
return;
}

Expand Down

0 comments on commit 34d9d1d

Please sign in to comment.