@@ -102,17 +102,22 @@ export const serve = (options: ServerOptions = {}) => {
102
102
stat = await Deno . lstat ( filePath ) ;
103
103
}
104
104
if ( stat . isFile ) {
105
- const { mtime } = stat ;
106
- const etag = mtime ? mtime . getTime ( ) . toString ( 16 ) + "-" + stat . size . toString ( 16 ) : null ;
105
+ const headers = new Headers ( { "Content-Type" : contentType } ) ;
106
+ const deployId = Deno . env . get ( "DENO_DEPLOYMENT_ID" ) ;
107
+ let etag : string | null = null ;
108
+ if ( deployId ) {
109
+ etag = `${ btoa ( pathname ) . replace ( / [ ^ a - z 0 - 9 ] / g, "" ) } -${ deployId } ` ;
110
+ } else {
111
+ const { mtime, size } = stat ;
112
+ if ( mtime ) {
113
+ etag = mtime . getTime ( ) . toString ( 16 ) + "-" + size . toString ( 16 ) ;
114
+ headers . append ( "Last-Modified" , new Date ( mtime ) . toUTCString ( ) ) ;
115
+ }
116
+ }
107
117
if ( etag && req . headers . get ( "If-None-Match" ) === etag ) {
108
118
return new Response ( null , { status : 304 } ) ;
109
119
}
110
120
const file = await Deno . open ( filePath , { read : true } ) ;
111
- const headers = new Headers ( { "Content-Type" : contentType } ) ;
112
- if ( mtime ) {
113
- headers . set ( "Etag" , etag ! ) ;
114
- headers . set ( "Last-Modified" , mtime . toUTCString ( ) ) ;
115
- }
116
121
return new Response ( readableStreamFromReader ( file ) , { headers } ) ;
117
122
}
118
123
} catch ( err ) {
@@ -134,30 +139,31 @@ export const serve = (options: ServerOptions = {}) => {
134
139
}
135
140
}
136
141
137
- let cookies : Map < string , string > | null = null ;
138
-
139
142
const customHTMLRewriter = new Map < string , HTMLRewriterHandlers > ( ) ;
140
143
const ctx = {
141
144
params : { } ,
142
145
headers : new Headers ( ) ,
143
146
cookies : {
144
- get : ( name : string ) => {
145
- if ( cookies === null ) {
146
- cookies = new Map < string , string > ( ) ;
147
+ _cookies : null as Map < string , string > | null ,
148
+ get ( name : string ) {
149
+ if ( this . _cookies === null ) {
150
+ this . _cookies = new Map < string , string > ( ) ;
147
151
const cookieHeader = req . headers . get ( "Cookie" ) ;
148
152
if ( cookieHeader ) {
149
153
for ( const cookie of cookieHeader . split ( ";" ) ) {
150
154
const [ key , value ] = util . splitBy ( cookie , "=" ) ;
151
- cookies . set ( key . trim ( ) , value ) ;
155
+ this . _cookies . set ( key . trim ( ) , value ) ;
152
156
}
153
157
}
154
158
}
155
- return cookies . get ( name ) ;
159
+ return this . _cookies . get ( name ) ;
156
160
} ,
157
- set : ( name : string , value : string , options ?: CookieOptions ) => {
161
+ set ( name : string , value : string , options ?: CookieOptions ) {
162
+ this . _cookies ?. set ( name , value ) ;
158
163
ctx . headers . set ( "Set-Cookie" , setCookieHeader ( name , value , options ) ) ;
159
164
} ,
160
- delete : ( name : string , options ?: CookieOptions ) => {
165
+ delete ( name : string , options ?: CookieOptions ) {
166
+ this . _cookies ?. delete ( name ) ;
161
167
ctx . headers . set ( "Set-Cookie" , setCookieHeader ( name , "" , { ...options , expires : new Date ( 0 ) } ) ) ;
162
168
} ,
163
169
} ,
@@ -321,12 +327,13 @@ export const serve = (options: ServerOptions = {}) => {
321
327
log . setLevel ( logLevel ) ;
322
328
}
323
329
330
+ // clean global cached objects
331
+ Reflect . deleteProperty ( globalThis , "__UNO_GENERATOR" ) ;
332
+ Reflect . deleteProperty ( globalThis , "__ALEPH_INDEX_HTML" ) ;
333
+
324
334
// inject global `__ALEPH_CONFIG`
325
335
Reflect . set ( globalThis , "__ALEPH_CONFIG" , Object . assign ( { } , config ) ) ;
326
336
327
- // delete previous `__UNO_GENERATOR`
328
- Reflect . deleteProperty ( globalThis , "__UNO_GENERATOR" ) ;
329
-
330
337
const { hostname, port = 8080 , certFile, keyFile, signal } = options ;
331
338
if ( Deno . env . get ( "ALEPH_CLI" ) ) {
332
339
Reflect . set ( globalThis , "__ALEPH_SERVER" , { hostname, port, certFile, keyFile, handler, signal } ) ;
0 commit comments