@middy/http-content-encoding 6.1.6 → 6.2.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.
Files changed (3) hide show
  1. package/index.d.ts +7 -7
  2. package/index.js +111 -110
  3. package/package.json +72 -75
package/index.d.ts CHANGED
@@ -1,12 +1,12 @@
1
- import middy from '@middy/core'
1
+ import type middy from "@middy/core";
2
2
 
3
3
  interface Options {
4
- br?: any
5
- gzip?: any
6
- deflate?: any
7
- overridePreferredEncoding?: string[]
4
+ br?: any;
5
+ gzip?: any;
6
+ deflate?: any;
7
+ overridePreferredEncoding?: string[];
8
8
  }
9
9
 
10
- declare function httpContentEncoding (options?: Options): middy.MiddlewareObj
10
+ declare function httpContentEncoding(options?: Options): middy.MiddlewareObj;
11
11
 
12
- export default httpContentEncoding
12
+ export default httpContentEncoding;
package/index.js CHANGED
@@ -1,30 +1,30 @@
1
- import { Readable } from 'node:stream'
1
+ import { Readable } from "node:stream";
2
2
 
3
3
  import {
4
- createBrotliCompress as brotliCompressStream,
5
- createGzip as gzipCompressStream,
6
- createDeflate as deflateCompressStream
7
- } from 'node:zlib'
4
+ createBrotliCompress as brotliCompressStream,
5
+ createDeflate as deflateCompressStream,
6
+ createGzip as gzipCompressStream,
7
+ } from "node:zlib";
8
8
 
9
- import { normalizeHttpResponse } from '@middy/util'
9
+ import { normalizeHttpResponse } from "@middy/util";
10
10
 
11
11
  const contentEncodingStreams = {
12
- br: brotliCompressStream,
13
- gzip: gzipCompressStream,
14
- deflate: deflateCompressStream
15
- }
12
+ br: brotliCompressStream,
13
+ gzip: gzipCompressStream,
14
+ deflate: deflateCompressStream,
15
+ };
16
16
 
17
17
  const defaults = {
18
- br: undefined,
19
- // zstd: undefined,
20
- gzip: undefined,
21
- deflate: undefined,
22
- overridePreferredEncoding: []
23
- }
18
+ br: undefined,
19
+ // zstd: undefined,
20
+ gzip: undefined,
21
+ deflate: undefined,
22
+ overridePreferredEncoding: [],
23
+ };
24
24
 
25
25
  export const getStream = (preferredEncoding) => {
26
- return contentEncodingStreams[preferredEncoding]()
27
- }
26
+ return contentEncodingStreams[preferredEncoding]();
27
+ };
28
28
 
29
29
  /*
30
30
  * `zstd` disabled due to lack of support in nodejs
@@ -32,98 +32,99 @@ export const getStream = (preferredEncoding) => {
32
32
  */
33
33
 
34
34
  const httpContentEncodingMiddleware = (opts) => {
35
- const options = { ...defaults, ...opts }
36
-
37
- const supportedContentEncodings = Object.keys(contentEncodingStreams)
38
-
39
- const httpContentEncodingMiddlewareAfter = async (request) => {
40
- normalizeHttpResponse(request)
41
- const {
42
- context: { preferredEncoding, preferredEncodings },
43
- response
44
- } = request
45
-
46
- // Encoding not supported, already encoded, or doesn't need to'
47
- const eventCacheControl =
48
- request.event?.headers?.['cache-control'] ??
49
- request.event?.headers?.['Cache-Control']
50
- if (eventCacheControl?.includes('no-transform')) {
51
- addHeaderPart(response, 'Cache-Control', 'no-transform')
52
- }
53
- const responseCacheControl =
54
- response.headers['Cache-Control'] ?? response.headers['cache-control']
55
- if (
56
- response.isBase64Encoded ||
57
- !preferredEncoding ||
58
- !supportedContentEncodings.includes(preferredEncoding) ||
59
- !response.body ||
60
- (typeof response.body !== 'string' &&
61
- !Buffer.isBuffer(response.body) &&
62
- !response.body?._readableState) ||
63
- responseCacheControl?.includes('no-transform')
64
- ) {
65
- return
66
- }
67
-
68
- let contentEncodingStream = contentEncodingStreams[preferredEncoding](
69
- options[preferredEncoding]
70
- )
71
- let contentEncoding = preferredEncoding
72
- for (const encoding of options.overridePreferredEncoding) {
73
- if (!preferredEncodings.includes(encoding)) continue
74
- contentEncodingStream = contentEncodingStreams[encoding](
75
- options[encoding]
76
- )
77
- contentEncoding = encoding
78
- break
79
- }
80
-
81
- // Support streamifyResponse
82
- if (response.body?._readableState) {
83
- request.response.headers['Content-Encoding'] = contentEncoding
84
- request.response.body = request.response.body.pipe(contentEncodingStream)
85
- addHeaderPart(response, 'Vary', 'Accept-Encoding')
86
- return
87
- }
88
-
89
- const stream = Readable.from(response.body).pipe(contentEncodingStream)
90
-
91
- const chunks = []
92
- for await (const chunk of stream) {
93
- chunks.push(chunk)
94
- }
95
-
96
- const body = Buffer.concat(chunks).toString('base64')
97
-
98
- // Only apply encoding if it's smaller
99
- if (body.length < response.body.length) {
100
- response.headers['Content-Encoding'] = contentEncoding
101
- response.body = body
102
- response.isBase64Encoded = true
103
- addHeaderPart(response, 'Vary', 'Accept-Encoding')
104
- }
105
-
106
- request.response = response
107
- }
108
-
109
- const httpContentEncodingMiddlewareOnError = async (request) => {
110
- if (typeof request.response === 'undefined') return
111
- await httpContentEncodingMiddlewareAfter(request)
112
- }
113
-
114
- return {
115
- after: httpContentEncodingMiddlewareAfter,
116
- onError: httpContentEncodingMiddlewareOnError
117
- }
118
- }
35
+ const options = { ...defaults, ...opts };
36
+
37
+ const supportedContentEncodings = Object.keys(contentEncodingStreams);
38
+
39
+ const httpContentEncodingMiddlewareAfter = async (request) => {
40
+ normalizeHttpResponse(request);
41
+ const {
42
+ context: { preferredEncoding, preferredEncodings },
43
+ response,
44
+ } = request;
45
+
46
+ // Encoding not supported, already encoded, or doesn't need to'
47
+ const eventCacheControl =
48
+ request.event?.headers?.["cache-control"] ??
49
+ request.event?.headers?.["Cache-Control"];
50
+ if (eventCacheControl?.includes("no-transform")) {
51
+ addHeaderPart(response, "Cache-Control", "no-transform");
52
+ }
53
+ const responseCacheControl =
54
+ response.headers["Cache-Control"] ?? response.headers["cache-control"];
55
+ if (
56
+ response.isBase64Encoded ||
57
+ !preferredEncoding ||
58
+ !supportedContentEncodings.includes(preferredEncoding) ||
59
+ !response.body ||
60
+ (typeof response.body !== "string" &&
61
+ !Buffer.isBuffer(response.body) &&
62
+ !response.body?._readableState) ||
63
+ responseCacheControl?.includes("no-transform")
64
+ ) {
65
+ return;
66
+ }
67
+
68
+ let contentEncodingStream = contentEncodingStreams[preferredEncoding](
69
+ options[preferredEncoding],
70
+ );
71
+ let contentEncoding = preferredEncoding;
72
+ for (const encoding of options.overridePreferredEncoding) {
73
+ if (!preferredEncodings.includes(encoding)) continue;
74
+ contentEncodingStream = contentEncodingStreams[encoding](
75
+ options[encoding],
76
+ );
77
+ contentEncoding = encoding;
78
+ break;
79
+ }
80
+
81
+ // Support streamifyResponse
82
+ if (response.body?._readableState) {
83
+ request.response.headers["Content-Encoding"] = contentEncoding;
84
+ request.response.body = request.response.body.pipe(contentEncodingStream);
85
+ addHeaderPart(response, "Vary", "Accept-Encoding");
86
+ return;
87
+ }
88
+
89
+ const stream = Readable.from(response.body).pipe(contentEncodingStream);
90
+
91
+ const chunks = [];
92
+ for await (const chunk of stream) {
93
+ chunks.push(chunk);
94
+ }
95
+
96
+ const body = Buffer.concat(chunks).toString("base64");
97
+
98
+ // Only apply encoding if it's smaller
99
+ if (body.length < response.body.length) {
100
+ response.headers["Content-Encoding"] = contentEncoding;
101
+ response.body = body;
102
+ response.isBase64Encoded = true;
103
+ addHeaderPart(response, "Vary", "Accept-Encoding");
104
+ }
105
+
106
+ request.response = response;
107
+ };
108
+
109
+ const httpContentEncodingMiddlewareOnError = async (request) => {
110
+ if (typeof request.response === "undefined") return;
111
+ await httpContentEncodingMiddlewareAfter(request);
112
+ };
113
+
114
+ return {
115
+ after: httpContentEncodingMiddlewareAfter,
116
+ onError: httpContentEncodingMiddlewareOnError,
117
+ };
118
+ };
119
119
 
120
120
  // header in offical name, lowercase varient handeled
121
121
  const addHeaderPart = (response, header, value) => {
122
- const headerLower = header.toLowerCase()
123
- header = response.headers[headerLower] ? headerLower : header
124
- response.headers[header] ??= ''
125
- response.headers[header] &&= response.headers[header] + ', '
126
- response.headers[header] += value
127
- }
128
-
129
- export default httpContentEncodingMiddleware
122
+ const headerLower = header.toLowerCase();
123
+ const sanitizedHeader = response.headers[headerLower] ? headerLower : header;
124
+ response.headers[sanitizedHeader] ??= "";
125
+ response.headers[sanitizedHeader] &&=
126
+ `${response.headers[sanitizedHeader]}, `;
127
+ response.headers[sanitizedHeader] += value;
128
+ };
129
+
130
+ export default httpContentEncodingMiddleware;
package/package.json CHANGED
@@ -1,77 +1,74 @@
1
1
  {
2
- "name": "@middy/http-content-encoding",
3
- "version": "6.1.6",
4
- "description": "Http content encoding middleware for the middy framework",
5
- "type": "module",
6
- "engines": {
7
- "node": ">=20"
8
- },
9
- "engineStrict": true,
10
- "publishConfig": {
11
- "access": "public"
12
- },
13
- "module": "./index.js",
14
- "exports": {
15
- ".": {
16
- "import": {
17
- "types": "./index.d.ts",
18
- "default": "./index.js"
19
- },
20
- "require": {
21
- "default": "./index.js"
22
- }
23
- }
24
- },
25
- "types": "index.d.ts",
26
- "files": [
27
- "index.js",
28
- "index.d.ts"
29
- ],
30
- "scripts": {
31
- "test": "npm run test:unit && npm run test:fuzz",
32
- "test:unit": "node --test __tests__/index.js",
33
- "test:fuzz": "node --test __tests__/fuzz.js",
34
- "test:benchmark": "node __benchmarks__/index.js"
35
- },
36
- "license": "MIT",
37
- "keywords": [
38
- "Lambda",
39
- "Middleware",
40
- "Serverless",
41
- "Framework",
42
- "AWS",
43
- "AWS Lambda",
44
- "Middy",
45
- "HTTP",
46
- "API",
47
- "Content Encoding",
48
- "gzip",
49
- "brotli",
50
- "deflate"
51
- ],
52
- "author": {
53
- "name": "Middy contributors",
54
- "url": "https://github.com/middyjs/middy/graphs/contributors"
55
- },
56
- "repository": {
57
- "type": "git",
58
- "url": "git+https://github.com/middyjs/middy.git",
59
- "directory": "packages/http-content-encoding"
60
- },
61
- "bugs": {
62
- "url": "https://github.com/middyjs/middy/issues"
63
- },
64
- "homepage": "https://middy.js.org",
65
- "funding": {
66
- "type": "github",
67
- "url": "https://github.com/sponsors/willfarrell"
68
- },
69
- "dependencies": {
70
- "@middy/util": "6.1.6"
71
- },
72
- "devDependencies": {
73
- "@datastream/core": "0.0.40",
74
- "@middy/core": "6.1.6"
75
- },
76
- "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
2
+ "name": "@middy/http-content-encoding",
3
+ "version": "6.2.1",
4
+ "description": "Http content encoding middleware for the middy framework",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=20"
8
+ },
9
+ "engineStrict": true,
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "module": "./index.js",
14
+ "exports": {
15
+ ".": {
16
+ "import": {
17
+ "types": "./index.d.ts",
18
+ "default": "./index.js"
19
+ },
20
+ "require": {
21
+ "default": "./index.js"
22
+ }
23
+ }
24
+ },
25
+ "types": "index.d.ts",
26
+ "files": ["index.js", "index.d.ts"],
27
+ "scripts": {
28
+ "test": "npm run test:unit && npm run test:fuzz",
29
+ "test:unit": "node --test",
30
+ "test:fuzz": "node --test index.fuzz.js",
31
+ "test:perf": "node --test index.perf.js"
32
+ },
33
+ "license": "MIT",
34
+ "keywords": [
35
+ "Lambda",
36
+ "Middleware",
37
+ "Serverless",
38
+ "Framework",
39
+ "AWS",
40
+ "AWS Lambda",
41
+ "Middy",
42
+ "HTTP",
43
+ "API",
44
+ "Content Encoding",
45
+ "gzip",
46
+ "brotli",
47
+ "deflate"
48
+ ],
49
+ "author": {
50
+ "name": "Middy contributors",
51
+ "url": "https://github.com/middyjs/middy/graphs/contributors"
52
+ },
53
+ "repository": {
54
+ "type": "git",
55
+ "url": "git+https://github.com/middyjs/middy.git",
56
+ "directory": "packages/http-content-encoding"
57
+ },
58
+ "bugs": {
59
+ "url": "https://github.com/middyjs/middy/issues"
60
+ },
61
+ "homepage": "https://middy.js.org",
62
+ "funding": {
63
+ "type": "github",
64
+ "url": "https://github.com/sponsors/willfarrell"
65
+ },
66
+ "dependencies": {
67
+ "@middy/util": "6.2.1"
68
+ },
69
+ "devDependencies": {
70
+ "@datastream/core": "0.0.40",
71
+ "@middy/core": "6.2.1"
72
+ },
73
+ "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
77
74
  }