feat(typedserver): Add configurable response compression (Brotli + Gzip) with defaults enabled and documentation
This commit is contained in:
73
readme.md
73
readme.md
@@ -341,6 +341,75 @@ await server.start();
|
||||
| `Cross-Origin-Embedder-Policy` | `crossOriginEmbedderPolicy` | Controls cross-origin embedding |
|
||||
| `Cross-Origin-Resource-Policy` | `crossOriginResourcePolicy` | Controls cross-origin resource sharing |
|
||||
|
||||
## 🗜️ Compression
|
||||
|
||||
TypedServer supports automatic response compression using Brotli and Gzip. Compression is powered by smartserve and enabled by default.
|
||||
|
||||
### Global Configuration
|
||||
|
||||
```typescript
|
||||
import { TypedServer } from '@api.global/typedserver';
|
||||
|
||||
const server = new TypedServer({
|
||||
serveDir: './dist',
|
||||
cors: true,
|
||||
|
||||
// Enable with defaults (brotli + gzip, threshold: 1024 bytes)
|
||||
compression: true,
|
||||
|
||||
// Or disable completely
|
||||
compression: false,
|
||||
|
||||
// Or configure in detail
|
||||
compression: {
|
||||
enabled: true,
|
||||
algorithms: ['br', 'gzip'], // Preferred order
|
||||
threshold: 1024, // Min size to compress (bytes)
|
||||
level: 4, // Compression level (1-11 for brotli, 1-9 for gzip)
|
||||
exclude: ['/api/stream/*'], // Skip these paths
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
### Per-Route Control with Decorators
|
||||
|
||||
Use `@Compress` and `@NoCompress` decorators for fine-grained control:
|
||||
|
||||
```typescript
|
||||
import * as smartserve from '@push.rocks/smartserve';
|
||||
|
||||
@smartserve.Route('/api')
|
||||
class ApiController {
|
||||
// Force maximum compression for this endpoint
|
||||
@smartserve.Get('/large-data')
|
||||
@smartserve.Compress({ level: 11 })
|
||||
async getLargeData(ctx: smartserve.IRequestContext): Promise<Response> {
|
||||
return new Response(JSON.stringify(largeDataset));
|
||||
}
|
||||
|
||||
// Disable compression for streaming endpoint
|
||||
@smartserve.Get('/events')
|
||||
@smartserve.NoCompress()
|
||||
async streamEvents(ctx: smartserve.IRequestContext): Promise<Response> {
|
||||
// Server-Sent Events shouldn't be compressed
|
||||
return new Response(eventStream, {
|
||||
headers: { 'Content-Type': 'text/event-stream' },
|
||||
});
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Compression Options Reference
|
||||
|
||||
| Option | Type | Default | Description |
|
||||
|--------|------|---------|-------------|
|
||||
| `enabled` | `boolean` | `true` | Enable/disable compression |
|
||||
| `algorithms` | `string[]` | `['br', 'gzip']` | Preferred algorithms in order |
|
||||
| `threshold` | `number` | `1024` | Minimum response size (bytes) to compress |
|
||||
| `level` | `number` | `4` | Compression level (1-11 for brotli, 1-9 for gzip) |
|
||||
| `compressibleTypes` | `string[]` | auto | MIME types to compress |
|
||||
| `exclude` | `string[]` | `[]` | Path patterns to skip |
|
||||
|
||||
## 📋 Configuration Reference
|
||||
|
||||
### IServerOptions
|
||||
@@ -366,6 +435,7 @@ await server.start();
|
||||
| `feedMetadata` | `object` | - | RSS feed metadata options |
|
||||
| `blockWaybackMachine` | `boolean` | `false` | Block Wayback Machine archiving |
|
||||
| `securityHeaders` | `ISecurityHeaders` | - | Security headers configuration |
|
||||
| `compression` | `ICompressionConfig \| boolean` | `true` | Response compression configuration |
|
||||
|
||||
## 🏗️ Package Exports
|
||||
|
||||
@@ -408,6 +478,9 @@ const websiteServer = new utilityservers.UtilityWebsiteServer({
|
||||
xContentTypeOptions: true,
|
||||
},
|
||||
|
||||
// Compression (enabled by default)
|
||||
compression: true, // or { level: 6, threshold: 512 }
|
||||
|
||||
// Other options
|
||||
cors: true, // default: true
|
||||
forceSsl: false, // default: false
|
||||
|
||||
Reference in New Issue
Block a user