@middy/http-content-encoding 4.6.1 → 4.6.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.
Files changed (3) hide show
  1. package/index.cjs +10 -1
  2. package/index.js +10 -1
  3. package/package.json +4 -4
package/index.cjs CHANGED
@@ -18,11 +18,16 @@ const contentEncodingStreams = {
18
18
  };
19
19
  const defaults = {
20
20
  br: undefined,
21
+ // zstd: undefined,
21
22
  gzip: undefined,
22
23
  deflate: undefined,
23
24
  overridePreferredEncoding: []
24
25
  };
25
- const httpContentEncodingMiddleware = (opts)=>{
26
+ /*
27
+ * `zstd` disabled due to lack of support in browsers
28
+ * https://github.com/Fyrd/caniuse/issues/4065
29
+ * https://github.com/andrew-aladev/brotli-vs-zstd
30
+ */ const httpContentEncodingMiddleware = (opts)=>{
26
31
  const options = {
27
32
  ...defaults,
28
33
  ...opts
@@ -31,6 +36,7 @@ const httpContentEncodingMiddleware = (opts)=>{
31
36
  const httpContentEncodingMiddlewareAfter = async (request)=>{
32
37
  (0, _util.normalizeHttpResponse)(request);
33
38
  const { event: { preferredEncoding, preferredEncodings }, response } = request;
39
+ // Encoding not supported OR already encoded
34
40
  if (response.isBase64Encoded || !preferredEncoding || !supportedContentEncodings.includes(preferredEncoding) || !response.body) {
35
41
  return;
36
42
  }
@@ -42,6 +48,7 @@ const httpContentEncodingMiddleware = (opts)=>{
42
48
  contentEncoding = encoding;
43
49
  break;
44
50
  }
51
+ // Support streamifyResponse
45
52
  if (response.body?._readableState) {
46
53
  request.response.headers['Content-Encoding'] = contentEncoding;
47
54
  request.response.body.pipe(contentEncodingStream);
@@ -52,7 +59,9 @@ const httpContentEncodingMiddleware = (opts)=>{
52
59
  for await (const chunk of stream){
53
60
  chunks.push(chunk);
54
61
  }
62
+ // TODO update to btoa if faster
55
63
  const body = Buffer.concat(chunks).toString('base64');
64
+ // Only apply encoding if it's smaller
56
65
  if (body.length < response.body.length) {
57
66
  response.headers['Content-Encoding'] = contentEncoding;
58
67
  response.body = body;
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
- const httpContentEncodingMiddleware = (opts)=>{
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
@@ -21,6 +26,7 @@ const httpContentEncodingMiddleware = (opts)=>{
21
26
  const httpContentEncodingMiddlewareAfter = async (request)=>{
22
27
  normalizeHttpResponse(request);
23
28
  const { event: { 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,6 +38,7 @@ const httpContentEncodingMiddleware = (opts)=>{
32
38
  contentEncoding = encoding;
33
39
  break;
34
40
  }
41
+ // Support streamifyResponse
35
42
  if (response.body?._readableState) {
36
43
  request.response.headers['Content-Encoding'] = contentEncoding;
37
44
  request.response.body.pipe(contentEncodingStream);
@@ -42,7 +49,9 @@ const httpContentEncodingMiddleware = (opts)=>{
42
49
  for await (const chunk of stream){
43
50
  chunks.push(chunk);
44
51
  }
52
+ // TODO update to btoa if faster
45
53
  const body = Buffer.concat(chunks).toString('base64');
54
+ // Only apply encoding if it's smaller
46
55
  if (body.length < response.body.length) {
47
56
  response.headers['Content-Encoding'] = contentEncoding;
48
57
  response.body = body;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-content-encoding",
3
- "version": "4.6.1",
3
+ "version": "4.6.2",
4
4
  "description": "Http content encoding middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -69,10 +69,10 @@
69
69
  "url": "https://github.com/sponsors/willfarrell"
70
70
  },
71
71
  "dependencies": {
72
- "@middy/util": "4.6.1"
72
+ "@middy/util": "4.6.2"
73
73
  },
74
74
  "devDependencies": {
75
- "@middy/core": "4.6.1"
75
+ "@middy/core": "4.6.2"
76
76
  },
77
- "gitHead": "253ed0e4ca95623decbade03938a07d837a1eba2"
77
+ "gitHead": "8b03a01abf5a9c08231ec5ced775e87f8be8f67d"
78
78
  }