-
-
Notifications
You must be signed in to change notification settings - Fork 67
feat(server): compression plugin filter option should override default content type filter #880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(server): compression plugin filter option should override default content type filter #880
Conversation
…t content type filter
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughRefactors compression filter logic in fetch and node adapters to centralize decision-making and explicitly avoid compressing Event Iterator responses. Updates tests to cover custom filter behavior (true/false), no-content-type handling, and event-stream invariants. Documentation/comments updated to reflect filter override semantics and default compressible content-type behavior. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Adapter
participant CompressionPlugin
participant Responder
Client->>Adapter: HTTP Request
Adapter->>Responder: Produce Response
Responder-->>Adapter: Response (body, headers)
Adapter->>CompressionPlugin: shouldCompress(req, res)?
CompressionPlugin->>CompressionPlugin: check Event Iterator (text/event-stream && no Content-Disposition)
alt Event Iterator
CompressionPlugin-->>Adapter: false
else
CompressionPlugin->>CompressionPlugin: userFilter? use it : defaultContentTypeFilter
CompressionPlugin-->>Adapter: true/false
end
alt true
Adapter->>Adapter: apply gzip
else false
Adapter->>Adapter: pass through
end
Adapter-->>Client: Final Response
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Poem
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. ✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @unnoq, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request enhances the CompressionPlugin in the server by allowing its filter option to explicitly override the default content type checks. This provides greater control over when responses are compressed, enabling compression even for content types typically considered non-compressible, while still preventing compression for Event Iterator responses.
Highlights
- Custom Filter Override: The CompressionPlugin's filter option now takes precedence over the default content type checks, allowing users to force compression for any response type (except Event Iterators) by returning true from their custom filter.
- Clarified Documentation: The JSDoc for the filter option has been updated to clearly explain its overriding behavior and to explicitly state that Event Iterator responses are never compressed.
- Comprehensive Test Coverage: New test cases have been added for both Fetch and Node.js adapters to validate that responses without content-type are not compressed, and that the custom filter correctly enables or disables compression regardless of the default content type rules.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request enhances the compression plugin by allowing the filter
option to override the default content-type based filtering logic. The changes are consistently applied to both the Fetch and Node.js server adapters. The implementation is solid, with updated documentation and comprehensive new tests that cover the override behavior, including important edge cases like event streams. I have one suggestion to improve code consistency in the Node.js adapter.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
More templates
@orpc/arktype
@orpc/client
@orpc/contract
@orpc/experimental-durable-event-iterator
@orpc/hey-api
@orpc/interop
@orpc/json-schema
@orpc/nest
@orpc/openapi
@orpc/openapi-client
@orpc/otel
@orpc/react
@orpc/react-query
@orpc/server
@orpc/shared
@orpc/solid-query
@orpc/standard-server
@orpc/standard-server-aws-lambda
@orpc/standard-server-fetch
@orpc/standard-server-node
@orpc/standard-server-peer
@orpc/svelte-query
@orpc/tanstack-query
@orpc/trpc
@orpc/valibot
@orpc/vue-colada
@orpc/vue-query
@orpc/zod
commit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
♻️ Duplicate comments (1)
packages/server/src/adapters/node/compression-plugin.ts (1)
28-28
: Good call: switched to hasHeader for Content-Disposition detectionUsing res.hasHeader('content-disposition') is clearer and avoids undefined checks. Matches prior feedback.
🧹 Nitpick comments (7)
packages/server/src/adapters/node/compression-plugin.ts (2)
9-13
: Clarify “never compress Event Iterator responses” vs. code’s Content-Disposition exceptionDocs say “never compressed,” but the implementation only blocks compression for event-stream when Content-Disposition is absent. If the Content-Disposition header is present, compression may proceed. Please align the comment with the actual behavior or remove the Content-Disposition exception in code.
Would you like me to update the JSDoc to: “Never compress Event Iterator responses (identified as text/event-stream without a Content-Disposition header)”?
29-29
: Make Content-Type handling robust and case-insensitiveTo avoid edge cases (header arrays and casing), normalize the header and lower-case before checking for event-stream.
Apply this diff:
- const contentType = res.getHeader('content-type')?.toString() + const ct = res.getHeader('content-type') + const contentType = Array.isArray(ct) + ? ct[0]?.toLowerCase() + : ct?.toString().toLowerCase() - if (!hasContentDisposition && contentType?.startsWith('text/event-stream')) { + if (!hasContentDisposition && contentType?.startsWith('text/event-stream')) { return false }Also applies to: 34-34
packages/server/src/adapters/node/compression-plugin.test.ts (2)
145-165
: Potential flakiness: ensure the filter is invoked deterministicallyWith a small response body (“output”), the compression middleware’s internal threshold logic can short-circuit before calling the filter in some implementations. To make this test robust, force filter invocation by setting threshold: 0 (or returning a large body).
Apply this diff:
- new CompressionPlugin({ filter }), + new CompressionPlugin({ filter, threshold: 0 }),If you’d rather keep the default threshold, consider returning largeText in the handler to guarantee the decision path.
8-8
: Add a Node test for “no Content-Type” to mirror Fetch behaviorFetch tests assert no compression when Content-Type is missing. After aligning the Node plugin’s default behavior, add a similar test here to avoid divergence across adapters.
Example to add:
it('should not compress when response has no content-type', async () => { const res = await request(async (req: IncomingMessage, res: ServerResponse) => { const handler = new RPCHandler(os.handler(() => 'output'), { plugins: [new CompressionPlugin()], interceptors: [ async (options) => { const result = await options.next() if (!result.matched) return result return { ...result, response: { ...result.response, headers: { ...result.response.headers, }, body: new Blob(['output']), // omit type -> no content-type }, } }, ], }) await handler.handle(req, res) }) .post('/') .set('accept-encoding', 'gzip, deflate') .send({ input: 'test' }) expect(res.status).toBe(200) expect(res.headers['content-encoding']).toBeUndefined() expect(res.headers['content-type']).toBeUndefined() expect(res.text).toContain('output') })Also applies to: 105-143, 145-165, 225-226
packages/server/src/adapters/fetch/compression-plugin.ts (2)
31-35
: Clarify doc to match detection heuristicDocs say “never compress Event Iterator responses,” while code only blocks when Content-Disposition is absent and Content-Type starts with text/event-stream. Consider updating wording accordingly to avoid confusion.
Would you like me to update the JSDoc here as well for parity with the Node adapter?
54-68
: Normalize Content-Type to lowercase for robust matchingLower-casing avoids casing edge cases for event-stream detection and keeps behavior consistent.
Apply this diff:
- this.filter = (request, response) => { - const hasContentDisposition = response.headers.has('content-disposition') - const contentType = response.headers.get('content-type') + this.filter = (request, response) => { + const hasContentDisposition = response.headers.has('content-disposition') + const contentType = response.headers.get('content-type')?.toLowerCase() /** * Never compress Event Iterator responses. */ if (!hasContentDisposition && contentType?.startsWith('text/event-stream')) { return false } return options.filter ? options.filter(request, response) : isCompressibleContentType(contentType) }packages/server/src/adapters/fetch/compression-plugin.test.ts (1)
525-567
: Nit: test name grammarConsider “should override filter and compress if filter returns true” for consistency with the next test.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
packages/server/src/adapters/fetch/compression-plugin.test.ts
(3 hunks)packages/server/src/adapters/fetch/compression-plugin.ts
(3 hunks)packages/server/src/adapters/node/compression-plugin.test.ts
(3 hunks)packages/server/src/adapters/node/compression-plugin.ts
(2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
packages/server/src/adapters/node/compression-plugin.test.ts (2)
packages/server/src/builder.ts (2)
handler
(273-280)os
(336-352)packages/server/src/adapters/node/compression-plugin.ts (1)
CompressionPlugin
(21-98)
packages/server/src/adapters/fetch/compression-plugin.ts (3)
packages/server/src/adapters/node/compression-plugin.ts (1)
CompressionPluginOptions
(7-15)packages/standard-server-peer/src/client.ts (1)
request
(73-176)packages/standard-server-peer/src/server.ts (1)
response
(146-186)
packages/server/src/adapters/fetch/compression-plugin.test.ts (1)
packages/server/src/adapters/fetch/compression-plugin.ts (1)
CompressionPlugin
(46-128)
🔇 Additional comments (8)
packages/server/src/adapters/node/compression-plugin.test.ts (3)
8-8
: Fixture LGTMUsing 2KB ensures threshold is crossed for compression-related tests.
105-143
: Override behavior test looks solidValidates that user filter can force compression for a non-default-compressible type (image/jpeg). Good coverage and assertion that filter is invoked.
225-226
: Event Iterator invariant retainedGood to see that even with a permissive filter, event-stream responses are not compressed.
packages/server/src/adapters/fetch/compression-plugin.ts (2)
49-49
: Good: filter is always definedSimplifies guard logic and centralizes decision-making.
110-110
: LGTM: simplified guardSince filter is always defined, this is the right simplification.
packages/server/src/adapters/fetch/compression-plugin.test.ts (3)
316-344
: Great: explicit “no Content-Type” behavior coveredConfirms the new default filter semantics for missing content-type.
568-592
: Filter-false behavior covered wellAsserts both no compression and that the filter was invoked with (Request, Response).
600-601
: Event Iterator invariant with explicit filterKeeps “no compression for event-stream” intact even when a permissive filter is provided.
Summary by CodeRabbit