Skip to content

Commit 86b6f23

Browse files
committed
Tidy up and refactor the middleware processing.
1 parent 79c938b commit 86b6f23

File tree

3 files changed

+68
-54
lines changed

3 files changed

+68
-54
lines changed

src/http/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* The context definition.
33
*/
4-
export default class Context implements Context {
4+
export default class Context {
55
/**
66
* Initialise an HTTP context.
77
*

src/http/processor.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// deno-lint-ignore-file no-explicit-any
2+
3+
import type Context from "./context.ts";
4+
5+
export default class Processor {
6+
/**
7+
* Initialise the HTTP processor.
8+
*
9+
* @param context The current HTTP context.
10+
* @constructor
11+
*/
12+
constructor(private context : Context) {}
13+
14+
/**
15+
* Process a body into a valid Response object.
16+
*
17+
* @param body A body value to process.
18+
* @returns A valid HTTP response object.
19+
*/
20+
public process(body: any): Response {
21+
// If the middleware provides a Response object, use it.
22+
if (body instanceof Response) {
23+
return body;
24+
}
25+
26+
const hasContentType = this.context.response.headers.get("content-type");
27+
28+
// If the middleware returns an object, process it as JSON.
29+
if (typeof body === "object") {
30+
if (!hasContentType) {
31+
this.context.response.headers.set("content-type", "application/json");
32+
}
33+
34+
return new Response(JSON.stringify(body), {
35+
headers: this.context.response.headers,
36+
});
37+
}
38+
39+
// If the middleware returns a string, process plain text or HTML.
40+
if (typeof body === "string") {
41+
const isHtml = (new RegExp(/<[a-z/][\s\S]*>/i)).test(body);
42+
43+
if (!hasContentType) {
44+
this.context.response.headers.set("content-type", "text/plain");
45+
}
46+
47+
if (isHtml && !hasContentType) {
48+
this.context.response.headers.set("content-type", "text/html");
49+
}
50+
51+
return new Response(body as string, {
52+
headers: this.context.response.headers,
53+
});
54+
}
55+
56+
return new Response(body as string);
57+
}
58+
}

src/kernel.ts

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
// deno-lint-ignore-file no-explicit-any
2-
31
import Context from "./http/context.ts";
2+
import Processor from "./http/processor.ts";
43

54
import type { Error } from "./error/interfaces/error.ts";
65

76
/**
87
* The root initialiser for the framework.
98
*/
109
export default class Kernel {
10+
/**
11+
* The response processor for the kernel.
12+
*/
13+
private processor : Processor;
14+
1115
/**
1216
* The current HTTP context.
1317
*/
@@ -18,11 +22,6 @@ export default class Kernel {
1822
*/
1923
private middleware: CallableFunction[] = [];
2024

21-
/**
22-
* The current middleware index.
23-
*/
24-
private currentIndex: number = 0;
25-
2625
/**
2726
* Initialise the kernel.
2827
*
@@ -33,6 +32,8 @@ export default class Kernel {
3332
new Request(Deno.env.get("APP_URL") as string),
3433
new Response(null),
3534
);
35+
36+
this.processor = new Processor(this.context);
3637
}
3738

3839
/**
@@ -92,7 +93,7 @@ export default class Kernel {
9293
);
9394

9495
if (!called) {
95-
this.context.response = this.process(body);
96+
this.context.response = this.processor.process(body);
9697
}
9798
}
9899
};
@@ -101,51 +102,6 @@ export default class Kernel {
101102
await execute(0);
102103
}
103104

104-
/**
105-
* Process middleware into an HTTP response.
106-
*
107-
* @param body The response body.
108-
* @returns
109-
*/
110-
private process(body: any): Response {
111-
// If the middleware provides a Response object, use it.
112-
if (body instanceof Response) {
113-
return body;
114-
}
115-
116-
const hasContentType = this.context.response.headers.get("content-type");
117-
118-
// If the middleware returns an object, process it as JSON.
119-
if (typeof body === "object") {
120-
if (!hasContentType) {
121-
this.context.response.headers.set("content-type", "application/json");
122-
}
123-
124-
return new Response(JSON.stringify(body), {
125-
headers: this.context.response.headers,
126-
});
127-
}
128-
129-
// If the middleware returns a string, process plain text or HTML.
130-
if (typeof body === "string") {
131-
const isHtml = (new RegExp(/<[a-z/][\s\S]*>/i)).test(body);
132-
133-
if (!hasContentType) {
134-
this.context.response.headers.set("content-type", "text/plain");
135-
}
136-
137-
if (isHtml && !hasContentType) {
138-
this.context.response.headers.set("content-type", "text/html");
139-
}
140-
141-
return new Response(body as string, {
142-
headers: this.context.response.headers,
143-
});
144-
}
145-
146-
return new Response(body as string);
147-
}
148-
149105
/**
150106
* Handles an error and returns a response.
151107
*

0 commit comments

Comments
 (0)