Skip to content

Commit 6670bad

Browse files
committed
Added docstring comment for the compress function
1 parent ea9cc34 commit 6670bad

File tree

1 file changed

+61
-4
lines changed

1 file changed

+61
-4
lines changed

packages/event-handler/src/rest/middleware/compress.ts

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,80 @@
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';
33
import {
44
CACHE_CONTROL_NO_TRANSFORM_REGEX,
55
COMPRESSIBLE_CONTENT_TYPE_REGEX,
66
COMPRESSION_ENCODING_TYPES,
77
} from '../constants.js';
88

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+
964
const compress = (options?: CompressionOptions): Middleware => {
1065
const threshold = options?.threshold ?? 1024;
1166

1267
return async (_, reqCtx, next) => {
1368
await next();
1469

1570
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');
1674

1775
// Check if response should be compressed
1876
if (
19-
reqCtx.res.headers.has('content-encoding') || // already encoded
20-
reqCtx.res.headers.has('transfer-encoding') || // already encoded or chunked
77+
isEncodedOrChunked ||
2178
reqCtx.request.method === 'HEAD' || // HEAD request
2279
(contentLength && Number(contentLength) < threshold) || // content-length below threshold
2380
!shouldCompress(reqCtx.res) || // not compressible type

0 commit comments

Comments
 (0)