1
+ import { AlephAPIResponse } from './api.ts'
1
2
import { createHtml } from './html.ts'
2
3
import log from './log.ts'
3
4
import { getContentType } from './mime.ts'
@@ -16,6 +17,7 @@ export async function start(appDir: string, port: number, isDev = false, reload
16
17
for await ( const req of s ) {
17
18
const url = new URL ( 'http://localhost/' + req . url )
18
19
const pathname = util . cleanPath ( url . pathname )
20
+ const resp = new AlephAPIResponse ( req )
19
21
20
22
try {
21
23
// serve hmr ws
@@ -72,11 +74,7 @@ export async function start(appDir: string, port: number, isDev = false, reload
72
74
const filePath = path . join ( project . buildDir , util . trimPrefix ( pathname , '/_aleph/' ) )
73
75
if ( existsFileSync ( filePath ) ) {
74
76
const body = await Deno . readFile ( filePath )
75
- req . respond ( {
76
- status : 200 ,
77
- headers : new Headers ( { 'Content-Type' : 'text/css; charset=utf-8' } ) ,
78
- body
79
- } ) . catch ( err => log . warn ( 'ServerRequest.respond:' , err . message ) )
77
+ resp . send ( body , 'text/css; charset=utf-8' , true )
80
78
continue
81
79
}
82
80
} else {
@@ -85,7 +83,7 @@ export async function start(appDir: string, port: number, isDev = false, reload
85
83
if ( mod ) {
86
84
const etag = req . headers . get ( 'If-None-Match' )
87
85
if ( etag && etag === mod . hash ) {
88
- req . respond ( { status : 304 } )
86
+ resp . end ( 304 )
89
87
continue
90
88
}
91
89
@@ -111,14 +109,8 @@ export async function start(appDir: string, port: number, isDev = false, reload
111
109
body = injectHmr ( { ...mod , jsContent : body } )
112
110
}
113
111
}
114
- req . respond ( {
115
- status : 200 ,
116
- headers : new Headers ( {
117
- 'Content-Type' : `application/${ reqSourceMap ? 'json' : 'javascript' } ; charset=utf-8` ,
118
- 'ETag' : mod . hash
119
- } ) ,
120
- body
121
- } ) . catch ( err => log . warn ( 'ServerRequest.respond:' , err . message ) )
112
+ resp . setHeader ( 'ETag' , mod . hash )
113
+ resp . send ( body , `application/${ reqSourceMap ? 'json' : 'javascript' } ; charset=utf-8` , true )
122
114
continue
123
115
}
124
116
}
@@ -127,32 +119,28 @@ export async function start(appDir: string, port: number, isDev = false, reload
127
119
// serve public files
128
120
const filePath = path . join ( project . appRoot , 'public' , pathname )
129
121
if ( existsFileSync ( filePath ) ) {
122
+ const info = await Deno . lstat ( filePath )
123
+ if ( info . mtime ?. toUTCString ( ) === req . headers . get ( 'If-Modified-Since' ) ) {
124
+ resp . end ( 304 )
125
+ continue
126
+ }
127
+
130
128
const body = await Deno . readFile ( filePath )
131
- req . respond ( {
132
- status : 200 ,
133
- headers : new Headers ( { 'Content-Type' : getContentType ( filePath ) } ) ,
134
- body
135
- } ) . catch ( err => log . warn ( 'ServerRequest.respond:' , err . message ) )
129
+ const ct = getContentType ( filePath )
130
+ resp . setHeader ( 'Last-Modified' , info . mtime ! . toUTCString ( ) )
131
+ resp . send ( body , ct , ct . startsWith ( 'text/' ) || / \. ( m ? j s | j s o n | x m l ) $ / i. test ( filePath ) )
136
132
continue
137
133
}
138
134
139
135
// ssr
140
136
const [ status , html ] = await project . getPageHtml ( { pathname, search : url . search } )
141
- req . respond ( {
142
- status,
143
- headers : new Headers ( { 'Content-Type' : 'text/html' } ) ,
144
- body : html
145
- } ) . catch ( err => log . warn ( 'ServerRequest.respond:' , err . message ) )
137
+ resp . status ( status ) . send ( html , 'text/html' , true )
146
138
} catch ( err ) {
147
- req . respond ( {
148
- status : 500 ,
149
- headers : new Headers ( { 'Content-Type' : 'text/html' } ) ,
150
- body : createHtml ( {
151
- lang : 'en' ,
152
- head : [ '<title>500 - internal server error</title>' ] ,
153
- body : `<p><strong><code>500</code></strong><small> - </small><span>${ err . message } </span></p>`
154
- } )
155
- } ) . catch ( err => log . warn ( 'ServerRequest.respond:' , err . message ) )
139
+ resp . status ( 500 ) . send ( createHtml ( {
140
+ lang : 'en' ,
141
+ head : [ '<title>500 - internal server error</title>' ] ,
142
+ body : `<p><strong><code>500</code></strong><small> - </small><span>${ err . message } </span></p>`
143
+ } ) , 'text/html' , true )
156
144
}
157
145
}
158
146
} catch ( err ) {
0 commit comments