1
- // deno-lint-ignore-file no-explicit-any
2
-
3
1
import Context from "./http/context.ts" ;
2
+ import Processor from "./http/processor.ts" ;
4
3
5
4
import type { Error } from "./error/interfaces/error.ts" ;
6
5
7
6
/**
8
7
* The root initialiser for the framework.
9
8
*/
10
9
export default class Kernel {
10
+ /**
11
+ * The response processor for the kernel.
12
+ */
13
+ private processor : Processor ;
14
+
11
15
/**
12
16
* The current HTTP context.
13
17
*/
@@ -18,11 +22,6 @@ export default class Kernel {
18
22
*/
19
23
private middleware : CallableFunction [ ] = [ ] ;
20
24
21
- /**
22
- * The current middleware index.
23
- */
24
- private currentIndex : number = 0 ;
25
-
26
25
/**
27
26
* Initialise the kernel.
28
27
*
@@ -33,6 +32,8 @@ export default class Kernel {
33
32
new Request ( Deno . env . get ( "APP_URL" ) as string ) ,
34
33
new Response ( null ) ,
35
34
) ;
35
+
36
+ this . processor = new Processor ( this . context ) ;
36
37
}
37
38
38
39
/**
@@ -92,7 +93,7 @@ export default class Kernel {
92
93
) ;
93
94
94
95
if ( ! called ) {
95
- this . context . response = this . process ( body ) ;
96
+ this . context . response = this . processor . process ( body ) ;
96
97
}
97
98
}
98
99
} ;
@@ -101,51 +102,6 @@ export default class Kernel {
101
102
await execute ( 0 ) ;
102
103
}
103
104
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
-
149
105
/**
150
106
* Handles an error and returns a response.
151
107
*
0 commit comments