|
1 |
| -import type { Middleware } from '@aws-lambda-powertools/event-handler/types'; |
2 |
| -import type { CompressionOptions } from 'src/types/rest.js'; |
| 1 | +import type { Middleware } from '../../types/index.js'; |
| 2 | +import type { CompressionOptions } from '../../types/rest.js'; |
3 | 3 | import {
|
4 | 4 | CACHE_CONTROL_NO_TRANSFORM_REGEX,
|
5 | 5 | COMPRESSIBLE_CONTENT_TYPE_REGEX,
|
6 | 6 | COMPRESSION_ENCODING_TYPES,
|
7 | 7 | } from '../constants.js';
|
8 | 8 |
|
| 9 | +/** |
| 10 | + * Compresses HTTP response bodies using standard compression algorithms. |
| 11 | + * |
| 12 | + * This middleware automatically compresses response bodies when they exceed |
| 13 | + * a specified threshold and the client supports compression. It respects |
| 14 | + * cache-control directives and only compresses appropriate content types. |
| 15 | + * |
| 16 | + * The middleware checks several conditions before compressing: |
| 17 | + * - Response is not already encoded or chunked |
| 18 | + * - Request method is not HEAD |
| 19 | + * - Content length exceeds the threshold |
| 20 | + * - Content type is compressible |
| 21 | + * - Cache-Control header doesn't contain no-transform |
| 22 | + * - Response has a body |
| 23 | + * |
| 24 | + * **Basic compression with default settings** |
| 25 | + * |
| 26 | + * @example |
| 27 | + * ```typescript |
| 28 | + * import { Router } from '@aws-lambda-powertools/event-handler'; |
| 29 | + * import { compress } from '@aws-lambda-powertools/event-handler/rest/middleware'; |
| 30 | + * |
| 31 | + * const app = new Router(); |
| 32 | + * |
| 33 | + * app.use(compress()); |
| 34 | + * |
| 35 | + * app.get('/api/data', async () => { |
| 36 | + * return { data: 'large response body...' }; |
| 37 | + * }); |
| 38 | + * ``` |
| 39 | + * |
| 40 | + * **Custom compression settings** |
| 41 | + * |
| 42 | + * @example |
| 43 | + * ```typescript |
| 44 | + * import { Router } from '@aws-lambda-powertools/event-handler'; |
| 45 | + * import { compress } from '@aws-lambda-powertools/event-handler/rest/middleware'; |
| 46 | + * |
| 47 | + * const app = new Router(); |
| 48 | + * |
| 49 | + * app.use(compress({ |
| 50 | + * threshold: 2048, |
| 51 | + * encoding: 'deflate' |
| 52 | + * })); |
| 53 | + * |
| 54 | + * app.get('/api/large-data', async () => { |
| 55 | + * return { data: 'very large response...' }; |
| 56 | + * }); |
| 57 | + * ``` |
| 58 | + * |
| 59 | + * @param options - Configuration options for compression behavior |
| 60 | + * @param options.threshold - Minimum response size in bytes to trigger compression (default: 1024) |
| 61 | + * @param options.encoding - Preferred compression encoding to use when client supports multiple formats |
| 62 | + */ |
| 63 | + |
9 | 64 | const compress = (options?: CompressionOptions): Middleware => {
|
10 | 65 | const threshold = options?.threshold ?? 1024;
|
11 | 66 |
|
12 | 67 | return async (_, reqCtx, next) => {
|
13 | 68 | await next();
|
14 | 69 |
|
15 | 70 | const contentLength = reqCtx.res.headers.get('content-length');
|
| 71 | + const isEncodedOrChunked = |
| 72 | + reqCtx.res.headers.has('content-encoding') || |
| 73 | + reqCtx.res.headers.has('transfer-encoding'); |
16 | 74 |
|
17 | 75 | // Check if response should be compressed
|
18 | 76 | if (
|
19 |
| - reqCtx.res.headers.has('content-encoding') || // already encoded |
20 |
| - reqCtx.res.headers.has('transfer-encoding') || // already encoded or chunked |
| 77 | + isEncodedOrChunked || |
21 | 78 | reqCtx.request.method === 'HEAD' || // HEAD request
|
22 | 79 | (contentLength && Number(contentLength) < threshold) || // content-length below threshold
|
23 | 80 | !shouldCompress(reqCtx.res) || // not compressible type
|
|
0 commit comments