Skip to content

Fix elysia standard-mode compression via the documented mapResponse pattern - #1089

Open
MDA2AV wants to merge 1 commit into
mainfrom
fix/elysia-standard-compression
Open

Fix elysia standard-mode compression via the documented mapResponse pattern#1089
MDA2AV wants to merge 1 commit into
mainfrom
fix/elysia-standard-compression

Conversation

@MDA2AV

@MDA2AV MDA2AV commented Aug 7, 2026

Copy link
Copy Markdown
Owner

Description

Closes #1075 — the elysia entry hand-rolled compression negotiation in /json/:count while marked mode: standard, and its br branch shipped deflate bytes labeled content-encoding: br (latent: the branch compared the whole header to "br", so validation's gzip, br never reached it).

Why not a compression plugin

The plugin route (the issue's first preference) is a dead end on elysia 1.4 — verified by building, not just reading:

  • elysia-compress fails to compile: it imports mapResponse from elysia's root export, removed in elysia 1.2.6 (vermaysha/elysia-compress#149); the repo was archived Nov 2025. Its default 24 h compressed-response cache would also collide with the json-comp rule's "no pre-compressed caches" clause.
  • @labzzhq/compressor (fork) carries the identical broken import; the remaining plugins are 2+ years stale.

Elysia's own docs prescribe compression via the mapResponse lifecycle hook (essential/life-cycle) — that is the framework's built-in compression mechanism, so this PR implements the docs pattern extended with real negotiation, keeping the entry standard:

  • first acceptable encoding in the client's own order (br or gzip, q=0 honoured), node:zlib at default settings — a bare Accept-Encoding: br now returns genuine brotli
  • 1024-byte threshold, same default as the express/fastify middleware (Add express framework #1068/Add fastify framework #1069 precedent)
  • compression runs per request — no caches, no pre-compressed bodies
  • ready-made Responses/files pass through untouched, so static serving is byte-identical to before

Two Elysia warts the hook must handle (worth knowing for future TS entries)

  1. headers is absent from the hook context on the error path — an unguarded read turns every 404 into a 500.
  2. With any mapResponse registered, letting an Elysia custom-status object (status() returns, plugin 404s) fall through swallows the status into an empty 200; the hook maps code-carrying objects explicitly. Without this, GET /static/nonexistent.txt broke.

Verification

  • ./scripts/validate.sh elysia: 31 passed, 0 failed (json-comp: Content-Encoding: gzip, compressed schema, per-request negotiation; static: 20 sizes + 404 intact)
  • Bare-br check (the case validation doesn't exercise): curl -H 'Accept-Encoding: br' /json/50?m=3 decompresses with zlib.brotliDecompressSync to the full correct 50-item schema with correct totals

Thanks @nigrosimone for the report.

…rn (#1075)

The entry hand-rolled Accept-Encoding negotiation in /json/:count with
Bun.gzipSync, and its br branch shipped Bun.deflateSync bytes labeled
"content-encoding: br" (latent because the branch compared the whole
header to "br", so "gzip, br" never reached it).

No compression plugin works on elysia 1.4: elysia-compress imports
mapResponse from elysia's root export, removed in 1.2.6
(vermaysha/elysia-compress#149, repo archived Nov 2025), and every fork
carries the same import. The framework's documented compression
mechanism is the mapResponse lifecycle hook, so this follows the
official docs example (elysiajs.com/essential/life-cycle) extended with
real per-request negotiation:

- picks the first acceptable encoding in the client's own order (br or
  gzip, q=0 honoured) via node:zlib at default settings — bare
  "Accept-Encoding: br" now returns genuine brotli
- 1024-byte threshold, matching the express/fastify middleware defaults
- no caches, no pre-compressed bodies; ready-made Responses and files
  pass through untouched (static serving identical to before)
- maps Elysia custom-status objects explicitly: with a mapResponse hook
  registered, letting a status() object fall through yields an empty
  200, which broke the static 404 check until handled

validate.sh elysia: 31 passed, 0 failed. Bare-br verified by
brotli-decompressing /json/50?m=3 to the full correct schema.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Repository owner deleted a comment from github-actions Bot Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

elysia entry: hand-rolled compression in standard mode, and a mislabeled br branch

1 participant