Skip to content

Commit

Permalink
refactor(event): use sendWebResponse for event.respondWith (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Aug 1, 2023
1 parent bc202c0 commit 21ed8d1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 54 deletions.
54 changes: 5 additions & 49 deletions src/event/event.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import type { IncomingHttpHeaders } from "node:http";
import type { H3EventContext, HTTPMethod, EventHandlerRequest } from "../types";
import type { NodeIncomingMessage, NodeServerResponse } from "../node";
import {
MIMES,
getRequestURL,
sanitizeStatusCode,
sanitizeStatusMessage,
} from "../utils";
import { H3Response } from "./response";
import { getRequestURL, sendWebResponse } from "../utils";

// TODO: Dedup from body.ts
const PayloadMethods: Set<HTTPMethod> = new Set([
Expand Down Expand Up @@ -135,48 +129,10 @@ export class H3Event<
return this._request;
}

// Implementation of FetchEvent
respondWith(r: H3Response | PromiseLike<H3Response>): void {
Promise.resolve(r).then((_response) => {
if (this.handled) {
return;
}

const response =
_response instanceof H3Response ? _response : new H3Response(_response);

for (const [key, value] of response.headers.entries()) {
this.node.res.setHeader(key, value);
}
if (response.status) {
this.node.res.statusCode = sanitizeStatusCode(
response.status,
this.node.res.statusCode
);
}
if (response.statusText) {
this.node.res.statusMessage = sanitizeStatusMessage(
response.statusText
);
}
if (response.redirected) {
this.node.res.setHeader("location", response.url);
}
if (!response._body) {
return this.node.res.end();
}
if (
typeof response._body === "string" ||
"buffer" in response._body ||
"byteLength" in response._body
) {
return this.node.res.end(response._body);
}
if (!response.headers.has("content-type")) {
response.headers.set("content-type", MIMES.json);
}
this.node.res.end(JSON.stringify(response._body));
});
respondWith(response: Response | PromiseLike<Response>): Promise<void> {
return Promise.resolve(response).then((_response) =>
sendWebResponse(this, _response)
);
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/event/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from "./event";
export * from "./headers";
export * from "./response";
export * from "./utils";

// TODO: Drop in next major version
export * from "./polyfills/headers";
export * from "./polyfills/response";
4 changes: 4 additions & 0 deletions src/event/headers.ts → src/event/polyfills/headers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @deprecated Please use native web Headers
* https://developer.mozilla.org/en-US/docs/Web/API/Headers
*/
export class H3Headers implements Headers {
_headers: Record<string, string>;

Expand Down
6 changes: 5 additions & 1 deletion src/event/response.ts → src/event/polyfills/response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { EventHandlerResponse } from "../types";
import type { EventHandlerResponse } from "../../types";
import { H3Headers } from "./headers";

/**
* @deprecated Please use native web Response
* https://developer.mozilla.org/en-US/docs/Web/API/Response
*/
export class H3Response implements Response {
readonly headers: H3Headers;
readonly status: number;
Expand Down
8 changes: 6 additions & 2 deletions src/utils/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,10 @@ export function writeEarlyHints(
}
}

export function sendWebResponse(event: H3Event, response: Response) {
export function sendWebResponse(
event: H3Event,
response: Response
): void | Promise<void> {
for (const [key, value] of response.headers) {
if (key === "set-cookie") {
event.node.res.appendHeader(key, splitCookiesString(value));
Expand All @@ -322,7 +325,8 @@ export function sendWebResponse(event: H3Event, response: Response) {
event.node.res.setHeader("location", response.url);
}
if (!response.body) {
return event.node.res.end();
event.node.res.end();
return;
}
return sendStream(event, response.body);
}

0 comments on commit 21ed8d1

Please sign in to comment.