@middy/http-content-encoding 5.0.3 → 5.2.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.
Files changed (2) hide show
  1. package/index.js +100 -60
  2. package/package.json +4 -4
package/index.js CHANGED
@@ -1,63 +1,103 @@
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';
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
+
4
11
  const contentEncodingStreams = {
5
- br: brotliCompressStream,
6
- gzip: gzipCompressStream,
7
- deflate: deflateCompressStream
8
- };
12
+ br: brotliCompressStream,
13
+ gzip: gzipCompressStream,
14
+ deflate: deflateCompressStream
15
+ }
16
+
9
17
  const defaults = {
10
- br: undefined,
11
- gzip: undefined,
12
- deflate: undefined,
13
- overridePreferredEncoding: []
14
- };
15
- const httpContentEncodingMiddleware = (opts)=>{
16
- const options = {
17
- ...defaults,
18
- ...opts
19
- };
20
- const supportedContentEncodings = Object.keys(contentEncodingStreams);
21
- const httpContentEncodingMiddlewareAfter = async (request)=>{
22
- normalizeHttpResponse(request);
23
- const { context: { preferredEncoding, preferredEncodings }, response } = request;
24
- if (response.isBase64Encoded || !preferredEncoding || !supportedContentEncodings.includes(preferredEncoding) || !response.body) {
25
- return;
26
- }
27
- let contentEncodingStream = contentEncodingStreams[preferredEncoding](options[preferredEncoding]);
28
- let contentEncoding = preferredEncoding;
29
- for (const encoding of options.overridePreferredEncoding){
30
- if (!preferredEncodings.includes(encoding)) continue;
31
- contentEncodingStream = contentEncodingStreams[encoding](options[encoding]);
32
- contentEncoding = encoding;
33
- break;
34
- }
35
- if (response.body?._readableState) {
36
- request.response.headers['Content-Encoding'] = contentEncoding;
37
- request.response.body.pipe(contentEncodingStream);
38
- return;
39
- }
40
- const stream = Readable.from(response.body).pipe(contentEncodingStream);
41
- const chunks = [];
42
- for await (const chunk of stream){
43
- chunks.push(chunk);
44
- }
45
- const body = Buffer.concat(chunks).toString('base64');
46
- if (body.length < response.body.length) {
47
- response.headers['Content-Encoding'] = contentEncoding;
48
- response.body = body;
49
- response.isBase64Encoded = true;
50
- }
51
- request.response = response;
52
- };
53
- const httpContentEncodingMiddlewareOnError = async (request)=>{
54
- if (typeof request.response === 'undefined') return;
55
- httpContentEncodingMiddlewareAfter(request);
56
- };
57
- return {
58
- after: httpContentEncodingMiddlewareAfter,
59
- onError: httpContentEncodingMiddlewareOnError
60
- };
61
- };
62
- export default httpContentEncodingMiddleware;
18
+ br: undefined,
19
+ // zstd: undefined,
20
+ gzip: undefined,
21
+ deflate: undefined,
22
+ overridePreferredEncoding: []
23
+ }
24
+
25
+ /*
26
+ * `zstd` disabled due to lack of support in browsers
27
+ * https://github.com/Fyrd/caniuse/issues/4065
28
+ * https://github.com/andrew-aladev/brotli-vs-zstd
29
+ */
30
+
31
+ const httpContentEncodingMiddleware = (opts) => {
32
+ const options = { ...defaults, ...opts }
33
+
34
+ const supportedContentEncodings = Object.keys(contentEncodingStreams)
35
+
36
+ const httpContentEncodingMiddlewareAfter = async (request) => {
37
+ normalizeHttpResponse(request)
38
+ const {
39
+ context: { preferredEncoding, preferredEncodings },
40
+ response
41
+ } = request
42
+
43
+ // Encoding not supported OR already encoded
44
+ if (
45
+ response.isBase64Encoded ||
46
+ !preferredEncoding ||
47
+ !supportedContentEncodings.includes(preferredEncoding) ||
48
+ !response.body
49
+ ) {
50
+ return
51
+ }
52
+
53
+ let contentEncodingStream = contentEncodingStreams[preferredEncoding](
54
+ options[preferredEncoding]
55
+ )
56
+ let contentEncoding = preferredEncoding
57
+ for (const encoding of options.overridePreferredEncoding) {
58
+ if (!preferredEncodings.includes(encoding)) continue
59
+ contentEncodingStream = contentEncodingStreams[encoding](
60
+ options[encoding]
61
+ )
62
+ contentEncoding = encoding
63
+ break
64
+ }
65
+
66
+ // Support streamifyResponse
67
+ if (response.body?._readableState) {
68
+ request.response.headers['Content-Encoding'] = contentEncoding
69
+ request.response.body.pipe(contentEncodingStream)
70
+ return
71
+ }
72
+
73
+ const stream = Readable.from(response.body).pipe(contentEncodingStream)
74
+
75
+ const chunks = []
76
+ for await (const chunk of stream) {
77
+ chunks.push(chunk)
78
+ }
79
+ // TODO update to btoa if faster
80
+ const body = Buffer.concat(chunks).toString('base64')
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
+ }
63
102
 
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.3",
3
+ "version": "5.2.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.3"
66
+ "@middy/util": "5.2.0"
67
67
  },
68
68
  "devDependencies": {
69
- "@middy/core": "5.0.3"
69
+ "@middy/core": "5.2.0"
70
70
  },
71
- "gitHead": "87660575a7ac2b52e4153c407a4c63c9449dcd0d"
71
+ "gitHead": "2d9096a49cd8fb62359517be96d6c93609df41f0"
72
72
  }