@middy/http-content-encoding 3.0.0-alpha.3 → 3.0.0-alpha.7
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 +26 -16
- package/index.js +2 -118
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,26 +1,36 @@
|
|
|
1
|
-
# Middy http-content-encoding middleware
|
|
2
|
-
|
|
3
|
-
<div align="center">
|
|
4
|
-
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.png"/>
|
|
5
|
-
</div>
|
|
6
|
-
|
|
7
1
|
<div align="center">
|
|
2
|
+
<h1>Middy http-content-encoding middleware</h1>
|
|
3
|
+
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
|
|
8
4
|
<p><strong>HTTP content encoding middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
|
|
9
|
-
</div>
|
|
10
|
-
|
|
11
|
-
<div align="center">
|
|
12
5
|
<p>
|
|
13
|
-
<a href="
|
|
6
|
+
<a href="https://www.npmjs.com/package/@middy/http-content-encoding?activeTab=versions">
|
|
14
7
|
<img src="https://badge.fury.io/js/%40middy%2Fhttp-content-encoding.svg" alt="npm version" style="max-width:100%;">
|
|
15
8
|
</a>
|
|
9
|
+
<a href="https://packagephobia.com/result?p=@middy/http-content-encoding">
|
|
10
|
+
<img src="https://packagephobia.com/badge?p=@middy/http-content-encoding" alt="npm install size" style="max-width:100%;">
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://github.com/middyjs/middy/actions">
|
|
13
|
+
<img src="https://github.com/middyjs/middy/workflows/Tests/badge.svg" alt="GitHub Actions test status badge" style="max-width:100%;">
|
|
14
|
+
</a>
|
|
15
|
+
<br/>
|
|
16
|
+
<a href="https://standardjs.com/">
|
|
17
|
+
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
|
|
18
|
+
</a>
|
|
16
19
|
<a href="https://snyk.io/test/github/middyjs/middy">
|
|
17
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%;">
|
|
18
21
|
</a>
|
|
19
|
-
<a href="https://
|
|
20
|
-
<img src="https://img.shields.io/
|
|
22
|
+
<a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
|
|
23
|
+
<img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
|
|
24
|
+
</a>
|
|
25
|
+
<a href="https://bestpractices.coreinfrastructure.org/projects/5280">
|
|
26
|
+
<img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
|
|
21
27
|
</a>
|
|
28
|
+
<br/>
|
|
22
29
|
<a href="https://gitter.im/middyjs/Lobby">
|
|
23
|
-
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter"
|
|
30
|
+
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
|
|
31
|
+
</a>
|
|
32
|
+
<a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
|
|
33
|
+
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
|
|
24
34
|
</a>
|
|
25
35
|
</p>
|
|
26
36
|
</div>
|
|
@@ -36,9 +46,9 @@ npm install --save @middy/http-content-encoding
|
|
|
36
46
|
```
|
|
37
47
|
|
|
38
48
|
## Options
|
|
39
|
-
- `br` (object) (
|
|
40
|
-
- `gzip` (object) (
|
|
41
|
-
- `deflate` (object) (
|
|
49
|
+
- `br` (object) (default `{}`): `zlib.createBrotliCompress` [brotliOptions](https://nodejs.org/api/zlib.html#zlib_class_brotlioptions)
|
|
50
|
+
- `gzip` (object) (default `{}`): `zlib.createGzip` [gzipOptions](https://nodejs.org/api/zlib.html#zlib_class_options)
|
|
51
|
+
- `deflate` (object) (default `{}`): `zlib.createDeflate` [deflateOptions](https://nodejs.org/api/zlib.html#zlib_class_options)
|
|
42
52
|
- `overridePreferredEncoding` (array[string]) (optional): Override the preferred encoding order, most browsers prefer `gzip` over `br`, even though `br` has higher compression. Default: `[]`
|
|
43
53
|
|
|
44
54
|
NOTES:
|
package/index.js
CHANGED
|
@@ -1,119 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
* `zstd` disabled due to lack of support in browsers
|
|
3
|
-
* https://github.com/Fyrd/caniuse/issues/4065
|
|
4
|
-
* https://github.com/andrew-aladev/brotli-vs-zstd
|
|
5
|
-
*/
|
|
1
|
+
import stream,{Readable,Writable}from'stream';import eventEmitter from'events';import{promisify}from'util';import{createBrotliCompress,createGzip,createDeflate}from'zlib';import{normalizeHttpResponse}from'@middy/util';const contentEncodingStreams={br:(opts={})=>createBrotliCompress(opts),gzip:(opts={})=>createGzip(opts),deflate:(opts={})=>createDeflate(opts)};const defaults={br:undefined,gzip:undefined,deflate:undefined,overridePreferredEncoding:[]};const httpContentEncodingMiddleware=opts=>{const options={...defaults,...opts};const supportedContentEncodings=Object.keys(contentEncodingStreams);const httpContentEncodingMiddlewareAfter=async request=>{normalizeHttpResponse(request);const{event:{preferredEncoding,preferredEncodings},response}=request;if(response.isBase64Encoded||!preferredEncoding||!supportedContentEncodings.includes(preferredEncoding)){return}const bodyIsString=typeof response.body==='string';let contentEncodingStream=contentEncodingStreams[preferredEncoding](options[preferredEncoding]);let contentEncoding=preferredEncoding;for(const encoding of options.overridePreferredEncoding){if(!preferredEncodings.includes(encoding))continue;contentEncodingStream=contentEncodingStreams[encoding](options[encoding]);contentEncoding=encoding;break}if(bodyIsString){const readStream=Readable.from(response.body,{objectMode:false});const chunks=[];const writeStream=new Writable({write(chunk,encoding,callback){chunks.push(chunk);callback()}});await pipeline(readStream,contentEncodingStream,writeStream);const body=Buffer.concat(chunks).toString('base64');if(body.length<response.body.length){response.headers['Content-Encoding']=contentEncoding;response.body=body;response.isBase64Encoded=true}}else if(isReadableStream(response.body)){response.headers['Content-Encoding']=contentEncoding;response.body=response.body.pipe(contentEncodingStream);response.isBase64Encoded=true}request.response=response};const httpContentEncodingMiddlewareOnError=async request=>{if(request.response===undefined)return;return httpContentEncodingMiddlewareAfter(request)};return{after:httpContentEncodingMiddlewareAfter,onError:httpContentEncodingMiddlewareOnError}};const isReadableStream=stream1=>{return stream1 instanceof eventEmitter&&stream1.readable!==false};const polyfillPipelinePromise=()=>{if(process.version<'v15.0.0'){return promisify(stream.pipeline)}else{return stream.promises.pipeline}};const pipeline=polyfillPipelinePromise();export default httpContentEncodingMiddleware
|
|
6
2
|
|
|
7
|
-
|
|
8
|
-
// import {pipeline} from 'stream/promises' // available in node >=15
|
|
9
|
-
import { promisify } from 'util'
|
|
10
|
-
|
|
11
|
-
import { createBrotliCompress, createGzip, createDeflate } from 'zlib'
|
|
12
|
-
// import {ZSTDCompress as createZstdCompress} from 'simple-zstd'
|
|
13
|
-
|
|
14
|
-
import { normalizeHttpResponse } from '@middy/util'
|
|
15
|
-
const pipeline = promisify(pipelineCallback)
|
|
16
|
-
|
|
17
|
-
const contentEncodingStreams = {
|
|
18
|
-
br: (opts = {}) => createBrotliCompress(opts),
|
|
19
|
-
// zstd: (opt = {}) => createZstdCompress(opts),
|
|
20
|
-
gzip: (opts = {}) => createGzip(opts),
|
|
21
|
-
deflate: (opts = {}) => createDeflate(opts)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const defaults = {
|
|
25
|
-
br: undefined,
|
|
26
|
-
// zstd: undefined,
|
|
27
|
-
gzip: undefined,
|
|
28
|
-
deflate: undefined,
|
|
29
|
-
overridePreferredEncoding: []
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const httpContentEncodingMiddleware = (opts) => {
|
|
33
|
-
const options = { ...defaults, ...opts }
|
|
34
|
-
|
|
35
|
-
const supportedContentEncodings = Object.keys(contentEncodingStreams)
|
|
36
|
-
|
|
37
|
-
const httpContentEncodingMiddlewareAfter = async (request) => {
|
|
38
|
-
normalizeHttpResponse(request)
|
|
39
|
-
const { event: { preferredEncoding, preferredEncodings }, response } = request
|
|
40
|
-
|
|
41
|
-
// Encoding not supported OR already encoded
|
|
42
|
-
if (response.isBase64Encoded || !preferredEncoding || !supportedContentEncodings.includes(preferredEncoding)) { return }
|
|
43
|
-
|
|
44
|
-
const bodyIsString = typeof response.body === 'string'
|
|
45
|
-
|
|
46
|
-
let contentEncodingStream = contentEncodingStreams[preferredEncoding](options[preferredEncoding])
|
|
47
|
-
let contentEncoding = preferredEncoding
|
|
48
|
-
for (const encoding of options.overridePreferredEncoding) {
|
|
49
|
-
if (!preferredEncodings.includes(encoding)) continue
|
|
50
|
-
contentEncodingStream = contentEncodingStreams[encoding](options[encoding])
|
|
51
|
-
contentEncoding = encoding
|
|
52
|
-
break
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (bodyIsString) {
|
|
56
|
-
const readStream = Readable.from(response.body, { objectMode: false })
|
|
57
|
-
|
|
58
|
-
const chunks = []
|
|
59
|
-
const writeStream = new Writable({
|
|
60
|
-
write (chunk, encoding, callback) {
|
|
61
|
-
chunks.push(chunk)
|
|
62
|
-
callback()
|
|
63
|
-
}
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
await pipeline(
|
|
67
|
-
readStream,
|
|
68
|
-
contentEncodingStream,
|
|
69
|
-
writeStream
|
|
70
|
-
)
|
|
71
|
-
|
|
72
|
-
const body = Buffer.concat(chunks).toString('base64')
|
|
73
|
-
|
|
74
|
-
// Only apply encoding if it's smaller
|
|
75
|
-
if (body.length < response.body.length) {
|
|
76
|
-
response.headers['Content-Encoding'] = contentEncoding
|
|
77
|
-
response.body = body
|
|
78
|
-
response.isBase64Encoded = true
|
|
79
|
-
}
|
|
80
|
-
} else if (isReadableStream(response.body)) {
|
|
81
|
-
// Note: body to not cast to string to allow stream chaining
|
|
82
|
-
response.headers['Content-Encoding'] = contentEncoding
|
|
83
|
-
response.body = response.body.pipe(contentEncodingStream)
|
|
84
|
-
response.isBase64Encoded = true
|
|
85
|
-
}
|
|
86
|
-
request.response = response
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const httpContentEncodingMiddlewareOnError = async (request) => {
|
|
90
|
-
if (request.response === undefined) return
|
|
91
|
-
return httpContentEncodingMiddlewareAfter(request)
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return {
|
|
95
|
-
after: httpContentEncodingMiddlewareAfter,
|
|
96
|
-
onError: httpContentEncodingMiddlewareOnError
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const isReadableStream = (stream) =>
|
|
101
|
-
typeof stream?.pipe === 'function' &&
|
|
102
|
-
stream?.readable !== false &&
|
|
103
|
-
typeof stream?._read === 'function' &&
|
|
104
|
-
typeof stream?._readableState === 'object'
|
|
105
|
-
|
|
106
|
-
const polyfillPipelinePromise = async () => {
|
|
107
|
-
if (process.version < 'v15.0.0') {
|
|
108
|
-
const stream = await import('stream');
|
|
109
|
-
const util = await import('util');
|
|
110
|
-
return util.promisify(stream.pipeline);
|
|
111
|
-
} else {
|
|
112
|
-
const stream = await import('stream/promises')
|
|
113
|
-
return stream.pipeline
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
global.pipeline = await polyfillPipelinePromise()
|
|
117
|
-
// import {pipeline} from 'stream/promises'
|
|
118
|
-
|
|
119
|
-
export default httpContentEncodingMiddleware
|
|
3
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-content-encoding",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.7",
|
|
4
4
|
"description": "Http content encoding middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -51,10 +51,10 @@
|
|
|
51
51
|
},
|
|
52
52
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@middy/util": "^3.0.0-alpha.
|
|
54
|
+
"@middy/util": "^3.0.0-alpha.7"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@middy/core": "^3.0.0-alpha.
|
|
57
|
+
"@middy/core": "^3.0.0-alpha.7"
|
|
58
58
|
},
|
|
59
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "5cef39ebe49c201f97d71bb0680004de4b82cb91"
|
|
60
60
|
}
|