@middy/http-content-encoding 3.0.0-alpha.5 → 3.0.0-alpha.8
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.cjs +3 -0
- package/index.js +2 -98
- package/package.json +13 -6
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.cjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _stream=_interopRequireWildcard(require("stream"));var _events=_interopRequireDefault(require("events"));var _util=require("util");var _zlib=require("zlib");var _util1=require("@middy/util");function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function _interopRequireWildcard(obj){if(obj&&obj.__esModule){return obj}else{var newObj={};if(obj!=null){for(var key in obj){if(Object.prototype.hasOwnProperty.call(obj,key)){var desc=Object.defineProperty&&Object.getOwnPropertyDescriptor?Object.getOwnPropertyDescriptor(obj,key):{};if(desc.get||desc.set){Object.defineProperty(newObj,key,desc)}else{newObj[key]=obj[key]}}}}newObj.default=obj;return newObj}}const contentEncodingStreams={br:(opts={})=>(0,_zlib).createBrotliCompress(opts),gzip:(opts={})=>(0,_zlib).createGzip(opts),deflate:(opts={})=>(0,_zlib).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=>{(0,_util1).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=_stream.Readable.from(response.body,{objectMode:false});const chunks=[];const writeStream=new _stream.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=stream=>{return stream instanceof _events.default&&stream.readable!==false};const polyfillPipelinePromise=()=>{if(process.version<"v15.0.0"){return(0,_util).promisify(_stream.default.pipeline)}else{return _stream.default.promises.pipeline}};const pipeline=polyfillPipelinePromise();var _default=httpContentEncodingMiddleware;exports.default=_default
|
|
2
|
+
|
|
3
|
+
//# sourceMappingURL=index.cjs.map
|
package/index.js
CHANGED
|
@@ -1,99 +1,3 @@
|
|
|
1
|
-
import stream, { Readable, Writable }
|
|
2
|
-
import { promisify } from 'util';
|
|
3
|
-
import { createBrotliCompress, createGzip, createDeflate } from 'zlib';
|
|
4
|
-
import { normalizeHttpResponse } from '@middy/util';
|
|
5
|
-
const contentEncodingStreams = {
|
|
6
|
-
br: (opts = {}) => createBrotliCompress(opts),
|
|
7
|
-
gzip: (opts = {}) => createGzip(opts),
|
|
8
|
-
deflate: (opts = {}) => createDeflate(opts)
|
|
9
|
-
};
|
|
10
|
-
const defaults = {
|
|
11
|
-
br: undefined,
|
|
12
|
-
gzip: undefined,
|
|
13
|
-
deflate: undefined,
|
|
14
|
-
overridePreferredEncoding: []
|
|
15
|
-
};
|
|
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
|
|
16
2
|
|
|
17
|
-
|
|
18
|
-
const options = { ...defaults,
|
|
19
|
-
...opts
|
|
20
|
-
};
|
|
21
|
-
const supportedContentEncodings = Object.keys(contentEncodingStreams);
|
|
22
|
-
|
|
23
|
-
const httpContentEncodingMiddlewareAfter = async request => {
|
|
24
|
-
normalizeHttpResponse(request);
|
|
25
|
-
const {
|
|
26
|
-
event: {
|
|
27
|
-
preferredEncoding,
|
|
28
|
-
preferredEncodings
|
|
29
|
-
},
|
|
30
|
-
response
|
|
31
|
-
} = request;
|
|
32
|
-
|
|
33
|
-
if (response.isBase64Encoded || !preferredEncoding || !supportedContentEncodings.includes(preferredEncoding)) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const bodyIsString = typeof response.body === 'string';
|
|
38
|
-
let contentEncodingStream = contentEncodingStreams[preferredEncoding](options[preferredEncoding]);
|
|
39
|
-
let contentEncoding = preferredEncoding;
|
|
40
|
-
|
|
41
|
-
for (const encoding of options.overridePreferredEncoding) {
|
|
42
|
-
if (!preferredEncodings.includes(encoding)) continue;
|
|
43
|
-
contentEncodingStream = contentEncodingStreams[encoding](options[encoding]);
|
|
44
|
-
contentEncoding = encoding;
|
|
45
|
-
break;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (bodyIsString) {
|
|
49
|
-
const readStream = Readable.from(response.body, {
|
|
50
|
-
objectMode: false
|
|
51
|
-
});
|
|
52
|
-
const chunks = [];
|
|
53
|
-
const writeStream = new Writable({
|
|
54
|
-
write(chunk, encoding, callback) {
|
|
55
|
-
chunks.push(chunk);
|
|
56
|
-
callback();
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
});
|
|
60
|
-
await pipeline(readStream, contentEncodingStream, writeStream);
|
|
61
|
-
const body = Buffer.concat(chunks).toString('base64');
|
|
62
|
-
|
|
63
|
-
if (body.length < response.body.length) {
|
|
64
|
-
response.headers['Content-Encoding'] = contentEncoding;
|
|
65
|
-
response.body = body;
|
|
66
|
-
response.isBase64Encoded = true;
|
|
67
|
-
}
|
|
68
|
-
} else if (isReadableStream(response.body)) {
|
|
69
|
-
response.headers['Content-Encoding'] = contentEncoding;
|
|
70
|
-
response.body = response.body.pipe(contentEncodingStream);
|
|
71
|
-
response.isBase64Encoded = true;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
request.response = response;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
const httpContentEncodingMiddlewareOnError = async request => {
|
|
78
|
-
if (request.response === undefined) return;
|
|
79
|
-
return httpContentEncodingMiddlewareAfter(request);
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
return {
|
|
83
|
-
after: httpContentEncodingMiddlewareAfter,
|
|
84
|
-
onError: httpContentEncodingMiddlewareOnError
|
|
85
|
-
};
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
const isReadableStream = stream => typeof (stream === null || stream === void 0 ? void 0 : stream.pipe) === 'function' && (stream === null || stream === void 0 ? void 0 : stream.readable) !== false && typeof (stream === null || stream === void 0 ? void 0 : stream._read) === 'function' && typeof (stream === null || stream === void 0 ? void 0 : stream._readableState) === 'object';
|
|
89
|
-
|
|
90
|
-
const polyfillPipelinePromise = () => {
|
|
91
|
-
if (process.version < 'v15.0.0') {
|
|
92
|
-
return promisify(stream.pipeline);
|
|
93
|
-
} else {
|
|
94
|
-
return stream.promises.pipeline;
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
const pipeline = polyfillPipelinePromise();
|
|
99
|
-
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.8",
|
|
4
4
|
"description": "Http content encoding middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -10,10 +10,17 @@
|
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"exports":
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./index.js",
|
|
16
|
+
"require": "./index.cjs",
|
|
17
|
+
"types": "./index.d.ts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
14
20
|
"types": "index.d.ts",
|
|
15
21
|
"files": [
|
|
16
22
|
"index.js",
|
|
23
|
+
"index.cjs",
|
|
17
24
|
"index.d.ts"
|
|
18
25
|
],
|
|
19
26
|
"scripts": {
|
|
@@ -49,12 +56,12 @@
|
|
|
49
56
|
"bugs": {
|
|
50
57
|
"url": "https://github.com/middyjs/middy/issues"
|
|
51
58
|
},
|
|
52
|
-
"homepage": "https://
|
|
59
|
+
"homepage": "https://middy.js.org",
|
|
53
60
|
"dependencies": {
|
|
54
|
-
"@middy/util": "^3.0.0-alpha.
|
|
61
|
+
"@middy/util": "^3.0.0-alpha.8"
|
|
55
62
|
},
|
|
56
63
|
"devDependencies": {
|
|
57
|
-
"@middy/core": "^3.0.0-alpha.
|
|
64
|
+
"@middy/core": "^3.0.0-alpha.8"
|
|
58
65
|
},
|
|
59
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "c04b3a0c2f326906b34973878d6f823778a5ea99"
|
|
60
67
|
}
|