Files
bunext/dist/utils/compress-response.js
tbenandClaude Opus 5.5 96e392f556 1.4.0: not_found, server-only response options, compression
- Server functions can return not_found: true to render the 404 page
  with status 404.
- Page components and window.__PAGE_PROPS__ (and HMR updates) only get
  props, query, url and error. response_options, redirect, cache settings
  and res_transform stay on the server, so cookies and private headers no
  longer end up in the HTML. BunextPageProps is narrowed to match
  (breaking for code that read server-only fields from component props).
- response_options headers go through a Headers object, so repeated
  headers such as several Set-Cookie survive.
- Redirects: status_code wins over permanent; response_options headers
  and res_transform apply to redirects too.
- Gzip for compressible text responses; compress: false to turn it off.
- Files the router matches but never bundles (.server.ts, __root,
  excluded paths and directories) return 404 instead of looping through
  dev rebuild retries.
- Missing public files return 404 rather than 403, so dotted page paths
  like /users/john.doe fall through to pages again.
- importFresh detects rewrites within the filesystem's mtime granularity.
- README: self-hosted React (not esm.sh), snake_case option names, static
  file serving, cache headers, the new options.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-09 04:14:56 +01:00

46 lines
1.7 KiB
JavaScript

const COMPRESSIBLE = /^(text\/(?!event-stream)|application\/(javascript|json|xml|manifest\+json|ld\+json|rss\+xml|atom\+xml)|image\/svg\+xml)/i;
/** Smaller bodies aren't worth the gzip overhead. */
const MIN_SIZE = 1024;
/**
* Gzips `res` when the client accepts gzip and the body is compressible
* text. Streams (no buffering), so it is safe for large and streamed
* responses. Responses that are already encoded, ranged, bodiless or
* event streams pass through untouched.
*/
export default function compressResponse(req, res) {
if (!res.body || req.method === "HEAD")
return res;
if (res.status < 200 || res.status === 204 || res.status === 206) {
return res;
}
if (res.headers.has("Content-Encoding"))
return res;
if (!/\bgzip\b/i.test(req.headers.get("Accept-Encoding") || "")) {
return res;
}
if (!COMPRESSIBLE.test(res.headers.get("Content-Type") || ""))
return res;
if (/\bno-transform\b/i.test(res.headers.get("Cache-Control") || "")) {
return res;
}
const length = Number(res.headers.get("Content-Length"));
if (length && length < MIN_SIZE)
return res;
// Copy: headers of fetch()/Response.redirect responses are immutable
const headers = new Headers(res.headers);
headers.set("Content-Encoding", "gzip");
headers.delete("Content-Length");
const vary = headers.get("Vary");
if (!vary) {
headers.set("Vary", "Accept-Encoding");
}
else if (!/\b(accept-encoding|\*)\b/i.test(vary)) {
headers.set("Vary", `${vary}, Accept-Encoding`);
}
return new Response(res.body.pipeThrough(new CompressionStream("gzip")), {
status: res.status,
statusText: res.statusText,
headers,
});
}