Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: reduce usage of event.node.res #1513

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/dev/error.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import { H3Event, setResponseStatus } from "h3";
import {
H3Event,
setResponseHeader,
setResponseStatus,
getResponseStatus,
getResponseStatusText,
send,
} from "h3";
import { NitroErrorHandler } from "../types";

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

Check warning on line 12 in src/dev/error.ts

View check run for this annotation

Codecov / codecov/patch

src/dev/error.ts#L12

Added line #L12 was not covered by tests
setResponseStatus(event, 503, "Server Unavailable");

let body;
let title;
if (error) {
title = `${event.node.res.statusCode} ${event.node.res.statusMessage}`;
title = `${getResponseStatus(event)} ${getResponseStatusText(event)}`;

Check warning on line 18 in src/dev/error.ts

View check run for this annotation

Codecov / codecov/patch

src/dev/error.ts#L18

Added line #L18 was not covered by tests
body = `<code><pre>${error.stack}</pre></code>`;
} else {
title = "Reloading server...";
body =
"<progress></progress><script>document.querySelector('progress').indeterminate=true</script>";
}

if (event.handled) {
return;
}

event.node.res.end(`<!DOCTYPE html>
send(
event,
`<!DOCTYPE html>

Check warning on line 28 in src/dev/error.ts

View check run for this annotation

Codecov / codecov/patch

src/dev/error.ts#L26-L28

Added lines #L26 - L28 were not covered by tests
<html lang="en">
<head>
<meta charset="utf-8">
Expand All @@ -43,7 +48,8 @@
</main>
</body>
</html>
`);
`
);

Check warning on line 52 in src/dev/error.ts

View check run for this annotation

Codecov / codecov/patch

src/dev/error.ts#L51-L52

Added lines #L51 - L52 were not covered by tests
}

export default errorHandler as NitroErrorHandler;
16 changes: 7 additions & 9 deletions src/runtime/error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// import ansiHTML from 'ansi-html'
import { setResponseStatus } from "h3";
import { setResponseHeader, setResponseStatus, send } from "h3";
import type { NitroErrorHandler } from "../types";
import { normalizeError, isJsonRequest } from "./utils";

Expand Down Expand Up @@ -44,14 +44,12 @@ export default <NitroErrorHandler>function (error, event) {

setResponseStatus(event, statusCode, statusMessage);

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));
}
if (isJsonRequest(event)) {
setResponseHeader(event, "Content-Type", "application/json");
send(event, JSON.stringify(errorObject));
} else {
setResponseHeader(event, "Content-Type", "text/html");
send(event, renderHTMLError(errorObject));
}
};

Expand Down
43 changes: 24 additions & 19 deletions src/runtime/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { H3Event, eventHandler, setResponseStatus } from "h3";
import {
H3Event,
eventHandler,
getResponseStatus,
send,
setResponseHeader,
setResponseHeaders,
setResponseStatus,
} from "h3";
import { useNitroApp } from "./app";

export interface RenderResponse {
Expand All @@ -16,25 +24,22 @@ export function defineRenderHandler(handler: RenderHandler) {
return eventHandler(async (event) => {
// TODO: Use serve-placeholder
if (event.path.endsWith("/favicon.ico")) {
if (!event.handled) {
event.node.res.setHeader("Content-Type", "image/x-icon");
event.node.res.end(
"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
);
}
setResponseHeader(event, "Content-Type", "image/x-icon");
send(
event,
"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
);
return;
}

const response = await handler(event);
if (!response) {
if (!event.handled) {
event.node.res.statusCode =
event.node.res.statusCode === 200 ? 500 : event.node.res.statusCode;
event.node.res.end(
"No response returned from render handler: " + event.path
);
}
return;
const _currentStatus = getResponseStatus(event);
setResponseStatus(event, _currentStatus === 200 ? 500 : _currentStatus);
return send(
event,
"No response returned from render handler: " + event.path
);
}

// Allow hooking and modifying response
Expand All @@ -46,10 +51,10 @@ export function defineRenderHandler(handler: RenderHandler) {
// TODO: Caching support

// Send headers
if (!event.node.res.headersSent && response.headers) {
for (const header in response.headers) {
event.node.res.setHeader(header, response.headers[header]);
}
if (response.headers) {
setResponseHeaders(event, response.headers);
}
if (response.statusCode || response.statusMessage) {
setResponseStatus(event, response.statusCode, response.statusMessage);
}

Expand Down
47 changes: 26 additions & 21 deletions src/runtime/static.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { eventHandler, createError, getRequestHeader } from "h3";
import {
eventHandler,
createError,
getRequestHeader,
getResponseHeader,
setResponseHeader,
setResponseStatus,
removeResponseHeader,
send,
} from "h3";
import {
decodePath,
joinURL,
Expand Down Expand Up @@ -39,7 +48,7 @@ export default eventHandler((event) => {
"",
];
if (encodings.length > 1) {
event.node.res.setHeader("Vary", "Accept-Encoding");
setResponseHeader(event, "Vary", "Accept-Encoding");
}

for (const encoding of encodings) {
Expand All @@ -55,7 +64,7 @@ export default eventHandler((event) => {

if (!asset) {
if (isPublicAssetURL(id)) {
event.node.res.removeHeader("cache-control");
removeResponseHeader(event, "Cache-Control");
throw createError({
statusMessage: "Cannot find static asset " + id,
statusCode: 404,
Expand All @@ -66,10 +75,8 @@ export default eventHandler((event) => {

const ifNotMatch = getRequestHeader(event, "if-none-match") === asset.etag;
if (ifNotMatch) {
if (!event.handled) {
event.node.res.statusCode = 304;
event.node.res.end();
}
setResponseStatus(event, 304, "Not Modified");
send(event, "");
return;
}

Expand All @@ -80,31 +87,29 @@ export default eventHandler((event) => {
asset.mtime &&
new Date(ifModifiedSinceH) >= mtimeDate
) {
if (!event.handled) {
event.node.res.statusCode = 304;
event.node.res.end();
}
setResponseStatus(event, 304, "Not Modified");
send(event, "");
return;
}

if (asset.type && !event.node.res.getHeader("Content-Type")) {
event.node.res.setHeader("Content-Type", asset.type);
if (asset.type && !getResponseHeader(event, "Content-Type")) {
setResponseHeader(event, "Content-Type", asset.type);
}

if (asset.etag && !event.node.res.getHeader("ETag")) {
event.node.res.setHeader("ETag", asset.etag);
if (asset.etag && !getResponseHeader(event, "ETag")) {
setResponseHeader(event, "ETag", asset.etag);
}

if (asset.mtime && !event.node.res.getHeader("Last-Modified")) {
event.node.res.setHeader("Last-Modified", mtimeDate.toUTCString());
if (asset.mtime && !getResponseHeader(event, "Last-Modified")) {
setResponseHeader(event, "Last-Modified", mtimeDate.toUTCString());
}

if (asset.encoding && !event.node.res.getHeader("Content-Encoding")) {
event.node.res.setHeader("Content-Encoding", asset.encoding);
if (asset.encoding && !getResponseHeader(event, "Content-Encoding")) {
setResponseHeader(event, "Content-Encoding", asset.encoding);
}

if (asset.size > 0 && !event.node.res.getHeader("Content-Length")) {
event.node.res.setHeader("Content-Length", asset.size);
if (asset.size > 0 && !getResponseHeader(event, "Content-Length")) {
setResponseHeader(event, "Content-Length", asset.size);
}

return readAsset(id);
Expand Down