Skip to content

Commit 85cf38b

Browse files
feat(server): Compression Plugin (#869)
```ts const handler = new RPCHandler(router, { plugins: [ new CompressionPlugin(), ], }) ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - New Features - Added a Compression Plugin for Fetch and Node adapters: configurable encodings, threshold, and filter; automatically compresses compatible responses and respects cache-control no-transform. - Documentation - Added Compression Plugin docs and sidebar entry. - Clarified Body Limit example supports any compatible handler. - Tests - Added comprehensive Fetch and Node tests covering encodings, thresholds, filters, headers, and error scenarios. - Chores - Updated Dependabot to exclude the compression package. - Added compression dev-dependencies and relaxed server build warnings. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent dea79a3 commit 85cf38b

File tree

13 files changed

+978
-0
lines changed

13 files changed

+978
-0
lines changed

.github/dependabot.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ updates:
2222
- '@antfu/eslint-config'
2323
- 'eslint-plugin-*'
2424
- '@hey-api/*'
25+
- compression # inline dependency
2526
update-types:
2627
- minor
2728
- patch

apps/content/.vitepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export default withMermaid(defineConfig({
141141
{ text: 'Dedupe Requests', link: '/docs/plugins/dedupe-requests' },
142142
{ text: 'Batch Requests', link: '/docs/plugins/batch-requests' },
143143
{ text: 'Client Retry', link: '/docs/plugins/client-retry' },
144+
{ text: 'Compression', link: '/docs/plugins/compression' },
144145
{ text: 'Body Limit', link: '/docs/plugins/body-limit' },
145146
{ text: 'Simple CSRF Protection', link: '/docs/plugins/simple-csrf-protection' },
146147
{ text: 'Strict GET method', link: '/docs/plugins/strict-get-method' },

apps/content/docs/plugins/body-limit.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ const handler = new RPCHandler(router, {
2929
],
3030
})
3131
```
32+
33+
::: info
34+
The `handler` can be any supported oRPC handler, such as [RPCHandler](/docs/rpc-handler), [OpenAPIHandler](/docs/openapi/openapi-handler), or another custom handler.
35+
:::
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
title: Compression Plugin
3+
description: A plugin for oRPC that compresses response bodies.
4+
---
5+
6+
# Compression Plugin
7+
8+
The **Compression Plugin** compresses response bodies to reduce bandwidth usage and improve performance.
9+
10+
## Import
11+
12+
Depending on your adapter, import the corresponding plugin:
13+
14+
```ts
15+
import { CompressionPlugin } from '@orpc/server/node'
16+
import { CompressionPlugin } from '@orpc/server/fetch'
17+
```
18+
19+
## Setup
20+
21+
Add the plugin to your handler configuration:
22+
23+
```ts
24+
const handler = new RPCHandler(router, {
25+
plugins: [
26+
new CompressionPlugin(),
27+
],
28+
})
29+
```
30+
31+
::: info
32+
The `handler` can be any supported oRPC handler, such as [RPCHandler](/docs/rpc-handler), [OpenAPIHandler](/docs/openapi/openapi-handler), or another custom handler.
33+
:::

packages/server/build.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineBuildConfig } from 'unbuild'
2+
3+
export default defineBuildConfig({
4+
/**
5+
* Disable warnings as errors because we need to inline the `compression` package,
6+
* which is not ESModule-friendly.
7+
*/
8+
failOnWarn: false,
9+
})

packages/server/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@
135135
"cookie": "^1.0.2"
136136
},
137137
"devDependencies": {
138+
"@types/compression": "^1.8.1",
138139
"@types/ws": "^8.18.1",
140+
"compression": "^1.8.1",
139141
"crossws": "^0.4.1",
140142
"next": "^15.4.5",
141143
"supertest": "^7.1.4",

0 commit comments

Comments
 (0)