@middy/http-content-encoding 5.0.0-alpha.1 → 5.0.0-rc.0
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.js +65 -96
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,103 +1,72 @@
|
|
|
1
|
-
import { Readable } from 'node:stream'
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
createBrotliCompress as brotliCompressStream,
|
|
5
|
-
createGzip as gzipCompressStream,
|
|
6
|
-
createDeflate as deflateCompressStream
|
|
7
|
-
} from 'node:zlib'
|
|
8
|
-
|
|
9
|
-
import { normalizeHttpResponse } from '@middy/util'
|
|
10
|
-
|
|
1
|
+
import { Readable } from 'node:stream';
|
|
2
|
+
import { createBrotliCompress as brotliCompressStream, createGzip as gzipCompressStream, createDeflate as deflateCompressStream } from 'node:zlib';
|
|
3
|
+
import { normalizeHttpResponse } from '@middy/util';
|
|
11
4
|
const contentEncodingStreams = {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
5
|
+
br: brotliCompressStream,
|
|
6
|
+
gzip: gzipCompressStream,
|
|
7
|
+
deflate: deflateCompressStream
|
|
8
|
+
};
|
|
17
9
|
const defaults = {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
10
|
+
br: undefined,
|
|
11
|
+
// zstd: undefined,
|
|
12
|
+
gzip: undefined,
|
|
13
|
+
deflate: undefined,
|
|
14
|
+
overridePreferredEncoding: []
|
|
15
|
+
};
|
|
25
16
|
/*
|
|
26
17
|
* `zstd` disabled due to lack of support in browsers
|
|
27
18
|
* https://github.com/Fyrd/caniuse/issues/4065
|
|
28
19
|
* https://github.com/andrew-aladev/brotli-vs-zstd
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
// Only apply encoding if it's smaller
|
|
83
|
-
if (body.length < response.body.length) {
|
|
84
|
-
response.headers['Content-Encoding'] = contentEncoding
|
|
85
|
-
response.body = body
|
|
86
|
-
response.isBase64Encoded = true
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
request.response = response
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const httpContentEncodingMiddlewareOnError = async (request) => {
|
|
93
|
-
if (typeof request.response === 'undefined') return
|
|
94
|
-
httpContentEncodingMiddlewareAfter(request)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return {
|
|
98
|
-
after: httpContentEncodingMiddlewareAfter,
|
|
99
|
-
onError: httpContentEncodingMiddlewareOnError
|
|
100
|
-
}
|
|
101
|
-
}
|
|
20
|
+
*/ const httpContentEncodingMiddleware = (opts)=>{
|
|
21
|
+
const options = {
|
|
22
|
+
...defaults,
|
|
23
|
+
...opts
|
|
24
|
+
};
|
|
25
|
+
const supportedContentEncodings = Object.keys(contentEncodingStreams);
|
|
26
|
+
const httpContentEncodingMiddlewareAfter = async (request)=>{
|
|
27
|
+
normalizeHttpResponse(request);
|
|
28
|
+
const { context: { preferredEncoding, preferredEncodings }, response } = request;
|
|
29
|
+
// Encoding not supported OR already encoded
|
|
30
|
+
if (response.isBase64Encoded || !preferredEncoding || !supportedContentEncodings.includes(preferredEncoding) || !response.body) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
let contentEncodingStream = contentEncodingStreams[preferredEncoding](options[preferredEncoding]);
|
|
34
|
+
let contentEncoding = preferredEncoding;
|
|
35
|
+
for (const encoding of options.overridePreferredEncoding){
|
|
36
|
+
if (!preferredEncodings.includes(encoding)) continue;
|
|
37
|
+
contentEncodingStream = contentEncodingStreams[encoding](options[encoding]);
|
|
38
|
+
contentEncoding = encoding;
|
|
39
|
+
break;
|
|
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
|
+
}
|
|
47
|
+
const stream = Readable.from(response.body).pipe(contentEncodingStream);
|
|
48
|
+
const chunks = [];
|
|
49
|
+
for await (const chunk of stream){
|
|
50
|
+
chunks.push(chunk);
|
|
51
|
+
}
|
|
52
|
+
// TODO update to btoa if faster
|
|
53
|
+
const body = Buffer.concat(chunks).toString('base64');
|
|
54
|
+
// Only apply encoding if it's smaller
|
|
55
|
+
if (body.length < response.body.length) {
|
|
56
|
+
response.headers['Content-Encoding'] = contentEncoding;
|
|
57
|
+
response.body = body;
|
|
58
|
+
response.isBase64Encoded = true;
|
|
59
|
+
}
|
|
60
|
+
request.response = response;
|
|
61
|
+
};
|
|
62
|
+
const httpContentEncodingMiddlewareOnError = async (request)=>{
|
|
63
|
+
if (typeof request.response === 'undefined') return;
|
|
64
|
+
httpContentEncodingMiddlewareAfter(request);
|
|
65
|
+
};
|
|
66
|
+
return {
|
|
67
|
+
after: httpContentEncodingMiddlewareAfter,
|
|
68
|
+
onError: httpContentEncodingMiddlewareOnError
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
export default httpContentEncodingMiddleware;
|
|
102
72
|
|
|
103
|
-
export default httpContentEncodingMiddleware
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-content-encoding",
|
|
3
|
-
"version": "5.0.0-
|
|
3
|
+
"version": "5.0.0-rc.0",
|
|
4
4
|
"description": "Http content encoding middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -63,10 +63,10 @@
|
|
|
63
63
|
"url": "https://github.com/sponsors/willfarrell"
|
|
64
64
|
},
|
|
65
65
|
"dependencies": {
|
|
66
|
-
"@middy/util": "5.0.0-
|
|
66
|
+
"@middy/util": "5.0.0-rc.0"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
|
-
"@middy/core": "5.0.0-
|
|
69
|
+
"@middy/core": "5.0.0-rc.0"
|
|
70
70
|
},
|
|
71
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "403c54ba9f05e038d1ee7541f9e4a7a6d46e9916"
|
|
72
72
|
}
|