@middy/http-content-encoding 4.6.5 → 5.0.0-alpha.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.js +96 -65
- package/package.json +5 -11
- package/index.cjs +0 -82
package/index.js
CHANGED
|
@@ -1,72 +1,103 @@
|
|
|
1
|
-
import { Readable } from 'node:stream'
|
|
2
|
-
|
|
3
|
-
import {
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
12
|
+
br: brotliCompressStream,
|
|
13
|
+
gzip: gzipCompressStream,
|
|
14
|
+
deflate: deflateCompressStream
|
|
15
|
+
}
|
|
16
|
+
|
|
9
17
|
const defaults = {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
18
|
+
br: undefined,
|
|
19
|
+
// zstd: undefined,
|
|
20
|
+
gzip: undefined,
|
|
21
|
+
deflate: undefined,
|
|
22
|
+
overridePreferredEncoding: []
|
|
23
|
+
}
|
|
24
|
+
|
|
16
25
|
/*
|
|
17
26
|
* `zstd` disabled due to lack of support in browsers
|
|
18
27
|
* https://github.com/Fyrd/caniuse/issues/4065
|
|
19
28
|
* https://github.com/andrew-aladev/brotli-vs-zstd
|
|
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
|
-
|
|
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
|
+
}
|
|
72
102
|
|
|
103
|
+
export default httpContentEncodingMiddleware
|
package/package.json
CHANGED
|
@@ -1,33 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-content-encoding",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0-alpha.1",
|
|
4
4
|
"description": "Http content encoding middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=18"
|
|
8
8
|
},
|
|
9
9
|
"engineStrict": true,
|
|
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": "
|
|
66
|
+
"@middy/util": "5.0.0-alpha.1"
|
|
73
67
|
},
|
|
74
68
|
"devDependencies": {
|
|
75
|
-
"@middy/core": "
|
|
69
|
+
"@middy/core": "5.0.0-alpha.1"
|
|
76
70
|
},
|
|
77
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
|
|
78
72
|
}
|
package/index.cjs
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(module, "exports", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return _default;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _nodestream = require("node:stream");
|
|
12
|
-
const _nodezlib = require("node:zlib");
|
|
13
|
-
const _util = require("@middy/util");
|
|
14
|
-
const contentEncodingStreams = {
|
|
15
|
-
br: _nodezlib.createBrotliCompress,
|
|
16
|
-
gzip: _nodezlib.createGzip,
|
|
17
|
-
deflate: _nodezlib.createDeflate
|
|
18
|
-
};
|
|
19
|
-
const defaults = {
|
|
20
|
-
br: undefined,
|
|
21
|
-
// zstd: undefined,
|
|
22
|
-
gzip: undefined,
|
|
23
|
-
deflate: undefined,
|
|
24
|
-
overridePreferredEncoding: []
|
|
25
|
-
};
|
|
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)=>{
|
|
31
|
-
const options = {
|
|
32
|
-
...defaults,
|
|
33
|
-
...opts
|
|
34
|
-
};
|
|
35
|
-
const supportedContentEncodings = Object.keys(contentEncodingStreams);
|
|
36
|
-
const httpContentEncodingMiddlewareAfter = async (request)=>{
|
|
37
|
-
(0, _util.normalizeHttpResponse)(request);
|
|
38
|
-
const { event: { preferredEncoding, preferredEncodings }, response } = request;
|
|
39
|
-
// Encoding not supported OR already encoded
|
|
40
|
-
if (response.isBase64Encoded || !preferredEncoding || !supportedContentEncodings.includes(preferredEncoding) || !response.body) {
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
let contentEncodingStream = contentEncodingStreams[preferredEncoding](options[preferredEncoding]);
|
|
44
|
-
let contentEncoding = preferredEncoding;
|
|
45
|
-
for (const encoding of options.overridePreferredEncoding){
|
|
46
|
-
if (!preferredEncodings.includes(encoding)) continue;
|
|
47
|
-
contentEncodingStream = contentEncodingStreams[encoding](options[encoding]);
|
|
48
|
-
contentEncoding = encoding;
|
|
49
|
-
break;
|
|
50
|
-
}
|
|
51
|
-
// Support streamifyResponse
|
|
52
|
-
if (response.body?._readableState) {
|
|
53
|
-
request.response.headers['Content-Encoding'] = contentEncoding;
|
|
54
|
-
request.response.body.pipe(contentEncodingStream);
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
const stream = _nodestream.Readable.from(response.body).pipe(contentEncodingStream);
|
|
58
|
-
const chunks = [];
|
|
59
|
-
for await (const chunk of stream){
|
|
60
|
-
chunks.push(chunk);
|
|
61
|
-
}
|
|
62
|
-
// TODO update to btoa if faster
|
|
63
|
-
const body = Buffer.concat(chunks).toString('base64');
|
|
64
|
-
// Only apply encoding if it's smaller
|
|
65
|
-
if (body.length < response.body.length) {
|
|
66
|
-
response.headers['Content-Encoding'] = contentEncoding;
|
|
67
|
-
response.body = body;
|
|
68
|
-
response.isBase64Encoded = true;
|
|
69
|
-
}
|
|
70
|
-
request.response = response;
|
|
71
|
-
};
|
|
72
|
-
const httpContentEncodingMiddlewareOnError = async (request)=>{
|
|
73
|
-
if (typeof request.response === 'undefined') return;
|
|
74
|
-
httpContentEncodingMiddlewareAfter(request);
|
|
75
|
-
};
|
|
76
|
-
return {
|
|
77
|
-
after: httpContentEncodingMiddlewareAfter,
|
|
78
|
-
onError: httpContentEncodingMiddlewareOnError
|
|
79
|
-
};
|
|
80
|
-
};
|
|
81
|
-
const _default = httpContentEncodingMiddleware;
|
|
82
|
-
|