lambda-compression 0.2.26 → 0.2.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -45,6 +45,71 @@ export const handler: ProxyHandler = async (event, context) => {
45
45
  };
46
46
  ```
47
47
 
48
+ ## How It Works
49
+
50
+ The `compress` function inspects the `accept-encoding` header from the incoming request to determine which compression algorithms the client supports. It prioritises encodings in the following order:
51
+
52
+ 1. **Brotli** (`br`) - Most efficient compression
53
+ 2. **Gzip** (`gzip`) - Widely supported
54
+ 3. **Deflate** (`deflate`) - Basic compression
55
+
56
+ If a supported encoding is found, the response body is compressed using the corresponding algorithm, the `content-encoding` header is set, and the body is base64-encoded as required by AWS Lambda. If no supported encodings are present or the response has no body, the original result is returned unchanged.
57
+
58
+ ## Supported Encodings
59
+
60
+ - `br` (Brotli): Offers the best compression ratio, ideal for modern browsers and clients.
61
+ - `gzip`: Balanced compression and speed, supported by most HTTP clients.
62
+ - `deflate`: Basic compression, less efficient but still useful for compatibility.
63
+
64
+ The library uses Node.js's built-in `zlib` module for compression.
65
+
66
+ ## API Reference
67
+
68
+ ### `compress(event, result)`
69
+
70
+ Compresses the response body based on the client's accepted encodings.
71
+
72
+ **Parameters:**
73
+ - `event` (APIGatewayProxyEventV2): The Lambda event object containing request headers.
74
+ - `result` (APIGatewayProxyStructuredResultV2): The response object to be compressed.
75
+
76
+ **Returns:** APIGatewayProxyStructuredResultV2 - The modified response with compressed body if applicable.
77
+
78
+ **Notes:**
79
+ - The response body must be a string. Binary data should not be passed directly.
80
+ - Compression only occurs if the body exists and a supported encoding is accepted.
81
+ - The function modifies the result object in place and returns it.
82
+
83
+ ## Requirements and Limitations
84
+
85
+ - **Node.js Environment:** Requires Node.js with zlib support (available in AWS Lambda runtime).
86
+ - **Body Type:** Only string bodies are supported for compression. If your response contains binary data, handle encoding separately.
87
+ - **Performance:** Compression adds CPU overhead. For small responses (<1KB), compression may not provide benefits and could increase response size due to encoding.
88
+ - **Client Support:** Ensure clients send appropriate `accept-encoding` headers.
89
+
90
+ ## Examples
91
+
92
+ ### Basic JSON Response
93
+ ```typescript
94
+ return compress(event, {
95
+ statusCode: 200,
96
+ headers: { 'Content-Type': 'application/json' },
97
+ body: JSON.stringify({ message: 'Hello World' }),
98
+ });
99
+ ```
100
+
101
+ ### HTML Response
102
+ ```typescript
103
+ return compress(event, {
104
+ statusCode: 200,
105
+ headers: { 'Content-Type': 'text/html' },
106
+ body: '<html><body><h1>Hello</h1></body></html>',
107
+ });
108
+ ```
109
+
110
+ ### No Compression (Client doesn't support)
111
+ If the client sends `accept-encoding: identity` or no header, the response remains uncompressed.
112
+
48
113
  ## Also See
49
114
 
50
115
  - [compression.js in lambda-api project](https://github.com/jeremydaly/lambda-api/blob/main/lib/compression.js)
@@ -0,0 +1,3 @@
1
+ import type { APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2 } from 'aws-lambda';
2
+ export declare const compress: (event: APIGatewayProxyEventV2, result: APIGatewayProxyStructuredResultV2) => APIGatewayProxyStructuredResultV2;
3
+ //# sourceMappingURL=lambdaCompression.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lambdaCompression.d.ts","sourceRoot":"","sources":["../../src/lambdaCompression.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,iCAAiC,EAAE,MAAM,YAAY,CAAC;AAI5F,eAAO,MAAM,QAAQ,GACnB,OAAO,sBAAsB,EAC7B,QAAQ,iCAAiC,KACxC,iCA0CF,CAAC"}
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.compress = void 0;
7
+ const zlib_1 = __importDefault(require("zlib"));
8
+ const compress = (event, result) => {
9
+ // when there is no body, there is nothing to compress
10
+ if (!result.body) {
11
+ return result;
12
+ }
13
+ const acceptEncodingHeader = event.headers['accept-encoding'];
14
+ // determine accepted encodings
15
+ const encodings = new Set();
16
+ if (acceptEncodingHeader) {
17
+ acceptEncodingHeader.split(',').forEach((encoding) => {
18
+ encodings.add(encoding.toLowerCase().trim());
19
+ });
20
+ }
21
+ if (!result.headers) {
22
+ result.headers = {};
23
+ }
24
+ if (encodings.has('br')) {
25
+ result.headers['content-encoding'] = 'br';
26
+ result.isBase64Encoded = true;
27
+ result.body = zlib_1.default.brotliCompressSync(result.body).toString('base64');
28
+ return result;
29
+ }
30
+ if (encodings.has('gzip')) {
31
+ result.headers['content-encoding'] = 'gzip';
32
+ result.isBase64Encoded = true;
33
+ result.body = zlib_1.default.gzipSync(result.body).toString('base64');
34
+ return result;
35
+ }
36
+ if (encodings.has('deflate')) {
37
+ result.headers['content-encoding'] = 'deflate ';
38
+ result.isBase64Encoded = true;
39
+ result.body = zlib_1.default.deflateSync(result.body).toString('base64');
40
+ return result;
41
+ }
42
+ // if there are no supported encodings, return unencoded message
43
+ return result;
44
+ };
45
+ exports.compress = compress;
46
+ //# sourceMappingURL=lambdaCompression.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lambdaCompression.js","sourceRoot":"","sources":["../../src/lambdaCompression.ts"],"names":[],"mappings":";;;;;;AAEA,gDAAwB;AAEjB,MAAM,QAAQ,GAAG,CACtB,KAA6B,EAC7B,MAAyC,EACN,EAAE;IACrC,sDAAsD;IACtD,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;QACjB,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,MAAM,oBAAoB,GAAG,KAAK,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC;IAE9D,+BAA+B;IAC/B,MAAM,SAAS,GAAgB,IAAI,GAAG,EAAE,CAAC;IACzC,IAAI,oBAAoB,EAAE,CAAC;QACzB,oBAAoB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;Y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C,CAAC;AA7CW,QAAA,QAAQ,YA6CnB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lambda-compression",
3
- "version": "0.2.26",
3
+ "version": "0.2.27",
4
4
  "description": "HTTP payload compression for AWS Lambda functions",
5
5
  "keywords": [
6
6
  "lambda",