Comparing version 3.2.3 to 3.2.4
# Changelog | ||
## v3.2.4 - Jan 13, 2024 | ||
- Avoid etags when body is a stream | ||
## v3.2.3 - Jan 13, 2024 | ||
@@ -7,3 +11,3 @@ | ||
- Accommodate Bun 1.1.43 bugfix to HEAD content-length header | ||
- Avoid etags and compression when body is a stream | ||
- Avoid compression when body is a stream | ||
- Add `maxSize` option to compression middleware (default 2GB) | ||
@@ -10,0 +14,0 @@ - Add LRU cache for file-based mime detection |
{ | ||
"name": "bunshine", | ||
"version": "3.2.3", | ||
"version": "3.2.4", | ||
"module": "index.ts", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -21,3 +21,3 @@ import { TypedArray } from 'type-fest'; | ||
const resp = await next(); | ||
if (!_shouldGenerateEtag(resp)) { | ||
if (!_shouldGenerateEtag(context.request, resp)) { | ||
return resp; | ||
@@ -64,3 +64,3 @@ } | ||
function _shouldGenerateEtag(response: Response) { | ||
function _shouldGenerateEtag(request: Request, response: Response) { | ||
// Ensure the response object is valid | ||
@@ -71,7 +71,5 @@ if (!(response instanceof Response)) { | ||
// List of status codes where ETags generally make sense | ||
// Technically 404 could be included, but the application should use custom logic | ||
// Do not generate ETag for status codes that don't make sense | ||
// Technically 404 could be included, but each application should use custom logic | ||
const validStatusCodes = [200, 201, 203, 204, 206, /*404,*/ 410]; | ||
// Check if the response status code is in the valid list | ||
if (!validStatusCodes.includes(response.status)) { | ||
@@ -81,11 +79,17 @@ return false; | ||
// Check if the response is cacheable | ||
// Do not generate ETag for non-cacheable responses | ||
const cacheControl = response.headers.get('Cache-Control'); | ||
if (cacheControl && /no-store/i.test(cacheControl)) { | ||
return false; // Do not generate ETag for non-cacheable responses | ||
return false; | ||
} | ||
// Check if the response method supports ETag generation | ||
const method = response.headers.get('X-Request-Method') || 'GET'; // Custom header to track the request method | ||
const methodsThatSupportEtag = ['GET', 'HEAD', 'PUT', 'POST', 'PATCH']; | ||
// Do not generate ETag for streams | ||
const contentType = response.headers.get('Content-Type'); | ||
if (contentType && /stream/i.test(contentType)) { | ||
return false; | ||
} | ||
// Do not generate ETag for HEAD nor DELETE | ||
const method = request.headers.get('X-Request-Method') || request.method; | ||
const methodsThatSupportEtag = ['GET', 'PUT', 'POST', 'PATCH']; | ||
if (!methodsThatSupportEtag.includes(method.toUpperCase())) { | ||
@@ -95,3 +99,3 @@ return false; | ||
// Check if the response has a body or meaningful representation | ||
// Do not generate Etag on empty bodies | ||
const contentLength = response.headers.get('Content-Length'); | ||
@@ -105,3 +109,2 @@ if ( | ||
// If all conditions are met, it makes sense to generate an ETag | ||
return true; | ||
@@ -108,0 +111,0 @@ } |
2210579
2643