(request: Request)
| 149 | }; |
| 150 | |
| 151 | const staticFile = async (request: Request) => { |
| 152 | try { |
| 153 | const url = getRequestUrl(request, opts); |
| 154 | |
| 155 | if (isStaticPath(request.method || 'GET', url)) { |
| 156 | const { filePath, content } = await openStaticFile(url); |
| 157 | // We know that it's in the static folder, but it could still be missing |
| 158 | // If we start the stream with a missing file, it will throw a 500 error during the stream |
| 159 | if (!(await content.exists())) { |
| 160 | return new Response('Not Found', { |
| 161 | status: 404, |
| 162 | headers: { 'Content-Type': 'text/plain; charset=utf-8', 'X-Not-Found': url.pathname }, |
| 163 | }); |
| 164 | } |
| 165 | |
| 166 | const ext = extname(filePath).replace(/^\./, ''); |
| 167 | |
| 168 | return new Response(await content.stream(), { |
| 169 | status: 200, |
| 170 | headers: { |
| 171 | 'content-type': MIME_TYPES[ext] || 'text/plain; charset=utf-8', |
| 172 | 'Cache-Control': opts.static?.cacheControl || 'max-age=3600', |
| 173 | }, |
| 174 | }); |
| 175 | } |
| 176 | |
| 177 | return null; |
| 178 | } catch (e) { |
| 179 | console.error(e); |
| 180 | return new Response(String(e || 'Error'), { |
| 181 | status: 500, |
| 182 | headers: { 'Content-Type': 'text/plain; charset=utf-8', 'X-Error': 'bun-server' }, |
| 183 | }); |
| 184 | } |
| 185 | }; |
| 186 | |
| 187 | return { |
| 188 | router, |
no test coverage detected
searching dependent graphs…