Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
rossrobino committed Sep 7, 2024
1 parent 8b873f1 commit f7ffed1
Showing 1 changed file with 19 additions and 29 deletions.
48 changes: 19 additions & 29 deletions packages/domco/src/node/request-listener/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* copied from https://github.com/mjackson/remix-the-web/blob/main/packages/node-fetch-server/src/lib/request-listener.ts
*
* created this issue https://github.com/mjackson/remix-the-web/issues/13
* Adapted from https://github.com/mjackson/remix-the-web/blob/main/packages/node-fetch-server
* to use as middleware: https://github.com/mjackson/remix-the-web/issues/13
*/
import type { MaybePromise } from "../../types/helper/index.js";
import type { ReadStream } from "node:fs";
Expand All @@ -18,12 +17,14 @@ type ClientAddress = {
* [Node.js Reference](https://nodejs.org/api/net.html#socketremoteaddress)
*/
address: string;

/**
* The family of the client IP address.
*
* [Node.js Reference](https://nodejs.org/api/net.html#socketremotefamily)
*/
family: "IPv4" | "IPv6";
family: IncomingMessage["socket"]["remoteFamily"];

/**
* The remote port of the client that sent the request.
*
Expand All @@ -32,14 +33,6 @@ type ClientAddress = {
port: number;
};

/**
* A function that handles an error that occurred during request handling. May return a response to
* send to the client, or `void` which creates an early return.
*
* [MDN `Response` Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response)
*/
type ErrorHandler = (error: unknown) => MaybePromise<Response | void>;

/**
* A function that handles an incoming request and returns a response.
*
Expand All @@ -65,11 +58,15 @@ type RequestListenerOptions = {
* ```
*/
host?: string;

/**
* An error handler that determines the response when the request handler throws an error. By
* default a 500 Internal Server Error response will be sent.
* A function that handles an error that occurred during request handling. May return a response to
* send to the client, or `void` which creates an early return.
*
* [MDN `Response` Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response)
*/
onError?: ErrorHandler;
onError?: (error: unknown) => MaybePromise<Response | void>;

/**
* Overrides the protocol of the incoming request URL. By default the request URL protocol is
* derived from the connection protocol. So e.g. when serving over HTTPS (using
Expand Down Expand Up @@ -103,23 +100,20 @@ export const createRequestListener = (
const request = createRequest(req, url, controller.signal);
const client = {
address: req.socket.remoteAddress!,
family: req.socket.remoteFamily! as ClientAddress["family"],
family: req.socket.remoteFamily!,
port: req.socket.remotePort!,
};

let response: Response;
try {
response = await handler(request, client);
} catch (error) {
try {
const errorResponse = await onError(error);
// handled in vite middleware via `next(error)`
if (!errorResponse) return;
response = errorResponse;
} catch (error) {
console.error(`There was an error in the error handler: ${error}`);
response = internalServerError();
const errorResponse = await onError(error);
if (!errorResponse) {
// handled by the user, in this case - Vite middleware via `next(error)`
return;
}
response = errorResponse;
}

// Use the rawHeaders API and iterate over response.headers so we are sure to send multiple
Expand All @@ -133,7 +127,7 @@ export const createRequestListener = (
res.writeHead(response.status, rawHeaders);

if (response.body != null && req.method !== "HEAD") {
//@ts-expect-error
// @ts-expect-error
for await (let chunk of response.body) {
res.write(chunk);
}
Expand All @@ -145,10 +139,6 @@ export const createRequestListener = (

const defaultErrorHandler = (error: unknown) => {
console.error(error);
return internalServerError();
};

const internalServerError = () => {
return new Response(
// "Internal Server Error"
new Uint8Array([
Expand Down

0 comments on commit f7ffed1

Please sign in to comment.