lambda-compression 0.1.1

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 ADDED
@@ -0,0 +1,39 @@
1
+ # Lambda HTTP Payload Compression
2
+
3
+ This library provides a wrapper that can be used to compress content in responses when using the [AWS HTTP API](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html).
4
+
5
+ The library supports compression with `br`, `gzip` and `deflate`. It will return responses with compressed data matching the supported `accept-encoding` header provided by the client.
6
+
7
+ ## Usage
8
+
9
+ This library provides only one method `compress` that accepts two parameters with the respective types of `APIGatewayProxyEventV2` and `APIGatewayProxyStructuredResultV2` (for more details on these types, see [TypeScript Types for AWS Lambda](https://maxrohde.com/2022/01/02/typescript-types-for-aws-lambda/)).
10
+
11
+ Simply call the `compress` method as follows before returning the result of your Lambda.
12
+
13
+ ```typescript
14
+ import { compress } from 'lambda-compression';
15
+
16
+ import {
17
+ Handler,
18
+ APIGatewayProxyEventV2,
19
+ APIGatewayProxyResultV2,
20
+ } from 'aws-lambda';
21
+
22
+ type ProxyHandler = Handler<APIGatewayProxyEventV2, APIGatewayProxyResultV2>;
23
+
24
+ export const handler: ProxyHandler = async (event, context) => {
25
+ return compress(event, {
26
+ statusCode: 201,
27
+ headers: {
28
+ 'Content-Type': 'application/json',
29
+ },
30
+ body: '{"data":"hello"}',
31
+ });
32
+ };
33
+ ```
34
+
35
+ ## Also See
36
+
37
+ - [compression.js in lambda-api project](https://github.com/jeremydaly/lambda-api/blob/main/lib/compression.js)
38
+ - [Serverless Content Encoding for Serverless](https://www.npmjs.com/package/serverless-content-encoding)
39
+ - [serverless-content-encoding](https://github.com/dong-dohai/serverless-content-encoding) - no longer maintained
@@ -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,EACV,sBAAsB,EACtB,iCAAiC,EAClC,MAAM,YAAY,CAAC;AAIpB,eAAO,MAAM,QAAQ,UACZ,sBAAsB,UACrB,iCAAiC,KACxC,iCAyCF,CAAC"}
@@ -0,0 +1,45 @@
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
+ throw new Error(`Encodings not supported ${encodings}`);
43
+ };
44
+ exports.compress = compress;
45
+ //# sour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AI,GAAG,cAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAC5D,OAAO,MAAM,CAAC;KACf;IAED,IAAI,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE;QAC5B,MAAM,CAAC,OAAO,CAAC,kBAAkB,CAAC,GAAG,UAAU,CAAC;QAChD,MAAM,CAAC,eAAe,GAAG,IAAI,CAAC;QAC9B,MAAM,CAAC,IAAI,GAAG,cAAI,CAAC,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAC/D,OAAO,MAAM,CAAC;KACf;IAED,MAAM,IAAI,KAAK,CAAC,2BAA2B,SAAS,EAAE,CAAC,CAAC;AAC1D,CAAC,CAAC;AA5CW,QAAA,QAAQ,YA4CnB"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "lambda-compression",
3
+ "version": "0.1.1",
4
+ "description": "HTTP payload compression for AWS Lambda functions",
5
+ "keywords": [
6
+ "lambda",
7
+ "api-gateway",
8
+ "node",
9
+ "javascript",
10
+ "aws"
11
+ ],
12
+ "homepage": "https://github.com/goldstack/goldstack/tree/master/workspaces/utils/packages/lambda-compression#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/goldstack/goldstack/issues"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/goldstack/goldstack.git"
19
+ },
20
+ "license": "MIT",
21
+ "author": "Max Rohde",
22
+ "sideEffects": false,
23
+ "main": "dist/src/lambdaCompression.js",
24
+ "scripts": {
25
+ "build": "yarn clean && yarn compile",
26
+ "build:watch": "yarn clean && yarn compile-watch",
27
+ "clean": "rimraf ./dist",
28
+ "compile": "tsc -p tsconfig.json",
29
+ "compile-watch": "tsc -p tsconfig.json --watch",
30
+ "compile-watch:light": "nodemon --watch ./src/ -e '*' --exec 'yarn compile'",
31
+ "coverage": "jest --collect-coverage --passWithNoTests --config=./jest.config.js --runInBand",
32
+ "prepublishOnly": "yarn run build",
33
+ "publish": "utils-git changed --exec \"yarn npm publish $@\"",
34
+ "test-ci": "jest --passWithNoTests --config=./jest.config.js --runInBand",
35
+ "version:apply": "utils-git changed --exec \"yarn version $@ && yarn version apply\""
36
+ },
37
+ "devDependencies": {
38
+ "@goldstack/utils-git": "0.1.36",
39
+ "@types/aws-lambda": "^8.10.88",
40
+ "@types/jest": "^27.5.1",
41
+ "@types/node": "^17.0.33",
42
+ "jest": "^28.1.0",
43
+ "rimraf": "^3.0.2",
44
+ "ts-jest": "^28.0.2",
45
+ "typescript": "^4.7.4"
46
+ },
47
+ "publishConfig": {
48
+ "main": "dist/src/lambdaCompression.js"
49
+ }
50
+ }