@middy/http-content-encoding 5.0.0-alpha.0 → 5.0.0-alpha.2
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 +3 -2
- package/index.js +17 -3
- package/package.json +4 -10
- package/index.cjs +0 -66
package/README.md
CHANGED
|
@@ -19,8 +19,9 @@
|
|
|
19
19
|
<a href="https://snyk.io/test/github/middyjs/middy">
|
|
20
20
|
<img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
|
|
21
21
|
</a>
|
|
22
|
-
<a href="https://
|
|
23
|
-
<img src="https://
|
|
22
|
+
<a href="https://github.com/middyjs/middy/actions/workflows/sast.yml">
|
|
23
|
+
<img src="https://github.com/middyjs/middy/actions/workflows/sast.yml/badge.svg
|
|
24
|
+
?branch=main&event=push" alt="CodeQL" style="max-width:100%;">
|
|
24
25
|
</a>
|
|
25
26
|
<a href="https://bestpractices.coreinfrastructure.org/projects/5280">
|
|
26
27
|
<img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
|
package/index.js
CHANGED
|
@@ -8,11 +8,16 @@ const contentEncodingStreams = {
|
|
|
8
8
|
};
|
|
9
9
|
const defaults = {
|
|
10
10
|
br: undefined,
|
|
11
|
+
// zstd: undefined,
|
|
11
12
|
gzip: undefined,
|
|
12
13
|
deflate: undefined,
|
|
13
14
|
overridePreferredEncoding: []
|
|
14
15
|
};
|
|
15
|
-
|
|
16
|
+
/*
|
|
17
|
+
* `zstd` disabled due to lack of support in browsers
|
|
18
|
+
* https://github.com/Fyrd/caniuse/issues/4065
|
|
19
|
+
* https://github.com/andrew-aladev/brotli-vs-zstd
|
|
20
|
+
*/ const httpContentEncodingMiddleware = (opts)=>{
|
|
16
21
|
const options = {
|
|
17
22
|
...defaults,
|
|
18
23
|
...opts
|
|
@@ -20,7 +25,8 @@ const httpContentEncodingMiddleware = (opts)=>{
|
|
|
20
25
|
const supportedContentEncodings = Object.keys(contentEncodingStreams);
|
|
21
26
|
const httpContentEncodingMiddlewareAfter = async (request)=>{
|
|
22
27
|
normalizeHttpResponse(request);
|
|
23
|
-
const {
|
|
28
|
+
const { context: { preferredEncoding, preferredEncodings }, response } = request;
|
|
29
|
+
// Encoding not supported OR already encoded
|
|
24
30
|
if (response.isBase64Encoded || !preferredEncoding || !supportedContentEncodings.includes(preferredEncoding) || !response.body) {
|
|
25
31
|
return;
|
|
26
32
|
}
|
|
@@ -32,12 +38,20 @@ const httpContentEncodingMiddleware = (opts)=>{
|
|
|
32
38
|
contentEncoding = encoding;
|
|
33
39
|
break;
|
|
34
40
|
}
|
|
41
|
+
// Support streamifyResponse
|
|
42
|
+
if (response.body?._readableState) {
|
|
43
|
+
request.response.headers['Content-Encoding'] = contentEncoding;
|
|
44
|
+
request.response.body.pipe(contentEncodingStream);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
35
47
|
const stream = Readable.from(response.body).pipe(contentEncodingStream);
|
|
36
48
|
const chunks = [];
|
|
37
49
|
for await (const chunk of stream){
|
|
38
50
|
chunks.push(chunk);
|
|
39
51
|
}
|
|
52
|
+
// TODO update to btoa if faster
|
|
40
53
|
const body = Buffer.concat(chunks).toString('base64');
|
|
54
|
+
// Only apply encoding if it's smaller
|
|
41
55
|
if (body.length < response.body.length) {
|
|
42
56
|
response.headers['Content-Encoding'] = contentEncoding;
|
|
43
57
|
response.body = body;
|
|
@@ -47,7 +61,7 @@ const httpContentEncodingMiddleware = (opts)=>{
|
|
|
47
61
|
};
|
|
48
62
|
const httpContentEncodingMiddlewareOnError = async (request)=>{
|
|
49
63
|
if (typeof request.response === 'undefined') return;
|
|
50
|
-
|
|
64
|
+
httpContentEncodingMiddlewareAfter(request);
|
|
51
65
|
};
|
|
52
66
|
return {
|
|
53
67
|
after: httpContentEncodingMiddlewareAfter,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-content-encoding",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.2",
|
|
4
4
|
"description": "Http content encoding middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -10,24 +10,18 @@
|
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"main": "./index.cjs",
|
|
14
13
|
"module": "./index.js",
|
|
15
14
|
"exports": {
|
|
16
15
|
".": {
|
|
17
16
|
"import": {
|
|
18
17
|
"types": "./index.d.ts",
|
|
19
18
|
"default": "./index.js"
|
|
20
|
-
},
|
|
21
|
-
"require": {
|
|
22
|
-
"types": "./index.d.ts",
|
|
23
|
-
"default": "./index.cjs"
|
|
24
19
|
}
|
|
25
20
|
}
|
|
26
21
|
},
|
|
27
22
|
"types": "index.d.ts",
|
|
28
23
|
"files": [
|
|
29
24
|
"index.js",
|
|
30
|
-
"index.cjs",
|
|
31
25
|
"index.d.ts"
|
|
32
26
|
],
|
|
33
27
|
"scripts": {
|
|
@@ -69,10 +63,10 @@
|
|
|
69
63
|
"url": "https://github.com/sponsors/willfarrell"
|
|
70
64
|
},
|
|
71
65
|
"dependencies": {
|
|
72
|
-
"@middy/util": "5.0.0-alpha.
|
|
66
|
+
"@middy/util": "5.0.0-alpha.2"
|
|
73
67
|
},
|
|
74
68
|
"devDependencies": {
|
|
75
|
-
"@middy/core": "5.0.0-alpha.
|
|
69
|
+
"@middy/core": "5.0.0-alpha.2"
|
|
76
70
|
},
|
|
77
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
|
|
78
72
|
}
|
package/index.cjs
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(module, "exports", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: ()=>_default
|
|
8
|
-
});
|
|
9
|
-
const _nodeStream = require("node:stream");
|
|
10
|
-
const _nodeZlib = require("node:zlib");
|
|
11
|
-
const _util = require("@middy/util");
|
|
12
|
-
const contentEncodingStreams = {
|
|
13
|
-
br: _nodeZlib.createBrotliCompress,
|
|
14
|
-
gzip: _nodeZlib.createGzip,
|
|
15
|
-
deflate: _nodeZlib.createDeflate
|
|
16
|
-
};
|
|
17
|
-
const defaults = {
|
|
18
|
-
br: undefined,
|
|
19
|
-
gzip: undefined,
|
|
20
|
-
deflate: undefined,
|
|
21
|
-
overridePreferredEncoding: []
|
|
22
|
-
};
|
|
23
|
-
const httpContentEncodingMiddleware = (opts)=>{
|
|
24
|
-
const options = {
|
|
25
|
-
...defaults,
|
|
26
|
-
...opts
|
|
27
|
-
};
|
|
28
|
-
const supportedContentEncodings = Object.keys(contentEncodingStreams);
|
|
29
|
-
const httpContentEncodingMiddlewareAfter = async (request)=>{
|
|
30
|
-
(0, _util.normalizeHttpResponse)(request);
|
|
31
|
-
const { event: { preferredEncoding , preferredEncodings } , response } = request;
|
|
32
|
-
if (response.isBase64Encoded || !preferredEncoding || !supportedContentEncodings.includes(preferredEncoding) || !response.body) {
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
let contentEncodingStream = contentEncodingStreams[preferredEncoding](options[preferredEncoding]);
|
|
36
|
-
let contentEncoding = preferredEncoding;
|
|
37
|
-
for (const encoding of options.overridePreferredEncoding){
|
|
38
|
-
if (!preferredEncodings.includes(encoding)) continue;
|
|
39
|
-
contentEncodingStream = contentEncodingStreams[encoding](options[encoding]);
|
|
40
|
-
contentEncoding = encoding;
|
|
41
|
-
break;
|
|
42
|
-
}
|
|
43
|
-
const stream = _nodeStream.Readable.from(response.body).pipe(contentEncodingStream);
|
|
44
|
-
const chunks = [];
|
|
45
|
-
for await (const chunk of stream){
|
|
46
|
-
chunks.push(chunk);
|
|
47
|
-
}
|
|
48
|
-
const body = Buffer.concat(chunks).toString('base64');
|
|
49
|
-
if (body.length < response.body.length) {
|
|
50
|
-
response.headers['Content-Encoding'] = contentEncoding;
|
|
51
|
-
response.body = body;
|
|
52
|
-
response.isBase64Encoded = true;
|
|
53
|
-
}
|
|
54
|
-
request.response = response;
|
|
55
|
-
};
|
|
56
|
-
const httpContentEncodingMiddlewareOnError = async (request)=>{
|
|
57
|
-
if (typeof request.response === 'undefined') return;
|
|
58
|
-
return httpContentEncodingMiddlewareAfter(request);
|
|
59
|
-
};
|
|
60
|
-
return {
|
|
61
|
-
after: httpContentEncodingMiddlewareAfter,
|
|
62
|
-
onError: httpContentEncodingMiddlewareOnError
|
|
63
|
-
};
|
|
64
|
-
};
|
|
65
|
-
const _default = httpContentEncodingMiddleware;
|
|
66
|
-
|