@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.
- package/index.d.ts +7 -7
- package/index.js +111 -110
- package/package.json +72 -75
package/index.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import middy from
|
|
1
|
+
import type middy from "@middy/core";
|
|
2
2
|
|
|
3
3
|
interface Options {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
br?: any;
|
|
5
|
+
gzip?: any;
|
|
6
|
+
deflate?: any;
|
|
7
|
+
overridePreferredEncoding?: string[];
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
declare function httpContentEncoding
|
|
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
|
|
1
|
+
import { Readable } from "node:stream";
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
} from
|
|
4
|
+
createBrotliCompress as brotliCompressStream,
|
|
5
|
+
createDeflate as deflateCompressStream,
|
|
6
|
+
createGzip as gzipCompressStream,
|
|
7
|
+
} from "node:zlib";
|
|
8
8
|
|
|
9
|
-
import { normalizeHttpResponse } from
|
|
9
|
+
import { normalizeHttpResponse } from "@middy/util";
|
|
10
10
|
|
|
11
11
|
const contentEncodingStreams = {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
12
|
+
br: brotliCompressStream,
|
|
13
|
+
gzip: gzipCompressStream,
|
|
14
|
+
deflate: deflateCompressStream,
|
|
15
|
+
};
|
|
16
16
|
|
|
17
17
|
const defaults = {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
}
|